diff options
author | Xavier Roche <xroche@users.noreply.github.com> | 2014-06-15 12:10:52 +0000 |
---|---|---|
committer | Xavier Roche <xroche@users.noreply.github.com> | 2014-06-15 12:10:52 +0000 |
commit | 96a727e4fbac121323db0e0e1f56dbc543cb58e0 (patch) | |
tree | cd3eb829e73482686e48ccd870e4833ae151503f | |
parent | 4c876451be1a5e6ace67830130efc882ee3d2195 (diff) |
Better coucal_is_acceptable_pow2() logic
-rw-r--r-- | src/coucal.c | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/src/coucal.c b/src/coucal.c index efe0923..e84c512 100644 --- a/src/coucal.c +++ b/src/coucal.c @@ -995,6 +995,10 @@ static int coucal_add_item_(coucal hashtable, coucal_item item) { } } +static INTHASH_INLINE int coucal_is_acceptable_pow2(size_t lg_size) { + return lg_size <= COUCAL_HASH_SIZE && lg_size < sizeof(size_t)*8; +} + int coucal_write_value(coucal hashtable, coucal_key_const name, coucal_value_const value) { /* replace of add item */ @@ -1038,8 +1042,8 @@ int coucal_write_value(coucal hashtable, coucal_key_const name, hashtable->stats.rehash_count++; /* realloc */ - coucal_assert(hashtable, hashtable->lg_size < COUCAL_HASH_SIZE); hashtable->lg_size++; + coucal_assert(hashtable, coucal_is_acceptable_pow2(hashtable->lg_size)); hashtable->items = (coucal_item *) realloc(hashtable->items, alloc_size); if (hashtable->items == NULL) { @@ -1326,8 +1330,7 @@ static INTHASH_INLINE size_t coucal_get_pow2(size_t initial_size) { coucal coucal_new(size_t initial_size) { const size_t lg_size = coucal_get_pow2(initial_size); - const int lg_valid = lg_size <= COUCAL_HASH_SIZE - && lg_size < sizeof(size_t)*8; + const int lg_valid = coucal_is_acceptable_pow2(lg_size); coucal hashtable = lg_valid ? (coucal) calloc(1, sizeof(struct_coucal)) : NULL; coucal_item *const items = |