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