Review: Remove long_u: memfile: Enable -Wconversion.

- Add memfile.c to converted files list.
- Fix conversion issues:
    * bhdr_T          : bh_page_count : int  -> unsigned.
    * bhdr_T          : bh_flags      : char -> unsigned.
    * mf_new()        : page_count    : int  -> unsigned.
    * mf_get()        : page_count    : int  -> unsigned.
    * mf_release()    : page_count    : int  -> unsigned.
    * mf_alloc_bhdr() : page_count    : int  -> unsigned.
    * mf_trans_add()  : page_count    : int  -> unsigned.
    * mf_put()        : flags         : int  -> unsigned.
This commit is contained in:
Eliseo Martínez
2014-10-20 20:05:40 +02:00
parent fce201e656
commit 7ffed667d4
4 changed files with 26 additions and 18 deletions

View File

@@ -45,6 +45,7 @@ set(CONV_SOURCES
hashtab.c hashtab.c
log.c log.c
map.c map.c
memfile.c
memory.c memory.c
misc2.c misc2.c
profile.c profile.c

View File

@@ -43,8 +43,10 @@
/// mf_trans_del() may translate negative to positive block number /// mf_trans_del() may translate negative to positive block number
/// mf_fullname() make file name full path (use before first :cd) /// mf_fullname() make file name full path (use before first :cd)
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <inttypes.h> #include <inttypes.h>
#include <limits.h>
#include <string.h> #include <string.h>
#include "nvim/vim.h" #include "nvim/vim.h"
@@ -116,7 +118,8 @@ memfile_T *mf_open(char_u *fname, int flags)
if (mfp->mf_fd >= 0 && os_fileinfo_fd(mfp->mf_fd, &file_info)) { if (mfp->mf_fd >= 0 && os_fileinfo_fd(mfp->mf_fd, &file_info)) {
uint64_t blocksize = os_fileinfo_blocksize(&file_info); uint64_t blocksize = os_fileinfo_blocksize(&file_info);
if (blocksize >= MIN_SWAP_PAGE_SIZE && blocksize <= MAX_SWAP_PAGE_SIZE) { 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;
} }
} }
@@ -147,7 +150,9 @@ memfile_T *mf_open(char_u *fname, int flags)
--shift; --shift;
} }
mfp->mf_used_count_max = (p_mm << shift) / page_size; assert(p_mm << shift >= p_mm); // check we don't overflow
assert(p_mm < UINT_MAX); // check we can cast to unsigned
mfp->mf_used_count_max = (unsigned)(p_mm << shift) / page_size;
if (mfp->mf_used_count_max < 10) if (mfp->mf_used_count_max < 10)
mfp->mf_used_count_max = 10; mfp->mf_used_count_max = 10;
} }
@@ -254,7 +259,7 @@ void mf_new_page_size(memfile_T *mfp, unsigned new_size)
/// ///
/// @param negative TRUE if negative block number desired (data block) /// @param negative TRUE if negative block number 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, int page_count) bhdr_T *mf_new(memfile_T *mfp, int 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.
@@ -311,8 +316,7 @@ bhdr_T *mf_new(memfile_T *mfp, int negative, int page_count)
// Init the data to all zero, to avoid reading uninitialized data. // Init the data to all zero, to avoid reading uninitialized data.
// This also avoids that the passwd file ends up in the swap file! // This also avoids that the passwd file ends up in the swap file!
(void)memset((char *)(hp->bh_data), 0, (void)memset((char *)(hp->bh_data), 0, mfp->mf_page_size * page_count);
(size_t)mfp->mf_page_size * page_count);
return hp; return hp;
} }
@@ -322,7 +326,7 @@ bhdr_T *mf_new(memfile_T *mfp, int negative, int page_count)
// Caller should first check a negative nr with mf_trans_del(). // Caller should first check a negative nr with mf_trans_del().
// //
// @return NULL if not found // @return NULL if not found
bhdr_T *mf_get(memfile_T *mfp, blocknr_T nr, int page_count) bhdr_T *mf_get(memfile_T *mfp, blocknr_T nr, unsigned page_count)
{ {
// check block number exists // check block number exists
if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min) if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
@@ -369,7 +373,7 @@ bhdr_T *mf_get(memfile_T *mfp, blocknr_T nr, int page_count)
/// @param infile Block should be in file (needed for recovery). /// @param infile 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, int dirty, int infile)
{ {
int flags = hp->bh_flags; unsigned flags = hp->bh_flags;
if ((flags & BH_LOCKED) == 0) if ((flags & BH_LOCKED) == 0)
EMSG(_("E293: block was not locked")); EMSG(_("E293: block was not locked"));
@@ -558,7 +562,7 @@ static void mf_rem_used(memfile_T *mfp, bhdr_T *hp)
/// - Tried to create swap file but couldn't. /// - Tried to create swap file but couldn't.
/// - All blocks are locked. /// - All blocks are locked.
/// - Unlocked dirty block found, but flush failed. /// - Unlocked dirty block found, but flush failed.
static bhdr_T *mf_release(memfile_T *mfp, int page_count) static bhdr_T *mf_release(memfile_T *mfp, unsigned page_count)
{ {
// don't release while in mf_close_file() // don't release while in mf_close_file()
if (mf_dont_release) if (mf_dont_release)
@@ -654,7 +658,7 @@ int mf_release_all(void)
} }
/// Allocate a block header and a block of memory for it. /// Allocate a block header and a block of memory for it.
static bhdr_T *mf_alloc_bhdr(memfile_T *mfp, int page_count) static bhdr_T *mf_alloc_bhdr(memfile_T *mfp, unsigned page_count)
{ {
bhdr_T *hp = xmalloc(sizeof(bhdr_T)); bhdr_T *hp = xmalloc(sizeof(bhdr_T));
hp->bh_data = xmalloc(mfp->mf_page_size * page_count); hp->bh_data = xmalloc(mfp->mf_page_size * page_count);
@@ -811,7 +815,7 @@ static int mf_trans_add(memfile_T *mfp, bhdr_T *hp)
// Otherwise use mf_blocknr_max. // Otherwise use mf_blocknr_max.
blocknr_T new_bnum; blocknr_T new_bnum;
bhdr_T *freep = mfp->mf_free_first; bhdr_T *freep = mfp->mf_free_first;
int page_count = hp->bh_page_count; unsigned page_count = hp->bh_page_count;
if (freep != NULL && freep->bh_page_count >= page_count) { if (freep != NULL && freep->bh_page_count >= page_count) {
new_bnum = freep->bh_bnum; new_bnum = freep->bh_bnum;
// If the page count of the free block was larger, reduce it. // If the page count of the free block was larger, reduce it.
@@ -980,7 +984,7 @@ static void mf_hash_free_all(mf_hashtab_T *mht)
/// @return A pointer to a mf_hashitem_T or NULL if the item was not found. /// @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) static mf_hashitem_T *mf_hash_find(mf_hashtab_T *mht, blocknr_T key)
{ {
mf_hashitem_T *mhi = mht->mht_buckets[key & mht->mht_mask]; mf_hashitem_T *mhi = mht->mht_buckets[(unsigned long)key & mht->mht_mask];
while (mhi != NULL && mhi->mhi_key != key) while (mhi != NULL && mhi->mhi_key != key)
mhi = mhi->mhi_next; mhi = mhi->mhi_next;
return mhi; return mhi;
@@ -989,7 +993,7 @@ static mf_hashitem_T *mf_hash_find(mf_hashtab_T *mht, blocknr_T key)
/// Add item to hashtable. Item 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) static void mf_hash_add_item(mf_hashtab_T *mht, mf_hashitem_T *mhi)
{ {
long_u idx = mhi->mhi_key & mht->mht_mask; long_u idx = (unsigned long)mhi->mhi_key & mht->mht_mask;
mhi->mhi_next = mht->mht_buckets[idx]; mhi->mhi_next = mht->mht_buckets[idx];
mhi->mhi_prev = NULL; mhi->mhi_prev = NULL;
if (mhi->mhi_next != NULL) if (mhi->mhi_next != NULL)
@@ -1010,7 +1014,8 @@ static void mf_hash_add_item(mf_hashtab_T *mht, mf_hashitem_T *mhi)
static void mf_hash_rem_item(mf_hashtab_T *mht, mf_hashitem_T *mhi) static void mf_hash_rem_item(mf_hashtab_T *mht, mf_hashitem_T *mhi)
{ {
if (mhi->mhi_prev == NULL) if (mhi->mhi_prev == NULL)
mht->mht_buckets[mhi->mhi_key & mht->mht_mask] = mhi->mhi_next; mht->mht_buckets[(unsigned long)mhi->mhi_key & mht->mht_mask] =
mhi->mhi_next;
else else
mhi->mhi_prev->mhi_next = mhi->mhi_next; mhi->mhi_prev->mhi_next = mhi->mhi_next;

View File

@@ -64,11 +64,11 @@ typedef struct bhdr {
struct bhdr *bh_next; /// next block header in free or used list struct bhdr *bh_next; /// next block header in free or used list
struct bhdr *bh_prev; /// previous block header in used list struct bhdr *bh_prev; /// previous block header in used list
char_u *bh_data; /// pointer to memory (for used block) char_u *bh_data; /// pointer to memory (for used block)
int bh_page_count; /// number of pages in this block unsigned bh_page_count; /// number of pages in this block
#define BH_DIRTY 1 #define BH_DIRTY 1U
#define BH_LOCKED 2 #define BH_LOCKED 2U
char bh_flags; // BH_DIRTY or BH_LOCKED unsigned bh_flags; // BH_DIRTY or BH_LOCKED
} bhdr_T; } bhdr_T;
/// A block number translation list item. /// A block number translation list item.

View File

@@ -41,6 +41,7 @@
* mf_get(). * mf_get().
*/ */
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <inttypes.h> #include <inttypes.h>
#include <string.h> #include <string.h>
@@ -2725,7 +2726,8 @@ static void ml_flush_line(buf_T *buf)
*/ */
static bhdr_T *ml_new_data(memfile_T *mfp, int negative, int page_count) static bhdr_T *ml_new_data(memfile_T *mfp, int negative, int page_count)
{ {
bhdr_T *hp = mf_new(mfp, negative, page_count); assert(page_count >= 0);
bhdr_T *hp = mf_new(mfp, negative, (unsigned)page_count);
DATA_BL *dp = (DATA_BL *)(hp->bh_data); DATA_BL *dp = (DATA_BL *)(hp->bh_data);
dp->db_id = DATA_ID; dp->db_id = DATA_ID;
dp->db_txt_start = dp->db_txt_end = page_count * mfp->mf_page_size; dp->db_txt_start = dp->db_txt_end = page_count * mfp->mf_page_size;