aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/hiredis/dict.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/hiredis/dict.c')
-rw-r--r--contrib/hiredis/dict.c47
1 files changed, 25 insertions, 22 deletions
diff --git a/contrib/hiredis/dict.c b/contrib/hiredis/dict.c
index 0fbc1b4cf..ad571818e 100644
--- a/contrib/hiredis/dict.c
+++ b/contrib/hiredis/dict.c
@@ -34,6 +34,7 @@
*/
#include "fmacros.h"
+#include "alloc.h"
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
@@ -71,7 +72,10 @@ static void _dictReset(dict *ht) {
/* Create a new hash table */
static dict *dictCreate(dictType *type, void *privDataPtr) {
- dict *ht = malloc(sizeof(*ht));
+ dict *ht = hi_malloc(sizeof(*ht));
+ if (ht == NULL)
+ return NULL;
+
_dictInit(ht,type,privDataPtr);
return ht;
}
@@ -97,7 +101,9 @@ static int dictExpand(dict *ht, unsigned long size) {
_dictInit(&n, ht->type, ht->privdata);
n.size = realsize;
n.sizemask = realsize-1;
- n.table = calloc(realsize,sizeof(dictEntry*));
+ n.table = hi_calloc(realsize,sizeof(dictEntry*));
+ if (n.table == NULL)
+ return DICT_ERR;
/* Copy all the elements from the old to the new table:
* note that if the old hash table is empty ht->size is zero,
@@ -124,7 +130,7 @@ static int dictExpand(dict *ht, unsigned long size) {
}
}
assert(ht->used == 0);
- free(ht->table);
+ hi_free(ht->table);
/* Remap the new hashtable in the old */
*ht = n;
@@ -142,7 +148,10 @@ static int dictAdd(dict *ht, void *key, void *val) {
return DICT_ERR;
/* Allocates the memory and stores key */
- entry = malloc(sizeof(*entry));
+ entry = hi_malloc(sizeof(*entry));
+ if (entry == NULL)
+ return DICT_ERR;
+
entry->next = ht->table[index];
ht->table[index] = entry;
@@ -166,17 +175,18 @@ static int dictReplace(dict *ht, void *key, void *val) {
return 1;
/* It already exists, get the entry */
entry = dictFind(ht, key);
+ if (entry == NULL)
+ return 0;
+
/* Free the old value and set the new one */
/* Set the new value and free the old one. Note that it is important
* to do that in this order, as the value may just be exactly the same
* as the previous one. In this context, think to reference counting,
* you want to increment (set), and then decrement (free), and not the
* reverse. */
- if (entry) {
- auxentry = *entry;
- dictSetHashVal(ht, entry, val);
- dictFreeEntryVal(ht, &auxentry);
- }
+ auxentry = *entry;
+ dictSetHashVal(ht, entry, val);
+ dictFreeEntryVal(ht, &auxentry);
return 0;
}
@@ -201,7 +211,7 @@ static int dictDelete(dict *ht, const void *key) {
dictFreeEntryKey(ht,de);
dictFreeEntryVal(ht,de);
- free(de);
+ hi_free(de);
ht->used--;
return DICT_OK;
}
@@ -224,13 +234,13 @@ static int _dictClear(dict *ht) {
nextHe = he->next;
dictFreeEntryKey(ht, he);
dictFreeEntryVal(ht, he);
- free(he);
+ hi_free(he);
ht->used--;
he = nextHe;
}
}
/* Free the table and the allocated cache structure */
- free(ht->table);
+ hi_free(ht->table);
/* Re-initialize the table */
_dictReset(ht);
return DICT_OK; /* never fails */
@@ -239,7 +249,7 @@ static int _dictClear(dict *ht) {
/* Clear & Release the hash table */
static void dictRelease(dict *ht) {
_dictClear(ht);
- free(ht);
+ hi_free(ht);
}
static dictEntry *dictFind(dict *ht, const void *key) {
@@ -257,14 +267,11 @@ static dictEntry *dictFind(dict *ht, const void *key) {
return NULL;
}
-static dictIterator *dictGetIterator(dict *ht) {
- dictIterator *iter = malloc(sizeof(*iter));
-
+static void dictInitIterator(dictIterator *iter, dict *ht) {
iter->ht = ht;
iter->index = -1;
iter->entry = NULL;
iter->nextEntry = NULL;
- return iter;
}
static dictEntry *dictNext(dictIterator *iter) {
@@ -287,16 +294,12 @@ static dictEntry *dictNext(dictIterator *iter) {
return NULL;
}
-static void dictReleaseIterator(dictIterator *iter) {
- free(iter);
-}
-
/* ------------------------- private functions ------------------------------ */
/* Expand the hash table if needed */
static int _dictExpandIfNeeded(dict *ht) {
/* If the hash table is empty expand it to the initial size,
- * if the table is "full" dobule its size. */
+ * if the table is "full" double its size. */
if (ht->size == 0)
return dictExpand(ht, DICT_HT_INITIAL_SIZE);
if (ht->used == ht->size)