aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2014-11-06 17:09:39 -0500
committerJustin M. Keyes <justinkz@gmail.com>2014-11-06 17:09:39 -0500
commit8455fd5e338624a76bd2e4b3176f51eeac76ff7b (patch)
tree9a42ed343d477515aa34b525f2ccdabea40026f7
parent7c7874d11b07167e3ed2b5a2fc21484ff1c6be34 (diff)
parent8386255803c6a643d05e158e2cba9bcb10a09b75 (diff)
downloadrneovim-8455fd5e338624a76bd2e4b3176f51eeac76ff7b.tar.gz
rneovim-8455fd5e338624a76bd2e4b3176f51eeac76ff7b.tar.bz2
rneovim-8455fd5e338624a76bd2e4b3176f51eeac76ff7b.zip
Merge pull request #1340 from elmart/remove-long_u
Remove project-specific integer types: long_u. (2)
-rw-r--r--clint-files.txt23
-rw-r--r--src/nvim/CMakeLists.txt1
-rw-r--r--src/nvim/buffer_defs.h1
-rw-r--r--src/nvim/eval.c18
-rw-r--r--src/nvim/memfile.c1066
-rw-r--r--src/nvim/memfile.h10
-rw-r--r--src/nvim/memfile_defs.h191
-rw-r--r--src/nvim/memline.c120
-rw-r--r--src/nvim/option.c2
9 files changed, 662 insertions, 770 deletions
diff --git a/clint-files.txt b/clint-files.txt
index 99f2fecc7c..a0aec7a31c 100644
--- a/clint-files.txt
+++ b/clint-files.txt
@@ -1,10 +1,10 @@
src/nvim/api/buffer.c
src/nvim/api/buffer.h
src/nvim/api/private/defs.h
-src/nvim/api/private/helpers.c
-src/nvim/api/private/helpers.h
src/nvim/api/private/handle.c
src/nvim/api/private/handle.h
+src/nvim/api/private/helpers.c
+src/nvim/api/private/helpers.h
src/nvim/api/tabpage.c
src/nvim/api/tabpage.h
src/nvim/api/vim.c
@@ -22,6 +22,15 @@ src/nvim/log.h
src/nvim/map.c
src/nvim/map.h
src/nvim/map_defs.h
+src/nvim/memfile.c
+src/nvim/memfile.h
+src/nvim/memfile_defs.h
+src/nvim/msgpack_rpc/channel.c
+src/nvim/msgpack_rpc/channel.h
+src/nvim/msgpack_rpc/helpers.c
+src/nvim/msgpack_rpc/helpers.h
+src/nvim/msgpack_rpc/server.c
+src/nvim/msgpack_rpc/server.h
src/nvim/os/env.c
src/nvim/os/event.c
src/nvim/os/event.h
@@ -42,13 +51,7 @@ src/nvim/os/signal.c
src/nvim/os/signal.h
src/nvim/os/time.c
src/nvim/os/time.h
-src/nvim/msgpack_rpc/server.c
-src/nvim/msgpack_rpc/server.h
-src/nvim/msgpack_rpc/channel.c
-src/nvim/msgpack_rpc/channel.h
-src/nvim/msgpack_rpc/helpers.c
-src/nvim/msgpack_rpc/helpers.h
-src/nvim/tempfile.c
-src/nvim/tempfile.h
src/nvim/profile.c
src/nvim/profile.h
+src/nvim/tempfile.c
+src/nvim/tempfile.h
diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt
index 57fb0fb50e..5b97cf5f40 100644
--- a/src/nvim/CMakeLists.txt
+++ b/src/nvim/CMakeLists.txt
@@ -45,6 +45,7 @@ set(CONV_SOURCES
hashtab.c
log.c
map.c
+ memfile.c
memory.c
misc2.c
profile.c
diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h
index 4ea288c258..094d6dfd05 100644
--- a/src/nvim/buffer_defs.h
+++ b/src/nvim/buffer_defs.h
@@ -78,7 +78,6 @@ typedef struct wininfo_S wininfo_T;
typedef struct frame_S frame_T;
typedef int scid_T; /* script ID */
typedef struct file_buffer buf_T; /* forward declaration */
-typedef struct memfile memfile_T;
// for struct memline (it needs memfile_T)
#include "nvim/memline_defs.h"
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index 3e7c3d05c1..d75807800c 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -10,6 +10,7 @@
* eval.c: Expression evaluation.
*/
+#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdarg.h>
@@ -3034,10 +3035,10 @@ static char_u *cat_prefix_varname(int prefix, char_u *name)
*/
char_u *get_user_var_name(expand_T *xp, int idx)
{
- static long_u gdone;
- static long_u bdone;
- static long_u wdone;
- static long_u tdone;
+ static size_t gdone;
+ static size_t bdone;
+ static size_t wdone;
+ static size_t tdone;
static int vidx;
static hashitem_T *hi;
hashtab_T *ht;
@@ -11749,7 +11750,7 @@ static void f_readfile(typval_T *argvars, typval_T *rettv)
if (*p == '\n' || readlen <= 0) {
listitem_T *li;
char_u *s = NULL;
- long_u len = p - start;
+ size_t len = p - start;
/* Finished a line. Remove CRs before NL. */
if (readlen > 0 && !binary) {
@@ -11760,9 +11761,10 @@ static void f_readfile(typval_T *argvars, typval_T *rettv)
while (prevlen > 0 && prev[prevlen - 1] == '\r')
--prevlen;
}
- if (prevlen == 0)
+ if (prevlen == 0) {
+ assert(len < INT_MAX);
s = vim_strnsave(start, (int)len);
- else {
+ } else {
/* Change "prev" buffer to be the right size. This way
* the bytes are only copied once, and very long lines are
* allocated only once. */
@@ -18152,7 +18154,7 @@ static char_u *autoload_name(char_u *name)
*/
char_u *get_user_func_name(expand_T *xp, int idx)
{
- static long_u done;
+ static size_t done;
static hashitem_T *hi;
ufunc_T *fp;
diff --git a/src/nvim/memfile.c b/src/nvim/memfile.c
index 827cff2299..1656acb689 100644
--- a/src/nvim/memfile.c
+++ b/src/nvim/memfile.c
@@ -1,39 +1,54 @@
/*
- * VIM - Vi IMproved by Bram Moolenaar
+ * 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.
- */
-
+/// An abstraction to handle 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:
+/// - 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.
+/// - 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.
+///
+/// The functions for using a memfile:
+///
+/// mf_open() open a new or existing memfile
+/// mf_open_file() open a swap file for an existing memfile
+/// mf_close() close (and delete) a memfile
+/// mf_new() create a new block in a memfile and lock it
+/// mf_get() get an existing block and lock it
+/// mf_put() unlock a block, may be marked for writing
+/// mf_free() remove a block
+/// mf_sync() sync changed parts of memfile to disk
+/// mf_release_all() release as much memory as possible
+/// mf_trans_del() may translate negative to positive block number
+/// mf_fullname() make file name full path (use before first :cd)
+
+#include <assert.h>
#include <errno.h>
#include <inttypes.h>
+#include <limits.h>
#include <string.h>
+#include <stdbool.h>
#include "nvim/vim.h"
#include "nvim/ascii.h"
@@ -49,101 +64,88 @@
#include "nvim/ui.h"
#include "nvim/os/os.h"
-#define MEMFILE_PAGE_SIZE 4096 /* default page size */
+#define MEMFILE_PAGE_SIZE 4096 /// default page size
-static long_u total_mem_used = 0; /* total memory used for memfiles */
+static size_t total_mem_used = 0; /// total memory used for memfiles
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "memfile.c.generated.h"
#endif
-/*
- * The functions for using a memfile:
- *
- * mf_open() open a new or existing memfile
- * mf_open_file() open a swap file for an existing memfile
- * mf_close() close (and delete) a memfile
- * mf_new() create a new block in a memfile and lock it
- * mf_get() get an existing block and lock it
- * mf_put() unlock a block, may be marked for writing
- * mf_free() remove a block
- * mf_sync() sync changed parts of memfile to disk
- * mf_release_all() release as much memory as possible
- * mf_trans_del() may translate negative to positive block number
- * mf_fullname() make file name full path (use before first :cd)
- */
-/*
- * Open an existing or new memory block file.
- *
- * fname: name of file to use (NULL means no file at all)
- * Note: fname must have been allocated, it is not copied!
- * If opening the file fails, fname is freed.
- * flags: flags for open() call
- *
- * If fname != NULL and file cannot be opened, fail.
- *
- * return value: identifier for this memory block file.
- */
+/// Open a new or existing memory block file.
+///
+/// @param fname Name of file to use.
+/// - If NULL, it means no file (use memory only).
+/// - If not NULL:
+/// * Should correspond to an existing file.
+/// * String must have been allocated (it is not copied).
+/// * If opening the file fails, it is freed and function fails.
+
+/// @param flags Flags for open() call.
+///
+/// @return - The open memory file, on success.
+/// - NULL, on failure.
memfile_T *mf_open(char_u *fname, int flags)
{
off_t size;
memfile_T *mfp = xmalloc(sizeof(memfile_T));
- if (fname == NULL) { /* no file for this memfile, use memory only */
+ if (fname == NULL) { // no file, use memory only
mfp->mf_fname = NULL;
mfp->mf_ffname = NULL;
mfp->mf_fd = -1;
- } else {
- mf_do_open(mfp, fname, flags); /* try to open the file */
+ } else { // try to open the file
+ mf_do_open(mfp, fname, flags);
- /* if the file cannot be opened, return here */
- if (mfp->mf_fd < 0) {
+ if (mfp->mf_fd < 0) { // fail if file could not be opened
free(mfp);
return NULL;
}
}
- mfp->mf_free_first = NULL; /* free list is empty */
- mfp->mf_used_first = NULL; /* used list is empty */
+ mfp->mf_free_first = NULL; // free list is empty
+ mfp->mf_used_first = NULL; // used list is empty
mfp->mf_used_last = NULL;
- mfp->mf_dirty = FALSE;
+ mfp->mf_dirty = false;
mfp->mf_used_count = 0;
mf_hash_init(&mfp->mf_hash);
mf_hash_init(&mfp->mf_trans);
mfp->mf_page_size = MEMFILE_PAGE_SIZE;
- /*
- * Try to set the page size equal to the block size of the device.
- * Speeds up I/O a lot.
- * When recovering, the actual block size will be retrieved from block 0
- * in ml_recover(). The size used here may be wrong, therefore
- * mf_blocknr_max must be rounded up.
- */
+ // Try to set the page size equal to device's block size. Speeds up I/O a lot.
FileInfo file_info;
if (mfp->mf_fd >= 0 && os_fileinfo_fd(mfp->mf_fd, &file_info)) {
uint64_t blocksize = os_fileinfo_blocksize(&file_info);
if (blocksize >= MIN_SWAP_PAGE_SIZE && blocksize <= MAX_SWAP_PAGE_SIZE) {
- mfp->mf_page_size = blocksize;
+ assert(blocksize <= UINT_MAX);
+ mfp->mf_page_size = (unsigned)blocksize;
}
}
- if (mfp->mf_fd < 0 || (flags & (O_TRUNC|O_EXCL))
- || (size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0)
- mfp->mf_blocknr_max = 0; /* no file or empty file */
- else
- mfp->mf_blocknr_max = (blocknr_T)((size + mfp->mf_page_size - 1)
- / mfp->mf_page_size);
+ // When recovering, the actual block size will be retrieved from block 0
+ // in ml_recover(). The size used here may be wrong, therefore mf_blocknr_max
+ // must be rounded up.
+ if (mfp->mf_fd < 0
+ || (flags & (O_TRUNC|O_EXCL))
+ || (size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0) {
+ // no file or empty file
+ mfp->mf_blocknr_max = 0;
+ } else {
+ assert(sizeof(off_t) <= sizeof(blocknr_T)
+ && mfp->mf_page_size > 0
+ && mfp->mf_page_size - 1 <= INT64_MAX - size);
+ mfp->mf_blocknr_max = (((blocknr_T)size + mfp->mf_page_size - 1)
+ / mfp->mf_page_size);
+ }
mfp->mf_blocknr_min = -1;
mfp->mf_neg_count = 0;
mfp->mf_infile_count = mfp->mf_blocknr_max;
- /*
- * Compute maximum number of pages ('maxmem' is in Kbyte):
- * 'mammem' * 1Kbyte / page-size-in-bytes.
- * Avoid overflow by first reducing page size as much as possible.
- */
+ // Compute maximum number of pages ('maxmem' is in Kbytes):
+ // 'mammem' * 1Kbyte / page-size-in-bytes.
+ // Avoid overflow by first reducing page size as much as possible.
{
int shift = 10;
unsigned page_size = mfp->mf_page_size;
@@ -152,7 +154,10 @@ memfile_T *mf_open(char_u *fname, int flags)
page_size /= 2;
--shift;
}
- mfp->mf_used_count_max = (p_mm << shift) / page_size;
+
+ assert(p_mm <= LONG_MAX >> shift); // check we don't overflow
+ assert((uintmax_t)(p_mm << shift) <= UINT_MAX); // check we can cast safely
+ mfp->mf_used_count_max = (unsigned)(p_mm << shift) / page_size;
if (mfp->mf_used_count_max < 10)
mfp->mf_used_count_max = 10;
}
@@ -160,88 +165,84 @@ memfile_T *mf_open(char_u *fname, int flags)
return mfp;
}
-/*
- * Open a file for an existing memfile. Used when updatecount set from 0 to
- * some value.
- * If the file already exists, this fails.
- * "fname" is the name of file to use (NULL means no file at all)
- * Note: "fname" must have been allocated, it is not copied! If opening the
- * file fails, "fname" is freed.
- *
- * return value: FAIL if file could not be opened, OK otherwise
- */
+/// Open a file for an existing memfile.
+///
+/// Used when updatecount set from 0 to some value.
+///
+/// @param fname Name of file to use.
+/// - If NULL, it means no file (use memory only).
+/// - If not NULL:
+/// * Should correspond to an existing file.
+/// * String must have been allocated (it is not copied).
+/// * If opening the file fails, it is freed and function fails.
+///
+/// @return OK On success.
+/// FAIL If file could not be opened.
int mf_open_file(memfile_T *mfp, char_u *fname)
{
- mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); /* try to open the file */
+ mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); // try to open the file
if (mfp->mf_fd < 0)
return FAIL;
- mfp->mf_dirty = TRUE;
+ mfp->mf_dirty = true;
return OK;
}
-/*
- * Close a memory file and delete the associated file if 'del_file' is TRUE.
- */
-void mf_close(memfile_T *mfp, int del_file)
+/// Close a memory file and optionally delete the associated file.
+///
+/// @param del_file Whether to delete associated file.
+void mf_close(memfile_T *mfp, bool del_file)
{
bhdr_T *hp, *nextp;
- if (mfp == NULL) /* safety check */
+ if (mfp == NULL) { // safety check
return;
- if (mfp->mf_fd >= 0) {
- if (close(mfp->mf_fd) < 0)
+ }
+ if (mfp->mf_fd >= 0 && close(mfp->mf_fd) < 0) {
EMSG(_(e_swapclose));
}
if (del_file && mfp->mf_fname != NULL)
os_remove((char *)mfp->mf_fname);
- /* free entries in used list */
+ // free entries in used list
for (hp = mfp->mf_used_first; hp != NULL; hp = nextp) {
total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
nextp = hp->bh_next;
mf_free_bhdr(hp);
}
- while (mfp->mf_free_first != NULL) /* free entries in free list */
+ while (mfp->mf_free_first != NULL) // free entries in free list
free(mf_rem_free(mfp));
mf_hash_free(&mfp->mf_hash);
- mf_hash_free_all(&mfp->mf_trans); /* free hashtable and its items */
+ mf_hash_free_all(&mfp->mf_trans); // free hashtable and its items
free(mfp->mf_fname);
free(mfp->mf_ffname);
free(mfp);
}
-/*
- * Close the swap file for a memfile. Used when 'swapfile' is reset.
- */
-void
-mf_close_file (
- buf_T *buf,
- int getlines /* get all lines into memory? */
-)
+/// Close the swap file for a memfile. Used when 'swapfile' is reset.
+///
+/// @param getlines Whether to get all lines into memory.
+void mf_close_file(buf_T *buf, bool getlines)
{
- memfile_T *mfp;
- linenr_T lnum;
-
- mfp = buf->b_ml.ml_mfp;
- if (mfp == NULL || mfp->mf_fd < 0) /* nothing to close */
+ memfile_T *mfp = buf->b_ml.ml_mfp;
+ if (mfp == NULL || mfp->mf_fd < 0) // nothing to close
return;
if (getlines) {
- /* get all blocks in memory by accessing all lines (clumsy!) */
- mf_dont_release = TRUE;
- for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
- (void)ml_get_buf(buf, lnum, FALSE);
- mf_dont_release = FALSE;
- /* TODO: should check if all blocks are really in core */
+ // get all blocks in memory by accessing all lines (clumsy!)
+ mf_dont_release = true;
+ for (linenr_T lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
+ (void)ml_get_buf(buf, lnum, false);
+ mf_dont_release = false;
+ // TODO(elmart): should check if all blocks are really in core
}
- if (close(mfp->mf_fd) < 0) /* close the file */
+ if (close(mfp->mf_fd) < 0) // close the file
EMSG(_(e_swapclose));
mfp->mf_fd = -1;
if (mfp->mf_fname != NULL) {
- os_remove((char *)mfp->mf_fname); // delete the swap file
+ os_remove((char *)mfp->mf_fname); // delete the swap file
free(mfp->mf_fname);
free(mfp->mf_ffname);
mfp->mf_fname = NULL;
@@ -249,53 +250,45 @@ mf_close_file (
}
}
-/*
- * Set new size for a memfile. Used when block 0 of a swapfile has been read
- * and the size it indicates differs from what was guessed.
- */
+/// Set new size for a memfile. Used when block 0 of a swapfile has been read
+/// and the size it indicates differs from what was guessed.
void mf_new_page_size(memfile_T *mfp, unsigned new_size)
{
- /* Correct the memory used for block 0 to the new size, because it will be
- * freed with that size later on. */
- total_mem_used += new_size - mfp->mf_page_size;
+ // Correct the memory used for block 0 to the new size, because it will be
+ // freed with that size later on.
+ if (new_size >= mfp->mf_page_size) {
+ total_mem_used += new_size - mfp->mf_page_size;
+ } else {
+ total_mem_used -= mfp->mf_page_size - new_size;
+ }
mfp->mf_page_size = new_size;
}
-/*
- * get a new block
- *
- * negative: TRUE if negative block number desired (data block)
- */
-bhdr_T *mf_new(memfile_T *mfp, int negative, int page_count)
+/// Get a new block
+///
+/// @param negative Whether a negative block number is desired (data block).
+/// @param page_count Desired number of pages.
+bhdr_T *mf_new(memfile_T *mfp, bool negative, unsigned page_count)
{
- bhdr_T *hp; /* new bhdr_T */
- bhdr_T *freep; /* first block in free list */
- char_u *p;
-
- /*
- * If we reached the maximum size for the used memory blocks, release one
- * If a bhdr_T is returned, use it and adjust the page_count if necessary.
- */
- hp = mf_release(mfp, page_count);
-
- /*
- * Decide on the number to use:
- * If there is a free block, use its number.
- * Otherwise use mf_block_min for a negative number, mf_block_max for
- * a positive number.
- */
- freep = mfp->mf_free_first;
+ // If we reached the maximum size for the used memory blocks, release one.
+ // If a bhdr_T is returned, use it and adjust the page_count if necessary.
+ // If no bhdr_T is returned, a new one will be created.
+ bhdr_T *hp = mf_release(mfp, page_count); // the block to be returned
+
+ // Decide on the number to use:
+ // If there is a free block, use its number.
+ // Otherwise use mf_block_min for a negative number, mf_block_max for
+ // a positive number.
+ bhdr_T *freep = mfp->mf_free_first; // first free block
if (!negative && freep != NULL && freep->bh_page_count >= page_count) {
- /*
- * If the block in the free list has more pages, take only the number
- * of pages needed and allocate a new bhdr_T with data
- *
- * If the number of pages matches and mf_release() did not return a
- * bhdr_T, use the bhdr_T from the free list and allocate the data
- *
- * If the number of pages matches and mf_release() returned a bhdr_T,
- * just use the number and free the bhdr_T from the free list
- */
+ // If the block in the free list has more pages, take only the number
+ // of pages needed and allocate a new bhdr_T with data.
+ //
+ // If the number of pages matches and mf_release() did not return a
+ // bhdr_T, use the bhdr_T from the free list and allocate the data.
+ //
+ // If the number of pages matches and mf_release() returned a bhdr_T,
+ // just use the number and free the bhdr_T from the free list
if (freep->bh_page_count > page_count) {
if (hp == NULL) {
hp = mf_alloc_bhdr(mfp, page_count);
@@ -303,16 +296,16 @@ bhdr_T *mf_new(memfile_T *mfp, int negative, int page_count)
hp->bh_bnum = freep->bh_bnum;
freep->bh_bnum += page_count;
freep->bh_page_count -= page_count;
- } else if (hp == NULL) { /* need to allocate memory for this block */
- p = xmalloc(mfp->mf_page_size * page_count);
+ } else if (hp == NULL) { // need to allocate memory for this block
+ void *p = xmalloc(mfp->mf_page_size * page_count);
hp = mf_rem_free(mfp);
hp->bh_data = p;
- } else { /* use the number, remove entry from free list */
+ } else { // use the number, remove entry from free list
freep = mf_rem_free(mfp);
hp->bh_bnum = freep->bh_bnum;
free(freep);
}
- } else { /* get a new number */
+ } else { // get a new number
if (hp == NULL) {
hp = mf_alloc_bhdr(mfp, page_count);
}
@@ -324,49 +317,41 @@ bhdr_T *mf_new(memfile_T *mfp, int negative, int page_count)
mfp->mf_blocknr_max += page_count;
}
}
- hp->bh_flags = BH_LOCKED | BH_DIRTY; /* new block is always dirty */
- mfp->mf_dirty = TRUE;
+ hp->bh_flags = BH_LOCKED | BH_DIRTY; // new block is always dirty
+ mfp->mf_dirty = true;
hp->bh_page_count = page_count;
mf_ins_used(mfp, hp);
mf_ins_hash(mfp, hp);
- /*
- * Init the data to all zero, to avoid reading uninitialized data.
- * This also avoids that the passwd file ends up in the swap file!
- */
- (void)memset((char *)(hp->bh_data), 0,
- (size_t)mfp->mf_page_size * page_count);
+ // Init the data to all zero, to avoid reading uninitialized data.
+ // This also avoids that the passwd file ends up in the swap file!
+ (void)memset(hp->bh_data, 0, mfp->mf_page_size * page_count);
return hp;
}
-/*
- * Get existing block "nr" with "page_count" pages.
- *
- * Note: The caller should first check a negative nr with mf_trans_del()
- */
-bhdr_T *mf_get(memfile_T *mfp, blocknr_T nr, int page_count)
+// Get existing block "nr" with "page_count" pages.
+//
+// Caller should first check a negative nr with mf_trans_del().
+//
+// @return NULL if not found
+bhdr_T *mf_get(memfile_T *mfp, blocknr_T nr, unsigned page_count)
{
- bhdr_T *hp;
- /* doesn't exist */
+ // check block number exists
if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
return NULL;
- /*
- * see if it is in the cache
- */
- hp = mf_find_hash(mfp, nr);
- if (hp == NULL) { /* not in the hash list */
- if (nr < 0 || nr >= mfp->mf_infile_count) /* can't be in the file */
+ // see if it is in the cache
+ bhdr_T *hp = mf_find_hash(mfp, nr);
+ if (hp == NULL) { // not in the hash list
+ if (nr < 0 || nr >= mfp->mf_infile_count) // can't be in the file
return NULL;
- /* could check here if the block is in the free list */
+ // could check here if the block is in the free list
- /*
- * Check if we need to flush an existing block.
- * If so, use that block.
- * If not, allocate a new block.
- */
+ // Check if we need to flush an existing block.
+ // If so, use that block.
+ // If not, allocate a new block.
hp = mf_release(mfp, page_count);
if (hp == NULL) {
hp = mf_alloc_bhdr(mfp, page_count);
@@ -375,101 +360,88 @@ bhdr_T *mf_get(memfile_T *mfp, blocknr_T nr, int page_count)
hp->bh_bnum = nr;
hp->bh_flags = 0;
hp->bh_page_count = page_count;
- if (mf_read(mfp, hp) == FAIL) { /* cannot read the block! */
+ if (mf_read(mfp, hp) == FAIL) { // cannot read the block
mf_free_bhdr(hp);
return NULL;
}
} else {
- mf_rem_used(mfp, hp); /* remove from list, insert in front below */
+ mf_rem_used(mfp, hp); // remove from list, insert in front below
mf_rem_hash(mfp, hp);
}
hp->bh_flags |= BH_LOCKED;
- mf_ins_used(mfp, hp); /* put in front of used list */
- mf_ins_hash(mfp, hp); /* put in front of hash list */
+ mf_ins_used(mfp, hp); // put in front of used list
+ mf_ins_hash(mfp, hp); // put in front of hash list
return hp;
}
-/*
- * release the block *hp
- *
- * dirty: Block must be written to file later
- * infile: Block should be in file (needed for recovery)
- *
- * no return value, function cannot fail
- */
-void mf_put(memfile_T *mfp, bhdr_T *hp, int dirty, int infile)
+/// Release the block *hp.
+///
+/// @param dirty Whether block must be written to file later.
+/// @param infile Whether block should be in file (needed for recovery).
+void mf_put(memfile_T *mfp, bhdr_T *hp, bool dirty, bool infile)
{
- int flags;
-
- flags = hp->bh_flags;
+ unsigned flags = hp->bh_flags;
if ((flags & BH_LOCKED) == 0)
EMSG(_("E293: block was not locked"));
flags &= ~BH_LOCKED;
if (dirty) {
flags |= BH_DIRTY;
- mfp->mf_dirty = TRUE;
+ mfp->mf_dirty = true;
}
hp->bh_flags = flags;
if (infile)
- mf_trans_add(mfp, hp); /* may translate negative in positive nr */
+ mf_trans_add(mfp, hp); // may translate negative in positive nr
}
-/*
- * block *hp is no longer in used, may put it in the free list of memfile *mfp
- */
+/// Signal block as no longer used (may put it in the free list).
void mf_free(memfile_T *mfp, bhdr_T *hp)
{
- free(hp->bh_data); /* free the memory */
- mf_rem_hash(mfp, hp); /* get *hp out of the hash list */
- mf_rem_used(mfp, hp); /* get *hp out of the used list */
+ free(hp->bh_data); // free data
+ mf_rem_hash(mfp, hp); // get *hp out of the hash list
+ mf_rem_used(mfp, hp); // get *hp out of the used list
if (hp->bh_bnum < 0) {
- free(hp); /* don't want negative numbers in free list */
+ free(hp); // don't want negative numbers in free list
mfp->mf_neg_count--;
- } else
- mf_ins_free(mfp, hp); /* put *hp in the free list */
+ } else {
+ mf_ins_free(mfp, hp); // put *hp in the free list
+ }
}
-/*
- * Sync the memory file *mfp to disk.
- * Flags:
- * MFS_ALL If not given, blocks with negative numbers are not synced,
- * even when they are dirty!
- * MFS_STOP Stop syncing when a character becomes available, but sync at
- * least one block.
- * MFS_FLUSH Make sure buffers are flushed to disk, so they will survive a
- * system crash.
- * MFS_ZERO Only write block 0.
- *
- * Return FAIL for failure, OK otherwise
- */
+/// Sync memory file to disk.
+///
+/// @param flags MFS_ALL If not given, blocks with negative numbers are not
+/// synced, even when they are dirty.
+/// MFS_STOP Stop syncing when a character becomes available,
+/// but sync at least one block.
+/// MFS_FLUSH Make sure buffers are flushed to disk, so they will
+/// survive a system crash.
+/// MFS_ZERO Only write block 0.
+///
+/// @return FAIL If failure. Possible causes:
+/// - No file (nothing to do).
+/// - Write error (probably full disk).
+/// OK Otherwise.
int mf_sync(memfile_T *mfp, int flags)
{
- int status;
- bhdr_T *hp;
-#if defined(SYNC_DUP_CLOSE)
- int fd;
-#endif
int got_int_save = got_int;
- if (mfp->mf_fd < 0) { /* there is no file, nothing to do */
- mfp->mf_dirty = FALSE;
+ if (mfp->mf_fd < 0) { // there is no file, nothing to do
+ mfp->mf_dirty = false;
return FAIL;
}
- /* Only a CTRL-C while writing will break us here, not one typed
- * previously. */
- got_int = FALSE;
-
- /*
- * sync from last to first (may reduce the probability of an inconsistent
- * file) If a write fails, it is very likely caused by a full filesystem.
- * Then we only try to write blocks within the existing file. If that also
- * fails then we give up.
- */
- status = OK;
+ // Only a CTRL-C while writing will break us here, not one typed previously.
+ got_int = false;
+
+ // Sync from last to first (may reduce the probability of an inconsistent
+ // file). If a write fails, it is very likely caused by a full filesystem.
+ // Then we only try to write blocks within the existing file. If that also
+ // fails then we give up.
+ int status = OK;
+ bhdr_T *hp;
for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
if (((flags & MFS_ALL) || hp->bh_bnum >= 0)
&& (hp->bh_flags & BH_DIRTY)
@@ -478,26 +450,24 @@ int mf_sync(memfile_T *mfp, int flags)
if ((flags & MFS_ZERO) && hp->bh_bnum != 0)
continue;
if (mf_write(mfp, hp) == FAIL) {
- if (status == FAIL) /* double error: quit syncing */
+ if (status == FAIL) // double error: quit syncing
break;
status = FAIL;
}
- if (flags & MFS_STOP) {
- /* Stop when char available now. */
+ if (flags & MFS_STOP) { // Stop when char available now.
if (ui_char_avail())
break;
- } else
+ } else {
ui_breakcheck();
+ }
if (got_int)
break;
}
- /*
- * If the whole list is flushed, the memfile is not dirty anymore.
- * In case of an error this flag is also set, to avoid trying all the time.
- */
+ // If the whole list is flushed, the memfile is not dirty anymore.
+ // In case of an error, dirty flag is also set, to avoid trying all the time.
if (hp == NULL || status == FAIL)
- mfp->mf_dirty = FALSE;
+ mfp->mf_dirty = false;
if ((flags & MFS_FLUSH) && *p_sws != NUL) {
#if defined(UNIX)
@@ -505,21 +475,23 @@ int mf_sync(memfile_T *mfp, int flags)
if (STRCMP(p_sws, "fsync") == 0) {
if (fsync(mfp->mf_fd))
status = FAIL;
- } else
+ } else {
# endif
- /* OpenNT is strictly POSIX (Benzinger) */
- /* Tandem/Himalaya NSK-OSS doesn't have sync() */
+ // OpenNT is strictly POSIX (Benzinger).
+ // Tandem/Himalaya NSK-OSS doesn't have sync()
# if defined(__OPENNT) || defined(__TANDEM)
fflush(NULL);
# else
sync();
# endif
+# ifdef HAVE_FSYNC
+ }
+# endif
#endif
# ifdef SYNC_DUP_CLOSE
- /*
- * Win32 is a bit more work: Duplicate the file handle and close it.
- * This should flush the file to disk.
- */
+ // Win32 is a bit more work: Duplicate the file handle and close it.
+ // This should flush the file to disk.
+ int fd;
if ((fd = dup(mfp->mf_fd)) >= 0)
close(fd);
# endif
@@ -530,54 +502,42 @@ int mf_sync(memfile_T *mfp, int flags)
return status;
}
-/*
- * For all blocks in memory file *mfp that have a positive block number set
- * the dirty flag. These are blocks that need to be written to a newly
- * created swapfile.
- */
+/// Set dirty flag for all blocks in memory file with a positive block number.
+/// These are blocks that need to be written to a newly created swapfile.
void mf_set_dirty(memfile_T *mfp)
{
- bhdr_T *hp;
-
+ bhdr_T *hp;
for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
if (hp->bh_bnum > 0)
hp->bh_flags |= BH_DIRTY;
- mfp->mf_dirty = TRUE;
+ mfp->mf_dirty = true;
}
-/*
- * insert block *hp in front of hashlist of memfile *mfp
- */
+/// Insert block in front of memfile's hash list.
static void mf_ins_hash(memfile_T *mfp, bhdr_T *hp)
{
mf_hash_add_item(&mfp->mf_hash, (mf_hashitem_T *)hp);
}
-/*
- * remove block *hp from hashlist of memfile list *mfp
- */
+/// Remove block from memfile's hash list.
static void mf_rem_hash(memfile_T *mfp, bhdr_T *hp)
{
mf_hash_rem_item(&mfp->mf_hash, (mf_hashitem_T *)hp);
}
-/*
- * look in hash lists of memfile *mfp for block header with number 'nr'
- */
+/// Lookup block with number "nr" in memfile's hash list.
static bhdr_T *mf_find_hash(memfile_T *mfp, blocknr_T nr)
{
return (bhdr_T *)mf_hash_find(&mfp->mf_hash, nr);
}
-/*
- * insert block *hp in front of used list of memfile *mfp
- */
+/// Insert block at the front of memfile's used list.
static void mf_ins_used(memfile_T *mfp, bhdr_T *hp)
{
hp->bh_next = mfp->mf_used_first;
mfp->mf_used_first = hp;
hp->bh_prev = NULL;
- if (hp->bh_next == NULL) /* list was empty, adjust last pointer */
+ if (hp->bh_next == NULL) // list was empty, adjust last pointer
mfp->mf_used_last = hp;
else
hp->bh_next->bh_prev = hp;
@@ -585,52 +545,51 @@ static void mf_ins_used(memfile_T *mfp, bhdr_T *hp)
total_mem_used += hp->bh_page_count * mfp->mf_page_size;
}
-/*
- * remove block *hp from used list of memfile *mfp
- */
+/// Remove block from memfile's used list.
static void mf_rem_used(memfile_T *mfp, bhdr_T *hp)
{
- if (hp->bh_next == NULL) /* last block in used list */
+ if (hp->bh_next == NULL) // last block in used list
mfp->mf_used_last = hp->bh_prev;
else
hp->bh_next->bh_prev = hp->bh_prev;
- if (hp->bh_prev == NULL) /* first block in used list */
+
+ if (hp->bh_prev == NULL) // first block in used list
mfp->mf_used_first = hp->bh_next;
else
hp->bh_prev->bh_next = hp->bh_next;
+
mfp->mf_used_count -= hp->bh_page_count;
total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
}
-/*
- * Release the least recently used block from the used list if the number
- * of used memory blocks gets to big.
- *
- * Return the block header to the caller, including the memory block, so
- * it can be re-used. Make sure the page_count is right.
- */
-static bhdr_T *mf_release(memfile_T *mfp, int page_count)
+/// Try to release the least recently used block from the used list if the
+/// number of used memory blocks gets too big.
+///
+/// @return The block header, when release needed and possible.
+/// Resulting block header includes memory block, so it can be
+/// reused. Page count is checked to be right.
+/// NULL, when release not needed, or not possible.
+/// Not needed when number of blocks less than allowed maximum and
+/// total memory used below 'maxmemtot'.
+/// Not possible when:
+/// - Called while closing file.
+/// - Tried to create swap file but couldn't.
+/// - All blocks are locked.
+/// - Unlocked dirty block found, but flush failed.
+static bhdr_T *mf_release(memfile_T *mfp, unsigned page_count)
{
- bhdr_T *hp;
- int need_release;
-
- /* don't release while in mf_close_file() */
+ // don't release while in mf_close_file()
if (mf_dont_release)
return NULL;
- /*
- * Need to release a block if the number of blocks for this memfile is
- * higher than the maximum or total memory used is over 'maxmemtot'
- */
- need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max)
- || (total_mem_used >> 10) >= (long_u)p_mmt);
-
- /*
- * Try to create a swap file if the amount of memory used is getting too
- * high.
- */
+ /// Need to release a block if the number of blocks for this memfile is
+ /// higher than the maximum one or total memory used is over 'maxmemtot'.
+ bool need_release = (mfp->mf_used_count >= mfp->mf_used_count_max
+ || (total_mem_used >> 10) >= (size_t)p_mmt);
+
+ /// Try to create swap file if the amount of memory used is getting too high.
if (mfp->mf_fd < 0 && need_release && p_uc) {
- /* find for which buffer this memfile is */
+ // find for which buffer this memfile is
buf_T *buf = NULL;
FOR_ALL_BUFFERS(bp) {
if (bp->b_ml.ml_mfp == mfp) {
@@ -643,37 +602,31 @@ static bhdr_T *mf_release(memfile_T *mfp, int page_count)
}
}
- /*
- * don't release a block if
- * there is no file for this memfile
- * or
- * the number of blocks for this memfile is lower than the maximum
- * and
- * total memory used is not up to 'maxmemtot'
- */
+ /// Don't release a block if:
+ /// there is no file for this memfile
+ /// or
+ /// the number of blocks for this memfile is lower than the maximum
+ /// and
+ /// total memory used is not up to 'maxmemtot'
if (mfp->mf_fd < 0 || !need_release)
return NULL;
+ bhdr_T *hp;
for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
if (!(hp->bh_flags & BH_LOCKED))
break;
- if (hp == NULL) /* not a single one that can be released */
+ if (hp == NULL) // not a single one that can be released
return NULL;
- /*
- * If the block is dirty, write it.
- * If the write fails we don't free it.
- */
+ // If the block is dirty, write it.
+ // If the write fails we don't free it.
if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL)
return NULL;
mf_rem_used(mfp, hp);
mf_rem_hash(mfp, hp);
- /*
- * If a bhdr_T is returned, make sure that the page_count of bh_data is
- * right
- */
+ /// Make sure page_count of bh_data is right.
if (hp->bh_page_count != page_count) {
free(hp->bh_data);
hp->bh_data = xmalloc(mfp->mf_page_size * page_count);
@@ -682,38 +635,35 @@ static bhdr_T *mf_release(memfile_T *mfp, int page_count)
return hp;
}
-/*
- * release as many blocks as possible
- * Used in case of out of memory
- *
- * return TRUE if any memory was released
- */
-int mf_release_all(void)
+/// Release as many blocks as possible.
+///
+/// Used in case of out of memory
+///
+/// @return Whether any memory was released.
+bool mf_release_all(void)
{
- memfile_T *mfp;
- bhdr_T *hp;
- int retval = FALSE;
-
+ bool retval = false;
FOR_ALL_BUFFERS(buf) {
- mfp = buf->b_ml.ml_mfp;
+ memfile_T *mfp = buf->b_ml.ml_mfp;
if (mfp != NULL) {
- /* If no swap file yet, may open one */
+ // If no swap file yet, may open one.
if (mfp->mf_fd < 0 && buf->b_may_swap)
ml_open_file(buf);
- /* only if there is a swapfile */
+ // Flush as many blocks as possible, only if there is a swapfile.
if (mfp->mf_fd >= 0) {
- for (hp = mfp->mf_used_last; hp != NULL; ) {
+ for (bhdr_T *hp = mfp->mf_used_last; hp != NULL; ) {
if (!(hp->bh_flags & BH_LOCKED)
&& (!(hp->bh_flags & BH_DIRTY)
|| mf_write(mfp, hp) != FAIL)) {
mf_rem_used(mfp, hp);
mf_rem_hash(mfp, hp);
mf_free_bhdr(hp);
- hp = mfp->mf_used_last; /* re-start, list was changed */
- retval = TRUE;
- } else
+ hp = mfp->mf_used_last; // restart, list was changed
+ retval = true;
+ } else {
hp = hp->bh_prev;
+ }
}
}
}
@@ -721,70 +671,60 @@ int mf_release_all(void)
return retval;
}
-/*
- * Allocate a block header and a block of memory for it
- */
-static bhdr_T *mf_alloc_bhdr(memfile_T *mfp, int page_count)
+/// Allocate a block header and a block of memory for it.
+static bhdr_T *mf_alloc_bhdr(memfile_T *mfp, unsigned page_count)
{
bhdr_T *hp = xmalloc(sizeof(bhdr_T));
hp->bh_data = xmalloc(mfp->mf_page_size * page_count);
hp->bh_page_count = page_count;
-
return hp;
}
-/*
- * Free a block header and the block of memory for it
- */
+/// Free a block header and its block memory.
static void mf_free_bhdr(bhdr_T *hp)
{
free(hp->bh_data);
free(hp);
}
-/*
- * insert entry *hp in the free list
- */
+/// Insert a block in the free list.
static void mf_ins_free(memfile_T *mfp, bhdr_T *hp)
{
hp->bh_next = mfp->mf_free_first;
mfp->mf_free_first = hp;
}
-/*
- * remove the first entry from the free list and return a pointer to it
- * Note: caller must check that mfp->mf_free_first is not NULL!
- */
+/// Remove the first block in the free list and return it.
+///
+/// Caller must check that mfp->mf_free_first is not NULL.
static bhdr_T *mf_rem_free(memfile_T *mfp)
{
- bhdr_T *hp;
-
- hp = mfp->mf_free_first;
+ bhdr_T *hp = mfp->mf_free_first;
mfp->mf_free_first = hp->bh_next;
return hp;
}
-/*
- * read a block from disk
- *
- * Return FAIL for failure, OK otherwise
- */
+/// Read a block from disk.
+///
+/// @return OK On success.
+/// FAIL On failure. Could be:
+/// - No file.
+/// - Error reading file.
static int mf_read(memfile_T *mfp, bhdr_T *hp)
{
- off_t offset;
- unsigned page_size;
- unsigned size;
-
- if (mfp->mf_fd < 0) /* there is no file, can't read */
+ if (mfp->mf_fd < 0) // there is no file, can't read
return FAIL;
- page_size = mfp->mf_page_size;
- offset = (off_t)page_size * hp->bh_bnum;
- size = page_size * hp->bh_page_count;
+ unsigned page_size = mfp->mf_page_size;
+ // TODO(elmart): Check (page_size * hp->bh_bnum) within off_t bounds.
+ off_t offset = (off_t)(page_size * hp->bh_bnum);
if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset) {
PERROR(_("E294: Seek error in swap file read"));
return FAIL;
}
+ // check for overflow; we know that page_size must be > 0
+ assert(hp->bh_page_count <= UINT_MAX / page_size);
+ unsigned size = page_size * hp->bh_page_count;
if ((unsigned)read_eintr(mfp->mf_fd, hp->bh_data, size) != size) {
PERROR(_("E295: Read error in swap file"));
return FAIL;
@@ -793,121 +733,112 @@ static int mf_read(memfile_T *mfp, bhdr_T *hp)
return OK;
}
-/*
- * write a block to disk
- *
- * Return FAIL for failure, OK otherwise
- */
+/// Write a block to disk.
+///
+/// @return OK On success.
+/// FAIL On failure. Could be:
+/// - No file.
+/// - Could not translate negative block number to positive.
+/// - Seek error in swap file.
+/// - Write error in swap file.
static int mf_write(memfile_T *mfp, bhdr_T *hp)
{
- off_t offset; /* offset in the file */
- blocknr_T nr; /* block nr which is being written */
- bhdr_T *hp2;
- unsigned page_size; /* number of bytes in a page */
- unsigned page_count; /* number of pages written */
- unsigned size; /* number of bytes written */
-
- if (mfp->mf_fd < 0) /* there is no file, can't write */
+ off_t offset; // offset in the file
+ blocknr_T nr; // block nr which is being written
+ bhdr_T *hp2;
+ unsigned page_size; // number of bytes in a page
+ unsigned page_count; // number of pages written
+ unsigned size; // number of bytes written
+
+ if (mfp->mf_fd < 0) // there is no file, can't write
return FAIL;
- if (hp->bh_bnum < 0) /* must assign file block number */
+ if (hp->bh_bnum < 0) // must assign file block number
if (mf_trans_add(mfp, hp) == FAIL)
return FAIL;
page_size = mfp->mf_page_size;
- /*
- * We don't want gaps in the file. Write the blocks in front of *hp
- * to extend the file.
- * If block 'mf_infile_count' is not in the hash list, it has been
- * freed. Fill the space in the file with data from the current block.
- */
- for (;; ) {
+ /// We don't want gaps in the file. Write the blocks in front of *hp
+ /// to extend the file.
+ /// If block 'mf_infile_count' is not in the hash list, it has been
+ /// freed. Fill the space in the file with data from the current block.
+ for (;;) {
nr = hp->bh_bnum;
- if (nr > mfp->mf_infile_count) { /* beyond end of file */
+ if (nr > mfp->mf_infile_count) { // beyond end of file
nr = mfp->mf_infile_count;
- hp2 = mf_find_hash(mfp, nr); /* NULL caught below */
- } else
+ hp2 = mf_find_hash(mfp, nr); // NULL caught below
+ } else {
hp2 = hp;
+ }
- offset = (off_t)page_size * nr;
+ // TODO(elmart): Check (page_size * nr) within off_t bounds.
+ offset = (off_t)(page_size * nr);
if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset) {
PERROR(_("E296: Seek error in swap file write"));
return FAIL;
}
- if (hp2 == NULL) /* freed block, fill with dummy data */
+ if (hp2 == NULL) // freed block, fill with dummy data
page_count = 1;
else
page_count = hp2->bh_page_count;
size = page_size * page_count;
if (mf_write_block(mfp, hp2 == NULL ? hp : hp2, offset, size) == FAIL) {
- /*
- * Avoid repeating the error message, this mostly happens when the
- * disk is full. We give the message again only after a successful
- * write or when hitting a key. We keep on trying, in case some
- * space becomes available.
- */
+ /// Avoid repeating the error message, this mostly happens when the
+ /// disk is full. We give the message again only after a successful
+ /// write or when hitting a key. We keep on trying, in case some
+ /// space becomes available.
if (!did_swapwrite_msg)
EMSG(_("E297: Write error in swap file"));
- did_swapwrite_msg = TRUE;
+ did_swapwrite_msg = true;
return FAIL;
}
- did_swapwrite_msg = FALSE;
- if (hp2 != NULL) /* written a non-dummy block */
+ did_swapwrite_msg = false;
+ if (hp2 != NULL) // written a non-dummy block
hp2->bh_flags &= ~BH_DIRTY;
- /* appended to the file */
- if (nr + (blocknr_T)page_count > mfp->mf_infile_count)
+ if (nr + (blocknr_T)page_count > mfp->mf_infile_count) // appended to file
mfp->mf_infile_count = nr + page_count;
- if (nr == hp->bh_bnum) /* written the desired block */
+ if (nr == hp->bh_bnum) // written the desired block
break;
}
return OK;
}
-/*
- * Write block "hp" with data size "size" to file "mfp->mf_fd".
- * Return FAIL or OK.
- */
-static int mf_write_block(memfile_T *mfp, bhdr_T *hp, off_t offset, unsigned size)
+/// Write block to memfile's file.
+///
+/// @return OK On success.
+/// FAIL On failure.
+static int mf_write_block(memfile_T *mfp, bhdr_T *hp,
+ off_t offset, unsigned size)
{
- char_u *data = hp->bh_data;
+ void *data = hp->bh_data;
int result = OK;
-
if ((unsigned)write_eintr(mfp->mf_fd, data, size) != size)
result = FAIL;
-
return result;
}
-/*
- * Make block number for *hp positive and add it to the translation list
- *
- * Return FAIL for failure, OK otherwise
- */
+/// Make block number positive and add it to the translation list.
+///
+/// @return OK On success.
+/// FAIL On failure.
static int mf_trans_add(memfile_T *mfp, bhdr_T *hp)
{
- bhdr_T *freep;
- blocknr_T new_bnum;
- int page_count;
-
- if (hp->bh_bnum >= 0) /* it's already positive */
+ if (hp->bh_bnum >= 0) // it's already positive
return OK;
- NR_TRANS *np = xmalloc(sizeof(NR_TRANS));
+ mf_blocknr_trans_item_T *np = xmalloc(sizeof(mf_blocknr_trans_item_T));
- /*
- * Get a new number for the block.
- * If the first item in the free list has sufficient pages, use its number
- * Otherwise use mf_blocknr_max.
- */
- freep = mfp->mf_free_first;
- page_count = hp->bh_page_count;
+ // Get a new number for the block.
+ // If the first item in the free list has sufficient pages, use its number.
+ // Otherwise use mf_blocknr_max.
+ blocknr_T new_bnum;
+ bhdr_T *freep = mfp->mf_free_first;
+ unsigned page_count = hp->bh_page_count;
if (freep != NULL && freep->bh_page_count >= page_count) {
new_bnum = freep->bh_bnum;
- /*
- * If the page count of the free block was larger, reduce it.
- * If the page count matches, remove the block from the free list
- */
+ // If the page count of the free block was larger, reduce it.
+ // If the page count matches, remove the block from the free list.
if (freep->bh_page_count > page_count) {
freep->bh_bnum += page_count;
freep->bh_page_count -= page_count;
@@ -920,38 +851,35 @@ static int mf_trans_add(memfile_T *mfp, bhdr_T *hp)
mfp->mf_blocknr_max += page_count;
}
- np->nt_old_bnum = hp->bh_bnum; /* adjust number */
+ np->nt_old_bnum = hp->bh_bnum; // adjust number
np->nt_new_bnum = new_bnum;
- mf_rem_hash(mfp, hp); /* remove from old hash list */
+ mf_rem_hash(mfp, hp); // remove from old hash list
hp->bh_bnum = new_bnum;
- mf_ins_hash(mfp, hp); /* insert in new hash list */
+ mf_ins_hash(mfp, hp); // insert in new hash list
- /* Insert "np" into "mf_trans" hashtable with key "np->nt_old_bnum" */
+ // Insert "np" into "mf_trans" hashtable with key "np->nt_old_bnum".
mf_hash_add_item(&mfp->mf_trans, (mf_hashitem_T *)np);
return OK;
}
-/*
- * Lookup a translation from the trans lists and delete the entry
- *
- * Return the positive new number when found, the old number when not found
- */
+/// Lookup translation from trans list and delete the entry.
+///
+/// @return The positive new number When found.
+/// The old number When not found.
blocknr_T mf_trans_del(memfile_T *mfp, blocknr_T old_nr)
{
- NR_TRANS *np;
- blocknr_T new_bnum;
-
- np = (NR_TRANS *)mf_hash_find(&mfp->mf_trans, old_nr);
+ mf_blocknr_trans_item_T *np =
+ (mf_blocknr_trans_item_T *)mf_hash_find(&mfp->mf_trans, old_nr);
- if (np == NULL) /* not found */
+ if (np == NULL) // not found
return old_nr;
mfp->mf_neg_count--;
- new_bnum = np->nt_new_bnum;
+ blocknr_T new_bnum = np->nt_new_bnum;
- /* remove entry from the trans list */
+ // remove entry from the trans list
mf_hash_rem_item(&mfp->mf_trans, (mf_hashitem_T *)np);
free(np);
@@ -959,20 +887,19 @@ blocknr_T mf_trans_del(memfile_T *mfp, blocknr_T old_nr)
return new_bnum;
}
-/*
- * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
- * Only called when creating or renaming the swapfile. Either way it's a new
- * name so we must work out the full path name.
- */
+/// Set full file name of memfile's swapfile, out of simple file name and some
+/// other considerations.
+///
+/// Only called when creating or renaming the swapfile. Either way it's a new
+/// name so we must work out the full path name.
void mf_set_ffname(memfile_T *mfp)
{
- mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE);
+ mfp->mf_ffname = FullName_save(mfp->mf_fname, false);
}
-/*
- * Make the name of the file used for the memfile a full path.
- * Used before doing a :cd
- */
+/// Make name of memfile's swapfile a full path.
+///
+/// Used before doing a :cd
void mf_fullname(memfile_T *mfp)
{
if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL) {
@@ -982,55 +909,37 @@ void mf_fullname(memfile_T *mfp)
}
}
-/*
- * return TRUE if there are any translations pending for 'mfp'
- */
-int mf_need_trans(memfile_T *mfp)
+/// Return TRUE if there are any translations pending for memfile.
+bool mf_need_trans(memfile_T *mfp)
{
return mfp->mf_fname != NULL && mfp->mf_neg_count > 0;
}
-/*
- * Open a swap file for a memfile.
- * The "fname" must be in allocated memory, and is consumed (also when an
- * error occurs).
- */
-static void
-mf_do_open (
- memfile_T *mfp,
- char_u *fname,
- int flags /* flags for open() */
-)
+/// Open memfile's swapfile.
+///
+/// "fname" must be in allocated memory, and is consumed (also when error).
+///
+/// @param flags Flags for open().
+static void mf_do_open(memfile_T *mfp, char_u *fname, int flags)
{
+ // fname cannot be NameBuff, because it must have been allocated.
mfp->mf_fname = fname;
-
- /*
- * Get the full path name before the open, because this is
- * not possible after the open on the Amiga.
- * fname cannot be NameBuff, because it must have been allocated.
- */
mf_set_ffname(mfp);
- /*
- * Extra security check: When creating a swap file it really shouldn't
- * exist yet. If there is a symbolic link, this is most likely an attack.
- */
+ /// Extra security check: When creating a swap file it really shouldn't
+ /// exist yet. If there is a symbolic link, this is most likely an attack.
FileInfo file_info;
if ((flags & O_CREAT)
&& os_fileinfo_link((char *)mfp->mf_fname, &file_info)) {
mfp->mf_fd = -1;
EMSG(_("E300: Swap file already exists (symlink attack?)"));
} else {
- /*
- * try to open the file
- */
+ // try to open the file
flags |= O_NOFOLLOW;
mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags);
}
- /*
- * If the file cannot be opened, use memory only
- */
+ // If the file cannot be opened, use memory only
if (mfp->mf_fd < 0) {
free(mfp->mf_fname);
free(mfp->mf_ffname);
@@ -1045,25 +954,21 @@ mf_do_open (
#ifdef HAVE_SELINUX
mch_copy_sec(fname, mfp->mf_fname);
#endif
- mch_hide(mfp->mf_fname); /* try setting the 'hidden' flag */
+ mch_hide(mfp->mf_fname); // try setting the 'hidden' flag
}
}
-/*
- * Implementation of mf_hashtab_T follows.
- */
+//
+// Implementation of mf_hashtab_T.
+//
-/*
- * The number of buckets in the hashtable is increased by a factor of
- * MHT_GROWTH_FACTOR when the average number of items per bucket
- * exceeds 2 ^ MHT_LOG_LOAD_FACTOR.
- */
+/// The number of buckets in the hashtable is increased by a factor of
+/// MHT_GROWTH_FACTOR when the average number of items per bucket
+/// exceeds 2 ^ MHT_LOG_LOAD_FACTOR.
#define MHT_LOG_LOAD_FACTOR 6
-#define MHT_GROWTH_FACTOR 2 /* must be a power of two */
+#define MHT_GROWTH_FACTOR 2 // must be a power of two
-/*
- * Initialize an empty hash table.
- */
+/// Initialize an empty hash table.
static void mf_hash_init(mf_hashtab_T *mht)
{
memset(mht, 0, sizeof(mf_hashtab_T));
@@ -1071,27 +976,21 @@ static void mf_hash_init(mf_hashtab_T *mht)
mht->mht_mask = MHT_INIT_SIZE - 1;
}
-/*
- * Free the array of a hash table. Does not free the items it contains!
- * The hash table must not be used again without another mf_hash_init() call.
- */
+/// Free the array of a hash table. Does not free the items it contains!
+/// The hash table must not be used again without another mf_hash_init() call.
static void mf_hash_free(mf_hashtab_T *mht)
{
if (mht->mht_buckets != mht->mht_small_buckets)
free(mht->mht_buckets);
}
-/*
- * Free the array of a hash table and all the items it contains.
- */
+/// Free the array of a hash table and all the items it contains.
static void mf_hash_free_all(mf_hashtab_T *mht)
{
- long_u idx;
- mf_hashitem_T *mhi;
- mf_hashitem_T *next;
+ mf_hashitem_T *next;
- for (idx = 0; idx <= mht->mht_mask; idx++)
- for (mhi = mht->mht_buckets[idx]; mhi != NULL; mhi = next) {
+ for (size_t idx = 0; idx <= mht->mht_mask; idx++)
+ for (mf_hashitem_T *mhi = mht->mht_buckets[idx]; mhi != NULL; mhi = next) {
next = mhi->mhi_next;
free(mhi);
}
@@ -1099,30 +998,21 @@ static void mf_hash_free_all(mf_hashtab_T *mht)
mf_hash_free(mht);
}
-/*
- * Find "key" in hashtable "mht".
- * Returns a pointer to a mf_hashitem_T or NULL if the item was not found.
- */
+/// Find by key.
+///
+/// @return A pointer to a mf_hashitem_T or NULL if the item was not found.
static mf_hashitem_T *mf_hash_find(mf_hashtab_T *mht, blocknr_T key)
{
- mf_hashitem_T *mhi;
-
- mhi = mht->mht_buckets[key & mht->mht_mask];
+ mf_hashitem_T *mhi = mht->mht_buckets[(size_t)key & mht->mht_mask];
while (mhi != NULL && mhi->mhi_key != key)
mhi = mhi->mhi_next;
-
return mhi;
}
-/*
- * Add item "mhi" to hashtable "mht".
- * "mhi" must not be NULL.
- */
+/// Add item to hashtable. Item must not be NULL.
static void mf_hash_add_item(mf_hashtab_T *mht, mf_hashitem_T *mhi)
{
- long_u idx;
-
- idx = mhi->mhi_key & mht->mht_mask;
+ size_t idx = (size_t)mhi->mhi_key & mht->mht_mask;
mhi->mhi_next = mht->mht_buckets[idx];
mhi->mhi_prev = NULL;
if (mhi->mhi_next != NULL)
@@ -1131,24 +1021,19 @@ static void mf_hash_add_item(mf_hashtab_T *mht, mf_hashitem_T *mhi)
mht->mht_count++;
- /*
- * Grow hashtable when we have more thank 2^MHT_LOG_LOAD_FACTOR
- * items per bucket on average
- */
- if (mht->mht_fixed == 0
- && (mht->mht_count >> MHT_LOG_LOAD_FACTOR) > mht->mht_mask) {
+ /// Grow hashtable when we have more thank 2^MHT_LOG_LOAD_FACTOR
+ /// items per bucket on average.
+ if ((mht->mht_count >> MHT_LOG_LOAD_FACTOR) > mht->mht_mask) {
mf_hash_grow(mht);
}
}
-/*
- * Remove item "mhi" from hashtable "mht".
- * "mhi" must not be NULL and must have been inserted into "mht".
- */
+/// Remove item from hashtable. Item must be non NULL and within hashtable.
static void mf_hash_rem_item(mf_hashtab_T *mht, mf_hashitem_T *mhi)
{
if (mhi->mhi_prev == NULL)
- mht->mht_buckets[mhi->mhi_key & mht->mht_mask] = mhi->mhi_next;
+ mht->mht_buckets[(size_t)mhi->mhi_key & mht->mht_mask] =
+ mhi->mhi_next;
else
mhi->mhi_prev->mhi_next = mhi->mhi_next;
@@ -1157,46 +1042,37 @@ static void mf_hash_rem_item(mf_hashtab_T *mht, mf_hashitem_T *mhi)
mht->mht_count--;
- /* We could shrink the table here, but it typically takes little memory,
- * so why bother? */
+ // We could shrink the table here, but it typically takes little memory,
+ // so why bother?
}
-/*
- * Increase number of buckets in the hashtable by MHT_GROWTH_FACTOR and
- * rehash items.
- */
+/// Increase number of buckets in the hashtable by MHT_GROWTH_FACTOR and
+/// rehash items.
static void mf_hash_grow(mf_hashtab_T *mht)
{
- long_u i, j;
- int shift;
- mf_hashitem_T *mhi;
- mf_hashitem_T *tails[MHT_GROWTH_FACTOR];
- mf_hashitem_T **buckets;
- size_t size;
+ size_t size = (mht->mht_mask + 1) * MHT_GROWTH_FACTOR * sizeof(void *);
+ mf_hashitem_T **buckets = xcalloc(1, size);
- size = (mht->mht_mask + 1) * MHT_GROWTH_FACTOR * sizeof(void *);
- buckets = xcalloc(1, size);
-
- shift = 0;
+ int shift = 0;
while ((mht->mht_mask >> shift) != 0)
shift++;
- for (i = 0; i <= mht->mht_mask; i++) {
- /*
- * Traverse the items in the i-th original bucket and move them into
- * MHT_GROWTH_FACTOR new buckets, preserving their relative order
- * within each new bucket. Preserving the order is important because
- * mf_get() tries to keep most recently used items at the front of
- * each bucket.
- *
- * Here we strongly rely on the fact the hashes are computed modulo
- * a power of two.
- */
-
+ for (size_t i = 0; i <= mht->mht_mask; i++) {
+ /// Traverse the items in the i-th original bucket and move them into
+ /// MHT_GROWTH_FACTOR new buckets, preserving their relative order
+ /// within each new bucket. Preserving the order is important because
+ /// mf_get() tries to keep most recently used items at the front of
+ /// each bucket.
+ ///
+ /// Here we strongly rely on the fact that hashes are computed modulo
+ /// a power of two.
+
+ mf_hashitem_T *tails[MHT_GROWTH_FACTOR];
memset(tails, 0, sizeof(tails));
- for (mhi = mht->mht_buckets[i]; mhi != NULL; mhi = mhi->mhi_next) {
- j = (mhi->mhi_key >> shift) & (MHT_GROWTH_FACTOR - 1);
+ for (mf_hashitem_T *mhi = mht->mht_buckets[i];
+ mhi != NULL; mhi = mhi->mhi_next) {
+ size_t j = (mhi->mhi_key >> shift) & (MHT_GROWTH_FACTOR - 1);
if (tails[j] == NULL) {
buckets[i + (j << shift)] = mhi;
tails[j] = mhi;
@@ -1208,7 +1084,7 @@ static void mf_hash_grow(mf_hashtab_T *mht)
}
}
- for (j = 0; j < MHT_GROWTH_FACTOR; j++)
+ for (size_t j = 0; j < MHT_GROWTH_FACTOR; j++)
if (tails[j] != NULL)
tails[j]->mhi_next = NULL;
}
diff --git a/src/nvim/memfile.h b/src/nvim/memfile.h
index 3e71853326..085fa22f12 100644
--- a/src/nvim/memfile.h
+++ b/src/nvim/memfile.h
@@ -4,11 +4,11 @@
#include "nvim/buffer_defs.h"
#include "nvim/memfile_defs.h"
-/* flags for mf_sync() */
-#define MFS_ALL 1 /* also sync blocks with negative numbers */
-#define MFS_STOP 2 /* stop syncing when a character is available */
-#define MFS_FLUSH 4 /* flushed file to disk */
-#define MFS_ZERO 8 /* only write block 0 */
+/// flags for mf_sync()
+#define MFS_ALL 1 /// also sync blocks with negative numbers
+#define MFS_STOP 2 /// stop syncing when a character is available
+#define MFS_FLUSH 4 /// flushed file to disk
+#define MFS_ZERO 8 /// only write block 0
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "memfile.h.generated.h"
diff --git a/src/nvim/memfile_defs.h b/src/nvim/memfile_defs.h
index 2e6e914b57..cc71e1a7ff 100644
--- a/src/nvim/memfile_defs.h
+++ b/src/nvim/memfile_defs.h
@@ -1,99 +1,108 @@
#ifndef NVIM_MEMFILE_DEFS_H
#define NVIM_MEMFILE_DEFS_H
-#include "nvim/types.h"
-
-typedef struct block_hdr bhdr_T;
-typedef long blocknr_T;
+#include <stdint.h>
+#include <stdbool.h>
-/*
- * mf_hashtab_T is a chained hashtable with blocknr_T key and arbitrary
- * structures as items. This is an intrusive data structure: we require
- * that items begin with mf_hashitem_T which contains the key and linked
- * list pointers. List of items in each bucket is doubly-linked.
- */
-
-typedef struct mf_hashitem_S mf_hashitem_T;
+#include "nvim/types.h"
-struct mf_hashitem_S {
- mf_hashitem_T *mhi_next;
- mf_hashitem_T *mhi_prev;
+/// A block number.
+///
+/// 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.
+typedef int64_t blocknr_T;
+
+/// A hash item.
+///
+/// Items' keys are block numbers.
+/// Items in the same bucket are organized into a doubly-linked list.
+///
+/// Therefore, items can be arbitrary data structures beginning with pointers
+/// for the list and and a block number key.
+typedef struct mf_hashitem {
+ struct mf_hashitem *mhi_next;
+ struct mf_hashitem *mhi_prev;
blocknr_T mhi_key;
-};
-
-#define MHT_INIT_SIZE 64
-
-typedef struct mf_hashtab_S {
- long_u mht_mask; /* mask used for hash value (nr of items
- * in array is "mht_mask" + 1) */
- long_u mht_count; /* nr of items inserted into hashtable */
- mf_hashitem_T **mht_buckets; /* points to mht_small_buckets or
- *dynamically allocated array */
- mf_hashitem_T *mht_small_buckets[MHT_INIT_SIZE]; /* initial buckets */
- char mht_fixed; /* non-zero value forbids growth */
+} mf_hashitem_T;
+
+/// Initial size for a hashtable.
+#define MHT_INIT_SIZE 64
+
+/// A chained hashtable with block numbers as keys and arbitrary data structures
+/// as items.
+///
+/// This is an intrusive data structure: we require that items begin with
+/// mf_hashitem_T which contains the key and linked list pointers. List of items
+/// in each bucket is doubly-linked.
+typedef struct mf_hashtab {
+ size_t mht_mask; /// mask used to mod hash value to array index
+ /// (nr of items in array is 'mht_mask + 1')
+ size_t mht_count; /// number of items inserted
+ mf_hashitem_T **mht_buckets; /// points to the array of buckets (can be
+ /// mht_small_buckets or a newly allocated array
+ /// when mht_small_buckets becomes too small)
+ mf_hashitem_T *mht_small_buckets[MHT_INIT_SIZE]; /// initial buckets
} mf_hashtab_T;
-/*
- * for each (previously) used block in the memfile there is one block header.
- *
- * The block may be linked in the used list OR in the free list.
- * The used blocks are also kept in hash lists.
- *
- * The used list is a doubly linked list, most recently used block first.
- * The blocks in the used list have a block of memory allocated.
- * mf_used_count is the number of pages in the used list.
- * The hash lists are used to quickly find a block in the used list.
- * The free list is a single linked list, not sorted.
- * The blocks in the free list have no block of memory allocated and
- * the contents of the block in the file (if any) is irrelevant.
- */
-
-struct block_hdr {
- mf_hashitem_T bh_hashitem; /* header for hash table and key */
-#define bh_bnum bh_hashitem.mhi_key /* block number, part of bh_hashitem */
-
- bhdr_T *bh_next; /* next block_hdr in free or used list */
- bhdr_T *bh_prev; /* previous block_hdr in used list */
- char_u *bh_data; /* pointer to memory (for used block) */
- int bh_page_count; /* number of pages in this block */
-
-#define BH_DIRTY 1
-#define BH_LOCKED 2
- char bh_flags; /* BH_DIRTY or BH_LOCKED */
-};
-
-/*
- * when a block with a negative number is flushed to the file, it gets
- * a positive number. Because the reference to the block is still the negative
- * number, we remember the translation to the new positive number in the
- * double linked trans lists. The structure is the same as the hash lists.
- */
-typedef struct nr_trans NR_TRANS;
-
-struct nr_trans {
- mf_hashitem_T nt_hashitem; /* header for hash table and key */
-#define nt_old_bnum nt_hashitem.mhi_key /* old, negative, number */
-
- blocknr_T nt_new_bnum; /* new, positive, number */
-};
-
-struct memfile {
- char_u *mf_fname; /* name of the file */
- char_u *mf_ffname; /* idem, full path */
- int mf_fd; /* file descriptor */
- bhdr_T *mf_free_first; /* first block_hdr in free list */
- bhdr_T *mf_used_first; /* mru block_hdr in used list */
- bhdr_T *mf_used_last; /* lru block_hdr in used list */
- unsigned mf_used_count; /* number of pages in used list */
- unsigned mf_used_count_max; /* maximum number of pages in memory */
- mf_hashtab_T mf_hash; /* hash lists */
- mf_hashtab_T mf_trans; /* trans lists */
- blocknr_T mf_blocknr_max; /* highest positive block number + 1*/
- blocknr_T mf_blocknr_min; /* lowest negative block number - 1 */
- blocknr_T mf_neg_count; /* number of negative blocks numbers */
- blocknr_T mf_infile_count; /* number of pages in the file */
- unsigned mf_page_size; /* number of bytes in a page */
- int mf_dirty; /* TRUE if there are dirty blocks */
-};
-
-#endif // NVIM_MEMFILE_DEFS_H
+/// A block header.
+///
+/// There is a block header for each previously used block in the memfile.
+///
+/// The block may be linked in the used list OR in the free list.
+/// The used blocks are also kept in hash lists.
+///
+/// The used list is a doubly linked list, most recently used block first.
+/// The blocks in the used list have a block of memory allocated.
+/// mf_used_count is the number of pages in the used list.
+/// The hash lists are used to quickly find a block in the used list.
+/// The free list is a single linked list, not sorted.
+/// The blocks in the free list have no block of memory allocated and
+/// the contents of the block in the file (if any) is irrelevant.
+typedef struct bhdr {
+ mf_hashitem_T bh_hashitem; /// header for hash table and key
+#define bh_bnum bh_hashitem.mhi_key /// block number, part of bh_hashitem
+
+ struct bhdr *bh_next; /// next block header in free or used list
+ struct bhdr *bh_prev; /// previous block header in used list
+ void *bh_data; /// pointer to memory (for used block)
+ unsigned bh_page_count; /// number of pages in this block
+
+#define BH_DIRTY 1U
+#define BH_LOCKED 2U
+ unsigned bh_flags; // BH_DIRTY or BH_LOCKED
+} bhdr_T;
+
+/// A block number translation list item.
+///
+/// When a block with a negative number is flushed to the file, it gets
+/// a positive number. Because the reference to the block is still the negative
+/// number, we remember the translation to the new positive number in the
+/// double linked trans lists. The structure is the same as the hash lists.
+typedef struct mf_blocknr_trans_item {
+ mf_hashitem_T nt_hashitem; /// header for hash table and key
+#define nt_old_bnum nt_hashitem.mhi_key /// old, negative, number
+ blocknr_T nt_new_bnum; /// new, positive, number
+} mf_blocknr_trans_item_T;
+
+/// A memory file.
+typedef struct memfile {
+ char_u *mf_fname; /// name of the file
+ char_u *mf_ffname; /// idem, full path
+ int mf_fd; /// file descriptor
+ bhdr_T *mf_free_first; /// first block header in free list
+ bhdr_T *mf_used_first; /// mru block header in used list
+ bhdr_T *mf_used_last; /// lru block header in used list
+ unsigned mf_used_count; /// number of pages in used list
+ unsigned mf_used_count_max; /// maximum number of pages in memory
+ mf_hashtab_T mf_hash; /// hash lists
+ mf_hashtab_T mf_trans; /// trans lists
+ blocknr_T mf_blocknr_max; /// highest positive block number + 1
+ blocknr_T mf_blocknr_min; /// lowest negative block number - 1
+ blocknr_T mf_neg_count; /// number of negative blocks numbers
+ blocknr_T mf_infile_count; /// number of pages in the file
+ unsigned mf_page_size; /// number of bytes in a page
+ bool mf_dirty; /// TRUE if there are dirty blocks
+} memfile_T;
+
+#endif // NVIM_MEMFILE_DEFS_H
diff --git a/src/nvim/memline.c b/src/nvim/memline.c
index 04ee0d7f55..91fbe62c18 100644
--- a/src/nvim/memline.c
+++ b/src/nvim/memline.c
@@ -41,6 +41,7 @@
* mf_get().
*/
+#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <string.h>
@@ -296,12 +297,12 @@ int ml_open(buf_T *buf)
/*
* fill block0 struct and write page 0
*/
- hp = mf_new(mfp, FALSE, 1);
+ hp = mf_new(mfp, false, 1);
if (hp->bh_bnum != 0) {
EMSG(_("E298: Didn't get block nr 0?"));
goto error;
}
- b0p = (ZERO_BL *)(hp->bh_data);
+ b0p = hp->bh_data;
b0p->b0_id[0] = BLOCK0_ID0;
b0p->b0_id[1] = BLOCK0_ID1;
@@ -330,7 +331,7 @@ int ml_open(buf_T *buf)
* Only works when there's a swapfile, otherwise it's done when the file
* is created.
*/
- mf_put(mfp, hp, TRUE, FALSE);
+ mf_put(mfp, hp, true, false);
if (!buf->b_help && !B_SPELL(buf))
(void)mf_sync(mfp, 0);
@@ -343,13 +344,13 @@ int ml_open(buf_T *buf)
EMSG(_("E298: Didn't get block nr 1?"));
goto error;
}
- pp = (PTR_BL *)(hp->bh_data);
+ pp = hp->bh_data;
pp->pb_count = 1;
pp->pb_pointer[0].pe_bnum = 2;
pp->pb_pointer[0].pe_page_count = 1;
pp->pb_pointer[0].pe_old_lnum = 1;
pp->pb_pointer[0].pe_line_count = 1; /* line count after insertion */
- mf_put(mfp, hp, TRUE, FALSE);
+ mf_put(mfp, hp, true, false);
/*
* Allocate first data block and create an empty line 1.
@@ -360,7 +361,7 @@ int ml_open(buf_T *buf)
goto error;
}
- dp = (DATA_BL *)(hp->bh_data);
+ dp = hp->bh_data;
dp->db_index[0] = --dp->db_txt_start; /* at end of block */
dp->db_free -= 1 + INDEX_SIZE;
dp->db_line_count = 1;
@@ -371,8 +372,8 @@ int ml_open(buf_T *buf)
error:
if (mfp != NULL) {
if (hp)
- mf_put(mfp, hp, FALSE, FALSE);
- mf_close(mfp, TRUE); /* will also free(mfp->mf_fname) */
+ mf_put(mfp, hp, false, false);
+ mf_close(mfp, true); /* will also free(mfp->mf_fname) */
}
buf->b_ml.ml_mfp = NULL;
return FAIL;
@@ -525,7 +526,7 @@ void ml_open_file(buf_T *buf)
break;
}
/* Writing block 0 failed: close the file and try another dir */
- mf_close_file(buf, FALSE);
+ mf_close_file(buf, false);
}
}
@@ -563,7 +564,7 @@ void ml_close(buf_T *buf, int del_file)
{
if (buf->b_ml.ml_mfp == NULL) /* not open */
return;
- mf_close(buf->b_ml.ml_mfp, del_file); /* close the .swp file */
+ mf_close(buf->b_ml.ml_mfp, del_file); /* close the .swp file */
if (buf->b_ml.ml_line_lnum != 0 && (buf->b_ml.ml_flags & ML_LINE_DIRTY))
free(buf->b_ml.ml_line_ptr);
free(buf->b_ml.ml_stack);
@@ -638,9 +639,9 @@ static void ml_upd_block0(buf_T *buf, upd_block0_T what)
ZERO_BL *b0p;
mfp = buf->b_ml.ml_mfp;
- if (mfp == NULL || (hp = mf_get(mfp, (blocknr_T)0, 1)) == NULL)
+ if (mfp == NULL || (hp = mf_get(mfp, 0, 1)) == NULL)
return;
- b0p = (ZERO_BL *)(hp->bh_data);
+ b0p = hp->bh_data;
if (ml_check_b0_id(b0p) == FAIL)
EMSG(_("E304: ml_upd_block0(): Didn't get block 0??"));
else {
@@ -649,7 +650,7 @@ static void ml_upd_block0(buf_T *buf, upd_block0_T what)
else /* what == UB_SAME_DIR */
set_b0_dir_flag(b0p, buf);
}
- mf_put(mfp, hp, TRUE, FALSE);
+ mf_put(mfp, hp, true, false);
}
/*
@@ -866,7 +867,7 @@ void ml_recover(void)
/*
* try to read block 0
*/
- if ((hp = mf_get(mfp, (blocknr_T)0, 1)) == NULL) {
+ if ((hp = mf_get(mfp, 0, 1)) == NULL) {
msg_start();
MSG_PUTS_ATTR(_("Unable to read block 0 from "), attr | MSG_HIST);
msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST);
@@ -876,7 +877,7 @@ void ml_recover(void)
msg_end();
goto theend;
}
- b0p = (ZERO_BL *)(hp->bh_data);
+ b0p = hp->bh_data;
if (STRNCMP(b0p->b0_version, "VIM 3.0", 7) == 0) {
msg_start();
msg_outtrans_attr(mfp->mf_fname, MSG_HIST);
@@ -924,7 +925,7 @@ void ml_recover(void)
if ((size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0)
mfp->mf_blocknr_max = 0; /* no file or empty file */
else
- mfp->mf_blocknr_max = (blocknr_T)(size / mfp->mf_page_size);
+ mfp->mf_blocknr_max = size / mfp->mf_page_size;
mfp->mf_infile_count = mfp->mf_blocknr_max;
/* need to reallocate the memory used to store the data */
@@ -932,7 +933,7 @@ void ml_recover(void)
memmove(p, hp->bh_data, previous_page_size);
free(hp->bh_data);
hp->bh_data = p;
- b0p = (ZERO_BL *)(hp->bh_data);
+ b0p = hp->bh_data;
}
/*
@@ -980,7 +981,7 @@ void ml_recover(void)
b0_fenc = vim_strnsave(p, (int)(b0p->b0_fname + fnsize - p));
}
- mf_put(mfp, hp, FALSE, FALSE); /* release block 0 */
+ mf_put(mfp, hp, false, false); /* release block 0 */
hp = NULL;
/*
@@ -1025,12 +1026,12 @@ void ml_recover(void)
serious_error = FALSE;
for (; !got_int; line_breakcheck()) {
if (hp != NULL)
- mf_put(mfp, hp, FALSE, FALSE); /* release previous block */
+ mf_put(mfp, hp, false, false); /* release previous block */
/*
* get block
*/
- if ((hp = mf_get(mfp, (blocknr_T)bnum, page_count)) == NULL) {
+ if ((hp = mf_get(mfp, bnum, page_count)) == NULL) {
if (bnum == 1) {
EMSG2(_("E309: Unable to read block 1 from %s"), mfp->mf_fname);
goto theend;
@@ -1039,7 +1040,7 @@ void ml_recover(void)
ml_append(lnum++, (char_u *)_("???MANY LINES MISSING"),
(colnr_T)0, TRUE);
} else { /* there is a block */
- pp = (PTR_BL *)(hp->bh_data);
+ pp = hp->bh_data;
if (pp->pb_id == PTR_ID) { /* it is a pointer block */
/* check line count when using pointer block first time */
if (idx == 0 && line_count != 0) {
@@ -1096,7 +1097,7 @@ void ml_recover(void)
continue;
}
} else { /* not a pointer block */
- dp = (DATA_BL *)(hp->bh_data);
+ dp = hp->bh_data;
if (dp->db_id != DATA_ID) { /* block id wrong */
if (bnum == 1) {
EMSG2(_("E310: Block 1 ID wrong (%s not a .swp file?)"),
@@ -1236,8 +1237,8 @@ theend:
recoverymode = FALSE;
if (mfp != NULL) {
if (hp != NULL)
- mf_put(mfp, hp, FALSE, FALSE);
- mf_close(mfp, FALSE); /* will also free(mfp->mf_fname) */
+ mf_put(mfp, hp, false, false);
+ mf_close(mfp, false); /* will also free(mfp->mf_fname) */
}
if (buf != NULL) {
free(buf->b_ml.ml_stack);
@@ -1809,7 +1810,7 @@ errorret:
goto errorret;
}
- dp = (DATA_BL *)(hp->bh_data);
+ dp = hp->bh_data;
ptr = (char_u *)dp +
((dp->db_index[lnum - buf->b_ml.ml_locked_low]) & DB_INDEX_MASK);
@@ -1938,7 +1939,7 @@ ml_append_int (
/* get line count before the insertion */
line_count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low;
- dp = (DATA_BL *)(hp->bh_data);
+ dp = hp->bh_data;
/*
* If
@@ -1964,7 +1965,7 @@ ml_append_int (
line_count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low;
CHECK(buf->b_ml.ml_locked_low != lnum + 1, "locked_low != lnum + 1");
- dp = (DATA_BL *)(hp->bh_data);
+ dp = hp->bh_data;
}
++buf->b_ml.ml_line_count;
@@ -2083,8 +2084,8 @@ ml_append_int (
line_count_left = line_count;
line_count_right = 0;
}
- dp_right = (DATA_BL *)(hp_right->bh_data);
- dp_left = (DATA_BL *)(hp_left->bh_data);
+ dp_right = hp_right->bh_data;
+ dp_left = hp_left->bh_data;
bnum_left = hp_left->bh_bnum;
bnum_right = hp_right->bh_bnum;
page_count_left = hp_left->bh_page_count;
@@ -2166,7 +2167,7 @@ ml_append_int (
buf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
if (!newfile && db_idx >= 0 && in_left)
buf->b_ml.ml_flags |= ML_LOCKED_POS;
- mf_put(mfp, hp_new, TRUE, FALSE);
+ mf_put(mfp, hp_new, true, false);
/*
* flush the old data block
@@ -2186,10 +2187,10 @@ ml_append_int (
pb_idx = ip->ip_index;
if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL)
return FAIL;
- pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */
+ pp = hp->bh_data; /* must be pointer block */
if (pp->pb_id != PTR_ID) {
EMSG(_("E317: pointer block id wrong 3"));
- mf_put(mfp, hp, FALSE, FALSE);
+ mf_put(mfp, hp, false, false);
return FAIL;
}
/*
@@ -2215,7 +2216,7 @@ ml_append_int (
if (lnum_right != 0)
pp->pb_pointer[pb_idx + 1].pe_old_lnum = lnum_right;
- mf_put(mfp, hp, TRUE, FALSE);
+ mf_put(mfp, hp, true, false);
buf->b_ml.ml_stack_top = stack_idx + 1; /* truncate stack */
if (lineadd) {
@@ -2243,7 +2244,7 @@ ml_append_int (
hp_new = ml_new_ptr(mfp);
if (hp_new == NULL) /* TODO: try to fix tree */
return FAIL;
- pp_new = (PTR_BL *)(hp_new->bh_data);
+ pp_new = hp_new->bh_data;
if (hp->bh_bnum != 1)
break;
@@ -2260,7 +2261,7 @@ ml_append_int (
pp->pb_pointer[0].pe_line_count = buf->b_ml.ml_line_count;
pp->pb_pointer[0].pe_old_lnum = 1;
pp->pb_pointer[0].pe_page_count = 1;
- mf_put(mfp, hp, TRUE, FALSE); /* release block 1 */
+ mf_put(mfp, hp, true, false); /* release block 1 */
hp = hp_new; /* new block is to be split */
pp = pp_new;
CHECK(stack_idx != 0, _("stack_idx should be 0"));
@@ -2312,8 +2313,8 @@ ml_append_int (
bnum_right = hp_new->bh_bnum;
page_count_left = 1;
page_count_right = 1;
- mf_put(mfp, hp, TRUE, FALSE);
- mf_put(mfp, hp_new, TRUE, FALSE);
+ mf_put(mfp, hp, true, false);
+ mf_put(mfp, hp_new, true, false);
}
}
@@ -2426,7 +2427,7 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, int message)
if ((hp = ml_find_line(buf, lnum, ML_DELETE)) == NULL)
return FAIL;
- dp = (DATA_BL *)(hp->bh_data);
+ dp = hp->bh_data;
/* compute line count before the delete */
count = (long)(buf->b_ml.ml_locked_high)
- (long)(buf->b_ml.ml_locked_low) + 2;
@@ -2460,10 +2461,10 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, int message)
idx = ip->ip_index;
if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL)
return FAIL;
- pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */
+ pp = hp->bh_data; /* must be pointer block */
if (pp->pb_id != PTR_ID) {
EMSG(_("E317: pointer block id wrong 4"));
- mf_put(mfp, hp, FALSE, FALSE);
+ mf_put(mfp, hp, false, false);
return FAIL;
}
count = --(pp->pb_count);
@@ -2473,7 +2474,7 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, int message)
if (count != idx) /* move entries after the deleted one */
memmove(&pp->pb_pointer[idx], &pp->pb_pointer[idx + 1],
(size_t)(count - idx) * sizeof(PTR_EN));
- mf_put(mfp, hp, TRUE, FALSE);
+ mf_put(mfp, hp, true, false);
buf->b_ml.ml_stack_top = stack_idx; /* truncate stack */
/* fix line count for rest of blocks in the stack */
@@ -2540,7 +2541,7 @@ void ml_setmarked(linenr_T lnum)
if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL)
return; /* give error message? */
- dp = (DATA_BL *)(hp->bh_data);
+ dp = hp->bh_data;
dp->db_index[lnum - curbuf->b_ml.ml_locked_low] |= DB_MARKED;
curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
}
@@ -2571,7 +2572,7 @@ linenr_T ml_firstmarked(void)
if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL)
return (linenr_T)0; /* give error message? */
- dp = (DATA_BL *)(hp->bh_data);
+ dp = hp->bh_data;
for (i = lnum - curbuf->b_ml.ml_locked_low;
lnum <= curbuf->b_ml.ml_locked_high; ++i, ++lnum)
@@ -2611,7 +2612,7 @@ void ml_clearmarked(void)
if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL)
return; /* give error message? */
- dp = (DATA_BL *)(hp->bh_data);
+ dp = hp->bh_data;
for (i = lnum - curbuf->b_ml.ml_locked_low;
lnum <= curbuf->b_ml.ml_locked_high; ++i, ++lnum)
@@ -2660,7 +2661,7 @@ static void ml_flush_line(buf_T *buf)
if (hp == NULL)
EMSGN(_("E320: Cannot find line %" PRId64), lnum);
else {
- dp = (DATA_BL *)(hp->bh_data);
+ dp = hp->bh_data;
idx = lnum - buf->b_ml.ml_locked_low;
start = ((dp->db_index[idx]) & DB_INDEX_MASK);
old_line = (char_u *)dp + start;
@@ -2725,8 +2726,9 @@ static void ml_flush_line(buf_T *buf)
*/
static bhdr_T *ml_new_data(memfile_T *mfp, int negative, int page_count)
{
- bhdr_T *hp = mf_new(mfp, negative, page_count);
- DATA_BL *dp = (DATA_BL *)(hp->bh_data);
+ assert(page_count >= 0);
+ bhdr_T *hp = mf_new(mfp, negative, (unsigned)page_count);
+ DATA_BL *dp = hp->bh_data;
dp->db_id = DATA_ID;
dp->db_txt_start = dp->db_txt_end = page_count * mfp->mf_page_size;
dp->db_free = dp->db_txt_start - HEADER_SIZE;
@@ -2740,8 +2742,8 @@ static bhdr_T *ml_new_data(memfile_T *mfp, int negative, int page_count)
*/
static bhdr_T *ml_new_ptr(memfile_T *mfp)
{
- bhdr_T *hp = mf_new(mfp, FALSE, 1);
- PTR_BL *pp = (PTR_BL *)(hp->bh_data);
+ bhdr_T *hp = mf_new(mfp, false, 1);
+ PTR_BL *pp = hp->bh_data;
pp->pb_id = PTR_ID;
pp->pb_count = 0;
pp->pb_count_max = (mfp->mf_page_size - sizeof(PTR_BL)) / sizeof(PTR_EN) + 1;
@@ -2855,7 +2857,7 @@ static bhdr_T *ml_find_line(buf_T *buf, linenr_T lnum, int action)
else if (action == ML_DELETE)
--high;
- dp = (DATA_BL *)(hp->bh_data);
+ dp = hp->bh_data;
if (dp->db_id == DATA_ID) { /* data block */
buf->b_ml.ml_locked = hp;
buf->b_ml.ml_locked_low = low;
@@ -2920,11 +2922,11 @@ static bhdr_T *ml_find_line(buf_T *buf, linenr_T lnum, int action)
pp->pb_pointer[idx].pe_line_count++;
dirty = TRUE;
}
- mf_put(mfp, hp, dirty, FALSE);
+ mf_put(mfp, hp, dirty, false);
}
error_block:
- mf_put(mfp, hp, FALSE, FALSE);
+ mf_put(mfp, hp, false, false);
error_noblock:
/*
* If action is ML_DELETE or ML_INSERT we have to correct the tree for
@@ -2986,15 +2988,15 @@ static void ml_lineadd(buf_T *buf, int count)
ip = &(buf->b_ml.ml_stack[idx]);
if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL)
break;
- pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */
+ pp = hp->bh_data; /* must be pointer block */
if (pp->pb_id != PTR_ID) {
- mf_put(mfp, hp, FALSE, FALSE);
+ mf_put(mfp, hp, false, false);
EMSG(_("E317: pointer block id wrong 2"));
break;
}
pp->pb_pointer[ip->ip_index].pe_line_count += count;
ip->ip_high += count;
- mf_put(mfp, hp, TRUE, FALSE);
+ mf_put(mfp, hp, true, false);
}
}
@@ -3639,7 +3641,7 @@ void ml_setflags(buf_T *buf)
return;
for (hp = buf->b_ml.ml_mfp->mf_used_last; hp != NULL; hp = hp->bh_prev) {
if (hp->bh_bnum == 0) {
- b0p = (ZERO_BL *)(hp->bh_data);
+ b0p = hp->bh_data;
b0p->b0_dirty = buf->b_changed ? B0_DIRTY : 0;
b0p->b0_flags = (b0p->b0_flags & ~B0_FF_MASK)
| (get_fileformat(buf) + 1);
@@ -3751,7 +3753,7 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype)
buf->b_ml.ml_usedchunks = -1;
return;
}
- dp = (DATA_BL *)(hp->bh_data);
+ dp = hp->bh_data;
count = (long)(buf->b_ml.ml_locked_high) -
(long)(buf->b_ml.ml_locked_low) + 1;
idx = curline - buf->b_ml.ml_locked_low;
@@ -3800,7 +3802,7 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype)
buf->b_ml.ml_usedchunks = -1;
return;
}
- dp = (DATA_BL *)(hp->bh_data);
+ dp = hp->bh_data;
if (dp->db_line_count == 1)
rest = dp->db_txt_end - dp->db_txt_start;
else
@@ -3911,7 +3913,7 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
if (curline > buf->b_ml.ml_line_count
|| (hp = ml_find_line(buf, curline, ML_FIND)) == NULL)
return -1;
- dp = (DATA_BL *)(hp->bh_data);
+ dp = hp->bh_data;
count = (long)(buf->b_ml.ml_locked_high) -
(long)(buf->b_ml.ml_locked_low) + 1;
start_idx = idx = curline - buf->b_ml.ml_locked_low;
diff --git a/src/nvim/option.c b/src/nvim/option.c
index ca868120a0..207be0bd74 100644
--- a/src/nvim/option.c
+++ b/src/nvim/option.c
@@ -5010,7 +5010,7 @@ set_bool_option (
else
/* no need to reset curbuf->b_may_swap, ml_open_file() will check
* buf->b_p_swf */
- mf_close_file(curbuf, TRUE); /* remove the swap file */
+ mf_close_file(curbuf, true); /* remove the swap file */
}
/* when 'terse' is set change 'shortmess' */
else if ((int *)varp == &p_terse) {