Patch 7.0.167
[MacVim/jjgod.git] / src / memfile.c
blobeac0c0877ee56e2305b02dbd149e95164d652386
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(WIN32) || defined(_WIN64)
36 # include "vimio.h" /* for lseek(), must be before vim.h */
37 #endif
39 #include "vim.h"
41 #ifdef HAVE_FCNTL_H
42 # include <fcntl.h>
43 #endif
46 * Some systems have the page size in statfs.f_bsize, some in stat.st_blksize
48 #ifdef HAVE_ST_BLKSIZE
49 # define STATFS stat
50 # define F_BSIZE st_blksize
51 # define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
52 #else
53 # ifdef HAVE_SYS_STATFS_H
54 # include <sys/statfs.h>
55 # define STATFS statfs
56 # define F_BSIZE f_bsize
57 # ifdef __MINT__ /* do we still need this? */
58 # define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
59 # endif
60 # endif
61 #endif
64 * for Amiga Dos 2.0x we use Flush
66 #ifdef AMIGA
67 # ifdef FEAT_ARP
68 extern int dos2; /* this is in os_amiga.c */
69 # endif
70 # ifdef SASC
71 # include <proto/dos.h>
72 # include <ios1.h> /* for chkufb() */
73 # endif
74 #endif
76 #define MEMFILE_PAGE_SIZE 4096 /* default page size */
78 static long_u total_mem_used = 0; /* total memory used for memfiles */
79 static int dont_release = FALSE; /* don't release blocks */
81 static void mf_ins_hash __ARGS((memfile_T *, bhdr_T *));
82 static void mf_rem_hash __ARGS((memfile_T *, bhdr_T *));
83 static bhdr_T *mf_find_hash __ARGS((memfile_T *, blocknr_T));
84 static void mf_ins_used __ARGS((memfile_T *, bhdr_T *));
85 static void mf_rem_used __ARGS((memfile_T *, bhdr_T *));
86 static bhdr_T *mf_release __ARGS((memfile_T *, int));
87 static bhdr_T *mf_alloc_bhdr __ARGS((memfile_T *, int));
88 static void mf_free_bhdr __ARGS((bhdr_T *));
89 static void mf_ins_free __ARGS((memfile_T *, bhdr_T *));
90 static bhdr_T *mf_rem_free __ARGS((memfile_T *));
91 static int mf_read __ARGS((memfile_T *, bhdr_T *));
92 static int mf_write __ARGS((memfile_T *, bhdr_T *));
93 static int mf_trans_add __ARGS((memfile_T *, bhdr_T *));
94 static void mf_do_open __ARGS((memfile_T *, char_u *, int));
97 * The functions for using a memfile:
99 * mf_open() open a new or existing memfile
100 * mf_open_file() open a swap file for an existing memfile
101 * mf_close() close (and delete) a memfile
102 * mf_new() create a new block in a memfile and lock it
103 * mf_get() get an existing block and lock it
104 * mf_put() unlock a block, may be marked for writing
105 * mf_free() remove a block
106 * mf_sync() sync changed parts of memfile to disk
107 * mf_release_all() release as much memory as possible
108 * mf_trans_del() may translate negative to positive block number
109 * mf_fullname() make file name full path (use before first :cd)
113 * Open an existing or new memory block file.
115 * fname: name of file to use (NULL means no file at all)
116 * Note: fname must have been allocated, it is not copied!
117 * If opening the file fails, fname is freed.
118 * flags: flags for open() call
120 * If fname != NULL and file cannot be opened, fail.
122 * return value: identifier for this memory block file.
124 memfile_T *
125 mf_open(fname, flags)
126 char_u *fname;
127 int flags;
129 memfile_T *mfp;
130 int i;
131 off_t size;
132 #if defined(STATFS) && defined(UNIX) && !defined(__QNX__)
133 # define USE_FSTATFS
134 struct STATFS stf;
135 #endif
137 if ((mfp = (memfile_T *)alloc((unsigned)sizeof(memfile_T))) == NULL)
138 return NULL;
140 if (fname == NULL) /* no file for this memfile, use memory only */
142 mfp->mf_fname = NULL;
143 mfp->mf_ffname = NULL;
144 mfp->mf_fd = -1;
146 else
148 mf_do_open(mfp, fname, flags); /* try to open the file */
150 /* if the file cannot be opened, return here */
151 if (mfp->mf_fd < 0)
153 vim_free(mfp);
154 return NULL;
158 mfp->mf_free_first = NULL; /* free list is empty */
159 mfp->mf_used_first = NULL; /* used list is empty */
160 mfp->mf_used_last = NULL;
161 mfp->mf_dirty = FALSE;
162 mfp->mf_used_count = 0;
163 for (i = 0; i < MEMHASHSIZE; ++i)
165 mfp->mf_hash[i] = NULL; /* hash lists are empty */
166 mfp->mf_trans[i] = NULL; /* trans lists are empty */
168 mfp->mf_page_size = MEMFILE_PAGE_SIZE;
170 #ifdef USE_FSTATFS
172 * Try to set the page size equal to the block size of the device.
173 * Speeds up I/O a lot.
174 * When recovering, the actual block size will be retrieved from block 0
175 * in ml_recover(). The size used here may be wrong, therefore
176 * mf_blocknr_max must be rounded up.
178 if (mfp->mf_fd >= 0
179 && fstatfs(mfp->mf_fd, &stf, sizeof(struct statfs), 0) == 0
180 && stf.F_BSIZE >= MIN_SWAP_PAGE_SIZE
181 && stf.F_BSIZE <= MAX_SWAP_PAGE_SIZE)
182 mfp->mf_page_size = stf.F_BSIZE;
183 #endif
185 if (mfp->mf_fd < 0 || (flags & (O_TRUNC|O_EXCL))
186 || (size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0)
187 mfp->mf_blocknr_max = 0; /* no file or empty file */
188 else
189 mfp->mf_blocknr_max = (blocknr_T)((size + mfp->mf_page_size - 1)
190 / mfp->mf_page_size);
191 mfp->mf_blocknr_min = -1;
192 mfp->mf_neg_count = 0;
193 mfp->mf_infile_count = mfp->mf_blocknr_max;
194 mfp->mf_used_count_max = p_mm * 1024 / mfp->mf_page_size;
196 return mfp;
200 * Open a file for an existing memfile. Used when updatecount set from 0 to
201 * some value.
202 * If the file already exists, this fails.
203 * "fname" is the name of file to use (NULL means no file at all)
204 * Note: "fname" must have been allocated, it is not copied! If opening the
205 * file fails, "fname" is freed.
207 * return value: FAIL if file could not be opened, OK otherwise
210 mf_open_file(mfp, fname)
211 memfile_T *mfp;
212 char_u *fname;
214 mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); /* try to open the file */
216 if (mfp->mf_fd < 0)
217 return FAIL;
219 mfp->mf_dirty = TRUE;
220 return OK;
224 * close a memory file and delete the associated file if 'del_file' is TRUE
226 void
227 mf_close(mfp, del_file)
228 memfile_T *mfp;
229 int del_file;
231 bhdr_T *hp, *nextp;
232 NR_TRANS *tp, *tpnext;
233 int i;
235 if (mfp == NULL) /* safety check */
236 return;
237 if (mfp->mf_fd >= 0)
239 if (close(mfp->mf_fd) < 0)
240 EMSG(_(e_swapclose));
242 if (del_file && mfp->mf_fname != NULL)
243 mch_remove(mfp->mf_fname);
244 /* free entries in used list */
245 for (hp = mfp->mf_used_first; hp != NULL; hp = nextp)
247 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
248 nextp = hp->bh_next;
249 mf_free_bhdr(hp);
251 while (mfp->mf_free_first != NULL) /* free entries in free list */
252 vim_free(mf_rem_free(mfp));
253 for (i = 0; i < MEMHASHSIZE; ++i) /* free entries in trans lists */
254 for (tp = mfp->mf_trans[i]; tp != NULL; tp = tpnext)
256 tpnext = tp->nt_next;
257 vim_free(tp);
259 vim_free(mfp->mf_fname);
260 vim_free(mfp->mf_ffname);
261 vim_free(mfp);
265 * Close the swap file for a memfile. Used when 'swapfile' is reset.
267 void
268 mf_close_file(buf, getlines)
269 buf_T *buf;
270 int getlines; /* get all lines into memory? */
272 memfile_T *mfp;
273 linenr_T lnum;
275 mfp = buf->b_ml.ml_mfp;
276 if (mfp == NULL || mfp->mf_fd < 0) /* nothing to close */
277 return;
279 if (getlines)
281 /* get all blocks in memory by accessing all lines (clumsy!) */
282 dont_release = TRUE;
283 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
284 (void)ml_get_buf(buf, lnum, FALSE);
285 dont_release = FALSE;
286 /* TODO: should check if all blocks are really in core */
289 if (close(mfp->mf_fd) < 0) /* close the file */
290 EMSG(_(e_swapclose));
291 mfp->mf_fd = -1;
293 if (mfp->mf_fname != NULL)
295 mch_remove(mfp->mf_fname); /* delete the swap file */
296 vim_free(mfp->mf_fname);
297 vim_free(mfp->mf_ffname);
298 mfp->mf_fname = NULL;
299 mfp->mf_ffname = NULL;
304 * Set new size for a memfile. Used when block 0 of a swapfile has been read
305 * and the size it indicates differs from what was guessed.
307 void
308 mf_new_page_size(mfp, new_size)
309 memfile_T *mfp;
310 unsigned new_size;
312 /* Correct the memory used for block 0 to the new size, because it will be
313 * freed with that size later on. */
314 total_mem_used += new_size - mfp->mf_page_size;
315 mfp->mf_page_size = new_size;
319 * get a new block
321 * negative: TRUE if negative block number desired (data block)
323 bhdr_T *
324 mf_new(mfp, negative, page_count)
325 memfile_T *mfp;
326 int negative;
327 int page_count;
329 bhdr_T *hp; /* new bhdr_T */
330 bhdr_T *freep; /* first block in free list */
331 char_u *p;
334 * If we reached the maximum size for the used memory blocks, release one
335 * If a bhdr_T is returned, use it and adjust the page_count if necessary.
337 hp = mf_release(mfp, page_count);
340 * Decide on the number to use:
341 * If there is a free block, use its number.
342 * Otherwise use mf_block_min for a negative number, mf_block_max for
343 * a positive number.
345 freep = mfp->mf_free_first;
346 if (!negative && freep != NULL && freep->bh_page_count >= page_count)
349 * If the block in the free list has more pages, take only the number
350 * of pages needed and allocate a new bhdr_T with data
352 * If the number of pages matches and mf_release did not return a bhdr_T,
353 * use the bhdr_T from the free list and allocate the data
355 * If the number of pages matches and mf_release returned a bhdr_T,
356 * just use the number and free the bhdr_T from the free list
358 if (freep->bh_page_count > page_count)
360 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
361 return NULL;
362 hp->bh_bnum = freep->bh_bnum;
363 freep->bh_bnum += page_count;
364 freep->bh_page_count -= page_count;
366 else if (hp == NULL) /* need to allocate memory for this block */
368 if ((p = (char_u *)alloc(mfp->mf_page_size * page_count)) == NULL)
369 return NULL;
370 hp = mf_rem_free(mfp);
371 hp->bh_data = p;
373 else /* use the number, remove entry from free list */
375 freep = mf_rem_free(mfp);
376 hp->bh_bnum = freep->bh_bnum;
377 vim_free(freep);
380 else /* get a new number */
382 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
383 return NULL;
384 if (negative)
386 hp->bh_bnum = mfp->mf_blocknr_min--;
387 mfp->mf_neg_count++;
389 else
391 hp->bh_bnum = mfp->mf_blocknr_max;
392 mfp->mf_blocknr_max += page_count;
395 hp->bh_flags = BH_LOCKED | BH_DIRTY; /* new block is always dirty */
396 mfp->mf_dirty = TRUE;
397 hp->bh_page_count = page_count;
398 mf_ins_used(mfp, hp);
399 mf_ins_hash(mfp, hp);
402 * Init the data to all zero, to avoid reading uninitialized data.
403 * This also avoids that the passwd file ends up in the swap file!
405 (void)vim_memset((char *)(hp->bh_data), 0, (size_t)mfp->mf_page_size);
407 return hp;
411 * get existing block 'nr' with 'page_count' pages
413 * Note: The caller should first check a negative nr with mf_trans_del()
415 bhdr_T *
416 mf_get(mfp, nr, page_count)
417 memfile_T *mfp;
418 blocknr_T nr;
419 int page_count;
421 bhdr_T *hp;
422 /* doesn't exist */
423 if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
424 return NULL;
427 * see if it is in the cache
429 hp = mf_find_hash(mfp, nr);
430 if (hp == NULL) /* not in the hash list */
432 if (nr < 0 || nr >= mfp->mf_infile_count) /* can't be in the file */
433 return NULL;
435 /* could check here if the block is in the free list */
438 * Check if we need to flush an existing block.
439 * If so, use that block.
440 * If not, allocate a new block.
442 hp = mf_release(mfp, page_count);
443 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
444 return NULL;
446 hp->bh_bnum = nr;
447 hp->bh_flags = 0;
448 hp->bh_page_count = page_count;
449 if (mf_read(mfp, hp) == FAIL) /* cannot read the block! */
451 mf_free_bhdr(hp);
452 return NULL;
455 else
457 mf_rem_used(mfp, hp); /* remove from list, insert in front below */
458 mf_rem_hash(mfp, hp);
461 hp->bh_flags |= BH_LOCKED;
462 mf_ins_used(mfp, hp); /* put in front of used list */
463 mf_ins_hash(mfp, hp); /* put in front of hash list */
465 return hp;
469 * release the block *hp
471 * dirty: Block must be written to file later
472 * infile: Block should be in file (needed for recovery)
474 * no return value, function cannot fail
476 void
477 mf_put(mfp, hp, dirty, infile)
478 memfile_T *mfp;
479 bhdr_T *hp;
480 int dirty;
481 int infile;
483 int flags;
485 flags = hp->bh_flags;
487 if ((flags & BH_LOCKED) == 0)
488 EMSG(_("E293: block was not locked"));
489 flags &= ~BH_LOCKED;
490 if (dirty)
492 flags |= BH_DIRTY;
493 mfp->mf_dirty = TRUE;
495 hp->bh_flags = flags;
496 if (infile)
497 mf_trans_add(mfp, hp); /* may translate negative in positive nr */
501 * block *hp is no longer in used, may put it in the free list of memfile *mfp
503 void
504 mf_free(mfp, hp)
505 memfile_T *mfp;
506 bhdr_T *hp;
508 vim_free(hp->bh_data); /* free the memory */
509 mf_rem_hash(mfp, hp); /* get *hp out of the hash list */
510 mf_rem_used(mfp, hp); /* get *hp out of the used list */
511 if (hp->bh_bnum < 0)
513 vim_free(hp); /* don't want negative numbers in free list */
514 mfp->mf_neg_count--;
516 else
517 mf_ins_free(mfp, hp); /* put *hp in the free list */
520 #if defined(__MORPHOS__) && defined(__libnix__)
521 /* function is missing in MorphOS libnix version */
522 extern unsigned long *__stdfiledes;
524 static unsigned long
525 fdtofh(int filedescriptor)
527 return __stdfiledes[filedescriptor];
529 #endif
532 * Sync the memory file *mfp to disk.
533 * Flags:
534 * MFS_ALL If not given, blocks with negative numbers are not synced,
535 * even when they are dirty!
536 * MFS_STOP Stop syncing when a character becomes available, but sync at
537 * least one block.
538 * MFS_FLUSH Make sure buffers are flushed to disk, so they will survive a
539 * system crash.
540 * MFS_ZERO Only write block 0.
542 * Return FAIL for failure, OK otherwise
545 mf_sync(mfp, flags)
546 memfile_T *mfp;
547 int flags;
549 int status;
550 bhdr_T *hp;
551 #if defined(SYNC_DUP_CLOSE) && !defined(MSDOS)
552 int fd;
553 #endif
554 int got_int_save = got_int;
556 if (mfp->mf_fd < 0) /* there is no file, nothing to do */
558 mfp->mf_dirty = FALSE;
559 return FAIL;
562 /* Only a CTRL-C while writing will break us here, not one typed
563 * previously. */
564 got_int = FALSE;
567 * sync from last to first (may reduce the probability of an inconsistent
568 * file) If a write fails, it is very likely caused by a full filesystem.
569 * Then we only try to write blocks within the existing file. If that also
570 * fails then we give up.
572 status = OK;
573 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
574 if (((flags & MFS_ALL) || hp->bh_bnum >= 0)
575 && (hp->bh_flags & BH_DIRTY)
576 && (status == OK || (hp->bh_bnum >= 0
577 && hp->bh_bnum < mfp->mf_infile_count)))
579 if ((flags & MFS_ZERO) && hp->bh_bnum != 0)
580 continue;
581 if (mf_write(mfp, hp) == FAIL)
583 if (status == FAIL) /* double error: quit syncing */
584 break;
585 status = FAIL;
587 if (flags & MFS_STOP)
589 /* Stop when char available now. */
590 if (ui_char_avail())
591 break;
593 else
594 ui_breakcheck();
595 if (got_int)
596 break;
600 * If the whole list is flushed, the memfile is not dirty anymore.
601 * In case of an error this flag is also set, to avoid trying all the time.
603 if (hp == NULL || status == FAIL)
604 mfp->mf_dirty = FALSE;
606 if ((flags & MFS_FLUSH) && *p_sws != NUL)
608 #if defined(UNIX)
609 # ifdef HAVE_FSYNC
611 * most Unixes have the very useful fsync() function, just what we need.
612 * However, with OS/2 and EMX it is also available, but there are
613 * reports of bad problems with it (a bug in HPFS.IFS).
614 * So we disable use of it here in case someone tries to be smart
615 * and changes os_os2_cfg.h... (even though there is no __EMX__ test
616 * in the #if, as __EMX__ does not have sync(); we hope for a timely
617 * sync from the system itself).
619 # if defined(__EMX__)
620 error "Dont use fsync with EMX! Read emxdoc.doc or emxfix01.doc for info."
621 # endif
622 if (STRCMP(p_sws, "fsync") == 0)
624 if (fsync(mfp->mf_fd))
625 status = FAIL;
627 else
628 # endif
629 /* OpenNT is strictly POSIX (Benzinger) */
630 /* Tandem/Himalaya NSK-OSS doesn't have sync() */
631 # if defined(__OPENNT) || defined(__TANDEM)
632 fflush(NULL);
633 # else
634 sync();
635 # endif
636 #endif
637 #ifdef VMS
638 if (STRCMP(p_sws, "fsync") == 0)
640 if (fsync(mfp->mf_fd))
641 status = FAIL;
643 #endif
644 #ifdef MSDOS
645 if (_dos_commit(mfp->mf_fd))
646 status = FAIL;
647 #else
648 # ifdef SYNC_DUP_CLOSE
650 * Win32 is a bit more work: Duplicate the file handle and close it.
651 * This should flush the file to disk.
653 if ((fd = dup(mfp->mf_fd)) >= 0)
654 close(fd);
655 # endif
656 #endif
657 #ifdef AMIGA
658 # if defined(__AROS__) || defined(__amigaos4__)
659 if (fsync(mfp->mf_fd) != 0)
660 status = FAIL;
661 # else
663 * Flush() only exists for AmigaDos 2.0.
664 * For 1.3 it should be done with close() + open(), but then the risk
665 * is that the open() may fail and lose the file....
667 # ifdef FEAT_ARP
668 if (dos2)
669 # endif
670 # ifdef SASC
672 struct UFB *fp = chkufb(mfp->mf_fd);
674 if (fp != NULL)
675 Flush(fp->ufbfh);
677 # else
678 # if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
680 # if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__)
681 /* Have function (in libnix at least),
682 * but ain't got no prototype anywhere. */
683 extern unsigned long fdtofh(int filedescriptor);
684 # endif
685 # if !defined(__libnix__)
686 fflush(NULL);
687 # else
688 BPTR fh = (BPTR)fdtofh(mfp->mf_fd);
690 if (fh != 0)
691 Flush(fh);
692 # endif
694 # else /* assume Manx */
695 Flush(_devtab[mfp->mf_fd].fd);
696 # endif
697 # endif
698 # endif
699 #endif /* AMIGA */
702 got_int |= got_int_save;
704 return status;
708 * For all blocks in memory file *mfp that have a positive block number set
709 * the dirty flag. These are blocks that need to be written to a newly
710 * created swapfile.
712 void
713 mf_set_dirty(mfp)
714 memfile_T *mfp;
716 bhdr_T *hp;
718 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
719 if (hp->bh_bnum > 0)
720 hp->bh_flags |= BH_DIRTY;
721 mfp->mf_dirty = TRUE;
725 * insert block *hp in front of hashlist of memfile *mfp
727 static void
728 mf_ins_hash(mfp, hp)
729 memfile_T *mfp;
730 bhdr_T *hp;
732 bhdr_T *hhp;
733 int hash;
735 hash = MEMHASH(hp->bh_bnum);
736 hhp = mfp->mf_hash[hash];
737 hp->bh_hash_next = hhp;
738 hp->bh_hash_prev = NULL;
739 if (hhp != NULL)
740 hhp->bh_hash_prev = hp;
741 mfp->mf_hash[hash] = hp;
745 * remove block *hp from hashlist of memfile list *mfp
747 static void
748 mf_rem_hash(mfp, hp)
749 memfile_T *mfp;
750 bhdr_T *hp;
752 if (hp->bh_hash_prev == NULL)
753 mfp->mf_hash[MEMHASH(hp->bh_bnum)] = hp->bh_hash_next;
754 else
755 hp->bh_hash_prev->bh_hash_next = hp->bh_hash_next;
757 if (hp->bh_hash_next)
758 hp->bh_hash_next->bh_hash_prev = hp->bh_hash_prev;
762 * look in hash lists of memfile *mfp for block header with number 'nr'
764 static bhdr_T *
765 mf_find_hash(mfp, nr)
766 memfile_T *mfp;
767 blocknr_T nr;
769 bhdr_T *hp;
771 for (hp = mfp->mf_hash[MEMHASH(nr)]; hp != NULL; hp = hp->bh_hash_next)
772 if (hp->bh_bnum == nr)
773 break;
774 return hp;
778 * insert block *hp in front of used list of memfile *mfp
780 static void
781 mf_ins_used(mfp, hp)
782 memfile_T *mfp;
783 bhdr_T *hp;
785 hp->bh_next = mfp->mf_used_first;
786 mfp->mf_used_first = hp;
787 hp->bh_prev = NULL;
788 if (hp->bh_next == NULL) /* list was empty, adjust last pointer */
789 mfp->mf_used_last = hp;
790 else
791 hp->bh_next->bh_prev = hp;
792 mfp->mf_used_count += hp->bh_page_count;
793 total_mem_used += hp->bh_page_count * mfp->mf_page_size;
797 * remove block *hp from used list of memfile *mfp
799 static void
800 mf_rem_used(mfp, hp)
801 memfile_T *mfp;
802 bhdr_T *hp;
804 if (hp->bh_next == NULL) /* last block in used list */
805 mfp->mf_used_last = hp->bh_prev;
806 else
807 hp->bh_next->bh_prev = hp->bh_prev;
808 if (hp->bh_prev == NULL) /* first block in used list */
809 mfp->mf_used_first = hp->bh_next;
810 else
811 hp->bh_prev->bh_next = hp->bh_next;
812 mfp->mf_used_count -= hp->bh_page_count;
813 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
817 * Release the least recently used block from the used list if the number
818 * of used memory blocks gets to big.
820 * Return the block header to the caller, including the memory block, so
821 * it can be re-used. Make sure the page_count is right.
823 static bhdr_T *
824 mf_release(mfp, page_count)
825 memfile_T *mfp;
826 int page_count;
828 bhdr_T *hp;
829 int need_release;
830 buf_T *buf;
832 /* don't release while in mf_close_file() */
833 if (dont_release)
834 return NULL;
837 * Need to release a block if the number of blocks for this memfile is
838 * higher than the maximum or total memory used is over 'maxmemtot'
840 need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max)
841 || (total_mem_used >> 10) >= (long_u)p_mmt);
844 * Try to create a swap file if the amount of memory used is getting too
845 * high.
847 if (mfp->mf_fd < 0 && need_release && p_uc)
849 /* find for which buffer this memfile is */
850 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
851 if (buf->b_ml.ml_mfp == mfp)
852 break;
853 if (buf != NULL && buf->b_may_swap)
854 ml_open_file(buf);
858 * don't release a block if
859 * there is no file for this memfile
860 * or
861 * the number of blocks for this memfile is lower than the maximum
862 * and
863 * total memory used is not up to 'maxmemtot'
865 if (mfp->mf_fd < 0 || !need_release)
866 return NULL;
868 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
869 if (!(hp->bh_flags & BH_LOCKED))
870 break;
871 if (hp == NULL) /* not a single one that can be released */
872 return NULL;
875 * If the block is dirty, write it.
876 * If the write fails we don't free it.
878 if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL)
879 return NULL;
881 mf_rem_used(mfp, hp);
882 mf_rem_hash(mfp, hp);
885 * If a bhdr_T is returned, make sure that the page_count of bh_data is
886 * right
888 if (hp->bh_page_count != page_count)
890 vim_free(hp->bh_data);
891 if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL)
893 vim_free(hp);
894 return NULL;
896 hp->bh_page_count = page_count;
898 return hp;
902 * release as many blocks as possible
903 * Used in case of out of memory
905 * return TRUE if any memory was released
908 mf_release_all()
910 buf_T *buf;
911 memfile_T *mfp;
912 bhdr_T *hp;
913 int retval = FALSE;
915 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
917 mfp = buf->b_ml.ml_mfp;
918 if (mfp != NULL)
920 /* If no swap file yet, may open one */
921 if (mfp->mf_fd < 0 && buf->b_may_swap)
922 ml_open_file(buf);
924 /* only if there is a swapfile */
925 if (mfp->mf_fd >= 0)
927 for (hp = mfp->mf_used_last; hp != NULL; )
929 if (!(hp->bh_flags & BH_LOCKED)
930 && (!(hp->bh_flags & BH_DIRTY)
931 || mf_write(mfp, hp) != FAIL))
933 mf_rem_used(mfp, hp);
934 mf_rem_hash(mfp, hp);
935 mf_free_bhdr(hp);
936 hp = mfp->mf_used_last; /* re-start, list was changed */
937 retval = TRUE;
939 else
940 hp = hp->bh_prev;
945 return retval;
949 * Allocate a block header and a block of memory for it
951 static bhdr_T *
952 mf_alloc_bhdr(mfp, page_count)
953 memfile_T *mfp;
954 int page_count;
956 bhdr_T *hp;
958 if ((hp = (bhdr_T *)alloc((unsigned)sizeof(bhdr_T))) != NULL)
960 if ((hp->bh_data = (char_u *)alloc(mfp->mf_page_size * page_count))
961 == NULL)
963 vim_free(hp); /* not enough memory */
964 return NULL;
966 hp->bh_page_count = page_count;
968 return hp;
972 * Free a block header and the block of memory for it
974 static void
975 mf_free_bhdr(hp)
976 bhdr_T *hp;
978 vim_free(hp->bh_data);
979 vim_free(hp);
983 * insert entry *hp in the free list
985 static void
986 mf_ins_free(mfp, hp)
987 memfile_T *mfp;
988 bhdr_T *hp;
990 hp->bh_next = mfp->mf_free_first;
991 mfp->mf_free_first = hp;
995 * remove the first entry from the free list and return a pointer to it
996 * Note: caller must check that mfp->mf_free_first is not NULL!
998 static bhdr_T *
999 mf_rem_free(mfp)
1000 memfile_T *mfp;
1002 bhdr_T *hp;
1004 hp = mfp->mf_free_first;
1005 mfp->mf_free_first = hp->bh_next;
1006 return hp;
1010 * read a block from disk
1012 * Return FAIL for failure, OK otherwise
1014 static int
1015 mf_read(mfp, hp)
1016 memfile_T *mfp;
1017 bhdr_T *hp;
1019 off_t offset;
1020 unsigned page_size;
1021 unsigned size;
1023 if (mfp->mf_fd < 0) /* there is no file, can't read */
1024 return FAIL;
1026 page_size = mfp->mf_page_size;
1027 offset = (off_t)page_size * hp->bh_bnum;
1028 size = page_size * hp->bh_page_count;
1029 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1031 PERROR(_("E294: Seek error in swap file read"));
1032 return FAIL;
1034 if ((unsigned)vim_read(mfp->mf_fd, hp->bh_data, size) != size)
1036 PERROR(_("E295: Read error in swap file"));
1037 return FAIL;
1039 return OK;
1043 * write a block to disk
1045 * Return FAIL for failure, OK otherwise
1047 static int
1048 mf_write(mfp, hp)
1049 memfile_T *mfp;
1050 bhdr_T *hp;
1052 off_t offset; /* offset in the file */
1053 blocknr_T nr; /* block nr which is being written */
1054 bhdr_T *hp2;
1055 unsigned page_size; /* number of bytes in a page */
1056 unsigned page_count; /* number of pages written */
1057 unsigned size; /* number of bytes written */
1059 if (mfp->mf_fd < 0) /* there is no file, can't write */
1060 return FAIL;
1062 if (hp->bh_bnum < 0) /* must assign file block number */
1063 if (mf_trans_add(mfp, hp) == FAIL)
1064 return FAIL;
1066 page_size = mfp->mf_page_size;
1069 * We don't want gaps in the file. Write the blocks in front of *hp
1070 * to extend the file.
1071 * If block 'mf_infile_count' is not in the hash list, it has been
1072 * freed. Fill the space in the file with data from the current block.
1074 for (;;)
1076 nr = hp->bh_bnum;
1077 if (nr > mfp->mf_infile_count) /* beyond end of file */
1079 nr = mfp->mf_infile_count;
1080 hp2 = mf_find_hash(mfp, nr); /* NULL catched below */
1082 else
1083 hp2 = hp;
1085 offset = (off_t)page_size * nr;
1086 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1088 PERROR(_("E296: Seek error in swap file write"));
1089 return FAIL;
1091 if (hp2 == NULL) /* freed block, fill with dummy data */
1092 page_count = 1;
1093 else
1094 page_count = hp2->bh_page_count;
1095 size = page_size * page_count;
1096 if ((unsigned)vim_write(mfp->mf_fd,
1097 (hp2 == NULL ? hp : hp2)->bh_data, size) != size)
1100 * Avoid repeating the error message, this mostly happens when the
1101 * disk is full. We give the message again only after a succesful
1102 * write or when hitting a key. We keep on trying, in case some
1103 * space becomes available.
1105 if (!did_swapwrite_msg)
1106 EMSG(_("E297: Write error in swap file"));
1107 did_swapwrite_msg = TRUE;
1108 return FAIL;
1110 did_swapwrite_msg = FALSE;
1111 if (hp2 != NULL) /* written a non-dummy block */
1112 hp2->bh_flags &= ~BH_DIRTY;
1113 /* appended to the file */
1114 if (nr + (blocknr_T)page_count > mfp->mf_infile_count)
1115 mfp->mf_infile_count = nr + page_count;
1116 if (nr == hp->bh_bnum) /* written the desired block */
1117 break;
1119 return OK;
1123 * Make block number for *hp positive and add it to the translation list
1125 * Return FAIL for failure, OK otherwise
1127 static int
1128 mf_trans_add(mfp, hp)
1129 memfile_T *mfp;
1130 bhdr_T *hp;
1132 bhdr_T *freep;
1133 blocknr_T new_bnum;
1134 int hash;
1135 NR_TRANS *np;
1136 int page_count;
1138 if (hp->bh_bnum >= 0) /* it's already positive */
1139 return OK;
1141 if ((np = (NR_TRANS *)alloc((unsigned)sizeof(NR_TRANS))) == NULL)
1142 return FAIL;
1145 * get a new number for the block.
1146 * If the first item in the free list has sufficient pages, use its number
1147 * Otherwise use mf_blocknr_max.
1149 freep = mfp->mf_free_first;
1150 page_count = hp->bh_page_count;
1151 if (freep != NULL && freep->bh_page_count >= page_count)
1153 new_bnum = freep->bh_bnum;
1155 * If the page count of the free block was larger, recude it.
1156 * If the page count matches, remove the block from the free list
1158 if (freep->bh_page_count > page_count)
1160 freep->bh_bnum += page_count;
1161 freep->bh_page_count -= page_count;
1163 else
1165 freep = mf_rem_free(mfp);
1166 vim_free(freep);
1169 else
1171 new_bnum = mfp->mf_blocknr_max;
1172 mfp->mf_blocknr_max += page_count;
1175 np->nt_old_bnum = hp->bh_bnum; /* adjust number */
1176 np->nt_new_bnum = new_bnum;
1178 mf_rem_hash(mfp, hp); /* remove from old hash list */
1179 hp->bh_bnum = new_bnum;
1180 mf_ins_hash(mfp, hp); /* insert in new hash list */
1182 hash = MEMHASH(np->nt_old_bnum); /* insert in trans list */
1183 np->nt_next = mfp->mf_trans[hash];
1184 mfp->mf_trans[hash] = np;
1185 if (np->nt_next != NULL)
1186 np->nt_next->nt_prev = np;
1187 np->nt_prev = NULL;
1189 return OK;
1193 * Lookup a tranlation from the trans lists and delete the entry
1195 * Return the positive new number when found, the old number when not found
1197 blocknr_T
1198 mf_trans_del(mfp, old_nr)
1199 memfile_T *mfp;
1200 blocknr_T old_nr;
1202 int hash;
1203 NR_TRANS *np;
1204 blocknr_T new_bnum;
1206 hash = MEMHASH(old_nr);
1207 for (np = mfp->mf_trans[hash]; np != NULL; np = np->nt_next)
1208 if (np->nt_old_bnum == old_nr)
1209 break;
1210 if (np == NULL) /* not found */
1211 return old_nr;
1213 mfp->mf_neg_count--;
1214 new_bnum = np->nt_new_bnum;
1215 if (np->nt_prev != NULL) /* remove entry from the trans list */
1216 np->nt_prev->nt_next = np->nt_next;
1217 else
1218 mfp->mf_trans[hash] = np->nt_next;
1219 if (np->nt_next != NULL)
1220 np->nt_next->nt_prev = np->nt_prev;
1221 vim_free(np);
1223 return new_bnum;
1227 * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
1228 * Only called when creating or renaming the swapfile. Either way it's a new
1229 * name so we must work out the full path name.
1231 void
1232 mf_set_ffname(mfp)
1233 memfile_T *mfp;
1235 mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE);
1239 * Make the name of the file used for the memfile a full path.
1240 * Used before doing a :cd
1242 void
1243 mf_fullname(mfp)
1244 memfile_T *mfp;
1246 if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL)
1248 vim_free(mfp->mf_fname);
1249 mfp->mf_fname = mfp->mf_ffname;
1250 mfp->mf_ffname = NULL;
1255 * return TRUE if there are any translations pending for 'mfp'
1258 mf_need_trans(mfp)
1259 memfile_T *mfp;
1261 return (mfp->mf_fname != NULL && mfp->mf_neg_count > 0);
1265 * Open a swap file for a memfile.
1266 * The "fname" must be in allocated memory, and is consumed (also when an
1267 * error occurs).
1269 static void
1270 mf_do_open(mfp, fname, flags)
1271 memfile_T *mfp;
1272 char_u *fname;
1273 int flags; /* flags for open() */
1275 #ifdef HAVE_LSTAT
1276 struct stat sb;
1277 #endif
1279 mfp->mf_fname = fname;
1282 * Get the full path name before the open, because this is
1283 * not possible after the open on the Amiga.
1284 * fname cannot be NameBuff, because it must have been allocated.
1286 mf_set_ffname(mfp);
1287 #if defined(MSDOS) || defined(MSWIN) || defined(RISCOS)
1289 * A ":!cd e:xxx" may change the directory without us knowning, use the
1290 * full pathname always. Careful: This frees fname!
1292 mf_fullname(mfp);
1293 #endif
1295 #ifdef HAVE_LSTAT
1297 * Extra security check: When creating a swap file it really shouldn't
1298 * exist yet. If there is a symbolic link, this is most likely an attack.
1300 if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0)
1302 mfp->mf_fd = -1;
1303 EMSG(_("E300: Swap file already exists (symlink attack?)"));
1305 else
1306 #endif
1309 * try to open the file
1311 flags |= O_EXTRA | O_NOFOLLOW;
1312 #ifdef WIN32
1313 /* Prevent handle inheritance that cause problems with Cscope
1314 * (swap file may not be deleted if cscope connection was open after
1315 * the file) */
1316 flags |= O_NOINHERIT;
1317 #endif
1318 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags);
1322 * If the file cannot be opened, use memory only
1324 if (mfp->mf_fd < 0)
1326 vim_free(mfp->mf_fname);
1327 vim_free(mfp->mf_ffname);
1328 mfp->mf_fname = NULL;
1329 mfp->mf_ffname = NULL;
1331 else
1332 mch_hide(mfp->mf_fname); /* try setting the 'hidden' flag */