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);