1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
11 * memfile.c: Contains the functions for handling blocks of memory which can
12 * be stored in a file. This is the implementation of a sort of virtual memory.
14 * A memfile consists of a sequence of blocks. The blocks numbered from 0
15 * upwards have been assigned a place in the actual file. The block number
16 * is equal to the page number in the file. The
17 * blocks with negative numbers are currently in memory only. They can be
18 * assigned a place in the file when too much memory is being used. At that
19 * moment they get a new, positive, number. A list is used for translation of
20 * negative to positive numbers.
22 * The size of a block is a multiple of a page size, normally the page size of
23 * the device the file is on. Most blocks are 1 page long. A Block of multiple
24 * pages is used for a line that does not fit in a single page.
26 * Each block can be in memory and/or in a file. The block stays in memory
27 * as long as it is locked. If it is no longer locked it can be swapped out to
28 * the file. It is only written to the file if it has been changed.
30 * Under normal operation the file is created when opening the memory file and
31 * deleted when closing the memory file. Only with recovery an existing memory
35 #if defined MSDOS || defined(WIN32) || defined(_WIN64)
36 # include "vimio.h" /* for lseek(), must be before vim.h */
46 * Some systems have the page size in statfs.f_bsize, some in stat.st_blksize
48 #ifdef HAVE_ST_BLKSIZE
50 # define F_BSIZE st_blksize
51 # define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
53 # ifdef HAVE_SYS_STATFS_H
54 # include <sys/statfs.h>
55 # define STATFS statfs
56 # define F_BSIZE f_bsize
57 # ifdef __MINT__ /* do we still need this? */
58 # define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
64 * for Amiga Dos 2.0x we use Flush
68 extern int dos2
; /* this is in os_amiga.c */
71 # include <proto/dos.h>
72 # include <ios1.h> /* for chkufb() */
76 #define MEMFILE_PAGE_SIZE 4096 /* default page size */
78 static long_u total_mem_used
= 0; /* total memory used for memfiles */
79 static int dont_release
= FALSE
; /* don't release blocks */
81 static void mf_ins_hash
__ARGS((memfile_T
*, bhdr_T
*));
82 static void mf_rem_hash
__ARGS((memfile_T
*, bhdr_T
*));
83 static bhdr_T
*mf_find_hash
__ARGS((memfile_T
*, blocknr_T
));
84 static void mf_ins_used
__ARGS((memfile_T
*, bhdr_T
*));
85 static void mf_rem_used
__ARGS((memfile_T
*, bhdr_T
*));
86 static bhdr_T
*mf_release
__ARGS((memfile_T
*, int));
87 static bhdr_T
*mf_alloc_bhdr
__ARGS((memfile_T
*, int));
88 static void mf_free_bhdr
__ARGS((bhdr_T
*));
89 static void mf_ins_free
__ARGS((memfile_T
*, bhdr_T
*));
90 static bhdr_T
*mf_rem_free
__ARGS((memfile_T
*));
91 static int mf_read
__ARGS((memfile_T
*, bhdr_T
*));
92 static int mf_write
__ARGS((memfile_T
*, bhdr_T
*));
93 static int mf_trans_add
__ARGS((memfile_T
*, bhdr_T
*));
94 static void mf_do_open
__ARGS((memfile_T
*, char_u
*, int));
97 * The functions for using a memfile:
99 * mf_open() open a new or existing memfile
100 * mf_open_file() open a swap file for an existing memfile
101 * mf_close() close (and delete) a memfile
102 * mf_new() create a new block in a memfile and lock it
103 * mf_get() get an existing block and lock it
104 * mf_put() unlock a block, may be marked for writing
105 * mf_free() remove a block
106 * mf_sync() sync changed parts of memfile to disk
107 * mf_release_all() release as much memory as possible
108 * mf_trans_del() may translate negative to positive block number
109 * mf_fullname() make file name full path (use before first :cd)
113 * Open an existing or new memory block file.
115 * fname: name of file to use (NULL means no file at all)
116 * Note: fname must have been allocated, it is not copied!
117 * If opening the file fails, fname is freed.
118 * flags: flags for open() call
120 * If fname != NULL and file cannot be opened, fail.
122 * return value: identifier for this memory block file.
125 mf_open(fname
, flags
)
132 #if defined(STATFS) && defined(UNIX) && !defined(__QNX__)
137 if ((mfp
= (memfile_T
*)alloc((unsigned)sizeof(memfile_T
))) == NULL
)
140 if (fname
== NULL
) /* no file for this memfile, use memory only */
142 mfp
->mf_fname
= NULL
;
143 mfp
->mf_ffname
= NULL
;
148 mf_do_open(mfp
, fname
, flags
); /* try to open the file */
150 /* if the file cannot be opened, return here */
158 mfp
->mf_free_first
= NULL
; /* free list is empty */
159 mfp
->mf_used_first
= NULL
; /* used list is empty */
160 mfp
->mf_used_last
= NULL
;
161 mfp
->mf_dirty
= FALSE
;
162 mfp
->mf_used_count
= 0;
163 for (i
= 0; i
< MEMHASHSIZE
; ++i
)
165 mfp
->mf_hash
[i
] = NULL
; /* hash lists are empty */
166 mfp
->mf_trans
[i
] = NULL
; /* trans lists are empty */
168 mfp
->mf_page_size
= MEMFILE_PAGE_SIZE
;
172 * Try to set the page size equal to the block size of the device.
173 * Speeds up I/O a lot.
174 * When recovering, the actual block size will be retrieved from block 0
175 * in ml_recover(). The size used here may be wrong, therefore
176 * mf_blocknr_max must be rounded up.
179 && fstatfs(mfp
->mf_fd
, &stf
, sizeof(struct statfs
), 0) == 0
180 && stf
.F_BSIZE
>= MIN_SWAP_PAGE_SIZE
181 && stf
.F_BSIZE
<= MAX_SWAP_PAGE_SIZE
)
182 mfp
->mf_page_size
= stf
.F_BSIZE
;
185 if (mfp
->mf_fd
< 0 || (flags
& (O_TRUNC
|O_EXCL
))
186 || (size
= lseek(mfp
->mf_fd
, (off_t
)0L, SEEK_END
)) <= 0)
187 mfp
->mf_blocknr_max
= 0; /* no file or empty file */
189 mfp
->mf_blocknr_max
= (blocknr_T
)((size
+ mfp
->mf_page_size
- 1)
190 / mfp
->mf_page_size
);
191 mfp
->mf_blocknr_min
= -1;
192 mfp
->mf_neg_count
= 0;
193 mfp
->mf_infile_count
= mfp
->mf_blocknr_max
;
194 mfp
->mf_used_count_max
= p_mm
* 1024 / mfp
->mf_page_size
;
200 * Open a file for an existing memfile. Used when updatecount set from 0 to
202 * If the file already exists, this fails.
203 * "fname" is the name of file to use (NULL means no file at all)
204 * Note: "fname" must have been allocated, it is not copied! If opening the
205 * file fails, "fname" is freed.
207 * return value: FAIL if file could not be opened, OK otherwise
210 mf_open_file(mfp
, fname
)
214 mf_do_open(mfp
, fname
, O_RDWR
|O_CREAT
|O_EXCL
); /* try to open the file */
219 mfp
->mf_dirty
= TRUE
;
224 * close a memory file and delete the associated file if 'del_file' is TRUE
227 mf_close(mfp
, del_file
)
232 NR_TRANS
*tp
, *tpnext
;
235 if (mfp
== NULL
) /* safety check */
239 if (close(mfp
->mf_fd
) < 0)
240 EMSG(_(e_swapclose
));
242 if (del_file
&& mfp
->mf_fname
!= NULL
)
243 mch_remove(mfp
->mf_fname
);
244 /* free entries in used list */
245 for (hp
= mfp
->mf_used_first
; hp
!= NULL
; hp
= nextp
)
247 total_mem_used
-= hp
->bh_page_count
* mfp
->mf_page_size
;
251 while (mfp
->mf_free_first
!= NULL
) /* free entries in free list */
252 vim_free(mf_rem_free(mfp
));
253 for (i
= 0; i
< MEMHASHSIZE
; ++i
) /* free entries in trans lists */
254 for (tp
= mfp
->mf_trans
[i
]; tp
!= NULL
; tp
= tpnext
)
256 tpnext
= tp
->nt_next
;
259 vim_free(mfp
->mf_fname
);
260 vim_free(mfp
->mf_ffname
);
265 * Close the swap file for a memfile. Used when 'swapfile' is reset.
268 mf_close_file(buf
, getlines
)
270 int getlines
; /* get all lines into memory? */
275 mfp
= buf
->b_ml
.ml_mfp
;
276 if (mfp
== NULL
|| mfp
->mf_fd
< 0) /* nothing to close */
281 /* get all blocks in memory by accessing all lines (clumsy!) */
283 for (lnum
= 1; lnum
<= buf
->b_ml
.ml_line_count
; ++lnum
)
284 (void)ml_get_buf(buf
, lnum
, FALSE
);
285 dont_release
= FALSE
;
286 /* TODO: should check if all blocks are really in core */
289 if (close(mfp
->mf_fd
) < 0) /* close the file */
290 EMSG(_(e_swapclose
));
293 if (mfp
->mf_fname
!= NULL
)
295 mch_remove(mfp
->mf_fname
); /* delete the swap file */
296 vim_free(mfp
->mf_fname
);
297 vim_free(mfp
->mf_ffname
);
298 mfp
->mf_fname
= NULL
;
299 mfp
->mf_ffname
= NULL
;
304 * Set new size for a memfile. Used when block 0 of a swapfile has been read
305 * and the size it indicates differs from what was guessed.
308 mf_new_page_size(mfp
, new_size
)
312 /* Correct the memory used for block 0 to the new size, because it will be
313 * freed with that size later on. */
314 total_mem_used
+= new_size
- mfp
->mf_page_size
;
315 mfp
->mf_page_size
= new_size
;
321 * negative: TRUE if negative block number desired (data block)
324 mf_new(mfp
, negative
, page_count
)
329 bhdr_T
*hp
; /* new bhdr_T */
330 bhdr_T
*freep
; /* first block in free list */
334 * If we reached the maximum size for the used memory blocks, release one
335 * If a bhdr_T is returned, use it and adjust the page_count if necessary.
337 hp
= mf_release(mfp
, page_count
);
340 * Decide on the number to use:
341 * If there is a free block, use its number.
342 * Otherwise use mf_block_min for a negative number, mf_block_max for
345 freep
= mfp
->mf_free_first
;
346 if (!negative
&& freep
!= NULL
&& freep
->bh_page_count
>= page_count
)
349 * If the block in the free list has more pages, take only the number
350 * of pages needed and allocate a new bhdr_T with data
352 * If the number of pages matches and mf_release did not return a bhdr_T,
353 * use the bhdr_T from the free list and allocate the data
355 * If the number of pages matches and mf_release returned a bhdr_T,
356 * just use the number and free the bhdr_T from the free list
358 if (freep
->bh_page_count
> page_count
)
360 if (hp
== NULL
&& (hp
= mf_alloc_bhdr(mfp
, page_count
)) == NULL
)
362 hp
->bh_bnum
= freep
->bh_bnum
;
363 freep
->bh_bnum
+= page_count
;
364 freep
->bh_page_count
-= page_count
;
366 else if (hp
== NULL
) /* need to allocate memory for this block */
368 if ((p
= (char_u
*)alloc(mfp
->mf_page_size
* page_count
)) == NULL
)
370 hp
= mf_rem_free(mfp
);
373 else /* use the number, remove entry from free list */
375 freep
= mf_rem_free(mfp
);
376 hp
->bh_bnum
= freep
->bh_bnum
;
380 else /* get a new number */
382 if (hp
== NULL
&& (hp
= mf_alloc_bhdr(mfp
, page_count
)) == NULL
)
386 hp
->bh_bnum
= mfp
->mf_blocknr_min
--;
391 hp
->bh_bnum
= mfp
->mf_blocknr_max
;
392 mfp
->mf_blocknr_max
+= page_count
;
395 hp
->bh_flags
= BH_LOCKED
| BH_DIRTY
; /* new block is always dirty */
396 mfp
->mf_dirty
= TRUE
;
397 hp
->bh_page_count
= page_count
;
398 mf_ins_used(mfp
, hp
);
399 mf_ins_hash(mfp
, hp
);
402 * Init the data to all zero, to avoid reading uninitialized data.
403 * This also avoids that the passwd file ends up in the swap file!
405 (void)vim_memset((char *)(hp
->bh_data
), 0, (size_t)mfp
->mf_page_size
);
411 * get existing block 'nr' with 'page_count' pages
413 * Note: The caller should first check a negative nr with mf_trans_del()
416 mf_get(mfp
, nr
, page_count
)
423 if (nr
>= mfp
->mf_blocknr_max
|| nr
<= mfp
->mf_blocknr_min
)
427 * see if it is in the cache
429 hp
= mf_find_hash(mfp
, nr
);
430 if (hp
== NULL
) /* not in the hash list */
432 if (nr
< 0 || nr
>= mfp
->mf_infile_count
) /* can't be in the file */
435 /* could check here if the block is in the free list */
438 * Check if we need to flush an existing block.
439 * If so, use that block.
440 * If not, allocate a new block.
442 hp
= mf_release(mfp
, page_count
);
443 if (hp
== NULL
&& (hp
= mf_alloc_bhdr(mfp
, page_count
)) == NULL
)
448 hp
->bh_page_count
= page_count
;
449 if (mf_read(mfp
, hp
) == FAIL
) /* cannot read the block! */
457 mf_rem_used(mfp
, hp
); /* remove from list, insert in front below */
458 mf_rem_hash(mfp
, hp
);
461 hp
->bh_flags
|= BH_LOCKED
;
462 mf_ins_used(mfp
, hp
); /* put in front of used list */
463 mf_ins_hash(mfp
, hp
); /* put in front of hash list */
469 * release the block *hp
471 * dirty: Block must be written to file later
472 * infile: Block should be in file (needed for recovery)
474 * no return value, function cannot fail
477 mf_put(mfp
, hp
, dirty
, infile
)
485 flags
= hp
->bh_flags
;
487 if ((flags
& BH_LOCKED
) == 0)
488 EMSG(_("E293: block was not locked"));
493 mfp
->mf_dirty
= TRUE
;
495 hp
->bh_flags
= flags
;
497 mf_trans_add(mfp
, hp
); /* may translate negative in positive nr */
501 * block *hp is no longer in used, may put it in the free list of memfile *mfp
508 vim_free(hp
->bh_data
); /* free the memory */
509 mf_rem_hash(mfp
, hp
); /* get *hp out of the hash list */
510 mf_rem_used(mfp
, hp
); /* get *hp out of the used list */
513 vim_free(hp
); /* don't want negative numbers in free list */
517 mf_ins_free(mfp
, hp
); /* put *hp in the free list */
520 #if defined(__MORPHOS__) && defined(__libnix__)
521 /* function is missing in MorphOS libnix version */
522 extern unsigned long *__stdfiledes
;
525 fdtofh(int filedescriptor
)
527 return __stdfiledes
[filedescriptor
];
532 * Sync the memory file *mfp to disk.
534 * MFS_ALL If not given, blocks with negative numbers are not synced,
535 * even when they are dirty!
536 * MFS_STOP Stop syncing when a character becomes available, but sync at
538 * MFS_FLUSH Make sure buffers are flushed to disk, so they will survive a
540 * MFS_ZERO Only write block 0.
542 * Return FAIL for failure, OK otherwise
551 #if defined(SYNC_DUP_CLOSE) && !defined(MSDOS)
554 int got_int_save
= got_int
;
556 if (mfp
->mf_fd
< 0) /* there is no file, nothing to do */
558 mfp
->mf_dirty
= FALSE
;
562 /* Only a CTRL-C while writing will break us here, not one typed
567 * sync from last to first (may reduce the probability of an inconsistent
568 * file) If a write fails, it is very likely caused by a full filesystem.
569 * Then we only try to write blocks within the existing file. If that also
570 * fails then we give up.
573 for (hp
= mfp
->mf_used_last
; hp
!= NULL
; hp
= hp
->bh_prev
)
574 if (((flags
& MFS_ALL
) || hp
->bh_bnum
>= 0)
575 && (hp
->bh_flags
& BH_DIRTY
)
576 && (status
== OK
|| (hp
->bh_bnum
>= 0
577 && hp
->bh_bnum
< mfp
->mf_infile_count
)))
579 if ((flags
& MFS_ZERO
) && hp
->bh_bnum
!= 0)
581 if (mf_write(mfp
, hp
) == FAIL
)
583 if (status
== FAIL
) /* double error: quit syncing */
587 if (flags
& MFS_STOP
)
589 /* Stop when char available now. */
600 * If the whole list is flushed, the memfile is not dirty anymore.
601 * In case of an error this flag is also set, to avoid trying all the time.
603 if (hp
== NULL
|| status
== FAIL
)
604 mfp
->mf_dirty
= FALSE
;
606 if ((flags
& MFS_FLUSH
) && *p_sws
!= NUL
)
611 * most Unixes have the very useful fsync() function, just what we need.
612 * However, with OS/2 and EMX it is also available, but there are
613 * reports of bad problems with it (a bug in HPFS.IFS).
614 * So we disable use of it here in case someone tries to be smart
615 * and changes os_os2_cfg.h... (even though there is no __EMX__ test
616 * in the #if, as __EMX__ does not have sync(); we hope for a timely
617 * sync from the system itself).
619 # if defined(__EMX__)
620 error
"Dont use fsync with EMX! Read emxdoc.doc or emxfix01.doc for info."
622 if (STRCMP(p_sws
, "fsync") == 0)
624 if (fsync(mfp
->mf_fd
))
629 /* OpenNT is strictly POSIX (Benzinger) */
630 /* Tandem/Himalaya NSK-OSS doesn't have sync() */
631 # if defined(__OPENNT) || defined(__TANDEM)
638 if (STRCMP(p_sws
, "fsync") == 0)
640 if (fsync(mfp
->mf_fd
))
645 if (_dos_commit(mfp
->mf_fd
))
648 # ifdef SYNC_DUP_CLOSE
650 * Win32 is a bit more work: Duplicate the file handle and close it.
651 * This should flush the file to disk.
653 if ((fd
= dup(mfp
->mf_fd
)) >= 0)
659 if (fsync(mfp
->mf_fd
) != 0)
663 * Flush() only exists for AmigaDos 2.0.
664 * For 1.3 it should be done with close() + open(), but then the risk
665 * is that the open() may fail and lose the file....
672 struct UFB
*fp
= chkufb(mfp
->mf_fd
);
678 # if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
680 # if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__)
681 /* Have function (in libnix at least),
682 * but ain't got no prototype anywhere. */
683 extern unsigned long fdtofh(int filedescriptor
);
685 # if !defined(__libnix__)
688 BPTR fh
= (BPTR
)fdtofh(mfp
->mf_fd
);
694 # else /* assume Manx */
695 Flush(_devtab
[mfp
->mf_fd
].fd
);
702 got_int
|= got_int_save
;
708 * For all blocks in memory file *mfp that have a positive block number set
709 * the dirty flag. These are blocks that need to be written to a newly
718 for (hp
= mfp
->mf_used_last
; hp
!= NULL
; hp
= hp
->bh_prev
)
720 hp
->bh_flags
|= BH_DIRTY
;
721 mfp
->mf_dirty
= TRUE
;
725 * insert block *hp in front of hashlist of memfile *mfp
735 hash
= MEMHASH(hp
->bh_bnum
);
736 hhp
= mfp
->mf_hash
[hash
];
737 hp
->bh_hash_next
= hhp
;
738 hp
->bh_hash_prev
= NULL
;
740 hhp
->bh_hash_prev
= hp
;
741 mfp
->mf_hash
[hash
] = hp
;
745 * remove block *hp from hashlist of memfile list *mfp
752 if (hp
->bh_hash_prev
== NULL
)
753 mfp
->mf_hash
[MEMHASH(hp
->bh_bnum
)] = hp
->bh_hash_next
;
755 hp
->bh_hash_prev
->bh_hash_next
= hp
->bh_hash_next
;
757 if (hp
->bh_hash_next
)
758 hp
->bh_hash_next
->bh_hash_prev
= hp
->bh_hash_prev
;
762 * look in hash lists of memfile *mfp for block header with number 'nr'
765 mf_find_hash(mfp
, nr
)
771 for (hp
= mfp
->mf_hash
[MEMHASH(nr
)]; hp
!= NULL
; hp
= hp
->bh_hash_next
)
772 if (hp
->bh_bnum
== nr
)
778 * insert block *hp in front of used list of memfile *mfp
785 hp
->bh_next
= mfp
->mf_used_first
;
786 mfp
->mf_used_first
= hp
;
788 if (hp
->bh_next
== NULL
) /* list was empty, adjust last pointer */
789 mfp
->mf_used_last
= hp
;
791 hp
->bh_next
->bh_prev
= hp
;
792 mfp
->mf_used_count
+= hp
->bh_page_count
;
793 total_mem_used
+= hp
->bh_page_count
* mfp
->mf_page_size
;
797 * remove block *hp from used list of memfile *mfp
804 if (hp
->bh_next
== NULL
) /* last block in used list */
805 mfp
->mf_used_last
= hp
->bh_prev
;
807 hp
->bh_next
->bh_prev
= hp
->bh_prev
;
808 if (hp
->bh_prev
== NULL
) /* first block in used list */
809 mfp
->mf_used_first
= hp
->bh_next
;
811 hp
->bh_prev
->bh_next
= hp
->bh_next
;
812 mfp
->mf_used_count
-= hp
->bh_page_count
;
813 total_mem_used
-= hp
->bh_page_count
* mfp
->mf_page_size
;
817 * Release the least recently used block from the used list if the number
818 * of used memory blocks gets to big.
820 * Return the block header to the caller, including the memory block, so
821 * it can be re-used. Make sure the page_count is right.
824 mf_release(mfp
, page_count
)
832 /* don't release while in mf_close_file() */
837 * Need to release a block if the number of blocks for this memfile is
838 * higher than the maximum or total memory used is over 'maxmemtot'
840 need_release
= ((mfp
->mf_used_count
>= mfp
->mf_used_count_max
)
841 || (total_mem_used
>> 10) >= (long_u
)p_mmt
);
844 * Try to create a swap file if the amount of memory used is getting too
847 if (mfp
->mf_fd
< 0 && need_release
&& p_uc
)
849 /* find for which buffer this memfile is */
850 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
851 if (buf
->b_ml
.ml_mfp
== mfp
)
853 if (buf
!= NULL
&& buf
->b_may_swap
)
858 * don't release a block if
859 * there is no file for this memfile
861 * the number of blocks for this memfile is lower than the maximum
863 * total memory used is not up to 'maxmemtot'
865 if (mfp
->mf_fd
< 0 || !need_release
)
868 for (hp
= mfp
->mf_used_last
; hp
!= NULL
; hp
= hp
->bh_prev
)
869 if (!(hp
->bh_flags
& BH_LOCKED
))
871 if (hp
== NULL
) /* not a single one that can be released */
875 * If the block is dirty, write it.
876 * If the write fails we don't free it.
878 if ((hp
->bh_flags
& BH_DIRTY
) && mf_write(mfp
, hp
) == FAIL
)
881 mf_rem_used(mfp
, hp
);
882 mf_rem_hash(mfp
, hp
);
885 * If a bhdr_T is returned, make sure that the page_count of bh_data is
888 if (hp
->bh_page_count
!= page_count
)
890 vim_free(hp
->bh_data
);
891 if ((hp
->bh_data
= alloc(mfp
->mf_page_size
* page_count
)) == NULL
)
896 hp
->bh_page_count
= page_count
;
902 * release as many blocks as possible
903 * Used in case of out of memory
905 * return TRUE if any memory was released
915 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
917 mfp
= buf
->b_ml
.ml_mfp
;
920 /* If no swap file yet, may open one */
921 if (mfp
->mf_fd
< 0 && buf
->b_may_swap
)
924 /* only if there is a swapfile */
927 for (hp
= mfp
->mf_used_last
; hp
!= NULL
; )
929 if (!(hp
->bh_flags
& BH_LOCKED
)
930 && (!(hp
->bh_flags
& BH_DIRTY
)
931 || mf_write(mfp
, hp
) != FAIL
))
933 mf_rem_used(mfp
, hp
);
934 mf_rem_hash(mfp
, hp
);
936 hp
= mfp
->mf_used_last
; /* re-start, list was changed */
949 * Allocate a block header and a block of memory for it
952 mf_alloc_bhdr(mfp
, page_count
)
958 if ((hp
= (bhdr_T
*)alloc((unsigned)sizeof(bhdr_T
))) != NULL
)
960 if ((hp
->bh_data
= (char_u
*)alloc(mfp
->mf_page_size
* page_count
))
963 vim_free(hp
); /* not enough memory */
966 hp
->bh_page_count
= page_count
;
972 * Free a block header and the block of memory for it
978 vim_free(hp
->bh_data
);
983 * insert entry *hp in the free list
990 hp
->bh_next
= mfp
->mf_free_first
;
991 mfp
->mf_free_first
= hp
;
995 * remove the first entry from the free list and return a pointer to it
996 * Note: caller must check that mfp->mf_free_first is not NULL!
1004 hp
= mfp
->mf_free_first
;
1005 mfp
->mf_free_first
= hp
->bh_next
;
1010 * read a block from disk
1012 * Return FAIL for failure, OK otherwise
1023 if (mfp
->mf_fd
< 0) /* there is no file, can't read */
1026 page_size
= mfp
->mf_page_size
;
1027 offset
= (off_t
)page_size
* hp
->bh_bnum
;
1028 size
= page_size
* hp
->bh_page_count
;
1029 if (lseek(mfp
->mf_fd
, offset
, SEEK_SET
) != offset
)
1031 EMSG(_("E294: Seek error in swap file read"));
1034 if ((unsigned)vim_read(mfp
->mf_fd
, hp
->bh_data
, size
) != size
)
1036 EMSG(_("E295: Read error in swap file"));
1043 * write a block to disk
1045 * Return FAIL for failure, OK otherwise
1052 off_t offset
; /* offset in the file */
1053 blocknr_T nr
; /* block nr which is being written */
1055 unsigned page_size
; /* number of bytes in a page */
1056 unsigned page_count
; /* number of pages written */
1057 unsigned size
; /* number of bytes written */
1059 if (mfp
->mf_fd
< 0) /* there is no file, can't write */
1062 if (hp
->bh_bnum
< 0) /* must assign file block number */
1063 if (mf_trans_add(mfp
, hp
) == FAIL
)
1066 page_size
= mfp
->mf_page_size
;
1069 * We don't want gaps in the file. Write the blocks in front of *hp
1070 * to extend the file.
1071 * If block 'mf_infile_count' is not in the hash list, it has been
1072 * freed. Fill the space in the file with data from the current block.
1077 if (nr
> mfp
->mf_infile_count
) /* beyond end of file */
1079 nr
= mfp
->mf_infile_count
;
1080 hp2
= mf_find_hash(mfp
, nr
); /* NULL catched below */
1085 offset
= (off_t
)page_size
* nr
;
1086 if (lseek(mfp
->mf_fd
, offset
, SEEK_SET
) != offset
)
1088 EMSG(_("E296: Seek error in swap file write"));
1091 if (hp2
== NULL
) /* freed block, fill with dummy data */
1094 page_count
= hp2
->bh_page_count
;
1095 size
= page_size
* page_count
;
1096 if ((unsigned)vim_write(mfp
->mf_fd
,
1097 (hp2
== NULL
? hp
: hp2
)->bh_data
, size
) != size
)
1100 * Avoid repeating the error message, this mostly happens when the
1101 * disk is full. We give the message again only after a succesful
1102 * write or when hitting a key. We keep on trying, in case some
1103 * space becomes available.
1105 if (!did_swapwrite_msg
)
1106 EMSG(_("E297: Write error in swap file"));
1107 did_swapwrite_msg
= TRUE
;
1110 did_swapwrite_msg
= FALSE
;
1111 if (hp2
!= NULL
) /* written a non-dummy block */
1112 hp2
->bh_flags
&= ~BH_DIRTY
;
1113 /* appended to the file */
1114 if (nr
+ (blocknr_T
)page_count
> mfp
->mf_infile_count
)
1115 mfp
->mf_infile_count
= nr
+ page_count
;
1116 if (nr
== hp
->bh_bnum
) /* written the desired block */
1123 * Make block number for *hp positive and add it to the translation list
1125 * Return FAIL for failure, OK otherwise
1128 mf_trans_add(mfp
, hp
)
1138 if (hp
->bh_bnum
>= 0) /* it's already positive */
1141 if ((np
= (NR_TRANS
*)alloc((unsigned)sizeof(NR_TRANS
))) == NULL
)
1145 * get a new number for the block.
1146 * If the first item in the free list has sufficient pages, use its number
1147 * Otherwise use mf_blocknr_max.
1149 freep
= mfp
->mf_free_first
;
1150 page_count
= hp
->bh_page_count
;
1151 if (freep
!= NULL
&& freep
->bh_page_count
>= page_count
)
1153 new_bnum
= freep
->bh_bnum
;
1155 * If the page count of the free block was larger, recude it.
1156 * If the page count matches, remove the block from the free list
1158 if (freep
->bh_page_count
> page_count
)
1160 freep
->bh_bnum
+= page_count
;
1161 freep
->bh_page_count
-= page_count
;
1165 freep
= mf_rem_free(mfp
);
1171 new_bnum
= mfp
->mf_blocknr_max
;
1172 mfp
->mf_blocknr_max
+= page_count
;
1175 np
->nt_old_bnum
= hp
->bh_bnum
; /* adjust number */
1176 np
->nt_new_bnum
= new_bnum
;
1178 mf_rem_hash(mfp
, hp
); /* remove from old hash list */
1179 hp
->bh_bnum
= new_bnum
;
1180 mf_ins_hash(mfp
, hp
); /* insert in new hash list */
1182 hash
= MEMHASH(np
->nt_old_bnum
); /* insert in trans list */
1183 np
->nt_next
= mfp
->mf_trans
[hash
];
1184 mfp
->mf_trans
[hash
] = np
;
1185 if (np
->nt_next
!= NULL
)
1186 np
->nt_next
->nt_prev
= np
;
1193 * Lookup a tranlation from the trans lists and delete the entry
1195 * Return the positive new number when found, the old number when not found
1198 mf_trans_del(mfp
, old_nr
)
1206 hash
= MEMHASH(old_nr
);
1207 for (np
= mfp
->mf_trans
[hash
]; np
!= NULL
; np
= np
->nt_next
)
1208 if (np
->nt_old_bnum
== old_nr
)
1210 if (np
== NULL
) /* not found */
1213 mfp
->mf_neg_count
--;
1214 new_bnum
= np
->nt_new_bnum
;
1215 if (np
->nt_prev
!= NULL
) /* remove entry from the trans list */
1216 np
->nt_prev
->nt_next
= np
->nt_next
;
1218 mfp
->mf_trans
[hash
] = np
->nt_next
;
1219 if (np
->nt_next
!= NULL
)
1220 np
->nt_next
->nt_prev
= np
->nt_prev
;
1227 * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
1228 * Only called when creating or renaming the swapfile. Either way it's a new
1229 * name so we must work out the full path name.
1235 mfp
->mf_ffname
= FullName_save(mfp
->mf_fname
, FALSE
);
1239 * Make the name of the file used for the memfile a full path.
1240 * Used before doing a :cd
1246 if (mfp
!= NULL
&& mfp
->mf_fname
!= NULL
&& mfp
->mf_ffname
!= NULL
)
1248 vim_free(mfp
->mf_fname
);
1249 mfp
->mf_fname
= mfp
->mf_ffname
;
1250 mfp
->mf_ffname
= NULL
;
1255 * return TRUE if there are any translations pending for 'mfp'
1261 return (mfp
->mf_fname
!= NULL
&& mfp
->mf_neg_count
> 0);
1265 * Open a swap file for a memfile.
1266 * The "fname" must be in allocated memory, and is consumed (also when an
1270 mf_do_open(mfp
, fname
, flags
)
1273 int flags
; /* flags for open() */
1279 mfp
->mf_fname
= fname
;
1282 * Get the full path name before the open, because this is
1283 * not possible after the open on the Amiga.
1284 * fname cannot be NameBuff, because it must have been allocated.
1287 #if defined(MSDOS) || defined(MSWIN) || defined(RISCOS)
1289 * A ":!cd e:xxx" may change the directory without us knowning, use the
1290 * full pathname always. Careful: This frees fname!
1297 * Extra security check: When creating a swap file it really shouldn't
1298 * exist yet. If there is a symbolic link, this is most likely an attack.
1300 if ((flags
& O_CREAT
) && mch_lstat((char *)mfp
->mf_fname
, &sb
) >= 0)
1303 EMSG(_("E300: Swap file already exists (symlink attack?)"));
1309 * try to open the file
1311 flags
|= O_EXTRA
| O_NOFOLLOW
;
1313 /* Prevent handle inheritance that cause problems with Cscope
1314 * (swap file may not be deleted if cscope connection was open after
1316 flags
|= O_NOINHERIT
;
1318 mfp
->mf_fd
= mch_open_rw((char *)mfp
->mf_fname
, flags
);
1322 * If the file cannot be opened, use memory only
1326 vim_free(mfp
->mf_fname
);
1327 vim_free(mfp
->mf_ffname
);
1328 mfp
->mf_fname
= NULL
;
1329 mfp
->mf_ffname
= NULL
;
1332 mch_hide(mfp
->mf_fname
); /* try setting the 'hidden' flag */