comparison src/memfile.c @ 18800:f41b55f9357c v8.1.2388

patch 8.1.2388: using old C style comments Commit: https://github.com/vim/vim/commit/4ba37b5833de99db9e9afe8928b31c864182405c Author: Bram Moolenaar <Bram@vim.org> Date: Wed Dec 4 21:57:43 2019 +0100 patch 8.1.2388: using old C style comments Problem: Using old C style comments. Solution: Use // comments where appropriate.
author Bram Moolenaar <Bram@vim.org>
date Wed, 04 Dec 2019 22:00:04 +0100
parents ce04ebdf26b8
children aadd1cae2ff5
comparison
equal deleted inserted replaced
18799:d7d0942e231b 18800:f41b55f9357c
44 #else 44 #else
45 # ifdef HAVE_SYS_STATFS_H 45 # ifdef HAVE_SYS_STATFS_H
46 # include <sys/statfs.h> 46 # include <sys/statfs.h>
47 # define STATFS statfs 47 # define STATFS statfs
48 # define F_BSIZE f_bsize 48 # define F_BSIZE f_bsize
49 # ifdef __MINT__ /* do we still need this? */ 49 # ifdef __MINT__ // do we still need this?
50 # define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf)) 50 # define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
51 # endif 51 # endif
52 # endif 52 # endif
53 #endif 53 #endif
54 54
55 /* 55 /*
56 * for Amiga Dos 2.0x we use Flush 56 * for Amiga Dos 2.0x we use Flush
57 */ 57 */
58 #ifdef AMIGA 58 #ifdef AMIGA
59 # ifdef FEAT_ARP 59 # ifdef FEAT_ARP
60 extern int dos2; /* this is in os_amiga.c */ 60 extern int dos2; // this is in os_amiga.c
61 # endif 61 # endif
62 # ifdef SASC 62 # ifdef SASC
63 # include <proto/dos.h> 63 # include <proto/dos.h>
64 # include <ios1.h> /* for chkufb() */ 64 # include <ios1.h> // for chkufb()
65 # endif 65 # endif
66 #endif 66 #endif
67 67
68 #define MEMFILE_PAGE_SIZE 4096 /* default page size */ 68 #define MEMFILE_PAGE_SIZE 4096 // default page size
69 69
70 static long_u total_mem_used = 0; /* total memory used for memfiles */ 70 static long_u total_mem_used = 0; // total memory used for memfiles
71 71
72 static void mf_ins_hash(memfile_T *, bhdr_T *); 72 static void mf_ins_hash(memfile_T *, bhdr_T *);
73 static void mf_rem_hash(memfile_T *, bhdr_T *); 73 static void mf_rem_hash(memfile_T *, bhdr_T *);
74 static bhdr_T *mf_find_hash(memfile_T *, blocknr_T); 74 static bhdr_T *mf_find_hash(memfile_T *, blocknr_T);
75 static void mf_ins_used(memfile_T *, bhdr_T *); 75 static void mf_ins_used(memfile_T *, bhdr_T *);
131 #endif 131 #endif
132 132
133 if ((mfp = ALLOC_ONE(memfile_T)) == NULL) 133 if ((mfp = ALLOC_ONE(memfile_T)) == NULL)
134 return NULL; 134 return NULL;
135 135
136 if (fname == NULL) /* no file for this memfile, use memory only */ 136 if (fname == NULL) // no file for this memfile, use memory only
137 { 137 {
138 mfp->mf_fname = NULL; 138 mfp->mf_fname = NULL;
139 mfp->mf_ffname = NULL; 139 mfp->mf_ffname = NULL;
140 mfp->mf_fd = -1; 140 mfp->mf_fd = -1;
141 } 141 }
142 else 142 else
143 { 143 {
144 mf_do_open(mfp, fname, flags); /* try to open the file */ 144 mf_do_open(mfp, fname, flags); // try to open the file
145 145
146 /* if the file cannot be opened, return here */ 146 // if the file cannot be opened, return here
147 if (mfp->mf_fd < 0) 147 if (mfp->mf_fd < 0)
148 { 148 {
149 vim_free(mfp); 149 vim_free(mfp);
150 return NULL; 150 return NULL;
151 } 151 }
152 } 152 }
153 153
154 mfp->mf_free_first = NULL; /* free list is empty */ 154 mfp->mf_free_first = NULL; // free list is empty
155 mfp->mf_used_first = NULL; /* used list is empty */ 155 mfp->mf_used_first = NULL; // used list is empty
156 mfp->mf_used_last = NULL; 156 mfp->mf_used_last = NULL;
157 mfp->mf_dirty = FALSE; 157 mfp->mf_dirty = FALSE;
158 mfp->mf_used_count = 0; 158 mfp->mf_used_count = 0;
159 mf_hash_init(&mfp->mf_hash); 159 mf_hash_init(&mfp->mf_hash);
160 mf_hash_init(&mfp->mf_trans); 160 mf_hash_init(&mfp->mf_trans);
178 mfp->mf_page_size = stf.F_BSIZE; 178 mfp->mf_page_size = stf.F_BSIZE;
179 #endif 179 #endif
180 180
181 if (mfp->mf_fd < 0 || (flags & (O_TRUNC|O_EXCL)) 181 if (mfp->mf_fd < 0 || (flags & (O_TRUNC|O_EXCL))
182 || (size = vim_lseek(mfp->mf_fd, (off_T)0L, SEEK_END)) <= 0) 182 || (size = vim_lseek(mfp->mf_fd, (off_T)0L, SEEK_END)) <= 0)
183 mfp->mf_blocknr_max = 0; /* no file or empty file */ 183 mfp->mf_blocknr_max = 0; // no file or empty file
184 else 184 else
185 mfp->mf_blocknr_max = (blocknr_T)((size + mfp->mf_page_size - 1) 185 mfp->mf_blocknr_max = (blocknr_T)((size + mfp->mf_page_size - 1)
186 / mfp->mf_page_size); 186 / mfp->mf_page_size);
187 mfp->mf_blocknr_min = -1; 187 mfp->mf_blocknr_min = -1;
188 mfp->mf_neg_count = 0; 188 mfp->mf_neg_count = 0;
221 * return value: FAIL if file could not be opened, OK otherwise 221 * return value: FAIL if file could not be opened, OK otherwise
222 */ 222 */
223 int 223 int
224 mf_open_file(memfile_T *mfp, char_u *fname) 224 mf_open_file(memfile_T *mfp, char_u *fname)
225 { 225 {
226 mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); /* try to open the file */ 226 mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); // try to open the file
227 227
228 if (mfp->mf_fd < 0) 228 if (mfp->mf_fd < 0)
229 return FAIL; 229 return FAIL;
230 230
231 mfp->mf_dirty = TRUE; 231 mfp->mf_dirty = TRUE;
238 void 238 void
239 mf_close(memfile_T *mfp, int del_file) 239 mf_close(memfile_T *mfp, int del_file)
240 { 240 {
241 bhdr_T *hp, *nextp; 241 bhdr_T *hp, *nextp;
242 242
243 if (mfp == NULL) /* safety check */ 243 if (mfp == NULL) // safety check
244 return; 244 return;
245 if (mfp->mf_fd >= 0) 245 if (mfp->mf_fd >= 0)
246 { 246 {
247 if (close(mfp->mf_fd) < 0) 247 if (close(mfp->mf_fd) < 0)
248 emsg(_(e_swapclose)); 248 emsg(_(e_swapclose));
249 } 249 }
250 if (del_file && mfp->mf_fname != NULL) 250 if (del_file && mfp->mf_fname != NULL)
251 mch_remove(mfp->mf_fname); 251 mch_remove(mfp->mf_fname);
252 /* free entries in used list */ 252 // free entries in used list
253 for (hp = mfp->mf_used_first; hp != NULL; hp = nextp) 253 for (hp = mfp->mf_used_first; hp != NULL; hp = nextp)
254 { 254 {
255 total_mem_used -= hp->bh_page_count * mfp->mf_page_size; 255 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
256 nextp = hp->bh_next; 256 nextp = hp->bh_next;
257 mf_free_bhdr(hp); 257 mf_free_bhdr(hp);
258 } 258 }
259 while (mfp->mf_free_first != NULL) /* free entries in free list */ 259 while (mfp->mf_free_first != NULL) // free entries in free list
260 vim_free(mf_rem_free(mfp)); 260 vim_free(mf_rem_free(mfp));
261 mf_hash_free(&mfp->mf_hash); 261 mf_hash_free(&mfp->mf_hash);
262 mf_hash_free_all(&mfp->mf_trans); /* free hashtable and its items */ 262 mf_hash_free_all(&mfp->mf_trans); // free hashtable and its items
263 vim_free(mfp->mf_fname); 263 vim_free(mfp->mf_fname);
264 vim_free(mfp->mf_ffname); 264 vim_free(mfp->mf_ffname);
265 vim_free(mfp); 265 vim_free(mfp);
266 } 266 }
267 267
269 * Close the swap file for a memfile. Used when 'swapfile' is reset. 269 * Close the swap file for a memfile. Used when 'swapfile' is reset.
270 */ 270 */
271 void 271 void
272 mf_close_file( 272 mf_close_file(
273 buf_T *buf, 273 buf_T *buf,
274 int getlines) /* get all lines into memory? */ 274 int getlines) // get all lines into memory?
275 { 275 {
276 memfile_T *mfp; 276 memfile_T *mfp;
277 linenr_T lnum; 277 linenr_T lnum;
278 278
279 mfp = buf->b_ml.ml_mfp; 279 mfp = buf->b_ml.ml_mfp;
280 if (mfp == NULL || mfp->mf_fd < 0) /* nothing to close */ 280 if (mfp == NULL || mfp->mf_fd < 0) // nothing to close
281 return; 281 return;
282 282
283 if (getlines) 283 if (getlines)
284 { 284 {
285 /* get all blocks in memory by accessing all lines (clumsy!) */ 285 // get all blocks in memory by accessing all lines (clumsy!)
286 mf_dont_release = TRUE; 286 mf_dont_release = TRUE;
287 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum) 287 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
288 (void)ml_get_buf(buf, lnum, FALSE); 288 (void)ml_get_buf(buf, lnum, FALSE);
289 mf_dont_release = FALSE; 289 mf_dont_release = FALSE;
290 /* TODO: should check if all blocks are really in core */ 290 // TODO: should check if all blocks are really in core
291 } 291 }
292 292
293 if (close(mfp->mf_fd) < 0) /* close the file */ 293 if (close(mfp->mf_fd) < 0) // close the file
294 emsg(_(e_swapclose)); 294 emsg(_(e_swapclose));
295 mfp->mf_fd = -1; 295 mfp->mf_fd = -1;
296 296
297 if (mfp->mf_fname != NULL) 297 if (mfp->mf_fname != NULL)
298 { 298 {
299 mch_remove(mfp->mf_fname); /* delete the swap file */ 299 mch_remove(mfp->mf_fname); // delete the swap file
300 VIM_CLEAR(mfp->mf_fname); 300 VIM_CLEAR(mfp->mf_fname);
301 VIM_CLEAR(mfp->mf_ffname); 301 VIM_CLEAR(mfp->mf_ffname);
302 } 302 }
303 } 303 }
304 304
307 * and the size it indicates differs from what was guessed. 307 * and the size it indicates differs from what was guessed.
308 */ 308 */
309 void 309 void
310 mf_new_page_size(memfile_T *mfp, unsigned new_size) 310 mf_new_page_size(memfile_T *mfp, unsigned new_size)
311 { 311 {
312 /* Correct the memory used for block 0 to the new size, because it will be 312 // Correct the memory used for block 0 to the new size, because it will be
313 * freed with that size later on. */ 313 // freed with that size later on.
314 total_mem_used += new_size - mfp->mf_page_size; 314 total_mem_used += new_size - mfp->mf_page_size;
315 mfp->mf_page_size = new_size; 315 mfp->mf_page_size = new_size;
316 } 316 }
317 317
318 /* 318 /*
321 * negative: TRUE if negative block number desired (data block) 321 * negative: TRUE if negative block number desired (data block)
322 */ 322 */
323 bhdr_T * 323 bhdr_T *
324 mf_new(memfile_T *mfp, int negative, int page_count) 324 mf_new(memfile_T *mfp, int negative, int page_count)
325 { 325 {
326 bhdr_T *hp; /* new bhdr_T */ 326 bhdr_T *hp; // new bhdr_T
327 bhdr_T *freep; /* first block in free list */ 327 bhdr_T *freep; // first block in free list
328 char_u *p; 328 char_u *p;
329 329
330 /* 330 /*
331 * If we reached the maximum size for the used memory blocks, release one 331 * If we reached the maximum size for the used memory blocks, release one
332 * If a bhdr_T is returned, use it and adjust the page_count if necessary. 332 * If a bhdr_T is returned, use it and adjust the page_count if necessary.
358 return NULL; 358 return NULL;
359 hp->bh_bnum = freep->bh_bnum; 359 hp->bh_bnum = freep->bh_bnum;
360 freep->bh_bnum += page_count; 360 freep->bh_bnum += page_count;
361 freep->bh_page_count -= page_count; 361 freep->bh_page_count -= page_count;
362 } 362 }
363 else if (hp == NULL) /* need to allocate memory for this block */ 363 else if (hp == NULL) // need to allocate memory for this block
364 { 364 {
365 if ((p = alloc(mfp->mf_page_size * page_count)) == NULL) 365 if ((p = alloc(mfp->mf_page_size * page_count)) == NULL)
366 return NULL; 366 return NULL;
367 hp = mf_rem_free(mfp); 367 hp = mf_rem_free(mfp);
368 hp->bh_data = p; 368 hp->bh_data = p;
369 } 369 }
370 else /* use the number, remove entry from free list */ 370 else // use the number, remove entry from free list
371 { 371 {
372 freep = mf_rem_free(mfp); 372 freep = mf_rem_free(mfp);
373 hp->bh_bnum = freep->bh_bnum; 373 hp->bh_bnum = freep->bh_bnum;
374 vim_free(freep); 374 vim_free(freep);
375 } 375 }
376 } 376 }
377 else /* get a new number */ 377 else // get a new number
378 { 378 {
379 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL) 379 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
380 return NULL; 380 return NULL;
381 if (negative) 381 if (negative)
382 { 382 {
387 { 387 {
388 hp->bh_bnum = mfp->mf_blocknr_max; 388 hp->bh_bnum = mfp->mf_blocknr_max;
389 mfp->mf_blocknr_max += page_count; 389 mfp->mf_blocknr_max += page_count;
390 } 390 }
391 } 391 }
392 hp->bh_flags = BH_LOCKED | BH_DIRTY; /* new block is always dirty */ 392 hp->bh_flags = BH_LOCKED | BH_DIRTY; // new block is always dirty
393 mfp->mf_dirty = TRUE; 393 mfp->mf_dirty = TRUE;
394 hp->bh_page_count = page_count; 394 hp->bh_page_count = page_count;
395 mf_ins_used(mfp, hp); 395 mf_ins_used(mfp, hp);
396 mf_ins_hash(mfp, hp); 396 mf_ins_hash(mfp, hp);
397 397
412 */ 412 */
413 bhdr_T * 413 bhdr_T *
414 mf_get(memfile_T *mfp, blocknr_T nr, int page_count) 414 mf_get(memfile_T *mfp, blocknr_T nr, int page_count)
415 { 415 {
416 bhdr_T *hp; 416 bhdr_T *hp;
417 /* doesn't exist */ 417 // doesn't exist
418 if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min) 418 if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
419 return NULL; 419 return NULL;
420 420
421 /* 421 /*
422 * see if it is in the cache 422 * see if it is in the cache
423 */ 423 */
424 hp = mf_find_hash(mfp, nr); 424 hp = mf_find_hash(mfp, nr);
425 if (hp == NULL) /* not in the hash list */ 425 if (hp == NULL) // not in the hash list
426 { 426 {
427 if (nr < 0 || nr >= mfp->mf_infile_count) /* can't be in the file */ 427 if (nr < 0 || nr >= mfp->mf_infile_count) // can't be in the file
428 return NULL; 428 return NULL;
429 429
430 /* could check here if the block is in the free list */ 430 // could check here if the block is in the free list
431 431
432 /* 432 /*
433 * Check if we need to flush an existing block. 433 * Check if we need to flush an existing block.
434 * If so, use that block. 434 * If so, use that block.
435 * If not, allocate a new block. 435 * If not, allocate a new block.
439 return NULL; 439 return NULL;
440 440
441 hp->bh_bnum = nr; 441 hp->bh_bnum = nr;
442 hp->bh_flags = 0; 442 hp->bh_flags = 0;
443 hp->bh_page_count = page_count; 443 hp->bh_page_count = page_count;
444 if (mf_read(mfp, hp) == FAIL) /* cannot read the block! */ 444 if (mf_read(mfp, hp) == FAIL) // cannot read the block!
445 { 445 {
446 mf_free_bhdr(hp); 446 mf_free_bhdr(hp);
447 return NULL; 447 return NULL;
448 } 448 }
449 } 449 }
450 else 450 else
451 { 451 {
452 mf_rem_used(mfp, hp); /* remove from list, insert in front below */ 452 mf_rem_used(mfp, hp); // remove from list, insert in front below
453 mf_rem_hash(mfp, hp); 453 mf_rem_hash(mfp, hp);
454 } 454 }
455 455
456 hp->bh_flags |= BH_LOCKED; 456 hp->bh_flags |= BH_LOCKED;
457 mf_ins_used(mfp, hp); /* put in front of used list */ 457 mf_ins_used(mfp, hp); // put in front of used list
458 mf_ins_hash(mfp, hp); /* put in front of hash list */ 458 mf_ins_hash(mfp, hp); // put in front of hash list
459 459
460 return hp; 460 return hp;
461 } 461 }
462 462
463 /* 463 /*
487 flags |= BH_DIRTY; 487 flags |= BH_DIRTY;
488 mfp->mf_dirty = TRUE; 488 mfp->mf_dirty = TRUE;
489 } 489 }
490 hp->bh_flags = flags; 490 hp->bh_flags = flags;
491 if (infile) 491 if (infile)
492 mf_trans_add(mfp, hp); /* may translate negative in positive nr */ 492 mf_trans_add(mfp, hp); // may translate negative in positive nr
493 } 493 }
494 494
495 /* 495 /*
496 * block *hp is no longer in used, may put it in the free list of memfile *mfp 496 * block *hp is no longer in used, may put it in the free list of memfile *mfp
497 */ 497 */
498 void 498 void
499 mf_free(memfile_T *mfp, bhdr_T *hp) 499 mf_free(memfile_T *mfp, bhdr_T *hp)
500 { 500 {
501 vim_free(hp->bh_data); /* free the memory */ 501 vim_free(hp->bh_data); // free the memory
502 mf_rem_hash(mfp, hp); /* get *hp out of the hash list */ 502 mf_rem_hash(mfp, hp); // get *hp out of the hash list
503 mf_rem_used(mfp, hp); /* get *hp out of the used list */ 503 mf_rem_used(mfp, hp); // get *hp out of the used list
504 if (hp->bh_bnum < 0) 504 if (hp->bh_bnum < 0)
505 { 505 {
506 vim_free(hp); /* don't want negative numbers in free list */ 506 vim_free(hp); // don't want negative numbers in free list
507 mfp->mf_neg_count--; 507 mfp->mf_neg_count--;
508 } 508 }
509 else 509 else
510 mf_ins_free(mfp, hp); /* put *hp in the free list */ 510 mf_ins_free(mfp, hp); // put *hp in the free list
511 } 511 }
512 512
513 #if defined(__MORPHOS__) && defined(__libnix__) 513 #if defined(__MORPHOS__) && defined(__libnix__)
514 /* function is missing in MorphOS libnix version */ 514 // function is missing in MorphOS libnix version
515 extern unsigned long *__stdfiledes; 515 extern unsigned long *__stdfiledes;
516 516
517 static unsigned long 517 static unsigned long
518 fdtofh(int filedescriptor) 518 fdtofh(int filedescriptor)
519 { 519 {
539 { 539 {
540 int status; 540 int status;
541 bhdr_T *hp; 541 bhdr_T *hp;
542 int got_int_save = got_int; 542 int got_int_save = got_int;
543 543
544 if (mfp->mf_fd < 0) /* there is no file, nothing to do */ 544 if (mfp->mf_fd < 0) // there is no file, nothing to do
545 { 545 {
546 mfp->mf_dirty = FALSE; 546 mfp->mf_dirty = FALSE;
547 return FAIL; 547 return FAIL;
548 } 548 }
549 549
550 /* Only a CTRL-C while writing will break us here, not one typed 550 // Only a CTRL-C while writing will break us here, not one typed
551 * previously. */ 551 // previously.
552 got_int = FALSE; 552 got_int = FALSE;
553 553
554 /* 554 /*
555 * sync from last to first (may reduce the probability of an inconsistent 555 * sync from last to first (may reduce the probability of an inconsistent
556 * file) If a write fails, it is very likely caused by a full filesystem. 556 * file) If a write fails, it is very likely caused by a full filesystem.
566 { 566 {
567 if ((flags & MFS_ZERO) && hp->bh_bnum != 0) 567 if ((flags & MFS_ZERO) && hp->bh_bnum != 0)
568 continue; 568 continue;
569 if (mf_write(mfp, hp) == FAIL) 569 if (mf_write(mfp, hp) == FAIL)
570 { 570 {
571 if (status == FAIL) /* double error: quit syncing */ 571 if (status == FAIL) // double error: quit syncing
572 break; 572 break;
573 status = FAIL; 573 status = FAIL;
574 } 574 }
575 if (flags & MFS_STOP) 575 if (flags & MFS_STOP)
576 { 576 {
577 /* Stop when char available now. */ 577 // Stop when char available now.
578 if (ui_char_avail()) 578 if (ui_char_avail())
579 break; 579 break;
580 } 580 }
581 else 581 else
582 ui_breakcheck(); 582 ui_breakcheck();
603 if (vim_fsync(mfp->mf_fd)) 603 if (vim_fsync(mfp->mf_fd))
604 status = FAIL; 604 status = FAIL;
605 } 605 }
606 else 606 else
607 # endif 607 # endif
608 /* OpenNT is strictly POSIX (Benzinger) */ 608 // OpenNT is strictly POSIX (Benzinger)
609 /* Tandem/Himalaya NSK-OSS doesn't have sync() */ 609 // Tandem/Himalaya NSK-OSS doesn't have sync()
610 /* No sync() on Stratus VOS */ 610 // No sync() on Stratus VOS
611 # if defined(__OPENNT) || defined(__TANDEM) || defined(__VOS__) 611 # if defined(__OPENNT) || defined(__TANDEM) || defined(__VOS__)
612 fflush(NULL); 612 fflush(NULL);
613 # else 613 # else
614 sync(); 614 sync();
615 # endif 615 # endif
647 } 647 }
648 # else 648 # else
649 # if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__) 649 # if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
650 { 650 {
651 # if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__) 651 # if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__)
652 /* Have function (in libnix at least), 652 // Have function (in libnix at least),
653 * but ain't got no prototype anywhere. */ 653 // but ain't got no prototype anywhere.
654 extern unsigned long fdtofh(int filedescriptor); 654 extern unsigned long fdtofh(int filedescriptor);
655 # endif 655 # endif
656 # if !defined(__libnix__) 656 # if !defined(__libnix__)
657 fflush(NULL); 657 fflush(NULL);
658 # else 658 # else
660 660
661 if (fh != 0) 661 if (fh != 0)
662 Flush(fh); 662 Flush(fh);
663 # endif 663 # endif
664 } 664 }
665 # else /* assume Manx */ 665 # else // assume Manx
666 Flush(_devtab[mfp->mf_fd].fd); 666 Flush(_devtab[mfp->mf_fd].fd);
667 # endif 667 # endif
668 # endif 668 # endif
669 # endif 669 # endif
670 #endif /* AMIGA */ 670 #endif // AMIGA
671 } 671 }
672 672
673 got_int |= got_int_save; 673 got_int |= got_int_save;
674 674
675 return status; 675 return status;
725 mf_ins_used(memfile_T *mfp, bhdr_T *hp) 725 mf_ins_used(memfile_T *mfp, bhdr_T *hp)
726 { 726 {
727 hp->bh_next = mfp->mf_used_first; 727 hp->bh_next = mfp->mf_used_first;
728 mfp->mf_used_first = hp; 728 mfp->mf_used_first = hp;
729 hp->bh_prev = NULL; 729 hp->bh_prev = NULL;
730 if (hp->bh_next == NULL) /* list was empty, adjust last pointer */ 730 if (hp->bh_next == NULL) // list was empty, adjust last pointer
731 mfp->mf_used_last = hp; 731 mfp->mf_used_last = hp;
732 else 732 else
733 hp->bh_next->bh_prev = hp; 733 hp->bh_next->bh_prev = hp;
734 mfp->mf_used_count += hp->bh_page_count; 734 mfp->mf_used_count += hp->bh_page_count;
735 total_mem_used += hp->bh_page_count * mfp->mf_page_size; 735 total_mem_used += hp->bh_page_count * mfp->mf_page_size;
739 * remove block *hp from used list of memfile *mfp 739 * remove block *hp from used list of memfile *mfp
740 */ 740 */
741 static void 741 static void
742 mf_rem_used(memfile_T *mfp, bhdr_T *hp) 742 mf_rem_used(memfile_T *mfp, bhdr_T *hp)
743 { 743 {
744 if (hp->bh_next == NULL) /* last block in used list */ 744 if (hp->bh_next == NULL) // last block in used list
745 mfp->mf_used_last = hp->bh_prev; 745 mfp->mf_used_last = hp->bh_prev;
746 else 746 else
747 hp->bh_next->bh_prev = hp->bh_prev; 747 hp->bh_next->bh_prev = hp->bh_prev;
748 if (hp->bh_prev == NULL) /* first block in used list */ 748 if (hp->bh_prev == NULL) // first block in used list
749 mfp->mf_used_first = hp->bh_next; 749 mfp->mf_used_first = hp->bh_next;
750 else 750 else
751 hp->bh_prev->bh_next = hp->bh_next; 751 hp->bh_prev->bh_next = hp->bh_next;
752 mfp->mf_used_count -= hp->bh_page_count; 752 mfp->mf_used_count -= hp->bh_page_count;
753 total_mem_used -= hp->bh_page_count * mfp->mf_page_size; 753 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
767 { 767 {
768 bhdr_T *hp; 768 bhdr_T *hp;
769 int need_release; 769 int need_release;
770 buf_T *buf; 770 buf_T *buf;
771 771
772 /* don't release while in mf_close_file() */ 772 // don't release while in mf_close_file()
773 if (mf_dont_release) 773 if (mf_dont_release)
774 return NULL; 774 return NULL;
775 775
776 /* 776 /*
777 * Need to release a block if the number of blocks for this memfile is 777 * Need to release a block if the number of blocks for this memfile is
784 * Try to create a swap file if the amount of memory used is getting too 784 * Try to create a swap file if the amount of memory used is getting too
785 * high. 785 * high.
786 */ 786 */
787 if (mfp->mf_fd < 0 && need_release && p_uc) 787 if (mfp->mf_fd < 0 && need_release && p_uc)
788 { 788 {
789 /* find for which buffer this memfile is */ 789 // find for which buffer this memfile is
790 FOR_ALL_BUFFERS(buf) 790 FOR_ALL_BUFFERS(buf)
791 if (buf->b_ml.ml_mfp == mfp) 791 if (buf->b_ml.ml_mfp == mfp)
792 break; 792 break;
793 if (buf != NULL && buf->b_may_swap) 793 if (buf != NULL && buf->b_may_swap)
794 ml_open_file(buf); 794 ml_open_file(buf);
806 return NULL; 806 return NULL;
807 807
808 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev) 808 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
809 if (!(hp->bh_flags & BH_LOCKED)) 809 if (!(hp->bh_flags & BH_LOCKED))
810 break; 810 break;
811 if (hp == NULL) /* not a single one that can be released */ 811 if (hp == NULL) // not a single one that can be released
812 return NULL; 812 return NULL;
813 813
814 /* 814 /*
815 * If the block is dirty, write it. 815 * If the block is dirty, write it.
816 * If the write fails we don't free it. 816 * If the write fails we don't free it.
855 FOR_ALL_BUFFERS(buf) 855 FOR_ALL_BUFFERS(buf)
856 { 856 {
857 mfp = buf->b_ml.ml_mfp; 857 mfp = buf->b_ml.ml_mfp;
858 if (mfp != NULL) 858 if (mfp != NULL)
859 { 859 {
860 /* If no swap file yet, may open one */ 860 // If no swap file yet, may open one
861 if (mfp->mf_fd < 0 && buf->b_may_swap) 861 if (mfp->mf_fd < 0 && buf->b_may_swap)
862 ml_open_file(buf); 862 ml_open_file(buf);
863 863
864 /* only if there is a swapfile */ 864 // only if there is a swapfile
865 if (mfp->mf_fd >= 0) 865 if (mfp->mf_fd >= 0)
866 { 866 {
867 for (hp = mfp->mf_used_last; hp != NULL; ) 867 for (hp = mfp->mf_used_last; hp != NULL; )
868 { 868 {
869 if (!(hp->bh_flags & BH_LOCKED) 869 if (!(hp->bh_flags & BH_LOCKED)
871 || mf_write(mfp, hp) != FAIL)) 871 || mf_write(mfp, hp) != FAIL))
872 { 872 {
873 mf_rem_used(mfp, hp); 873 mf_rem_used(mfp, hp);
874 mf_rem_hash(mfp, hp); 874 mf_rem_hash(mfp, hp);
875 mf_free_bhdr(hp); 875 mf_free_bhdr(hp);
876 hp = mfp->mf_used_last; /* re-start, list was changed */ 876 hp = mfp->mf_used_last; // re-start, list was changed
877 retval = TRUE; 877 retval = TRUE;
878 } 878 }
879 else 879 else
880 hp = hp->bh_prev; 880 hp = hp->bh_prev;
881 } 881 }
895 895
896 if ((hp = ALLOC_ONE(bhdr_T)) != NULL) 896 if ((hp = ALLOC_ONE(bhdr_T)) != NULL)
897 { 897 {
898 if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL) 898 if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL)
899 { 899 {
900 vim_free(hp); /* not enough memory */ 900 vim_free(hp); // not enough memory
901 return NULL; 901 return NULL;
902 } 902 }
903 hp->bh_page_count = page_count; 903 hp->bh_page_count = page_count;
904 } 904 }
905 return hp; 905 return hp;
949 { 949 {
950 off_T offset; 950 off_T offset;
951 unsigned page_size; 951 unsigned page_size;
952 unsigned size; 952 unsigned size;
953 953
954 if (mfp->mf_fd < 0) /* there is no file, can't read */ 954 if (mfp->mf_fd < 0) // there is no file, can't read
955 return FAIL; 955 return FAIL;
956 956
957 page_size = mfp->mf_page_size; 957 page_size = mfp->mf_page_size;
958 offset = (off_T)page_size * hp->bh_bnum; 958 offset = (off_T)page_size * hp->bh_bnum;
959 size = page_size * hp->bh_page_count; 959 size = page_size * hp->bh_page_count;
967 PERROR(_("E295: Read error in swap file")); 967 PERROR(_("E295: Read error in swap file"));
968 return FAIL; 968 return FAIL;
969 } 969 }
970 970
971 #ifdef FEAT_CRYPT 971 #ifdef FEAT_CRYPT
972 /* Decrypt if 'key' is set and this is a data block. And when changing the 972 // Decrypt if 'key' is set and this is a data block. And when changing the
973 * key. */ 973 // key.
974 if (*mfp->mf_buffer->b_p_key != NUL || mfp->mf_old_key != NULL) 974 if (*mfp->mf_buffer->b_p_key != NUL || mfp->mf_old_key != NULL)
975 ml_decrypt_data(mfp, hp->bh_data, offset, size); 975 ml_decrypt_data(mfp, hp->bh_data, offset, size);
976 #endif 976 #endif
977 977
978 return OK; 978 return OK;
984 * Return FAIL for failure, OK otherwise 984 * Return FAIL for failure, OK otherwise
985 */ 985 */
986 static int 986 static int
987 mf_write(memfile_T *mfp, bhdr_T *hp) 987 mf_write(memfile_T *mfp, bhdr_T *hp)
988 { 988 {
989 off_T offset; /* offset in the file */ 989 off_T offset; // offset in the file
990 blocknr_T nr; /* block nr which is being written */ 990 blocknr_T nr; // block nr which is being written
991 bhdr_T *hp2; 991 bhdr_T *hp2;
992 unsigned page_size; /* number of bytes in a page */ 992 unsigned page_size; // number of bytes in a page
993 unsigned page_count; /* number of pages written */ 993 unsigned page_count; // number of pages written
994 unsigned size; /* number of bytes written */ 994 unsigned size; // number of bytes written
995 995
996 if (mfp->mf_fd < 0 && !mfp->mf_reopen) 996 if (mfp->mf_fd < 0 && !mfp->mf_reopen)
997 // there is no file and there was no file, can't write 997 // there is no file and there was no file, can't write
998 return FAIL; 998 return FAIL;
999 999
1000 if (hp->bh_bnum < 0) /* must assign file block number */ 1000 if (hp->bh_bnum < 0) // must assign file block number
1001 if (mf_trans_add(mfp, hp) == FAIL) 1001 if (mf_trans_add(mfp, hp) == FAIL)
1002 return FAIL; 1002 return FAIL;
1003 1003
1004 page_size = mfp->mf_page_size; 1004 page_size = mfp->mf_page_size;
1005 1005
1012 for (;;) 1012 for (;;)
1013 { 1013 {
1014 int attempt; 1014 int attempt;
1015 1015
1016 nr = hp->bh_bnum; 1016 nr = hp->bh_bnum;
1017 if (nr > mfp->mf_infile_count) /* beyond end of file */ 1017 if (nr > mfp->mf_infile_count) // beyond end of file
1018 { 1018 {
1019 nr = mfp->mf_infile_count; 1019 nr = mfp->mf_infile_count;
1020 hp2 = mf_find_hash(mfp, nr); /* NULL caught below */ 1020 hp2 = mf_find_hash(mfp, nr); // NULL caught below
1021 } 1021 }
1022 else 1022 else
1023 hp2 = hp; 1023 hp2 = hp;
1024 1024
1025 offset = (off_T)page_size * nr; 1025 offset = (off_T)page_size * nr;
1026 if (hp2 == NULL) /* freed block, fill with dummy data */ 1026 if (hp2 == NULL) // freed block, fill with dummy data
1027 page_count = 1; 1027 page_count = 1;
1028 else 1028 else
1029 page_count = hp2->bh_page_count; 1029 page_count = hp2->bh_page_count;
1030 size = page_size * page_count; 1030 size = page_size * page_count;
1031 1031
1065 return FAIL; 1065 return FAIL;
1066 } 1066 }
1067 } 1067 }
1068 1068
1069 did_swapwrite_msg = FALSE; 1069 did_swapwrite_msg = FALSE;
1070 if (hp2 != NULL) /* written a non-dummy block */ 1070 if (hp2 != NULL) // written a non-dummy block
1071 hp2->bh_flags &= ~BH_DIRTY; 1071 hp2->bh_flags &= ~BH_DIRTY;
1072 /* appended to the file */ 1072 // appended to the file
1073 if (nr + (blocknr_T)page_count > mfp->mf_infile_count) 1073 if (nr + (blocknr_T)page_count > mfp->mf_infile_count)
1074 mfp->mf_infile_count = nr + page_count; 1074 mfp->mf_infile_count = nr + page_count;
1075 if (nr == hp->bh_bnum) /* written the desired block */ 1075 if (nr == hp->bh_bnum) // written the desired block
1076 break; 1076 break;
1077 } 1077 }
1078 return OK; 1078 return OK;
1079 } 1079 }
1080 1080
1092 { 1092 {
1093 char_u *data = hp->bh_data; 1093 char_u *data = hp->bh_data;
1094 int result = OK; 1094 int result = OK;
1095 1095
1096 #ifdef FEAT_CRYPT 1096 #ifdef FEAT_CRYPT
1097 /* Encrypt if 'key' is set and this is a data block. */ 1097 // Encrypt if 'key' is set and this is a data block.
1098 if (*mfp->mf_buffer->b_p_key != NUL) 1098 if (*mfp->mf_buffer->b_p_key != NUL)
1099 { 1099 {
1100 data = ml_encrypt_data(mfp, data, offset, size); 1100 data = ml_encrypt_data(mfp, data, offset, size);
1101 if (data == NULL) 1101 if (data == NULL)
1102 return FAIL; 1102 return FAIL;
1125 bhdr_T *freep; 1125 bhdr_T *freep;
1126 blocknr_T new_bnum; 1126 blocknr_T new_bnum;
1127 NR_TRANS *np; 1127 NR_TRANS *np;
1128 int page_count; 1128 int page_count;
1129 1129
1130 if (hp->bh_bnum >= 0) /* it's already positive */ 1130 if (hp->bh_bnum >= 0) // it's already positive
1131 return OK; 1131 return OK;
1132 1132
1133 if ((np = ALLOC_ONE(NR_TRANS)) == NULL) 1133 if ((np = ALLOC_ONE(NR_TRANS)) == NULL)
1134 return FAIL; 1134 return FAIL;
1135 1135
1162 { 1162 {
1163 new_bnum = mfp->mf_blocknr_max; 1163 new_bnum = mfp->mf_blocknr_max;
1164 mfp->mf_blocknr_max += page_count; 1164 mfp->mf_blocknr_max += page_count;
1165 } 1165 }
1166 1166
1167 np->nt_old_bnum = hp->bh_bnum; /* adjust number */ 1167 np->nt_old_bnum = hp->bh_bnum; // adjust number
1168 np->nt_new_bnum = new_bnum; 1168 np->nt_new_bnum = new_bnum;
1169 1169
1170 mf_rem_hash(mfp, hp); /* remove from old hash list */ 1170 mf_rem_hash(mfp, hp); // remove from old hash list
1171 hp->bh_bnum = new_bnum; 1171 hp->bh_bnum = new_bnum;
1172 mf_ins_hash(mfp, hp); /* insert in new hash list */ 1172 mf_ins_hash(mfp, hp); // insert in new hash list
1173 1173
1174 /* Insert "np" into "mf_trans" hashtable with key "np->nt_old_bnum" */ 1174 // Insert "np" into "mf_trans" hashtable with key "np->nt_old_bnum"
1175 mf_hash_add_item(&mfp->mf_trans, (mf_hashitem_T *)np); 1175 mf_hash_add_item(&mfp->mf_trans, (mf_hashitem_T *)np);
1176 1176
1177 return OK; 1177 return OK;
1178 } 1178 }
1179 1179
1188 NR_TRANS *np; 1188 NR_TRANS *np;
1189 blocknr_T new_bnum; 1189 blocknr_T new_bnum;
1190 1190
1191 np = (NR_TRANS *)mf_hash_find(&mfp->mf_trans, old_nr); 1191 np = (NR_TRANS *)mf_hash_find(&mfp->mf_trans, old_nr);
1192 1192
1193 if (np == NULL) /* not found */ 1193 if (np == NULL) // not found
1194 return old_nr; 1194 return old_nr;
1195 1195
1196 mfp->mf_neg_count--; 1196 mfp->mf_neg_count--;
1197 new_bnum = np->nt_new_bnum; 1197 new_bnum = np->nt_new_bnum;
1198 1198
1199 /* remove entry from the trans list */ 1199 // remove entry from the trans list
1200 mf_hash_rem_item(&mfp->mf_trans, (mf_hashitem_T *)np); 1200 mf_hash_rem_item(&mfp->mf_trans, (mf_hashitem_T *)np);
1201 1201
1202 vim_free(np); 1202 vim_free(np);
1203 1203
1204 return new_bnum; 1204 return new_bnum;
1246 */ 1246 */
1247 static void 1247 static void
1248 mf_do_open( 1248 mf_do_open(
1249 memfile_T *mfp, 1249 memfile_T *mfp,
1250 char_u *fname, 1250 char_u *fname,
1251 int flags) /* flags for open() */ 1251 int flags) // flags for open()
1252 { 1252 {
1253 #ifdef HAVE_LSTAT 1253 #ifdef HAVE_LSTAT
1254 stat_T sb; 1254 stat_T sb;
1255 #endif 1255 #endif
1256 1256
1286 /* 1286 /*
1287 * try to open the file 1287 * try to open the file
1288 */ 1288 */
1289 flags |= O_EXTRA | O_NOFOLLOW; 1289 flags |= O_EXTRA | O_NOFOLLOW;
1290 #ifdef MSWIN 1290 #ifdef MSWIN
1291 /* Prevent handle inheritance that cause problems with Cscope 1291 // Prevent handle inheritance that cause problems with Cscope
1292 * (swap file may not be deleted if cscope connection was open after 1292 // (swap file may not be deleted if cscope connection was open after
1293 * the file) */ 1293 // the file)
1294 flags |= O_NOINHERIT; 1294 flags |= O_NOINHERIT;
1295 #endif 1295 #endif
1296 mfp->mf_flags = flags; 1296 mfp->mf_flags = flags;
1297 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags); 1297 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags);
1298 } 1298 }
1313 (void)fcntl(mfp->mf_fd, F_SETFD, fdflags | FD_CLOEXEC); 1313 (void)fcntl(mfp->mf_fd, F_SETFD, fdflags | FD_CLOEXEC);
1314 #endif 1314 #endif
1315 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) 1315 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
1316 mch_copy_sec(fname, mfp->mf_fname); 1316 mch_copy_sec(fname, mfp->mf_fname);
1317 #endif 1317 #endif
1318 mch_hide(mfp->mf_fname); /* try setting the 'hidden' flag */ 1318 mch_hide(mfp->mf_fname); // try setting the 'hidden' flag
1319 } 1319 }
1320 } 1320 }
1321 1321
1322 /* 1322 /*
1323 * Implementation of mf_hashtab_T follows. 1323 * Implementation of mf_hashtab_T follows.
1327 * The number of buckets in the hashtable is increased by a factor of 1327 * The number of buckets in the hashtable is increased by a factor of
1328 * MHT_GROWTH_FACTOR when the average number of items per bucket 1328 * MHT_GROWTH_FACTOR when the average number of items per bucket
1329 * exceeds 2 ^ MHT_LOG_LOAD_FACTOR. 1329 * exceeds 2 ^ MHT_LOG_LOAD_FACTOR.
1330 */ 1330 */
1331 #define MHT_LOG_LOAD_FACTOR 6 1331 #define MHT_LOG_LOAD_FACTOR 6
1332 #define MHT_GROWTH_FACTOR 2 /* must be a power of two */ 1332 #define MHT_GROWTH_FACTOR 2 // must be a power of two
1333 1333
1334 /* 1334 /*
1335 * Initialize an empty hash table. 1335 * Initialize an empty hash table.
1336 */ 1336 */
1337 static void 1337 static void
1414 if (mht->mht_fixed == 0 1414 if (mht->mht_fixed == 0
1415 && (mht->mht_count >> MHT_LOG_LOAD_FACTOR) > mht->mht_mask) 1415 && (mht->mht_count >> MHT_LOG_LOAD_FACTOR) > mht->mht_mask)
1416 { 1416 {
1417 if (mf_hash_grow(mht) == FAIL) 1417 if (mf_hash_grow(mht) == FAIL)
1418 { 1418 {
1419 /* stop trying to grow after first failure to allocate memory */ 1419 // stop trying to grow after first failure to allocate memory
1420 mht->mht_fixed = 1; 1420 mht->mht_fixed = 1;
1421 } 1421 }
1422 } 1422 }
1423 } 1423 }
1424 1424
1437 if (mhi->mhi_next != NULL) 1437 if (mhi->mhi_next != NULL)
1438 mhi->mhi_next->mhi_prev = mhi->mhi_prev; 1438 mhi->mhi_next->mhi_prev = mhi->mhi_prev;
1439 1439
1440 mht->mht_count--; 1440 mht->mht_count--;
1441 1441
1442 /* We could shrink the table here, but it typically takes little memory, 1442 // We could shrink the table here, but it typically takes little memory,
1443 * so why bother? */ 1443 // so why bother?
1444 } 1444 }
1445 1445
1446 /* 1446 /*
1447 * Increase number of buckets in the hashtable by MHT_GROWTH_FACTOR and 1447 * Increase number of buckets in the hashtable by MHT_GROWTH_FACTOR and
1448 * rehash items. 1448 * rehash items.