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