annotate src/memfile.c @ 734:5220ea46cb9a

updated for version 7.0221
author vimboss
date Sat, 11 Mar 2006 21:35:11 +0000
parents 0f9f4761ad9c
children fa9ee618000f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
4 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11 * memfile.c: Contains the functions for handling blocks of memory which can
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 * be stored in a file. This is the implementation of a sort of virtual memory.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 * A memfile consists of a sequence of blocks. The blocks numbered from 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 * upwards have been assigned a place in the actual file. The block number
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 * is equal to the page number in the file. The
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 * blocks with negative numbers are currently in memory only. They can be
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 * assigned a place in the file when too much memory is being used. At that
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19 * moment they get a new, positive, number. A list is used for translation of
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20 * negative to positive numbers.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22 * The size of a block is a multiple of a page size, normally the page size of
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 * the device the file is on. Most blocks are 1 page long. A Block of multiple
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 * pages is used for a line that does not fit in a single page.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 * Each block can be in memory and/or in a file. The block stays in memory
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 * as long as it is locked. If it is no longer locked it can be swapped out to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 * the file. It is only written to the file if it has been changed.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 * Under normal operation the file is created when opening the memory file and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 * deleted when closing the memory file. Only with recovery an existing memory
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32 * file is opened.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 #if defined MSDOS || defined(WIN32) || defined(_WIN64)
714
0f9f4761ad9c updated for version 7.0216
vimboss
parents: 630
diff changeset
36 # include "vimio.h" /* for lseek(), must be before vim.h */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 #include "vim.h"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41 #ifdef HAVE_FCNTL_H
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42 # include <fcntl.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 * 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
47 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 #ifdef HAVE_ST_BLKSIZE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 # define STATFS stat
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 # define F_BSIZE st_blksize
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 # define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53 # ifdef HAVE_SYS_STATFS_H
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 # include <sys/statfs.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 # define STATFS statfs
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 # define F_BSIZE f_bsize
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 # ifdef __MINT__ /* do we still need this? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58 # define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64 * for Amiga Dos 2.0x we use Flush
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66 #ifdef AMIGA
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 # ifdef FEAT_ARP
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68 extern int dos2; /* this is in os_amiga.c */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
69 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70 # ifdef SASC
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 # include <proto/dos.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72 # include <ios1.h> /* for chkufb() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 #define MEMFILE_PAGE_SIZE 4096 /* default page size */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78 static long_u total_mem_used = 0; /* total memory used for memfiles */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 static int dont_release = FALSE; /* don't release blocks */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81 static void mf_ins_hash __ARGS((memfile_T *, bhdr_T *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82 static void mf_rem_hash __ARGS((memfile_T *, bhdr_T *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83 static bhdr_T *mf_find_hash __ARGS((memfile_T *, blocknr_T));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 static void mf_ins_used __ARGS((memfile_T *, bhdr_T *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85 static void mf_rem_used __ARGS((memfile_T *, bhdr_T *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 static bhdr_T *mf_release __ARGS((memfile_T *, int));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 static bhdr_T *mf_alloc_bhdr __ARGS((memfile_T *, int));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88 static void mf_free_bhdr __ARGS((bhdr_T *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89 static void mf_ins_free __ARGS((memfile_T *, bhdr_T *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90 static bhdr_T *mf_rem_free __ARGS((memfile_T *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91 static int mf_read __ARGS((memfile_T *, bhdr_T *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92 static int mf_write __ARGS((memfile_T *, bhdr_T *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
93 static int mf_trans_add __ARGS((memfile_T *, bhdr_T *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94 static void mf_do_open __ARGS((memfile_T *, char_u *, int));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
96 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97 * The functions for using a memfile:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
99 * mf_open() open a new or existing memfile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
100 * mf_open_file() open a swap file for an existing memfile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
101 * mf_close() close (and delete) a memfile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
102 * mf_new() create a new block in a memfile and lock it
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
103 * mf_get() get an existing block and lock it
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
104 * mf_put() unlock a block, may be marked for writing
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
105 * mf_free() remove a block
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106 * mf_sync() sync changed parts of memfile to disk
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
107 * mf_release_all() release as much memory as possible
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 * mf_trans_del() may translate negative to positive block number
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
109 * mf_fullname() make file name full path (use before first :cd)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113 * Open an existing or new memory block file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115 * fname: name of file to use (NULL means no file at all)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116 * Note: fname must have been allocated, it is not copied!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 * If opening the file fails, fname is freed.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
118 * flags: flags for open() call
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
119 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120 * If fname != NULL and file cannot be opened, fail.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122 * return value: identifier for this memory block file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
124 memfile_T *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
125 mf_open(fname, flags)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126 char_u *fname;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127 int flags;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130 int i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
131 off_t size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
132 #if defined(STATFS) && defined(UNIX) && !defined(__QNX__)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
133 # define USE_FSTATFS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
134 struct STATFS stf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
135 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
136
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
137 if ((mfp = (memfile_T *)alloc((unsigned)sizeof(memfile_T))) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
138 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
139
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
140 if (fname == NULL) /* no file for this memfile, use memory only */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
141 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
142 mfp->mf_fname = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
143 mfp->mf_ffname = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
144 mfp->mf_fd = -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
145 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
146 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
147 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
148 mf_do_open(mfp, fname, flags); /* try to open the file */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
149
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
150 /* if the file cannot be opened, return here */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
151 if (mfp->mf_fd < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
152 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
153 vim_free(mfp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
154 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
155 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
156 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
157
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
158 mfp->mf_free_first = NULL; /* free list is empty */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
159 mfp->mf_used_first = NULL; /* used list is empty */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
160 mfp->mf_used_last = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
161 mfp->mf_dirty = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
162 mfp->mf_used_count = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
163 for (i = 0; i < MEMHASHSIZE; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
164 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
165 mfp->mf_hash[i] = NULL; /* hash lists are empty */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
166 mfp->mf_trans[i] = NULL; /* trans lists are empty */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
167 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
168 mfp->mf_page_size = MEMFILE_PAGE_SIZE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
169
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
170 #ifdef USE_FSTATFS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
171 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
172 * Try to set the page size equal to the block size of the device.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
173 * Speeds up I/O a lot.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
174 * When recovering, the actual block size will be retrieved from block 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
175 * in ml_recover(). The size used here may be wrong, therefore
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
176 * mf_blocknr_max must be rounded up.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
177 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
178 if (mfp->mf_fd >= 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
179 && fstatfs(mfp->mf_fd, &stf, sizeof(struct statfs), 0) == 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
180 && stf.F_BSIZE >= MIN_SWAP_PAGE_SIZE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
181 && stf.F_BSIZE <= MAX_SWAP_PAGE_SIZE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
182 mfp->mf_page_size = stf.F_BSIZE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
183 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
184
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
185 if (mfp->mf_fd < 0 || (flags & (O_TRUNC|O_EXCL))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
186 || (size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
187 mfp->mf_blocknr_max = 0; /* no file or empty file */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
188 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
189 mfp->mf_blocknr_max = (blocknr_T)((size + mfp->mf_page_size - 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
190 / mfp->mf_page_size);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
191 mfp->mf_blocknr_min = -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
192 mfp->mf_neg_count = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
193 mfp->mf_infile_count = mfp->mf_blocknr_max;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
194 mfp->mf_used_count_max = p_mm * 1024 / mfp->mf_page_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
195
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
196 return mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
197 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
198
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
199 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
200 * Open a file for an existing memfile. Used when updatecount set from 0 to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
201 * some value.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
202 * If the file already exists, this fails.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
203 * "fname" is the name of file to use (NULL means no file at all)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
204 * Note: "fname" must have been allocated, it is not copied! If opening the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
205 * file fails, "fname" is freed.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
206 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
207 * return value: FAIL if file could not be opened, OK otherwise
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
208 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
209 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
210 mf_open_file(mfp, fname)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
211 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
212 char_u *fname;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
213 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
214 mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); /* try to open the file */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
215
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
216 if (mfp->mf_fd < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
217 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
218
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
219 mfp->mf_dirty = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
220 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
221 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
222
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
223 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
224 * close a memory file and delete the associated file if 'del_file' is TRUE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
225 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
226 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
227 mf_close(mfp, del_file)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
228 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
229 int del_file;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
230 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
231 bhdr_T *hp, *nextp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
232 NR_TRANS *tp, *tpnext;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
233 int i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
234
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
235 if (mfp == NULL) /* safety check */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
236 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
237 if (mfp->mf_fd >= 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
238 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
239 if (close(mfp->mf_fd) < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
240 EMSG(_(e_swapclose));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
241 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
242 if (del_file && mfp->mf_fname != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
243 mch_remove(mfp->mf_fname);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
244 /* free entries in used list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
245 for (hp = mfp->mf_used_first; hp != NULL; hp = nextp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
246 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
247 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
248 nextp = hp->bh_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
249 mf_free_bhdr(hp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
250 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
251 while (mfp->mf_free_first != NULL) /* free entries in free list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
252 vim_free(mf_rem_free(mfp));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
253 for (i = 0; i < MEMHASHSIZE; ++i) /* free entries in trans lists */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
254 for (tp = mfp->mf_trans[i]; tp != NULL; tp = tpnext)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
255 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
256 tpnext = tp->nt_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
257 vim_free(tp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
258 }
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
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
268 mf_close_file(buf, getlines)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
269 buf_T *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
270 int getlines; /* get all lines into memory? */
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;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
276 if (mfp == NULL || mfp->mf_fd < 0) /* nothing to close */
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 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
281 /* get all blocks in memory by accessing all lines (clumsy!) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
282 dont_release = TRUE;
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);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
285 dont_release = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
286 /* TODO: should check if all blocks are really in core */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
287 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
288
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
289 if (close(mfp->mf_fd) < 0) /* close the file */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
290 EMSG(_(e_swapclose));
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 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
295 mch_remove(mfp->mf_fname); /* delete the swap file */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
296 vim_free(mfp->mf_fname);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
297 vim_free(mfp->mf_ffname);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
298 mfp->mf_fname = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
299 mfp->mf_ffname = NULL;
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
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
303 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
304 * 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
305 * and the size it indicates differs from what was guessed.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
306 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
307 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
308 mf_new_page_size(mfp, new_size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
309 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
310 unsigned new_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
311 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
312 /* Correct the memory used for block 0 to the new size, because it will be
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
313 * freed with that size later on. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
314 total_mem_used += new_size - mfp->mf_page_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
315 mfp->mf_page_size = new_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
316 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
317
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
318 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
319 * get a new block
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
320 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
321 * negative: TRUE if negative block number desired (data block)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
322 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
323 bhdr_T *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
324 mf_new(mfp, negative, page_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
325 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
326 int negative;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
327 int page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
328 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
329 bhdr_T *hp; /* new bhdr_T */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
330 bhdr_T *freep; /* first block in free list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
331 char_u *p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
332
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
333 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
334 * If we reached the maximum size for the used memory blocks, release one
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
335 * 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
336 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
337 hp = mf_release(mfp, page_count);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
338
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
339 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
340 * Decide on the number to use:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
341 * If there is a free block, use its number.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
342 * Otherwise use mf_block_min for a negative number, mf_block_max for
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
343 * a positive number.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
344 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
345 freep = mfp->mf_free_first;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
346 if (!negative && freep != NULL && freep->bh_page_count >= page_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
347 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
348 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
349 * If the block in the free list has more pages, take only the number
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
350 * of pages needed and allocate a new bhdr_T with data
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
351 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
352 * If the number of pages matches and mf_release did not return a bhdr_T,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
353 * use the bhdr_T from the free list and allocate the data
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
354 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
355 * If the number of pages matches and mf_release returned a bhdr_T,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
356 * just use the number and free the bhdr_T from the free list
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
357 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
358 if (freep->bh_page_count > page_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
359 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
360 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
361 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
362 hp->bh_bnum = freep->bh_bnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
363 freep->bh_bnum += page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
364 freep->bh_page_count -= page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
365 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
366 else if (hp == NULL) /* need to allocate memory for this block */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
367 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
368 if ((p = (char_u *)alloc(mfp->mf_page_size * page_count)) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
369 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
370 hp = mf_rem_free(mfp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
371 hp->bh_data = p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
372 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
373 else /* use the number, remove entry from free list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
374 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
375 freep = mf_rem_free(mfp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
376 hp->bh_bnum = freep->bh_bnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
377 vim_free(freep);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
378 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
379 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
380 else /* get a new number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
381 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
382 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
383 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
384 if (negative)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
385 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
386 hp->bh_bnum = mfp->mf_blocknr_min--;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
387 mfp->mf_neg_count++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
388 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
389 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
390 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
391 hp->bh_bnum = mfp->mf_blocknr_max;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
392 mfp->mf_blocknr_max += page_count;
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 hp->bh_flags = BH_LOCKED | BH_DIRTY; /* new block is always dirty */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
396 mfp->mf_dirty = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
397 hp->bh_page_count = page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
398 mf_ins_used(mfp, hp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
399 mf_ins_hash(mfp, hp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
400
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
401 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
402 * Init the data to all zero, to avoid reading uninitialized data.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
403 * This also avoids that the passwd file ends up in the swap file!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
404 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
405 (void)vim_memset((char *)(hp->bh_data), 0, (size_t)mfp->mf_page_size);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
406
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
407 return hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
408 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
409
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
410 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
411 * get existing block 'nr' with 'page_count' pages
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
412 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
413 * Note: The caller should first check a negative nr with mf_trans_del()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
414 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
415 bhdr_T *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
416 mf_get(mfp, nr, page_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
417 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
418 blocknr_T nr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
419 int page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
420 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
421 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
422 /* doesn't exist */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
423 if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
424 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
425
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
426 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
427 * see if it is in the cache
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
428 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
429 hp = mf_find_hash(mfp, nr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
430 if (hp == NULL) /* not in the hash list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
431 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
432 if (nr < 0 || nr >= mfp->mf_infile_count) /* can't be in the file */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
433 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
434
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
435 /* could check here if the block is in the free list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
436
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
437 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
438 * Check if we need to flush an existing block.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
439 * If so, use that block.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
440 * If not, allocate a new block.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
441 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
442 hp = mf_release(mfp, page_count);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
443 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
444 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
445
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
446 hp->bh_bnum = nr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
447 hp->bh_flags = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
448 hp->bh_page_count = page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
449 if (mf_read(mfp, hp) == FAIL) /* cannot read the block! */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
450 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
451 mf_free_bhdr(hp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
452 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
453 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
454 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
455 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
456 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
457 mf_rem_used(mfp, hp); /* remove from list, insert in front below */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
458 mf_rem_hash(mfp, hp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
459 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
460
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
461 hp->bh_flags |= BH_LOCKED;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
462 mf_ins_used(mfp, hp); /* put in front of used list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
463 mf_ins_hash(mfp, hp); /* put in front of hash list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
464
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
465 return hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
466 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
467
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
468 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
469 * release the block *hp
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
470 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
471 * dirty: Block must be written to file later
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
472 * infile: Block should be in file (needed for recovery)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
473 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
474 * no return value, function cannot fail
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
475 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
476 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
477 mf_put(mfp, hp, dirty, infile)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
478 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
479 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
480 int dirty;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
481 int infile;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
482 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
483 int flags;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
484
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
485 flags = hp->bh_flags;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
486
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
487 if ((flags & BH_LOCKED) == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
488 EMSG(_("E293: block was not locked"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
489 flags &= ~BH_LOCKED;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
490 if (dirty)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
491 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
492 flags |= BH_DIRTY;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
493 mfp->mf_dirty = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
494 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
495 hp->bh_flags = flags;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
496 if (infile)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
497 mf_trans_add(mfp, hp); /* may translate negative in positive nr */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
498 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
499
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
500 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
501 * 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
502 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
503 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
504 mf_free(mfp, hp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
505 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
506 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
507 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
508 vim_free(hp->bh_data); /* free the memory */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
509 mf_rem_hash(mfp, hp); /* get *hp out of the hash list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
510 mf_rem_used(mfp, hp); /* get *hp out of the used list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
511 if (hp->bh_bnum < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
512 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
513 vim_free(hp); /* don't want negative numbers in free list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
514 mfp->mf_neg_count--;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
515 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
516 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
517 mf_ins_free(mfp, hp); /* put *hp in the free list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
518 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
519
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
520 #if defined(__MORPHOS__)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
521 /* function is missing in MorphOS libnix version */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
522 extern unsigned long *__stdfiledes;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
523
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
524 static unsigned long
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
525 fdtofh(int filedescriptor)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
526 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
527 return __stdfiledes[filedescriptor];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
528 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
529 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
530
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
531 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
532 * Sync the memory file *mfp to disk.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
533 * Flags:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
534 * MFS_ALL If not given, blocks with negative numbers are not synced,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
535 * even when they are dirty!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
536 * MFS_STOP Stop syncing when a character becomes available, but sync at
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
537 * least one block.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
538 * MFS_FLUSH Make sure buffers are flushed to disk, so they will survive a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
539 * system crash.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
540 * MFS_ZERO Only write block 0.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
541 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
542 * Return FAIL for failure, OK otherwise
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
543 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
544 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
545 mf_sync(mfp, flags)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
546 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
547 int flags;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
548 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
549 int status;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
550 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
551 #if defined(SYNC_DUP_CLOSE) && !defined(MSDOS)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
552 int fd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
553 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
554 int got_int_save = got_int;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
555
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
556 if (mfp->mf_fd < 0) /* there is no file, nothing to do */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
557 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
558 mfp->mf_dirty = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
559 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
560 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
561
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
562 /* Only a CTRL-C while writing will break us here, not one typed
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
563 * previously. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
564 got_int = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
565
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
566 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
567 * sync from last to first (may reduce the probability of an inconsistent
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
568 * file) If a write fails, it is very likely caused by a full filesystem.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
569 * Then we only try to write blocks within the existing file. If that also
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
570 * fails then we give up.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
571 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
572 status = OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
573 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
574 if (((flags & MFS_ALL) || hp->bh_bnum >= 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
575 && (hp->bh_flags & BH_DIRTY)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
576 && (status == OK || (hp->bh_bnum >= 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
577 && hp->bh_bnum < mfp->mf_infile_count)))
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_ZERO) && hp->bh_bnum != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
580 continue;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
581 if (mf_write(mfp, hp) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
582 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
583 if (status == FAIL) /* double error: quit syncing */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
584 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
585 status = FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
586 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
587 if (flags & MFS_STOP)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
588 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
589 /* Stop when char available now. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
590 if (ui_char_avail())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
591 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
592 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
593 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
594 ui_breakcheck();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
595 if (got_int)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
596 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
597 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
598
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
599 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
600 * If the whole list is flushed, the memfile is not dirty anymore.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
601 * 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
602 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
603 if (hp == NULL || status == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
604 mfp->mf_dirty = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
605
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
606 if ((flags & MFS_FLUSH) && *p_sws != NUL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
607 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
608 #if defined(UNIX)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
609 # ifdef HAVE_FSYNC
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
610 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
611 * most Unixes have the very useful fsync() function, just what we need.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
612 * However, with OS/2 and EMX it is also available, but there are
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
613 * reports of bad problems with it (a bug in HPFS.IFS).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
614 * So we disable use of it here in case someone tries to be smart
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
615 * and changes os_os2_cfg.h... (even though there is no __EMX__ test
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
616 * in the #if, as __EMX__ does not have sync(); we hope for a timely
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
617 * sync from the system itself).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
618 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
619 # if defined(__EMX__)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
620 error "Dont use fsync with EMX! Read emxdoc.doc or emxfix01.doc for info."
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
621 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
622 if (STRCMP(p_sws, "fsync") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
623 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
624 if (fsync(mfp->mf_fd))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
625 status = FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
626 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
627 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
628 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
629 /* OpenNT is strictly POSIX (Benzinger) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
630 /* Tandem/Himalaya NSK-OSS doesn't have sync() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
631 # if defined(__OPENNT) || defined(__TANDEM)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
632 fflush(NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
633 # else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
634 sync();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
635 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
636 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
637 #ifdef VMS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
638 if (STRCMP(p_sws, "fsync") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
639 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
640 if (fsync(mfp->mf_fd))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
641 status = FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
642 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
643 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
644 #ifdef MSDOS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
645 if (_dos_commit(mfp->mf_fd))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
646 status = FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
647 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
648 # ifdef SYNC_DUP_CLOSE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
649 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
650 * Win32 is a bit more work: Duplicate the file handle and close it.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
651 * This should flush the file to disk.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
652 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
653 if ((fd = dup(mfp->mf_fd)) >= 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
654 close(fd);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
655 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
656 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
657 #ifdef AMIGA
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
658 # ifdef __AROS__
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
659 if (fsync(mfp->mf_fd) != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
660 status = FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
661 # else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
662 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
663 * Flush() only exists for AmigaDos 2.0.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
664 * For 1.3 it should be done with close() + open(), but then the risk
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
665 * is that the open() may fail and lose the file....
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
666 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
667 # ifdef FEAT_ARP
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
668 if (dos2)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
669 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
670 # ifdef SASC
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
671 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
672 struct UFB *fp = chkufb(mfp->mf_fd);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
673
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
674 if (fp != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
675 Flush(fp->ufbfh);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
676 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
677 # else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
678 # if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
679 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
680 # if defined(__GNUC__) && !defined(__MORPHOS__)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
681 /* Have function (in libnix at least),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
682 * but ain't got no prototype anywhere. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
683 extern unsigned long fdtofh(int filedescriptor);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
684 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
685 BPTR fh = (BPTR)fdtofh(mfp->mf_fd);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
686
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
687 if (fh != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
688 Flush(fh);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
689 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
690 # else /* assume Manx */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
691 Flush(_devtab[mfp->mf_fd].fd);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
692 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
693 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
694 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
695 #endif /* AMIGA */
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 got_int |= got_int_save;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
699
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
700 return status;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
701 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
702
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
703 /*
630
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
704 * 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
705 * 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
706 * created swapfile.
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
707 */
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
708 void
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
709 mf_set_dirty(mfp)
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
710 memfile_T *mfp;
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
711 {
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
712 bhdr_T *hp;
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
713
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
714 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
715 if (hp->bh_bnum > 0)
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
716 hp->bh_flags |= BH_DIRTY;
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
717 mfp->mf_dirty = TRUE;
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
718 }
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
719
52c87a746f4a updated for version 7.0181
vimboss
parents: 557
diff changeset
720 /*
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
721 * insert block *hp in front of hashlist of memfile *mfp
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
722 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
723 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
724 mf_ins_hash(mfp, hp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
725 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
726 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
727 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
728 bhdr_T *hhp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
729 int hash;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
730
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
731 hash = MEMHASH(hp->bh_bnum);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
732 hhp = mfp->mf_hash[hash];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
733 hp->bh_hash_next = hhp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
734 hp->bh_hash_prev = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
735 if (hhp != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
736 hhp->bh_hash_prev = hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
737 mfp->mf_hash[hash] = hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
738 }
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 * remove block *hp from hashlist of memfile list *mfp
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
742 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
743 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
744 mf_rem_hash(mfp, hp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
745 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
746 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
747 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
748 if (hp->bh_hash_prev == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
749 mfp->mf_hash[MEMHASH(hp->bh_bnum)] = hp->bh_hash_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
750 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
751 hp->bh_hash_prev->bh_hash_next = hp->bh_hash_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
752
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
753 if (hp->bh_hash_next)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
754 hp->bh_hash_next->bh_hash_prev = hp->bh_hash_prev;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
755 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
756
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
757 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
758 * look in hash lists of memfile *mfp for block header with number 'nr'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
759 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
760 static bhdr_T *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
761 mf_find_hash(mfp, nr)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
762 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
763 blocknr_T nr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
764 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
765 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
766
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
767 for (hp = mfp->mf_hash[MEMHASH(nr)]; hp != NULL; hp = hp->bh_hash_next)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
768 if (hp->bh_bnum == nr)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
769 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
770 return hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
771 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
772
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
773 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
774 * insert block *hp in front of used list of memfile *mfp
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
775 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
776 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
777 mf_ins_used(mfp, hp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
778 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
779 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
780 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
781 hp->bh_next = mfp->mf_used_first;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
782 mfp->mf_used_first = hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
783 hp->bh_prev = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
784 if (hp->bh_next == NULL) /* list was empty, adjust last pointer */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
785 mfp->mf_used_last = hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
786 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
787 hp->bh_next->bh_prev = hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
788 mfp->mf_used_count += hp->bh_page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
789 total_mem_used += hp->bh_page_count * mfp->mf_page_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
790 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
791
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
792 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
793 * remove block *hp from used list of memfile *mfp
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
794 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
795 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
796 mf_rem_used(mfp, hp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
797 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
798 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
799 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
800 if (hp->bh_next == NULL) /* last block in used list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
801 mfp->mf_used_last = hp->bh_prev;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
802 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
803 hp->bh_next->bh_prev = hp->bh_prev;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
804 if (hp->bh_prev == NULL) /* first block in used list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
805 mfp->mf_used_first = hp->bh_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
806 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
807 hp->bh_prev->bh_next = hp->bh_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
808 mfp->mf_used_count -= hp->bh_page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
809 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
810 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
811
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
812 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
813 * Release the least recently used block from the used list if the number
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
814 * of used memory blocks gets to big.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
815 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
816 * Return the block header to the caller, including the memory block, so
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
817 * it can be re-used. Make sure the page_count is right.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
818 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
819 static bhdr_T *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
820 mf_release(mfp, page_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
821 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
822 int page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
823 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
824 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
825 int need_release;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
826 buf_T *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
827
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
828 /* don't release while in mf_close_file() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
829 if (dont_release)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
830 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
831
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
832 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
833 * Need to release a block if the number of blocks for this memfile is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
834 * higher than the maximum or total memory used is over 'maxmemtot'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
835 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
836 need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
837 || (total_mem_used >> 10) >= (long_u)p_mmt);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
838
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
839 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
840 * 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
841 * high.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
842 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
843 if (mfp->mf_fd < 0 && need_release && p_uc)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
844 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
845 /* find for which buffer this memfile is */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
846 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
847 if (buf->b_ml.ml_mfp == mfp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
848 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
849 if (buf != NULL && buf->b_may_swap)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
850 ml_open_file(buf);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
851 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
852
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
853 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
854 * don't release a block if
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
855 * there is no file for this memfile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
856 * or
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
857 * the number of blocks for this memfile is lower than the maximum
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
858 * and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
859 * total memory used is not up to 'maxmemtot'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
860 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
861 if (mfp->mf_fd < 0 || !need_release)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
862 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
863
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
864 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
865 if (!(hp->bh_flags & BH_LOCKED))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
866 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
867 if (hp == NULL) /* not a single one that can be released */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
868 return NULL;
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 * If the block is dirty, write it.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
872 * If the write fails we don't free it.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
873 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
874 if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
875 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
876
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
877 mf_rem_used(mfp, hp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
878 mf_rem_hash(mfp, hp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
879
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
880 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
881 * 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
882 * right
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
883 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
884 if (hp->bh_page_count != page_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
885 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
886 vim_free(hp->bh_data);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
887 if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
888 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
889 vim_free(hp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
890 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
891 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
892 hp->bh_page_count = page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
893 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
894 return hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
895 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
896
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
897 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
898 * release as many blocks as possible
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
899 * Used in case of out of memory
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
900 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
901 * return TRUE if any memory was released
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
902 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
903 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
904 mf_release_all()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
905 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
906 buf_T *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
907 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
908 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
909 int retval = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
910
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
911 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
912 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
913 mfp = buf->b_ml.ml_mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
914 if (mfp != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
915 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
916 /* If no swap file yet, may open one */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
917 if (mfp->mf_fd < 0 && buf->b_may_swap)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
918 ml_open_file(buf);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
919
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
920 /* only if there is a swapfile */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
921 if (mfp->mf_fd >= 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
922 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
923 for (hp = mfp->mf_used_last; hp != NULL; )
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
924 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
925 if (!(hp->bh_flags & BH_LOCKED)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
926 && (!(hp->bh_flags & BH_DIRTY)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
927 || mf_write(mfp, hp) != FAIL))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
928 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
929 mf_rem_used(mfp, hp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
930 mf_rem_hash(mfp, hp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
931 mf_free_bhdr(hp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
932 hp = mfp->mf_used_last; /* re-start, list was changed */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
933 retval = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
934 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
935 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
936 hp = hp->bh_prev;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
937 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
938 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
939 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
940 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
941 return retval;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
942 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
943
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
944 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
945 * Allocate a block header and a block of memory for it
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
946 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
947 static bhdr_T *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
948 mf_alloc_bhdr(mfp, page_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
949 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
950 int page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
951 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
952 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
953
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
954 if ((hp = (bhdr_T *)alloc((unsigned)sizeof(bhdr_T))) != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
955 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
956 if ((hp->bh_data = (char_u *)alloc(mfp->mf_page_size * page_count))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
957 == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
958 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
959 vim_free(hp); /* not enough memory */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
960 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
961 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
962 hp->bh_page_count = page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
963 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
964 return hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
965 }
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 * Free a block header and the block of memory for it
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
969 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
970 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
971 mf_free_bhdr(hp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
972 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
973 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
974 vim_free(hp->bh_data);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
975 vim_free(hp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
976 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
977
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
978 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
979 * insert entry *hp in the free list
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
980 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
981 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
982 mf_ins_free(mfp, hp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
983 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
984 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
985 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
986 hp->bh_next = mfp->mf_free_first;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
987 mfp->mf_free_first = hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
988 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
989
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
990 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
991 * remove the first entry from the free list and return a pointer to it
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
992 * Note: caller must check that mfp->mf_free_first is not NULL!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
993 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
994 static bhdr_T *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
995 mf_rem_free(mfp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
996 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
997 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
998 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
999
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1000 hp = mfp->mf_free_first;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1001 mfp->mf_free_first = hp->bh_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1002 return hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1003 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1004
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1005 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1006 * read a block from disk
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1007 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1008 * Return FAIL for failure, OK otherwise
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1009 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1010 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1011 mf_read(mfp, hp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1012 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1013 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1014 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1015 off_t offset;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1016 unsigned page_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1017 unsigned size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1018
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1019 if (mfp->mf_fd < 0) /* there is no file, can't read */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1020 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1021
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1022 page_size = mfp->mf_page_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1023 offset = (off_t)page_size * hp->bh_bnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1024 size = page_size * hp->bh_page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1025 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1026 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1027 EMSG(_("E294: Seek error in swap file read"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1028 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1029 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1030 if ((unsigned)vim_read(mfp->mf_fd, hp->bh_data, size) != size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1031 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1032 EMSG(_("E295: Read error in swap file"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1033 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1034 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1035 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1036 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1037
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1038 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1039 * write a block to disk
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1040 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1041 * Return FAIL for failure, OK otherwise
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1042 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1043 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1044 mf_write(mfp, hp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1045 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1046 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1047 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1048 off_t offset; /* offset in the file */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1049 blocknr_T nr; /* block nr which is being written */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1050 bhdr_T *hp2;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1051 unsigned page_size; /* number of bytes in a page */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1052 unsigned page_count; /* number of pages written */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1053 unsigned size; /* number of bytes written */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1054
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1055 if (mfp->mf_fd < 0) /* there is no file, can't write */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1056 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1057
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1058 if (hp->bh_bnum < 0) /* must assign file block number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1059 if (mf_trans_add(mfp, hp) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1060 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1061
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1062 page_size = mfp->mf_page_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1063
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1064 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1065 * 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
1066 * to extend the file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1067 * If block 'mf_infile_count' is not in the hash list, it has been
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1068 * freed. Fill the space in the file with data from the current block.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1069 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1070 for (;;)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1071 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1072 nr = hp->bh_bnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1073 if (nr > mfp->mf_infile_count) /* beyond end of file */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1074 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1075 nr = mfp->mf_infile_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1076 hp2 = mf_find_hash(mfp, nr); /* NULL catched below */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1077 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1078 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1079 hp2 = hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1080
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1081 offset = (off_t)page_size * nr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1082 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1083 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1084 EMSG(_("E296: Seek error in swap file write"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1085 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1086 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1087 if (hp2 == NULL) /* freed block, fill with dummy data */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1088 page_count = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1089 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1090 page_count = hp2->bh_page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1091 size = page_size * page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1092 if ((unsigned)vim_write(mfp->mf_fd,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1093 (hp2 == NULL ? hp : hp2)->bh_data, size) != size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1094 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1095 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1096 * Avoid repeating the error message, this mostly happens when the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1097 * disk is full. We give the message again only after a succesful
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1098 * write or when hitting a key. We keep on trying, in case some
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1099 * space becomes available.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1100 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1101 if (!did_swapwrite_msg)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1102 EMSG(_("E297: Write error in swap file"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1103 did_swapwrite_msg = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1104 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1105 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1106 did_swapwrite_msg = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1107 if (hp2 != NULL) /* written a non-dummy block */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1108 hp2->bh_flags &= ~BH_DIRTY;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1109 /* appended to the file */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1110 if (nr + (blocknr_T)page_count > mfp->mf_infile_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1111 mfp->mf_infile_count = nr + page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1112 if (nr == hp->bh_bnum) /* written the desired block */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1113 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1114 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1115 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1116 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1117
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1118 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1119 * Make block number for *hp positive and add it to the translation list
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1120 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1121 * Return FAIL for failure, OK otherwise
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1122 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1123 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1124 mf_trans_add(mfp, hp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1125 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1126 bhdr_T *hp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1127 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1128 bhdr_T *freep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1129 blocknr_T new_bnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1130 int hash;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1131 NR_TRANS *np;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1132 int page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1133
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1134 if (hp->bh_bnum >= 0) /* it's already positive */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1135 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1136
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1137 if ((np = (NR_TRANS *)alloc((unsigned)sizeof(NR_TRANS))) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1138 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1139
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1140 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1141 * get a new number for the block.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1142 * If the first item in the free list has sufficient pages, use its number
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1143 * Otherwise use mf_blocknr_max.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1144 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1145 freep = mfp->mf_free_first;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1146 page_count = hp->bh_page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1147 if (freep != NULL && freep->bh_page_count >= page_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1148 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1149 new_bnum = freep->bh_bnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1150 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1151 * If the page count of the free block was larger, recude it.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1152 * If the page count matches, remove the block from the free list
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1153 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1154 if (freep->bh_page_count > page_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1155 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1156 freep->bh_bnum += page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1157 freep->bh_page_count -= page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1158 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1159 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1160 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1161 freep = mf_rem_free(mfp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1162 vim_free(freep);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1163 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1164 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1165 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1166 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1167 new_bnum = mfp->mf_blocknr_max;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1168 mfp->mf_blocknr_max += page_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1169 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1170
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1171 np->nt_old_bnum = hp->bh_bnum; /* adjust number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1172 np->nt_new_bnum = new_bnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1173
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1174 mf_rem_hash(mfp, hp); /* remove from old hash list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1175 hp->bh_bnum = new_bnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1176 mf_ins_hash(mfp, hp); /* insert in new hash list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1177
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1178 hash = MEMHASH(np->nt_old_bnum); /* insert in trans list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1179 np->nt_next = mfp->mf_trans[hash];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1180 mfp->mf_trans[hash] = np;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1181 if (np->nt_next != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1182 np->nt_next->nt_prev = np;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1183 np->nt_prev = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1184
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1185 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1186 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1187
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1188 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1189 * Lookup a tranlation from the trans lists and delete the entry
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1190 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1191 * Return the positive new number when found, the old number when not found
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1192 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1193 blocknr_T
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1194 mf_trans_del(mfp, old_nr)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1195 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1196 blocknr_T old_nr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1197 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1198 int hash;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1199 NR_TRANS *np;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1200 blocknr_T new_bnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1201
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1202 hash = MEMHASH(old_nr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1203 for (np = mfp->mf_trans[hash]; np != NULL; np = np->nt_next)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1204 if (np->nt_old_bnum == old_nr)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1205 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1206 if (np == NULL) /* not found */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1207 return old_nr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1208
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1209 mfp->mf_neg_count--;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1210 new_bnum = np->nt_new_bnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1211 if (np->nt_prev != NULL) /* remove entry from the trans list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1212 np->nt_prev->nt_next = np->nt_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1213 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1214 mfp->mf_trans[hash] = np->nt_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1215 if (np->nt_next != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1216 np->nt_next->nt_prev = np->nt_prev;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1217 vim_free(np);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1218
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1219 return new_bnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1220 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1221
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1222 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1223 * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1224 * Only called when creating or renaming the swapfile. Either way it's a new
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1225 * name so we must work out the full path name.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1226 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1227 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1228 mf_set_ffname(mfp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1229 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1230 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1231 mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1232 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1233
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1234 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1235 * Make the name of the file used for the memfile a full path.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1236 * Used before doing a :cd
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1237 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1238 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1239 mf_fullname(mfp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1240 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1241 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1242 if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1243 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1244 vim_free(mfp->mf_fname);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1245 mfp->mf_fname = mfp->mf_ffname;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1246 mfp->mf_ffname = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1247 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1248 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1249
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1250 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1251 * return TRUE if there are any translations pending for 'mfp'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1252 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1253 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1254 mf_need_trans(mfp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1255 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1256 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1257 return (mfp->mf_fname != NULL && mfp->mf_neg_count > 0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1258 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1259
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1260 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1261 * Open a swap file for a memfile.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1262 * The "fname" must be in allocated memory, and is consumed (also when an
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1263 * error occurs).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1264 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1265 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1266 mf_do_open(mfp, fname, flags)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1267 memfile_T *mfp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1268 char_u *fname;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1269 int flags; /* flags for open() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1270 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1271 #ifdef HAVE_LSTAT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1272 struct stat sb;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1273 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1274
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1275 mfp->mf_fname = fname;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1276
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1277 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1278 * Get the full path name before the open, because this is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1279 * not possible after the open on the Amiga.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1280 * fname cannot be NameBuff, because it must have been allocated.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1281 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1282 mf_set_ffname(mfp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1283 #if defined(MSDOS) || defined(MSWIN) || defined(RISCOS)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1284 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1285 * A ":!cd e:xxx" may change the directory without us knowning, use the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1286 * full pathname always. Careful: This frees fname!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1287 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1288 mf_fullname(mfp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1289 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1290
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1291 #ifdef HAVE_LSTAT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1292 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1293 * Extra security check: When creating a swap file it really shouldn't
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1294 * exist yet. If there is a symbolic link, this is most likely an attack.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1295 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1296 if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1297 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1298 mfp->mf_fd = -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1299 EMSG(_("E300: Swap file already exists (symlink attack?)"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1300 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1301 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1302 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1303 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1304 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1305 * try to open the file
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1306 */
557
862863033fdd updated for version 7.0158
vimboss
parents: 7
diff changeset
1307 flags |= O_EXTRA | O_NOFOLLOW;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1308 #ifdef WIN32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1309 /* Prevent handle inheritance that cause problems with Cscope
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1310 * (swap file may not be deleted if cscope connection was open after
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1311 * the file) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1312 flags |= O_NOINHERIT;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1313 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1314 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1315 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1316
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1317 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1318 * If the file cannot be opened, use memory only
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1319 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1320 if (mfp->mf_fd < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1321 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1322 vim_free(mfp->mf_fname);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1323 vim_free(mfp->mf_ffname);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1324 mfp->mf_fname = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1325 mfp->mf_ffname = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1326 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1327 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1328 mch_hide(mfp->mf_fname); /* try setting the 'hidden' flag */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1329 }