bool hash_table_insert(struct hash_table *m, void *key, void *value)
{
    if (!m) { return false; }
    
    const fsize_t current_load = hash_table_get_current_load(m);
    
    if (current_load > m->load_factor) {
        hash_table_rehash(m);
    }
    
    struct slist *bucket = get_map_bucket(m, key, true);
    if (!bucket) { return false; }
    
    struct slist_node *found = slist_find(bucket, key);
    if (found) {
        return false;
    }

    struct gcl_pair *node = create_map_node(key, value);
    if (node) {
        slist_insert(bucket, node);
        ++m->count;
    }
    
    return node != NULL;
}
Exemple #2
0
static int hash_table_resize(struct hash_table_t *ht)
{
    struct hash_bucket_t **tmp;
    unsigned int htable_new_size = ht->table_size << 1;

    if (htable_new_size != 0) { //Prevent overflow.
        tmp = lkmalloc(htable_new_size * sizeof(struct hash_bucket_t *)); 
        if (!tmp) {
            printk(KERN_ERR "No more memory in %s", __FUNCTION__);
            return 0;
        }
        lkmfree(ht->buckets);
        ht->buckets = tmp;
        ht->table_size = htable_new_size;
        ht->hash_mask = ht->table_size - 1;
        hash_table_rehash(ht); 
    }

    return 1;
}