summaryrefslogtreecommitdiff
path: root/hashmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'hashmap.c')
-rw-r--r--hashmap.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/hashmap.c b/hashmap.c
index e44d8a3..bb7c997 100644
--- a/hashmap.c
+++ b/hashmap.c
@@ -114,6 +114,7 @@ int hashmap_bucket(const struct hashmap *map, unsigned int hash)
static void rehash(struct hashmap *map, unsigned int newsize)
{
+ /* map->table MUST NOT be NULL when this function is called */
unsigned int i, oldsize = map->tablesize;
struct hashmap_entry **oldtable = map->table;
@@ -134,6 +135,7 @@ static void rehash(struct hashmap *map, unsigned int newsize)
static inline struct hashmap_entry **find_entry_ptr(const struct hashmap *map,
const struct hashmap_entry *key, const void *keydata)
{
+ /* map->table MUST NOT be NULL when this function is called */
struct hashmap_entry **e = &map->table[bucket(map, key)];
while (*e && !entry_equals(map, *e, key, keydata))
e = &(*e)->next;
@@ -196,6 +198,8 @@ struct hashmap_entry *hashmap_get(const struct hashmap *map,
const struct hashmap_entry *key,
const void *keydata)
{
+ if (!map->table)
+ return NULL;
return *find_entry_ptr(map, key, keydata);
}
@@ -211,8 +215,12 @@ struct hashmap_entry *hashmap_get_next(const struct hashmap *map,
void hashmap_add(struct hashmap *map, struct hashmap_entry *entry)
{
- unsigned int b = bucket(map, entry);
+ unsigned int b;
+
+ if (!map->table)
+ alloc_table(map, HASHMAP_INITIAL_SIZE);
+ b = bucket(map, entry);
/* add entry */
entry->next = map->table[b];
map->table[b] = entry;
@@ -230,7 +238,11 @@ struct hashmap_entry *hashmap_remove(struct hashmap *map,
const void *keydata)
{
struct hashmap_entry *old;
- struct hashmap_entry **e = find_entry_ptr(map, key, keydata);
+ struct hashmap_entry **e;
+
+ if (!map->table)
+ return NULL;
+ e = find_entry_ptr(map, key, keydata);
if (!*e)
return NULL;