blob: abb7c3fb3f2d1ae2aa541d8df04179470886054e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# endif
50#endif
51
52/*
53 * for Amiga Dos 2.0x we use Flush
54 */
55#ifdef AMIGA
56# ifdef FEAT_ARP
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010057extern int dos2; // this is in os_amiga.c
Bram Moolenaar071d4272004-06-13 20:20:40 +000058# endif
59# ifdef SASC
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010060# include <ios1.h> // for chkufb()
Bram Moolenaar071d4272004-06-13 20:20:40 +000061# endif
62#endif
63
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010064#define MEMFILE_PAGE_SIZE 4096 // default page size
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010066static long_u total_mem_used = 0; // total memory used for memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010068static void mf_ins_hash(memfile_T *, bhdr_T *);
69static void mf_rem_hash(memfile_T *, bhdr_T *);
70static bhdr_T *mf_find_hash(memfile_T *, blocknr_T);
71static void mf_ins_used(memfile_T *, bhdr_T *);
72static void mf_rem_used(memfile_T *, bhdr_T *);
73static bhdr_T *mf_release(memfile_T *, int);
74static bhdr_T *mf_alloc_bhdr(memfile_T *, int);
75static void mf_free_bhdr(bhdr_T *);
76static void mf_ins_free(memfile_T *, bhdr_T *);
77static bhdr_T *mf_rem_free(memfile_T *);
78static int mf_read(memfile_T *, bhdr_T *);
79static int mf_write(memfile_T *, bhdr_T *);
Bram Moolenaar8767f522016-07-01 17:17:39 +020080static int mf_write_block(memfile_T *mfp, bhdr_T *hp, off_T offset, unsigned size);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010081static int mf_trans_add(memfile_T *, bhdr_T *);
82static void mf_do_open(memfile_T *, char_u *, int);
83static void mf_hash_init(mf_hashtab_T *);
84static void mf_hash_free(mf_hashtab_T *);
85static void mf_hash_free_all(mf_hashtab_T *);
86static mf_hashitem_T *mf_hash_find(mf_hashtab_T *, blocknr_T);
87static void mf_hash_add_item(mf_hashtab_T *, mf_hashitem_T *);
88static void mf_hash_rem_item(mf_hashtab_T *, mf_hashitem_T *);
89static int mf_hash_grow(mf_hashtab_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
91/*
92 * The functions for using a memfile:
93 *
94 * mf_open() open a new or existing memfile
95 * mf_open_file() open a swap file for an existing memfile
96 * mf_close() close (and delete) a memfile
97 * mf_new() create a new block in a memfile and lock it
98 * mf_get() get an existing block and lock it
99 * mf_put() unlock a block, may be marked for writing
100 * mf_free() remove a block
101 * mf_sync() sync changed parts of memfile to disk
102 * mf_release_all() release as much memory as possible
103 * mf_trans_del() may translate negative to positive block number
104 * mf_fullname() make file name full path (use before first :cd)
105 */
106
107/*
108 * Open an existing or new memory block file.
109 *
110 * fname: name of file to use (NULL means no file at all)
111 * Note: fname must have been allocated, it is not copied!
112 * If opening the file fails, fname is freed.
113 * flags: flags for open() call
114 *
115 * If fname != NULL and file cannot be opened, fail.
116 *
117 * return value: identifier for this memory block file.
118 */
119 memfile_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100120mf_open(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121{
122 memfile_T *mfp;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200123 off_T size;
Bram Moolenaara03dbed2013-05-23 22:27:03 +0200124#if defined(STATFS) && defined(UNIX) && !defined(__QNX__) && !defined(__minix)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125# define USE_FSTATFS
126 struct STATFS stf;
127#endif
128
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200129 if ((mfp = ALLOC_ONE(memfile_T)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 return NULL;
131
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100132 if (fname == NULL) // no file for this memfile, use memory only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 {
134 mfp->mf_fname = NULL;
135 mfp->mf_ffname = NULL;
136 mfp->mf_fd = -1;
137 }
138 else
139 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100140 mf_do_open(mfp, fname, flags); // try to open the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100142 // if the file cannot be opened, return here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 if (mfp->mf_fd < 0)
144 {
145 vim_free(mfp);
146 return NULL;
147 }
148 }
149
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100150 mfp->mf_free_first = NULL; // free list is empty
151 mfp->mf_used_first = NULL; // used list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 mfp->mf_used_last = NULL;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100153 mfp->mf_dirty = MF_DIRTY_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 mfp->mf_used_count = 0;
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100155 mf_hash_init(&mfp->mf_hash);
156 mf_hash_init(&mfp->mf_trans);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 mfp->mf_page_size = MEMFILE_PAGE_SIZE;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200158#ifdef FEAT_CRYPT
159 mfp->mf_old_key = NULL;
160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161
162#ifdef USE_FSTATFS
163 /*
164 * Try to set the page size equal to the block size of the device.
165 * Speeds up I/O a lot.
166 * When recovering, the actual block size will be retrieved from block 0
167 * in ml_recover(). The size used here may be wrong, therefore
168 * mf_blocknr_max must be rounded up.
169 */
170 if (mfp->mf_fd >= 0
171 && fstatfs(mfp->mf_fd, &stf, sizeof(struct statfs), 0) == 0
172 && stf.F_BSIZE >= MIN_SWAP_PAGE_SIZE
173 && stf.F_BSIZE <= MAX_SWAP_PAGE_SIZE)
174 mfp->mf_page_size = stf.F_BSIZE;
175#endif
176
177 if (mfp->mf_fd < 0 || (flags & (O_TRUNC|O_EXCL))
Bram Moolenaar8767f522016-07-01 17:17:39 +0200178 || (size = vim_lseek(mfp->mf_fd, (off_T)0L, SEEK_END)) <= 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100179 mfp->mf_blocknr_max = 0; // no file or empty file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 else
181 mfp->mf_blocknr_max = (blocknr_T)((size + mfp->mf_page_size - 1)
182 / mfp->mf_page_size);
183 mfp->mf_blocknr_min = -1;
184 mfp->mf_neg_count = 0;
185 mfp->mf_infile_count = mfp->mf_blocknr_max;
Bram Moolenaarfe265ff2007-05-11 18:15:45 +0000186
187 /*
188 * Compute maximum number of pages ('maxmem' is in Kbyte):
189 * 'mammem' * 1Kbyte / page-size-in-bytes.
190 * Avoid overflow by first reducing page size as much as possible.
191 */
192 {
193 int shift = 10;
194 unsigned page_size = mfp->mf_page_size;
195
196 while (shift > 0 && (page_size & 1) == 0)
197 {
198 page_size = page_size >> 1;
199 --shift;
200 }
201 mfp->mf_used_count_max = (p_mm << shift) / page_size;
202 if (mfp->mf_used_count_max < 10)
203 mfp->mf_used_count_max = 10;
204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206 return mfp;
207}
208
209/*
210 * Open a file for an existing memfile. Used when updatecount set from 0 to
211 * some value.
212 * If the file already exists, this fails.
213 * "fname" is the name of file to use (NULL means no file at all)
214 * Note: "fname" must have been allocated, it is not copied! If opening the
215 * file fails, "fname" is freed.
216 *
217 * return value: FAIL if file could not be opened, OK otherwise
218 */
219 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100220mf_open_file(memfile_T *mfp, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100222 mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); // try to open the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223
224 if (mfp->mf_fd < 0)
225 return FAIL;
226
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100227 mfp->mf_dirty = MF_DIRTY_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 return OK;
229}
230
231/*
Bram Moolenaar945e2db2010-06-05 17:43:32 +0200232 * Close a memory file and delete the associated file if 'del_file' is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 */
234 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100235mf_close(memfile_T *mfp, int del_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 bhdr_T *hp, *nextp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100239 if (mfp == NULL) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 return;
241 if (mfp->mf_fd >= 0)
242 {
243 if (close(mfp->mf_fd) < 0)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +0000244 emsg(_(e_close_error_on_swap_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 }
246 if (del_file && mfp->mf_fname != NULL)
247 mch_remove(mfp->mf_fname);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100248 // free entries in used list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 for (hp = mfp->mf_used_first; hp != NULL; hp = nextp)
250 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +0000251 total_mem_used -= (long_u)hp->bh_page_count * mfp->mf_page_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 nextp = hp->bh_next;
253 mf_free_bhdr(hp);
254 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100255 while (mfp->mf_free_first != NULL) // free entries in free list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 vim_free(mf_rem_free(mfp));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100257 mf_hash_free(&mfp->mf_hash);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100258 mf_hash_free_all(&mfp->mf_trans); // free hashtable and its items
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 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
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100268mf_close_file(
269 buf_T *buf,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100270 int getlines) // get all lines into memory?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271{
272 memfile_T *mfp;
273 linenr_T lnum;
274
275 mfp = buf->b_ml.ml_mfp;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100276 if (mfp == NULL || mfp->mf_fd < 0) // nothing to close
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 return;
278
279 if (getlines)
280 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100281 // get all blocks in memory by accessing all lines (clumsy!)
Bram Moolenaar47b8b152007-02-07 02:41:57 +0000282 mf_dont_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
284 (void)ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar47b8b152007-02-07 02:41:57 +0000285 mf_dont_release = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100286 // TODO: should check if all blocks are really in core
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 }
288
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100289 if (close(mfp->mf_fd) < 0) // close the file
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +0000290 emsg(_(e_close_error_on_swap_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 mfp->mf_fd = -1;
292
293 if (mfp->mf_fname != NULL)
294 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100295 mch_remove(mfp->mf_fname); // delete the swap file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100296 VIM_CLEAR(mfp->mf_fname);
297 VIM_CLEAR(mfp->mf_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 }
299}
300
301/*
302 * Set new size for a memfile. Used when block 0 of a swapfile has been read
303 * and the size it indicates differs from what was guessed.
304 */
305 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100306mf_new_page_size(memfile_T *mfp, unsigned new_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100308 // Correct the memory used for block 0 to the new size, because it will be
309 // freed with that size later on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 total_mem_used += new_size - mfp->mf_page_size;
311 mfp->mf_page_size = new_size;
312}
313
314/*
315 * get a new block
316 *
317 * negative: TRUE if negative block number desired (data block)
318 */
319 bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100320mf_new(memfile_T *mfp, int negative, int page_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100322 bhdr_T *hp; // new bhdr_T
323 bhdr_T *freep; // first block in free list
Bram Moolenaar17e79192007-05-06 14:15:36 +0000324 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325
326 /*
327 * If we reached the maximum size for the used memory blocks, release one
328 * If a bhdr_T is returned, use it and adjust the page_count if necessary.
329 */
330 hp = mf_release(mfp, page_count);
331
332/*
333 * Decide on the number to use:
334 * If there is a free block, use its number.
335 * Otherwise use mf_block_min for a negative number, mf_block_max for
336 * a positive number.
337 */
338 freep = mfp->mf_free_first;
339 if (!negative && freep != NULL && freep->bh_page_count >= page_count)
340 {
341 /*
342 * If the block in the free list has more pages, take only the number
343 * of pages needed and allocate a new bhdr_T with data
344 *
Bram Moolenaar17e79192007-05-06 14:15:36 +0000345 * If the number of pages matches and mf_release() did not return a
346 * bhdr_T, use the bhdr_T from the free list and allocate the data
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 *
Bram Moolenaar17e79192007-05-06 14:15:36 +0000348 * If the number of pages matches and mf_release() returned a bhdr_T,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 * just use the number and free the bhdr_T from the free list
350 */
351 if (freep->bh_page_count > page_count)
352 {
353 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
354 return NULL;
355 hp->bh_bnum = freep->bh_bnum;
356 freep->bh_bnum += page_count;
357 freep->bh_page_count -= page_count;
358 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100359 else if (hp == NULL) // need to allocate memory for this block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +0000361 if ((p = alloc((size_t)mfp->mf_page_size * page_count)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 return NULL;
363 hp = mf_rem_free(mfp);
364 hp->bh_data = p;
365 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100366 else // use the number, remove entry from free list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 {
368 freep = mf_rem_free(mfp);
369 hp->bh_bnum = freep->bh_bnum;
370 vim_free(freep);
371 }
372 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100373 else // get a new number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {
375 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
376 return NULL;
377 if (negative)
378 {
379 hp->bh_bnum = mfp->mf_blocknr_min--;
380 mfp->mf_neg_count++;
381 }
382 else
383 {
384 hp->bh_bnum = mfp->mf_blocknr_max;
385 mfp->mf_blocknr_max += page_count;
386 }
387 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100388 hp->bh_flags = BH_LOCKED | BH_DIRTY; // new block is always dirty
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100389 mfp->mf_dirty = MF_DIRTY_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 hp->bh_page_count = page_count;
391 mf_ins_used(mfp, hp);
392 mf_ins_hash(mfp, hp);
393
394 /*
395 * Init the data to all zero, to avoid reading uninitialized data.
396 * This also avoids that the passwd file ends up in the swap file!
397 */
Bram Moolenaar945e2db2010-06-05 17:43:32 +0200398 (void)vim_memset((char *)(hp->bh_data), 0,
399 (size_t)mfp->mf_page_size * page_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400
401 return hp;
402}
403
404/*
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200405 * Get existing block "nr" with "page_count" pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 *
407 * Note: The caller should first check a negative nr with mf_trans_del()
408 */
409 bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100410mf_get(memfile_T *mfp, blocknr_T nr, int page_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411{
412 bhdr_T *hp;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100413 // doesn't exist
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
415 return NULL;
416
417 /*
418 * see if it is in the cache
419 */
420 hp = mf_find_hash(mfp, nr);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100421 if (hp == NULL) // not in the hash list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100423 if (nr < 0 || nr >= mfp->mf_infile_count) // can't be in the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 return NULL;
425
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100426 // could check here if the block is in the free list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427
428 /*
429 * Check if we need to flush an existing block.
430 * If so, use that block.
431 * If not, allocate a new block.
432 */
433 hp = mf_release(mfp, page_count);
Bram Moolenaarb67ba032023-04-22 21:14:26 +0100434 if (hp == NULL && page_count > 0)
435 hp = mf_alloc_bhdr(mfp, page_count);
436 if (hp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 return NULL;
438
439 hp->bh_bnum = nr;
440 hp->bh_flags = 0;
441 hp->bh_page_count = page_count;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100442 if (mf_read(mfp, hp) == FAIL) // cannot read the block!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 {
444 mf_free_bhdr(hp);
445 return NULL;
446 }
447 }
448 else
449 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100450 mf_rem_used(mfp, hp); // remove from list, insert in front below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 mf_rem_hash(mfp, hp);
452 }
453
454 hp->bh_flags |= BH_LOCKED;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100455 mf_ins_used(mfp, hp); // put in front of used list
456 mf_ins_hash(mfp, hp); // put in front of hash list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457
458 return hp;
459}
460
461/*
462 * release the block *hp
463 *
464 * dirty: Block must be written to file later
465 * infile: Block should be in file (needed for recovery)
466 *
467 * no return value, function cannot fail
468 */
469 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100470mf_put(
471 memfile_T *mfp,
472 bhdr_T *hp,
473 int dirty,
474 int infile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
476 int flags;
477
478 flags = hp->bh_flags;
479
480 if ((flags & BH_LOCKED) == 0)
RestorerZ68ebcee2023-05-31 17:12:14 +0100481 iemsg(e_block_was_not_locked);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 flags &= ~BH_LOCKED;
483 if (dirty)
484 {
485 flags |= BH_DIRTY;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100486 if (mfp->mf_dirty != MF_DIRTY_YES_NOSYNC)
487 mfp->mf_dirty = MF_DIRTY_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 }
489 hp->bh_flags = flags;
490 if (infile)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100491 mf_trans_add(mfp, hp); // may translate negative in positive nr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492}
493
494/*
495 * block *hp is no longer in used, may put it in the free list of memfile *mfp
496 */
497 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100498mf_free(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100500 vim_free(hp->bh_data); // free the memory
501 mf_rem_hash(mfp, hp); // get *hp out of the hash list
502 mf_rem_used(mfp, hp); // get *hp out of the used list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 if (hp->bh_bnum < 0)
504 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100505 vim_free(hp); // don't want negative numbers in free list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 mfp->mf_neg_count--;
507 }
508 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100509 mf_ins_free(mfp, hp); // put *hp in the free list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510}
511
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512/*
513 * Sync the memory file *mfp to disk.
514 * Flags:
515 * MFS_ALL If not given, blocks with negative numbers are not synced,
516 * even when they are dirty!
517 * MFS_STOP Stop syncing when a character becomes available, but sync at
518 * least one block.
519 * MFS_FLUSH Make sure buffers are flushed to disk, so they will survive a
520 * system crash.
521 * MFS_ZERO Only write block 0.
522 *
523 * Return FAIL for failure, OK otherwise
524 */
525 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100526mf_sync(memfile_T *mfp, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
528 int status;
529 bhdr_T *hp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 int got_int_save = got_int;
531
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100532 if (mfp->mf_fd < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 {
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100534 // there is no file, nothing to do
535 mfp->mf_dirty = MF_DIRTY_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 return FAIL;
537 }
538
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100539 // Only a CTRL-C while writing will break us here, not one typed
540 // previously.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 got_int = FALSE;
542
543 /*
544 * sync from last to first (may reduce the probability of an inconsistent
545 * file) If a write fails, it is very likely caused by a full filesystem.
546 * Then we only try to write blocks within the existing file. If that also
547 * fails then we give up.
548 */
549 status = OK;
550 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
551 if (((flags & MFS_ALL) || hp->bh_bnum >= 0)
552 && (hp->bh_flags & BH_DIRTY)
553 && (status == OK || (hp->bh_bnum >= 0
554 && hp->bh_bnum < mfp->mf_infile_count)))
555 {
556 if ((flags & MFS_ZERO) && hp->bh_bnum != 0)
557 continue;
558 if (mf_write(mfp, hp) == FAIL)
559 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100560 if (status == FAIL) // double error: quit syncing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 break;
562 status = FAIL;
563 }
564 if (flags & MFS_STOP)
565 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100566 // Stop when char available now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 if (ui_char_avail())
568 break;
569 }
570 else
571 ui_breakcheck();
572 if (got_int)
573 break;
574 }
575
576 /*
577 * If the whole list is flushed, the memfile is not dirty anymore.
578 * In case of an error this flag is also set, to avoid trying all the time.
579 */
580 if (hp == NULL || status == FAIL)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100581 mfp->mf_dirty = MF_DIRTY_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582
583 if ((flags & MFS_FLUSH) && *p_sws != NUL)
584 {
585#if defined(UNIX)
586# ifdef HAVE_FSYNC
587 /*
588 * most Unixes have the very useful fsync() function, just what we need.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 if (STRCMP(p_sws, "fsync") == 0)
591 {
Bram Moolenaara7870192019-02-14 12:56:36 +0100592 if (vim_fsync(mfp->mf_fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 status = FAIL;
594 }
595 else
596# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100597 // OpenNT is strictly POSIX (Benzinger)
598 // Tandem/Himalaya NSK-OSS doesn't have sync()
599 // No sync() on Stratus VOS
Bram Moolenaarba17ed62015-02-27 18:25:16 +0100600# if defined(__OPENNT) || defined(__TANDEM) || defined(__VOS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 fflush(NULL);
602# else
603 sync();
604# endif
605#endif
606#ifdef VMS
607 if (STRCMP(p_sws, "fsync") == 0)
608 {
Bram Moolenaara7870192019-02-14 12:56:36 +0100609 if (vim_fsync(mfp->mf_fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 status = FAIL;
611 }
612#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100613#ifdef MSWIN
Bram Moolenaar0bd40512018-09-21 14:48:53 +0200614 if (_commit(mfp->mf_fd))
615 status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616#endif
617#ifdef AMIGA
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000618# if defined(__AROS__) || defined(__amigaos4__)
Bram Moolenaara7870192019-02-14 12:56:36 +0100619 if (vim_fsync(mfp->mf_fd) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 status = FAIL;
621# else
622 /*
623 * Flush() only exists for AmigaDos 2.0.
624 * For 1.3 it should be done with close() + open(), but then the risk
625 * is that the open() may fail and lose the file....
626 */
627# ifdef FEAT_ARP
628 if (dos2)
629# endif
630# ifdef SASC
631 {
632 struct UFB *fp = chkufb(mfp->mf_fd);
633
634 if (fp != NULL)
635 Flush(fp->ufbfh);
636 }
637# else
638# if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
639 {
Bram Moolenaar89f37272006-09-26 11:48:34 +0000640# if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100641 // Have function (in libnix at least),
642 // but ain't got no prototype anywhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 extern unsigned long fdtofh(int filedescriptor);
644# endif
Bram Moolenaar89f37272006-09-26 11:48:34 +0000645# if !defined(__libnix__)
646 fflush(NULL);
647# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 BPTR fh = (BPTR)fdtofh(mfp->mf_fd);
649
650 if (fh != 0)
651 Flush(fh);
Bram Moolenaar89f37272006-09-26 11:48:34 +0000652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100654# else // assume Manx
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 Flush(_devtab[mfp->mf_fd].fd);
656# endif
657# endif
658# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100659#endif // AMIGA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
661
662 got_int |= got_int_save;
663
664 return status;
665}
666
667/*
Bram Moolenaarc32840f2006-01-14 21:23:38 +0000668 * For all blocks in memory file *mfp that have a positive block number set
669 * the dirty flag. These are blocks that need to be written to a newly
670 * created swapfile.
671 */
672 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100673mf_set_dirty(memfile_T *mfp)
Bram Moolenaarc32840f2006-01-14 21:23:38 +0000674{
675 bhdr_T *hp;
676
677 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
678 if (hp->bh_bnum > 0)
679 hp->bh_flags |= BH_DIRTY;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100680 mfp->mf_dirty = MF_DIRTY_YES;
Bram Moolenaarc32840f2006-01-14 21:23:38 +0000681}
682
683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 * insert block *hp in front of hashlist of memfile *mfp
685 */
686 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100687mf_ins_hash(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100689 mf_hash_add_item(&mfp->mf_hash, (mf_hashitem_T *)hp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690}
691
692/*
693 * remove block *hp from hashlist of memfile list *mfp
694 */
695 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100696mf_rem_hash(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697{
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100698 mf_hash_rem_item(&mfp->mf_hash, (mf_hashitem_T *)hp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699}
700
701/*
702 * look in hash lists of memfile *mfp for block header with number 'nr'
703 */
704 static bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100705mf_find_hash(memfile_T *mfp, blocknr_T nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706{
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100707 return (bhdr_T *)mf_hash_find(&mfp->mf_hash, nr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708}
709
710/*
711 * insert block *hp in front of used list of memfile *mfp
712 */
713 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100714mf_ins_used(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
716 hp->bh_next = mfp->mf_used_first;
717 mfp->mf_used_first = hp;
718 hp->bh_prev = NULL;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100719 if (hp->bh_next == NULL) // list was empty, adjust last pointer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 mfp->mf_used_last = hp;
721 else
722 hp->bh_next->bh_prev = hp;
723 mfp->mf_used_count += hp->bh_page_count;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +0000724 total_mem_used += (long_u)hp->bh_page_count * mfp->mf_page_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725}
726
727/*
728 * remove block *hp from used list of memfile *mfp
729 */
730 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100731mf_rem_used(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100733 if (hp->bh_next == NULL) // last block in used list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 mfp->mf_used_last = hp->bh_prev;
735 else
736 hp->bh_next->bh_prev = hp->bh_prev;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100737 if (hp->bh_prev == NULL) // first block in used list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 mfp->mf_used_first = hp->bh_next;
739 else
740 hp->bh_prev->bh_next = hp->bh_next;
741 mfp->mf_used_count -= hp->bh_page_count;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +0000742 total_mem_used -= (long_u)hp->bh_page_count * mfp->mf_page_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743}
744
745/*
746 * Release the least recently used block from the used list if the number
747 * of used memory blocks gets to big.
748 *
749 * Return the block header to the caller, including the memory block, so
750 * it can be re-used. Make sure the page_count is right.
Bram Moolenaarbc563362015-06-09 18:35:25 +0200751 *
752 * Returns NULL if no block is released.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 */
754 static bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100755mf_release(memfile_T *mfp, int page_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756{
757 bhdr_T *hp;
758 int need_release;
759 buf_T *buf;
760
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100761 // don't release while in mf_close_file()
Bram Moolenaar47b8b152007-02-07 02:41:57 +0000762 if (mf_dont_release)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 return NULL;
764
765 /*
766 * Need to release a block if the number of blocks for this memfile is
767 * higher than the maximum or total memory used is over 'maxmemtot'
768 */
769 need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max)
770 || (total_mem_used >> 10) >= (long_u)p_mmt);
771
772 /*
773 * Try to create a swap file if the amount of memory used is getting too
774 * high.
775 */
776 if (mfp->mf_fd < 0 && need_release && p_uc)
777 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100778 // find for which buffer this memfile is
Bram Moolenaar29323592016-07-24 22:04:11 +0200779 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (buf->b_ml.ml_mfp == mfp)
781 break;
782 if (buf != NULL && buf->b_may_swap)
783 ml_open_file(buf);
784 }
785
786 /*
787 * don't release a block if
788 * there is no file for this memfile
789 * or
790 * the number of blocks for this memfile is lower than the maximum
791 * and
792 * total memory used is not up to 'maxmemtot'
793 */
794 if (mfp->mf_fd < 0 || !need_release)
795 return NULL;
796
797 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
798 if (!(hp->bh_flags & BH_LOCKED))
799 break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100800 if (hp == NULL) // not a single one that can be released
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 return NULL;
802
803 /*
804 * If the block is dirty, write it.
805 * If the write fails we don't free it.
806 */
807 if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL)
808 return NULL;
809
810 mf_rem_used(mfp, hp);
811 mf_rem_hash(mfp, hp);
812
813 /*
814 * If a bhdr_T is returned, make sure that the page_count of bh_data is
815 * right
816 */
817 if (hp->bh_page_count != page_count)
818 {
Bram Moolenaarb67ba032023-04-22 21:14:26 +0100819 VIM_CLEAR(hp->bh_data);
820 if (page_count > 0)
821 hp->bh_data = alloc((size_t)mfp->mf_page_size * page_count);
822 if (hp->bh_data == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
824 vim_free(hp);
825 return NULL;
826 }
827 hp->bh_page_count = page_count;
828 }
829 return hp;
830}
831
832/*
833 * release as many blocks as possible
834 * Used in case of out of memory
835 *
836 * return TRUE if any memory was released
837 */
838 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100839mf_release_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
841 buf_T *buf;
842 memfile_T *mfp;
843 bhdr_T *hp;
844 int retval = FALSE;
845
Bram Moolenaar29323592016-07-24 22:04:11 +0200846 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {
848 mfp = buf->b_ml.ml_mfp;
849 if (mfp != NULL)
850 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100851 // If no swap file yet, may open one
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 if (mfp->mf_fd < 0 && buf->b_may_swap)
853 ml_open_file(buf);
854
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100855 // only if there is a swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 if (mfp->mf_fd >= 0)
857 {
858 for (hp = mfp->mf_used_last; hp != NULL; )
859 {
860 if (!(hp->bh_flags & BH_LOCKED)
861 && (!(hp->bh_flags & BH_DIRTY)
862 || mf_write(mfp, hp) != FAIL))
863 {
864 mf_rem_used(mfp, hp);
865 mf_rem_hash(mfp, hp);
866 mf_free_bhdr(hp);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100867 hp = mfp->mf_used_last; // re-start, list was changed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 retval = TRUE;
869 }
870 else
871 hp = hp->bh_prev;
872 }
873 }
874 }
875 }
876 return retval;
877}
878
879/*
Bram Moolenaarb67ba032023-04-22 21:14:26 +0100880 * Allocate a block header and a block of memory for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 */
882 static bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100883mf_alloc_bhdr(memfile_T *mfp, int page_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884{
885 bhdr_T *hp;
886
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000887 if ((hp = ALLOC_ONE(bhdr_T)) == NULL)
888 return NULL;
889
Bram Moolenaarb67ba032023-04-22 21:14:26 +0100890 if ((hp->bh_data = alloc((size_t)mfp->mf_page_size * page_count)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000892 vim_free(hp); // not enough memory
893 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000895 hp->bh_page_count = page_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 return hp;
897}
898
899/*
Bram Moolenaarb67ba032023-04-22 21:14:26 +0100900 * Free a block header and the block of memory for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 */
902 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100903mf_free_bhdr(bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904{
905 vim_free(hp->bh_data);
906 vim_free(hp);
907}
908
909/*
Bram Moolenaarb67ba032023-04-22 21:14:26 +0100910 * Insert entry *hp in the free list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 */
912 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100913mf_ins_free(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914{
915 hp->bh_next = mfp->mf_free_first;
916 mfp->mf_free_first = hp;
917}
918
919/*
920 * remove the first entry from the free list and return a pointer to it
921 * Note: caller must check that mfp->mf_free_first is not NULL!
922 */
923 static bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100924mf_rem_free(memfile_T *mfp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925{
926 bhdr_T *hp;
927
928 hp = mfp->mf_free_first;
929 mfp->mf_free_first = hp->bh_next;
930 return hp;
931}
932
933/*
934 * read a block from disk
935 *
936 * Return FAIL for failure, OK otherwise
937 */
938 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100939mf_read(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940{
Bram Moolenaar8767f522016-07-01 17:17:39 +0200941 off_T offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 unsigned page_size;
943 unsigned size;
944
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100945 if (mfp->mf_fd < 0) // there is no file, can't read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 return FAIL;
947
948 page_size = mfp->mf_page_size;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200949 offset = (off_T)page_size * hp->bh_bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 size = page_size * hp->bh_page_count;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200951 if (vim_lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000953 PERROR(_(e_seek_error_in_swap_file_read));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 return FAIL;
955 }
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100956 if ((unsigned)read_eintr(mfp->mf_fd, hp->bh_data, size) != size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000958 PERROR(_(e_read_error_in_swap_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 return FAIL;
960 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200961
962#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100963 // Decrypt if 'key' is set and this is a data block. And when changing the
964 // key.
Bram Moolenaar4a8c2cf2015-12-19 15:28:18 +0100965 if (*mfp->mf_buffer->b_p_key != NUL || mfp->mf_old_key != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200966 ml_decrypt_data(mfp, hp->bh_data, offset, size);
967#endif
968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 return OK;
970}
971
972/*
973 * write a block to disk
974 *
975 * Return FAIL for failure, OK otherwise
976 */
977 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100978mf_write(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100980 off_T offset; // offset in the file
981 blocknr_T nr; // block nr which is being written
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 bhdr_T *hp2;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100983 unsigned page_size; // number of bytes in a page
984 unsigned page_count; // number of pages written
985 unsigned size; // number of bytes written
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
Bram Moolenaarb58a4b92019-05-27 23:36:21 +0200987 if (mfp->mf_fd < 0 && !mfp->mf_reopen)
988 // there is no file and there was no file, can't write
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 return FAIL;
990
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100991 if (hp->bh_bnum < 0) // must assign file block number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (mf_trans_add(mfp, hp) == FAIL)
993 return FAIL;
994
995 page_size = mfp->mf_page_size;
996
997 /*
998 * We don't want gaps in the file. Write the blocks in front of *hp
999 * to extend the file.
1000 * If block 'mf_infile_count' is not in the hash list, it has been
1001 * freed. Fill the space in the file with data from the current block.
1002 */
1003 for (;;)
1004 {
Bram Moolenaarb58a4b92019-05-27 23:36:21 +02001005 int attempt;
1006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 nr = hp->bh_bnum;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001008 if (nr > mfp->mf_infile_count) // beyond end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
1010 nr = mfp->mf_infile_count;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001011 hp2 = mf_find_hash(mfp, nr); // NULL caught below
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 else
1014 hp2 = hp;
1015
Bram Moolenaar8767f522016-07-01 17:17:39 +02001016 offset = (off_T)page_size * nr;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001017 if (hp2 == NULL) // freed block, fill with dummy data
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 page_count = 1;
1019 else
1020 page_count = hp2->bh_page_count;
1021 size = page_size * page_count;
Bram Moolenaarb58a4b92019-05-27 23:36:21 +02001022
1023 for (attempt = 1; attempt <= 2; ++attempt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 {
Bram Moolenaarb58a4b92019-05-27 23:36:21 +02001025 if (mfp->mf_fd >= 0)
1026 {
1027 if (vim_lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1028 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001029 PERROR(_(e_seek_error_in_swap_file_write));
Bram Moolenaarb58a4b92019-05-27 23:36:21 +02001030 return FAIL;
1031 }
1032 if (mf_write_block(mfp,
1033 hp2 == NULL ? hp : hp2, offset, size) == OK)
1034 break;
1035 }
1036
1037 if (attempt == 1)
1038 {
1039 // If the swap file is on a network drive, and the network
1040 // gets disconnected and then re-connected, we can maybe fix it
1041 // by closing and then re-opening the file.
1042 if (mfp->mf_fd >= 0)
1043 close(mfp->mf_fd);
1044 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, mfp->mf_flags);
1045 mfp->mf_reopen = (mfp->mf_fd < 0);
1046 }
1047 if (attempt == 2 || mfp->mf_fd < 0)
1048 {
1049 // Avoid repeating the error message, this mostly happens when
1050 // the disk is full. We give the message again only after a
1051 // successful write or when hitting a key. We keep on trying,
1052 // in case some space becomes available.
1053 if (!did_swapwrite_msg)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001054 emsg(_(e_write_error_in_swap_file));
Bram Moolenaarb58a4b92019-05-27 23:36:21 +02001055 did_swapwrite_msg = TRUE;
1056 return FAIL;
1057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
Bram Moolenaarb58a4b92019-05-27 23:36:21 +02001059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 did_swapwrite_msg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001061 if (hp2 != NULL) // written a non-dummy block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 hp2->bh_flags &= ~BH_DIRTY;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001063 // appended to the file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 if (nr + (blocknr_T)page_count > mfp->mf_infile_count)
1065 mfp->mf_infile_count = nr + page_count;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001066 if (nr == hp->bh_bnum) // written the desired block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 break;
1068 }
1069 return OK;
1070}
1071
1072/*
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001073 * Write block "hp" with data size "size" to file "mfp->mf_fd".
1074 * Takes care of encryption.
1075 * Return FAIL or OK.
1076 */
1077 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001078mf_write_block(
1079 memfile_T *mfp,
1080 bhdr_T *hp,
Bram Moolenaar8767f522016-07-01 17:17:39 +02001081 off_T offset UNUSED,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001082 unsigned size)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001083{
1084 char_u *data = hp->bh_data;
1085 int result = OK;
1086
1087#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001088 // Encrypt if 'key' is set and this is a data block.
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001089 if (*mfp->mf_buffer->b_p_key != NUL)
1090 {
1091 data = ml_encrypt_data(mfp, data, offset, size);
1092 if (data == NULL)
1093 return FAIL;
1094 }
1095#endif
1096
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001097 if ((unsigned)write_eintr(mfp->mf_fd, data, size) != size)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001098 result = FAIL;
1099
1100#ifdef FEAT_CRYPT
1101 if (data != hp->bh_data)
1102 vim_free(data);
1103#endif
1104
1105 return result;
1106}
1107
1108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 * Make block number for *hp positive and add it to the translation list
1110 *
1111 * Return FAIL for failure, OK otherwise
1112 */
1113 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001114mf_trans_add(memfile_T *mfp, bhdr_T *hp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
1116 bhdr_T *freep;
1117 blocknr_T new_bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 NR_TRANS *np;
1119 int page_count;
1120
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001121 if (hp->bh_bnum >= 0) // it's already positive
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 return OK;
1123
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001124 if ((np = ALLOC_ONE(NR_TRANS)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 return FAIL;
1126
1127/*
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001128 * Get a new number for the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 * If the first item in the free list has sufficient pages, use its number
1130 * Otherwise use mf_blocknr_max.
1131 */
1132 freep = mfp->mf_free_first;
1133 page_count = hp->bh_page_count;
1134 if (freep != NULL && freep->bh_page_count >= page_count)
1135 {
1136 new_bnum = freep->bh_bnum;
1137 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001138 * If the page count of the free block was larger, reduce it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 * If the page count matches, remove the block from the free list
1140 */
1141 if (freep->bh_page_count > page_count)
1142 {
1143 freep->bh_bnum += page_count;
1144 freep->bh_page_count -= page_count;
1145 }
1146 else
1147 {
1148 freep = mf_rem_free(mfp);
1149 vim_free(freep);
1150 }
1151 }
1152 else
1153 {
1154 new_bnum = mfp->mf_blocknr_max;
1155 mfp->mf_blocknr_max += page_count;
1156 }
1157
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001158 np->nt_old_bnum = hp->bh_bnum; // adjust number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 np->nt_new_bnum = new_bnum;
1160
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001161 mf_rem_hash(mfp, hp); // remove from old hash list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 hp->bh_bnum = new_bnum;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001163 mf_ins_hash(mfp, hp); // insert in new hash list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001165 // Insert "np" into "mf_trans" hashtable with key "np->nt_old_bnum"
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001166 mf_hash_add_item(&mfp->mf_trans, (mf_hashitem_T *)np);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
1168 return OK;
1169}
1170
1171/*
Bram Moolenaarbc563362015-06-09 18:35:25 +02001172 * Lookup a translation from the trans lists and delete the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 *
1174 * Return the positive new number when found, the old number when not found
1175 */
1176 blocknr_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001177mf_trans_del(memfile_T *mfp, blocknr_T old_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 NR_TRANS *np;
1180 blocknr_T new_bnum;
1181
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001182 np = (NR_TRANS *)mf_hash_find(&mfp->mf_trans, old_nr);
1183
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001184 if (np == NULL) // not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 return old_nr;
1186
1187 mfp->mf_neg_count--;
1188 new_bnum = np->nt_new_bnum;
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001189
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001190 // remove entry from the trans list
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001191 mf_hash_rem_item(&mfp->mf_trans, (mf_hashitem_T *)np);
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 vim_free(np);
1194
1195 return new_bnum;
1196}
1197
1198/*
1199 * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
1200 * Only called when creating or renaming the swapfile. Either way it's a new
1201 * name so we must work out the full path name.
1202 */
1203 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001204mf_set_ffname(memfile_T *mfp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205{
1206 mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE);
1207}
1208
1209/*
1210 * Make the name of the file used for the memfile a full path.
1211 * Used before doing a :cd
1212 */
1213 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001214mf_fullname(memfile_T *mfp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001216 if (mfp == NULL || mfp->mf_fname == NULL || mfp->mf_ffname == NULL)
1217 return;
1218
1219 vim_free(mfp->mf_fname);
1220 mfp->mf_fname = mfp->mf_ffname;
1221 mfp->mf_ffname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222}
1223
1224/*
1225 * return TRUE if there are any translations pending for 'mfp'
1226 */
1227 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001228mf_need_trans(memfile_T *mfp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229{
1230 return (mfp->mf_fname != NULL && mfp->mf_neg_count > 0);
1231}
1232
1233/*
1234 * Open a swap file for a memfile.
1235 * The "fname" must be in allocated memory, and is consumed (also when an
1236 * error occurs).
1237 */
1238 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001239mf_do_open(
1240 memfile_T *mfp,
1241 char_u *fname,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001242 int flags) // flags for open()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243{
1244#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02001245 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246#endif
1247
1248 mfp->mf_fname = fname;
1249
1250 /*
1251 * Get the full path name before the open, because this is
1252 * not possible after the open on the Amiga.
1253 * fname cannot be NameBuff, because it must have been allocated.
1254 */
1255 mf_set_ffname(mfp);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001256#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001258 * A ":!cd e:xxx" may change the directory without us knowing, use the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 * full pathname always. Careful: This frees fname!
1260 */
1261 mf_fullname(mfp);
1262#endif
1263
1264#ifdef HAVE_LSTAT
1265 /*
1266 * Extra security check: When creating a swap file it really shouldn't
1267 * exist yet. If there is a symbolic link, this is most likely an attack.
1268 */
1269 if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0)
1270 {
1271 mfp->mf_fd = -1;
Bram Moolenaareaaac012022-01-02 17:00:40 +00001272 emsg(_(e_swap_file_already_exists_symlink_attack));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274 else
1275#endif
1276 {
1277 /*
1278 * try to open the file
1279 */
Bram Moolenaara5792f52005-11-23 21:25:05 +00001280 flags |= O_EXTRA | O_NOFOLLOW;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001281#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001282 // Prevent handle inheritance that cause problems with Cscope
1283 // (swap file may not be deleted if cscope connection was open after
1284 // the file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 flags |= O_NOINHERIT;
1286#endif
Bram Moolenaarb58a4b92019-05-27 23:36:21 +02001287 mfp->mf_flags = flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags);
1289 }
1290
1291 /*
1292 * If the file cannot be opened, use memory only
1293 */
1294 if (mfp->mf_fd < 0)
1295 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001296 VIM_CLEAR(mfp->mf_fname);
1297 VIM_CLEAR(mfp->mf_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299 else
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00001300 {
Bram Moolenaarf05da212009-11-17 16:13:15 +00001301#ifdef HAVE_FD_CLOEXEC
1302 int fdflags = fcntl(mfp->mf_fd, F_GETFD);
1303 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01001304 (void)fcntl(mfp->mf_fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00001305#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02001306#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00001307 mch_copy_sec(fname, mfp->mf_fname);
1308#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001309 mch_hide(mfp->mf_fname); // try setting the 'hidden' flag
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00001310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001312
1313/*
1314 * Implementation of mf_hashtab_T follows.
1315 */
1316
1317/*
1318 * The number of buckets in the hashtable is increased by a factor of
1319 * MHT_GROWTH_FACTOR when the average number of items per bucket
1320 * exceeds 2 ^ MHT_LOG_LOAD_FACTOR.
1321 */
1322#define MHT_LOG_LOAD_FACTOR 6
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001323#define MHT_GROWTH_FACTOR 2 // must be a power of two
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001324
1325/*
1326 * Initialize an empty hash table.
1327 */
1328 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001329mf_hash_init(mf_hashtab_T *mht)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001330{
Bram Moolenaara80faa82020-04-12 19:37:17 +02001331 CLEAR_POINTER(mht);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001332 mht->mht_buckets = mht->mht_small_buckets;
1333 mht->mht_mask = MHT_INIT_SIZE - 1;
1334}
1335
1336/*
1337 * Free the array of a hash table. Does not free the items it contains!
1338 * The hash table must not be used again without another mf_hash_init() call.
1339 */
1340 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001341mf_hash_free(mf_hashtab_T *mht)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001342{
1343 if (mht->mht_buckets != mht->mht_small_buckets)
1344 vim_free(mht->mht_buckets);
1345}
1346
1347/*
1348 * Free the array of a hash table and all the items it contains.
1349 */
1350 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001351mf_hash_free_all(mf_hashtab_T *mht)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001352{
1353 long_u idx;
1354 mf_hashitem_T *mhi;
1355 mf_hashitem_T *next;
1356
1357 for (idx = 0; idx <= mht->mht_mask; idx++)
1358 for (mhi = mht->mht_buckets[idx]; mhi != NULL; mhi = next)
1359 {
1360 next = mhi->mhi_next;
1361 vim_free(mhi);
1362 }
1363
1364 mf_hash_free(mht);
1365}
1366
1367/*
1368 * Find "key" in hashtable "mht".
1369 * Returns a pointer to a mf_hashitem_T or NULL if the item was not found.
1370 */
1371 static mf_hashitem_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001372mf_hash_find(mf_hashtab_T *mht, blocknr_T key)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001373{
1374 mf_hashitem_T *mhi;
1375
1376 mhi = mht->mht_buckets[key & mht->mht_mask];
1377 while (mhi != NULL && mhi->mhi_key != key)
1378 mhi = mhi->mhi_next;
1379
1380 return mhi;
1381}
1382
1383/*
1384 * Add item "mhi" to hashtable "mht".
1385 * "mhi" must not be NULL.
1386 */
1387 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001388mf_hash_add_item(mf_hashtab_T *mht, mf_hashitem_T *mhi)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001389{
1390 long_u idx;
1391
1392 idx = mhi->mhi_key & mht->mht_mask;
1393 mhi->mhi_next = mht->mht_buckets[idx];
1394 mhi->mhi_prev = NULL;
1395 if (mhi->mhi_next != NULL)
1396 mhi->mhi_next->mhi_prev = mhi;
1397 mht->mht_buckets[idx] = mhi;
1398
1399 mht->mht_count++;
1400
1401 /*
1402 * Grow hashtable when we have more thank 2^MHT_LOG_LOAD_FACTOR
1403 * items per bucket on average
1404 */
1405 if (mht->mht_fixed == 0
1406 && (mht->mht_count >> MHT_LOG_LOAD_FACTOR) > mht->mht_mask)
1407 {
1408 if (mf_hash_grow(mht) == FAIL)
1409 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001410 // stop trying to grow after first failure to allocate memory
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001411 mht->mht_fixed = 1;
1412 }
1413 }
1414}
1415
1416/*
1417 * Remove item "mhi" from hashtable "mht".
1418 * "mhi" must not be NULL and must have been inserted into "mht".
1419 */
1420 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001421mf_hash_rem_item(mf_hashtab_T *mht, mf_hashitem_T *mhi)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001422{
1423 if (mhi->mhi_prev == NULL)
1424 mht->mht_buckets[mhi->mhi_key & mht->mht_mask] = mhi->mhi_next;
1425 else
1426 mhi->mhi_prev->mhi_next = mhi->mhi_next;
1427
1428 if (mhi->mhi_next != NULL)
1429 mhi->mhi_next->mhi_prev = mhi->mhi_prev;
1430
1431 mht->mht_count--;
1432
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001433 // We could shrink the table here, but it typically takes little memory,
1434 // so why bother?
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001435}
1436
1437/*
1438 * Increase number of buckets in the hashtable by MHT_GROWTH_FACTOR and
1439 * rehash items.
1440 * Returns FAIL when out of memory.
1441 */
1442 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001443mf_hash_grow(mf_hashtab_T *mht)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001444{
1445 long_u i, j;
1446 int shift;
1447 mf_hashitem_T *mhi;
1448 mf_hashitem_T *tails[MHT_GROWTH_FACTOR];
1449 mf_hashitem_T **buckets;
1450 size_t size;
1451
1452 size = (mht->mht_mask + 1) * MHT_GROWTH_FACTOR * sizeof(void *);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001453 buckets = lalloc_clear(size, FALSE);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001454 if (buckets == NULL)
1455 return FAIL;
1456
1457 shift = 0;
1458 while ((mht->mht_mask >> shift) != 0)
1459 shift++;
1460
1461 for (i = 0; i <= mht->mht_mask; i++)
1462 {
1463 /*
1464 * Traverse the items in the i-th original bucket and move them into
1465 * MHT_GROWTH_FACTOR new buckets, preserving their relative order
1466 * within each new bucket. Preserving the order is important because
1467 * mf_get() tries to keep most recently used items at the front of
1468 * each bucket.
1469 *
1470 * Here we strongly rely on the fact the hashes are computed modulo
1471 * a power of two.
1472 */
1473
Bram Moolenaara80faa82020-04-12 19:37:17 +02001474 CLEAR_FIELD(tails);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001475
1476 for (mhi = mht->mht_buckets[i]; mhi != NULL; mhi = mhi->mhi_next)
1477 {
1478 j = (mhi->mhi_key >> shift) & (MHT_GROWTH_FACTOR - 1);
1479 if (tails[j] == NULL)
1480 {
1481 buckets[i + (j << shift)] = mhi;
1482 tails[j] = mhi;
1483 mhi->mhi_prev = NULL;
1484 }
1485 else
1486 {
1487 tails[j]->mhi_next = mhi;
1488 mhi->mhi_prev = tails[j];
1489 tails[j] = mhi;
1490 }
1491 }
1492
1493 for (j = 0; j < MHT_GROWTH_FACTOR; j++)
1494 if (tails[j] != NULL)
1495 tails[j]->mhi_next = NULL;
1496 }
1497
1498 if (mht->mht_buckets != mht->mht_small_buckets)
1499 vim_free(mht->mht_buckets);
1500
1501 mht->mht_buckets = buckets;
1502 mht->mht_mask = (mht->mht_mask + 1) * MHT_GROWTH_FACTOR - 1;
1503
1504 return OK;
1505}