aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliseo Martínez <eliseomarmol@gmail.com>2014-10-21 00:19:58 +0200
committerEliseo Martínez <eliseomarmol@gmail.com>2014-11-06 22:53:42 +0100
commit200f8ee0e572670f595d18853d18ccb8d3326db3 (patch)
tree272a6b13ef063f1e9176bced61d19631064e49b3
parent7ffed667d4ed1214e31d9f13bc1bd30f116c1b79 (diff)
downloadrneovim-200f8ee0e572670f595d18853d18ccb8d3326db3.tar.gz
rneovim-200f8ee0e572670f595d18853d18ccb8d3326db3.tar.bz2
rneovim-200f8ee0e572670f595d18853d18ccb8d3326db3.zip
Review: Remove long_u: memfile: Refactor: long_u -> size_t.
- memfile_defs.h: * hashtab_T: mht_mask: long_u -> size_t. Masks are used to truncate keys to a value adequate for an index in the array of entries. Value of the mask plus one is the current size of the array. Both of those reasons imply the soundness of size_t for this type. * hashtab_T: mht_count: long_u -> size_t. - memfile.c: * total_mem_used: long_u -> size_t. * mf_hash_free_all: idx: long_u -> size_t. * mf_hash_add_item: idx: long_u -> size_t. * mf_hash_find: idx: long_u -> size_t. * mf_hash_grow: i: long_u -> size_t. * mf_hash_grow: j: long_u -> size_t.
-rw-r--r--src/nvim/memfile.c24
-rw-r--r--src/nvim/memfile_defs.h7
2 files changed, 18 insertions, 13 deletions
diff --git a/src/nvim/memfile.c b/src/nvim/memfile.c
index 0e70865834..0ad2a3a718 100644
--- a/src/nvim/memfile.c
+++ b/src/nvim/memfile.c
@@ -66,7 +66,7 @@
#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"
@@ -251,7 +251,11 @@ 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;
+ 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;
}
@@ -571,7 +575,7 @@ 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
/// higher than the maximum one or total memory used is over 'maxmemtot'.
int need_release = (mfp->mf_used_count >= mfp->mf_used_count_max
- || (total_mem_used >> 10) >= (long_u)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.
if (mfp->mf_fd < 0 && need_release && p_uc) {
@@ -970,7 +974,7 @@ static void mf_hash_free_all(mf_hashtab_T *mht)
{
mf_hashitem_T *next;
- for (long_u idx = 0; idx <= mht->mht_mask; idx++)
+ 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);
@@ -984,7 +988,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.
static mf_hashitem_T *mf_hash_find(mf_hashtab_T *mht, blocknr_T key)
{
- mf_hashitem_T *mhi = mht->mht_buckets[(unsigned long)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;
@@ -993,7 +997,7 @@ static mf_hashitem_T *mf_hash_find(mf_hashtab_T *mht, blocknr_T key)
/// 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 = (unsigned long)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)
@@ -1014,7 +1018,7 @@ 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)
{
if (mhi->mhi_prev == NULL)
- mht->mht_buckets[(unsigned long)mhi->mhi_key & mht->mht_mask] =
+ mht->mht_buckets[(size_t)mhi->mhi_key & mht->mht_mask] =
mhi->mhi_next;
else
mhi->mhi_prev->mhi_next = mhi->mhi_next;
@@ -1039,7 +1043,7 @@ static void mf_hash_grow(mf_hashtab_T *mht)
while ((mht->mht_mask >> shift) != 0)
shift++;
- for (long_u i = 0; i <= mht->mht_mask; i++) {
+ 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
@@ -1054,7 +1058,7 @@ static void mf_hash_grow(mf_hashtab_T *mht)
for (mf_hashitem_T *mhi = mht->mht_buckets[i];
mhi != NULL; mhi = mhi->mhi_next) {
- long_u j = (mhi->mhi_key >> shift) & (MHT_GROWTH_FACTOR - 1);
+ size_t j = (mhi->mhi_key >> shift) & (MHT_GROWTH_FACTOR - 1);
if (tails[j] == NULL) {
buckets[i + (j << shift)] = mhi;
tails[j] = mhi;
@@ -1066,7 +1070,7 @@ static void mf_hash_grow(mf_hashtab_T *mht)
}
}
- for (long_u 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_defs.h b/src/nvim/memfile_defs.h
index ebecdea3a0..723270f760 100644
--- a/src/nvim/memfile_defs.h
+++ b/src/nvim/memfile_defs.h
@@ -1,6 +1,7 @@
#ifndef NVIM_MEMFILE_DEFS_H
#define NVIM_MEMFILE_DEFS_H
+#include <stdint.h>
#include "nvim/types.h"
/// A block number.
@@ -24,7 +25,7 @@ typedef struct mf_hashitem {
} mf_hashitem_T;
/// Initial size for a hashtable.
-#define MHT_INIT_SIZE 64
+#define MHT_INIT_SIZE 64
/// A chained hashtable with block numbers as keys and arbitrary data structures
/// as items.
@@ -33,9 +34,9 @@ typedef struct mf_hashitem {
/// mf_hashitem_T which contains the key and linked list pointers. List of items
/// in each bucket is doubly-linked.
typedef struct mf_hashtab {
- long_u mht_mask; /// mask used to mod hash value to array index
+ size_t mht_mask; /// mask used to mod hash value to array index
/// (nr of items in array is 'mht_mask + 1')
- long_u mht_count; /// number of items inserted
+ 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)