Mercurial > vim
annotate src/memfile.c @ 13399:ad9ed174d094
Added tag v8.0.1573 for changeset aef75fbfc07dcc1ef4c80a84defa64e9192fd478
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Mon, 05 Mar 2018 22:45:06 +0100 |
parents | ac42c4b11dbc |
children | aa9c7588d30a |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
7 | 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 *); |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
84 static int mf_write_block(memfile_T *mfp, bhdr_T *hp, off_T offset, unsigned size); |
7803
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; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
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)) | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
182 || (size = vim_lseek(mfp->mf_fd, (off_T)0L, SEEK_END)) <= 0) |
7 | 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 */ | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
10359
diff
changeset
|
300 VIM_CLEAR(mfp->mf_fname); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
10359
diff
changeset
|
301 VIM_CLEAR(mfp->mf_ffname); |
7 | 302 } |
303 } | |
304 | |
305 /* | |
306 * Set new size for a memfile. Used when block 0 of a swapfile has been read | |
307 * and the size it indicates differs from what was guessed. | |
308 */ | |
309 void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
310 mf_new_page_size(memfile_T *mfp, unsigned new_size) |
7 | 311 { |
312 /* Correct the memory used for block 0 to the new size, because it will be | |
313 * freed with that size later on. */ | |
314 total_mem_used += new_size - mfp->mf_page_size; | |
315 mfp->mf_page_size = new_size; | |
316 } | |
317 | |
318 /* | |
319 * get a new block | |
320 * | |
321 * negative: TRUE if negative block number desired (data block) | |
322 */ | |
323 bhdr_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
324 mf_new(memfile_T *mfp, int negative, int page_count) |
7 | 325 { |
1166 | 326 bhdr_T *hp; /* new bhdr_T */ |
327 bhdr_T *freep; /* first block in free list */ | |
328 char_u *p; | |
7 | 329 |
330 /* | |
331 * If we reached the maximum size for the used memory blocks, release one | |
332 * If a bhdr_T is returned, use it and adjust the page_count if necessary. | |
333 */ | |
334 hp = mf_release(mfp, page_count); | |
335 | |
336 /* | |
337 * Decide on the number to use: | |
338 * If there is a free block, use its number. | |
339 * Otherwise use mf_block_min for a negative number, mf_block_max for | |
340 * a positive number. | |
341 */ | |
342 freep = mfp->mf_free_first; | |
343 if (!negative && freep != NULL && freep->bh_page_count >= page_count) | |
344 { | |
345 /* | |
346 * If the block in the free list has more pages, take only the number | |
347 * of pages needed and allocate a new bhdr_T with data | |
348 * | |
1166 | 349 * If the number of pages matches and mf_release() did not return a |
350 * bhdr_T, use the bhdr_T from the free list and allocate the data | |
7 | 351 * |
1166 | 352 * If the number of pages matches and mf_release() returned a bhdr_T, |
7 | 353 * just use the number and free the bhdr_T from the free list |
354 */ | |
355 if (freep->bh_page_count > page_count) | |
356 { | |
357 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL) | |
358 return NULL; | |
359 hp->bh_bnum = freep->bh_bnum; | |
360 freep->bh_bnum += page_count; | |
361 freep->bh_page_count -= page_count; | |
362 } | |
363 else if (hp == NULL) /* need to allocate memory for this block */ | |
364 { | |
365 if ((p = (char_u *)alloc(mfp->mf_page_size * page_count)) == NULL) | |
366 return NULL; | |
367 hp = mf_rem_free(mfp); | |
368 hp->bh_data = p; | |
369 } | |
370 else /* use the number, remove entry from free list */ | |
371 { | |
372 freep = mf_rem_free(mfp); | |
373 hp->bh_bnum = freep->bh_bnum; | |
374 vim_free(freep); | |
375 } | |
376 } | |
377 else /* get a new number */ | |
378 { | |
379 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL) | |
380 return NULL; | |
381 if (negative) | |
382 { | |
383 hp->bh_bnum = mfp->mf_blocknr_min--; | |
384 mfp->mf_neg_count++; | |
385 } | |
386 else | |
387 { | |
388 hp->bh_bnum = mfp->mf_blocknr_max; | |
389 mfp->mf_blocknr_max += page_count; | |
390 } | |
391 } | |
392 hp->bh_flags = BH_LOCKED | BH_DIRTY; /* new block is always dirty */ | |
393 mfp->mf_dirty = TRUE; | |
394 hp->bh_page_count = page_count; | |
395 mf_ins_used(mfp, hp); | |
396 mf_ins_hash(mfp, hp); | |
397 | |
398 /* | |
399 * Init the data to all zero, to avoid reading uninitialized data. | |
400 * This also avoids that the passwd file ends up in the swap file! | |
401 */ | |
2249
6d3d35ff2c2b
Use full path in undofile(). Updated docs.
Bram Moolenaar <bram@vim.org>
parents:
2003
diff
changeset
|
402 (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
|
403 (size_t)mfp->mf_page_size * page_count); |
7 | 404 |
405 return hp; | |
406 } | |
407 | |
408 /* | |
2267 | 409 * Get existing block "nr" with "page_count" pages. |
7 | 410 * |
411 * Note: The caller should first check a negative nr with mf_trans_del() | |
412 */ | |
413 bhdr_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
414 mf_get(memfile_T *mfp, blocknr_T nr, int page_count) |
7 | 415 { |
416 bhdr_T *hp; | |
417 /* doesn't exist */ | |
418 if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min) | |
419 return NULL; | |
420 | |
421 /* | |
422 * see if it is in the cache | |
423 */ | |
424 hp = mf_find_hash(mfp, nr); | |
425 if (hp == NULL) /* not in the hash list */ | |
426 { | |
427 if (nr < 0 || nr >= mfp->mf_infile_count) /* can't be in the file */ | |
428 return NULL; | |
429 | |
430 /* could check here if the block is in the free list */ | |
431 | |
432 /* | |
433 * Check if we need to flush an existing block. | |
434 * If so, use that block. | |
435 * If not, allocate a new block. | |
436 */ | |
437 hp = mf_release(mfp, page_count); | |
438 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL) | |
439 return NULL; | |
440 | |
441 hp->bh_bnum = nr; | |
442 hp->bh_flags = 0; | |
443 hp->bh_page_count = page_count; | |
444 if (mf_read(mfp, hp) == FAIL) /* cannot read the block! */ | |
445 { | |
446 mf_free_bhdr(hp); | |
447 return NULL; | |
448 } | |
449 } | |
450 else | |
451 { | |
452 mf_rem_used(mfp, hp); /* remove from list, insert in front below */ | |
453 mf_rem_hash(mfp, hp); | |
454 } | |
455 | |
456 hp->bh_flags |= BH_LOCKED; | |
457 mf_ins_used(mfp, hp); /* put in front of used list */ | |
458 mf_ins_hash(mfp, hp); /* put in front of hash list */ | |
459 | |
460 return hp; | |
461 } | |
462 | |
463 /* | |
464 * release the block *hp | |
465 * | |
466 * dirty: Block must be written to file later | |
467 * infile: Block should be in file (needed for recovery) | |
468 * | |
469 * no return value, function cannot fail | |
470 */ | |
471 void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
472 mf_put( |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
473 memfile_T *mfp, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
474 bhdr_T *hp, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
475 int dirty, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
476 int infile) |
7 | 477 { |
478 int flags; | |
479 | |
480 flags = hp->bh_flags; | |
481 | |
482 if ((flags & BH_LOCKED) == 0) | |
10359
66f1b5bf3fa6
commit https://github.com/vim/vim/commit/95f096030ed1a8afea028f2ea295d6f6a70f466f
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
483 IEMSG(_("E293: block was not locked")); |
7 | 484 flags &= ~BH_LOCKED; |
485 if (dirty) | |
486 { | |
487 flags |= BH_DIRTY; | |
488 mfp->mf_dirty = TRUE; | |
489 } | |
490 hp->bh_flags = flags; | |
491 if (infile) | |
492 mf_trans_add(mfp, hp); /* may translate negative in positive nr */ | |
493 } | |
494 | |
495 /* | |
496 * block *hp is no longer in used, may put it in the free list of memfile *mfp | |
497 */ | |
498 void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
499 mf_free(memfile_T *mfp, bhdr_T *hp) |
7 | 500 { |
501 vim_free(hp->bh_data); /* free the memory */ | |
502 mf_rem_hash(mfp, hp); /* get *hp out of the hash list */ | |
503 mf_rem_used(mfp, hp); /* get *hp out of the used list */ | |
504 if (hp->bh_bnum < 0) | |
505 { | |
506 vim_free(hp); /* don't want negative numbers in free list */ | |
507 mfp->mf_neg_count--; | |
508 } | |
509 else | |
510 mf_ins_free(mfp, hp); /* put *hp in the free list */ | |
511 } | |
512 | |
984 | 513 #if defined(__MORPHOS__) && defined(__libnix__) |
7 | 514 /* function is missing in MorphOS libnix version */ |
515 extern unsigned long *__stdfiledes; | |
516 | |
517 static unsigned long | |
518 fdtofh(int filedescriptor) | |
519 { | |
520 return __stdfiledes[filedescriptor]; | |
521 } | |
522 #endif | |
523 | |
524 /* | |
525 * Sync the memory file *mfp to disk. | |
526 * Flags: | |
527 * MFS_ALL If not given, blocks with negative numbers are not synced, | |
528 * even when they are dirty! | |
529 * MFS_STOP Stop syncing when a character becomes available, but sync at | |
530 * least one block. | |
531 * MFS_FLUSH Make sure buffers are flushed to disk, so they will survive a | |
532 * system crash. | |
533 * MFS_ZERO Only write block 0. | |
534 * | |
535 * Return FAIL for failure, OK otherwise | |
536 */ | |
537 int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
538 mf_sync(memfile_T *mfp, int flags) |
7 | 539 { |
540 int status; | |
541 bhdr_T *hp; | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
7961
diff
changeset
|
542 #if defined(SYNC_DUP_CLOSE) |
7 | 543 int fd; |
544 #endif | |
545 int got_int_save = got_int; | |
546 | |
547 if (mfp->mf_fd < 0) /* there is no file, nothing to do */ | |
548 { | |
549 mfp->mf_dirty = FALSE; | |
550 return FAIL; | |
551 } | |
552 | |
553 /* Only a CTRL-C while writing will break us here, not one typed | |
554 * previously. */ | |
555 got_int = FALSE; | |
556 | |
557 /* | |
558 * sync from last to first (may reduce the probability of an inconsistent | |
559 * file) If a write fails, it is very likely caused by a full filesystem. | |
560 * Then we only try to write blocks within the existing file. If that also | |
561 * fails then we give up. | |
562 */ | |
563 status = OK; | |
564 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev) | |
565 if (((flags & MFS_ALL) || hp->bh_bnum >= 0) | |
566 && (hp->bh_flags & BH_DIRTY) | |
567 && (status == OK || (hp->bh_bnum >= 0 | |
568 && hp->bh_bnum < mfp->mf_infile_count))) | |
569 { | |
570 if ((flags & MFS_ZERO) && hp->bh_bnum != 0) | |
571 continue; | |
572 if (mf_write(mfp, hp) == FAIL) | |
573 { | |
574 if (status == FAIL) /* double error: quit syncing */ | |
575 break; | |
576 status = FAIL; | |
577 } | |
578 if (flags & MFS_STOP) | |
579 { | |
580 /* Stop when char available now. */ | |
581 if (ui_char_avail()) | |
582 break; | |
583 } | |
584 else | |
585 ui_breakcheck(); | |
586 if (got_int) | |
587 break; | |
588 } | |
589 | |
590 /* | |
591 * If the whole list is flushed, the memfile is not dirty anymore. | |
592 * In case of an error this flag is also set, to avoid trying all the time. | |
593 */ | |
594 if (hp == NULL || status == FAIL) | |
595 mfp->mf_dirty = FALSE; | |
596 | |
597 if ((flags & MFS_FLUSH) && *p_sws != NUL) | |
598 { | |
599 #if defined(UNIX) | |
600 # ifdef HAVE_FSYNC | |
601 /* | |
602 * most Unixes have the very useful fsync() function, just what we need. | |
603 */ | |
604 if (STRCMP(p_sws, "fsync") == 0) | |
605 { | |
606 if (fsync(mfp->mf_fd)) | |
607 status = FAIL; | |
608 } | |
609 else | |
610 # endif | |
611 /* OpenNT is strictly POSIX (Benzinger) */ | |
612 /* Tandem/Himalaya NSK-OSS doesn't have sync() */ | |
6637 | 613 /* No sync() on Stratus VOS */ |
614 # if defined(__OPENNT) || defined(__TANDEM) || defined(__VOS__) | |
7 | 615 fflush(NULL); |
616 # else | |
617 sync(); | |
618 # endif | |
619 #endif | |
620 #ifdef VMS | |
621 if (STRCMP(p_sws, "fsync") == 0) | |
622 { | |
623 if (fsync(mfp->mf_fd)) | |
624 status = FAIL; | |
625 } | |
626 #endif | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
7961
diff
changeset
|
627 #ifdef SYNC_DUP_CLOSE |
7 | 628 /* |
629 * Win32 is a bit more work: Duplicate the file handle and close it. | |
630 * This should flush the file to disk. | |
631 */ | |
632 if ((fd = dup(mfp->mf_fd)) >= 0) | |
633 close(fd); | |
634 #endif | |
635 #ifdef AMIGA | |
1030 | 636 # if defined(__AROS__) || defined(__amigaos4__) |
7 | 637 if (fsync(mfp->mf_fd) != 0) |
638 status = FAIL; | |
639 # else | |
640 /* | |
641 * Flush() only exists for AmigaDos 2.0. | |
642 * For 1.3 it should be done with close() + open(), but then the risk | |
643 * is that the open() may fail and lose the file.... | |
644 */ | |
645 # ifdef FEAT_ARP | |
646 if (dos2) | |
647 # endif | |
648 # ifdef SASC | |
649 { | |
650 struct UFB *fp = chkufb(mfp->mf_fd); | |
651 | |
652 if (fp != NULL) | |
653 Flush(fp->ufbfh); | |
654 } | |
655 # else | |
656 # if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__) | |
657 { | |
984 | 658 # if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__) |
7 | 659 /* Have function (in libnix at least), |
660 * but ain't got no prototype anywhere. */ | |
661 extern unsigned long fdtofh(int filedescriptor); | |
662 # endif | |
984 | 663 # if !defined(__libnix__) |
664 fflush(NULL); | |
665 # else | |
7 | 666 BPTR fh = (BPTR)fdtofh(mfp->mf_fd); |
667 | |
668 if (fh != 0) | |
669 Flush(fh); | |
984 | 670 # endif |
7 | 671 } |
672 # else /* assume Manx */ | |
673 Flush(_devtab[mfp->mf_fd].fd); | |
674 # endif | |
675 # endif | |
676 # endif | |
677 #endif /* AMIGA */ | |
678 } | |
679 | |
680 got_int |= got_int_save; | |
681 | |
682 return status; | |
683 } | |
684 | |
685 /* | |
630 | 686 * For all blocks in memory file *mfp that have a positive block number set |
687 * the dirty flag. These are blocks that need to be written to a newly | |
688 * created swapfile. | |
689 */ | |
690 void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
691 mf_set_dirty(memfile_T *mfp) |
630 | 692 { |
693 bhdr_T *hp; | |
694 | |
695 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev) | |
696 if (hp->bh_bnum > 0) | |
697 hp->bh_flags |= BH_DIRTY; | |
698 mfp->mf_dirty = TRUE; | |
699 } | |
700 | |
701 /* | |
7 | 702 * insert block *hp in front of hashlist of memfile *mfp |
703 */ | |
704 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
705 mf_ins_hash(memfile_T *mfp, bhdr_T *hp) |
7 | 706 { |
2730 | 707 mf_hash_add_item(&mfp->mf_hash, (mf_hashitem_T *)hp); |
7 | 708 } |
709 | |
710 /* | |
711 * remove block *hp from hashlist of memfile list *mfp | |
712 */ | |
713 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
714 mf_rem_hash(memfile_T *mfp, bhdr_T *hp) |
7 | 715 { |
2730 | 716 mf_hash_rem_item(&mfp->mf_hash, (mf_hashitem_T *)hp); |
7 | 717 } |
718 | |
719 /* | |
720 * look in hash lists of memfile *mfp for block header with number 'nr' | |
721 */ | |
722 static bhdr_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
723 mf_find_hash(memfile_T *mfp, blocknr_T nr) |
7 | 724 { |
2730 | 725 return (bhdr_T *)mf_hash_find(&mfp->mf_hash, nr); |
7 | 726 } |
727 | |
728 /* | |
729 * insert block *hp in front of used list of memfile *mfp | |
730 */ | |
731 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
732 mf_ins_used(memfile_T *mfp, bhdr_T *hp) |
7 | 733 { |
734 hp->bh_next = mfp->mf_used_first; | |
735 mfp->mf_used_first = hp; | |
736 hp->bh_prev = NULL; | |
737 if (hp->bh_next == NULL) /* list was empty, adjust last pointer */ | |
738 mfp->mf_used_last = hp; | |
739 else | |
740 hp->bh_next->bh_prev = hp; | |
741 mfp->mf_used_count += hp->bh_page_count; | |
742 total_mem_used += hp->bh_page_count * mfp->mf_page_size; | |
743 } | |
744 | |
745 /* | |
746 * remove block *hp from used list of memfile *mfp | |
747 */ | |
748 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
749 mf_rem_used(memfile_T *mfp, bhdr_T *hp) |
7 | 750 { |
751 if (hp->bh_next == NULL) /* last block in used list */ | |
752 mfp->mf_used_last = hp->bh_prev; | |
753 else | |
754 hp->bh_next->bh_prev = hp->bh_prev; | |
755 if (hp->bh_prev == NULL) /* first block in used list */ | |
756 mfp->mf_used_first = hp->bh_next; | |
757 else | |
758 hp->bh_prev->bh_next = hp->bh_next; | |
759 mfp->mf_used_count -= hp->bh_page_count; | |
760 total_mem_used -= hp->bh_page_count * mfp->mf_page_size; | |
761 } | |
762 | |
763 /* | |
764 * Release the least recently used block from the used list if the number | |
765 * of used memory blocks gets to big. | |
766 * | |
767 * Return the block header to the caller, including the memory block, so | |
768 * it can be re-used. Make sure the page_count is right. | |
6817 | 769 * |
770 * Returns NULL if no block is released. | |
7 | 771 */ |
772 static bhdr_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
773 mf_release(memfile_T *mfp, int page_count) |
7 | 774 { |
775 bhdr_T *hp; | |
776 int need_release; | |
777 buf_T *buf; | |
778 | |
779 /* don't release while in mf_close_file() */ | |
1066 | 780 if (mf_dont_release) |
7 | 781 return NULL; |
782 | |
783 /* | |
784 * Need to release a block if the number of blocks for this memfile is | |
785 * higher than the maximum or total memory used is over 'maxmemtot' | |
786 */ | |
787 need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max) | |
788 || (total_mem_used >> 10) >= (long_u)p_mmt); | |
789 | |
790 /* | |
791 * Try to create a swap file if the amount of memory used is getting too | |
792 * high. | |
793 */ | |
794 if (mfp->mf_fd < 0 && need_release && p_uc) | |
795 { | |
796 /* find for which buffer this memfile is */ | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9536
diff
changeset
|
797 FOR_ALL_BUFFERS(buf) |
7 | 798 if (buf->b_ml.ml_mfp == mfp) |
799 break; | |
800 if (buf != NULL && buf->b_may_swap) | |
801 ml_open_file(buf); | |
802 } | |
803 | |
804 /* | |
805 * don't release a block if | |
806 * there is no file for this memfile | |
807 * or | |
808 * the number of blocks for this memfile is lower than the maximum | |
809 * and | |
810 * total memory used is not up to 'maxmemtot' | |
811 */ | |
812 if (mfp->mf_fd < 0 || !need_release) | |
813 return NULL; | |
814 | |
815 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev) | |
816 if (!(hp->bh_flags & BH_LOCKED)) | |
817 break; | |
818 if (hp == NULL) /* not a single one that can be released */ | |
819 return NULL; | |
820 | |
821 /* | |
822 * If the block is dirty, write it. | |
823 * If the write fails we don't free it. | |
824 */ | |
825 if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL) | |
826 return NULL; | |
827 | |
828 mf_rem_used(mfp, hp); | |
829 mf_rem_hash(mfp, hp); | |
830 | |
831 /* | |
832 * If a bhdr_T is returned, make sure that the page_count of bh_data is | |
833 * right | |
834 */ | |
835 if (hp->bh_page_count != page_count) | |
836 { | |
837 vim_free(hp->bh_data); | |
838 if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL) | |
839 { | |
840 vim_free(hp); | |
841 return NULL; | |
842 } | |
843 hp->bh_page_count = page_count; | |
844 } | |
845 return hp; | |
846 } | |
847 | |
848 /* | |
849 * release as many blocks as possible | |
850 * Used in case of out of memory | |
851 * | |
852 * return TRUE if any memory was released | |
853 */ | |
854 int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
855 mf_release_all(void) |
7 | 856 { |
857 buf_T *buf; | |
858 memfile_T *mfp; | |
859 bhdr_T *hp; | |
860 int retval = FALSE; | |
861 | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9536
diff
changeset
|
862 FOR_ALL_BUFFERS(buf) |
7 | 863 { |
864 mfp = buf->b_ml.ml_mfp; | |
865 if (mfp != NULL) | |
866 { | |
867 /* If no swap file yet, may open one */ | |
868 if (mfp->mf_fd < 0 && buf->b_may_swap) | |
869 ml_open_file(buf); | |
870 | |
871 /* only if there is a swapfile */ | |
872 if (mfp->mf_fd >= 0) | |
873 { | |
874 for (hp = mfp->mf_used_last; hp != NULL; ) | |
875 { | |
876 if (!(hp->bh_flags & BH_LOCKED) | |
877 && (!(hp->bh_flags & BH_DIRTY) | |
878 || mf_write(mfp, hp) != FAIL)) | |
879 { | |
880 mf_rem_used(mfp, hp); | |
881 mf_rem_hash(mfp, hp); | |
882 mf_free_bhdr(hp); | |
883 hp = mfp->mf_used_last; /* re-start, list was changed */ | |
884 retval = TRUE; | |
885 } | |
886 else | |
887 hp = hp->bh_prev; | |
888 } | |
889 } | |
890 } | |
891 } | |
892 return retval; | |
893 } | |
894 | |
895 /* | |
896 * Allocate a block header and a block of memory for it | |
897 */ | |
898 static bhdr_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
899 mf_alloc_bhdr(memfile_T *mfp, int page_count) |
7 | 900 { |
901 bhdr_T *hp; | |
902 | |
903 if ((hp = (bhdr_T *)alloc((unsigned)sizeof(bhdr_T))) != NULL) | |
904 { | |
905 if ((hp->bh_data = (char_u *)alloc(mfp->mf_page_size * page_count)) | |
906 == NULL) | |
907 { | |
908 vim_free(hp); /* not enough memory */ | |
909 return NULL; | |
910 } | |
911 hp->bh_page_count = page_count; | |
912 } | |
913 return hp; | |
914 } | |
915 | |
916 /* | |
917 * Free a block header and the block of memory for it | |
918 */ | |
919 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
920 mf_free_bhdr(bhdr_T *hp) |
7 | 921 { |
922 vim_free(hp->bh_data); | |
923 vim_free(hp); | |
924 } | |
925 | |
926 /* | |
927 * insert entry *hp in the free list | |
928 */ | |
929 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
930 mf_ins_free(memfile_T *mfp, bhdr_T *hp) |
7 | 931 { |
932 hp->bh_next = mfp->mf_free_first; | |
933 mfp->mf_free_first = hp; | |
934 } | |
935 | |
936 /* | |
937 * remove the first entry from the free list and return a pointer to it | |
938 * Note: caller must check that mfp->mf_free_first is not NULL! | |
939 */ | |
940 static bhdr_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
941 mf_rem_free(memfile_T *mfp) |
7 | 942 { |
943 bhdr_T *hp; | |
944 | |
945 hp = mfp->mf_free_first; | |
946 mfp->mf_free_first = hp->bh_next; | |
947 return hp; | |
948 } | |
949 | |
950 /* | |
951 * read a block from disk | |
952 * | |
953 * Return FAIL for failure, OK otherwise | |
954 */ | |
955 static int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
956 mf_read(memfile_T *mfp, bhdr_T *hp) |
7 | 957 { |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
958 off_T offset; |
7 | 959 unsigned page_size; |
960 unsigned size; | |
961 | |
962 if (mfp->mf_fd < 0) /* there is no file, can't read */ | |
963 return FAIL; | |
964 | |
965 page_size = mfp->mf_page_size; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
966 offset = (off_T)page_size * hp->bh_bnum; |
7 | 967 size = page_size * hp->bh_page_count; |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
968 if (vim_lseek(mfp->mf_fd, offset, SEEK_SET) != offset) |
7 | 969 { |
1033 | 970 PERROR(_("E294: Seek error in swap file read")); |
7 | 971 return FAIL; |
972 } | |
2664 | 973 if ((unsigned)read_eintr(mfp->mf_fd, hp->bh_data, size) != size) |
7 | 974 { |
1033 | 975 PERROR(_("E295: Read error in swap file")); |
7 | 976 return FAIL; |
977 } | |
2267 | 978 |
979 #ifdef FEAT_CRYPT | |
7347
8977b399cf55
commit https://github.com/vim/vim/commit/4a8c2cfc56b9affc36934aa0f20d8cfd2b1511c8
Christian Brabandt <cb@256bit.org>
parents:
6817
diff
changeset
|
980 /* 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
|
981 * key. */ |
8977b399cf55
commit https://github.com/vim/vim/commit/4a8c2cfc56b9affc36934aa0f20d8cfd2b1511c8
Christian Brabandt <cb@256bit.org>
parents:
6817
diff
changeset
|
982 if (*mfp->mf_buffer->b_p_key != NUL || mfp->mf_old_key != NULL) |
2267 | 983 ml_decrypt_data(mfp, hp->bh_data, offset, size); |
984 #endif | |
985 | |
7 | 986 return OK; |
987 } | |
988 | |
989 /* | |
990 * write a block to disk | |
991 * | |
992 * Return FAIL for failure, OK otherwise | |
993 */ | |
994 static int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
995 mf_write(memfile_T *mfp, bhdr_T *hp) |
7 | 996 { |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
997 off_T offset; /* offset in the file */ |
7 | 998 blocknr_T nr; /* block nr which is being written */ |
999 bhdr_T *hp2; | |
1000 unsigned page_size; /* number of bytes in a page */ | |
1001 unsigned page_count; /* number of pages written */ | |
1002 unsigned size; /* number of bytes written */ | |
1003 | |
1004 if (mfp->mf_fd < 0) /* there is no file, can't write */ | |
1005 return FAIL; | |
1006 | |
1007 if (hp->bh_bnum < 0) /* must assign file block number */ | |
1008 if (mf_trans_add(mfp, hp) == FAIL) | |
1009 return FAIL; | |
1010 | |
1011 page_size = mfp->mf_page_size; | |
1012 | |
1013 /* | |
1014 * We don't want gaps in the file. Write the blocks in front of *hp | |
1015 * to extend the file. | |
1016 * If block 'mf_infile_count' is not in the hash list, it has been | |
1017 * freed. Fill the space in the file with data from the current block. | |
1018 */ | |
1019 for (;;) | |
1020 { | |
1021 nr = hp->bh_bnum; | |
1022 if (nr > mfp->mf_infile_count) /* beyond end of file */ | |
1023 { | |
1024 nr = mfp->mf_infile_count; | |
4352 | 1025 hp2 = mf_find_hash(mfp, nr); /* NULL caught below */ |
7 | 1026 } |
1027 else | |
1028 hp2 = hp; | |
1029 | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
1030 offset = (off_T)page_size * nr; |
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
1031 if (vim_lseek(mfp->mf_fd, offset, SEEK_SET) != offset) |
7 | 1032 { |
1033 | 1033 PERROR(_("E296: Seek error in swap file write")); |
7 | 1034 return FAIL; |
1035 } | |
1036 if (hp2 == NULL) /* freed block, fill with dummy data */ | |
1037 page_count = 1; | |
1038 else | |
1039 page_count = hp2->bh_page_count; | |
1040 size = page_size * page_count; | |
2267 | 1041 if (mf_write_block(mfp, hp2 == NULL ? hp : hp2, offset, size) == FAIL) |
7 | 1042 { |
1043 /* | |
1044 * Avoid repeating the error message, this mostly happens when the | |
1226 | 1045 * disk is full. We give the message again only after a successful |
7 | 1046 * write or when hitting a key. We keep on trying, in case some |
1047 * space becomes available. | |
1048 */ | |
1049 if (!did_swapwrite_msg) | |
1050 EMSG(_("E297: Write error in swap file")); | |
1051 did_swapwrite_msg = TRUE; | |
1052 return FAIL; | |
1053 } | |
1054 did_swapwrite_msg = FALSE; | |
1055 if (hp2 != NULL) /* written a non-dummy block */ | |
1056 hp2->bh_flags &= ~BH_DIRTY; | |
1057 /* appended to the file */ | |
1058 if (nr + (blocknr_T)page_count > mfp->mf_infile_count) | |
1059 mfp->mf_infile_count = nr + page_count; | |
1060 if (nr == hp->bh_bnum) /* written the desired block */ | |
1061 break; | |
1062 } | |
1063 return OK; | |
1064 } | |
1065 | |
1066 /* | |
2267 | 1067 * Write block "hp" with data size "size" to file "mfp->mf_fd". |
1068 * Takes care of encryption. | |
1069 * Return FAIL or OK. | |
1070 */ | |
1071 static int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1072 mf_write_block( |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1073 memfile_T *mfp, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1074 bhdr_T *hp, |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
1075 off_T offset UNUSED, |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1076 unsigned size) |
2267 | 1077 { |
1078 char_u *data = hp->bh_data; | |
1079 int result = OK; | |
1080 | |
1081 #ifdef FEAT_CRYPT | |
1082 /* Encrypt if 'key' is set and this is a data block. */ | |
1083 if (*mfp->mf_buffer->b_p_key != NUL) | |
1084 { | |
1085 data = ml_encrypt_data(mfp, data, offset, size); | |
1086 if (data == NULL) | |
1087 return FAIL; | |
1088 } | |
1089 #endif | |
1090 | |
2664 | 1091 if ((unsigned)write_eintr(mfp->mf_fd, data, size) != size) |
2267 | 1092 result = FAIL; |
1093 | |
1094 #ifdef FEAT_CRYPT | |
1095 if (data != hp->bh_data) | |
1096 vim_free(data); | |
1097 #endif | |
1098 | |
1099 return result; | |
1100 } | |
1101 | |
1102 /* | |
7 | 1103 * Make block number for *hp positive and add it to the translation list |
1104 * | |
1105 * Return FAIL for failure, OK otherwise | |
1106 */ | |
1107 static int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1108 mf_trans_add(memfile_T *mfp, bhdr_T *hp) |
7 | 1109 { |
1110 bhdr_T *freep; | |
1111 blocknr_T new_bnum; | |
1112 NR_TRANS *np; | |
1113 int page_count; | |
1114 | |
1115 if (hp->bh_bnum >= 0) /* it's already positive */ | |
1116 return OK; | |
1117 | |
1118 if ((np = (NR_TRANS *)alloc((unsigned)sizeof(NR_TRANS))) == NULL) | |
1119 return FAIL; | |
1120 | |
1121 /* | |
2267 | 1122 * Get a new number for the block. |
7 | 1123 * If the first item in the free list has sufficient pages, use its number |
1124 * Otherwise use mf_blocknr_max. | |
1125 */ | |
1126 freep = mfp->mf_free_first; | |
1127 page_count = hp->bh_page_count; | |
1128 if (freep != NULL && freep->bh_page_count >= page_count) | |
1129 { | |
1130 new_bnum = freep->bh_bnum; | |
1131 /* | |
4352 | 1132 * If the page count of the free block was larger, reduce it. |
7 | 1133 * If the page count matches, remove the block from the free list |
1134 */ | |
1135 if (freep->bh_page_count > page_count) | |
1136 { | |
1137 freep->bh_bnum += page_count; | |
1138 freep->bh_page_count -= page_count; | |
1139 } | |
1140 else | |
1141 { | |
1142 freep = mf_rem_free(mfp); | |
1143 vim_free(freep); | |
1144 } | |
1145 } | |
1146 else | |
1147 { | |
1148 new_bnum = mfp->mf_blocknr_max; | |
1149 mfp->mf_blocknr_max += page_count; | |
1150 } | |
1151 | |
1152 np->nt_old_bnum = hp->bh_bnum; /* adjust number */ | |
1153 np->nt_new_bnum = new_bnum; | |
1154 | |
1155 mf_rem_hash(mfp, hp); /* remove from old hash list */ | |
1156 hp->bh_bnum = new_bnum; | |
1157 mf_ins_hash(mfp, hp); /* insert in new hash list */ | |
1158 | |
2730 | 1159 /* Insert "np" into "mf_trans" hashtable with key "np->nt_old_bnum" */ |
1160 mf_hash_add_item(&mfp->mf_trans, (mf_hashitem_T *)np); | |
7 | 1161 |
1162 return OK; | |
1163 } | |
1164 | |
1165 /* | |
6817 | 1166 * Lookup a translation from the trans lists and delete the entry. |
7 | 1167 * |
1168 * Return the positive new number when found, the old number when not found | |
1169 */ | |
1170 blocknr_T | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1171 mf_trans_del(memfile_T *mfp, blocknr_T old_nr) |
7 | 1172 { |
1173 NR_TRANS *np; | |
1174 blocknr_T new_bnum; | |
1175 | |
2730 | 1176 np = (NR_TRANS *)mf_hash_find(&mfp->mf_trans, old_nr); |
1177 | |
7 | 1178 if (np == NULL) /* not found */ |
1179 return old_nr; | |
1180 | |
1181 mfp->mf_neg_count--; | |
1182 new_bnum = np->nt_new_bnum; | |
2730 | 1183 |
1184 /* remove entry from the trans list */ | |
1185 mf_hash_rem_item(&mfp->mf_trans, (mf_hashitem_T *)np); | |
1186 | |
7 | 1187 vim_free(np); |
1188 | |
1189 return new_bnum; | |
1190 } | |
1191 | |
1192 /* | |
1193 * Set mfp->mf_ffname according to mfp->mf_fname and some other things. | |
1194 * Only called when creating or renaming the swapfile. Either way it's a new | |
1195 * name so we must work out the full path name. | |
1196 */ | |
1197 void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1198 mf_set_ffname(memfile_T *mfp) |
7 | 1199 { |
1200 mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE); | |
1201 } | |
1202 | |
1203 /* | |
1204 * Make the name of the file used for the memfile a full path. | |
1205 * Used before doing a :cd | |
1206 */ | |
1207 void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1208 mf_fullname(memfile_T *mfp) |
7 | 1209 { |
1210 if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL) | |
1211 { | |
1212 vim_free(mfp->mf_fname); | |
1213 mfp->mf_fname = mfp->mf_ffname; | |
1214 mfp->mf_ffname = NULL; | |
1215 } | |
1216 } | |
1217 | |
1218 /* | |
1219 * return TRUE if there are any translations pending for 'mfp' | |
1220 */ | |
1221 int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1222 mf_need_trans(memfile_T *mfp) |
7 | 1223 { |
1224 return (mfp->mf_fname != NULL && mfp->mf_neg_count > 0); | |
1225 } | |
1226 | |
1227 /* | |
1228 * Open a swap file for a memfile. | |
1229 * The "fname" must be in allocated memory, and is consumed (also when an | |
1230 * error occurs). | |
1231 */ | |
1232 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1233 mf_do_open( |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1234 memfile_T *mfp, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1235 char_u *fname, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1236 int flags) /* flags for open() */ |
7 | 1237 { |
1238 #ifdef HAVE_LSTAT | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
1239 stat_T sb; |
7 | 1240 #endif |
1241 | |
1242 mfp->mf_fname = fname; | |
1243 | |
1244 /* | |
1245 * Get the full path name before the open, because this is | |
1246 * not possible after the open on the Amiga. | |
1247 * fname cannot be NameBuff, because it must have been allocated. | |
1248 */ | |
1249 mf_set_ffname(mfp); | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
7961
diff
changeset
|
1250 #if defined(MSWIN) |
7 | 1251 /* |
4352 | 1252 * A ":!cd e:xxx" may change the directory without us knowing, use the |
7 | 1253 * full pathname always. Careful: This frees fname! |
1254 */ | |
1255 mf_fullname(mfp); | |
1256 #endif | |
1257 | |
1258 #ifdef HAVE_LSTAT | |
1259 /* | |
1260 * Extra security check: When creating a swap file it really shouldn't | |
1261 * exist yet. If there is a symbolic link, this is most likely an attack. | |
1262 */ | |
1263 if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0) | |
1264 { | |
1265 mfp->mf_fd = -1; | |
1266 EMSG(_("E300: Swap file already exists (symlink attack?)")); | |
1267 } | |
1268 else | |
1269 #endif | |
1270 { | |
1271 /* | |
1272 * try to open the file | |
1273 */ | |
557 | 1274 flags |= O_EXTRA | O_NOFOLLOW; |
7 | 1275 #ifdef WIN32 |
1276 /* Prevent handle inheritance that cause problems with Cscope | |
1277 * (swap file may not be deleted if cscope connection was open after | |
1278 * the file) */ | |
1279 flags |= O_NOINHERIT; | |
1280 #endif | |
1281 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags); | |
1282 } | |
1283 | |
1284 /* | |
1285 * If the file cannot be opened, use memory only | |
1286 */ | |
1287 if (mfp->mf_fd < 0) | |
1288 { | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
10359
diff
changeset
|
1289 VIM_CLEAR(mfp->mf_fname); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
10359
diff
changeset
|
1290 VIM_CLEAR(mfp->mf_ffname); |
7 | 1291 } |
1292 else | |
1583 | 1293 { |
2003 | 1294 #ifdef HAVE_FD_CLOEXEC |
1295 int fdflags = fcntl(mfp->mf_fd, F_GETFD); | |
1296 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) | |
7961
a7e58c6e4e9a
commit https://github.com/vim/vim/commit/fbc4b4db3a9690906a96e16724350a6241cf32a5
Christian Brabandt <cb@256bit.org>
parents:
7827
diff
changeset
|
1297 (void)fcntl(mfp->mf_fd, F_SETFD, fdflags | FD_CLOEXEC); |
2003 | 1298 #endif |
5788 | 1299 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 1300 mch_copy_sec(fname, mfp->mf_fname); |
1301 #endif | |
7 | 1302 mch_hide(mfp->mf_fname); /* try setting the 'hidden' flag */ |
1583 | 1303 } |
7 | 1304 } |
2730 | 1305 |
1306 /* | |
1307 * Implementation of mf_hashtab_T follows. | |
1308 */ | |
1309 | |
1310 /* | |
1311 * The number of buckets in the hashtable is increased by a factor of | |
1312 * MHT_GROWTH_FACTOR when the average number of items per bucket | |
1313 * exceeds 2 ^ MHT_LOG_LOAD_FACTOR. | |
1314 */ | |
1315 #define MHT_LOG_LOAD_FACTOR 6 | |
1316 #define MHT_GROWTH_FACTOR 2 /* must be a power of two */ | |
1317 | |
1318 /* | |
1319 * Initialize an empty hash table. | |
1320 */ | |
1321 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1322 mf_hash_init(mf_hashtab_T *mht) |
2730 | 1323 { |
1324 vim_memset(mht, 0, sizeof(mf_hashtab_T)); | |
1325 mht->mht_buckets = mht->mht_small_buckets; | |
1326 mht->mht_mask = MHT_INIT_SIZE - 1; | |
1327 } | |
1328 | |
1329 /* | |
1330 * Free the array of a hash table. Does not free the items it contains! | |
1331 * The hash table must not be used again without another mf_hash_init() call. | |
1332 */ | |
1333 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1334 mf_hash_free(mf_hashtab_T *mht) |
2730 | 1335 { |
1336 if (mht->mht_buckets != mht->mht_small_buckets) | |
1337 vim_free(mht->mht_buckets); | |
1338 } | |
1339 | |
1340 /* | |
1341 * Free the array of a hash table and all the items it contains. | |
1342 */ | |
1343 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1344 mf_hash_free_all(mf_hashtab_T *mht) |
2730 | 1345 { |
1346 long_u idx; | |
1347 mf_hashitem_T *mhi; | |
1348 mf_hashitem_T *next; | |
1349 | |
1350 for (idx = 0; idx <= mht->mht_mask; idx++) | |
1351 for (mhi = mht->mht_buckets[idx]; mhi != NULL; mhi = next) | |
1352 { | |
1353 next = mhi->mhi_next; | |
1354 vim_free(mhi); | |
1355 } | |
1356 | |
1357 mf_hash_free(mht); | |
1358 } | |
1359 | |
1360 /* | |
1361 * Find "key" in hashtable "mht". | |
1362 * Returns a pointer to a mf_hashitem_T or NULL if the item was not found. | |
1363 */ | |
1364 static mf_hashitem_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1365 mf_hash_find(mf_hashtab_T *mht, blocknr_T key) |
2730 | 1366 { |
1367 mf_hashitem_T *mhi; | |
1368 | |
1369 mhi = mht->mht_buckets[key & mht->mht_mask]; | |
1370 while (mhi != NULL && mhi->mhi_key != key) | |
1371 mhi = mhi->mhi_next; | |
1372 | |
1373 return mhi; | |
1374 } | |
1375 | |
1376 /* | |
1377 * Add item "mhi" to hashtable "mht". | |
1378 * "mhi" must not be NULL. | |
1379 */ | |
1380 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1381 mf_hash_add_item(mf_hashtab_T *mht, mf_hashitem_T *mhi) |
2730 | 1382 { |
1383 long_u idx; | |
1384 | |
1385 idx = mhi->mhi_key & mht->mht_mask; | |
1386 mhi->mhi_next = mht->mht_buckets[idx]; | |
1387 mhi->mhi_prev = NULL; | |
1388 if (mhi->mhi_next != NULL) | |
1389 mhi->mhi_next->mhi_prev = mhi; | |
1390 mht->mht_buckets[idx] = mhi; | |
1391 | |
1392 mht->mht_count++; | |
1393 | |
1394 /* | |
1395 * Grow hashtable when we have more thank 2^MHT_LOG_LOAD_FACTOR | |
1396 * items per bucket on average | |
1397 */ | |
1398 if (mht->mht_fixed == 0 | |
1399 && (mht->mht_count >> MHT_LOG_LOAD_FACTOR) > mht->mht_mask) | |
1400 { | |
1401 if (mf_hash_grow(mht) == FAIL) | |
1402 { | |
1403 /* stop trying to grow after first failure to allocate memory */ | |
1404 mht->mht_fixed = 1; | |
1405 } | |
1406 } | |
1407 } | |
1408 | |
1409 /* | |
1410 * Remove item "mhi" from hashtable "mht". | |
1411 * "mhi" must not be NULL and must have been inserted into "mht". | |
1412 */ | |
1413 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1414 mf_hash_rem_item(mf_hashtab_T *mht, mf_hashitem_T *mhi) |
2730 | 1415 { |
1416 if (mhi->mhi_prev == NULL) | |
1417 mht->mht_buckets[mhi->mhi_key & mht->mht_mask] = mhi->mhi_next; | |
1418 else | |
1419 mhi->mhi_prev->mhi_next = mhi->mhi_next; | |
1420 | |
1421 if (mhi->mhi_next != NULL) | |
1422 mhi->mhi_next->mhi_prev = mhi->mhi_prev; | |
1423 | |
1424 mht->mht_count--; | |
1425 | |
1426 /* We could shrink the table here, but it typically takes little memory, | |
1427 * so why bother? */ | |
1428 } | |
1429 | |
1430 /* | |
1431 * Increase number of buckets in the hashtable by MHT_GROWTH_FACTOR and | |
1432 * rehash items. | |
1433 * Returns FAIL when out of memory. | |
1434 */ | |
1435 static int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1436 mf_hash_grow(mf_hashtab_T *mht) |
2730 | 1437 { |
1438 long_u i, j; | |
1439 int shift; | |
1440 mf_hashitem_T *mhi; | |
1441 mf_hashitem_T *tails[MHT_GROWTH_FACTOR]; | |
1442 mf_hashitem_T **buckets; | |
1443 size_t size; | |
1444 | |
1445 size = (mht->mht_mask + 1) * MHT_GROWTH_FACTOR * sizeof(void *); | |
1446 buckets = (mf_hashitem_T **)lalloc_clear(size, FALSE); | |
1447 if (buckets == NULL) | |
1448 return FAIL; | |
1449 | |
1450 shift = 0; | |
1451 while ((mht->mht_mask >> shift) != 0) | |
1452 shift++; | |
1453 | |
1454 for (i = 0; i <= mht->mht_mask; i++) | |
1455 { | |
1456 /* | |
1457 * Traverse the items in the i-th original bucket and move them into | |
1458 * MHT_GROWTH_FACTOR new buckets, preserving their relative order | |
1459 * within each new bucket. Preserving the order is important because | |
1460 * mf_get() tries to keep most recently used items at the front of | |
1461 * each bucket. | |
1462 * | |
1463 * Here we strongly rely on the fact the hashes are computed modulo | |
1464 * a power of two. | |
1465 */ | |
1466 | |
1467 vim_memset(tails, 0, sizeof(tails)); | |
1468 | |
1469 for (mhi = mht->mht_buckets[i]; mhi != NULL; mhi = mhi->mhi_next) | |
1470 { | |
1471 j = (mhi->mhi_key >> shift) & (MHT_GROWTH_FACTOR - 1); | |
1472 if (tails[j] == NULL) | |
1473 { | |
1474 buckets[i + (j << shift)] = mhi; | |
1475 tails[j] = mhi; | |
1476 mhi->mhi_prev = NULL; | |
1477 } | |
1478 else | |
1479 { | |
1480 tails[j]->mhi_next = mhi; | |
1481 mhi->mhi_prev = tails[j]; | |
1482 tails[j] = mhi; | |
1483 } | |
1484 } | |
1485 | |
1486 for (j = 0; j < MHT_GROWTH_FACTOR; j++) | |
1487 if (tails[j] != NULL) | |
1488 tails[j]->mhi_next = NULL; | |
1489 } | |
1490 | |
1491 if (mht->mht_buckets != mht->mht_small_buckets) | |
1492 vim_free(mht->mht_buckets); | |
1493 | |
1494 mht->mht_buckets = buckets; | |
1495 mht->mht_mask = (mht->mht_mask + 1) * MHT_GROWTH_FACTOR - 1; | |
1496 | |
1497 return OK; | |
1498 } |