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 */
80 static void mf_ins_hash
__ARGS((memfile_T
*, bhdr_T
*));
81 static void mf_rem_hash
__ARGS((memfile_T
*, bhdr_T
*));
82 static bhdr_T
*mf_find_hash
__ARGS((memfile_T
*, blocknr_T
));
83 static void mf_ins_used
__ARGS((memfile_T
*, bhdr_T
*));
84 static void mf_rem_used
__ARGS((memfile_T
*, bhdr_T
*));
85 static bhdr_T
*mf_release
__ARGS((memfile_T
*, int));
86 static bhdr_T
*mf_alloc_bhdr
__ARGS((memfile_T
*, int));
87 static void mf_free_bhdr
__ARGS((bhdr_T
*));
88 static void mf_ins_free
__ARGS((memfile_T
*, bhdr_T
*));
89 static bhdr_T
*mf_rem_free
__ARGS((memfile_T
*));
90 static int mf_read
__ARGS((memfile_T
*, bhdr_T
*));
91 static int mf_write
__ARGS((memfile_T
*, bhdr_T
*));
92 static int mf_trans_add
__ARGS((memfile_T
*, bhdr_T
*));
93 static void mf_do_open
__ARGS((memfile_T
*, char_u
*, int));
96 * The functions for using a memfile:
98 * mf_open() open a new or existing memfile
99 * mf_open_file() open a swap file for an existing memfile
100 * mf_close() close (and delete) a memfile
101 * mf_new() create a new block in a memfile and lock it
102 * mf_get() get an existing block and lock it
103 * mf_put() unlock a block, may be marked for writing
104 * mf_free() remove a block
105 * mf_sync() sync changed parts of memfile to disk
106 * mf_release_all() release as much memory as possible
107 * mf_trans_del() may translate negative to positive block number
108 * mf_fullname() make file name full path (use before first :cd)
112 * Open an existing or new memory block file.
114 * fname: name of file to use (NULL means no file at all)
115 * Note: fname must have been allocated, it is not copied!
116 * If opening the file fails, fname is freed.
117 * flags: flags for open() call
119 * If fname != NULL and file cannot be opened, fail.
121 * return value: identifier for this memory block file.
124 mf_open(fname
, flags
)
131 #if defined(STATFS) && defined(UNIX) && !defined(__QNX__)
136 if ((mfp
= (memfile_T
*)alloc((unsigned)sizeof(memfile_T
))) == NULL
)
139 if (fname
== NULL
) /* no file for this memfile, use memory only */
141 mfp
->mf_fname
= NULL
;
142 mfp
->mf_ffname
= NULL
;
147 mf_do_open(mfp
, fname
, flags
); /* try to open the file */
149 /* if the file cannot be opened, return here */
157 mfp
->mf_free_first
= NULL
; /* free list is empty */
158 mfp
->mf_used_first
= NULL
; /* used list is empty */
159 mfp
->mf_used_last
= NULL
;
160 mfp
->mf_dirty
= FALSE
;
161 mfp
->mf_used_count
= 0;
162 for (i
= 0; i
< MEMHASHSIZE
; ++i
)
164 mfp
->mf_hash
[i
] = NULL
; /* hash lists are empty */
165 mfp
->mf_trans
[i
] = NULL
; /* trans lists are empty */
167 mfp
->mf_page_size
= MEMFILE_PAGE_SIZE
;
171 * Try to set the page size equal to the block size of the device.
172 * Speeds up I/O a lot.
173 * When recovering, the actual block size will be retrieved from block 0
174 * in ml_recover(). The size used here may be wrong, therefore
175 * mf_blocknr_max must be rounded up.
178 && fstatfs(mfp
->mf_fd
, &stf
, sizeof(struct statfs
), 0) == 0
179 && stf
.F_BSIZE
>= MIN_SWAP_PAGE_SIZE
180 && stf
.F_BSIZE
<= MAX_SWAP_PAGE_SIZE
)
181 mfp
->mf_page_size
= stf
.F_BSIZE
;
184 if (mfp
->mf_fd
< 0 || (flags
& (O_TRUNC
|O_EXCL
))
185 || (size
= lseek(mfp
->mf_fd
, (off_t
)0L, SEEK_END
)) <= 0)
186 mfp
->mf_blocknr_max
= 0; /* no file or empty file */
188 mfp
->mf_blocknr_max
= (blocknr_T
)((size
+ mfp
->mf_page_size
- 1)
189 / mfp
->mf_page_size
);
190 mfp
->mf_blocknr_min
= -1;
191 mfp
->mf_neg_count
= 0;
192 mfp
->mf_infile_count
= mfp
->mf_blocknr_max
;
195 * Compute maximum number of pages ('maxmem' is in Kbyte):
196 * 'mammem' * 1Kbyte / page-size-in-bytes.
197 * Avoid overflow by first reducing page size as much as possible.
201 unsigned page_size
= mfp
->mf_page_size
;
203 while (shift
> 0 && (page_size
& 1) == 0)
205 page_size
= page_size
>> 1;
208 mfp
->mf_used_count_max
= (p_mm
<< shift
) / page_size
;
209 if (mfp
->mf_used_count_max
< 10)
210 mfp
->mf_used_count_max
= 10;
217 * Open a file for an existing memfile. Used when updatecount set from 0 to
219 * If the file already exists, this fails.
220 * "fname" is the name of file to use (NULL means no file at all)
221 * Note: "fname" must have been allocated, it is not copied! If opening the
222 * file fails, "fname" is freed.
224 * return value: FAIL if file could not be opened, OK otherwise
227 mf_open_file(mfp
, fname
)
231 mf_do_open(mfp
, fname
, O_RDWR
|O_CREAT
|O_EXCL
); /* try to open the file */
236 mfp
->mf_dirty
= TRUE
;
241 * close a memory file and delete the associated file if 'del_file' is TRUE
244 mf_close(mfp
, del_file
)
249 NR_TRANS
*tp
, *tpnext
;
252 if (mfp
== NULL
) /* safety check */
256 if (close(mfp
->mf_fd
) < 0)
257 EMSG(_(e_swapclose
));
259 if (del_file
&& mfp
->mf_fname
!= NULL
)
260 mch_remove(mfp
->mf_fname
);
261 /* free entries in used list */
262 for (hp
= mfp
->mf_used_first
; hp
!= NULL
; hp
= nextp
)
264 total_mem_used
-= hp
->bh_page_count
* mfp
->mf_page_size
;
268 while (mfp
->mf_free_first
!= NULL
) /* free entries in free list */
269 vim_free(mf_rem_free(mfp
));
270 for (i
= 0; i
< MEMHASHSIZE
; ++i
) /* free entries in trans lists */
271 for (tp
= mfp
->mf_trans
[i
]; tp
!= NULL
; tp
= tpnext
)
273 tpnext
= tp
->nt_next
;
276 vim_free(mfp
->mf_fname
);
277 vim_free(mfp
->mf_ffname
);
282 * Close the swap file for a memfile. Used when 'swapfile' is reset.
285 mf_close_file(buf
, getlines
)
287 int getlines
; /* get all lines into memory? */
292 mfp
= buf
->b_ml
.ml_mfp
;
293 if (mfp
== NULL
|| mfp
->mf_fd
< 0) /* nothing to close */
298 /* get all blocks in memory by accessing all lines (clumsy!) */
299 mf_dont_release
= TRUE
;
300 for (lnum
= 1; lnum
<= buf
->b_ml
.ml_line_count
; ++lnum
)
301 (void)ml_get_buf(buf
, lnum
, FALSE
);
302 mf_dont_release
= FALSE
;
303 /* TODO: should check if all blocks are really in core */
306 if (close(mfp
->mf_fd
) < 0) /* close the file */
307 EMSG(_(e_swapclose
));
310 if (mfp
->mf_fname
!= NULL
)
312 mch_remove(mfp
->mf_fname
); /* delete the swap file */
313 vim_free(mfp
->mf_fname
);
314 vim_free(mfp
->mf_ffname
);
315 mfp
->mf_fname
= NULL
;
316 mfp
->mf_ffname
= NULL
;
321 * Set new size for a memfile. Used when block 0 of a swapfile has been read
322 * and the size it indicates differs from what was guessed.
325 mf_new_page_size(mfp
, new_size
)
329 /* Correct the memory used for block 0 to the new size, because it will be
330 * freed with that size later on. */
331 total_mem_used
+= new_size
- mfp
->mf_page_size
;
332 mfp
->mf_page_size
= new_size
;
338 * negative: TRUE if negative block number desired (data block)
341 mf_new(mfp
, negative
, page_count
)
346 bhdr_T
*hp
; /* new bhdr_T */
347 bhdr_T
*freep
; /* first block in free list */
351 * If we reached the maximum size for the used memory blocks, release one
352 * If a bhdr_T is returned, use it and adjust the page_count if necessary.
354 hp
= mf_release(mfp
, page_count
);
357 * Decide on the number to use:
358 * If there is a free block, use its number.
359 * Otherwise use mf_block_min for a negative number, mf_block_max for
362 freep
= mfp
->mf_free_first
;
363 if (!negative
&& freep
!= NULL
&& freep
->bh_page_count
>= page_count
)
366 * If the block in the free list has more pages, take only the number
367 * of pages needed and allocate a new bhdr_T with data
369 * If the number of pages matches and mf_release() did not return a
370 * bhdr_T, use the bhdr_T from the free list and allocate the data
372 * If the number of pages matches and mf_release() returned a bhdr_T,
373 * just use the number and free the bhdr_T from the free list
375 if (freep
->bh_page_count
> page_count
)
377 if (hp
== NULL
&& (hp
= mf_alloc_bhdr(mfp
, page_count
)) == NULL
)
379 hp
->bh_bnum
= freep
->bh_bnum
;
380 freep
->bh_bnum
+= page_count
;
381 freep
->bh_page_count
-= page_count
;
383 else if (hp
== NULL
) /* need to allocate memory for this block */
385 if ((p
= (char_u
*)alloc(mfp
->mf_page_size
* page_count
)) == NULL
)
387 hp
= mf_rem_free(mfp
);
390 else /* use the number, remove entry from free list */
392 freep
= mf_rem_free(mfp
);
393 hp
->bh_bnum
= freep
->bh_bnum
;
397 else /* get a new number */
399 if (hp
== NULL
&& (hp
= mf_alloc_bhdr(mfp
, page_count
)) == NULL
)
403 hp
->bh_bnum
= mfp
->mf_blocknr_min
--;
408 hp
->bh_bnum
= mfp
->mf_blocknr_max
;
409 mfp
->mf_blocknr_max
+= page_count
;
412 hp
->bh_flags
= BH_LOCKED
| BH_DIRTY
; /* new block is always dirty */
413 mfp
->mf_dirty
= TRUE
;
414 hp
->bh_page_count
= page_count
;
415 mf_ins_used(mfp
, hp
);
416 mf_ins_hash(mfp
, hp
);
419 * Init the data to all zero, to avoid reading uninitialized data.
420 * This also avoids that the passwd file ends up in the swap file!
422 (void)vim_memset((char *)(hp
->bh_data
), 0, (size_t)mfp
->mf_page_size
);
428 * get existing block 'nr' with 'page_count' pages
430 * Note: The caller should first check a negative nr with mf_trans_del()
433 mf_get(mfp
, nr
, page_count
)
440 if (nr
>= mfp
->mf_blocknr_max
|| nr
<= mfp
->mf_blocknr_min
)
444 * see if it is in the cache
446 hp
= mf_find_hash(mfp
, nr
);
447 if (hp
== NULL
) /* not in the hash list */
449 if (nr
< 0 || nr
>= mfp
->mf_infile_count
) /* can't be in the file */
452 /* could check here if the block is in the free list */
455 * Check if we need to flush an existing block.
456 * If so, use that block.
457 * If not, allocate a new block.
459 hp
= mf_release(mfp
, page_count
);
460 if (hp
== NULL
&& (hp
= mf_alloc_bhdr(mfp
, page_count
)) == NULL
)
465 hp
->bh_page_count
= page_count
;
466 if (mf_read(mfp
, hp
) == FAIL
) /* cannot read the block! */
474 mf_rem_used(mfp
, hp
); /* remove from list, insert in front below */
475 mf_rem_hash(mfp
, hp
);
478 hp
->bh_flags
|= BH_LOCKED
;
479 mf_ins_used(mfp
, hp
); /* put in front of used list */
480 mf_ins_hash(mfp
, hp
); /* put in front of hash list */
486 * release the block *hp
488 * dirty: Block must be written to file later
489 * infile: Block should be in file (needed for recovery)
491 * no return value, function cannot fail
494 mf_put(mfp
, hp
, dirty
, infile
)
502 flags
= hp
->bh_flags
;
504 if ((flags
& BH_LOCKED
) == 0)
505 EMSG(_("E293: block was not locked"));
510 mfp
->mf_dirty
= TRUE
;
512 hp
->bh_flags
= flags
;
514 mf_trans_add(mfp
, hp
); /* may translate negative in positive nr */
518 * block *hp is no longer in used, may put it in the free list of memfile *mfp
525 vim_free(hp
->bh_data
); /* free the memory */
526 mf_rem_hash(mfp
, hp
); /* get *hp out of the hash list */
527 mf_rem_used(mfp
, hp
); /* get *hp out of the used list */
530 vim_free(hp
); /* don't want negative numbers in free list */
534 mf_ins_free(mfp
, hp
); /* put *hp in the free list */
537 #if defined(__MORPHOS__) && defined(__libnix__)
538 /* function is missing in MorphOS libnix version */
539 extern unsigned long *__stdfiledes
;
542 fdtofh(int filedescriptor
)
544 return __stdfiledes
[filedescriptor
];
549 * Sync the memory file *mfp to disk.
551 * MFS_ALL If not given, blocks with negative numbers are not synced,
552 * even when they are dirty!
553 * MFS_STOP Stop syncing when a character becomes available, but sync at
555 * MFS_FLUSH Make sure buffers are flushed to disk, so they will survive a
557 * MFS_ZERO Only write block 0.
559 * Return FAIL for failure, OK otherwise
568 #if defined(SYNC_DUP_CLOSE) && !defined(MSDOS)
571 int got_int_save
= got_int
;
573 if (mfp
->mf_fd
< 0) /* there is no file, nothing to do */
575 mfp
->mf_dirty
= FALSE
;
579 /* Only a CTRL-C while writing will break us here, not one typed
584 * sync from last to first (may reduce the probability of an inconsistent
585 * file) If a write fails, it is very likely caused by a full filesystem.
586 * Then we only try to write blocks within the existing file. If that also
587 * fails then we give up.
590 for (hp
= mfp
->mf_used_last
; hp
!= NULL
; hp
= hp
->bh_prev
)
591 if (((flags
& MFS_ALL
) || hp
->bh_bnum
>= 0)
592 && (hp
->bh_flags
& BH_DIRTY
)
593 && (status
== OK
|| (hp
->bh_bnum
>= 0
594 && hp
->bh_bnum
< mfp
->mf_infile_count
)))
596 if ((flags
& MFS_ZERO
) && hp
->bh_bnum
!= 0)
598 if (mf_write(mfp
, hp
) == FAIL
)
600 if (status
== FAIL
) /* double error: quit syncing */
604 if (flags
& MFS_STOP
)
606 /* Stop when char available now. */
617 * If the whole list is flushed, the memfile is not dirty anymore.
618 * In case of an error this flag is also set, to avoid trying all the time.
620 if (hp
== NULL
|| status
== FAIL
)
621 mfp
->mf_dirty
= FALSE
;
623 if ((flags
& MFS_FLUSH
) && *p_sws
!= NUL
)
628 * most Unixes have the very useful fsync() function, just what we need.
629 * However, with OS/2 and EMX it is also available, but there are
630 * reports of bad problems with it (a bug in HPFS.IFS).
631 * So we disable use of it here in case someone tries to be smart
632 * and changes os_os2_cfg.h... (even though there is no __EMX__ test
633 * in the #if, as __EMX__ does not have sync(); we hope for a timely
634 * sync from the system itself).
636 # if defined(__EMX__)
637 error
"Dont use fsync with EMX! Read emxdoc.doc or emxfix01.doc for info."
639 if (STRCMP(p_sws
, "fsync") == 0)
641 if (fsync(mfp
->mf_fd
))
646 /* OpenNT is strictly POSIX (Benzinger) */
647 /* Tandem/Himalaya NSK-OSS doesn't have sync() */
648 # if defined(__OPENNT) || defined(__TANDEM)
655 if (STRCMP(p_sws
, "fsync") == 0)
657 if (fsync(mfp
->mf_fd
))
662 if (_dos_commit(mfp
->mf_fd
))
665 # ifdef SYNC_DUP_CLOSE
667 * Win32 is a bit more work: Duplicate the file handle and close it.
668 * This should flush the file to disk.
670 if ((fd
= dup(mfp
->mf_fd
)) >= 0)
675 # if defined(__AROS__) || defined(__amigaos4__)
676 if (fsync(mfp
->mf_fd
) != 0)
680 * Flush() only exists for AmigaDos 2.0.
681 * For 1.3 it should be done with close() + open(), but then the risk
682 * is that the open() may fail and lose the file....
689 struct UFB
*fp
= chkufb(mfp
->mf_fd
);
695 # if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
697 # if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__)
698 /* Have function (in libnix at least),
699 * but ain't got no prototype anywhere. */
700 extern unsigned long fdtofh(int filedescriptor
);
702 # if !defined(__libnix__)
705 BPTR fh
= (BPTR
)fdtofh(mfp
->mf_fd
);
711 # else /* assume Manx */
712 Flush(_devtab
[mfp
->mf_fd
].fd
);
719 got_int
|= got_int_save
;
725 * For all blocks in memory file *mfp that have a positive block number set
726 * the dirty flag. These are blocks that need to be written to a newly
735 for (hp
= mfp
->mf_used_last
; hp
!= NULL
; hp
= hp
->bh_prev
)
737 hp
->bh_flags
|= BH_DIRTY
;
738 mfp
->mf_dirty
= TRUE
;
742 * insert block *hp in front of hashlist of memfile *mfp
752 hash
= MEMHASH(hp
->bh_bnum
);
753 hhp
= mfp
->mf_hash
[hash
];
754 hp
->bh_hash_next
= hhp
;
755 hp
->bh_hash_prev
= NULL
;
757 hhp
->bh_hash_prev
= hp
;
758 mfp
->mf_hash
[hash
] = hp
;
762 * remove block *hp from hashlist of memfile list *mfp
769 if (hp
->bh_hash_prev
== NULL
)
770 mfp
->mf_hash
[MEMHASH(hp
->bh_bnum
)] = hp
->bh_hash_next
;
772 hp
->bh_hash_prev
->bh_hash_next
= hp
->bh_hash_next
;
774 if (hp
->bh_hash_next
)
775 hp
->bh_hash_next
->bh_hash_prev
= hp
->bh_hash_prev
;
779 * look in hash lists of memfile *mfp for block header with number 'nr'
782 mf_find_hash(mfp
, nr
)
788 for (hp
= mfp
->mf_hash
[MEMHASH(nr
)]; hp
!= NULL
; hp
= hp
->bh_hash_next
)
789 if (hp
->bh_bnum
== nr
)
795 * insert block *hp in front of used list of memfile *mfp
802 hp
->bh_next
= mfp
->mf_used_first
;
803 mfp
->mf_used_first
= hp
;
805 if (hp
->bh_next
== NULL
) /* list was empty, adjust last pointer */
806 mfp
->mf_used_last
= hp
;
808 hp
->bh_next
->bh_prev
= hp
;
809 mfp
->mf_used_count
+= hp
->bh_page_count
;
810 total_mem_used
+= hp
->bh_page_count
* mfp
->mf_page_size
;
814 * remove block *hp from used list of memfile *mfp
821 if (hp
->bh_next
== NULL
) /* last block in used list */
822 mfp
->mf_used_last
= hp
->bh_prev
;
824 hp
->bh_next
->bh_prev
= hp
->bh_prev
;
825 if (hp
->bh_prev
== NULL
) /* first block in used list */
826 mfp
->mf_used_first
= hp
->bh_next
;
828 hp
->bh_prev
->bh_next
= hp
->bh_next
;
829 mfp
->mf_used_count
-= hp
->bh_page_count
;
830 total_mem_used
-= hp
->bh_page_count
* mfp
->mf_page_size
;
834 * Release the least recently used block from the used list if the number
835 * of used memory blocks gets to big.
837 * Return the block header to the caller, including the memory block, so
838 * it can be re-used. Make sure the page_count is right.
841 mf_release(mfp
, page_count
)
849 /* don't release while in mf_close_file() */
854 * Need to release a block if the number of blocks for this memfile is
855 * higher than the maximum or total memory used is over 'maxmemtot'
857 need_release
= ((mfp
->mf_used_count
>= mfp
->mf_used_count_max
)
858 || (total_mem_used
>> 10) >= (long_u
)p_mmt
);
861 * Try to create a swap file if the amount of memory used is getting too
864 if (mfp
->mf_fd
< 0 && need_release
&& p_uc
)
866 /* find for which buffer this memfile is */
867 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
868 if (buf
->b_ml
.ml_mfp
== mfp
)
870 if (buf
!= NULL
&& buf
->b_may_swap
)
875 * don't release a block if
876 * there is no file for this memfile
878 * the number of blocks for this memfile is lower than the maximum
880 * total memory used is not up to 'maxmemtot'
882 if (mfp
->mf_fd
< 0 || !need_release
)
885 for (hp
= mfp
->mf_used_last
; hp
!= NULL
; hp
= hp
->bh_prev
)
886 if (!(hp
->bh_flags
& BH_LOCKED
))
888 if (hp
== NULL
) /* not a single one that can be released */
892 * If the block is dirty, write it.
893 * If the write fails we don't free it.
895 if ((hp
->bh_flags
& BH_DIRTY
) && mf_write(mfp
, hp
) == FAIL
)
898 mf_rem_used(mfp
, hp
);
899 mf_rem_hash(mfp
, hp
);
902 * If a bhdr_T is returned, make sure that the page_count of bh_data is
905 if (hp
->bh_page_count
!= page_count
)
907 vim_free(hp
->bh_data
);
908 if ((hp
->bh_data
= alloc(mfp
->mf_page_size
* page_count
)) == NULL
)
913 hp
->bh_page_count
= page_count
;
919 * release as many blocks as possible
920 * Used in case of out of memory
922 * return TRUE if any memory was released
932 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
934 mfp
= buf
->b_ml
.ml_mfp
;
937 /* If no swap file yet, may open one */
938 if (mfp
->mf_fd
< 0 && buf
->b_may_swap
)
941 /* only if there is a swapfile */
944 for (hp
= mfp
->mf_used_last
; hp
!= NULL
; )
946 if (!(hp
->bh_flags
& BH_LOCKED
)
947 && (!(hp
->bh_flags
& BH_DIRTY
)
948 || mf_write(mfp
, hp
) != FAIL
))
950 mf_rem_used(mfp
, hp
);
951 mf_rem_hash(mfp
, hp
);
953 hp
= mfp
->mf_used_last
; /* re-start, list was changed */
966 * Allocate a block header and a block of memory for it
969 mf_alloc_bhdr(mfp
, page_count
)
975 if ((hp
= (bhdr_T
*)alloc((unsigned)sizeof(bhdr_T
))) != NULL
)
977 if ((hp
->bh_data
= (char_u
*)alloc(mfp
->mf_page_size
* page_count
))
980 vim_free(hp
); /* not enough memory */
983 hp
->bh_page_count
= page_count
;
989 * Free a block header and the block of memory for it
995 vim_free(hp
->bh_data
);
1000 * insert entry *hp in the free list
1003 mf_ins_free(mfp
, hp
)
1007 hp
->bh_next
= mfp
->mf_free_first
;
1008 mfp
->mf_free_first
= hp
;
1012 * remove the first entry from the free list and return a pointer to it
1013 * Note: caller must check that mfp->mf_free_first is not NULL!
1021 hp
= mfp
->mf_free_first
;
1022 mfp
->mf_free_first
= hp
->bh_next
;
1027 * read a block from disk
1029 * Return FAIL for failure, OK otherwise
1040 if (mfp
->mf_fd
< 0) /* there is no file, can't read */
1043 page_size
= mfp
->mf_page_size
;
1044 offset
= (off_t
)page_size
* hp
->bh_bnum
;
1045 size
= page_size
* hp
->bh_page_count
;
1046 if (lseek(mfp
->mf_fd
, offset
, SEEK_SET
) != offset
)
1048 PERROR(_("E294: Seek error in swap file read"));
1051 if ((unsigned)vim_read(mfp
->mf_fd
, hp
->bh_data
, size
) != size
)
1053 PERROR(_("E295: Read error in swap file"));
1060 * write a block to disk
1062 * Return FAIL for failure, OK otherwise
1069 off_t offset
; /* offset in the file */
1070 blocknr_T nr
; /* block nr which is being written */
1072 unsigned page_size
; /* number of bytes in a page */
1073 unsigned page_count
; /* number of pages written */
1074 unsigned size
; /* number of bytes written */
1076 if (mfp
->mf_fd
< 0) /* there is no file, can't write */
1079 if (hp
->bh_bnum
< 0) /* must assign file block number */
1080 if (mf_trans_add(mfp
, hp
) == FAIL
)
1083 page_size
= mfp
->mf_page_size
;
1086 * We don't want gaps in the file. Write the blocks in front of *hp
1087 * to extend the file.
1088 * If block 'mf_infile_count' is not in the hash list, it has been
1089 * freed. Fill the space in the file with data from the current block.
1094 if (nr
> mfp
->mf_infile_count
) /* beyond end of file */
1096 nr
= mfp
->mf_infile_count
;
1097 hp2
= mf_find_hash(mfp
, nr
); /* NULL catched below */
1102 offset
= (off_t
)page_size
* nr
;
1103 if (lseek(mfp
->mf_fd
, offset
, SEEK_SET
) != offset
)
1105 PERROR(_("E296: Seek error in swap file write"));
1108 if (hp2
== NULL
) /* freed block, fill with dummy data */
1111 page_count
= hp2
->bh_page_count
;
1112 size
= page_size
* page_count
;
1113 if ((unsigned)vim_write(mfp
->mf_fd
,
1114 (hp2
== NULL
? hp
: hp2
)->bh_data
, size
) != size
)
1117 * Avoid repeating the error message, this mostly happens when the
1118 * disk is full. We give the message again only after a successful
1119 * write or when hitting a key. We keep on trying, in case some
1120 * space becomes available.
1122 if (!did_swapwrite_msg
)
1123 EMSG(_("E297: Write error in swap file"));
1124 did_swapwrite_msg
= TRUE
;
1127 did_swapwrite_msg
= FALSE
;
1128 if (hp2
!= NULL
) /* written a non-dummy block */
1129 hp2
->bh_flags
&= ~BH_DIRTY
;
1130 /* appended to the file */
1131 if (nr
+ (blocknr_T
)page_count
> mfp
->mf_infile_count
)
1132 mfp
->mf_infile_count
= nr
+ page_count
;
1133 if (nr
== hp
->bh_bnum
) /* written the desired block */
1140 * Make block number for *hp positive and add it to the translation list
1142 * Return FAIL for failure, OK otherwise
1145 mf_trans_add(mfp
, hp
)
1155 if (hp
->bh_bnum
>= 0) /* it's already positive */
1158 if ((np
= (NR_TRANS
*)alloc((unsigned)sizeof(NR_TRANS
))) == NULL
)
1162 * get a new number for the block.
1163 * If the first item in the free list has sufficient pages, use its number
1164 * Otherwise use mf_blocknr_max.
1166 freep
= mfp
->mf_free_first
;
1167 page_count
= hp
->bh_page_count
;
1168 if (freep
!= NULL
&& freep
->bh_page_count
>= page_count
)
1170 new_bnum
= freep
->bh_bnum
;
1172 * If the page count of the free block was larger, recude it.
1173 * If the page count matches, remove the block from the free list
1175 if (freep
->bh_page_count
> page_count
)
1177 freep
->bh_bnum
+= page_count
;
1178 freep
->bh_page_count
-= page_count
;
1182 freep
= mf_rem_free(mfp
);
1188 new_bnum
= mfp
->mf_blocknr_max
;
1189 mfp
->mf_blocknr_max
+= page_count
;
1192 np
->nt_old_bnum
= hp
->bh_bnum
; /* adjust number */
1193 np
->nt_new_bnum
= new_bnum
;
1195 mf_rem_hash(mfp
, hp
); /* remove from old hash list */
1196 hp
->bh_bnum
= new_bnum
;
1197 mf_ins_hash(mfp
, hp
); /* insert in new hash list */
1199 hash
= MEMHASH(np
->nt_old_bnum
); /* insert in trans list */
1200 np
->nt_next
= mfp
->mf_trans
[hash
];
1201 mfp
->mf_trans
[hash
] = np
;
1202 if (np
->nt_next
!= NULL
)
1203 np
->nt_next
->nt_prev
= np
;
1210 * Lookup a translation from the trans lists and delete the entry
1212 * Return the positive new number when found, the old number when not found
1215 mf_trans_del(mfp
, old_nr
)
1223 hash
= MEMHASH(old_nr
);
1224 for (np
= mfp
->mf_trans
[hash
]; np
!= NULL
; np
= np
->nt_next
)
1225 if (np
->nt_old_bnum
== old_nr
)
1227 if (np
== NULL
) /* not found */
1230 mfp
->mf_neg_count
--;
1231 new_bnum
= np
->nt_new_bnum
;
1232 if (np
->nt_prev
!= NULL
) /* remove entry from the trans list */
1233 np
->nt_prev
->nt_next
= np
->nt_next
;
1235 mfp
->mf_trans
[hash
] = np
->nt_next
;
1236 if (np
->nt_next
!= NULL
)
1237 np
->nt_next
->nt_prev
= np
->nt_prev
;
1244 * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
1245 * Only called when creating or renaming the swapfile. Either way it's a new
1246 * name so we must work out the full path name.
1252 mfp
->mf_ffname
= FullName_save(mfp
->mf_fname
, FALSE
);
1256 * Make the name of the file used for the memfile a full path.
1257 * Used before doing a :cd
1263 if (mfp
!= NULL
&& mfp
->mf_fname
!= NULL
&& mfp
->mf_ffname
!= NULL
)
1265 vim_free(mfp
->mf_fname
);
1266 mfp
->mf_fname
= mfp
->mf_ffname
;
1267 mfp
->mf_ffname
= NULL
;
1272 * return TRUE if there are any translations pending for 'mfp'
1278 return (mfp
->mf_fname
!= NULL
&& mfp
->mf_neg_count
> 0);
1282 * Open a swap file for a memfile.
1283 * The "fname" must be in allocated memory, and is consumed (also when an
1287 mf_do_open(mfp
, fname
, flags
)
1290 int flags
; /* flags for open() */
1296 mfp
->mf_fname
= fname
;
1299 * Get the full path name before the open, because this is
1300 * not possible after the open on the Amiga.
1301 * fname cannot be NameBuff, because it must have been allocated.
1304 #if defined(MSDOS) || defined(MSWIN) || defined(RISCOS)
1306 * A ":!cd e:xxx" may change the directory without us knowning, use the
1307 * full pathname always. Careful: This frees fname!
1314 * Extra security check: When creating a swap file it really shouldn't
1315 * exist yet. If there is a symbolic link, this is most likely an attack.
1317 if ((flags
& O_CREAT
) && mch_lstat((char *)mfp
->mf_fname
, &sb
) >= 0)
1320 EMSG(_("E300: Swap file already exists (symlink attack?)"));
1326 * try to open the file
1328 flags
|= O_EXTRA
| O_NOFOLLOW
;
1330 /* Prevent handle inheritance that cause problems with Cscope
1331 * (swap file may not be deleted if cscope connection was open after
1333 flags
|= O_NOINHERIT
;
1335 mfp
->mf_fd
= mch_open_rw((char *)mfp
->mf_fname
, flags
);
1339 * If the file cannot be opened, use memory only
1343 vim_free(mfp
->mf_fname
);
1344 vim_free(mfp
->mf_ffname
);
1345 mfp
->mf_fname
= NULL
;
1346 mfp
->mf_ffname
= NULL
;
1349 mch_hide(mfp
->mf_fname
); /* try setting the 'hidden' flag */