Merge branch 'vim-with-runtime' into feat/code-check
[vim_extended.git] / src / memfile.c
blobbd03e65a6de985bf9bc8ec08a5770bff03aa20d4
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
11 * memfile.c: Contains the functions for handling blocks of memory which can
12 * be stored in a file. This is the implementation of a sort of virtual memory.
14 * A memfile consists of a sequence of blocks. The blocks numbered from 0
15 * upwards have been assigned a place in the actual file. The block number
16 * is equal to the page number in the file. The
17 * blocks with negative numbers are currently in memory only. They can be
18 * assigned a place in the file when too much memory is being used. At that
19 * moment they get a new, positive, number. A list is used for translation of
20 * negative to positive numbers.
22 * The size of a block is a multiple of a page size, normally the page size of
23 * the device the file is on. Most blocks are 1 page long. A Block of multiple
24 * pages is used for a line that does not fit in a single page.
26 * Each block can be in memory and/or in a file. The block stays in memory
27 * as long as it is locked. If it is no longer locked it can be swapped out to
28 * the file. It is only written to the file if it has been changed.
30 * Under normal operation the file is created when opening the memory file and
31 * deleted when closing the memory file. Only with recovery an existing memory
32 * file is opened.
35 #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
36 # include "vimio.h" /* for lseek(), must be before vim.h */
37 #endif
39 #include "vim.h"
42 * Some systems have the page size in statfs.f_bsize, some in stat.st_blksize
44 #ifdef HAVE_ST_BLKSIZE
45 # define STATFS stat
46 # define F_BSIZE st_blksize
47 # define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
48 #else
49 # ifdef HAVE_SYS_STATFS_H
50 # include <sys/statfs.h>
51 # define STATFS statfs
52 # define F_BSIZE f_bsize
53 # ifdef __MINT__ /* do we still need this? */
54 # define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
55 # endif
56 # endif
57 #endif
60 * for Amiga Dos 2.0x we use Flush
62 #ifdef AMIGA
63 # ifdef FEAT_ARP
64 extern int dos2; /* this is in os_amiga.c */
65 # endif
66 # ifdef SASC
67 # include <proto/dos.h>
68 # include <ios1.h> /* for chkufb() */
69 # endif
70 #endif
72 #define MEMFILE_PAGE_SIZE 4096 /* default page size */
74 static long_u total_mem_used = 0; /* total memory used for memfiles */
76 static void mf_ins_hash __ARGS((memfile_T *, bhdr_T *));
77 static void mf_rem_hash __ARGS((memfile_T *, bhdr_T *));
78 static bhdr_T *mf_find_hash __ARGS((memfile_T *, blocknr_T));
79 static void mf_ins_used __ARGS((memfile_T *, bhdr_T *));
80 static void mf_rem_used __ARGS((memfile_T *, bhdr_T *));
81 static bhdr_T *mf_release __ARGS((memfile_T *, int));
82 static bhdr_T *mf_alloc_bhdr __ARGS((memfile_T *, int));
83 static void mf_free_bhdr __ARGS((bhdr_T *));
84 static void mf_ins_free __ARGS((memfile_T *, bhdr_T *));
85 static bhdr_T *mf_rem_free __ARGS((memfile_T *));
86 static int mf_read __ARGS((memfile_T *, bhdr_T *));
87 static int mf_write __ARGS((memfile_T *, bhdr_T *));
88 static int mf_trans_add __ARGS((memfile_T *, bhdr_T *));
89 static void mf_do_open __ARGS((memfile_T *, char_u *, int));
92 * The functions for using a memfile:
94 * mf_open() open a new or existing memfile
95 * mf_open_file() open a swap file for an existing memfile
96 * mf_close() close (and delete) a memfile
97 * mf_new() create a new block in a memfile and lock it
98 * mf_get() get an existing block and lock it
99 * mf_put() unlock a block, may be marked for writing
100 * mf_free() remove a block
101 * mf_sync() sync changed parts of memfile to disk
102 * mf_release_all() release as much memory as possible
103 * mf_trans_del() may translate negative to positive block number
104 * mf_fullname() make file name full path (use before first :cd)
108 * Open an existing or new memory block file.
110 * fname: name of file to use (NULL means no file at all)
111 * Note: fname must have been allocated, it is not copied!
112 * If opening the file fails, fname is freed.
113 * flags: flags for open() call
115 * If fname != NULL and file cannot be opened, fail.
117 * return value: identifier for this memory block file.
119 memfile_T *
120 mf_open(fname, flags)
121 char_u *fname;
122 int flags;
124 memfile_T *mfp;
125 int i;
126 off_t size;
127 #if defined(STATFS) && defined(UNIX) && !defined(__QNX__)
128 # define USE_FSTATFS
129 struct STATFS stf;
130 #endif
132 if ((mfp = (memfile_T *)alloc((unsigned)sizeof(memfile_T))) == NULL)
133 return NULL;
135 if (fname == NULL) /* no file for this memfile, use memory only */
137 mfp->mf_fname = NULL;
138 mfp->mf_ffname = NULL;
139 mfp->mf_fd = -1;
141 else
143 mf_do_open(mfp, fname, flags); /* try to open the file */
145 /* if the file cannot be opened, return here */
146 if (mfp->mf_fd < 0)
148 vim_free(mfp);
149 return NULL;
153 mfp->mf_free_first = NULL; /* free list is empty */
154 mfp->mf_used_first = NULL; /* used list is empty */
155 mfp->mf_used_last = NULL;
156 mfp->mf_dirty = FALSE;
157 mfp->mf_used_count = 0;
158 for (i = 0; i < MEMHASHSIZE; ++i)
160 mfp->mf_hash[i] = NULL; /* hash lists are empty */
161 mfp->mf_trans[i] = NULL; /* trans lists are empty */
163 mfp->mf_page_size = MEMFILE_PAGE_SIZE;
165 #ifdef USE_FSTATFS
167 * Try to set the page size equal to the block size of the device.
168 * Speeds up I/O a lot.
169 * When recovering, the actual block size will be retrieved from block 0
170 * in ml_recover(). The size used here may be wrong, therefore
171 * mf_blocknr_max must be rounded up.
173 if (mfp->mf_fd >= 0
174 && fstatfs(mfp->mf_fd, &stf, sizeof(struct statfs), 0) == 0
175 && stf.F_BSIZE >= MIN_SWAP_PAGE_SIZE
176 && stf.F_BSIZE <= MAX_SWAP_PAGE_SIZE)
177 mfp->mf_page_size = stf.F_BSIZE;
178 #endif
180 if (mfp->mf_fd < 0 || (flags & (O_TRUNC|O_EXCL))
181 || (size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0)
182 mfp->mf_blocknr_max = 0; /* no file or empty file */
183 else
184 mfp->mf_blocknr_max = (blocknr_T)((size + mfp->mf_page_size - 1)
185 / mfp->mf_page_size);
186 mfp->mf_blocknr_min = -1;
187 mfp->mf_neg_count = 0;
188 mfp->mf_infile_count = mfp->mf_blocknr_max;
191 * Compute maximum number of pages ('maxmem' is in Kbyte):
192 * 'mammem' * 1Kbyte / page-size-in-bytes.
193 * Avoid overflow by first reducing page size as much as possible.
196 int shift = 10;
197 unsigned page_size = mfp->mf_page_size;
199 while (shift > 0 && (page_size & 1) == 0)
201 page_size = page_size >> 1;
202 --shift;
204 mfp->mf_used_count_max = (p_mm << shift) / page_size;
205 if (mfp->mf_used_count_max < 10)
206 mfp->mf_used_count_max = 10;
209 return mfp;
213 * Open a file for an existing memfile. Used when updatecount set from 0 to
214 * some value.
215 * If the file already exists, this fails.
216 * "fname" is the name of file to use (NULL means no file at all)
217 * Note: "fname" must have been allocated, it is not copied! If opening the
218 * file fails, "fname" is freed.
220 * return value: FAIL if file could not be opened, OK otherwise
223 mf_open_file(mfp, fname)
224 memfile_T *mfp;
225 char_u *fname;
227 mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); /* try to open the file */
229 if (mfp->mf_fd < 0)
230 return FAIL;
232 mfp->mf_dirty = TRUE;
233 return OK;
237 * close a memory file and delete the associated file if 'del_file' is TRUE
239 void
240 mf_close(mfp, del_file)
241 memfile_T *mfp;
242 int del_file;
244 bhdr_T *hp, *nextp;
245 NR_TRANS *tp, *tpnext;
246 int i;
248 if (mfp == NULL) /* safety check */
249 return;
250 if (mfp->mf_fd >= 0)
252 if (close(mfp->mf_fd) < 0)
253 EMSG(_(e_swapclose));
255 if (del_file && mfp->mf_fname != NULL)
256 mch_remove(mfp->mf_fname);
257 /* free entries in used list */
258 for (hp = mfp->mf_used_first; hp != NULL; hp = nextp)
260 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
261 nextp = hp->bh_next;
262 mf_free_bhdr(hp);
264 while (mfp->mf_free_first != NULL) /* free entries in free list */
265 vim_free(mf_rem_free(mfp));
266 for (i = 0; i < MEMHASHSIZE; ++i) /* free entries in trans lists */
267 for (tp = mfp->mf_trans[i]; tp != NULL; tp = tpnext)
269 tpnext = tp->nt_next;
270 vim_free(tp);
272 vim_free(mfp->mf_fname);
273 vim_free(mfp->mf_ffname);
274 vim_free(mfp);
278 * Close the swap file for a memfile. Used when 'swapfile' is reset.
280 void
281 mf_close_file(buf, getlines)
282 buf_T *buf;
283 int getlines; /* get all lines into memory? */
285 memfile_T *mfp;
286 linenr_T lnum;
288 mfp = buf->b_ml.ml_mfp;
289 if (mfp == NULL || mfp->mf_fd < 0) /* nothing to close */
290 return;
292 if (getlines)
294 /* get all blocks in memory by accessing all lines (clumsy!) */
295 mf_dont_release = TRUE;
296 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
297 (void)ml_get_buf(buf, lnum, FALSE);
298 mf_dont_release = FALSE;
299 /* TODO: should check if all blocks are really in core */
302 if (close(mfp->mf_fd) < 0) /* close the file */
303 EMSG(_(e_swapclose));
304 mfp->mf_fd = -1;
306 if (mfp->mf_fname != NULL)
308 mch_remove(mfp->mf_fname); /* delete the swap file */
309 vim_free(mfp->mf_fname);
310 vim_free(mfp->mf_ffname);
311 mfp->mf_fname = NULL;
312 mfp->mf_ffname = NULL;
317 * Set new size for a memfile. Used when block 0 of a swapfile has been read
318 * and the size it indicates differs from what was guessed.
320 void
321 mf_new_page_size(mfp, new_size)
322 memfile_T *mfp;
323 unsigned new_size;
325 /* Correct the memory used for block 0 to the new size, because it will be
326 * freed with that size later on. */
327 total_mem_used += new_size - mfp->mf_page_size;
328 mfp->mf_page_size = new_size;
332 * get a new block
334 * negative: TRUE if negative block number desired (data block)
336 bhdr_T *
337 mf_new(mfp, negative, page_count)
338 memfile_T *mfp;
339 int negative;
340 int page_count;
342 bhdr_T *hp; /* new bhdr_T */
343 bhdr_T *freep; /* first block in free list */
344 char_u *p;
347 * If we reached the maximum size for the used memory blocks, release one
348 * If a bhdr_T is returned, use it and adjust the page_count if necessary.
350 hp = mf_release(mfp, page_count);
353 * Decide on the number to use:
354 * If there is a free block, use its number.
355 * Otherwise use mf_block_min for a negative number, mf_block_max for
356 * a positive number.
358 freep = mfp->mf_free_first;
359 if (!negative && freep != NULL && freep->bh_page_count >= page_count)
362 * If the block in the free list has more pages, take only the number
363 * of pages needed and allocate a new bhdr_T with data
365 * If the number of pages matches and mf_release() did not return a
366 * bhdr_T, use the bhdr_T from the free list and allocate the data
368 * If the number of pages matches and mf_release() returned a bhdr_T,
369 * just use the number and free the bhdr_T from the free list
371 if (freep->bh_page_count > page_count)
373 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
374 return NULL;
375 hp->bh_bnum = freep->bh_bnum;
376 freep->bh_bnum += page_count;
377 freep->bh_page_count -= page_count;
379 else if (hp == NULL) /* need to allocate memory for this block */
381 if ((p = (char_u *)alloc(mfp->mf_page_size * page_count)) == NULL)
382 return NULL;
383 hp = mf_rem_free(mfp);
384 hp->bh_data = p;
386 else /* use the number, remove entry from free list */
388 freep = mf_rem_free(mfp);
389 hp->bh_bnum = freep->bh_bnum;
390 vim_free(freep);
393 else /* get a new number */
395 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
396 return NULL;
397 if (negative)
399 hp->bh_bnum = mfp->mf_blocknr_min--;
400 mfp->mf_neg_count++;
402 else
404 hp->bh_bnum = mfp->mf_blocknr_max;
405 mfp->mf_blocknr_max += page_count;
408 hp->bh_flags = BH_LOCKED | BH_DIRTY; /* new block is always dirty */
409 mfp->mf_dirty = TRUE;
410 hp->bh_page_count = page_count;
411 mf_ins_used(mfp, hp);
412 mf_ins_hash(mfp, hp);
415 * Init the data to all zero, to avoid reading uninitialized data.
416 * This also avoids that the passwd file ends up in the swap file!
418 (void)vim_memset((char *)(hp->bh_data), 0, (size_t)mfp->mf_page_size);
420 return hp;
424 * get existing block 'nr' with 'page_count' pages
426 * Note: The caller should first check a negative nr with mf_trans_del()
428 bhdr_T *
429 mf_get(mfp, nr, page_count)
430 memfile_T *mfp;
431 blocknr_T nr;
432 int page_count;
434 bhdr_T *hp;
435 /* doesn't exist */
436 if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
437 return NULL;
440 * see if it is in the cache
442 hp = mf_find_hash(mfp, nr);
443 if (hp == NULL) /* not in the hash list */
445 if (nr < 0 || nr >= mfp->mf_infile_count) /* can't be in the file */
446 return NULL;
448 /* could check here if the block is in the free list */
451 * Check if we need to flush an existing block.
452 * If so, use that block.
453 * If not, allocate a new block.
455 hp = mf_release(mfp, page_count);
456 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
457 return NULL;
459 hp->bh_bnum = nr;
460 hp->bh_flags = 0;
461 hp->bh_page_count = page_count;
462 if (mf_read(mfp, hp) == FAIL) /* cannot read the block! */
464 mf_free_bhdr(hp);
465 return NULL;
468 else
470 mf_rem_used(mfp, hp); /* remove from list, insert in front below */
471 mf_rem_hash(mfp, hp);
474 hp->bh_flags |= BH_LOCKED;
475 mf_ins_used(mfp, hp); /* put in front of used list */
476 mf_ins_hash(mfp, hp); /* put in front of hash list */
478 return hp;
482 * release the block *hp
484 * dirty: Block must be written to file later
485 * infile: Block should be in file (needed for recovery)
487 * no return value, function cannot fail
489 void
490 mf_put(mfp, hp, dirty, infile)
491 memfile_T *mfp;
492 bhdr_T *hp;
493 int dirty;
494 int infile;
496 int flags;
498 flags = hp->bh_flags;
500 if ((flags & BH_LOCKED) == 0)
501 EMSG(_("E293: block was not locked"));
502 flags &= ~BH_LOCKED;
503 if (dirty)
505 flags |= BH_DIRTY;
506 mfp->mf_dirty = TRUE;
508 hp->bh_flags = flags;
509 if (infile)
510 mf_trans_add(mfp, hp); /* may translate negative in positive nr */
514 * block *hp is no longer in used, may put it in the free list of memfile *mfp
516 void
517 mf_free(mfp, hp)
518 memfile_T *mfp;
519 bhdr_T *hp;
521 vim_free(hp->bh_data); /* free the memory */
522 mf_rem_hash(mfp, hp); /* get *hp out of the hash list */
523 mf_rem_used(mfp, hp); /* get *hp out of the used list */
524 if (hp->bh_bnum < 0)
526 vim_free(hp); /* don't want negative numbers in free list */
527 mfp->mf_neg_count--;
529 else
530 mf_ins_free(mfp, hp); /* put *hp in the free list */
533 #if defined(__MORPHOS__) && defined(__libnix__)
534 /* function is missing in MorphOS libnix version */
535 extern unsigned long *__stdfiledes;
537 static unsigned long
538 fdtofh(int filedescriptor)
540 return __stdfiledes[filedescriptor];
542 #endif
545 * Sync the memory file *mfp to disk.
546 * Flags:
547 * MFS_ALL If not given, blocks with negative numbers are not synced,
548 * even when they are dirty!
549 * MFS_STOP Stop syncing when a character becomes available, but sync at
550 * least one block.
551 * MFS_FLUSH Make sure buffers are flushed to disk, so they will survive a
552 * system crash.
553 * MFS_ZERO Only write block 0.
555 * Return FAIL for failure, OK otherwise
558 mf_sync(mfp, flags)
559 memfile_T *mfp;
560 int flags;
562 int status;
563 bhdr_T *hp;
564 #if defined(SYNC_DUP_CLOSE) && !defined(MSDOS)
565 int fd;
566 #endif
567 int got_int_save = got_int;
569 if (mfp->mf_fd < 0) /* there is no file, nothing to do */
571 mfp->mf_dirty = FALSE;
572 return FAIL;
575 /* Only a CTRL-C while writing will break us here, not one typed
576 * previously. */
577 got_int = FALSE;
580 * sync from last to first (may reduce the probability of an inconsistent
581 * file) If a write fails, it is very likely caused by a full filesystem.
582 * Then we only try to write blocks within the existing file. If that also
583 * fails then we give up.
585 status = OK;
586 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
587 if (((flags & MFS_ALL) || hp->bh_bnum >= 0)
588 && (hp->bh_flags & BH_DIRTY)
589 && (status == OK || (hp->bh_bnum >= 0
590 && hp->bh_bnum < mfp->mf_infile_count)))
592 if ((flags & MFS_ZERO) && hp->bh_bnum != 0)
593 continue;
594 if (mf_write(mfp, hp) == FAIL)
596 if (status == FAIL) /* double error: quit syncing */
597 break;
598 status = FAIL;
600 if (flags & MFS_STOP)
602 /* Stop when char available now. */
603 if (ui_char_avail())
604 break;
606 else
607 ui_breakcheck();
608 if (got_int)
609 break;
613 * If the whole list is flushed, the memfile is not dirty anymore.
614 * In case of an error this flag is also set, to avoid trying all the time.
616 if (hp == NULL || status == FAIL)
617 mfp->mf_dirty = FALSE;
619 if ((flags & MFS_FLUSH) && *p_sws != NUL)
621 #if defined(UNIX)
622 # ifdef HAVE_FSYNC
624 * most Unixes have the very useful fsync() function, just what we need.
625 * However, with OS/2 and EMX it is also available, but there are
626 * reports of bad problems with it (a bug in HPFS.IFS).
627 * So we disable use of it here in case someone tries to be smart
628 * and changes os_os2_cfg.h... (even though there is no __EMX__ test
629 * in the #if, as __EMX__ does not have sync(); we hope for a timely
630 * sync from the system itself).
632 # if defined(__EMX__)
633 error "Dont use fsync with EMX! Read emxdoc.doc or emxfix01.doc for info."
634 # endif
635 if (STRCMP(p_sws, "fsync") == 0)
637 if (fsync(mfp->mf_fd))
638 status = FAIL;
640 else
641 # endif
642 /* OpenNT is strictly POSIX (Benzinger) */
643 /* Tandem/Himalaya NSK-OSS doesn't have sync() */
644 # if defined(__OPENNT) || defined(__TANDEM)
645 fflush(NULL);
646 # else
647 sync();
648 # endif
649 #endif
650 #ifdef VMS
651 if (STRCMP(p_sws, "fsync") == 0)
653 if (fsync(mfp->mf_fd))
654 status = FAIL;
656 #endif
657 #ifdef MSDOS
658 if (_dos_commit(mfp->mf_fd))
659 status = FAIL;
660 #else
661 # ifdef SYNC_DUP_CLOSE
663 * Win32 is a bit more work: Duplicate the file handle and close it.
664 * This should flush the file to disk.
666 if ((fd = dup(mfp->mf_fd)) >= 0)
667 close(fd);
668 # endif
669 #endif
670 #ifdef AMIGA
671 # if defined(__AROS__) || defined(__amigaos4__)
672 if (fsync(mfp->mf_fd) != 0)
673 status = FAIL;
674 # else
676 * Flush() only exists for AmigaDos 2.0.
677 * For 1.3 it should be done with close() + open(), but then the risk
678 * is that the open() may fail and lose the file....
680 # ifdef FEAT_ARP
681 if (dos2)
682 # endif
683 # ifdef SASC
685 struct UFB *fp = chkufb(mfp->mf_fd);
687 if (fp != NULL)
688 Flush(fp->ufbfh);
690 # else
691 # if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
693 # if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__)
694 /* Have function (in libnix at least),
695 * but ain't got no prototype anywhere. */
696 extern unsigned long fdtofh(int filedescriptor);
697 # endif
698 # if !defined(__libnix__)
699 fflush(NULL);
700 # else
701 BPTR fh = (BPTR)fdtofh(mfp->mf_fd);
703 if (fh != 0)
704 Flush(fh);
705 # endif
707 # else /* assume Manx */
708 Flush(_devtab[mfp->mf_fd].fd);
709 # endif
710 # endif
711 # endif
712 #endif /* AMIGA */
715 got_int |= got_int_save;
717 return status;
721 * For all blocks in memory file *mfp that have a positive block number set
722 * the dirty flag. These are blocks that need to be written to a newly
723 * created swapfile.
725 void
726 mf_set_dirty(mfp)
727 memfile_T *mfp;
729 bhdr_T *hp;
731 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
732 if (hp->bh_bnum > 0)
733 hp->bh_flags |= BH_DIRTY;
734 mfp->mf_dirty = TRUE;
738 * insert block *hp in front of hashlist of memfile *mfp
740 static void
741 mf_ins_hash(mfp, hp)
742 memfile_T *mfp;
743 bhdr_T *hp;
745 bhdr_T *hhp;
746 int hash;
748 hash = MEMHASH(hp->bh_bnum);
749 hhp = mfp->mf_hash[hash];
750 hp->bh_hash_next = hhp;
751 hp->bh_hash_prev = NULL;
752 if (hhp != NULL)
753 hhp->bh_hash_prev = hp;
754 mfp->mf_hash[hash] = hp;
758 * remove block *hp from hashlist of memfile list *mfp
760 static void
761 mf_rem_hash(mfp, hp)
762 memfile_T *mfp;
763 bhdr_T *hp;
765 if (hp->bh_hash_prev == NULL)
766 mfp->mf_hash[MEMHASH(hp->bh_bnum)] = hp->bh_hash_next;
767 else
768 hp->bh_hash_prev->bh_hash_next = hp->bh_hash_next;
770 if (hp->bh_hash_next)
771 hp->bh_hash_next->bh_hash_prev = hp->bh_hash_prev;
775 * look in hash lists of memfile *mfp for block header with number 'nr'
777 static bhdr_T *
778 mf_find_hash(mfp, nr)
779 memfile_T *mfp;
780 blocknr_T nr;
782 bhdr_T *hp;
784 for (hp = mfp->mf_hash[MEMHASH(nr)]; hp != NULL; hp = hp->bh_hash_next)
785 if (hp->bh_bnum == nr)
786 break;
787 return hp;
791 * insert block *hp in front of used list of memfile *mfp
793 static void
794 mf_ins_used(mfp, hp)
795 memfile_T *mfp;
796 bhdr_T *hp;
798 hp->bh_next = mfp->mf_used_first;
799 mfp->mf_used_first = hp;
800 hp->bh_prev = NULL;
801 if (hp->bh_next == NULL) /* list was empty, adjust last pointer */
802 mfp->mf_used_last = hp;
803 else
804 hp->bh_next->bh_prev = hp;
805 mfp->mf_used_count += hp->bh_page_count;
806 total_mem_used += hp->bh_page_count * mfp->mf_page_size;
810 * remove block *hp from used list of memfile *mfp
812 static void
813 mf_rem_used(mfp, hp)
814 memfile_T *mfp;
815 bhdr_T *hp;
817 if (hp->bh_next == NULL) /* last block in used list */
818 mfp->mf_used_last = hp->bh_prev;
819 else
820 hp->bh_next->bh_prev = hp->bh_prev;
821 if (hp->bh_prev == NULL) /* first block in used list */
822 mfp->mf_used_first = hp->bh_next;
823 else
824 hp->bh_prev->bh_next = hp->bh_next;
825 mfp->mf_used_count -= hp->bh_page_count;
826 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
830 * Release the least recently used block from the used list if the number
831 * of used memory blocks gets to big.
833 * Return the block header to the caller, including the memory block, so
834 * it can be re-used. Make sure the page_count is right.
836 static bhdr_T *
837 mf_release(mfp, page_count)
838 memfile_T *mfp;
839 int page_count;
841 bhdr_T *hp;
842 int need_release;
843 buf_T *buf;
845 /* don't release while in mf_close_file() */
846 if (mf_dont_release)
847 return NULL;
850 * Need to release a block if the number of blocks for this memfile is
851 * higher than the maximum or total memory used is over 'maxmemtot'
853 need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max)
854 || (total_mem_used >> 10) >= (long_u)p_mmt);
857 * Try to create a swap file if the amount of memory used is getting too
858 * high.
860 if (mfp->mf_fd < 0 && need_release && p_uc)
862 /* find for which buffer this memfile is */
863 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
864 if (buf->b_ml.ml_mfp == mfp)
865 break;
866 if (buf != NULL && buf->b_may_swap)
867 ml_open_file(buf);
871 * don't release a block if
872 * there is no file for this memfile
873 * or
874 * the number of blocks for this memfile is lower than the maximum
875 * and
876 * total memory used is not up to 'maxmemtot'
878 if (mfp->mf_fd < 0 || !need_release)
879 return NULL;
881 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
882 if (!(hp->bh_flags & BH_LOCKED))
883 break;
884 if (hp == NULL) /* not a single one that can be released */
885 return NULL;
888 * If the block is dirty, write it.
889 * If the write fails we don't free it.
891 if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL)
892 return NULL;
894 mf_rem_used(mfp, hp);
895 mf_rem_hash(mfp, hp);
898 * If a bhdr_T is returned, make sure that the page_count of bh_data is
899 * right
901 if (hp->bh_page_count != page_count)
903 vim_free(hp->bh_data);
904 if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL)
906 vim_free(hp);
907 return NULL;
909 hp->bh_page_count = page_count;
911 return hp;
915 * release as many blocks as possible
916 * Used in case of out of memory
918 * return TRUE if any memory was released
921 mf_release_all()
923 buf_T *buf;
924 memfile_T *mfp;
925 bhdr_T *hp;
926 int retval = FALSE;
928 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
930 mfp = buf->b_ml.ml_mfp;
931 if (mfp != NULL)
933 /* If no swap file yet, may open one */
934 if (mfp->mf_fd < 0 && buf->b_may_swap)
935 ml_open_file(buf);
937 /* only if there is a swapfile */
938 if (mfp->mf_fd >= 0)
940 for (hp = mfp->mf_used_last; hp != NULL; )
942 if (!(hp->bh_flags & BH_LOCKED)
943 && (!(hp->bh_flags & BH_DIRTY)
944 || mf_write(mfp, hp) != FAIL))
946 mf_rem_used(mfp, hp);
947 mf_rem_hash(mfp, hp);
948 mf_free_bhdr(hp);
949 hp = mfp->mf_used_last; /* re-start, list was changed */
950 retval = TRUE;
952 else
953 hp = hp->bh_prev;
958 return retval;
962 * Allocate a block header and a block of memory for it
964 static bhdr_T *
965 mf_alloc_bhdr(mfp, page_count)
966 memfile_T *mfp;
967 int page_count;
969 bhdr_T *hp;
971 if ((hp = (bhdr_T *)alloc((unsigned)sizeof(bhdr_T))) != NULL)
973 if ((hp->bh_data = (char_u *)alloc(mfp->mf_page_size * page_count))
974 == NULL)
976 vim_free(hp); /* not enough memory */
977 return NULL;
979 hp->bh_page_count = page_count;
981 return hp;
985 * Free a block header and the block of memory for it
987 static void
988 mf_free_bhdr(hp)
989 bhdr_T *hp;
991 vim_free(hp->bh_data);
992 vim_free(hp);
996 * insert entry *hp in the free list
998 static void
999 mf_ins_free(mfp, hp)
1000 memfile_T *mfp;
1001 bhdr_T *hp;
1003 hp->bh_next = mfp->mf_free_first;
1004 mfp->mf_free_first = hp;
1008 * remove the first entry from the free list and return a pointer to it
1009 * Note: caller must check that mfp->mf_free_first is not NULL!
1011 static bhdr_T *
1012 mf_rem_free(mfp)
1013 memfile_T *mfp;
1015 bhdr_T *hp;
1017 hp = mfp->mf_free_first;
1018 mfp->mf_free_first = hp->bh_next;
1019 return hp;
1023 * read a block from disk
1025 * Return FAIL for failure, OK otherwise
1027 static int
1028 mf_read(mfp, hp)
1029 memfile_T *mfp;
1030 bhdr_T *hp;
1032 off_t offset;
1033 unsigned page_size;
1034 unsigned size;
1036 if (mfp->mf_fd < 0) /* there is no file, can't read */
1037 return FAIL;
1039 page_size = mfp->mf_page_size;
1040 offset = (off_t)page_size * hp->bh_bnum;
1041 size = page_size * hp->bh_page_count;
1042 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1044 PERROR(_("E294: Seek error in swap file read"));
1045 return FAIL;
1047 if ((unsigned)vim_read(mfp->mf_fd, hp->bh_data, size) != size)
1049 PERROR(_("E295: Read error in swap file"));
1050 return FAIL;
1052 return OK;
1056 * write a block to disk
1058 * Return FAIL for failure, OK otherwise
1060 static int
1061 mf_write(mfp, hp)
1062 memfile_T *mfp;
1063 bhdr_T *hp;
1065 off_t offset; /* offset in the file */
1066 blocknr_T nr; /* block nr which is being written */
1067 bhdr_T *hp2;
1068 unsigned page_size; /* number of bytes in a page */
1069 unsigned page_count; /* number of pages written */
1070 unsigned size; /* number of bytes written */
1072 if (mfp->mf_fd < 0) /* there is no file, can't write */
1073 return FAIL;
1075 if (hp->bh_bnum < 0) /* must assign file block number */
1076 if (mf_trans_add(mfp, hp) == FAIL)
1077 return FAIL;
1079 page_size = mfp->mf_page_size;
1082 * We don't want gaps in the file. Write the blocks in front of *hp
1083 * to extend the file.
1084 * If block 'mf_infile_count' is not in the hash list, it has been
1085 * freed. Fill the space in the file with data from the current block.
1087 for (;;)
1089 nr = hp->bh_bnum;
1090 if (nr > mfp->mf_infile_count) /* beyond end of file */
1092 nr = mfp->mf_infile_count;
1093 hp2 = mf_find_hash(mfp, nr); /* NULL catched below */
1095 else
1096 hp2 = hp;
1098 offset = (off_t)page_size * nr;
1099 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1101 PERROR(_("E296: Seek error in swap file write"));
1102 return FAIL;
1104 if (hp2 == NULL) /* freed block, fill with dummy data */
1105 page_count = 1;
1106 else
1107 page_count = hp2->bh_page_count;
1108 size = page_size * page_count;
1109 if ((unsigned)vim_write(mfp->mf_fd,
1110 (hp2 == NULL ? hp : hp2)->bh_data, size) != size)
1113 * Avoid repeating the error message, this mostly happens when the
1114 * disk is full. We give the message again only after a successful
1115 * write or when hitting a key. We keep on trying, in case some
1116 * space becomes available.
1118 if (!did_swapwrite_msg)
1119 EMSG(_("E297: Write error in swap file"));
1120 did_swapwrite_msg = TRUE;
1121 return FAIL;
1123 did_swapwrite_msg = FALSE;
1124 if (hp2 != NULL) /* written a non-dummy block */
1125 hp2->bh_flags &= ~BH_DIRTY;
1126 /* appended to the file */
1127 if (nr + (blocknr_T)page_count > mfp->mf_infile_count)
1128 mfp->mf_infile_count = nr + page_count;
1129 if (nr == hp->bh_bnum) /* written the desired block */
1130 break;
1132 return OK;
1136 * Make block number for *hp positive and add it to the translation list
1138 * Return FAIL for failure, OK otherwise
1140 static int
1141 mf_trans_add(mfp, hp)
1142 memfile_T *mfp;
1143 bhdr_T *hp;
1145 bhdr_T *freep;
1146 blocknr_T new_bnum;
1147 int hash;
1148 NR_TRANS *np;
1149 int page_count;
1151 if (hp->bh_bnum >= 0) /* it's already positive */
1152 return OK;
1154 if ((np = (NR_TRANS *)alloc((unsigned)sizeof(NR_TRANS))) == NULL)
1155 return FAIL;
1158 * get a new number for the block.
1159 * If the first item in the free list has sufficient pages, use its number
1160 * Otherwise use mf_blocknr_max.
1162 freep = mfp->mf_free_first;
1163 page_count = hp->bh_page_count;
1164 if (freep != NULL && freep->bh_page_count >= page_count)
1166 new_bnum = freep->bh_bnum;
1168 * If the page count of the free block was larger, recude it.
1169 * If the page count matches, remove the block from the free list
1171 if (freep->bh_page_count > page_count)
1173 freep->bh_bnum += page_count;
1174 freep->bh_page_count -= page_count;
1176 else
1178 freep = mf_rem_free(mfp);
1179 vim_free(freep);
1182 else
1184 new_bnum = mfp->mf_blocknr_max;
1185 mfp->mf_blocknr_max += page_count;
1188 np->nt_old_bnum = hp->bh_bnum; /* adjust number */
1189 np->nt_new_bnum = new_bnum;
1191 mf_rem_hash(mfp, hp); /* remove from old hash list */
1192 hp->bh_bnum = new_bnum;
1193 mf_ins_hash(mfp, hp); /* insert in new hash list */
1195 hash = MEMHASH(np->nt_old_bnum); /* insert in trans list */
1196 np->nt_next = mfp->mf_trans[hash];
1197 mfp->mf_trans[hash] = np;
1198 if (np->nt_next != NULL)
1199 np->nt_next->nt_prev = np;
1200 np->nt_prev = NULL;
1202 return OK;
1206 * Lookup a translation from the trans lists and delete the entry
1208 * Return the positive new number when found, the old number when not found
1210 blocknr_T
1211 mf_trans_del(mfp, old_nr)
1212 memfile_T *mfp;
1213 blocknr_T old_nr;
1215 int hash;
1216 NR_TRANS *np;
1217 blocknr_T new_bnum;
1219 hash = MEMHASH(old_nr);
1220 for (np = mfp->mf_trans[hash]; np != NULL; np = np->nt_next)
1221 if (np->nt_old_bnum == old_nr)
1222 break;
1223 if (np == NULL) /* not found */
1224 return old_nr;
1226 mfp->mf_neg_count--;
1227 new_bnum = np->nt_new_bnum;
1228 if (np->nt_prev != NULL) /* remove entry from the trans list */
1229 np->nt_prev->nt_next = np->nt_next;
1230 else
1231 mfp->mf_trans[hash] = np->nt_next;
1232 if (np->nt_next != NULL)
1233 np->nt_next->nt_prev = np->nt_prev;
1234 vim_free(np);
1236 return new_bnum;
1240 * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
1241 * Only called when creating or renaming the swapfile. Either way it's a new
1242 * name so we must work out the full path name.
1244 void
1245 mf_set_ffname(mfp)
1246 memfile_T *mfp;
1248 mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE);
1252 * Make the name of the file used for the memfile a full path.
1253 * Used before doing a :cd
1255 void
1256 mf_fullname(mfp)
1257 memfile_T *mfp;
1259 if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL)
1261 vim_free(mfp->mf_fname);
1262 mfp->mf_fname = mfp->mf_ffname;
1263 mfp->mf_ffname = NULL;
1268 * return TRUE if there are any translations pending for 'mfp'
1271 mf_need_trans(mfp)
1272 memfile_T *mfp;
1274 return (mfp->mf_fname != NULL && mfp->mf_neg_count > 0);
1278 * Open a swap file for a memfile.
1279 * The "fname" must be in allocated memory, and is consumed (also when an
1280 * error occurs).
1282 static void
1283 mf_do_open(mfp, fname, flags)
1284 memfile_T *mfp;
1285 char_u *fname;
1286 int flags; /* flags for open() */
1288 #ifdef HAVE_LSTAT
1289 struct stat sb;
1290 #endif
1292 mfp->mf_fname = fname;
1295 * Get the full path name before the open, because this is
1296 * not possible after the open on the Amiga.
1297 * fname cannot be NameBuff, because it must have been allocated.
1299 mf_set_ffname(mfp);
1300 #if defined(MSDOS) || defined(MSWIN) || defined(RISCOS)
1302 * A ":!cd e:xxx" may change the directory without us knowning, use the
1303 * full pathname always. Careful: This frees fname!
1305 mf_fullname(mfp);
1306 #endif
1308 #ifdef HAVE_LSTAT
1310 * Extra security check: When creating a swap file it really shouldn't
1311 * exist yet. If there is a symbolic link, this is most likely an attack.
1313 if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0)
1315 mfp->mf_fd = -1;
1316 EMSG(_("E300: Swap file already exists (symlink attack?)"));
1318 else
1319 #endif
1322 * try to open the file
1324 flags |= O_EXTRA | O_NOFOLLOW;
1325 #ifdef WIN32
1326 /* Prevent handle inheritance that cause problems with Cscope
1327 * (swap file may not be deleted if cscope connection was open after
1328 * the file) */
1329 flags |= O_NOINHERIT;
1330 #endif
1331 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags);
1335 * If the file cannot be opened, use memory only
1337 if (mfp->mf_fd < 0)
1339 vim_free(mfp->mf_fname);
1340 vim_free(mfp->mf_ffname);
1341 mfp->mf_fname = NULL;
1342 mfp->mf_ffname = NULL;
1344 else
1346 #ifdef HAVE_FD_CLOEXEC
1347 int fdflags = fcntl(mfp->mf_fd, F_GETFD);
1348 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1349 fcntl(mfp->mf_fd, F_SETFD, fdflags | FD_CLOEXEC);
1350 #endif
1351 #ifdef HAVE_SELINUX
1352 mch_copy_sec(fname, mfp->mf_fname);
1353 #endif
1354 mch_hide(mfp->mf_fname); /* try setting the 'hidden' flag */