annotate src/memfile.c @ 6685:fd495ad521dc v7.4.667

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