Review: Remove long_u: memfile: Refactor: int -> bool.

Replace int with bool where appropriate.
This commit is contained in:
Eliseo Martínez 2014-10-24 20:04:14 +02:00
parent 96e7f229a5
commit 7d4ec612b1
4 changed files with 53 additions and 51 deletions

View File

@ -48,6 +48,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <limits.h> #include <limits.h>
#include <string.h> #include <string.h>
#include <stdbool.h>
#include "nvim/vim.h" #include "nvim/vim.h"
#include "nvim/ascii.h" #include "nvim/ascii.h"
@ -107,7 +108,7 @@ memfile_T *mf_open(char_u *fname, int flags)
mfp->mf_free_first = NULL; // free list is empty mfp->mf_free_first = NULL; // free list is empty
mfp->mf_used_first = NULL; // used list is empty mfp->mf_used_first = NULL; // used list is empty
mfp->mf_used_last = NULL; mfp->mf_used_last = NULL;
mfp->mf_dirty = FALSE; mfp->mf_dirty = false;
mfp->mf_used_count = 0; mfp->mf_used_count = 0;
mf_hash_init(&mfp->mf_hash); mf_hash_init(&mfp->mf_hash);
mf_hash_init(&mfp->mf_trans); mf_hash_init(&mfp->mf_trans);
@ -180,14 +181,14 @@ int mf_open_file(memfile_T *mfp, char_u *fname)
if (mfp->mf_fd < 0) if (mfp->mf_fd < 0)
return FAIL; return FAIL;
mfp->mf_dirty = TRUE; mfp->mf_dirty = true;
return OK; return OK;
} }
/// Close a memory file and optionally delete the associated file. /// Close a memory file and optionally delete the associated file.
/// ///
/// @param del_file TRUE to delete associated file. /// @param del_file Whether to delete associated file.
void mf_close(memfile_T *mfp, int del_file) void mf_close(memfile_T *mfp, bool del_file)
{ {
bhdr_T *hp, *nextp; bhdr_T *hp, *nextp;
@ -216,8 +217,8 @@ void mf_close(memfile_T *mfp, int del_file)
/// Close the swap file for a memfile. Used when 'swapfile' is reset. /// Close the swap file for a memfile. Used when 'swapfile' is reset.
/// ///
/// @param getlines TRUE to get all lines into memory. /// @param getlines Whether to get all lines into memory.
void mf_close_file (buf_T *buf, int getlines) void mf_close_file (buf_T *buf, bool getlines)
{ {
memfile_T *mfp = buf->b_ml.ml_mfp; memfile_T *mfp = buf->b_ml.ml_mfp;
if (mfp == NULL || mfp->mf_fd < 0) // nothing to close if (mfp == NULL || mfp->mf_fd < 0) // nothing to close
@ -261,9 +262,9 @@ void mf_new_page_size(memfile_T *mfp, unsigned new_size)
/// Get a new block /// Get a new block
/// ///
/// @param negative TRUE if negative block number desired (data block) /// @param negative Whether a negative block number is desired (data block).
/// @param page_count Desired number of pages. /// @param page_count Desired number of pages.
bhdr_T *mf_new(memfile_T *mfp, int negative, unsigned page_count) bhdr_T *mf_new(memfile_T *mfp, bool negative, unsigned page_count)
{ {
// If we reached the maximum size for the used memory blocks, release one. // 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 a bhdr_T is returned, use it and adjust the page_count if necessary.
@ -313,7 +314,7 @@ bhdr_T *mf_new(memfile_T *mfp, int negative, unsigned page_count)
} }
} }
hp->bh_flags = BH_LOCKED | BH_DIRTY; // new block is always dirty hp->bh_flags = BH_LOCKED | BH_DIRTY; // new block is always dirty
mfp->mf_dirty = TRUE; mfp->mf_dirty = true;
hp->bh_page_count = page_count; hp->bh_page_count = page_count;
mf_ins_used(mfp, hp); mf_ins_used(mfp, hp);
mf_ins_hash(mfp, hp); mf_ins_hash(mfp, hp);
@ -373,9 +374,9 @@ bhdr_T *mf_get(memfile_T *mfp, blocknr_T nr, unsigned page_count)
/// Release the block *hp. /// Release the block *hp.
/// ///
/// @param dirty Block must be written to file later. /// @param dirty Whether block must be written to file later.
/// @param infile Block should be in file (needed for recovery). /// @param infile Whether block should be in file (needed for recovery).
void mf_put(memfile_T *mfp, bhdr_T *hp, int dirty, int infile) void mf_put(memfile_T *mfp, bhdr_T *hp, bool dirty, bool infile)
{ {
unsigned flags = hp->bh_flags; unsigned flags = hp->bh_flags;
@ -384,7 +385,7 @@ void mf_put(memfile_T *mfp, bhdr_T *hp, int dirty, int infile)
flags &= ~BH_LOCKED; flags &= ~BH_LOCKED;
if (dirty) { if (dirty) {
flags |= BH_DIRTY; flags |= BH_DIRTY;
mfp->mf_dirty = TRUE; mfp->mf_dirty = true;
} }
hp->bh_flags = flags; hp->bh_flags = flags;
if (infile) if (infile)
@ -423,7 +424,7 @@ int mf_sync(memfile_T *mfp, int flags)
int got_int_save = got_int; int got_int_save = got_int;
if (mfp->mf_fd < 0) { // there is no file, nothing to do if (mfp->mf_fd < 0) { // there is no file, nothing to do
mfp->mf_dirty = FALSE; mfp->mf_dirty = false;
return FAIL; return FAIL;
} }
@ -460,7 +461,7 @@ int mf_sync(memfile_T *mfp, int flags)
// If the whole list is flushed, the memfile is not dirty anymore. // 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. // In case of an error, dirty flag is also set, to avoid trying all the time.
if (hp == NULL || status == FAIL) if (hp == NULL || status == FAIL)
mfp->mf_dirty = FALSE; mfp->mf_dirty = false;
if ((flags & MFS_FLUSH) && *p_sws != NUL) { if ((flags & MFS_FLUSH) && *p_sws != NUL) {
#if defined(UNIX) #if defined(UNIX)
@ -500,7 +501,7 @@ void mf_set_dirty(memfile_T *mfp)
for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev) for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
if (hp->bh_bnum > 0) if (hp->bh_bnum > 0)
hp->bh_flags |= BH_DIRTY; hp->bh_flags |= BH_DIRTY;
mfp->mf_dirty = TRUE; mfp->mf_dirty = true;
} }
/// Insert block in front of memfile's hash list. /// Insert block in front of memfile's hash list.
@ -574,8 +575,8 @@ static bhdr_T *mf_release(memfile_T *mfp, unsigned page_count)
/// Need to release a block if the number of blocks for this memfile is /// 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'. /// higher than the maximum one or total memory used is over 'maxmemtot'.
int need_release = (mfp->mf_used_count >= mfp->mf_used_count_max bool need_release = (mfp->mf_used_count >= mfp->mf_used_count_max
|| (total_mem_used >> 10) >= (size_t)p_mmt); || (total_mem_used >> 10) >= (size_t)p_mmt);
/// Try to create swap file if the amount of memory used is getting too high. /// Try to create swap file if the amount of memory used is getting too high.
if (mfp->mf_fd < 0 && need_release && p_uc) { if (mfp->mf_fd < 0 && need_release && p_uc) {
@ -629,11 +630,10 @@ static bhdr_T *mf_release(memfile_T *mfp, unsigned page_count)
/// ///
/// Used in case of out of memory /// Used in case of out of memory
/// ///
/// @return TRUE If any memory was released. /// @return Whether any memory was released.
/// FALSE If not. bool mf_release_all(void)
int mf_release_all(void)
{ {
int retval = FALSE; bool retval = false;
FOR_ALL_BUFFERS(buf) { FOR_ALL_BUFFERS(buf) {
memfile_T *mfp = buf->b_ml.ml_mfp; memfile_T *mfp = buf->b_ml.ml_mfp;
if (mfp != NULL) { if (mfp != NULL) {
@ -651,7 +651,7 @@ int mf_release_all(void)
mf_rem_hash(mfp, hp); mf_rem_hash(mfp, hp);
mf_free_bhdr(hp); mf_free_bhdr(hp);
hp = mfp->mf_used_last; // restart, list was changed hp = mfp->mf_used_last; // restart, list was changed
retval = TRUE; retval = true;
} else } else
hp = hp->bh_prev; hp = hp->bh_prev;
} }
@ -895,7 +895,7 @@ void mf_fullname(memfile_T *mfp)
} }
/// Return TRUE if there are any translations pending for memfile. /// Return TRUE if there are any translations pending for memfile.
int mf_need_trans(memfile_T *mfp) bool mf_need_trans(memfile_T *mfp)
{ {
return mfp->mf_fname != NULL && mfp->mf_neg_count > 0; return mfp->mf_fname != NULL && mfp->mf_neg_count > 0;
} }

View File

@ -2,6 +2,8 @@
#define NVIM_MEMFILE_DEFS_H #define NVIM_MEMFILE_DEFS_H
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#include "nvim/types.h" #include "nvim/types.h"
/// A block number. /// A block number.
@ -100,7 +102,7 @@ typedef struct memfile {
blocknr_T mf_neg_count; /// number of negative blocks numbers blocknr_T mf_neg_count; /// number of negative blocks numbers
blocknr_T mf_infile_count; /// number of pages in the file blocknr_T mf_infile_count; /// number of pages in the file
unsigned mf_page_size; /// number of bytes in a page unsigned mf_page_size; /// number of bytes in a page
int mf_dirty; /// TRUE if there are dirty blocks bool mf_dirty; /// TRUE if there are dirty blocks
} memfile_T; } memfile_T;
#endif // NVIM_MEMFILE_DEFS_H #endif // NVIM_MEMFILE_DEFS_H

View File

@ -297,7 +297,7 @@ int ml_open(buf_T *buf)
/* /*
* fill block0 struct and write page 0 * fill block0 struct and write page 0
*/ */
hp = mf_new(mfp, FALSE, 1); hp = mf_new(mfp, false, 1);
if (hp->bh_bnum != 0) { if (hp->bh_bnum != 0) {
EMSG(_("E298: Didn't get block nr 0?")); EMSG(_("E298: Didn't get block nr 0?"));
goto error; goto error;
@ -331,7 +331,7 @@ int ml_open(buf_T *buf)
* Only works when there's a swapfile, otherwise it's done when the file * Only works when there's a swapfile, otherwise it's done when the file
* is created. * is created.
*/ */
mf_put(mfp, hp, TRUE, FALSE); mf_put(mfp, hp, true, false);
if (!buf->b_help && !B_SPELL(buf)) if (!buf->b_help && !B_SPELL(buf))
(void)mf_sync(mfp, 0); (void)mf_sync(mfp, 0);
@ -350,7 +350,7 @@ int ml_open(buf_T *buf)
pp->pb_pointer[0].pe_page_count = 1; pp->pb_pointer[0].pe_page_count = 1;
pp->pb_pointer[0].pe_old_lnum = 1; pp->pb_pointer[0].pe_old_lnum = 1;
pp->pb_pointer[0].pe_line_count = 1; /* line count after insertion */ 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. * Allocate first data block and create an empty line 1.
@ -372,8 +372,8 @@ int ml_open(buf_T *buf)
error: error:
if (mfp != NULL) { if (mfp != NULL) {
if (hp) if (hp)
mf_put(mfp, hp, FALSE, FALSE); mf_put(mfp, hp, false, false);
mf_close(mfp, TRUE); /* will also free(mfp->mf_fname) */ mf_close(mfp, true); /* will also free(mfp->mf_fname) */
} }
buf->b_ml.ml_mfp = NULL; buf->b_ml.ml_mfp = NULL;
return FAIL; return FAIL;
@ -526,7 +526,7 @@ void ml_open_file(buf_T *buf)
break; break;
} }
/* Writing block 0 failed: close the file and try another dir */ /* Writing block 0 failed: close the file and try another dir */
mf_close_file(buf, FALSE); mf_close_file(buf, false);
} }
} }
@ -564,7 +564,7 @@ void ml_close(buf_T *buf, int del_file)
{ {
if (buf->b_ml.ml_mfp == NULL) /* not open */ if (buf->b_ml.ml_mfp == NULL) /* not open */
return; 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)) 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_line_ptr);
free(buf->b_ml.ml_stack); free(buf->b_ml.ml_stack);
@ -650,7 +650,7 @@ static void ml_upd_block0(buf_T *buf, upd_block0_T what)
else /* what == UB_SAME_DIR */ else /* what == UB_SAME_DIR */
set_b0_dir_flag(b0p, buf); set_b0_dir_flag(b0p, buf);
} }
mf_put(mfp, hp, TRUE, FALSE); mf_put(mfp, hp, true, false);
} }
/* /*
@ -981,7 +981,7 @@ void ml_recover(void)
b0_fenc = vim_strnsave(p, (int)(b0p->b0_fname + fnsize - p)); 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; hp = NULL;
/* /*
@ -1026,7 +1026,7 @@ void ml_recover(void)
serious_error = FALSE; serious_error = FALSE;
for (; !got_int; line_breakcheck()) { for (; !got_int; line_breakcheck()) {
if (hp != NULL) if (hp != NULL)
mf_put(mfp, hp, FALSE, FALSE); /* release previous block */ mf_put(mfp, hp, false, false); /* release previous block */
/* /*
* get block * get block
@ -1237,8 +1237,8 @@ theend:
recoverymode = FALSE; recoverymode = FALSE;
if (mfp != NULL) { if (mfp != NULL) {
if (hp != NULL) if (hp != NULL)
mf_put(mfp, hp, FALSE, FALSE); mf_put(mfp, hp, false, false);
mf_close(mfp, FALSE); /* will also free(mfp->mf_fname) */ mf_close(mfp, false); /* will also free(mfp->mf_fname) */
} }
if (buf != NULL) { if (buf != NULL) {
free(buf->b_ml.ml_stack); free(buf->b_ml.ml_stack);
@ -2167,7 +2167,7 @@ ml_append_int (
buf->b_ml.ml_flags |= ML_LOCKED_DIRTY; buf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
if (!newfile && db_idx >= 0 && in_left) if (!newfile && db_idx >= 0 && in_left)
buf->b_ml.ml_flags |= ML_LOCKED_POS; 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 * flush the old data block
@ -2190,7 +2190,7 @@ ml_append_int (
pp = hp->bh_data; /* must be pointer block */ pp = hp->bh_data; /* must be pointer block */
if (pp->pb_id != PTR_ID) { if (pp->pb_id != PTR_ID) {
EMSG(_("E317: pointer block id wrong 3")); EMSG(_("E317: pointer block id wrong 3"));
mf_put(mfp, hp, FALSE, FALSE); mf_put(mfp, hp, false, false);
return FAIL; return FAIL;
} }
/* /*
@ -2216,7 +2216,7 @@ ml_append_int (
if (lnum_right != 0) if (lnum_right != 0)
pp->pb_pointer[pb_idx + 1].pe_old_lnum = lnum_right; 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 */ buf->b_ml.ml_stack_top = stack_idx + 1; /* truncate stack */
if (lineadd) { if (lineadd) {
@ -2261,7 +2261,7 @@ ml_append_int (
pp->pb_pointer[0].pe_line_count = buf->b_ml.ml_line_count; 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_old_lnum = 1;
pp->pb_pointer[0].pe_page_count = 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 */ hp = hp_new; /* new block is to be split */
pp = pp_new; pp = pp_new;
CHECK(stack_idx != 0, _("stack_idx should be 0")); CHECK(stack_idx != 0, _("stack_idx should be 0"));
@ -2313,8 +2313,8 @@ ml_append_int (
bnum_right = hp_new->bh_bnum; bnum_right = hp_new->bh_bnum;
page_count_left = 1; page_count_left = 1;
page_count_right = 1; page_count_right = 1;
mf_put(mfp, hp, TRUE, FALSE); mf_put(mfp, hp, true, false);
mf_put(mfp, hp_new, TRUE, FALSE); mf_put(mfp, hp_new, true, false);
} }
} }
@ -2464,7 +2464,7 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, int message)
pp = hp->bh_data; /* must be pointer block */ pp = hp->bh_data; /* must be pointer block */
if (pp->pb_id != PTR_ID) { if (pp->pb_id != PTR_ID) {
EMSG(_("E317: pointer block id wrong 4")); EMSG(_("E317: pointer block id wrong 4"));
mf_put(mfp, hp, FALSE, FALSE); mf_put(mfp, hp, false, false);
return FAIL; return FAIL;
} }
count = --(pp->pb_count); count = --(pp->pb_count);
@ -2474,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 */ if (count != idx) /* move entries after the deleted one */
memmove(&pp->pb_pointer[idx], &pp->pb_pointer[idx + 1], memmove(&pp->pb_pointer[idx], &pp->pb_pointer[idx + 1],
(size_t)(count - idx) * sizeof(PTR_EN)); (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 */ buf->b_ml.ml_stack_top = stack_idx; /* truncate stack */
/* fix line count for rest of blocks in the stack */ /* fix line count for rest of blocks in the stack */
@ -2742,7 +2742,7 @@ static bhdr_T *ml_new_data(memfile_T *mfp, int negative, int page_count)
*/ */
static bhdr_T *ml_new_ptr(memfile_T *mfp) static bhdr_T *ml_new_ptr(memfile_T *mfp)
{ {
bhdr_T *hp = mf_new(mfp, FALSE, 1); bhdr_T *hp = mf_new(mfp, false, 1);
PTR_BL *pp = hp->bh_data; PTR_BL *pp = hp->bh_data;
pp->pb_id = PTR_ID; pp->pb_id = PTR_ID;
pp->pb_count = 0; pp->pb_count = 0;
@ -2922,11 +2922,11 @@ static bhdr_T *ml_find_line(buf_T *buf, linenr_T lnum, int action)
pp->pb_pointer[idx].pe_line_count++; pp->pb_pointer[idx].pe_line_count++;
dirty = TRUE; dirty = TRUE;
} }
mf_put(mfp, hp, dirty, FALSE); mf_put(mfp, hp, dirty, false);
} }
error_block: error_block:
mf_put(mfp, hp, FALSE, FALSE); mf_put(mfp, hp, false, false);
error_noblock: error_noblock:
/* /*
* If action is ML_DELETE or ML_INSERT we have to correct the tree for * If action is ML_DELETE or ML_INSERT we have to correct the tree for
@ -2990,13 +2990,13 @@ static void ml_lineadd(buf_T *buf, int count)
break; break;
pp = hp->bh_data; /* must be pointer block */ pp = hp->bh_data; /* must be pointer block */
if (pp->pb_id != PTR_ID) { 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")); EMSG(_("E317: pointer block id wrong 2"));
break; break;
} }
pp->pb_pointer[ip->ip_index].pe_line_count += count; pp->pb_pointer[ip->ip_index].pe_line_count += count;
ip->ip_high += count; ip->ip_high += count;
mf_put(mfp, hp, TRUE, FALSE); mf_put(mfp, hp, true, false);
} }
} }

View File

@ -5010,7 +5010,7 @@ set_bool_option (
else else
/* no need to reset curbuf->b_may_swap, ml_open_file() will check /* no need to reset curbuf->b_may_swap, ml_open_file() will check
* buf->b_p_swf */ * 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' */ /* when 'terse' is set change 'shortmess' */
else if ((int *)varp == &p_terse) { else if ((int *)varp == &p_terse) {