blob: 8c41832c208e2b4d991c85e1efce253b687e5c76 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
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 */
9
10/*
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.
13 *
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.
21 *
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.
25 *
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.
29 *
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.
33 */
34
35#if defined MSDOS || defined(WIN32) || defined(_WIN64)
36# include <io.h> /* for lseek(), must be before vim.h */
37#endif
38
39#include "vim.h"
40
41#ifdef HAVE_FCNTL_H
42# include <fcntl.h>
43#endif
44
45/*
46 * Some systems have the page size in statfs.f_bsize, some in stat.st_blksize
47 */
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
62
63/*
64 * for Amiga Dos 2.0x we use Flush
65 */
66#ifdef AMIGA
67# ifdef FEAT_ARP
68extern 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
75
76#define MEMFILE_PAGE_SIZE 4096 /* default page size */
77
78static long_u total_mem_used = 0; /* total memory used for memfiles */
79static int dont_release = FALSE; /* don't release blocks */
80
81static void mf_ins_hash __ARGS((memfile_T *, bhdr_T *));
82static void mf_rem_hash __ARGS((memfile_T *, bhdr_T *));
83static bhdr_T *mf_find_hash __ARGS((memfile_T *, blocknr_T));
84static void mf_ins_used __ARGS((memfile_T *, bhdr_T *));
85static void mf_rem_used __ARGS((memfile_T *, bhdr_T *));
86static bhdr_T *mf_release __ARGS((memfile_T *, int));
87static bhdr_T *mf_alloc_bhdr __ARGS((memfile_T *, int));
88static void mf_free_bhdr __ARGS((bhdr_T *));
89static void mf_ins_free __ARGS((memfile_T *, bhdr_T *));
90static bhdr_T *mf_rem_free __ARGS((memfile_T *));
91static int mf_read __ARGS((memfile_T *, bhdr_T *));
92static int mf_write __ARGS((memfile_T *, bhdr_T *));
93static int mf_trans_add __ARGS((memfile_T *, bhdr_T *));
94static void mf_do_open __ARGS((memfile_T *, char_u *, int));
95
96/*
97 * The functions for using a memfile:
98 *
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)
110 */
111
112/*
113 * Open an existing or new memory block file.
114 *
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
119 *
120 * If fname != NULL and file cannot be opened, fail.
121 *
122 * return value: identifier for this memory block file.
123 */
124 memfile_T *
125mf_open(fname, flags)
126 char_u *fname;
127 int flags;
128{
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
136
137 if ((mfp = (memfile_T *)alloc((unsigned)sizeof(memfile_T))) == NULL)
138 return NULL;
139
140 if (fname == NULL) /* no file for this memfile, use memory only */
141 {
142 mfp->mf_fname = NULL;
143 mfp->mf_ffname = NULL;
144 mfp->mf_fd = -1;
145 }
146 else
147 {
148 mf_do_open(mfp, fname, flags); /* try to open the file */
149
150 /* if the file cannot be opened, return here */
151 if (mfp->mf_fd < 0)
152 {
153 vim_free(mfp);
154 return NULL;
155 }
156 }
157
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)
164 {
165 mfp->mf_hash[i] = NULL; /* hash lists are empty */
166 mfp->mf_trans[i] = NULL; /* trans lists are empty */
167 }
168 mfp->mf_page_size = MEMFILE_PAGE_SIZE;
169
170#ifdef USE_FSTATFS
171 /*
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.
177 */
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
184
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;
195
196 return mfp;
197}
198
199/*
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.
206 *
207 * return value: FAIL if file could not be opened, OK otherwise
208 */
209 int
210mf_open_file(mfp, fname)
211 memfile_T *mfp;
212 char_u *fname;
213{
214 mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); /* try to open the file */
215
216 if (mfp->mf_fd < 0)
217 return FAIL;
218
219 mfp->mf_dirty = TRUE;
220 return OK;
221}
222
223/*
224 * close a memory file and delete the associated file if 'del_file' is TRUE
225 */
226 void
227mf_close(mfp, del_file)
228 memfile_T *mfp;
229 int del_file;
230{
231 bhdr_T *hp, *nextp;
232 NR_TRANS *tp, *tpnext;
233 int i;
234
235 if (mfp == NULL) /* safety check */
236 return;
237 if (mfp->mf_fd >= 0)
238 {
239 if (close(mfp->mf_fd) < 0)
240 EMSG(_(e_swapclose));
241 }
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)
246 {
247 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
248 nextp = hp->bh_next;
249 mf_free_bhdr(hp);
250 }
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)
255 {
256 tpnext = tp->nt_next;
257 vim_free(tp);
258 }
259 vim_free(mfp->mf_fname);
260 vim_free(mfp->mf_ffname);
261 vim_free(mfp);
262}
263
264/*
265 * Close the swap file for a memfile. Used when 'swapfile' is reset.
266 */
267 void
268mf_close_file(buf, getlines)
269 buf_T *buf;
270 int getlines; /* get all lines into memory? */
271{
272 memfile_T *mfp;
273 linenr_T lnum;
274
275 mfp = buf->b_ml.ml_mfp;
276 if (mfp == NULL || mfp->mf_fd < 0) /* nothing to close */
277 return;
278
279 if (getlines)
280 {
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 */
287 }
288
289 if (close(mfp->mf_fd) < 0) /* close the file */
290 EMSG(_(e_swapclose));
291 mfp->mf_fd = -1;
292
293 if (mfp->mf_fname != NULL)
294 {
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;
300 }
301}
302
303/*
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.
306 */
307 void
308mf_new_page_size(mfp, new_size)
309 memfile_T *mfp;
310 unsigned new_size;
311{
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;
316}
317
318/*
319 * get a new block
320 *
321 * negative: TRUE if negative block number desired (data block)
322 */
323 bhdr_T *
324mf_new(mfp, negative, page_count)
325 memfile_T *mfp;
326 int negative;
327 int page_count;
328{
329 bhdr_T *hp; /* new bhdr_T */
330 bhdr_T *freep; /* first block in free list */
331 char_u *p;
332
333 /*
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.
336 */
337 hp = mf_release(mfp, page_count);
338
339/*
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.
344 */
345 freep = mfp->mf_free_first;
346 if (!negative && freep != NULL && freep->bh_page_count >= page_count)
347 {
348 /*
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
351 *
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
354 *
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
357 */
358 if (freep->bh_page_count > page_count)
359 {
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;
365 }
366 else if (hp == NULL) /* need to allocate memory for this block */
367 {
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;
372 }
373 else /* use the number, remove entry from free list */
374 {
375 freep = mf_rem_free(mfp);
376 hp->bh_bnum = freep->bh_bnum;
377 vim_free(freep);
378 }
379 }
380 else /* get a new number */
381 {
382 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
383 return NULL;
384 if (negative)
385 {
386 hp->bh_bnum = mfp->mf_blocknr_min--;
387 mfp->mf_neg_count++;
388 }
389 else
390 {
391 hp->bh_bnum = mfp->mf_blocknr_max;
392 mfp->mf_blocknr_max += page_count;
393 }
394 }
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);
400
401 /*
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!
404 */
405 (void)vim_memset((char *)(hp->bh_data), 0, (size_t)mfp->mf_page_size);
406
407 return hp;
408}
409
410/*
411 * get existing block 'nr' with 'page_count' pages
412 *
413 * Note: The caller should first check a negative nr with mf_trans_del()
414 */
415 bhdr_T *
416mf_get(mfp, nr, page_count)
417 memfile_T *mfp;
418 blocknr_T nr;
419 int page_count;
420{
421 bhdr_T *hp;
422 /* doesn't exist */
423 if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
424 return NULL;
425
426 /*
427 * see if it is in the cache
428 */
429 hp = mf_find_hash(mfp, nr);
430 if (hp == NULL) /* not in the hash list */
431 {
432 if (nr < 0 || nr >= mfp->mf_infile_count) /* can't be in the file */
433 return NULL;
434
435 /* could check here if the block is in the free list */
436
437 /*
438 * Check if we need to flush an existing block.
439 * If so, use that block.
440 * If not, allocate a new block.
441 */
442 hp = mf_release(mfp, page_count);
443 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
444 return NULL;
445
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! */
450 {
451 mf_free_bhdr(hp);
452 return NULL;
453 }
454 }
455 else
456 {
457 mf_rem_used(mfp, hp); /* remove from list, insert in front below */
458 mf_rem_hash(mfp, hp);
459 }
460
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 */
464
465 return hp;
466}
467
468/*
469 * release the block *hp
470 *
471 * dirty: Block must be written to file later
472 * infile: Block should be in file (needed for recovery)
473 *
474 * no return value, function cannot fail
475 */
476 void
477mf_put(mfp, hp, dirty, infile)
478 memfile_T *mfp;
479 bhdr_T *hp;
480 int dirty;
481 int infile;
482{
483 int flags;
484
485 flags = hp->bh_flags;
486
487 if ((flags & BH_LOCKED) == 0)
488 EMSG(_("E293: block was not locked"));
489 flags &= ~BH_LOCKED;
490 if (dirty)
491 {
492 flags |= BH_DIRTY;
493 mfp->mf_dirty = TRUE;
494 }
495 hp->bh_flags = flags;
496 if (infile)
497 mf_trans_add(mfp, hp); /* may translate negative in positive nr */
498}
499
500/*
501 * block *hp is no longer in used, may put it in the free list of memfile *mfp
502 */
503 void
504mf_free(mfp, hp)
505 memfile_T *mfp;
506 bhdr_T *hp;
507{
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)
512 {
513 vim_free(hp); /* don't want negative numbers in free list */
514 mfp->mf_neg_count--;
515 }
516 else
517 mf_ins_free(mfp, hp); /* put *hp in the free list */
518}
519
520#if defined(__MORPHOS__)
521/* function is missing in MorphOS libnix version */
522extern unsigned long *__stdfiledes;
523
524 static unsigned long
525fdtofh(int filedescriptor)
526{
527 return __stdfiledes[filedescriptor];
528}
529#endif
530
531/*
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.
541 *
542 * Return FAIL for failure, OK otherwise
543 */
544 int
545mf_sync(mfp, flags)
546 memfile_T *mfp;
547 int flags;
548{
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;
555
556 if (mfp->mf_fd < 0) /* there is no file, nothing to do */
557 {
558 mfp->mf_dirty = FALSE;
559 return FAIL;
560 }
561
562 /* Only a CTRL-C while writing will break us here, not one typed
563 * previously. */
564 got_int = FALSE;
565
566 /*
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.
571 */
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)))
578 {
579 if ((flags & MFS_ZERO) && hp->bh_bnum != 0)
580 continue;
581 if (mf_write(mfp, hp) == FAIL)
582 {
583 if (status == FAIL) /* double error: quit syncing */
584 break;
585 status = FAIL;
586 }
587 if (flags & MFS_STOP)
588 {
589 /* Stop when char available now. */
590 if (ui_char_avail())
591 break;
592 }
593 else
594 ui_breakcheck();
595 if (got_int)
596 break;
597 }
598
599 /*
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.
602 */
603 if (hp == NULL || status == FAIL)
604 mfp->mf_dirty = FALSE;
605
606 if ((flags & MFS_FLUSH) && *p_sws != NUL)
607 {
608#if defined(UNIX)
609# ifdef HAVE_FSYNC
610 /*
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).
618 */
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)
623 {
624 if (fsync(mfp->mf_fd))
625 status = FAIL;
626 }
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)
639 {
640 if (fsync(mfp->mf_fd))
641 status = FAIL;
642 }
643#endif
644#ifdef MSDOS
645 if (_dos_commit(mfp->mf_fd))
646 status = FAIL;
647#else
648# ifdef SYNC_DUP_CLOSE
649 /*
650 * Win32 is a bit more work: Duplicate the file handle and close it.
651 * This should flush the file to disk.
652 */
653 if ((fd = dup(mfp->mf_fd)) >= 0)
654 close(fd);
655# endif
656#endif
657#ifdef AMIGA
658# ifdef __AROS__
659 if (fsync(mfp->mf_fd) != 0)
660 status = FAIL;
661# else
662 /*
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....
666 */
667# ifdef FEAT_ARP
668 if (dos2)
669# endif
670# ifdef SASC
671 {
672 struct UFB *fp = chkufb(mfp->mf_fd);
673
674 if (fp != NULL)
675 Flush(fp->ufbfh);
676 }
677# else
678# if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
679 {
680# if defined(__GNUC__) && !defined(__MORPHOS__)
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 BPTR fh = (BPTR)fdtofh(mfp->mf_fd);
686
687 if (fh != 0)
688 Flush(fh);
689 }
690# else /* assume Manx */
691 Flush(_devtab[mfp->mf_fd].fd);
692# endif
693# endif
694# endif
695#endif /* AMIGA */
696 }
697
698 got_int |= got_int_save;
699
700 return status;
701}
702
703/*
704 * insert block *hp in front of hashlist of memfile *mfp
705 */
706 static void
707mf_ins_hash(mfp, hp)
708 memfile_T *mfp;
709 bhdr_T *hp;
710{
711 bhdr_T *hhp;
712 int hash;
713
714 hash = MEMHASH(hp->bh_bnum);
715 hhp = mfp->mf_hash[hash];
716 hp->bh_hash_next = hhp;
717 hp->bh_hash_prev = NULL;
718 if (hhp != NULL)
719 hhp->bh_hash_prev = hp;
720 mfp->mf_hash[hash] = hp;
721}
722
723/*
724 * remove block *hp from hashlist of memfile list *mfp
725 */
726 static void
727mf_rem_hash(mfp, hp)
728 memfile_T *mfp;
729 bhdr_T *hp;
730{
731 if (hp->bh_hash_prev == NULL)
732 mfp->mf_hash[MEMHASH(hp->bh_bnum)] = hp->bh_hash_next;
733 else
734 hp->bh_hash_prev->bh_hash_next = hp->bh_hash_next;
735
736 if (hp->bh_hash_next)
737 hp->bh_hash_next->bh_hash_prev = hp->bh_hash_prev;
738}
739
740/*
741 * look in hash lists of memfile *mfp for block header with number 'nr'
742 */
743 static bhdr_T *
744mf_find_hash(mfp, nr)
745 memfile_T *mfp;
746 blocknr_T nr;
747{
748 bhdr_T *hp;
749
750 for (hp = mfp->mf_hash[MEMHASH(nr)]; hp != NULL; hp = hp->bh_hash_next)
751 if (hp->bh_bnum == nr)
752 break;
753 return hp;
754}
755
756/*
757 * insert block *hp in front of used list of memfile *mfp
758 */
759 static void
760mf_ins_used(mfp, hp)
761 memfile_T *mfp;
762 bhdr_T *hp;
763{
764 hp->bh_next = mfp->mf_used_first;
765 mfp->mf_used_first = hp;
766 hp->bh_prev = NULL;
767 if (hp->bh_next == NULL) /* list was empty, adjust last pointer */
768 mfp->mf_used_last = hp;
769 else
770 hp->bh_next->bh_prev = hp;
771 mfp->mf_used_count += hp->bh_page_count;
772 total_mem_used += hp->bh_page_count * mfp->mf_page_size;
773}
774
775/*
776 * remove block *hp from used list of memfile *mfp
777 */
778 static void
779mf_rem_used(mfp, hp)
780 memfile_T *mfp;
781 bhdr_T *hp;
782{
783 if (hp->bh_next == NULL) /* last block in used list */
784 mfp->mf_used_last = hp->bh_prev;
785 else
786 hp->bh_next->bh_prev = hp->bh_prev;
787 if (hp->bh_prev == NULL) /* first block in used list */
788 mfp->mf_used_first = hp->bh_next;
789 else
790 hp->bh_prev->bh_next = hp->bh_next;
791 mfp->mf_used_count -= hp->bh_page_count;
792 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
793}
794
795/*
796 * Release the least recently used block from the used list if the number
797 * of used memory blocks gets to big.
798 *
799 * Return the block header to the caller, including the memory block, so
800 * it can be re-used. Make sure the page_count is right.
801 */
802 static bhdr_T *
803mf_release(mfp, page_count)
804 memfile_T *mfp;
805 int page_count;
806{
807 bhdr_T *hp;
808 int need_release;
809 buf_T *buf;
810
811 /* don't release while in mf_close_file() */
812 if (dont_release)
813 return NULL;
814
815 /*
816 * Need to release a block if the number of blocks for this memfile is
817 * higher than the maximum or total memory used is over 'maxmemtot'
818 */
819 need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max)
820 || (total_mem_used >> 10) >= (long_u)p_mmt);
821
822 /*
823 * Try to create a swap file if the amount of memory used is getting too
824 * high.
825 */
826 if (mfp->mf_fd < 0 && need_release && p_uc)
827 {
828 /* find for which buffer this memfile is */
829 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
830 if (buf->b_ml.ml_mfp == mfp)
831 break;
832 if (buf != NULL && buf->b_may_swap)
833 ml_open_file(buf);
834 }
835
836 /*
837 * don't release a block if
838 * there is no file for this memfile
839 * or
840 * the number of blocks for this memfile is lower than the maximum
841 * and
842 * total memory used is not up to 'maxmemtot'
843 */
844 if (mfp->mf_fd < 0 || !need_release)
845 return NULL;
846
847 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
848 if (!(hp->bh_flags & BH_LOCKED))
849 break;
850 if (hp == NULL) /* not a single one that can be released */
851 return NULL;
852
853 /*
854 * If the block is dirty, write it.
855 * If the write fails we don't free it.
856 */
857 if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL)
858 return NULL;
859
860 mf_rem_used(mfp, hp);
861 mf_rem_hash(mfp, hp);
862
863 /*
864 * If a bhdr_T is returned, make sure that the page_count of bh_data is
865 * right
866 */
867 if (hp->bh_page_count != page_count)
868 {
869 vim_free(hp->bh_data);
870 if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL)
871 {
872 vim_free(hp);
873 return NULL;
874 }
875 hp->bh_page_count = page_count;
876 }
877 return hp;
878}
879
880/*
881 * release as many blocks as possible
882 * Used in case of out of memory
883 *
884 * return TRUE if any memory was released
885 */
886 int
887mf_release_all()
888{
889 buf_T *buf;
890 memfile_T *mfp;
891 bhdr_T *hp;
892 int retval = FALSE;
893
894 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
895 {
896 mfp = buf->b_ml.ml_mfp;
897 if (mfp != NULL)
898 {
899 /* If no swap file yet, may open one */
900 if (mfp->mf_fd < 0 && buf->b_may_swap)
901 ml_open_file(buf);
902
903 /* only if there is a swapfile */
904 if (mfp->mf_fd >= 0)
905 {
906 for (hp = mfp->mf_used_last; hp != NULL; )
907 {
908 if (!(hp->bh_flags & BH_LOCKED)
909 && (!(hp->bh_flags & BH_DIRTY)
910 || mf_write(mfp, hp) != FAIL))
911 {
912 mf_rem_used(mfp, hp);
913 mf_rem_hash(mfp, hp);
914 mf_free_bhdr(hp);
915 hp = mfp->mf_used_last; /* re-start, list was changed */
916 retval = TRUE;
917 }
918 else
919 hp = hp->bh_prev;
920 }
921 }
922 }
923 }
924 return retval;
925}
926
927/*
928 * Allocate a block header and a block of memory for it
929 */
930 static bhdr_T *
931mf_alloc_bhdr(mfp, page_count)
932 memfile_T *mfp;
933 int page_count;
934{
935 bhdr_T *hp;
936
937 if ((hp = (bhdr_T *)alloc((unsigned)sizeof(bhdr_T))) != NULL)
938 {
939 if ((hp->bh_data = (char_u *)alloc(mfp->mf_page_size * page_count))
940 == NULL)
941 {
942 vim_free(hp); /* not enough memory */
943 return NULL;
944 }
945 hp->bh_page_count = page_count;
946 }
947 return hp;
948}
949
950/*
951 * Free a block header and the block of memory for it
952 */
953 static void
954mf_free_bhdr(hp)
955 bhdr_T *hp;
956{
957 vim_free(hp->bh_data);
958 vim_free(hp);
959}
960
961/*
962 * insert entry *hp in the free list
963 */
964 static void
965mf_ins_free(mfp, hp)
966 memfile_T *mfp;
967 bhdr_T *hp;
968{
969 hp->bh_next = mfp->mf_free_first;
970 mfp->mf_free_first = hp;
971}
972
973/*
974 * remove the first entry from the free list and return a pointer to it
975 * Note: caller must check that mfp->mf_free_first is not NULL!
976 */
977 static bhdr_T *
978mf_rem_free(mfp)
979 memfile_T *mfp;
980{
981 bhdr_T *hp;
982
983 hp = mfp->mf_free_first;
984 mfp->mf_free_first = hp->bh_next;
985 return hp;
986}
987
988/*
989 * read a block from disk
990 *
991 * Return FAIL for failure, OK otherwise
992 */
993 static int
994mf_read(mfp, hp)
995 memfile_T *mfp;
996 bhdr_T *hp;
997{
998 off_t offset;
999 unsigned page_size;
1000 unsigned size;
1001
1002 if (mfp->mf_fd < 0) /* there is no file, can't read */
1003 return FAIL;
1004
1005 page_size = mfp->mf_page_size;
1006 offset = (off_t)page_size * hp->bh_bnum;
1007 size = page_size * hp->bh_page_count;
1008 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1009 {
1010 EMSG(_("E294: Seek error in swap file read"));
1011 return FAIL;
1012 }
1013 if ((unsigned)vim_read(mfp->mf_fd, hp->bh_data, size) != size)
1014 {
1015 EMSG(_("E295: Read error in swap file"));
1016 return FAIL;
1017 }
1018 return OK;
1019}
1020
1021/*
1022 * write a block to disk
1023 *
1024 * Return FAIL for failure, OK otherwise
1025 */
1026 static int
1027mf_write(mfp, hp)
1028 memfile_T *mfp;
1029 bhdr_T *hp;
1030{
1031 off_t offset; /* offset in the file */
1032 blocknr_T nr; /* block nr which is being written */
1033 bhdr_T *hp2;
1034 unsigned page_size; /* number of bytes in a page */
1035 unsigned page_count; /* number of pages written */
1036 unsigned size; /* number of bytes written */
1037
1038 if (mfp->mf_fd < 0) /* there is no file, can't write */
1039 return FAIL;
1040
1041 if (hp->bh_bnum < 0) /* must assign file block number */
1042 if (mf_trans_add(mfp, hp) == FAIL)
1043 return FAIL;
1044
1045 page_size = mfp->mf_page_size;
1046
1047 /*
1048 * We don't want gaps in the file. Write the blocks in front of *hp
1049 * to extend the file.
1050 * If block 'mf_infile_count' is not in the hash list, it has been
1051 * freed. Fill the space in the file with data from the current block.
1052 */
1053 for (;;)
1054 {
1055 nr = hp->bh_bnum;
1056 if (nr > mfp->mf_infile_count) /* beyond end of file */
1057 {
1058 nr = mfp->mf_infile_count;
1059 hp2 = mf_find_hash(mfp, nr); /* NULL catched below */
1060 }
1061 else
1062 hp2 = hp;
1063
1064 offset = (off_t)page_size * nr;
1065 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1066 {
1067 EMSG(_("E296: Seek error in swap file write"));
1068 return FAIL;
1069 }
1070 if (hp2 == NULL) /* freed block, fill with dummy data */
1071 page_count = 1;
1072 else
1073 page_count = hp2->bh_page_count;
1074 size = page_size * page_count;
1075 if ((unsigned)vim_write(mfp->mf_fd,
1076 (hp2 == NULL ? hp : hp2)->bh_data, size) != size)
1077 {
1078 /*
1079 * Avoid repeating the error message, this mostly happens when the
1080 * disk is full. We give the message again only after a succesful
1081 * write or when hitting a key. We keep on trying, in case some
1082 * space becomes available.
1083 */
1084 if (!did_swapwrite_msg)
1085 EMSG(_("E297: Write error in swap file"));
1086 did_swapwrite_msg = TRUE;
1087 return FAIL;
1088 }
1089 did_swapwrite_msg = FALSE;
1090 if (hp2 != NULL) /* written a non-dummy block */
1091 hp2->bh_flags &= ~BH_DIRTY;
1092 /* appended to the file */
1093 if (nr + (blocknr_T)page_count > mfp->mf_infile_count)
1094 mfp->mf_infile_count = nr + page_count;
1095 if (nr == hp->bh_bnum) /* written the desired block */
1096 break;
1097 }
1098 return OK;
1099}
1100
1101/*
1102 * Make block number for *hp positive and add it to the translation list
1103 *
1104 * Return FAIL for failure, OK otherwise
1105 */
1106 static int
1107mf_trans_add(mfp, hp)
1108 memfile_T *mfp;
1109 bhdr_T *hp;
1110{
1111 bhdr_T *freep;
1112 blocknr_T new_bnum;
1113 int hash;
1114 NR_TRANS *np;
1115 int page_count;
1116
1117 if (hp->bh_bnum >= 0) /* it's already positive */
1118 return OK;
1119
1120 if ((np = (NR_TRANS *)alloc((unsigned)sizeof(NR_TRANS))) == NULL)
1121 return FAIL;
1122
1123/*
1124 * get a new number for the block.
1125 * If the first item in the free list has sufficient pages, use its number
1126 * Otherwise use mf_blocknr_max.
1127 */
1128 freep = mfp->mf_free_first;
1129 page_count = hp->bh_page_count;
1130 if (freep != NULL && freep->bh_page_count >= page_count)
1131 {
1132 new_bnum = freep->bh_bnum;
1133 /*
1134 * If the page count of the free block was larger, recude it.
1135 * If the page count matches, remove the block from the free list
1136 */
1137 if (freep->bh_page_count > page_count)
1138 {
1139 freep->bh_bnum += page_count;
1140 freep->bh_page_count -= page_count;
1141 }
1142 else
1143 {
1144 freep = mf_rem_free(mfp);
1145 vim_free(freep);
1146 }
1147 }
1148 else
1149 {
1150 new_bnum = mfp->mf_blocknr_max;
1151 mfp->mf_blocknr_max += page_count;
1152 }
1153
1154 np->nt_old_bnum = hp->bh_bnum; /* adjust number */
1155 np->nt_new_bnum = new_bnum;
1156
1157 mf_rem_hash(mfp, hp); /* remove from old hash list */
1158 hp->bh_bnum = new_bnum;
1159 mf_ins_hash(mfp, hp); /* insert in new hash list */
1160
1161 hash = MEMHASH(np->nt_old_bnum); /* insert in trans list */
1162 np->nt_next = mfp->mf_trans[hash];
1163 mfp->mf_trans[hash] = np;
1164 if (np->nt_next != NULL)
1165 np->nt_next->nt_prev = np;
1166 np->nt_prev = NULL;
1167
1168 return OK;
1169}
1170
1171/*
1172 * Lookup a tranlation from the trans lists and delete the entry
1173 *
1174 * Return the positive new number when found, the old number when not found
1175 */
1176 blocknr_T
1177mf_trans_del(mfp, old_nr)
1178 memfile_T *mfp;
1179 blocknr_T old_nr;
1180{
1181 int hash;
1182 NR_TRANS *np;
1183 blocknr_T new_bnum;
1184
1185 hash = MEMHASH(old_nr);
1186 for (np = mfp->mf_trans[hash]; np != NULL; np = np->nt_next)
1187 if (np->nt_old_bnum == old_nr)
1188 break;
1189 if (np == NULL) /* not found */
1190 return old_nr;
1191
1192 mfp->mf_neg_count--;
1193 new_bnum = np->nt_new_bnum;
1194 if (np->nt_prev != NULL) /* remove entry from the trans list */
1195 np->nt_prev->nt_next = np->nt_next;
1196 else
1197 mfp->mf_trans[hash] = np->nt_next;
1198 if (np->nt_next != NULL)
1199 np->nt_next->nt_prev = np->nt_prev;
1200 vim_free(np);
1201
1202 return new_bnum;
1203}
1204
1205/*
1206 * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
1207 * Only called when creating or renaming the swapfile. Either way it's a new
1208 * name so we must work out the full path name.
1209 */
1210 void
1211mf_set_ffname(mfp)
1212 memfile_T *mfp;
1213{
1214 mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE);
1215}
1216
1217/*
1218 * Make the name of the file used for the memfile a full path.
1219 * Used before doing a :cd
1220 */
1221 void
1222mf_fullname(mfp)
1223 memfile_T *mfp;
1224{
1225 if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL)
1226 {
1227 vim_free(mfp->mf_fname);
1228 mfp->mf_fname = mfp->mf_ffname;
1229 mfp->mf_ffname = NULL;
1230 }
1231}
1232
1233/*
1234 * return TRUE if there are any translations pending for 'mfp'
1235 */
1236 int
1237mf_need_trans(mfp)
1238 memfile_T *mfp;
1239{
1240 return (mfp->mf_fname != NULL && mfp->mf_neg_count > 0);
1241}
1242
1243/*
1244 * Open a swap file for a memfile.
1245 * The "fname" must be in allocated memory, and is consumed (also when an
1246 * error occurs).
1247 */
1248 static void
1249mf_do_open(mfp, fname, flags)
1250 memfile_T *mfp;
1251 char_u *fname;
1252 int flags; /* flags for open() */
1253{
1254#ifdef HAVE_LSTAT
1255 struct stat sb;
1256#endif
1257
1258 mfp->mf_fname = fname;
1259
1260 /*
1261 * Get the full path name before the open, because this is
1262 * not possible after the open on the Amiga.
1263 * fname cannot be NameBuff, because it must have been allocated.
1264 */
1265 mf_set_ffname(mfp);
1266#if defined(MSDOS) || defined(MSWIN) || defined(RISCOS)
1267 /*
1268 * A ":!cd e:xxx" may change the directory without us knowning, use the
1269 * full pathname always. Careful: This frees fname!
1270 */
1271 mf_fullname(mfp);
1272#endif
1273
1274#ifdef HAVE_LSTAT
1275 /*
1276 * Extra security check: When creating a swap file it really shouldn't
1277 * exist yet. If there is a symbolic link, this is most likely an attack.
1278 */
1279 if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0)
1280 {
1281 mfp->mf_fd = -1;
1282 EMSG(_("E300: Swap file already exists (symlink attack?)"));
1283 }
1284 else
1285#endif
1286 {
1287 /*
1288 * try to open the file
1289 */
1290 flags |= O_EXTRA;
1291#ifdef WIN32
1292 /* Prevent handle inheritance that cause problems with Cscope
1293 * (swap file may not be deleted if cscope connection was open after
1294 * the file) */
1295 flags |= O_NOINHERIT;
1296#endif
1297 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags);
1298 }
1299
1300 /*
1301 * If the file cannot be opened, use memory only
1302 */
1303 if (mfp->mf_fd < 0)
1304 {
1305 vim_free(mfp->mf_fname);
1306 vim_free(mfp->mf_ffname);
1307 mfp->mf_fname = NULL;
1308 mfp->mf_ffname = NULL;
1309 }
1310 else
1311 mch_hide(mfp->mf_fname); /* try setting the 'hidden' flag */
1312}