Mercurial > vim
annotate src/memfile.c @ 7875:12cae879b674
Added tag v7.4.1234 for changeset 2313f1a941535b2c284c52983788939654897a57
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Mon, 01 Feb 2016 22:15:05 +0100 |
parents | 41789f16d6b2 |
children | a7e58c6e4e9a |
rev | line source |
---|---|
7 | 1 /* vi:set ts=8 sts=4 sw=4: |
2 * | |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 * See README.txt for an overview of the Vim source code. | |
8 */ | |
9 | |
10 /* | |
11 * memfile.c: Contains the functions for handling blocks of memory which can | |
12 * be stored in a file. This is the implementation of a sort of virtual memory. | |
13 * | |
14 * A memfile consists of a sequence of blocks. The blocks numbered from 0 | |
15 * upwards have been assigned a place in the actual file. The block number | |
16 * is equal to the page number in the file. The | |
17 * blocks with negative numbers are currently in memory only. They can be | |
18 * assigned a place in the file when too much memory is being used. At that | |
19 * moment they get a new, positive, number. A list is used for translation of | |
20 * negative to positive numbers. | |
21 * | |
22 * The size of a block is a multiple of a page size, normally the page size of | |
23 * the device the file is on. Most blocks are 1 page long. A Block of multiple | |
24 * pages is used for a line that does not fit in a single page. | |
25 * | |
26 * Each block can be in memory and/or in a file. The block stays in memory | |
27 * as long as it is locked. If it is no longer locked it can be swapped out to | |
28 * the file. It is only written to the file if it has been changed. | |
29 * | |
30 * Under normal operation the file is created when opening the memory file and | |
31 * deleted when closing the memory file. Only with recovery an existing memory | |
32 * file is opened. | |
33 */ | |
34 | |
35 #include "vim.h" | |
36 | |
37 /* | |
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 | |
60 extern 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 | |
70 static long_u total_mem_used = 0; /* total memory used for memfiles */ | |
71 | |
7803
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
72 static void mf_ins_hash(memfile_T *, bhdr_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
73 static void mf_rem_hash(memfile_T *, bhdr_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
74 static bhdr_T *mf_find_hash(memfile_T *, blocknr_T); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
75 static void mf_ins_used(memfile_T *, bhdr_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
76 static void mf_rem_used(memfile_T *, bhdr_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
77 static bhdr_T *mf_release(memfile_T *, int); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
78 static bhdr_T *mf_alloc_bhdr(memfile_T *, int); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
79 static void mf_free_bhdr(bhdr_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
80 static void mf_ins_free(memfile_T *, bhdr_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
81 static bhdr_T *mf_rem_free(memfile_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
82 static int mf_read(memfile_T *, bhdr_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
83 static int mf_write(memfile_T *, bhdr_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
84 static int mf_write_block(memfile_T *mfp, bhdr_T *hp, off_t offset, unsigned size); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
85 static int mf_trans_add(memfile_T *, bhdr_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
86 static void mf_do_open(memfile_T *, char_u *, int); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
87 static void mf_hash_init(mf_hashtab_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
88 static void mf_hash_free(mf_hashtab_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
89 static void mf_hash_free_all(mf_hashtab_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
90 static mf_hashitem_T *mf_hash_find(mf_hashtab_T *, blocknr_T); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
91 static void mf_hash_add_item(mf_hashtab_T *, mf_hashitem_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
92 static void mf_hash_rem_item(mf_hashtab_T *, mf_hashitem_T *); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7347
diff
changeset
|
93 static int mf_hash_grow(mf_hashtab_T *); |
7 | 94 |
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 * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
124 mf_open(char_u *fname, int flags) |
7 | 125 { |
126 memfile_T *mfp; | |
127 off_t size; | |
4519
fba9c0f86f77
updated for version 7.3.1007
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
128 #if defined(STATFS) && defined(UNIX) && !defined(__QNX__) && !defined(__minix) |
7 | 129 # 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; | |
2730 | 159 mf_hash_init(&mfp->mf_hash); |
160 mf_hash_init(&mfp->mf_trans); | |
7 | 161 mfp->mf_page_size = MEMFILE_PAGE_SIZE; |
2267 | 162 #ifdef FEAT_CRYPT |
163 mfp->mf_old_key = NULL; | |
164 #endif | |
7 | 165 |
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; | |
1230 | 190 |
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 } | |
7 | 209 |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
224 mf_open_file(memfile_T *mfp, char_u *fname) |
7 | 225 { |
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 /* | |
2249
6d3d35ff2c2b
Use full path in undofile(). Updated docs.
Bram Moolenaar <bram@vim.org>
parents:
2003
diff
changeset
|
236 * Close a memory file and delete the associated file if 'del_file' is TRUE. |
7 | 237 */ |
238 void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
239 mf_close(memfile_T *mfp, int del_file) |
7 | 240 { |
241 bhdr_T *hp, *nextp; | |
242 | |
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)); | |
2730 | 261 mf_hash_free(&mfp->mf_hash); |
262 mf_hash_free_all(&mfp->mf_trans); /* free hashtable and its items */ | |
7 | 263 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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
272 mf_close_file( |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
273 buf_T *buf, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
274 int getlines) /* get all lines into memory? */ |
7 | 275 { |
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!) */ | |
1066 | 286 mf_dont_release = TRUE; |
7 | 287 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum) |
288 (void)ml_get_buf(buf, lnum, FALSE); | |
1066 | 289 mf_dont_release = FALSE; |
7 | 290 /* 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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
312 mf_new_page_size(memfile_T *mfp, unsigned new_size) |
7 | 313 { |
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 * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
326 mf_new(memfile_T *mfp, int negative, int page_count) |
7 | 327 { |
1166 | 328 bhdr_T *hp; /* new bhdr_T */ |
329 bhdr_T *freep; /* first block in free list */ | |
330 char_u *p; | |
7 | 331 |
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 * | |
1166 | 351 * 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 | |
7 | 353 * |
1166 | 354 * If the number of pages matches and mf_release() returned a bhdr_T, |
7 | 355 * 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 */ | |
2249
6d3d35ff2c2b
Use full path in undofile(). Updated docs.
Bram Moolenaar <bram@vim.org>
parents:
2003
diff
changeset
|
404 (void)vim_memset((char *)(hp->bh_data), 0, |
6d3d35ff2c2b
Use full path in undofile(). Updated docs.
Bram Moolenaar <bram@vim.org>
parents:
2003
diff
changeset
|
405 (size_t)mfp->mf_page_size * page_count); |
7 | 406 |
407 return hp; | |
408 } | |
409 | |
410 /* | |
2267 | 411 * Get existing block "nr" with "page_count" pages. |
7 | 412 * |
413 * Note: The caller should first check a negative nr with mf_trans_del() | |
414 */ | |
415 bhdr_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
416 mf_get(memfile_T *mfp, blocknr_T nr, int page_count) |
7 | 417 { |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
474 mf_put( |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
475 memfile_T *mfp, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
476 bhdr_T *hp, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
477 int dirty, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
478 int infile) |
7 | 479 { |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
501 mf_free(memfile_T *mfp, bhdr_T *hp) |
7 | 502 { |
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 | |
984 | 515 #if defined(__MORPHOS__) && defined(__libnix__) |
7 | 516 /* function is missing in MorphOS libnix version */ |
517 extern unsigned long *__stdfiledes; | |
518 | |
519 static unsigned long | |
520 fdtofh(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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
540 mf_sync(memfile_T *mfp, int flags) |
7 | 541 { |
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__) | |
4352 | 613 error "Don't use fsync with EMX! Read emxdoc.doc or emxfix01.doc for info." |
7 | 614 # 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() */ | |
6637 | 624 /* No sync() on Stratus VOS */ |
625 # if defined(__OPENNT) || defined(__TANDEM) || defined(__VOS__) | |
7 | 626 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 | |
1030 | 652 # if defined(__AROS__) || defined(__amigaos4__) |
7 | 653 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 { | |
984 | 674 # if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__) |
7 | 675 /* Have function (in libnix at least), |
676 * but ain't got no prototype anywhere. */ | |
677 extern unsigned long fdtofh(int filedescriptor); | |
678 # endif | |
984 | 679 # if !defined(__libnix__) |
680 fflush(NULL); | |
681 # else | |
7 | 682 BPTR fh = (BPTR)fdtofh(mfp->mf_fd); |
683 | |
684 if (fh != 0) | |
685 Flush(fh); | |
984 | 686 # endif |
7 | 687 } |
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 /* | |
630 | 702 * 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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
707 mf_set_dirty(memfile_T *mfp) |
630 | 708 { |
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 /* | |
7 | 718 * insert block *hp in front of hashlist of memfile *mfp |
719 */ | |
720 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
721 mf_ins_hash(memfile_T *mfp, bhdr_T *hp) |
7 | 722 { |
2730 | 723 mf_hash_add_item(&mfp->mf_hash, (mf_hashitem_T *)hp); |
7 | 724 } |
725 | |
726 /* | |
727 * remove block *hp from hashlist of memfile list *mfp | |
728 */ | |
729 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
730 mf_rem_hash(memfile_T *mfp, bhdr_T *hp) |
7 | 731 { |
2730 | 732 mf_hash_rem_item(&mfp->mf_hash, (mf_hashitem_T *)hp); |
7 | 733 } |
734 | |
735 /* | |
736 * look in hash lists of memfile *mfp for block header with number 'nr' | |
737 */ | |
738 static bhdr_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
739 mf_find_hash(memfile_T *mfp, blocknr_T nr) |
7 | 740 { |
2730 | 741 return (bhdr_T *)mf_hash_find(&mfp->mf_hash, nr); |
7 | 742 } |
743 | |
744 /* | |
745 * insert block *hp in front of used list of memfile *mfp | |
746 */ | |
747 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
748 mf_ins_used(memfile_T *mfp, bhdr_T *hp) |
7 | 749 { |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
765 mf_rem_used(memfile_T *mfp, bhdr_T *hp) |
7 | 766 { |
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. | |
6817 | 785 * |
786 * Returns NULL if no block is released. | |
7 | 787 */ |
788 static bhdr_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
789 mf_release(memfile_T *mfp, int page_count) |
7 | 790 { |
791 bhdr_T *hp; | |
792 int need_release; | |
793 buf_T *buf; | |
794 | |
795 /* don't release while in mf_close_file() */ | |
1066 | 796 if (mf_dont_release) |
7 | 797 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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
871 mf_release_all(void) |
7 | 872 { |
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 * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
915 mf_alloc_bhdr(memfile_T *mfp, int page_count) |
7 | 916 { |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
936 mf_free_bhdr(bhdr_T *hp) |
7 | 937 { |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
946 mf_ins_free(memfile_T *mfp, bhdr_T *hp) |
7 | 947 { |
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 * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
957 mf_rem_free(memfile_T *mfp) |
7 | 958 { |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
972 mf_read(memfile_T *mfp, bhdr_T *hp) |
7 | 973 { |
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 { | |
1033 | 986 PERROR(_("E294: Seek error in swap file read")); |
7 | 987 return FAIL; |
988 } | |
2664 | 989 if ((unsigned)read_eintr(mfp->mf_fd, hp->bh_data, size) != size) |
7 | 990 { |
1033 | 991 PERROR(_("E295: Read error in swap file")); |
7 | 992 return FAIL; |
993 } | |
2267 | 994 |
995 #ifdef FEAT_CRYPT | |
7347
8977b399cf55
commit https://github.com/vim/vim/commit/4a8c2cfc56b9affc36934aa0f20d8cfd2b1511c8
Christian Brabandt <cb@256bit.org>
parents:
6817
diff
changeset
|
996 /* Decrypt if 'key' is set and this is a data block. And when changing the |
8977b399cf55
commit https://github.com/vim/vim/commit/4a8c2cfc56b9affc36934aa0f20d8cfd2b1511c8
Christian Brabandt <cb@256bit.org>
parents:
6817
diff
changeset
|
997 * key. */ |
8977b399cf55
commit https://github.com/vim/vim/commit/4a8c2cfc56b9affc36934aa0f20d8cfd2b1511c8
Christian Brabandt <cb@256bit.org>
parents:
6817
diff
changeset
|
998 if (*mfp->mf_buffer->b_p_key != NUL || mfp->mf_old_key != NULL) |
2267 | 999 ml_decrypt_data(mfp, hp->bh_data, offset, size); |
1000 #endif | |
1001 | |
7 | 1002 return OK; |
1003 } | |
1004 | |
1005 /* | |
1006 * write a block to disk | |
1007 * | |
1008 * Return FAIL for failure, OK otherwise | |
1009 */ | |
1010 static int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1011 mf_write(memfile_T *mfp, bhdr_T *hp) |
7 | 1012 { |
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; | |
4352 | 1041 hp2 = mf_find_hash(mfp, nr); /* NULL caught below */ |
7 | 1042 } |
1043 else | |
1044 hp2 = hp; | |
1045 | |
1046 offset = (off_t)page_size * nr; | |
1047 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset) | |
1048 { | |
1033 | 1049 PERROR(_("E296: Seek error in swap file write")); |
7 | 1050 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; | |
2267 | 1057 if (mf_write_block(mfp, hp2 == NULL ? hp : hp2, offset, size) == FAIL) |
7 | 1058 { |
1059 /* | |
1060 * Avoid repeating the error message, this mostly happens when the | |
1226 | 1061 * disk is full. We give the message again only after a successful |
7 | 1062 * 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 /* | |
2267 | 1083 * 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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1088 mf_write_block( |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1089 memfile_T *mfp, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1090 bhdr_T *hp, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1091 off_t offset UNUSED, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1092 unsigned size) |
2267 | 1093 { |
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 | |
2664 | 1107 if ((unsigned)write_eintr(mfp->mf_fd, data, size) != size) |
2267 | 1108 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 /* | |
7 | 1119 * 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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1124 mf_trans_add(memfile_T *mfp, bhdr_T *hp) |
7 | 1125 { |
1126 bhdr_T *freep; | |
1127 blocknr_T new_bnum; | |
1128 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 /* | |
2267 | 1138 * Get a new number for the block. |
7 | 1139 * 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 /* | |
4352 | 1148 * If the page count of the free block was larger, reduce it. |
7 | 1149 * 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 | |
2730 | 1175 /* Insert "np" into "mf_trans" hashtable with key "np->nt_old_bnum" */ |
1176 mf_hash_add_item(&mfp->mf_trans, (mf_hashitem_T *)np); | |
7 | 1177 |
1178 return OK; | |
1179 } | |
1180 | |
1181 /* | |
6817 | 1182 * Lookup a translation from the trans lists and delete the entry. |
7 | 1183 * |
1184 * Return the positive new number when found, the old number when not found | |
1185 */ | |
1186 blocknr_T | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1187 mf_trans_del(memfile_T *mfp, blocknr_T old_nr) |
7 | 1188 { |
1189 NR_TRANS *np; | |
1190 blocknr_T new_bnum; | |
1191 | |
2730 | 1192 np = (NR_TRANS *)mf_hash_find(&mfp->mf_trans, old_nr); |
1193 | |
7 | 1194 if (np == NULL) /* not found */ |
1195 return old_nr; | |
1196 | |
1197 mfp->mf_neg_count--; | |
1198 new_bnum = np->nt_new_bnum; | |
2730 | 1199 |
1200 /* remove entry from the trans list */ | |
1201 mf_hash_rem_item(&mfp->mf_trans, (mf_hashitem_T *)np); | |
1202 | |
7 | 1203 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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1214 mf_set_ffname(memfile_T *mfp) |
7 | 1215 { |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1224 mf_fullname(memfile_T *mfp) |
7 | 1225 { |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1238 mf_need_trans(memfile_T *mfp) |
7 | 1239 { |
1240 return (mfp->mf_fname != NULL && mfp->mf_neg_count > 0); | |
1241 } | |
1242 | |
1243 /* | |
1244 * Open a swap file for a memfile. | |
1245 * The "fname" must be in allocated memory, and is consumed (also when an | |
1246 * error occurs). | |
1247 */ | |
1248 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1249 mf_do_open( |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1250 memfile_T *mfp, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1251 char_u *fname, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1252 int flags) /* flags for open() */ |
7 | 1253 { |
1254 #ifdef HAVE_LSTAT | |
1255 struct stat sb; | |
1256 #endif | |
1257 | |
1258 mfp->mf_fname = fname; | |
1259 | |
1260 /* | |
1261 * Get the full path name before the open, because this is | |
1262 * not possible after the open on the Amiga. | |
1263 * fname cannot be NameBuff, because it must have been allocated. | |
1264 */ | |
1265 mf_set_ffname(mfp); | |
2823 | 1266 #if defined(MSDOS) || defined(MSWIN) |
7 | 1267 /* |
4352 | 1268 * A ":!cd e:xxx" may change the directory without us knowing, use the |
7 | 1269 * full pathname always. Careful: This frees fname! |
1270 */ | |
1271 mf_fullname(mfp); | |
1272 #endif | |
1273 | |
1274 #ifdef HAVE_LSTAT | |
1275 /* | |
1276 * Extra security check: When creating a swap file it really shouldn't | |
1277 * exist yet. If there is a symbolic link, this is most likely an attack. | |
1278 */ | |
1279 if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0) | |
1280 { | |
1281 mfp->mf_fd = -1; | |
1282 EMSG(_("E300: Swap file already exists (symlink attack?)")); | |
1283 } | |
1284 else | |
1285 #endif | |
1286 { | |
1287 /* | |
1288 * try to open the file | |
1289 */ | |
557 | 1290 flags |= O_EXTRA | O_NOFOLLOW; |
7 | 1291 #ifdef WIN32 |
1292 /* Prevent handle inheritance that cause problems with Cscope | |
1293 * (swap file may not be deleted if cscope connection was open after | |
1294 * the file) */ | |
1295 flags |= O_NOINHERIT; | |
1296 #endif | |
1297 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags); | |
1298 } | |
1299 | |
1300 /* | |
1301 * If the file cannot be opened, use memory only | |
1302 */ | |
1303 if (mfp->mf_fd < 0) | |
1304 { | |
1305 vim_free(mfp->mf_fname); | |
1306 vim_free(mfp->mf_ffname); | |
1307 mfp->mf_fname = NULL; | |
1308 mfp->mf_ffname = NULL; | |
1309 } | |
1310 else | |
1583 | 1311 { |
2003 | 1312 #ifdef HAVE_FD_CLOEXEC |
1313 int fdflags = fcntl(mfp->mf_fd, F_GETFD); | |
1314 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) | |
1315 fcntl(mfp->mf_fd, F_SETFD, fdflags | FD_CLOEXEC); | |
1316 #endif | |
5788 | 1317 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 1318 mch_copy_sec(fname, mfp->mf_fname); |
1319 #endif | |
7 | 1320 mch_hide(mfp->mf_fname); /* try setting the 'hidden' flag */ |
1583 | 1321 } |
7 | 1322 } |
2730 | 1323 |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1340 mf_hash_init(mf_hashtab_T *mht) |
2730 | 1341 { |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1352 mf_hash_free(mf_hashtab_T *mht) |
2730 | 1353 { |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1362 mf_hash_free_all(mf_hashtab_T *mht) |
2730 | 1363 { |
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 * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1383 mf_hash_find(mf_hashtab_T *mht, blocknr_T key) |
2730 | 1384 { |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1399 mf_hash_add_item(mf_hashtab_T *mht, mf_hashitem_T *mhi) |
2730 | 1400 { |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1432 mf_hash_rem_item(mf_hashtab_T *mht, mf_hashitem_T *mhi) |
2730 | 1433 { |
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 | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1454 mf_hash_grow(mf_hashtab_T *mht) |
2730 | 1455 { |
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 } |