blob: 56719eec7c464e26a6bd9ea723836965e0bc4b7a [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
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#include "vim.h"
36
Bram Moolenaar071d4272004-06-13 20:20:40 +000037/*
38 * Some systems have the page size in statfs.f_bsize, some in stat.st_blksize
39 */
40#ifdef HAVE_ST_BLKSIZE
41# define STATFS stat
42# define F_BSIZE st_blksize
43# define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
44#else
45# ifdef HAVE_SYS_STATFS_H
46# include <sys/statfs.h>
47# define STATFS statfs
48# define F_BSIZE f_bsize
49# ifdef __MINT__ /* do we still need this? */
50# define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
51# endif
52# endif
53#endif
54
55/*
56 * for Amiga Dos 2.0x we use Flush
57 */
58#ifdef AMIGA
59# ifdef FEAT_ARP
60extern int dos2; /* this is in os_amiga.c */
61# endif
62# ifdef SASC
63# include <proto/dos.h>
64# include <ios1.h> /* for chkufb() */
65# endif
66#endif
67
68#define MEMFILE_PAGE_SIZE 4096 /* default page size */
69
70static long_u total_mem_used = 0; /* total memory used for memfiles */
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010072static void mf_ins_hash(memfile_T *, bhdr_T *);
73static void mf_rem_hash(memfile_T *, bhdr_T *);
74static bhdr_T *mf_find_hash(memfile_T *, blocknr_T);
75static void mf_ins_used(memfile_T *, bhdr_T *);
76static void mf_rem_used(memfile_T *, bhdr_T *);
77static bhdr_T *mf_release(memfile_T *, int);
78static bhdr_T *mf_alloc_bhdr(memfile_T *, int);
79static void mf_free_bhdr(bhdr_T *);
80static void mf_ins_free(memfile_T *, bhdr_T *);
81static bhdr_T *mf_rem_free(memfile_T *);
82static int mf_read(memfile_T *, bhdr_T *);
83static int mf_write(memfile_T *, bhdr_T *);
84static int mf_write_block(memfile_T *mfp, bhdr_T *hp, off_t offset, unsigned size);
85static int mf_trans_add(memfile_T *, bhdr_T *);
86static void mf_do_open(memfile_T *, char_u *, int);
87static void mf_hash_init(mf_hashtab_T *);
88static void mf_hash_free(mf_hashtab_T *);
89static void mf_hash_free_all(mf_hashtab_T *);
90static mf_hashitem_T *mf_hash_find(mf_hashtab_T *, blocknr_T);
91static void mf_hash_add_item(mf_hashtab_T *, mf_hashitem_T *);
92static void mf_hash_rem_item(mf_hashtab_T *, mf_hashitem_T *);
93static int mf_hash_grow(mf_hashtab_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
95/*
96 * The functions for using a memfile:
97 *
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)
109 */
110
111/*
112 * Open an existing or new memory block file.
113 *
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
118 *
119 * If fname != NULL and file cannot be opened, fail.
120 *
121 * return value: identifier for this memory block file.
122 */
123 memfile_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100124mf_open(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125{
126 memfile_T *mfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127 off_t size;
Bram Moolenaara03dbed2013-05-23 22:27:03 +0200128#if defined(STATFS) && defined(UNIX) && !defined(__QNX__) && !defined(__minix)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129# define USE_FSTATFS
130 struct STATFS stf;
131#endif
132
133 if ((mfp = (memfile_T *)alloc((unsigned)sizeof(memfile_T))) == NULL)
134 return NULL;
135
136 if (fname == NULL) /* no file for this memfile, use memory only */
137 {
138 mfp->mf_fname = NULL;
139 mfp->mf_ffname = NULL;
140 mfp->mf_fd = -1;
141 }
142 else
143 {
144 mf_do_open(mfp, fname, flags); /* try to open the file */
145
146 /* if the file cannot be opened, return here */
147 if (mfp->mf_fd < 0)
148 {
149 vim_free(mfp);
150 return NULL;
151 }
152 }
153
154 mfp->mf_free_first = NULL; /* free list is empty */
155 mfp->mf_used_first = NULL; /* used list is empty */
156 mfp->mf_used_last = NULL;
157 mfp->mf_dirty = FALSE;
158 mfp->mf_used_count = 0;
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100159 mf_hash_init(&mfp->mf_hash);
160 mf_hash_init(&mfp->mf_trans);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 mfp->mf_page_size = MEMFILE_PAGE_SIZE;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200162#ifdef FEAT_CRYPT
163 mfp->mf_old_key = NULL;
164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
166#ifdef USE_FSTATFS
167 /*
168 * Try to set the page size equal to the block size of the device.
169 * Speeds up I/O a lot.
170 * When recovering, the actual block size will be retrieved from block 0
171 * in ml_recover(). The size used here may be wrong, therefore
172 * mf_blocknr_max must be rounded up.
173 */
174 if (mfp->mf_fd >= 0
175 && fstatfs(mfp->mf_fd, &stf, sizeof(struct statfs), 0) == 0
176 && stf.F_BSIZE >= MIN_SWAP_PAGE_SIZE
177 && stf.F_BSIZE <= MAX_SWAP_PAGE_SIZE)
178 mfp->mf_page_size = stf.F_BSIZE;
179#endif
180
181 if (mfp->mf_fd < 0 || (flags & (O_TRUNC|O_EXCL))
182 || (size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0)
183 mfp->mf_blocknr_max = 0; /* no file or empty file */
184 else
185 mfp->mf_blocknr_max = (blocknr_T)((size + mfp->mf_page_size - 1)
186 / mfp->mf_page_size);
187 mfp->mf_blocknr_min = -1;
188 mfp->mf_neg_count = 0;
189 mfp->mf_infile_count = mfp->mf_blocknr_max;
Bram Moolenaarfe265ff2007-05-11 18:15:45 +0000190
191 /*
192 * Compute maximum number of pages ('maxmem' is in Kbyte):
193 * 'mammem' * 1Kbyte / page-size-in-bytes.
194 * Avoid overflow by first reducing page size as much as possible.
195 */
196 {
197 int shift = 10;
198 unsigned page_size = mfp->mf_page_size;
199
200 while (shift > 0 && (page_size & 1) == 0)
201 {
202 page_size = page_size >> 1;
203 --shift;
204 }
205 mfp->mf_used_count_max = (p_mm << shift) / page_size;
206 if (mfp->mf_used_count_max < 10)
207 mfp->mf_used_count_max = 10;
208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209
210 return mfp;
211}
212
213/*
214 * Open a file for an existing memfile. Used when updatecount set from 0 to
215 * some value.
216 * If the file already exists, this fails.
217 * "fname" is the name of file to use (NULL means no file at all)
218 * Note: "fname" must have been allocated, it is not copied! If opening the
219 * file fails, "fname" is freed.
220 *
221 * return value: FAIL if file could not be opened, OK otherwise
222 */
223 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100224mf_open_file(memfile_T *mfp, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225{
226 mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); /* try to open the file */
227
228 if (mfp->mf_fd < 0)
229 return FAIL;
230
231 mfp->mf_dirty = TRUE;
232 return OK;
233}
234
235/*
Bram Moolenaar945e2db2010-06-05 17:43:32 +0200236 * Close a memory file and delete the associated file if 'del_file' is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 */
238 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100239mf_close(memfile_T *mfp, int del_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240{
241 bhdr_T *hp, *nextp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242
243 if (mfp == NULL) /* safety check */
244 return;
245 if (mfp->mf_fd >= 0)
246 {
247 if (close(mfp->mf_fd) < 0)
248 EMSG(_(e_swapclose));
249 }
250 if (del_file && mfp->mf_fname != NULL)
251 mch_remove(mfp->mf_fname);
252 /* free entries in used list */
253 for (hp = mfp->mf_used_first; hp != NULL; hp = nextp)
254 {
255 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
256 nextp = hp->bh_next;
257 mf_free_bhdr(hp);
258 }
259 while (mfp->mf_free_first != NULL) /* free entries in free list */
260 vim_free(mf_rem_free(mfp));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100261 mf_hash_free(&mfp->mf_hash);
262 mf_hash_free_all(&mfp->mf_trans); /* free hashtable and its items */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 vim_free(mfp->mf_fname);
264 vim_free(mfp->mf_ffname);
265 vim_free(mfp);
266}
267
268/*
269 * Close the swap file for a memfile. Used when 'swapfile' is reset.
270 */
271 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100272mf_close_file(
273 buf_T *buf,
274 int getlines) /* get all lines into memory? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275{
276 memfile_T *mfp;
277 linenr_T lnum;
278
279 mfp = buf->b_ml.ml_mfp;
280 if (mfp == NULL || mfp->mf_fd < 0) /* nothing to close */
281 return;
282
283 if (getlines)
284 {
285 /* get all blocks in memory by accessing all lines (clumsy!) */
Bram Moolenaar47b8b152007-02-07 02:41:57 +0000286 mf_dont_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
288 (void)ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar47b8b152007-02-07 02:41:57 +0000289 mf_dont_release = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 /* TODO: should check if all blocks are really in core */
291 }
292
293 if (close(mfp->mf_fd) < 0) /* close the file */
294 EMSG(_(e_swapclose));
295 mfp->mf_fd = -1;
296
297 if (mfp->mf_fname != NULL)
298 {
299 mch_remove(mfp->mf_fname); /* delete the swap file */
300 vim_free(mfp->mf_fname);
301 vim_free(mfp->mf_ffname);
302 mfp->mf_fname = NULL;
303 mfp->mf_ffname = NULL;
304 }
305}
306
307/*
308 * Set new size for a memfile. Used when block 0 of a swapfile has been read
309 * and the size it indicates differs from what was guessed.
310 */
311 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100312mf_new_page_size(memfile_T *mfp, unsigned new_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313{
314 /* Correct the memory used for block 0 to the new size, because it will be
315 * freed with that size later on. */
316 total_mem_used += new_size - mfp->mf_page_size;
317 mfp->mf_page_size = new_size;
318}
319
320/*
321 * get a new block
322 *
323 * negative: TRUE if negative block number desired (data block)
324 */
325 bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100326mf_new(memfile_T *mfp, int negative, int page_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327{
Bram Moolenaar17e79192007-05-06 14:15:36 +0000328 bhdr_T *hp; /* new bhdr_T */
329 bhdr_T *freep; /* first block in free list */
330 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331
332 /*
333 * If we reached the maximum size for the used memory blocks, release one
334 * If a bhdr_T is returned, use it and adjust the page_count if necessary.
335 */
336 hp = mf_release(mfp, page_count);
337
338/*
339 * Decide on the number to use:
340 * If there is a free block, use its number.
341 * Otherwise use mf_block_min for a negative number, mf_block_max for
342 * a positive number.
343 */
344 freep = mfp->mf_free_first;
345 if (!negative && freep != NULL && freep->bh_page_count >= page_count)
346 {
347 /*
348 * If the block in the free list has more pages, take only the number
349 * of pages needed and allocate a new bhdr_T with data
350 *
Bram Moolenaar17e79192007-05-06 14:15:36 +0000351 * If the number of pages matches and mf_release() did not return a
352 * bhdr_T, use the bhdr_T from the free list and allocate the data
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 *
Bram Moolenaar17e79192007-05-06 14:15:36 +0000354 * If the number of pages matches and mf_release() returned a bhdr_T,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 * just use the number and free the bhdr_T from the free list
356 */
357 if (freep->bh_page_count > page_count)
358 {
359 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
360 return NULL;
361 hp->bh_bnum = freep->bh_bnum;
362 freep->bh_bnum += page_count;
363 freep->bh_page_count -= page_count;
364 }
365 else if (hp == NULL) /* need to allocate memory for this block */
366 {
367 if ((p = (char_u *)alloc(mfp->mf_page_size * page_count)) == NULL)
368 return NULL;
369 hp = mf_rem_free(mfp);
370 hp->bh_data = p;
371 }
372 else /* use the number, remove entry from free list */
373 {
374 freep = mf_rem_free(mfp);
375 hp->bh_bnum = freep->bh_bnum;
376 vim_free(freep);
377 }
378 }
379 else /* get a new number */
380 {
381 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
382 return NULL;
383 if (negative)
384 {
385 hp->bh_bnum = mfp->mf_blocknr_min--;
386 mfp->mf_neg_count++;
387 }
388 else
389 {
390 hp->bh_bnum = mfp->mf_blocknr_max;
391 mfp->mf_blocknr_max += page_count;
392 }
393 }
394 hp->bh_flags = BH_LOCKED | BH_DIRTY; /* new block is always dirty */
395 mfp->mf_dirty = TRUE;
396 hp->bh_page_count = page_count;
397 mf_ins_used(mfp, hp);
398 mf_ins_hash(mfp, hp);
399
400 /*
401 * Init the data to all zero, to avoid reading uninitialized data.
402 * This also avoids that the passwd file ends up in the swap file!
403 */
Bram Moolenaar945e2db2010-06-05 17:43:32 +0200404 (void)vim_memset((char *)(hp->bh_data), 0,
405 (size_t)mfp->mf_page_size * page_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406
407 return hp;
408}
409
410/*
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200411 * Get existing block "nr" with "page_count" pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 *
413 * Note: The caller should first check a negative nr with mf_trans_del()
414 */
415 bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100416mf_get(memfile_T *mfp, blocknr_T nr, int page_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417{
418 bhdr_T *hp;
419 /* doesn't exist */
420 if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
421 return NULL;
422
423 /*
424 * see if it is in the cache
425 */
426 hp = mf_find_hash(mfp, nr);
427 if (hp == NULL) /* not in the hash list */
428 {
429 if (nr < 0 || nr >= mfp->mf_infile_count) /* can't be in the file */
430 return NULL;
431
432 /* could check here if the block is in the free list */
433
434 /*
435 * Check if we need to flush an existing block.
436 * If so, use that block.
437 * If not, allocate a new block.
438 */
439 hp = mf_release(mfp, page_count);
440 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
441 return NULL;
442
443 hp->bh_bnum = nr;
444 hp->bh_flags = 0;
445 hp->bh_page_count = page_count;
446 if (mf_read(mfp, hp) == FAIL) /* cannot read the block! */
447 {
448 mf_free_bhdr(hp);
449 return NULL;
450 }
451 }
452 else
453 {
454 mf_rem_used(mfp, hp); /* remove from list, insert in front below */
455 mf_rem_hash(mfp, hp);
456 }
457
458 hp->bh_flags |= BH_LOCKED;
459 mf_ins_used(mfp, hp); /* put in front of used list */
460 mf_ins_hash(mfp, hp); /* put in front of hash list */
461
462 return hp;
463}
464
465/*
466 * release the block *hp
467 *
468 * dirty: Block must be written to file later
469 * infile: Block should be in file (needed for recovery)
470 *
471 * no return value, function cannot fail
472 */
473 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100474mf_put(
475 memfile_T *mfp,
476 bhdr_T *hp,
477 int dirty,
478 int infile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479{
480 int flags;
481
482 flags = hp->bh_flags;
483
484 if ((flags & BH_LOCKED) == 0)
485 EMSG(_("E293: block was not locked"));
486 flags &= ~BH_LOCKED;
487 if (dirty)
488 {
489 flags |= BH_DIRTY;
490 mfp->mf_dirty = TRUE;
491 }
492 hp->bh_flags = flags;
493 if (infile)
494 mf_trans_add(mfp, hp); /* may translate negative in positive nr */
495}
496
497/*
498 * block *hp is no longer in used, may put it in the free list of memfile *mfp
499 */
500 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100501mf_free(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502{
503 vim_free(hp->bh_data); /* free the memory */
504 mf_rem_hash(mfp, hp); /* get *hp out of the hash list */
505 mf_rem_used(mfp, hp); /* get *hp out of the used list */
506 if (hp->bh_bnum < 0)
507 {
508 vim_free(hp); /* don't want negative numbers in free list */
509 mfp->mf_neg_count--;
510 }
511 else
512 mf_ins_free(mfp, hp); /* put *hp in the free list */
513}
514
Bram Moolenaar89f37272006-09-26 11:48:34 +0000515#if defined(__MORPHOS__) && defined(__libnix__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516/* function is missing in MorphOS libnix version */
517extern unsigned long *__stdfiledes;
518
519 static unsigned long
520fdtofh(int filedescriptor)
521{
522 return __stdfiledes[filedescriptor];
523}
524#endif
525
526/*
527 * Sync the memory file *mfp to disk.
528 * Flags:
529 * MFS_ALL If not given, blocks with negative numbers are not synced,
530 * even when they are dirty!
531 * MFS_STOP Stop syncing when a character becomes available, but sync at
532 * least one block.
533 * MFS_FLUSH Make sure buffers are flushed to disk, so they will survive a
534 * system crash.
535 * MFS_ZERO Only write block 0.
536 *
537 * Return FAIL for failure, OK otherwise
538 */
539 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100540mf_sync(memfile_T *mfp, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
542 int status;
543 bhdr_T *hp;
544#if defined(SYNC_DUP_CLOSE) && !defined(MSDOS)
545 int fd;
546#endif
547 int got_int_save = got_int;
548
549 if (mfp->mf_fd < 0) /* there is no file, nothing to do */
550 {
551 mfp->mf_dirty = FALSE;
552 return FAIL;
553 }
554
555 /* Only a CTRL-C while writing will break us here, not one typed
556 * previously. */
557 got_int = FALSE;
558
559 /*
560 * sync from last to first (may reduce the probability of an inconsistent
561 * file) If a write fails, it is very likely caused by a full filesystem.
562 * Then we only try to write blocks within the existing file. If that also
563 * fails then we give up.
564 */
565 status = OK;
566 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
567 if (((flags & MFS_ALL) || hp->bh_bnum >= 0)
568 && (hp->bh_flags & BH_DIRTY)
569 && (status == OK || (hp->bh_bnum >= 0
570 && hp->bh_bnum < mfp->mf_infile_count)))
571 {
572 if ((flags & MFS_ZERO) && hp->bh_bnum != 0)
573 continue;
574 if (mf_write(mfp, hp) == FAIL)
575 {
576 if (status == FAIL) /* double error: quit syncing */
577 break;
578 status = FAIL;
579 }
580 if (flags & MFS_STOP)
581 {
582 /* Stop when char available now. */
583 if (ui_char_avail())
584 break;
585 }
586 else
587 ui_breakcheck();
588 if (got_int)
589 break;
590 }
591
592 /*
593 * If the whole list is flushed, the memfile is not dirty anymore.
594 * In case of an error this flag is also set, to avoid trying all the time.
595 */
596 if (hp == NULL || status == FAIL)
597 mfp->mf_dirty = FALSE;
598
599 if ((flags & MFS_FLUSH) && *p_sws != NUL)
600 {
601#if defined(UNIX)
602# ifdef HAVE_FSYNC
603 /*
604 * most Unixes have the very useful fsync() function, just what we need.
605 * However, with OS/2 and EMX it is also available, but there are
606 * reports of bad problems with it (a bug in HPFS.IFS).
607 * So we disable use of it here in case someone tries to be smart
608 * and changes os_os2_cfg.h... (even though there is no __EMX__ test
609 * in the #if, as __EMX__ does not have sync(); we hope for a timely
610 * sync from the system itself).
611 */
612# if defined(__EMX__)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200613 error "Don't use fsync with EMX! Read emxdoc.doc or emxfix01.doc for info."
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614# endif
615 if (STRCMP(p_sws, "fsync") == 0)
616 {
617 if (fsync(mfp->mf_fd))
618 status = FAIL;
619 }
620 else
621# endif
622 /* OpenNT is strictly POSIX (Benzinger) */
623 /* Tandem/Himalaya NSK-OSS doesn't have sync() */
Bram Moolenaarba17ed62015-02-27 18:25:16 +0100624 /* No sync() on Stratus VOS */
625# if defined(__OPENNT) || defined(__TANDEM) || defined(__VOS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 fflush(NULL);
627# else
628 sync();
629# endif
630#endif
631#ifdef VMS
632 if (STRCMP(p_sws, "fsync") == 0)
633 {
634 if (fsync(mfp->mf_fd))
635 status = FAIL;
636 }
637#endif
638#ifdef MSDOS
639 if (_dos_commit(mfp->mf_fd))
640 status = FAIL;
641#else
642# ifdef SYNC_DUP_CLOSE
643 /*
644 * Win32 is a bit more work: Duplicate the file handle and close it.
645 * This should flush the file to disk.
646 */
647 if ((fd = dup(mfp->mf_fd)) >= 0)
648 close(fd);
649# endif
650#endif
651#ifdef AMIGA
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000652# if defined(__AROS__) || defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (fsync(mfp->mf_fd) != 0)
654 status = FAIL;
655# else
656 /*
657 * Flush() only exists for AmigaDos 2.0.
658 * For 1.3 it should be done with close() + open(), but then the risk
659 * is that the open() may fail and lose the file....
660 */
661# ifdef FEAT_ARP
662 if (dos2)
663# endif
664# ifdef SASC
665 {
666 struct UFB *fp = chkufb(mfp->mf_fd);
667
668 if (fp != NULL)
669 Flush(fp->ufbfh);
670 }
671# else
672# if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
673 {
Bram Moolenaar89f37272006-09-26 11:48:34 +0000674# if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 /* Have function (in libnix at least),
676 * but ain't got no prototype anywhere. */
677 extern unsigned long fdtofh(int filedescriptor);
678# endif
Bram Moolenaar89f37272006-09-26 11:48:34 +0000679# if !defined(__libnix__)
680 fflush(NULL);
681# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 BPTR fh = (BPTR)fdtofh(mfp->mf_fd);
683
684 if (fh != 0)
685 Flush(fh);
Bram Moolenaar89f37272006-09-26 11:48:34 +0000686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
688# else /* assume Manx */
689 Flush(_devtab[mfp->mf_fd].fd);
690# endif
691# endif
692# endif
693#endif /* AMIGA */
694 }
695
696 got_int |= got_int_save;
697
698 return status;
699}
700
701/*
Bram Moolenaarc32840f2006-01-14 21:23:38 +0000702 * For all blocks in memory file *mfp that have a positive block number set
703 * the dirty flag. These are blocks that need to be written to a newly
704 * created swapfile.
705 */
706 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100707mf_set_dirty(memfile_T *mfp)
Bram Moolenaarc32840f2006-01-14 21:23:38 +0000708{
709 bhdr_T *hp;
710
711 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
712 if (hp->bh_bnum > 0)
713 hp->bh_flags |= BH_DIRTY;
714 mfp->mf_dirty = TRUE;
715}
716
717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 * insert block *hp in front of hashlist of memfile *mfp
719 */
720 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100721mf_ins_hash(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100723 mf_hash_add_item(&mfp->mf_hash, (mf_hashitem_T *)hp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724}
725
726/*
727 * remove block *hp from hashlist of memfile list *mfp
728 */
729 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100730mf_rem_hash(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731{
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100732 mf_hash_rem_item(&mfp->mf_hash, (mf_hashitem_T *)hp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733}
734
735/*
736 * look in hash lists of memfile *mfp for block header with number 'nr'
737 */
738 static bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100739mf_find_hash(memfile_T *mfp, blocknr_T nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740{
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100741 return (bhdr_T *)mf_hash_find(&mfp->mf_hash, nr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742}
743
744/*
745 * insert block *hp in front of used list of memfile *mfp
746 */
747 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100748mf_ins_used(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749{
750 hp->bh_next = mfp->mf_used_first;
751 mfp->mf_used_first = hp;
752 hp->bh_prev = NULL;
753 if (hp->bh_next == NULL) /* list was empty, adjust last pointer */
754 mfp->mf_used_last = hp;
755 else
756 hp->bh_next->bh_prev = hp;
757 mfp->mf_used_count += hp->bh_page_count;
758 total_mem_used += hp->bh_page_count * mfp->mf_page_size;
759}
760
761/*
762 * remove block *hp from used list of memfile *mfp
763 */
764 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100765mf_rem_used(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766{
767 if (hp->bh_next == NULL) /* last block in used list */
768 mfp->mf_used_last = hp->bh_prev;
769 else
770 hp->bh_next->bh_prev = hp->bh_prev;
771 if (hp->bh_prev == NULL) /* first block in used list */
772 mfp->mf_used_first = hp->bh_next;
773 else
774 hp->bh_prev->bh_next = hp->bh_next;
775 mfp->mf_used_count -= hp->bh_page_count;
776 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
777}
778
779/*
780 * Release the least recently used block from the used list if the number
781 * of used memory blocks gets to big.
782 *
783 * Return the block header to the caller, including the memory block, so
784 * it can be re-used. Make sure the page_count is right.
Bram Moolenaarbc563362015-06-09 18:35:25 +0200785 *
786 * Returns NULL if no block is released.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 */
788 static bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100789mf_release(memfile_T *mfp, int page_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790{
791 bhdr_T *hp;
792 int need_release;
793 buf_T *buf;
794
795 /* don't release while in mf_close_file() */
Bram Moolenaar47b8b152007-02-07 02:41:57 +0000796 if (mf_dont_release)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 return NULL;
798
799 /*
800 * Need to release a block if the number of blocks for this memfile is
801 * higher than the maximum or total memory used is over 'maxmemtot'
802 */
803 need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max)
804 || (total_mem_used >> 10) >= (long_u)p_mmt);
805
806 /*
807 * Try to create a swap file if the amount of memory used is getting too
808 * high.
809 */
810 if (mfp->mf_fd < 0 && need_release && p_uc)
811 {
812 /* find for which buffer this memfile is */
813 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
814 if (buf->b_ml.ml_mfp == mfp)
815 break;
816 if (buf != NULL && buf->b_may_swap)
817 ml_open_file(buf);
818 }
819
820 /*
821 * don't release a block if
822 * there is no file for this memfile
823 * or
824 * the number of blocks for this memfile is lower than the maximum
825 * and
826 * total memory used is not up to 'maxmemtot'
827 */
828 if (mfp->mf_fd < 0 || !need_release)
829 return NULL;
830
831 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
832 if (!(hp->bh_flags & BH_LOCKED))
833 break;
834 if (hp == NULL) /* not a single one that can be released */
835 return NULL;
836
837 /*
838 * If the block is dirty, write it.
839 * If the write fails we don't free it.
840 */
841 if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL)
842 return NULL;
843
844 mf_rem_used(mfp, hp);
845 mf_rem_hash(mfp, hp);
846
847 /*
848 * If a bhdr_T is returned, make sure that the page_count of bh_data is
849 * right
850 */
851 if (hp->bh_page_count != page_count)
852 {
853 vim_free(hp->bh_data);
854 if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL)
855 {
856 vim_free(hp);
857 return NULL;
858 }
859 hp->bh_page_count = page_count;
860 }
861 return hp;
862}
863
864/*
865 * release as many blocks as possible
866 * Used in case of out of memory
867 *
868 * return TRUE if any memory was released
869 */
870 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100871mf_release_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872{
873 buf_T *buf;
874 memfile_T *mfp;
875 bhdr_T *hp;
876 int retval = FALSE;
877
878 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
879 {
880 mfp = buf->b_ml.ml_mfp;
881 if (mfp != NULL)
882 {
883 /* If no swap file yet, may open one */
884 if (mfp->mf_fd < 0 && buf->b_may_swap)
885 ml_open_file(buf);
886
887 /* only if there is a swapfile */
888 if (mfp->mf_fd >= 0)
889 {
890 for (hp = mfp->mf_used_last; hp != NULL; )
891 {
892 if (!(hp->bh_flags & BH_LOCKED)
893 && (!(hp->bh_flags & BH_DIRTY)
894 || mf_write(mfp, hp) != FAIL))
895 {
896 mf_rem_used(mfp, hp);
897 mf_rem_hash(mfp, hp);
898 mf_free_bhdr(hp);
899 hp = mfp->mf_used_last; /* re-start, list was changed */
900 retval = TRUE;
901 }
902 else
903 hp = hp->bh_prev;
904 }
905 }
906 }
907 }
908 return retval;
909}
910
911/*
912 * Allocate a block header and a block of memory for it
913 */
914 static bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100915mf_alloc_bhdr(memfile_T *mfp, int page_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916{
917 bhdr_T *hp;
918
919 if ((hp = (bhdr_T *)alloc((unsigned)sizeof(bhdr_T))) != NULL)
920 {
921 if ((hp->bh_data = (char_u *)alloc(mfp->mf_page_size * page_count))
922 == NULL)
923 {
924 vim_free(hp); /* not enough memory */
925 return NULL;
926 }
927 hp->bh_page_count = page_count;
928 }
929 return hp;
930}
931
932/*
933 * Free a block header and the block of memory for it
934 */
935 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100936mf_free_bhdr(bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
938 vim_free(hp->bh_data);
939 vim_free(hp);
940}
941
942/*
943 * insert entry *hp in the free list
944 */
945 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100946mf_ins_free(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
948 hp->bh_next = mfp->mf_free_first;
949 mfp->mf_free_first = hp;
950}
951
952/*
953 * remove the first entry from the free list and return a pointer to it
954 * Note: caller must check that mfp->mf_free_first is not NULL!
955 */
956 static bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100957mf_rem_free(memfile_T *mfp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958{
959 bhdr_T *hp;
960
961 hp = mfp->mf_free_first;
962 mfp->mf_free_first = hp->bh_next;
963 return hp;
964}
965
966/*
967 * read a block from disk
968 *
969 * Return FAIL for failure, OK otherwise
970 */
971 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100972mf_read(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973{
974 off_t offset;
975 unsigned page_size;
976 unsigned size;
977
978 if (mfp->mf_fd < 0) /* there is no file, can't read */
979 return FAIL;
980
981 page_size = mfp->mf_page_size;
982 offset = (off_t)page_size * hp->bh_bnum;
983 size = page_size * hp->bh_page_count;
984 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
985 {
Bram Moolenaar3f2d9812006-11-07 17:02:30 +0000986 PERROR(_("E294: Seek error in swap file read"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 return FAIL;
988 }
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100989 if ((unsigned)read_eintr(mfp->mf_fd, hp->bh_data, size) != size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
Bram Moolenaar3f2d9812006-11-07 17:02:30 +0000991 PERROR(_("E295: Read error in swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 return FAIL;
993 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200994
995#ifdef FEAT_CRYPT
Bram Moolenaar4a8c2cf2015-12-19 15:28:18 +0100996 /* Decrypt if 'key' is set and this is a data block. And when changing the
997 * key. */
998 if (*mfp->mf_buffer->b_p_key != NUL || mfp->mf_old_key != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200999 ml_decrypt_data(mfp, hp->bh_data, offset, size);
1000#endif
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 return OK;
1003}
1004
1005/*
1006 * write a block to disk
1007 *
1008 * Return FAIL for failure, OK otherwise
1009 */
1010 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001011mf_write(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012{
1013 off_t offset; /* offset in the file */
1014 blocknr_T nr; /* block nr which is being written */
1015 bhdr_T *hp2;
1016 unsigned page_size; /* number of bytes in a page */
1017 unsigned page_count; /* number of pages written */
1018 unsigned size; /* number of bytes written */
1019
1020 if (mfp->mf_fd < 0) /* there is no file, can't write */
1021 return FAIL;
1022
1023 if (hp->bh_bnum < 0) /* must assign file block number */
1024 if (mf_trans_add(mfp, hp) == FAIL)
1025 return FAIL;
1026
1027 page_size = mfp->mf_page_size;
1028
1029 /*
1030 * We don't want gaps in the file. Write the blocks in front of *hp
1031 * to extend the file.
1032 * If block 'mf_infile_count' is not in the hash list, it has been
1033 * freed. Fill the space in the file with data from the current block.
1034 */
1035 for (;;)
1036 {
1037 nr = hp->bh_bnum;
1038 if (nr > mfp->mf_infile_count) /* beyond end of file */
1039 {
1040 nr = mfp->mf_infile_count;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001041 hp2 = mf_find_hash(mfp, nr); /* NULL caught below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 }
1043 else
1044 hp2 = hp;
1045
1046 offset = (off_t)page_size * nr;
1047 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1048 {
Bram Moolenaar3f2d9812006-11-07 17:02:30 +00001049 PERROR(_("E296: Seek error in swap file write"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 return FAIL;
1051 }
1052 if (hp2 == NULL) /* freed block, fill with dummy data */
1053 page_count = 1;
1054 else
1055 page_count = hp2->bh_page_count;
1056 size = page_size * page_count;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001057 if (mf_write_block(mfp, hp2 == NULL ? hp : hp2, offset, size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 {
1059 /*
1060 * Avoid repeating the error message, this mostly happens when the
Bram Moolenaar49325942007-05-10 19:19:59 +00001061 * disk is full. We give the message again only after a successful
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 * write or when hitting a key. We keep on trying, in case some
1063 * space becomes available.
1064 */
1065 if (!did_swapwrite_msg)
1066 EMSG(_("E297: Write error in swap file"));
1067 did_swapwrite_msg = TRUE;
1068 return FAIL;
1069 }
1070 did_swapwrite_msg = FALSE;
1071 if (hp2 != NULL) /* written a non-dummy block */
1072 hp2->bh_flags &= ~BH_DIRTY;
1073 /* appended to the file */
1074 if (nr + (blocknr_T)page_count > mfp->mf_infile_count)
1075 mfp->mf_infile_count = nr + page_count;
1076 if (nr == hp->bh_bnum) /* written the desired block */
1077 break;
1078 }
1079 return OK;
1080}
1081
1082/*
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001083 * Write block "hp" with data size "size" to file "mfp->mf_fd".
1084 * Takes care of encryption.
1085 * Return FAIL or OK.
1086 */
1087 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001088mf_write_block(
1089 memfile_T *mfp,
1090 bhdr_T *hp,
1091 off_t offset UNUSED,
1092 unsigned size)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001093{
1094 char_u *data = hp->bh_data;
1095 int result = OK;
1096
1097#ifdef FEAT_CRYPT
1098 /* Encrypt if 'key' is set and this is a data block. */
1099 if (*mfp->mf_buffer->b_p_key != NUL)
1100 {
1101 data = ml_encrypt_data(mfp, data, offset, size);
1102 if (data == NULL)
1103 return FAIL;
1104 }
1105#endif
1106
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001107 if ((unsigned)write_eintr(mfp->mf_fd, data, size) != size)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001108 result = FAIL;
1109
1110#ifdef FEAT_CRYPT
1111 if (data != hp->bh_data)
1112 vim_free(data);
1113#endif
1114
1115 return result;
1116}
1117
1118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 * Make block number for *hp positive and add it to the translation list
1120 *
1121 * Return FAIL for failure, OK otherwise
1122 */
1123 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001124mf_trans_add(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125{
1126 bhdr_T *freep;
1127 blocknr_T new_bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 NR_TRANS *np;
1129 int page_count;
1130
1131 if (hp->bh_bnum >= 0) /* it's already positive */
1132 return OK;
1133
1134 if ((np = (NR_TRANS *)alloc((unsigned)sizeof(NR_TRANS))) == NULL)
1135 return FAIL;
1136
1137/*
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001138 * Get a new number for the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 * If the first item in the free list has sufficient pages, use its number
1140 * Otherwise use mf_blocknr_max.
1141 */
1142 freep = mfp->mf_free_first;
1143 page_count = hp->bh_page_count;
1144 if (freep != NULL && freep->bh_page_count >= page_count)
1145 {
1146 new_bnum = freep->bh_bnum;
1147 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001148 * If the page count of the free block was larger, reduce it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 * If the page count matches, remove the block from the free list
1150 */
1151 if (freep->bh_page_count > page_count)
1152 {
1153 freep->bh_bnum += page_count;
1154 freep->bh_page_count -= page_count;
1155 }
1156 else
1157 {
1158 freep = mf_rem_free(mfp);
1159 vim_free(freep);
1160 }
1161 }
1162 else
1163 {
1164 new_bnum = mfp->mf_blocknr_max;
1165 mfp->mf_blocknr_max += page_count;
1166 }
1167
1168 np->nt_old_bnum = hp->bh_bnum; /* adjust number */
1169 np->nt_new_bnum = new_bnum;
1170
1171 mf_rem_hash(mfp, hp); /* remove from old hash list */
1172 hp->bh_bnum = new_bnum;
1173 mf_ins_hash(mfp, hp); /* insert in new hash list */
1174
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001175 /* Insert "np" into "mf_trans" hashtable with key "np->nt_old_bnum" */
1176 mf_hash_add_item(&mfp->mf_trans, (mf_hashitem_T *)np);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
1178 return OK;
1179}
1180
1181/*
Bram Moolenaarbc563362015-06-09 18:35:25 +02001182 * Lookup a translation from the trans lists and delete the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 *
1184 * Return the positive new number when found, the old number when not found
1185 */
1186 blocknr_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001187mf_trans_del(memfile_T *mfp, blocknr_T old_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 NR_TRANS *np;
1190 blocknr_T new_bnum;
1191
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001192 np = (NR_TRANS *)mf_hash_find(&mfp->mf_trans, old_nr);
1193
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 if (np == NULL) /* not found */
1195 return old_nr;
1196
1197 mfp->mf_neg_count--;
1198 new_bnum = np->nt_new_bnum;
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001199
1200 /* remove entry from the trans list */
1201 mf_hash_rem_item(&mfp->mf_trans, (mf_hashitem_T *)np);
1202
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 vim_free(np);
1204
1205 return new_bnum;
1206}
1207
1208/*
1209 * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
1210 * Only called when creating or renaming the swapfile. Either way it's a new
1211 * name so we must work out the full path name.
1212 */
1213 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001214mf_set_ffname(memfile_T *mfp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215{
1216 mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE);
1217}
1218
1219/*
1220 * Make the name of the file used for the memfile a full path.
1221 * Used before doing a :cd
1222 */
1223 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001224mf_fullname(memfile_T *mfp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225{
1226 if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL)
1227 {
1228 vim_free(mfp->mf_fname);
1229 mfp->mf_fname = mfp->mf_ffname;
1230 mfp->mf_ffname = NULL;
1231 }
1232}
1233
1234/*
1235 * return TRUE if there are any translations pending for 'mfp'
1236 */
1237 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001238mf_need_trans(memfile_T *mfp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239{
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
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001249mf_do_open(
1250 memfile_T *mfp,
1251 char_u *fname,
1252 int flags) /* flags for open() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253{
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);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001266#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001268 * A ":!cd e:xxx" may change the directory without us knowing, use the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 * 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 */
Bram Moolenaara5792f52005-11-23 21:25:05 +00001290 flags |= O_EXTRA | O_NOFOLLOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291#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
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00001311 {
Bram Moolenaarf05da212009-11-17 16:13:15 +00001312#ifdef HAVE_FD_CLOEXEC
1313 int fdflags = fcntl(mfp->mf_fd, F_GETFD);
1314 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01001315 (void)fcntl(mfp->mf_fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00001316#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02001317#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00001318 mch_copy_sec(fname, mfp->mf_fname);
1319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 mch_hide(mfp->mf_fname); /* try setting the 'hidden' flag */
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00001321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001323
1324/*
1325 * Implementation of mf_hashtab_T follows.
1326 */
1327
1328/*
1329 * The number of buckets in the hashtable is increased by a factor of
1330 * MHT_GROWTH_FACTOR when the average number of items per bucket
1331 * exceeds 2 ^ MHT_LOG_LOAD_FACTOR.
1332 */
1333#define MHT_LOG_LOAD_FACTOR 6
1334#define MHT_GROWTH_FACTOR 2 /* must be a power of two */
1335
1336/*
1337 * Initialize an empty hash table.
1338 */
1339 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001340mf_hash_init(mf_hashtab_T *mht)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001341{
1342 vim_memset(mht, 0, sizeof(mf_hashtab_T));
1343 mht->mht_buckets = mht->mht_small_buckets;
1344 mht->mht_mask = MHT_INIT_SIZE - 1;
1345}
1346
1347/*
1348 * Free the array of a hash table. Does not free the items it contains!
1349 * The hash table must not be used again without another mf_hash_init() call.
1350 */
1351 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001352mf_hash_free(mf_hashtab_T *mht)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001353{
1354 if (mht->mht_buckets != mht->mht_small_buckets)
1355 vim_free(mht->mht_buckets);
1356}
1357
1358/*
1359 * Free the array of a hash table and all the items it contains.
1360 */
1361 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001362mf_hash_free_all(mf_hashtab_T *mht)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001363{
1364 long_u idx;
1365 mf_hashitem_T *mhi;
1366 mf_hashitem_T *next;
1367
1368 for (idx = 0; idx <= mht->mht_mask; idx++)
1369 for (mhi = mht->mht_buckets[idx]; mhi != NULL; mhi = next)
1370 {
1371 next = mhi->mhi_next;
1372 vim_free(mhi);
1373 }
1374
1375 mf_hash_free(mht);
1376}
1377
1378/*
1379 * Find "key" in hashtable "mht".
1380 * Returns a pointer to a mf_hashitem_T or NULL if the item was not found.
1381 */
1382 static mf_hashitem_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001383mf_hash_find(mf_hashtab_T *mht, blocknr_T key)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001384{
1385 mf_hashitem_T *mhi;
1386
1387 mhi = mht->mht_buckets[key & mht->mht_mask];
1388 while (mhi != NULL && mhi->mhi_key != key)
1389 mhi = mhi->mhi_next;
1390
1391 return mhi;
1392}
1393
1394/*
1395 * Add item "mhi" to hashtable "mht".
1396 * "mhi" must not be NULL.
1397 */
1398 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001399mf_hash_add_item(mf_hashtab_T *mht, mf_hashitem_T *mhi)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001400{
1401 long_u idx;
1402
1403 idx = mhi->mhi_key & mht->mht_mask;
1404 mhi->mhi_next = mht->mht_buckets[idx];
1405 mhi->mhi_prev = NULL;
1406 if (mhi->mhi_next != NULL)
1407 mhi->mhi_next->mhi_prev = mhi;
1408 mht->mht_buckets[idx] = mhi;
1409
1410 mht->mht_count++;
1411
1412 /*
1413 * Grow hashtable when we have more thank 2^MHT_LOG_LOAD_FACTOR
1414 * items per bucket on average
1415 */
1416 if (mht->mht_fixed == 0
1417 && (mht->mht_count >> MHT_LOG_LOAD_FACTOR) > mht->mht_mask)
1418 {
1419 if (mf_hash_grow(mht) == FAIL)
1420 {
1421 /* stop trying to grow after first failure to allocate memory */
1422 mht->mht_fixed = 1;
1423 }
1424 }
1425}
1426
1427/*
1428 * Remove item "mhi" from hashtable "mht".
1429 * "mhi" must not be NULL and must have been inserted into "mht".
1430 */
1431 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001432mf_hash_rem_item(mf_hashtab_T *mht, mf_hashitem_T *mhi)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001433{
1434 if (mhi->mhi_prev == NULL)
1435 mht->mht_buckets[mhi->mhi_key & mht->mht_mask] = mhi->mhi_next;
1436 else
1437 mhi->mhi_prev->mhi_next = mhi->mhi_next;
1438
1439 if (mhi->mhi_next != NULL)
1440 mhi->mhi_next->mhi_prev = mhi->mhi_prev;
1441
1442 mht->mht_count--;
1443
1444 /* We could shrink the table here, but it typically takes little memory,
1445 * so why bother? */
1446}
1447
1448/*
1449 * Increase number of buckets in the hashtable by MHT_GROWTH_FACTOR and
1450 * rehash items.
1451 * Returns FAIL when out of memory.
1452 */
1453 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001454mf_hash_grow(mf_hashtab_T *mht)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001455{
1456 long_u i, j;
1457 int shift;
1458 mf_hashitem_T *mhi;
1459 mf_hashitem_T *tails[MHT_GROWTH_FACTOR];
1460 mf_hashitem_T **buckets;
1461 size_t size;
1462
1463 size = (mht->mht_mask + 1) * MHT_GROWTH_FACTOR * sizeof(void *);
1464 buckets = (mf_hashitem_T **)lalloc_clear(size, FALSE);
1465 if (buckets == NULL)
1466 return FAIL;
1467
1468 shift = 0;
1469 while ((mht->mht_mask >> shift) != 0)
1470 shift++;
1471
1472 for (i = 0; i <= mht->mht_mask; i++)
1473 {
1474 /*
1475 * Traverse the items in the i-th original bucket and move them into
1476 * MHT_GROWTH_FACTOR new buckets, preserving their relative order
1477 * within each new bucket. Preserving the order is important because
1478 * mf_get() tries to keep most recently used items at the front of
1479 * each bucket.
1480 *
1481 * Here we strongly rely on the fact the hashes are computed modulo
1482 * a power of two.
1483 */
1484
1485 vim_memset(tails, 0, sizeof(tails));
1486
1487 for (mhi = mht->mht_buckets[i]; mhi != NULL; mhi = mhi->mhi_next)
1488 {
1489 j = (mhi->mhi_key >> shift) & (MHT_GROWTH_FACTOR - 1);
1490 if (tails[j] == NULL)
1491 {
1492 buckets[i + (j << shift)] = mhi;
1493 tails[j] = mhi;
1494 mhi->mhi_prev = NULL;
1495 }
1496 else
1497 {
1498 tails[j]->mhi_next = mhi;
1499 mhi->mhi_prev = tails[j];
1500 tails[j] = mhi;
1501 }
1502 }
1503
1504 for (j = 0; j < MHT_GROWTH_FACTOR; j++)
1505 if (tails[j] != NULL)
1506 tails[j]->mhi_next = NULL;
1507 }
1508
1509 if (mht->mht_buckets != mht->mht_small_buckets)
1510 vim_free(mht->mht_buckets);
1511
1512 mht->mht_buckets = buckets;
1513 mht->mht_mask = (mht->mht_mask + 1) * MHT_GROWTH_FACTOR - 1;
1514
1515 return OK;
1516}