Changed DiffText and Constant highlight groups
[MacVim/jjgod.git] / src / memfile.c
blobd0dd8deef3fb2aa6d621642a9db1bf5f78d6a06e
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 */
80 static void mf_ins_hash __ARGS((memfile_T *, bhdr_T *));
81 static void mf_rem_hash __ARGS((memfile_T *, bhdr_T *));
82 static bhdr_T *mf_find_hash __ARGS((memfile_T *, blocknr_T));
83 static void mf_ins_used __ARGS((memfile_T *, bhdr_T *));
84 static void mf_rem_used __ARGS((memfile_T *, bhdr_T *));
85 static bhdr_T *mf_release __ARGS((memfile_T *, int));
86 static bhdr_T *mf_alloc_bhdr __ARGS((memfile_T *, int));
87 static void mf_free_bhdr __ARGS((bhdr_T *));
88 static void mf_ins_free __ARGS((memfile_T *, bhdr_T *));
89 static bhdr_T *mf_rem_free __ARGS((memfile_T *));
90 static int mf_read __ARGS((memfile_T *, bhdr_T *));
91 static int mf_write __ARGS((memfile_T *, bhdr_T *));
92 static int mf_trans_add __ARGS((memfile_T *, bhdr_T *));
93 static void mf_do_open __ARGS((memfile_T *, char_u *, int));
96 * The functions for using a memfile:
98 * mf_open() open a new or existing memfile
99 * mf_open_file() open a swap file for an existing memfile
100 * mf_close() close (and delete) a memfile
101 * mf_new() create a new block in a memfile and lock it
102 * mf_get() get an existing block and lock it
103 * mf_put() unlock a block, may be marked for writing
104 * mf_free() remove a block
105 * mf_sync() sync changed parts of memfile to disk
106 * mf_release_all() release as much memory as possible
107 * mf_trans_del() may translate negative to positive block number
108 * mf_fullname() make file name full path (use before first :cd)
112 * Open an existing or new memory block file.
114 * fname: name of file to use (NULL means no file at all)
115 * Note: fname must have been allocated, it is not copied!
116 * If opening the file fails, fname is freed.
117 * flags: flags for open() call
119 * If fname != NULL and file cannot be opened, fail.
121 * return value: identifier for this memory block file.
123 memfile_T *
124 mf_open(fname, flags)
125 char_u *fname;
126 int flags;
128 memfile_T *mfp;
129 int i;
130 off_t size;
131 #if defined(STATFS) && defined(UNIX) && !defined(__QNX__)
132 # define USE_FSTATFS
133 struct STATFS stf;
134 #endif
136 if ((mfp = (memfile_T *)alloc((unsigned)sizeof(memfile_T))) == NULL)
137 return NULL;
139 if (fname == NULL) /* no file for this memfile, use memory only */
141 mfp->mf_fname = NULL;
142 mfp->mf_ffname = NULL;
143 mfp->mf_fd = -1;
145 else
147 mf_do_open(mfp, fname, flags); /* try to open the file */
149 /* if the file cannot be opened, return here */
150 if (mfp->mf_fd < 0)
152 vim_free(mfp);
153 return NULL;
157 mfp->mf_free_first = NULL; /* free list is empty */
158 mfp->mf_used_first = NULL; /* used list is empty */
159 mfp->mf_used_last = NULL;
160 mfp->mf_dirty = FALSE;
161 mfp->mf_used_count = 0;
162 for (i = 0; i < MEMHASHSIZE; ++i)
164 mfp->mf_hash[i] = NULL; /* hash lists are empty */
165 mfp->mf_trans[i] = NULL; /* trans lists are empty */
167 mfp->mf_page_size = MEMFILE_PAGE_SIZE;
169 #ifdef USE_FSTATFS
171 * Try to set the page size equal to the block size of the device.
172 * Speeds up I/O a lot.
173 * When recovering, the actual block size will be retrieved from block 0
174 * in ml_recover(). The size used here may be wrong, therefore
175 * mf_blocknr_max must be rounded up.
177 if (mfp->mf_fd >= 0
178 && fstatfs(mfp->mf_fd, &stf, sizeof(struct statfs), 0) == 0
179 && stf.F_BSIZE >= MIN_SWAP_PAGE_SIZE
180 && stf.F_BSIZE <= MAX_SWAP_PAGE_SIZE)
181 mfp->mf_page_size = stf.F_BSIZE;
182 #endif
184 if (mfp->mf_fd < 0 || (flags & (O_TRUNC|O_EXCL))
185 || (size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0)
186 mfp->mf_blocknr_max = 0; /* no file or empty file */
187 else
188 mfp->mf_blocknr_max = (blocknr_T)((size + mfp->mf_page_size - 1)
189 / mfp->mf_page_size);
190 mfp->mf_blocknr_min = -1;
191 mfp->mf_neg_count = 0;
192 mfp->mf_infile_count = mfp->mf_blocknr_max;
195 * Compute maximum number of pages ('maxmem' is in Kbyte):
196 * 'mammem' * 1Kbyte / page-size-in-bytes.
197 * Avoid overflow by first reducing page size as much as possible.
200 int shift = 10;
201 unsigned page_size = mfp->mf_page_size;
203 while (shift > 0 && (page_size & 1) == 0)
205 page_size = page_size >> 1;
206 --shift;
208 mfp->mf_used_count_max = (p_mm << shift) / page_size;
209 if (mfp->mf_used_count_max < 10)
210 mfp->mf_used_count_max = 10;
213 return mfp;
217 * Open a file for an existing memfile. Used when updatecount set from 0 to
218 * some value.
219 * If the file already exists, this fails.
220 * "fname" is the name of file to use (NULL means no file at all)
221 * Note: "fname" must have been allocated, it is not copied! If opening the
222 * file fails, "fname" is freed.
224 * return value: FAIL if file could not be opened, OK otherwise
227 mf_open_file(mfp, fname)
228 memfile_T *mfp;
229 char_u *fname;
231 mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); /* try to open the file */
233 if (mfp->mf_fd < 0)
234 return FAIL;
236 mfp->mf_dirty = TRUE;
237 return OK;
241 * close a memory file and delete the associated file if 'del_file' is TRUE
243 void
244 mf_close(mfp, del_file)
245 memfile_T *mfp;
246 int del_file;
248 bhdr_T *hp, *nextp;
249 NR_TRANS *tp, *tpnext;
250 int i;
252 if (mfp == NULL) /* safety check */
253 return;
254 if (mfp->mf_fd >= 0)
256 if (close(mfp->mf_fd) < 0)
257 EMSG(_(e_swapclose));
259 if (del_file && mfp->mf_fname != NULL)
260 mch_remove(mfp->mf_fname);
261 /* free entries in used list */
262 for (hp = mfp->mf_used_first; hp != NULL; hp = nextp)
264 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
265 nextp = hp->bh_next;
266 mf_free_bhdr(hp);
268 while (mfp->mf_free_first != NULL) /* free entries in free list */
269 vim_free(mf_rem_free(mfp));
270 for (i = 0; i < MEMHASHSIZE; ++i) /* free entries in trans lists */
271 for (tp = mfp->mf_trans[i]; tp != NULL; tp = tpnext)
273 tpnext = tp->nt_next;
274 vim_free(tp);
276 vim_free(mfp->mf_fname);
277 vim_free(mfp->mf_ffname);
278 vim_free(mfp);
282 * Close the swap file for a memfile. Used when 'swapfile' is reset.
284 void
285 mf_close_file(buf, getlines)
286 buf_T *buf;
287 int getlines; /* get all lines into memory? */
289 memfile_T *mfp;
290 linenr_T lnum;
292 mfp = buf->b_ml.ml_mfp;
293 if (mfp == NULL || mfp->mf_fd < 0) /* nothing to close */
294 return;
296 if (getlines)
298 /* get all blocks in memory by accessing all lines (clumsy!) */
299 mf_dont_release = TRUE;
300 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
301 (void)ml_get_buf(buf, lnum, FALSE);
302 mf_dont_release = FALSE;
303 /* TODO: should check if all blocks are really in core */
306 if (close(mfp->mf_fd) < 0) /* close the file */
307 EMSG(_(e_swapclose));
308 mfp->mf_fd = -1;
310 if (mfp->mf_fname != NULL)
312 mch_remove(mfp->mf_fname); /* delete the swap file */
313 vim_free(mfp->mf_fname);
314 vim_free(mfp->mf_ffname);
315 mfp->mf_fname = NULL;
316 mfp->mf_ffname = NULL;
321 * Set new size for a memfile. Used when block 0 of a swapfile has been read
322 * and the size it indicates differs from what was guessed.
324 void
325 mf_new_page_size(mfp, new_size)
326 memfile_T *mfp;
327 unsigned new_size;
329 /* Correct the memory used for block 0 to the new size, because it will be
330 * freed with that size later on. */
331 total_mem_used += new_size - mfp->mf_page_size;
332 mfp->mf_page_size = new_size;
336 * get a new block
338 * negative: TRUE if negative block number desired (data block)
340 bhdr_T *
341 mf_new(mfp, negative, page_count)
342 memfile_T *mfp;
343 int negative;
344 int page_count;
346 bhdr_T *hp; /* new bhdr_T */
347 bhdr_T *freep; /* first block in free list */
348 char_u *p;
351 * If we reached the maximum size for the used memory blocks, release one
352 * If a bhdr_T is returned, use it and adjust the page_count if necessary.
354 hp = mf_release(mfp, page_count);
357 * Decide on the number to use:
358 * If there is a free block, use its number.
359 * Otherwise use mf_block_min for a negative number, mf_block_max for
360 * a positive number.
362 freep = mfp->mf_free_first;
363 if (!negative && freep != NULL && freep->bh_page_count >= page_count)
366 * If the block in the free list has more pages, take only the number
367 * of pages needed and allocate a new bhdr_T with data
369 * If the number of pages matches and mf_release() did not return a
370 * bhdr_T, use the bhdr_T from the free list and allocate the data
372 * If the number of pages matches and mf_release() returned a bhdr_T,
373 * just use the number and free the bhdr_T from the free list
375 if (freep->bh_page_count > page_count)
377 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
378 return NULL;
379 hp->bh_bnum = freep->bh_bnum;
380 freep->bh_bnum += page_count;
381 freep->bh_page_count -= page_count;
383 else if (hp == NULL) /* need to allocate memory for this block */
385 if ((p = (char_u *)alloc(mfp->mf_page_size * page_count)) == NULL)
386 return NULL;
387 hp = mf_rem_free(mfp);
388 hp->bh_data = p;
390 else /* use the number, remove entry from free list */
392 freep = mf_rem_free(mfp);
393 hp->bh_bnum = freep->bh_bnum;
394 vim_free(freep);
397 else /* get a new number */
399 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
400 return NULL;
401 if (negative)
403 hp->bh_bnum = mfp->mf_blocknr_min--;
404 mfp->mf_neg_count++;
406 else
408 hp->bh_bnum = mfp->mf_blocknr_max;
409 mfp->mf_blocknr_max += page_count;
412 hp->bh_flags = BH_LOCKED | BH_DIRTY; /* new block is always dirty */
413 mfp->mf_dirty = TRUE;
414 hp->bh_page_count = page_count;
415 mf_ins_used(mfp, hp);
416 mf_ins_hash(mfp, hp);
419 * Init the data to all zero, to avoid reading uninitialized data.
420 * This also avoids that the passwd file ends up in the swap file!
422 (void)vim_memset((char *)(hp->bh_data), 0, (size_t)mfp->mf_page_size);
424 return hp;
428 * get existing block 'nr' with 'page_count' pages
430 * Note: The caller should first check a negative nr with mf_trans_del()
432 bhdr_T *
433 mf_get(mfp, nr, page_count)
434 memfile_T *mfp;
435 blocknr_T nr;
436 int page_count;
438 bhdr_T *hp;
439 /* doesn't exist */
440 if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
441 return NULL;
444 * see if it is in the cache
446 hp = mf_find_hash(mfp, nr);
447 if (hp == NULL) /* not in the hash list */
449 if (nr < 0 || nr >= mfp->mf_infile_count) /* can't be in the file */
450 return NULL;
452 /* could check here if the block is in the free list */
455 * Check if we need to flush an existing block.
456 * If so, use that block.
457 * If not, allocate a new block.
459 hp = mf_release(mfp, page_count);
460 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
461 return NULL;
463 hp->bh_bnum = nr;
464 hp->bh_flags = 0;
465 hp->bh_page_count = page_count;
466 if (mf_read(mfp, hp) == FAIL) /* cannot read the block! */
468 mf_free_bhdr(hp);
469 return NULL;
472 else
474 mf_rem_used(mfp, hp); /* remove from list, insert in front below */
475 mf_rem_hash(mfp, hp);
478 hp->bh_flags |= BH_LOCKED;
479 mf_ins_used(mfp, hp); /* put in front of used list */
480 mf_ins_hash(mfp, hp); /* put in front of hash list */
482 return hp;
486 * release the block *hp
488 * dirty: Block must be written to file later
489 * infile: Block should be in file (needed for recovery)
491 * no return value, function cannot fail
493 void
494 mf_put(mfp, hp, dirty, infile)
495 memfile_T *mfp;
496 bhdr_T *hp;
497 int dirty;
498 int infile;
500 int flags;
502 flags = hp->bh_flags;
504 if ((flags & BH_LOCKED) == 0)
505 EMSG(_("E293: block was not locked"));
506 flags &= ~BH_LOCKED;
507 if (dirty)
509 flags |= BH_DIRTY;
510 mfp->mf_dirty = TRUE;
512 hp->bh_flags = flags;
513 if (infile)
514 mf_trans_add(mfp, hp); /* may translate negative in positive nr */
518 * block *hp is no longer in used, may put it in the free list of memfile *mfp
520 void
521 mf_free(mfp, hp)
522 memfile_T *mfp;
523 bhdr_T *hp;
525 vim_free(hp->bh_data); /* free the memory */
526 mf_rem_hash(mfp, hp); /* get *hp out of the hash list */
527 mf_rem_used(mfp, hp); /* get *hp out of the used list */
528 if (hp->bh_bnum < 0)
530 vim_free(hp); /* don't want negative numbers in free list */
531 mfp->mf_neg_count--;
533 else
534 mf_ins_free(mfp, hp); /* put *hp in the free list */
537 #if defined(__MORPHOS__) && defined(__libnix__)
538 /* function is missing in MorphOS libnix version */
539 extern unsigned long *__stdfiledes;
541 static unsigned long
542 fdtofh(int filedescriptor)
544 return __stdfiledes[filedescriptor];
546 #endif
549 * Sync the memory file *mfp to disk.
550 * Flags:
551 * MFS_ALL If not given, blocks with negative numbers are not synced,
552 * even when they are dirty!
553 * MFS_STOP Stop syncing when a character becomes available, but sync at
554 * least one block.
555 * MFS_FLUSH Make sure buffers are flushed to disk, so they will survive a
556 * system crash.
557 * MFS_ZERO Only write block 0.
559 * Return FAIL for failure, OK otherwise
562 mf_sync(mfp, flags)
563 memfile_T *mfp;
564 int flags;
566 int status;
567 bhdr_T *hp;
568 #if defined(SYNC_DUP_CLOSE) && !defined(MSDOS)
569 int fd;
570 #endif
571 int got_int_save = got_int;
573 if (mfp->mf_fd < 0) /* there is no file, nothing to do */
575 mfp->mf_dirty = FALSE;
576 return FAIL;
579 /* Only a CTRL-C while writing will break us here, not one typed
580 * previously. */
581 got_int = FALSE;
584 * sync from last to first (may reduce the probability of an inconsistent
585 * file) If a write fails, it is very likely caused by a full filesystem.
586 * Then we only try to write blocks within the existing file. If that also
587 * fails then we give up.
589 status = OK;
590 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
591 if (((flags & MFS_ALL) || hp->bh_bnum >= 0)
592 && (hp->bh_flags & BH_DIRTY)
593 && (status == OK || (hp->bh_bnum >= 0
594 && hp->bh_bnum < mfp->mf_infile_count)))
596 if ((flags & MFS_ZERO) && hp->bh_bnum != 0)
597 continue;
598 if (mf_write(mfp, hp) == FAIL)
600 if (status == FAIL) /* double error: quit syncing */
601 break;
602 status = FAIL;
604 if (flags & MFS_STOP)
606 /* Stop when char available now. */
607 if (ui_char_avail())
608 break;
610 else
611 ui_breakcheck();
612 if (got_int)
613 break;
617 * If the whole list is flushed, the memfile is not dirty anymore.
618 * In case of an error this flag is also set, to avoid trying all the time.
620 if (hp == NULL || status == FAIL)
621 mfp->mf_dirty = FALSE;
623 if ((flags & MFS_FLUSH) && *p_sws != NUL)
625 #if defined(UNIX)
626 # ifdef HAVE_FSYNC
628 * most Unixes have the very useful fsync() function, just what we need.
629 * However, with OS/2 and EMX it is also available, but there are
630 * reports of bad problems with it (a bug in HPFS.IFS).
631 * So we disable use of it here in case someone tries to be smart
632 * and changes os_os2_cfg.h... (even though there is no __EMX__ test
633 * in the #if, as __EMX__ does not have sync(); we hope for a timely
634 * sync from the system itself).
636 # if defined(__EMX__)
637 error "Dont use fsync with EMX! Read emxdoc.doc or emxfix01.doc for info."
638 # endif
639 if (STRCMP(p_sws, "fsync") == 0)
641 if (fsync(mfp->mf_fd))
642 status = FAIL;
644 else
645 # endif
646 /* OpenNT is strictly POSIX (Benzinger) */
647 /* Tandem/Himalaya NSK-OSS doesn't have sync() */
648 # if defined(__OPENNT) || defined(__TANDEM)
649 fflush(NULL);
650 # else
651 sync();
652 # endif
653 #endif
654 #ifdef VMS
655 if (STRCMP(p_sws, "fsync") == 0)
657 if (fsync(mfp->mf_fd))
658 status = FAIL;
660 #endif
661 #ifdef MSDOS
662 if (_dos_commit(mfp->mf_fd))
663 status = FAIL;
664 #else
665 # ifdef SYNC_DUP_CLOSE
667 * Win32 is a bit more work: Duplicate the file handle and close it.
668 * This should flush the file to disk.
670 if ((fd = dup(mfp->mf_fd)) >= 0)
671 close(fd);
672 # endif
673 #endif
674 #ifdef AMIGA
675 # if defined(__AROS__) || defined(__amigaos4__)
676 if (fsync(mfp->mf_fd) != 0)
677 status = FAIL;
678 # else
680 * Flush() only exists for AmigaDos 2.0.
681 * For 1.3 it should be done with close() + open(), but then the risk
682 * is that the open() may fail and lose the file....
684 # ifdef FEAT_ARP
685 if (dos2)
686 # endif
687 # ifdef SASC
689 struct UFB *fp = chkufb(mfp->mf_fd);
691 if (fp != NULL)
692 Flush(fp->ufbfh);
694 # else
695 # if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
697 # if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__)
698 /* Have function (in libnix at least),
699 * but ain't got no prototype anywhere. */
700 extern unsigned long fdtofh(int filedescriptor);
701 # endif
702 # if !defined(__libnix__)
703 fflush(NULL);
704 # else
705 BPTR fh = (BPTR)fdtofh(mfp->mf_fd);
707 if (fh != 0)
708 Flush(fh);
709 # endif
711 # else /* assume Manx */
712 Flush(_devtab[mfp->mf_fd].fd);
713 # endif
714 # endif
715 # endif
716 #endif /* AMIGA */
719 got_int |= got_int_save;
721 return status;
725 * For all blocks in memory file *mfp that have a positive block number set
726 * the dirty flag. These are blocks that need to be written to a newly
727 * created swapfile.
729 void
730 mf_set_dirty(mfp)
731 memfile_T *mfp;
733 bhdr_T *hp;
735 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
736 if (hp->bh_bnum > 0)
737 hp->bh_flags |= BH_DIRTY;
738 mfp->mf_dirty = TRUE;
742 * insert block *hp in front of hashlist of memfile *mfp
744 static void
745 mf_ins_hash(mfp, hp)
746 memfile_T *mfp;
747 bhdr_T *hp;
749 bhdr_T *hhp;
750 int hash;
752 hash = MEMHASH(hp->bh_bnum);
753 hhp = mfp->mf_hash[hash];
754 hp->bh_hash_next = hhp;
755 hp->bh_hash_prev = NULL;
756 if (hhp != NULL)
757 hhp->bh_hash_prev = hp;
758 mfp->mf_hash[hash] = hp;
762 * remove block *hp from hashlist of memfile list *mfp
764 static void
765 mf_rem_hash(mfp, hp)
766 memfile_T *mfp;
767 bhdr_T *hp;
769 if (hp->bh_hash_prev == NULL)
770 mfp->mf_hash[MEMHASH(hp->bh_bnum)] = hp->bh_hash_next;
771 else
772 hp->bh_hash_prev->bh_hash_next = hp->bh_hash_next;
774 if (hp->bh_hash_next)
775 hp->bh_hash_next->bh_hash_prev = hp->bh_hash_prev;
779 * look in hash lists of memfile *mfp for block header with number 'nr'
781 static bhdr_T *
782 mf_find_hash(mfp, nr)
783 memfile_T *mfp;
784 blocknr_T nr;
786 bhdr_T *hp;
788 for (hp = mfp->mf_hash[MEMHASH(nr)]; hp != NULL; hp = hp->bh_hash_next)
789 if (hp->bh_bnum == nr)
790 break;
791 return hp;
795 * insert block *hp in front of used list of memfile *mfp
797 static void
798 mf_ins_used(mfp, hp)
799 memfile_T *mfp;
800 bhdr_T *hp;
802 hp->bh_next = mfp->mf_used_first;
803 mfp->mf_used_first = hp;
804 hp->bh_prev = NULL;
805 if (hp->bh_next == NULL) /* list was empty, adjust last pointer */
806 mfp->mf_used_last = hp;
807 else
808 hp->bh_next->bh_prev = hp;
809 mfp->mf_used_count += hp->bh_page_count;
810 total_mem_used += hp->bh_page_count * mfp->mf_page_size;
814 * remove block *hp from used list of memfile *mfp
816 static void
817 mf_rem_used(mfp, hp)
818 memfile_T *mfp;
819 bhdr_T *hp;
821 if (hp->bh_next == NULL) /* last block in used list */
822 mfp->mf_used_last = hp->bh_prev;
823 else
824 hp->bh_next->bh_prev = hp->bh_prev;
825 if (hp->bh_prev == NULL) /* first block in used list */
826 mfp->mf_used_first = hp->bh_next;
827 else
828 hp->bh_prev->bh_next = hp->bh_next;
829 mfp->mf_used_count -= hp->bh_page_count;
830 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
834 * Release the least recently used block from the used list if the number
835 * of used memory blocks gets to big.
837 * Return the block header to the caller, including the memory block, so
838 * it can be re-used. Make sure the page_count is right.
840 static bhdr_T *
841 mf_release(mfp, page_count)
842 memfile_T *mfp;
843 int page_count;
845 bhdr_T *hp;
846 int need_release;
847 buf_T *buf;
849 /* don't release while in mf_close_file() */
850 if (mf_dont_release)
851 return NULL;
854 * Need to release a block if the number of blocks for this memfile is
855 * higher than the maximum or total memory used is over 'maxmemtot'
857 need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max)
858 || (total_mem_used >> 10) >= (long_u)p_mmt);
861 * Try to create a swap file if the amount of memory used is getting too
862 * high.
864 if (mfp->mf_fd < 0 && need_release && p_uc)
866 /* find for which buffer this memfile is */
867 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
868 if (buf->b_ml.ml_mfp == mfp)
869 break;
870 if (buf != NULL && buf->b_may_swap)
871 ml_open_file(buf);
875 * don't release a block if
876 * there is no file for this memfile
877 * or
878 * the number of blocks for this memfile is lower than the maximum
879 * and
880 * total memory used is not up to 'maxmemtot'
882 if (mfp->mf_fd < 0 || !need_release)
883 return NULL;
885 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
886 if (!(hp->bh_flags & BH_LOCKED))
887 break;
888 if (hp == NULL) /* not a single one that can be released */
889 return NULL;
892 * If the block is dirty, write it.
893 * If the write fails we don't free it.
895 if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL)
896 return NULL;
898 mf_rem_used(mfp, hp);
899 mf_rem_hash(mfp, hp);
902 * If a bhdr_T is returned, make sure that the page_count of bh_data is
903 * right
905 if (hp->bh_page_count != page_count)
907 vim_free(hp->bh_data);
908 if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL)
910 vim_free(hp);
911 return NULL;
913 hp->bh_page_count = page_count;
915 return hp;
919 * release as many blocks as possible
920 * Used in case of out of memory
922 * return TRUE if any memory was released
925 mf_release_all()
927 buf_T *buf;
928 memfile_T *mfp;
929 bhdr_T *hp;
930 int retval = FALSE;
932 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
934 mfp = buf->b_ml.ml_mfp;
935 if (mfp != NULL)
937 /* If no swap file yet, may open one */
938 if (mfp->mf_fd < 0 && buf->b_may_swap)
939 ml_open_file(buf);
941 /* only if there is a swapfile */
942 if (mfp->mf_fd >= 0)
944 for (hp = mfp->mf_used_last; hp != NULL; )
946 if (!(hp->bh_flags & BH_LOCKED)
947 && (!(hp->bh_flags & BH_DIRTY)
948 || mf_write(mfp, hp) != FAIL))
950 mf_rem_used(mfp, hp);
951 mf_rem_hash(mfp, hp);
952 mf_free_bhdr(hp);
953 hp = mfp->mf_used_last; /* re-start, list was changed */
954 retval = TRUE;
956 else
957 hp = hp->bh_prev;
962 return retval;
966 * Allocate a block header and a block of memory for it
968 static bhdr_T *
969 mf_alloc_bhdr(mfp, page_count)
970 memfile_T *mfp;
971 int page_count;
973 bhdr_T *hp;
975 if ((hp = (bhdr_T *)alloc((unsigned)sizeof(bhdr_T))) != NULL)
977 if ((hp->bh_data = (char_u *)alloc(mfp->mf_page_size * page_count))
978 == NULL)
980 vim_free(hp); /* not enough memory */
981 return NULL;
983 hp->bh_page_count = page_count;
985 return hp;
989 * Free a block header and the block of memory for it
991 static void
992 mf_free_bhdr(hp)
993 bhdr_T *hp;
995 vim_free(hp->bh_data);
996 vim_free(hp);
1000 * insert entry *hp in the free list
1002 static void
1003 mf_ins_free(mfp, hp)
1004 memfile_T *mfp;
1005 bhdr_T *hp;
1007 hp->bh_next = mfp->mf_free_first;
1008 mfp->mf_free_first = hp;
1012 * remove the first entry from the free list and return a pointer to it
1013 * Note: caller must check that mfp->mf_free_first is not NULL!
1015 static bhdr_T *
1016 mf_rem_free(mfp)
1017 memfile_T *mfp;
1019 bhdr_T *hp;
1021 hp = mfp->mf_free_first;
1022 mfp->mf_free_first = hp->bh_next;
1023 return hp;
1027 * read a block from disk
1029 * Return FAIL for failure, OK otherwise
1031 static int
1032 mf_read(mfp, hp)
1033 memfile_T *mfp;
1034 bhdr_T *hp;
1036 off_t offset;
1037 unsigned page_size;
1038 unsigned size;
1040 if (mfp->mf_fd < 0) /* there is no file, can't read */
1041 return FAIL;
1043 page_size = mfp->mf_page_size;
1044 offset = (off_t)page_size * hp->bh_bnum;
1045 size = page_size * hp->bh_page_count;
1046 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1048 PERROR(_("E294: Seek error in swap file read"));
1049 return FAIL;
1051 if ((unsigned)vim_read(mfp->mf_fd, hp->bh_data, size) != size)
1053 PERROR(_("E295: Read error in swap file"));
1054 return FAIL;
1056 return OK;
1060 * write a block to disk
1062 * Return FAIL for failure, OK otherwise
1064 static int
1065 mf_write(mfp, hp)
1066 memfile_T *mfp;
1067 bhdr_T *hp;
1069 off_t offset; /* offset in the file */
1070 blocknr_T nr; /* block nr which is being written */
1071 bhdr_T *hp2;
1072 unsigned page_size; /* number of bytes in a page */
1073 unsigned page_count; /* number of pages written */
1074 unsigned size; /* number of bytes written */
1076 if (mfp->mf_fd < 0) /* there is no file, can't write */
1077 return FAIL;
1079 if (hp->bh_bnum < 0) /* must assign file block number */
1080 if (mf_trans_add(mfp, hp) == FAIL)
1081 return FAIL;
1083 page_size = mfp->mf_page_size;
1086 * We don't want gaps in the file. Write the blocks in front of *hp
1087 * to extend the file.
1088 * If block 'mf_infile_count' is not in the hash list, it has been
1089 * freed. Fill the space in the file with data from the current block.
1091 for (;;)
1093 nr = hp->bh_bnum;
1094 if (nr > mfp->mf_infile_count) /* beyond end of file */
1096 nr = mfp->mf_infile_count;
1097 hp2 = mf_find_hash(mfp, nr); /* NULL catched below */
1099 else
1100 hp2 = hp;
1102 offset = (off_t)page_size * nr;
1103 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1105 PERROR(_("E296: Seek error in swap file write"));
1106 return FAIL;
1108 if (hp2 == NULL) /* freed block, fill with dummy data */
1109 page_count = 1;
1110 else
1111 page_count = hp2->bh_page_count;
1112 size = page_size * page_count;
1113 if ((unsigned)vim_write(mfp->mf_fd,
1114 (hp2 == NULL ? hp : hp2)->bh_data, size) != size)
1117 * Avoid repeating the error message, this mostly happens when the
1118 * disk is full. We give the message again only after a successful
1119 * write or when hitting a key. We keep on trying, in case some
1120 * space becomes available.
1122 if (!did_swapwrite_msg)
1123 EMSG(_("E297: Write error in swap file"));
1124 did_swapwrite_msg = TRUE;
1125 return FAIL;
1127 did_swapwrite_msg = FALSE;
1128 if (hp2 != NULL) /* written a non-dummy block */
1129 hp2->bh_flags &= ~BH_DIRTY;
1130 /* appended to the file */
1131 if (nr + (blocknr_T)page_count > mfp->mf_infile_count)
1132 mfp->mf_infile_count = nr + page_count;
1133 if (nr == hp->bh_bnum) /* written the desired block */
1134 break;
1136 return OK;
1140 * Make block number for *hp positive and add it to the translation list
1142 * Return FAIL for failure, OK otherwise
1144 static int
1145 mf_trans_add(mfp, hp)
1146 memfile_T *mfp;
1147 bhdr_T *hp;
1149 bhdr_T *freep;
1150 blocknr_T new_bnum;
1151 int hash;
1152 NR_TRANS *np;
1153 int page_count;
1155 if (hp->bh_bnum >= 0) /* it's already positive */
1156 return OK;
1158 if ((np = (NR_TRANS *)alloc((unsigned)sizeof(NR_TRANS))) == NULL)
1159 return FAIL;
1162 * get a new number for the block.
1163 * If the first item in the free list has sufficient pages, use its number
1164 * Otherwise use mf_blocknr_max.
1166 freep = mfp->mf_free_first;
1167 page_count = hp->bh_page_count;
1168 if (freep != NULL && freep->bh_page_count >= page_count)
1170 new_bnum = freep->bh_bnum;
1172 * If the page count of the free block was larger, recude it.
1173 * If the page count matches, remove the block from the free list
1175 if (freep->bh_page_count > page_count)
1177 freep->bh_bnum += page_count;
1178 freep->bh_page_count -= page_count;
1180 else
1182 freep = mf_rem_free(mfp);
1183 vim_free(freep);
1186 else
1188 new_bnum = mfp->mf_blocknr_max;
1189 mfp->mf_blocknr_max += page_count;
1192 np->nt_old_bnum = hp->bh_bnum; /* adjust number */
1193 np->nt_new_bnum = new_bnum;
1195 mf_rem_hash(mfp, hp); /* remove from old hash list */
1196 hp->bh_bnum = new_bnum;
1197 mf_ins_hash(mfp, hp); /* insert in new hash list */
1199 hash = MEMHASH(np->nt_old_bnum); /* insert in trans list */
1200 np->nt_next = mfp->mf_trans[hash];
1201 mfp->mf_trans[hash] = np;
1202 if (np->nt_next != NULL)
1203 np->nt_next->nt_prev = np;
1204 np->nt_prev = NULL;
1206 return OK;
1210 * Lookup a translation from the trans lists and delete the entry
1212 * Return the positive new number when found, the old number when not found
1214 blocknr_T
1215 mf_trans_del(mfp, old_nr)
1216 memfile_T *mfp;
1217 blocknr_T old_nr;
1219 int hash;
1220 NR_TRANS *np;
1221 blocknr_T new_bnum;
1223 hash = MEMHASH(old_nr);
1224 for (np = mfp->mf_trans[hash]; np != NULL; np = np->nt_next)
1225 if (np->nt_old_bnum == old_nr)
1226 break;
1227 if (np == NULL) /* not found */
1228 return old_nr;
1230 mfp->mf_neg_count--;
1231 new_bnum = np->nt_new_bnum;
1232 if (np->nt_prev != NULL) /* remove entry from the trans list */
1233 np->nt_prev->nt_next = np->nt_next;
1234 else
1235 mfp->mf_trans[hash] = np->nt_next;
1236 if (np->nt_next != NULL)
1237 np->nt_next->nt_prev = np->nt_prev;
1238 vim_free(np);
1240 return new_bnum;
1244 * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
1245 * Only called when creating or renaming the swapfile. Either way it's a new
1246 * name so we must work out the full path name.
1248 void
1249 mf_set_ffname(mfp)
1250 memfile_T *mfp;
1252 mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE);
1256 * Make the name of the file used for the memfile a full path.
1257 * Used before doing a :cd
1259 void
1260 mf_fullname(mfp)
1261 memfile_T *mfp;
1263 if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL)
1265 vim_free(mfp->mf_fname);
1266 mfp->mf_fname = mfp->mf_ffname;
1267 mfp->mf_ffname = NULL;
1272 * return TRUE if there are any translations pending for 'mfp'
1275 mf_need_trans(mfp)
1276 memfile_T *mfp;
1278 return (mfp->mf_fname != NULL && mfp->mf_neg_count > 0);
1282 * Open a swap file for a memfile.
1283 * The "fname" must be in allocated memory, and is consumed (also when an
1284 * error occurs).
1286 static void
1287 mf_do_open(mfp, fname, flags)
1288 memfile_T *mfp;
1289 char_u *fname;
1290 int flags; /* flags for open() */
1292 #ifdef HAVE_LSTAT
1293 struct stat sb;
1294 #endif
1296 mfp->mf_fname = fname;
1299 * Get the full path name before the open, because this is
1300 * not possible after the open on the Amiga.
1301 * fname cannot be NameBuff, because it must have been allocated.
1303 mf_set_ffname(mfp);
1304 #if defined(MSDOS) || defined(MSWIN) || defined(RISCOS)
1306 * A ":!cd e:xxx" may change the directory without us knowning, use the
1307 * full pathname always. Careful: This frees fname!
1309 mf_fullname(mfp);
1310 #endif
1312 #ifdef HAVE_LSTAT
1314 * Extra security check: When creating a swap file it really shouldn't
1315 * exist yet. If there is a symbolic link, this is most likely an attack.
1317 if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0)
1319 mfp->mf_fd = -1;
1320 EMSG(_("E300: Swap file already exists (symlink attack?)"));
1322 else
1323 #endif
1326 * try to open the file
1328 flags |= O_EXTRA | O_NOFOLLOW;
1329 #ifdef WIN32
1330 /* Prevent handle inheritance that cause problems with Cscope
1331 * (swap file may not be deleted if cscope connection was open after
1332 * the file) */
1333 flags |= O_NOINHERIT;
1334 #endif
1335 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags);
1339 * If the file cannot be opened, use memory only
1341 if (mfp->mf_fd < 0)
1343 vim_free(mfp->mf_fname);
1344 vim_free(mfp->mf_ffname);
1345 mfp->mf_fname = NULL;
1346 mfp->mf_ffname = NULL;
1348 else
1349 mch_hide(mfp->mf_fname); /* try setting the 'hidden' flag */