Example #1
0
/* hashmap_insert - enter (key, value) pair */
hashmap_entry_t *hashmap_insert(hashmap_t *map, char *key, uint32_t key_len, void *value)
{
    hashmap_entry_t *ht = NULL;
    
    if ((map->used >= map->size) && hashmap_grow(map)==-1)
    {
        printf("hashmap_grow error:%m");
        return NULL;
    }
    
    ht = (hashmap_entry_t *)calloc(1, sizeof(hashmap_entry_t) + key_len + 1);
    if(!ht)
    {
        printf("malloc hashmap entry error:%m");
        return NULL;
    }
    
    // don't need free key, it just free ht.
    ht->key = (char *)(ht + 1);
    memcpy(ht->key, key, key_len);
    ht->key_len = key_len;
    ht->value = value;
    ht->hash_code = hashcode_create(key, key_len);
    hashmap_link(map, ht);
    
    //printf("key:%s, val:%s, klen:%d\n", key, value, key_len);
    
    return (ht);
}
Example #2
0
int hashmap_insert(struct hashmap *map, struct hash_node *node, void *key)
{
	size_t slot;
	node->hash = map->hash(key);
	slot = node->hash & (map->len - 1);
	node->next = map->table[slot];
	map->table[slot] = node;
	map->count++;

	if (map->count > map->len * 3)
		hashmap_grow(map);
	return 0;
}