summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorXavier Roche <xroche@users.noreply.github.com>2014-06-15 11:12:08 +0000
committerXavier Roche <xroche@users.noreply.github.com>2014-06-15 11:12:08 +0000
commitc5003322b79ff69779319af0944daf0248cc5c9e (patch)
tree1519b05ed85b758a361959c2c235330c6343144d /src
parent8e72bb5deb74a68e7a909d97d7d395af9c33204d (diff)
Cleanup in coucal_new()
Diffstat (limited to 'src')
-rw-r--r--src/coucal.c35
1 files changed, 25 insertions, 10 deletions
diff --git a/src/coucal.c b/src/coucal.c
index 2efe862..4075b6f 100644
--- a/src/coucal.c
+++ b/src/coucal.c
@@ -1316,16 +1316,24 @@ intptr_t coucal_get_intptr(coucal hashtable, coucal_key_const name) {
return value;
}
-coucal coucal_new(size_t initial_size) {
- coucal hashtable = (coucal) calloc(1, sizeof(struct_coucal));
+static INTHASH_INLINE size_t coucal_get_pow2(size_t initial_size) {
+ size_t size;
+ for(size = MIN_LG_SIZE
+ ; size < COUCAL_HASH_SIZE && POW2(size) < initial_size
+ ; size++) ;
+ return size;
+}
- if (hashtable) {
- size_t size;
- for(size = MIN_LG_SIZE ; POW2(size) < initial_size ; size++) ;
- if ((hashtable->items =
- (coucal_item *) calloc(POW2(size), sizeof(coucal_item)))) {
- hashtable->lg_size = size;
- }
+coucal coucal_new(size_t initial_size) {
+ const size_t lg_size = coucal_get_pow2(initial_size);
+ coucal hashtable =
+ lg_size < COUCAL_HASH_SIZE ? (coucal) calloc(1, sizeof(struct_coucal)) : NULL;
+ coucal_item *const items =
+ (coucal_item *) calloc(POW2(lg_size), sizeof(coucal_item));
+
+ if (lg_size < COUCAL_HASH_SIZE && items != NULL && hashtable != NULL) {
+ hashtable->lg_size = lg_size;
+ hashtable->items = items;
hashtable->used = 0;
hashtable->stash.size = 0;
hashtable->pool.buffer = NULL;
@@ -1354,8 +1362,15 @@ coucal coucal_new(size_t initial_size) {
hashtable->custom.print.key = NULL;
hashtable->custom.print.value = NULL;
hashtable->custom.print.arg = NULL;
+ return hashtable;
+ }
+ if (items != NULL) {
+ free(items);
}
- return hashtable;
+ if (hashtable != NULL) {
+ free(hashtable);
+ }
+ return NULL;
}
int coucal_created(coucal hashtable) {