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(WIN16) || defined(WIN32) || defined(_WIN64)
36 # include "vimio.h" /* for lseek(), must be before vim.h */
42 * Some systems have the page size in statfs.f_bsize, some in stat.st_blksize
44 #ifdef HAVE_ST_BLKSIZE
46 # define F_BSIZE st_blksize
47 # define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
49 # ifdef HAVE_SYS_STATFS_H
50 # include <sys/statfs.h>
51 # define STATFS statfs
52 # define F_BSIZE f_bsize
53 # ifdef __MINT__ /* do we still need this? */
54 # define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
60 * for Amiga Dos 2.0x we use Flush
64 extern int dos2
; /* this is in os_amiga.c */
67 # include <proto/dos.h>
68 # include <ios1.h> /* for chkufb() */
72 #define MEMFILE_PAGE_SIZE 4096 /* default page size */
74 static long_u total_mem_used
= 0; /* total memory used for memfiles */
76 static void mf_ins_hash
__ARGS((memfile_T
*, bhdr_T
*));
77 static void mf_rem_hash
__ARGS((memfile_T
*, bhdr_T
*));
78 static bhdr_T
*mf_find_hash
__ARGS((memfile_T
*, blocknr_T
));
79 static void mf_ins_used
__ARGS((memfile_T
*, bhdr_T
*));
80 static void mf_rem_used
__ARGS((memfile_T
*, bhdr_T
*));
81 static bhdr_T
*mf_release
__ARGS((memfile_T
*, int));
82 static bhdr_T
*mf_alloc_bhdr
__ARGS((memfile_T
*, int));
83 static void mf_free_bhdr
__ARGS((bhdr_T
*));
84 static void mf_ins_free
__ARGS((memfile_T
*, bhdr_T
*));
85 static bhdr_T
*mf_rem_free
__ARGS((memfile_T
*));
86 static int mf_read
__ARGS((memfile_T
*, bhdr_T
*));
87 static int mf_write
__ARGS((memfile_T
*, bhdr_T
*));
88 static int mf_trans_add
__ARGS((memfile_T
*, bhdr_T
*));
89 static void mf_do_open
__ARGS((memfile_T
*, char_u
*, int));
92 * The functions for using a memfile:
94 * mf_open() open a new or existing memfile
95 * mf_open_file() open a swap file for an existing memfile
96 * mf_close() close (and delete) a memfile
97 * mf_new() create a new block in a memfile and lock it
98 * mf_get() get an existing block and lock it
99 * mf_put() unlock a block, may be marked for writing
100 * mf_free() remove a block
101 * mf_sync() sync changed parts of memfile to disk
102 * mf_release_all() release as much memory as possible
103 * mf_trans_del() may translate negative to positive block number
104 * mf_fullname() make file name full path (use before first :cd)
108 * Open an existing or new memory block file.
110 * fname: name of file to use (NULL means no file at all)
111 * Note: fname must have been allocated, it is not copied!
112 * If opening the file fails, fname is freed.
113 * flags: flags for open() call
115 * If fname != NULL and file cannot be opened, fail.
117 * return value: identifier for this memory block file.
120 mf_open(fname
, flags
)
127 #if defined(STATFS) && defined(UNIX) && !defined(__QNX__)
132 if ((mfp
= (memfile_T
*)alloc((unsigned)sizeof(memfile_T
))) == NULL
)
135 if (fname
== NULL
) /* no file for this memfile, use memory only */
137 mfp
->mf_fname
= NULL
;
138 mfp
->mf_ffname
= NULL
;
143 mf_do_open(mfp
, fname
, flags
); /* try to open the file */
145 /* if the file cannot be opened, return here */
153 mfp
->mf_free_first
= NULL
; /* free list is empty */
154 mfp
->mf_used_first
= NULL
; /* used list is empty */
155 mfp
->mf_used_last
= NULL
;
156 mfp
->mf_dirty
= FALSE
;
157 mfp
->mf_used_count
= 0;
158 for (i
= 0; i
< MEMHASHSIZE
; ++i
)
160 mfp
->mf_hash
[i
] = NULL
; /* hash lists are empty */
161 mfp
->mf_trans
[i
] = NULL
; /* trans lists are empty */
163 mfp
->mf_page_size
= MEMFILE_PAGE_SIZE
;
167 * Try to set the page size equal to the block size of the device.
168 * Speeds up I/O a lot.
169 * When recovering, the actual block size will be retrieved from block 0
170 * in ml_recover(). The size used here may be wrong, therefore
171 * mf_blocknr_max must be rounded up.
174 && fstatfs(mfp
->mf_fd
, &stf
, sizeof(struct statfs
), 0) == 0
175 && stf
.F_BSIZE
>= MIN_SWAP_PAGE_SIZE
176 && stf
.F_BSIZE
<= MAX_SWAP_PAGE_SIZE
)
177 mfp
->mf_page_size
= stf
.F_BSIZE
;
180 if (mfp
->mf_fd
< 0 || (flags
& (O_TRUNC
|O_EXCL
))
181 || (size
= lseek(mfp
->mf_fd
, (off_t
)0L, SEEK_END
)) <= 0)
182 mfp
->mf_blocknr_max
= 0; /* no file or empty file */
184 mfp
->mf_blocknr_max
= (blocknr_T
)((size
+ mfp
->mf_page_size
- 1)
185 / mfp
->mf_page_size
);
186 mfp
->mf_blocknr_min
= -1;
187 mfp
->mf_neg_count
= 0;
188 mfp
->mf_infile_count
= mfp
->mf_blocknr_max
;
191 * Compute maximum number of pages ('maxmem' is in Kbyte):
192 * 'mammem' * 1Kbyte / page-size-in-bytes.
193 * Avoid overflow by first reducing page size as much as possible.
197 unsigned page_size
= mfp
->mf_page_size
;
199 while (shift
> 0 && (page_size
& 1) == 0)
201 page_size
= page_size
>> 1;
204 mfp
->mf_used_count_max
= (p_mm
<< shift
) / page_size
;
205 if (mfp
->mf_used_count_max
< 10)
206 mfp
->mf_used_count_max
= 10;
213 * Open a file for an existing memfile. Used when updatecount set from 0 to
215 * If the file already exists, this fails.
216 * "fname" is the name of file to use (NULL means no file at all)
217 * Note: "fname" must have been allocated, it is not copied! If opening the
218 * file fails, "fname" is freed.
220 * return value: FAIL if file could not be opened, OK otherwise
223 mf_open_file(mfp
, fname
)
227 mf_do_open(mfp
, fname
, O_RDWR
|O_CREAT
|O_EXCL
); /* try to open the file */
232 mfp
->mf_dirty
= TRUE
;
237 * close a memory file and delete the associated file if 'del_file' is TRUE
240 mf_close(mfp
, del_file
)
245 NR_TRANS
*tp
, *tpnext
;
248 if (mfp
== NULL
) /* safety check */
252 if (close(mfp
->mf_fd
) < 0)
253 EMSG(_(e_swapclose
));
255 if (del_file
&& mfp
->mf_fname
!= NULL
)
256 mch_remove(mfp
->mf_fname
);
257 /* free entries in used list */
258 for (hp
= mfp
->mf_used_first
; hp
!= NULL
; hp
= nextp
)
260 total_mem_used
-= hp
->bh_page_count
* mfp
->mf_page_size
;
264 while (mfp
->mf_free_first
!= NULL
) /* free entries in free list */
265 vim_free(mf_rem_free(mfp
));
266 for (i
= 0; i
< MEMHASHSIZE
; ++i
) /* free entries in trans lists */
267 for (tp
= mfp
->mf_trans
[i
]; tp
!= NULL
; tp
= tpnext
)
269 tpnext
= tp
->nt_next
;
272 vim_free(mfp
->mf_fname
);
273 vim_free(mfp
->mf_ffname
);
278 * Close the swap file for a memfile. Used when 'swapfile' is reset.
281 mf_close_file(buf
, getlines
)
283 int getlines
; /* get all lines into memory? */
288 mfp
= buf
->b_ml
.ml_mfp
;
289 if (mfp
== NULL
|| mfp
->mf_fd
< 0) /* nothing to close */
294 /* get all blocks in memory by accessing all lines (clumsy!) */
295 mf_dont_release
= TRUE
;
296 for (lnum
= 1; lnum
<= buf
->b_ml
.ml_line_count
; ++lnum
)
297 (void)ml_get_buf(buf
, lnum
, FALSE
);
298 mf_dont_release
= FALSE
;
299 /* TODO: should check if all blocks are really in core */
302 if (close(mfp
->mf_fd
) < 0) /* close the file */
303 EMSG(_(e_swapclose
));
306 if (mfp
->mf_fname
!= NULL
)
308 mch_remove(mfp
->mf_fname
); /* delete the swap file */
309 vim_free(mfp
->mf_fname
);
310 vim_free(mfp
->mf_ffname
);
311 mfp
->mf_fname
= NULL
;
312 mfp
->mf_ffname
= NULL
;
317 * Set new size for a memfile. Used when block 0 of a swapfile has been read
318 * and the size it indicates differs from what was guessed.
321 mf_new_page_size(mfp
, new_size
)
325 /* Correct the memory used for block 0 to the new size, because it will be
326 * freed with that size later on. */
327 total_mem_used
+= new_size
- mfp
->mf_page_size
;
328 mfp
->mf_page_size
= new_size
;
334 * negative: TRUE if negative block number desired (data block)
337 mf_new(mfp
, negative
, page_count
)
342 bhdr_T
*hp
; /* new bhdr_T */
343 bhdr_T
*freep
; /* first block in free list */
347 * If we reached the maximum size for the used memory blocks, release one
348 * If a bhdr_T is returned, use it and adjust the page_count if necessary.
350 hp
= mf_release(mfp
, page_count
);
353 * Decide on the number to use:
354 * If there is a free block, use its number.
355 * Otherwise use mf_block_min for a negative number, mf_block_max for
358 freep
= mfp
->mf_free_first
;
359 if (!negative
&& freep
!= NULL
&& freep
->bh_page_count
>= page_count
)
362 * If the block in the free list has more pages, take only the number
363 * of pages needed and allocate a new bhdr_T with data
365 * If the number of pages matches and mf_release() did not return a
366 * bhdr_T, use the bhdr_T from the free list and allocate the data
368 * If the number of pages matches and mf_release() returned a bhdr_T,
369 * just use the number and free the bhdr_T from the free list
371 if (freep
->bh_page_count
> page_count
)
373 if (hp
== NULL
&& (hp
= mf_alloc_bhdr(mfp
, page_count
)) == NULL
)
375 hp
->bh_bnum
= freep
->bh_bnum
;
376 freep
->bh_bnum
+= page_count
;
377 freep
->bh_page_count
-= page_count
;
379 else if (hp
== NULL
) /* need to allocate memory for this block */
381 if ((p
= (char_u
*)alloc(mfp
->mf_page_size
* page_count
)) == NULL
)
383 hp
= mf_rem_free(mfp
);
386 else /* use the number, remove entry from free list */
388 freep
= mf_rem_free(mfp
);
389 hp
->bh_bnum
= freep
->bh_bnum
;
393 else /* get a new number */
395 if (hp
== NULL
&& (hp
= mf_alloc_bhdr(mfp
, page_count
)) == NULL
)
399 hp
->bh_bnum
= mfp
->mf_blocknr_min
--;
404 hp
->bh_bnum
= mfp
->mf_blocknr_max
;
405 mfp
->mf_blocknr_max
+= page_count
;
408 hp
->bh_flags
= BH_LOCKED
| BH_DIRTY
; /* new block is always dirty */
409 mfp
->mf_dirty
= TRUE
;
410 hp
->bh_page_count
= page_count
;
411 mf_ins_used(mfp
, hp
);
412 mf_ins_hash(mfp
, hp
);
415 * Init the data to all zero, to avoid reading uninitialized data.
416 * This also avoids that the passwd file ends up in the swap file!
418 (void)vim_memset((char *)(hp
->bh_data
), 0, (size_t)mfp
->mf_page_size
);
424 * get existing block 'nr' with 'page_count' pages
426 * Note: The caller should first check a negative nr with mf_trans_del()
429 mf_get(mfp
, nr
, page_count
)
436 if (nr
>= mfp
->mf_blocknr_max
|| nr
<= mfp
->mf_blocknr_min
)
440 * see if it is in the cache
442 hp
= mf_find_hash(mfp
, nr
);
443 if (hp
== NULL
) /* not in the hash list */
445 if (nr
< 0 || nr
>= mfp
->mf_infile_count
) /* can't be in the file */
448 /* could check here if the block is in the free list */
451 * Check if we need to flush an existing block.
452 * If so, use that block.
453 * If not, allocate a new block.
455 hp
= mf_release(mfp
, page_count
);
456 if (hp
== NULL
&& (hp
= mf_alloc_bhdr(mfp
, page_count
)) == NULL
)
461 hp
->bh_page_count
= page_count
;
462 if (mf_read(mfp
, hp
) == FAIL
) /* cannot read the block! */
470 mf_rem_used(mfp
, hp
); /* remove from list, insert in front below */
471 mf_rem_hash(mfp
, hp
);
474 hp
->bh_flags
|= BH_LOCKED
;
475 mf_ins_used(mfp
, hp
); /* put in front of used list */
476 mf_ins_hash(mfp
, hp
); /* put in front of hash list */
482 * release the block *hp
484 * dirty: Block must be written to file later
485 * infile: Block should be in file (needed for recovery)
487 * no return value, function cannot fail
490 mf_put(mfp
, hp
, dirty
, infile
)
498 flags
= hp
->bh_flags
;
500 if ((flags
& BH_LOCKED
) == 0)
501 EMSG(_("E293: block was not locked"));
506 mfp
->mf_dirty
= TRUE
;
508 hp
->bh_flags
= flags
;
510 mf_trans_add(mfp
, hp
); /* may translate negative in positive nr */
514 * block *hp is no longer in used, may put it in the free list of memfile *mfp
521 vim_free(hp
->bh_data
); /* free the memory */
522 mf_rem_hash(mfp
, hp
); /* get *hp out of the hash list */
523 mf_rem_used(mfp
, hp
); /* get *hp out of the used list */
526 vim_free(hp
); /* don't want negative numbers in free list */
530 mf_ins_free(mfp
, hp
); /* put *hp in the free list */
533 #if defined(__MORPHOS__) && defined(__libnix__)
534 /* function is missing in MorphOS libnix version */
535 extern unsigned long *__stdfiledes
;
538 fdtofh(int filedescriptor
)
540 return __stdfiledes
[filedescriptor
];
545 * Sync the memory file *mfp to disk.
547 * MFS_ALL If not given, blocks with negative numbers are not synced,
548 * even when they are dirty!
549 * MFS_STOP Stop syncing when a character becomes available, but sync at
551 * MFS_FLUSH Make sure buffers are flushed to disk, so they will survive a
553 * MFS_ZERO Only write block 0.
555 * Return FAIL for failure, OK otherwise
564 #if defined(SYNC_DUP_CLOSE) && !defined(MSDOS)
567 int got_int_save
= got_int
;
569 if (mfp
->mf_fd
< 0) /* there is no file, nothing to do */
571 mfp
->mf_dirty
= FALSE
;
575 /* Only a CTRL-C while writing will break us here, not one typed
580 * sync from last to first (may reduce the probability of an inconsistent
581 * file) If a write fails, it is very likely caused by a full filesystem.
582 * Then we only try to write blocks within the existing file. If that also
583 * fails then we give up.
586 for (hp
= mfp
->mf_used_last
; hp
!= NULL
; hp
= hp
->bh_prev
)
587 if (((flags
& MFS_ALL
) || hp
->bh_bnum
>= 0)
588 && (hp
->bh_flags
& BH_DIRTY
)
589 && (status
== OK
|| (hp
->bh_bnum
>= 0
590 && hp
->bh_bnum
< mfp
->mf_infile_count
)))
592 if ((flags
& MFS_ZERO
) && hp
->bh_bnum
!= 0)
594 if (mf_write(mfp
, hp
) == FAIL
)
596 if (status
== FAIL
) /* double error: quit syncing */
600 if (flags
& MFS_STOP
)
602 /* Stop when char available now. */
613 * If the whole list is flushed, the memfile is not dirty anymore.
614 * In case of an error this flag is also set, to avoid trying all the time.
616 if (hp
== NULL
|| status
== FAIL
)
617 mfp
->mf_dirty
= FALSE
;
619 if ((flags
& MFS_FLUSH
) && *p_sws
!= NUL
)
624 * most Unixes have the very useful fsync() function, just what we need.
625 * However, with OS/2 and EMX it is also available, but there are
626 * reports of bad problems with it (a bug in HPFS.IFS).
627 * So we disable use of it here in case someone tries to be smart
628 * and changes os_os2_cfg.h... (even though there is no __EMX__ test
629 * in the #if, as __EMX__ does not have sync(); we hope for a timely
630 * sync from the system itself).
632 # if defined(__EMX__)
633 error
"Dont use fsync with EMX! Read emxdoc.doc or emxfix01.doc for info."
635 if (STRCMP(p_sws
, "fsync") == 0)
637 if (fsync(mfp
->mf_fd
))
642 /* OpenNT is strictly POSIX (Benzinger) */
643 /* Tandem/Himalaya NSK-OSS doesn't have sync() */
644 # if defined(__OPENNT) || defined(__TANDEM)
651 if (STRCMP(p_sws
, "fsync") == 0)
653 if (fsync(mfp
->mf_fd
))
658 if (_dos_commit(mfp
->mf_fd
))
661 # ifdef SYNC_DUP_CLOSE
663 * Win32 is a bit more work: Duplicate the file handle and close it.
664 * This should flush the file to disk.
666 if ((fd
= dup(mfp
->mf_fd
)) >= 0)
671 # if defined(__AROS__) || defined(__amigaos4__)
672 if (fsync(mfp
->mf_fd
) != 0)
676 * Flush() only exists for AmigaDos 2.0.
677 * For 1.3 it should be done with close() + open(), but then the risk
678 * is that the open() may fail and lose the file....
685 struct UFB
*fp
= chkufb(mfp
->mf_fd
);
691 # if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
693 # if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__)
694 /* Have function (in libnix at least),
695 * but ain't got no prototype anywhere. */
696 extern unsigned long fdtofh(int filedescriptor
);
698 # if !defined(__libnix__)
701 BPTR fh
= (BPTR
)fdtofh(mfp
->mf_fd
);
707 # else /* assume Manx */
708 Flush(_devtab
[mfp
->mf_fd
].fd
);
715 got_int
|= got_int_save
;
721 * For all blocks in memory file *mfp that have a positive block number set
722 * the dirty flag. These are blocks that need to be written to a newly
731 for (hp
= mfp
->mf_used_last
; hp
!= NULL
; hp
= hp
->bh_prev
)
733 hp
->bh_flags
|= BH_DIRTY
;
734 mfp
->mf_dirty
= TRUE
;
738 * insert block *hp in front of hashlist of memfile *mfp
748 hash
= MEMHASH(hp
->bh_bnum
);
749 hhp
= mfp
->mf_hash
[hash
];
750 hp
->bh_hash_next
= hhp
;
751 hp
->bh_hash_prev
= NULL
;
753 hhp
->bh_hash_prev
= hp
;
754 mfp
->mf_hash
[hash
] = hp
;
758 * remove block *hp from hashlist of memfile list *mfp
765 if (hp
->bh_hash_prev
== NULL
)
766 mfp
->mf_hash
[MEMHASH(hp
->bh_bnum
)] = hp
->bh_hash_next
;
768 hp
->bh_hash_prev
->bh_hash_next
= hp
->bh_hash_next
;
770 if (hp
->bh_hash_next
)
771 hp
->bh_hash_next
->bh_hash_prev
= hp
->bh_hash_prev
;
775 * look in hash lists of memfile *mfp for block header with number 'nr'
778 mf_find_hash(mfp
, nr
)
784 for (hp
= mfp
->mf_hash
[MEMHASH(nr
)]; hp
!= NULL
; hp
= hp
->bh_hash_next
)
785 if (hp
->bh_bnum
== nr
)
791 * insert block *hp in front of used list of memfile *mfp
798 hp
->bh_next
= mfp
->mf_used_first
;
799 mfp
->mf_used_first
= hp
;
801 if (hp
->bh_next
== NULL
) /* list was empty, adjust last pointer */
802 mfp
->mf_used_last
= hp
;
804 hp
->bh_next
->bh_prev
= hp
;
805 mfp
->mf_used_count
+= hp
->bh_page_count
;
806 total_mem_used
+= hp
->bh_page_count
* mfp
->mf_page_size
;
810 * remove block *hp from used list of memfile *mfp
817 if (hp
->bh_next
== NULL
) /* last block in used list */
818 mfp
->mf_used_last
= hp
->bh_prev
;
820 hp
->bh_next
->bh_prev
= hp
->bh_prev
;
821 if (hp
->bh_prev
== NULL
) /* first block in used list */
822 mfp
->mf_used_first
= hp
->bh_next
;
824 hp
->bh_prev
->bh_next
= hp
->bh_next
;
825 mfp
->mf_used_count
-= hp
->bh_page_count
;
826 total_mem_used
-= hp
->bh_page_count
* mfp
->mf_page_size
;
830 * Release the least recently used block from the used list if the number
831 * of used memory blocks gets to big.
833 * Return the block header to the caller, including the memory block, so
834 * it can be re-used. Make sure the page_count is right.
837 mf_release(mfp
, page_count
)
845 /* don't release while in mf_close_file() */
850 * Need to release a block if the number of blocks for this memfile is
851 * higher than the maximum or total memory used is over 'maxmemtot'
853 need_release
= ((mfp
->mf_used_count
>= mfp
->mf_used_count_max
)
854 || (total_mem_used
>> 10) >= (long_u
)p_mmt
);
857 * Try to create a swap file if the amount of memory used is getting too
860 if (mfp
->mf_fd
< 0 && need_release
&& p_uc
)
862 /* find for which buffer this memfile is */
863 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
864 if (buf
->b_ml
.ml_mfp
== mfp
)
866 if (buf
!= NULL
&& buf
->b_may_swap
)
871 * don't release a block if
872 * there is no file for this memfile
874 * the number of blocks for this memfile is lower than the maximum
876 * total memory used is not up to 'maxmemtot'
878 if (mfp
->mf_fd
< 0 || !need_release
)
881 for (hp
= mfp
->mf_used_last
; hp
!= NULL
; hp
= hp
->bh_prev
)
882 if (!(hp
->bh_flags
& BH_LOCKED
))
884 if (hp
== NULL
) /* not a single one that can be released */
888 * If the block is dirty, write it.
889 * If the write fails we don't free it.
891 if ((hp
->bh_flags
& BH_DIRTY
) && mf_write(mfp
, hp
) == FAIL
)
894 mf_rem_used(mfp
, hp
);
895 mf_rem_hash(mfp
, hp
);
898 * If a bhdr_T is returned, make sure that the page_count of bh_data is
901 if (hp
->bh_page_count
!= page_count
)
903 vim_free(hp
->bh_data
);
904 if ((hp
->bh_data
= alloc(mfp
->mf_page_size
* page_count
)) == NULL
)
909 hp
->bh_page_count
= page_count
;
915 * release as many blocks as possible
916 * Used in case of out of memory
918 * return TRUE if any memory was released
928 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
930 mfp
= buf
->b_ml
.ml_mfp
;
933 /* If no swap file yet, may open one */
934 if (mfp
->mf_fd
< 0 && buf
->b_may_swap
)
937 /* only if there is a swapfile */
940 for (hp
= mfp
->mf_used_last
; hp
!= NULL
; )
942 if (!(hp
->bh_flags
& BH_LOCKED
)
943 && (!(hp
->bh_flags
& BH_DIRTY
)
944 || mf_write(mfp
, hp
) != FAIL
))
946 mf_rem_used(mfp
, hp
);
947 mf_rem_hash(mfp
, hp
);
949 hp
= mfp
->mf_used_last
; /* re-start, list was changed */
962 * Allocate a block header and a block of memory for it
965 mf_alloc_bhdr(mfp
, page_count
)
971 if ((hp
= (bhdr_T
*)alloc((unsigned)sizeof(bhdr_T
))) != NULL
)
973 if ((hp
->bh_data
= (char_u
*)alloc(mfp
->mf_page_size
* page_count
))
976 vim_free(hp
); /* not enough memory */
979 hp
->bh_page_count
= page_count
;
985 * Free a block header and the block of memory for it
991 vim_free(hp
->bh_data
);
996 * insert entry *hp in the free list
1003 hp
->bh_next
= mfp
->mf_free_first
;
1004 mfp
->mf_free_first
= hp
;
1008 * remove the first entry from the free list and return a pointer to it
1009 * Note: caller must check that mfp->mf_free_first is not NULL!
1017 hp
= mfp
->mf_free_first
;
1018 mfp
->mf_free_first
= hp
->bh_next
;
1023 * read a block from disk
1025 * Return FAIL for failure, OK otherwise
1036 if (mfp
->mf_fd
< 0) /* there is no file, can't read */
1039 page_size
= mfp
->mf_page_size
;
1040 offset
= (off_t
)page_size
* hp
->bh_bnum
;
1041 size
= page_size
* hp
->bh_page_count
;
1042 if (lseek(mfp
->mf_fd
, offset
, SEEK_SET
) != offset
)
1044 PERROR(_("E294: Seek error in swap file read"));
1047 if ((unsigned)vim_read(mfp
->mf_fd
, hp
->bh_data
, size
) != size
)
1049 PERROR(_("E295: Read error in swap file"));
1056 * write a block to disk
1058 * Return FAIL for failure, OK otherwise
1065 off_t offset
; /* offset in the file */
1066 blocknr_T nr
; /* block nr which is being written */
1068 unsigned page_size
; /* number of bytes in a page */
1069 unsigned page_count
; /* number of pages written */
1070 unsigned size
; /* number of bytes written */
1072 if (mfp
->mf_fd
< 0) /* there is no file, can't write */
1075 if (hp
->bh_bnum
< 0) /* must assign file block number */
1076 if (mf_trans_add(mfp
, hp
) == FAIL
)
1079 page_size
= mfp
->mf_page_size
;
1082 * We don't want gaps in the file. Write the blocks in front of *hp
1083 * to extend the file.
1084 * If block 'mf_infile_count' is not in the hash list, it has been
1085 * freed. Fill the space in the file with data from the current block.
1090 if (nr
> mfp
->mf_infile_count
) /* beyond end of file */
1092 nr
= mfp
->mf_infile_count
;
1093 hp2
= mf_find_hash(mfp
, nr
); /* NULL catched below */
1098 offset
= (off_t
)page_size
* nr
;
1099 if (lseek(mfp
->mf_fd
, offset
, SEEK_SET
) != offset
)
1101 PERROR(_("E296: Seek error in swap file write"));
1104 if (hp2
== NULL
) /* freed block, fill with dummy data */
1107 page_count
= hp2
->bh_page_count
;
1108 size
= page_size
* page_count
;
1109 if ((unsigned)vim_write(mfp
->mf_fd
,
1110 (hp2
== NULL
? hp
: hp2
)->bh_data
, size
) != size
)
1113 * Avoid repeating the error message, this mostly happens when the
1114 * disk is full. We give the message again only after a successful
1115 * write or when hitting a key. We keep on trying, in case some
1116 * space becomes available.
1118 if (!did_swapwrite_msg
)
1119 EMSG(_("E297: Write error in swap file"));
1120 did_swapwrite_msg
= TRUE
;
1123 did_swapwrite_msg
= FALSE
;
1124 if (hp2
!= NULL
) /* written a non-dummy block */
1125 hp2
->bh_flags
&= ~BH_DIRTY
;
1126 /* appended to the file */
1127 if (nr
+ (blocknr_T
)page_count
> mfp
->mf_infile_count
)
1128 mfp
->mf_infile_count
= nr
+ page_count
;
1129 if (nr
== hp
->bh_bnum
) /* written the desired block */
1136 * Make block number for *hp positive and add it to the translation list
1138 * Return FAIL for failure, OK otherwise
1141 mf_trans_add(mfp
, hp
)
1151 if (hp
->bh_bnum
>= 0) /* it's already positive */
1154 if ((np
= (NR_TRANS
*)alloc((unsigned)sizeof(NR_TRANS
))) == NULL
)
1158 * get a new number for the block.
1159 * If the first item in the free list has sufficient pages, use its number
1160 * Otherwise use mf_blocknr_max.
1162 freep
= mfp
->mf_free_first
;
1163 page_count
= hp
->bh_page_count
;
1164 if (freep
!= NULL
&& freep
->bh_page_count
>= page_count
)
1166 new_bnum
= freep
->bh_bnum
;
1168 * If the page count of the free block was larger, recude it.
1169 * If the page count matches, remove the block from the free list
1171 if (freep
->bh_page_count
> page_count
)
1173 freep
->bh_bnum
+= page_count
;
1174 freep
->bh_page_count
-= page_count
;
1178 freep
= mf_rem_free(mfp
);
1184 new_bnum
= mfp
->mf_blocknr_max
;
1185 mfp
->mf_blocknr_max
+= page_count
;
1188 np
->nt_old_bnum
= hp
->bh_bnum
; /* adjust number */
1189 np
->nt_new_bnum
= new_bnum
;
1191 mf_rem_hash(mfp
, hp
); /* remove from old hash list */
1192 hp
->bh_bnum
= new_bnum
;
1193 mf_ins_hash(mfp
, hp
); /* insert in new hash list */
1195 hash
= MEMHASH(np
->nt_old_bnum
); /* insert in trans list */
1196 np
->nt_next
= mfp
->mf_trans
[hash
];
1197 mfp
->mf_trans
[hash
] = np
;
1198 if (np
->nt_next
!= NULL
)
1199 np
->nt_next
->nt_prev
= np
;
1206 * Lookup a translation from the trans lists and delete the entry
1208 * Return the positive new number when found, the old number when not found
1211 mf_trans_del(mfp
, old_nr
)
1219 hash
= MEMHASH(old_nr
);
1220 for (np
= mfp
->mf_trans
[hash
]; np
!= NULL
; np
= np
->nt_next
)
1221 if (np
->nt_old_bnum
== old_nr
)
1223 if (np
== NULL
) /* not found */
1226 mfp
->mf_neg_count
--;
1227 new_bnum
= np
->nt_new_bnum
;
1228 if (np
->nt_prev
!= NULL
) /* remove entry from the trans list */
1229 np
->nt_prev
->nt_next
= np
->nt_next
;
1231 mfp
->mf_trans
[hash
] = np
->nt_next
;
1232 if (np
->nt_next
!= NULL
)
1233 np
->nt_next
->nt_prev
= np
->nt_prev
;
1240 * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
1241 * Only called when creating or renaming the swapfile. Either way it's a new
1242 * name so we must work out the full path name.
1248 mfp
->mf_ffname
= FullName_save(mfp
->mf_fname
, FALSE
);
1252 * Make the name of the file used for the memfile a full path.
1253 * Used before doing a :cd
1259 if (mfp
!= NULL
&& mfp
->mf_fname
!= NULL
&& mfp
->mf_ffname
!= NULL
)
1261 vim_free(mfp
->mf_fname
);
1262 mfp
->mf_fname
= mfp
->mf_ffname
;
1263 mfp
->mf_ffname
= NULL
;
1268 * return TRUE if there are any translations pending for 'mfp'
1274 return (mfp
->mf_fname
!= NULL
&& mfp
->mf_neg_count
> 0);
1278 * Open a swap file for a memfile.
1279 * The "fname" must be in allocated memory, and is consumed (also when an
1283 mf_do_open(mfp
, fname
, flags
)
1286 int flags
; /* flags for open() */
1292 mfp
->mf_fname
= fname
;
1295 * Get the full path name before the open, because this is
1296 * not possible after the open on the Amiga.
1297 * fname cannot be NameBuff, because it must have been allocated.
1300 #if defined(MSDOS) || defined(MSWIN) || defined(RISCOS)
1302 * A ":!cd e:xxx" may change the directory without us knowning, use the
1303 * full pathname always. Careful: This frees fname!
1310 * Extra security check: When creating a swap file it really shouldn't
1311 * exist yet. If there is a symbolic link, this is most likely an attack.
1313 if ((flags
& O_CREAT
) && mch_lstat((char *)mfp
->mf_fname
, &sb
) >= 0)
1316 EMSG(_("E300: Swap file already exists (symlink attack?)"));
1322 * try to open the file
1324 flags
|= O_EXTRA
| O_NOFOLLOW
;
1326 /* Prevent handle inheritance that cause problems with Cscope
1327 * (swap file may not be deleted if cscope connection was open after
1329 flags
|= O_NOINHERIT
;
1331 mfp
->mf_fd
= mch_open_rw((char *)mfp
->mf_fname
, flags
);
1335 * If the file cannot be opened, use memory only
1339 vim_free(mfp
->mf_fname
);
1340 vim_free(mfp
->mf_ffname
);
1341 mfp
->mf_fname
= NULL
;
1342 mfp
->mf_ffname
= NULL
;
1346 #ifdef HAVE_FD_CLOEXEC
1347 int fdflags
= fcntl(mfp
->mf_fd
, F_GETFD
);
1348 if (fdflags
>= 0 && (fdflags
& FD_CLOEXEC
) == 0)
1349 fcntl(mfp
->mf_fd
, F_SETFD
, fdflags
| FD_CLOEXEC
);
1352 mch_copy_sec(fname
, mfp
->mf_fname
);
1354 mch_hide(mfp
->mf_fname
); /* try setting the 'hidden' flag */