/* vi:set ts=8 sts=4 sw=4:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* memfile.c: Contains the functions for handling blocks of memory which can
* be stored in a file. This is the implementation of a sort of virtual memory.
*
* A memfile consists of a sequence of blocks. The blocks numbered from 0
* upwards have been assigned a place in the actual file. The block number
* is equal to the page number in the file. The
* blocks with negative numbers are currently in memory only. They can be
* assigned a place in the file when too much memory is being used. At that
* moment they get a new, positive, number. A list is used for translation of
* negative to positive numbers.
*
* The size of a block is a multiple of a page size, normally the page size of
* the device the file is on. Most blocks are 1 page long. A Block of multiple
* pages is used for a line that does not fit in a single page.
*
* Each block can be in memory and/or in a file. The block stays in memory
* as long as it is locked. If it is no longer locked it can be swapped out to
* the file. It is only written to the file if it has been changed.
*
* Under normal operation the file is created when opening the memory file and
* deleted when closing the memory file. Only with recovery an existing memory
* file is opened.
*/
#if defined MSDOS || defined(WIN32) || defined(_WIN64)
# include <io.h> /* for lseek(), must be before vim.h */
#endif
#include "vim.h"
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
/*
* Some systems have the page size in statfs.f_bsize, some in stat.st_blksize
*/
#ifdef HAVE_ST_BLKSIZE
# define STATFS stat
# define F_BSIZE st_blksize
# define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
#else
# ifdef HAVE_SYS_STATFS_H
# include <sys/statfs.h>
# define STATFS statfs
# define F_BSIZE f_bsize
# ifdef __MINT__ /* do we still need this? */
# define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
# endif
# endif
#endif
/*
* for Amiga Dos 2.0x we use Flush
*/
#ifdef AMIGA
# ifdef FEAT_ARP
extern int dos2; /* this is in os_amiga.c */
# endif
# ifdef SASC
# include <proto/dos.h>
# include <ios1.h> /* for chkufb() */
# endif
#endif
#define MEMFILE_PAGE_SIZE 4096 /* default page size */
static long_u total_mem_used = 0; /* total memory used for memfiles */
static int dont_release = FALSE; /* don't release blocks */
static void mf_ins_hash __ARGS((memfile_T *, bhdr_T *));
static void mf_rem_hash __ARGS((memfile_T *, bhdr_T *));
static bhdr_T *mf_find_hash __ARGS((memfile_T *, blocknr_T));
static void mf_ins_used __ARGS((memfile_T *, bhdr_T *));
static void mf_rem_used __ARGS((memfile_T *, bhdr_T *));
static bhdr_T *mf_release __ARGS((memfile_T *, int));
static bhdr_T *mf_alloc_bhdr __ARGS((memfile_T *, int));
static void mf_free_bhdr __ARGS((bhdr_T *));
static void mf_ins_free __ARGS((memfile_T *, bhdr_T *));
static bhdr_T *mf_rem_free __ARGS((memfile_T *));
static int mf_read __ARGS((memfile_T *, bhdr_T *));
static int mf_write __ARGS((memfile_T *, bhdr_T *));
static int mf_trans_add __ARGS((memfile_T *, bhdr_T *));
static void mf_do_open __ARGS((memfile_T *, char_u *, int));
/*
* The functions for using a memfile: