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