Mercurial > vim
annotate src/memfile.c @ 9614:d9d3ef70c859 v7.4.2084
commit https://github.com/vim/vim/commit/60084333816c585d5858bc085b2942f813102ae3
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Jul 20 22:23:49 2016 +0200
patch 7.4.2084
Problem: New digraph test makes testing hang.
Solution: Don't set "nocp".
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Wed, 20 Jul 2016 22:30:08 +0200 |
parents | b2aada04d84e |
children | fd9727ae3c49 |
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 *); |
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 */ | |
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; | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
7961
diff
changeset
|
544 #if defined(SYNC_DUP_CLOSE) |
7 | 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 */ | |
606 if (STRCMP(p_sws, "fsync") == 0) | |
607 { | |
608 if (fsync(mfp->mf_fd)) | |
609 status = FAIL; | |
610 } | |
611 else | |
612 # endif | |
613 /* OpenNT is strictly POSIX (Benzinger) */ | |
614 /* Tandem/Himalaya NSK-OSS doesn't have sync() */ | |
6637 | 615 /* No sync() on Stratus VOS */ |
616 # if defined(__OPENNT) || defined(__TANDEM) || defined(__VOS__) | |
7 | 617 fflush(NULL); |
618 # else | |
619 sync(); | |
620 # endif | |
621 #endif | |
622 #ifdef VMS | |
623 if (STRCMP(p_sws, "fsync") == 0) | |
624 { | |
625 if (fsync(mfp->mf_fd)) | |
626 status = FAIL; | |
627 } | |
628 #endif | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
7961
diff
changeset
|
629 #ifdef SYNC_DUP_CLOSE |
7 | 630 /* |
631 * Win32 is a bit more work: Duplicate the file handle and close it. | |
632 * This should flush the file to disk. | |
633 */ | |
634 if ((fd = dup(mfp->mf_fd)) >= 0) | |
635 close(fd); | |
636 #endif | |
637 #ifdef AMIGA | |
1030 | 638 # if defined(__AROS__) || defined(__amigaos4__) |
7 | 639 if (fsync(mfp->mf_fd) != 0) |
640 status = FAIL; | |
641 # else | |
642 /* | |
643 * Flush() only exists for AmigaDos 2.0. | |
644 * For 1.3 it should be done with close() + open(), but then the risk | |
645 * is that the open() may fail and lose the file.... | |
646 */ | |
647 # ifdef FEAT_ARP | |
648 if (dos2) | |
649 # endif | |
650 # ifdef SASC | |
651 { | |
652 struct UFB *fp = chkufb(mfp->mf_fd); | |
653 | |
654 if (fp != NULL) | |
655 Flush(fp->ufbfh); | |
656 } | |
657 # else | |
658 # if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__) | |
659 { | |
984 | 660 # if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__) |
7 | 661 /* Have function (in libnix at least), |
662 * but ain't got no prototype anywhere. */ | |
663 extern unsigned long fdtofh(int filedescriptor); | |
664 # endif | |
984 | 665 # if !defined(__libnix__) |
666 fflush(NULL); | |
667 # else | |
7 | 668 BPTR fh = (BPTR)fdtofh(mfp->mf_fd); |
669 | |
670 if (fh != 0) | |
671 Flush(fh); | |
984 | 672 # endif |
7 | 673 } |
674 # else /* assume Manx */ | |
675 Flush(_devtab[mfp->mf_fd].fd); | |
676 # endif | |
677 # endif | |
678 # endif | |
679 #endif /* AMIGA */ | |
680 } | |
681 | |
682 got_int |= got_int_save; | |
683 | |
684 return status; | |
685 } | |
686 | |
687 /* | |
630 | 688 * For all blocks in memory file *mfp that have a positive block number set |
689 * the dirty flag. These are blocks that need to be written to a newly | |
690 * created swapfile. | |
691 */ | |
692 void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
693 mf_set_dirty(memfile_T *mfp) |
630 | 694 { |
695 bhdr_T *hp; | |
696 | |
697 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev) | |
698 if (hp->bh_bnum > 0) | |
699 hp->bh_flags |= BH_DIRTY; | |
700 mfp->mf_dirty = TRUE; | |
701 } | |
702 | |
703 /* | |
7 | 704 * insert block *hp in front of hashlist of memfile *mfp |
705 */ | |
706 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
707 mf_ins_hash(memfile_T *mfp, bhdr_T *hp) |
7 | 708 { |
2730 | 709 mf_hash_add_item(&mfp->mf_hash, (mf_hashitem_T *)hp); |
7 | 710 } |
711 | |
712 /* | |
713 * remove block *hp from hashlist of memfile list *mfp | |
714 */ | |
715 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
716 mf_rem_hash(memfile_T *mfp, bhdr_T *hp) |
7 | 717 { |
2730 | 718 mf_hash_rem_item(&mfp->mf_hash, (mf_hashitem_T *)hp); |
7 | 719 } |
720 | |
721 /* | |
722 * look in hash lists of memfile *mfp for block header with number 'nr' | |
723 */ | |
724 static bhdr_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
725 mf_find_hash(memfile_T *mfp, blocknr_T nr) |
7 | 726 { |
2730 | 727 return (bhdr_T *)mf_hash_find(&mfp->mf_hash, nr); |
7 | 728 } |
729 | |
730 /* | |
731 * insert block *hp in front of used list of memfile *mfp | |
732 */ | |
733 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
734 mf_ins_used(memfile_T *mfp, bhdr_T *hp) |
7 | 735 { |
736 hp->bh_next = mfp->mf_used_first; | |
737 mfp->mf_used_first = hp; | |
738 hp->bh_prev = NULL; | |
739 if (hp->bh_next == NULL) /* list was empty, adjust last pointer */ | |
740 mfp->mf_used_last = hp; | |
741 else | |
742 hp->bh_next->bh_prev = hp; | |
743 mfp->mf_used_count += hp->bh_page_count; | |
744 total_mem_used += hp->bh_page_count * mfp->mf_page_size; | |
745 } | |
746 | |
747 /* | |
748 * remove block *hp from used list of memfile *mfp | |
749 */ | |
750 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
751 mf_rem_used(memfile_T *mfp, bhdr_T *hp) |
7 | 752 { |
753 if (hp->bh_next == NULL) /* last block in used list */ | |
754 mfp->mf_used_last = hp->bh_prev; | |
755 else | |
756 hp->bh_next->bh_prev = hp->bh_prev; | |
757 if (hp->bh_prev == NULL) /* first block in used list */ | |
758 mfp->mf_used_first = hp->bh_next; | |
759 else | |
760 hp->bh_prev->bh_next = hp->bh_next; | |
761 mfp->mf_used_count -= hp->bh_page_count; | |
762 total_mem_used -= hp->bh_page_count * mfp->mf_page_size; | |
763 } | |
764 | |
765 /* | |
766 * Release the least recently used block from the used list if the number | |
767 * of used memory blocks gets to big. | |
768 * | |
769 * Return the block header to the caller, including the memory block, so | |
770 * it can be re-used. Make sure the page_count is right. | |
6817 | 771 * |
772 * Returns NULL if no block is released. | |
7 | 773 */ |
774 static bhdr_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
775 mf_release(memfile_T *mfp, int page_count) |
7 | 776 { |
777 bhdr_T *hp; | |
778 int need_release; | |
779 buf_T *buf; | |
780 | |
781 /* don't release while in mf_close_file() */ | |
1066 | 782 if (mf_dont_release) |
7 | 783 return NULL; |
784 | |
785 /* | |
786 * Need to release a block if the number of blocks for this memfile is | |
787 * higher than the maximum or total memory used is over 'maxmemtot' | |
788 */ | |
789 need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max) | |
790 || (total_mem_used >> 10) >= (long_u)p_mmt); | |
791 | |
792 /* | |
793 * Try to create a swap file if the amount of memory used is getting too | |
794 * high. | |
795 */ | |
796 if (mfp->mf_fd < 0 && need_release && p_uc) | |
797 { | |
798 /* find for which buffer this memfile is */ | |
799 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
800 if (buf->b_ml.ml_mfp == mfp) | |
801 break; | |
802 if (buf != NULL && buf->b_may_swap) | |
803 ml_open_file(buf); | |
804 } | |
805 | |
806 /* | |
807 * don't release a block if | |
808 * there is no file for this memfile | |
809 * or | |
810 * the number of blocks for this memfile is lower than the maximum | |
811 * and | |
812 * total memory used is not up to 'maxmemtot' | |
813 */ | |
814 if (mfp->mf_fd < 0 || !need_release) | |
815 return NULL; | |
816 | |
817 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev) | |
818 if (!(hp->bh_flags & BH_LOCKED)) | |
819 break; | |
820 if (hp == NULL) /* not a single one that can be released */ | |
821 return NULL; | |
822 | |
823 /* | |
824 * If the block is dirty, write it. | |
825 * If the write fails we don't free it. | |
826 */ | |
827 if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL) | |
828 return NULL; | |
829 | |
830 mf_rem_used(mfp, hp); | |
831 mf_rem_hash(mfp, hp); | |
832 | |
833 /* | |
834 * If a bhdr_T is returned, make sure that the page_count of bh_data is | |
835 * right | |
836 */ | |
837 if (hp->bh_page_count != page_count) | |
838 { | |
839 vim_free(hp->bh_data); | |
840 if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL) | |
841 { | |
842 vim_free(hp); | |
843 return NULL; | |
844 } | |
845 hp->bh_page_count = page_count; | |
846 } | |
847 return hp; | |
848 } | |
849 | |
850 /* | |
851 * release as many blocks as possible | |
852 * Used in case of out of memory | |
853 * | |
854 * return TRUE if any memory was released | |
855 */ | |
856 int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
857 mf_release_all(void) |
7 | 858 { |
859 buf_T *buf; | |
860 memfile_T *mfp; | |
861 bhdr_T *hp; | |
862 int retval = FALSE; | |
863 | |
864 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
865 { | |
866 mfp = buf->b_ml.ml_mfp; | |
867 if (mfp != NULL) | |
868 { | |
869 /* If no swap file yet, may open one */ | |
870 if (mfp->mf_fd < 0 && buf->b_may_swap) | |
871 ml_open_file(buf); | |
872 | |
873 /* only if there is a swapfile */ | |
874 if (mfp->mf_fd >= 0) | |
875 { | |
876 for (hp = mfp->mf_used_last; hp != NULL; ) | |
877 { | |
878 if (!(hp->bh_flags & BH_LOCKED) | |
879 && (!(hp->bh_flags & BH_DIRTY) | |
880 || mf_write(mfp, hp) != FAIL)) | |
881 { | |
882 mf_rem_used(mfp, hp); | |
883 mf_rem_hash(mfp, hp); | |
884 mf_free_bhdr(hp); | |
885 hp = mfp->mf_used_last; /* re-start, list was changed */ | |
886 retval = TRUE; | |
887 } | |
888 else | |
889 hp = hp->bh_prev; | |
890 } | |
891 } | |
892 } | |
893 } | |
894 return retval; | |
895 } | |
896 | |
897 /* | |
898 * Allocate a block header and a block of memory for it | |
899 */ | |
900 static bhdr_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
901 mf_alloc_bhdr(memfile_T *mfp, int page_count) |
7 | 902 { |
903 bhdr_T *hp; | |
904 | |
905 if ((hp = (bhdr_T *)alloc((unsigned)sizeof(bhdr_T))) != NULL) | |
906 { | |
907 if ((hp->bh_data = (char_u *)alloc(mfp->mf_page_size * page_count)) | |
908 == NULL) | |
909 { | |
910 vim_free(hp); /* not enough memory */ | |
911 return NULL; | |
912 } | |
913 hp->bh_page_count = page_count; | |
914 } | |
915 return hp; | |
916 } | |
917 | |
918 /* | |
919 * Free a block header and the block of memory for it | |
920 */ | |
921 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
922 mf_free_bhdr(bhdr_T *hp) |
7 | 923 { |
924 vim_free(hp->bh_data); | |
925 vim_free(hp); | |
926 } | |
927 | |
928 /* | |
929 * insert entry *hp in the free list | |
930 */ | |
931 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
932 mf_ins_free(memfile_T *mfp, bhdr_T *hp) |
7 | 933 { |
934 hp->bh_next = mfp->mf_free_first; | |
935 mfp->mf_free_first = hp; | |
936 } | |
937 | |
938 /* | |
939 * remove the first entry from the free list and return a pointer to it | |
940 * Note: caller must check that mfp->mf_free_first is not NULL! | |
941 */ | |
942 static bhdr_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
943 mf_rem_free(memfile_T *mfp) |
7 | 944 { |
945 bhdr_T *hp; | |
946 | |
947 hp = mfp->mf_free_first; | |
948 mfp->mf_free_first = hp->bh_next; | |
949 return hp; | |
950 } | |
951 | |
952 /* | |
953 * read a block from disk | |
954 * | |
955 * Return FAIL for failure, OK otherwise | |
956 */ | |
957 static int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
958 mf_read(memfile_T *mfp, bhdr_T *hp) |
7 | 959 { |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
960 off_T offset; |
7 | 961 unsigned page_size; |
962 unsigned size; | |
963 | |
964 if (mfp->mf_fd < 0) /* there is no file, can't read */ | |
965 return FAIL; | |
966 | |
967 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
|
968 offset = (off_T)page_size * hp->bh_bnum; |
7 | 969 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
|
970 if (vim_lseek(mfp->mf_fd, offset, SEEK_SET) != offset) |
7 | 971 { |
1033 | 972 PERROR(_("E294: Seek error in swap file read")); |
7 | 973 return FAIL; |
974 } | |
2664 | 975 if ((unsigned)read_eintr(mfp->mf_fd, hp->bh_data, size) != size) |
7 | 976 { |
1033 | 977 PERROR(_("E295: Read error in swap file")); |
7 | 978 return FAIL; |
979 } | |
2267 | 980 |
981 #ifdef FEAT_CRYPT | |
7347
8977b399cf55
commit https://github.com/vim/vim/commit/4a8c2cfc56b9affc36934aa0f20d8cfd2b1511c8
Christian Brabandt <cb@256bit.org>
parents:
6817
diff
changeset
|
982 /* 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
|
983 * key. */ |
8977b399cf55
commit https://github.com/vim/vim/commit/4a8c2cfc56b9affc36934aa0f20d8cfd2b1511c8
Christian Brabandt <cb@256bit.org>
parents:
6817
diff
changeset
|
984 if (*mfp->mf_buffer->b_p_key != NUL || mfp->mf_old_key != NULL) |
2267 | 985 ml_decrypt_data(mfp, hp->bh_data, offset, size); |
986 #endif | |
987 | |
7 | 988 return OK; |
989 } | |
990 | |
991 /* | |
992 * write a block to disk | |
993 * | |
994 * Return FAIL for failure, OK otherwise | |
995 */ | |
996 static int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
997 mf_write(memfile_T *mfp, bhdr_T *hp) |
7 | 998 { |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
999 off_T offset; /* offset in the file */ |
7 | 1000 blocknr_T nr; /* block nr which is being written */ |
1001 bhdr_T *hp2; | |
1002 unsigned page_size; /* number of bytes in a page */ | |
1003 unsigned page_count; /* number of pages written */ | |
1004 unsigned size; /* number of bytes written */ | |
1005 | |
1006 if (mfp->mf_fd < 0) /* there is no file, can't write */ | |
1007 return FAIL; | |
1008 | |
1009 if (hp->bh_bnum < 0) /* must assign file block number */ | |
1010 if (mf_trans_add(mfp, hp) == FAIL) | |
1011 return FAIL; | |
1012 | |
1013 page_size = mfp->mf_page_size; | |
1014 | |
1015 /* | |
1016 * We don't want gaps in the file. Write the blocks in front of *hp | |
1017 * to extend the file. | |
1018 * If block 'mf_infile_count' is not in the hash list, it has been | |
1019 * freed. Fill the space in the file with data from the current block. | |
1020 */ | |
1021 for (;;) | |
1022 { | |
1023 nr = hp->bh_bnum; | |
1024 if (nr > mfp->mf_infile_count) /* beyond end of file */ | |
1025 { | |
1026 nr = mfp->mf_infile_count; | |
4352 | 1027 hp2 = mf_find_hash(mfp, nr); /* NULL caught below */ |
7 | 1028 } |
1029 else | |
1030 hp2 = hp; | |
1031 | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
1032 offset = (off_T)page_size * nr; |
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
1033 if (vim_lseek(mfp->mf_fd, offset, SEEK_SET) != offset) |
7 | 1034 { |
1033 | 1035 PERROR(_("E296: Seek error in swap file write")); |
7 | 1036 return FAIL; |
1037 } | |
1038 if (hp2 == NULL) /* freed block, fill with dummy data */ | |
1039 page_count = 1; | |
1040 else | |
1041 page_count = hp2->bh_page_count; | |
1042 size = page_size * page_count; | |
2267 | 1043 if (mf_write_block(mfp, hp2 == NULL ? hp : hp2, offset, size) == FAIL) |
7 | 1044 { |
1045 /* | |
1046 * Avoid repeating the error message, this mostly happens when the | |
1226 | 1047 * disk is full. We give the message again only after a successful |
7 | 1048 * write or when hitting a key. We keep on trying, in case some |
1049 * space becomes available. | |
1050 */ | |
1051 if (!did_swapwrite_msg) | |
1052 EMSG(_("E297: Write error in swap file")); | |
1053 did_swapwrite_msg = TRUE; | |
1054 return FAIL; | |
1055 } | |
1056 did_swapwrite_msg = FALSE; | |
1057 if (hp2 != NULL) /* written a non-dummy block */ | |
1058 hp2->bh_flags &= ~BH_DIRTY; | |
1059 /* appended to the file */ | |
1060 if (nr + (blocknr_T)page_count > mfp->mf_infile_count) | |
1061 mfp->mf_infile_count = nr + page_count; | |
1062 if (nr == hp->bh_bnum) /* written the desired block */ | |
1063 break; | |
1064 } | |
1065 return OK; | |
1066 } | |
1067 | |
1068 /* | |
2267 | 1069 * Write block "hp" with data size "size" to file "mfp->mf_fd". |
1070 * Takes care of encryption. | |
1071 * Return FAIL or OK. | |
1072 */ | |
1073 static int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1074 mf_write_block( |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1075 memfile_T *mfp, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1076 bhdr_T *hp, |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
1077 off_T offset UNUSED, |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1078 unsigned size) |
2267 | 1079 { |
1080 char_u *data = hp->bh_data; | |
1081 int result = OK; | |
1082 | |
1083 #ifdef FEAT_CRYPT | |
1084 /* Encrypt if 'key' is set and this is a data block. */ | |
1085 if (*mfp->mf_buffer->b_p_key != NUL) | |
1086 { | |
1087 data = ml_encrypt_data(mfp, data, offset, size); | |
1088 if (data == NULL) | |
1089 return FAIL; | |
1090 } | |
1091 #endif | |
1092 | |
2664 | 1093 if ((unsigned)write_eintr(mfp->mf_fd, data, size) != size) |
2267 | 1094 result = FAIL; |
1095 | |
1096 #ifdef FEAT_CRYPT | |
1097 if (data != hp->bh_data) | |
1098 vim_free(data); | |
1099 #endif | |
1100 | |
1101 return result; | |
1102 } | |
1103 | |
1104 /* | |
7 | 1105 * Make block number for *hp positive and add it to the translation list |
1106 * | |
1107 * Return FAIL for failure, OK otherwise | |
1108 */ | |
1109 static int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1110 mf_trans_add(memfile_T *mfp, bhdr_T *hp) |
7 | 1111 { |
1112 bhdr_T *freep; | |
1113 blocknr_T new_bnum; | |
1114 NR_TRANS *np; | |
1115 int page_count; | |
1116 | |
1117 if (hp->bh_bnum >= 0) /* it's already positive */ | |
1118 return OK; | |
1119 | |
1120 if ((np = (NR_TRANS *)alloc((unsigned)sizeof(NR_TRANS))) == NULL) | |
1121 return FAIL; | |
1122 | |
1123 /* | |
2267 | 1124 * Get a new number for the block. |
7 | 1125 * If the first item in the free list has sufficient pages, use its number |
1126 * Otherwise use mf_blocknr_max. | |
1127 */ | |
1128 freep = mfp->mf_free_first; | |
1129 page_count = hp->bh_page_count; | |
1130 if (freep != NULL && freep->bh_page_count >= page_count) | |
1131 { | |
1132 new_bnum = freep->bh_bnum; | |
1133 /* | |
4352 | 1134 * If the page count of the free block was larger, reduce it. |
7 | 1135 * If the page count matches, remove the block from the free list |
1136 */ | |
1137 if (freep->bh_page_count > page_count) | |
1138 { | |
1139 freep->bh_bnum += page_count; | |
1140 freep->bh_page_count -= page_count; | |
1141 } | |
1142 else | |
1143 { | |
1144 freep = mf_rem_free(mfp); | |
1145 vim_free(freep); | |
1146 } | |
1147 } | |
1148 else | |
1149 { | |
1150 new_bnum = mfp->mf_blocknr_max; | |
1151 mfp->mf_blocknr_max += page_count; | |
1152 } | |
1153 | |
1154 np->nt_old_bnum = hp->bh_bnum; /* adjust number */ | |
1155 np->nt_new_bnum = new_bnum; | |
1156 | |
1157 mf_rem_hash(mfp, hp); /* remove from old hash list */ | |
1158 hp->bh_bnum = new_bnum; | |
1159 mf_ins_hash(mfp, hp); /* insert in new hash list */ | |
1160 | |
2730 | 1161 /* Insert "np" into "mf_trans" hashtable with key "np->nt_old_bnum" */ |
1162 mf_hash_add_item(&mfp->mf_trans, (mf_hashitem_T *)np); | |
7 | 1163 |
1164 return OK; | |
1165 } | |
1166 | |
1167 /* | |
6817 | 1168 * Lookup a translation from the trans lists and delete the entry. |
7 | 1169 * |
1170 * Return the positive new number when found, the old number when not found | |
1171 */ | |
1172 blocknr_T | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1173 mf_trans_del(memfile_T *mfp, blocknr_T old_nr) |
7 | 1174 { |
1175 NR_TRANS *np; | |
1176 blocknr_T new_bnum; | |
1177 | |
2730 | 1178 np = (NR_TRANS *)mf_hash_find(&mfp->mf_trans, old_nr); |
1179 | |
7 | 1180 if (np == NULL) /* not found */ |
1181 return old_nr; | |
1182 | |
1183 mfp->mf_neg_count--; | |
1184 new_bnum = np->nt_new_bnum; | |
2730 | 1185 |
1186 /* remove entry from the trans list */ | |
1187 mf_hash_rem_item(&mfp->mf_trans, (mf_hashitem_T *)np); | |
1188 | |
7 | 1189 vim_free(np); |
1190 | |
1191 return new_bnum; | |
1192 } | |
1193 | |
1194 /* | |
1195 * Set mfp->mf_ffname according to mfp->mf_fname and some other things. | |
1196 * Only called when creating or renaming the swapfile. Either way it's a new | |
1197 * name so we must work out the full path name. | |
1198 */ | |
1199 void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1200 mf_set_ffname(memfile_T *mfp) |
7 | 1201 { |
1202 mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE); | |
1203 } | |
1204 | |
1205 /* | |
1206 * Make the name of the file used for the memfile a full path. | |
1207 * Used before doing a :cd | |
1208 */ | |
1209 void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1210 mf_fullname(memfile_T *mfp) |
7 | 1211 { |
1212 if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL) | |
1213 { | |
1214 vim_free(mfp->mf_fname); | |
1215 mfp->mf_fname = mfp->mf_ffname; | |
1216 mfp->mf_ffname = NULL; | |
1217 } | |
1218 } | |
1219 | |
1220 /* | |
1221 * return TRUE if there are any translations pending for 'mfp' | |
1222 */ | |
1223 int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1224 mf_need_trans(memfile_T *mfp) |
7 | 1225 { |
1226 return (mfp->mf_fname != NULL && mfp->mf_neg_count > 0); | |
1227 } | |
1228 | |
1229 /* | |
1230 * Open a swap file for a memfile. | |
1231 * The "fname" must be in allocated memory, and is consumed (also when an | |
1232 * error occurs). | |
1233 */ | |
1234 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1235 mf_do_open( |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1236 memfile_T *mfp, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1237 char_u *fname, |
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1238 int flags) /* flags for open() */ |
7 | 1239 { |
1240 #ifdef HAVE_LSTAT | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
1241 stat_T sb; |
7 | 1242 #endif |
1243 | |
1244 mfp->mf_fname = fname; | |
1245 | |
1246 /* | |
1247 * Get the full path name before the open, because this is | |
1248 * not possible after the open on the Amiga. | |
1249 * fname cannot be NameBuff, because it must have been allocated. | |
1250 */ | |
1251 mf_set_ffname(mfp); | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
7961
diff
changeset
|
1252 #if defined(MSWIN) |
7 | 1253 /* |
4352 | 1254 * A ":!cd e:xxx" may change the directory without us knowing, use the |
7 | 1255 * full pathname always. Careful: This frees fname! |
1256 */ | |
1257 mf_fullname(mfp); | |
1258 #endif | |
1259 | |
1260 #ifdef HAVE_LSTAT | |
1261 /* | |
1262 * Extra security check: When creating a swap file it really shouldn't | |
1263 * exist yet. If there is a symbolic link, this is most likely an attack. | |
1264 */ | |
1265 if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0) | |
1266 { | |
1267 mfp->mf_fd = -1; | |
1268 EMSG(_("E300: Swap file already exists (symlink attack?)")); | |
1269 } | |
1270 else | |
1271 #endif | |
1272 { | |
1273 /* | |
1274 * try to open the file | |
1275 */ | |
557 | 1276 flags |= O_EXTRA | O_NOFOLLOW; |
7 | 1277 #ifdef WIN32 |
1278 /* Prevent handle inheritance that cause problems with Cscope | |
1279 * (swap file may not be deleted if cscope connection was open after | |
1280 * the file) */ | |
1281 flags |= O_NOINHERIT; | |
1282 #endif | |
1283 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags); | |
1284 } | |
1285 | |
1286 /* | |
1287 * If the file cannot be opened, use memory only | |
1288 */ | |
1289 if (mfp->mf_fd < 0) | |
1290 { | |
1291 vim_free(mfp->mf_fname); | |
1292 vim_free(mfp->mf_ffname); | |
1293 mfp->mf_fname = NULL; | |
1294 mfp->mf_ffname = NULL; | |
1295 } | |
1296 else | |
1583 | 1297 { |
2003 | 1298 #ifdef HAVE_FD_CLOEXEC |
1299 int fdflags = fcntl(mfp->mf_fd, F_GETFD); | |
1300 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
|
1301 (void)fcntl(mfp->mf_fd, F_SETFD, fdflags | FD_CLOEXEC); |
2003 | 1302 #endif |
5788 | 1303 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 1304 mch_copy_sec(fname, mfp->mf_fname); |
1305 #endif | |
7 | 1306 mch_hide(mfp->mf_fname); /* try setting the 'hidden' flag */ |
1583 | 1307 } |
7 | 1308 } |
2730 | 1309 |
1310 /* | |
1311 * Implementation of mf_hashtab_T follows. | |
1312 */ | |
1313 | |
1314 /* | |
1315 * The number of buckets in the hashtable is increased by a factor of | |
1316 * MHT_GROWTH_FACTOR when the average number of items per bucket | |
1317 * exceeds 2 ^ MHT_LOG_LOAD_FACTOR. | |
1318 */ | |
1319 #define MHT_LOG_LOAD_FACTOR 6 | |
1320 #define MHT_GROWTH_FACTOR 2 /* must be a power of two */ | |
1321 | |
1322 /* | |
1323 * Initialize an empty hash table. | |
1324 */ | |
1325 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1326 mf_hash_init(mf_hashtab_T *mht) |
2730 | 1327 { |
1328 vim_memset(mht, 0, sizeof(mf_hashtab_T)); | |
1329 mht->mht_buckets = mht->mht_small_buckets; | |
1330 mht->mht_mask = MHT_INIT_SIZE - 1; | |
1331 } | |
1332 | |
1333 /* | |
1334 * Free the array of a hash table. Does not free the items it contains! | |
1335 * The hash table must not be used again without another mf_hash_init() call. | |
1336 */ | |
1337 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1338 mf_hash_free(mf_hashtab_T *mht) |
2730 | 1339 { |
1340 if (mht->mht_buckets != mht->mht_small_buckets) | |
1341 vim_free(mht->mht_buckets); | |
1342 } | |
1343 | |
1344 /* | |
1345 * Free the array of a hash table and all the items it contains. | |
1346 */ | |
1347 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1348 mf_hash_free_all(mf_hashtab_T *mht) |
2730 | 1349 { |
1350 long_u idx; | |
1351 mf_hashitem_T *mhi; | |
1352 mf_hashitem_T *next; | |
1353 | |
1354 for (idx = 0; idx <= mht->mht_mask; idx++) | |
1355 for (mhi = mht->mht_buckets[idx]; mhi != NULL; mhi = next) | |
1356 { | |
1357 next = mhi->mhi_next; | |
1358 vim_free(mhi); | |
1359 } | |
1360 | |
1361 mf_hash_free(mht); | |
1362 } | |
1363 | |
1364 /* | |
1365 * Find "key" in hashtable "mht". | |
1366 * Returns a pointer to a mf_hashitem_T or NULL if the item was not found. | |
1367 */ | |
1368 static mf_hashitem_T * | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1369 mf_hash_find(mf_hashtab_T *mht, blocknr_T key) |
2730 | 1370 { |
1371 mf_hashitem_T *mhi; | |
1372 | |
1373 mhi = mht->mht_buckets[key & mht->mht_mask]; | |
1374 while (mhi != NULL && mhi->mhi_key != key) | |
1375 mhi = mhi->mhi_next; | |
1376 | |
1377 return mhi; | |
1378 } | |
1379 | |
1380 /* | |
1381 * Add item "mhi" to hashtable "mht". | |
1382 * "mhi" must not be NULL. | |
1383 */ | |
1384 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1385 mf_hash_add_item(mf_hashtab_T *mht, mf_hashitem_T *mhi) |
2730 | 1386 { |
1387 long_u idx; | |
1388 | |
1389 idx = mhi->mhi_key & mht->mht_mask; | |
1390 mhi->mhi_next = mht->mht_buckets[idx]; | |
1391 mhi->mhi_prev = NULL; | |
1392 if (mhi->mhi_next != NULL) | |
1393 mhi->mhi_next->mhi_prev = mhi; | |
1394 mht->mht_buckets[idx] = mhi; | |
1395 | |
1396 mht->mht_count++; | |
1397 | |
1398 /* | |
1399 * Grow hashtable when we have more thank 2^MHT_LOG_LOAD_FACTOR | |
1400 * items per bucket on average | |
1401 */ | |
1402 if (mht->mht_fixed == 0 | |
1403 && (mht->mht_count >> MHT_LOG_LOAD_FACTOR) > mht->mht_mask) | |
1404 { | |
1405 if (mf_hash_grow(mht) == FAIL) | |
1406 { | |
1407 /* stop trying to grow after first failure to allocate memory */ | |
1408 mht->mht_fixed = 1; | |
1409 } | |
1410 } | |
1411 } | |
1412 | |
1413 /* | |
1414 * Remove item "mhi" from hashtable "mht". | |
1415 * "mhi" must not be NULL and must have been inserted into "mht". | |
1416 */ | |
1417 static void | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1418 mf_hash_rem_item(mf_hashtab_T *mht, mf_hashitem_T *mhi) |
2730 | 1419 { |
1420 if (mhi->mhi_prev == NULL) | |
1421 mht->mht_buckets[mhi->mhi_key & mht->mht_mask] = mhi->mhi_next; | |
1422 else | |
1423 mhi->mhi_prev->mhi_next = mhi->mhi_next; | |
1424 | |
1425 if (mhi->mhi_next != NULL) | |
1426 mhi->mhi_next->mhi_prev = mhi->mhi_prev; | |
1427 | |
1428 mht->mht_count--; | |
1429 | |
1430 /* We could shrink the table here, but it typically takes little memory, | |
1431 * so why bother? */ | |
1432 } | |
1433 | |
1434 /* | |
1435 * Increase number of buckets in the hashtable by MHT_GROWTH_FACTOR and | |
1436 * rehash items. | |
1437 * Returns FAIL when out of memory. | |
1438 */ | |
1439 static int | |
7827
41789f16d6b2
commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1440 mf_hash_grow(mf_hashtab_T *mht) |
2730 | 1441 { |
1442 long_u i, j; | |
1443 int shift; | |
1444 mf_hashitem_T *mhi; | |
1445 mf_hashitem_T *tails[MHT_GROWTH_FACTOR]; | |
1446 mf_hashitem_T **buckets; | |
1447 size_t size; | |
1448 | |
1449 size = (mht->mht_mask + 1) * MHT_GROWTH_FACTOR * sizeof(void *); | |
1450 buckets = (mf_hashitem_T **)lalloc_clear(size, FALSE); | |
1451 if (buckets == NULL) | |
1452 return FAIL; | |
1453 | |
1454 shift = 0; | |
1455 while ((mht->mht_mask >> shift) != 0) | |
1456 shift++; | |
1457 | |
1458 for (i = 0; i <= mht->mht_mask; i++) | |
1459 { | |
1460 /* | |
1461 * Traverse the items in the i-th original bucket and move them into | |
1462 * MHT_GROWTH_FACTOR new buckets, preserving their relative order | |
1463 * within each new bucket. Preserving the order is important because | |
1464 * mf_get() tries to keep most recently used items at the front of | |
1465 * each bucket. | |
1466 * | |
1467 * Here we strongly rely on the fact the hashes are computed modulo | |
1468 * a power of two. | |
1469 */ | |
1470 | |
1471 vim_memset(tails, 0, sizeof(tails)); | |
1472 | |
1473 for (mhi = mht->mht_buckets[i]; mhi != NULL; mhi = mhi->mhi_next) | |
1474 { | |
1475 j = (mhi->mhi_key >> shift) & (MHT_GROWTH_FACTOR - 1); | |
1476 if (tails[j] == NULL) | |
1477 { | |
1478 buckets[i + (j << shift)] = mhi; | |
1479 tails[j] = mhi; | |
1480 mhi->mhi_prev = NULL; | |
1481 } | |
1482 else | |
1483 { | |
1484 tails[j]->mhi_next = mhi; | |
1485 mhi->mhi_prev = tails[j]; | |
1486 tails[j] = mhi; | |
1487 } | |
1488 } | |
1489 | |
1490 for (j = 0; j < MHT_GROWTH_FACTOR; j++) | |
1491 if (tails[j] != NULL) | |
1492 tails[j]->mhi_next = NULL; | |
1493 } | |
1494 | |
1495 if (mht->mht_buckets != mht->mht_small_buckets) | |
1496 vim_free(mht->mht_buckets); | |
1497 | |
1498 mht->mht_buckets = buckets; | |
1499 mht->mht_mask = (mht->mht_mask + 1) * MHT_GROWTH_FACTOR - 1; | |
1500 | |
1501 return OK; | |
1502 } |