annotate src/memfile.c @ 28265:67ef250562e5 v8.2.4658

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