コード例 #1
0
void*
hashtable_remove(struct hashtable* h, char* key, unsigned int keylen)
{
    assert(h != NULL);

    /* Compute the hash to index into array. */
    int index = hash(key, keylen) % h->arraysize;
    struct list* chain = h->vals[index];
    assert(chain != NULL);
    
    /* Build a kv_pair object with the query key. */
    struct kv_pair query_item;
    query_item.key = key;
    query_item.keylen = keylen;

    struct kv_pair* removed
        = (struct kv_pair*)list_remove(chain, &query_item, &key_comparator);
    if (removed == NULL) {
        /* Key does not exist. */
        return NULL;
    }

    /* Key value pair removed. */
    --h->size;
    if (h->arraysize > h->init_size &&
        compute_load(h->size - 1, h->arraysize) < h->load_factor) {
        /* Half the size of array. */
        shrink(h);
    }
    void* res = removed->val;
    free(removed);
    
    return res;
}
コード例 #2
0
int
hashtable_add(struct hashtable* h, char* key, unsigned int keylen, void* val)
{
    assert(h != NULL);
    /* If LOAD_FACTOR exceeded, double the size of array. */
    if (compute_load(h->size + 1, h->arraysize) >= h->load_factor) {
        grow(h);
    }
    /* Compute the hash to index into array. */
    int index = hash(key, keylen) % h->arraysize;
    struct list* chain = h->vals[index];
    assert(chain != NULL);
    
    /* Append the new item to end of chain. */
    struct kv_pair* new_item = (struct kv_pair*)malloc(sizeof(struct kv_pair));
    if (new_item == NULL) {
        return ENOMEM;
    }
    new_item->key = key;
    new_item->keylen = keylen;
    new_item->val = val;
    int err = list_push_back(chain, new_item);
    if (err == ENOMEM) {
        return ENOMEM;
    }

    ++h->size;

    return 0;
}
コード例 #3
0
ファイル: int.cpp プロジェクト: yunxiaoxiao110/haiku
static void
update_int_load(int i)
{
    if (!try_acquire_spinlock(&sVectors[i].load_lock))
        return;

    int32 oldLoad = sVectors[i].load;
    compute_load(sVectors[i].last_measure_time, sVectors[i].last_measure_active,
                 sVectors[i].load, system_time());

    if (oldLoad != sVectors[i].load)
        atomic_add(&sVectors[i].assigned_cpu->load, sVectors[i].load - oldLoad);

    release_spinlock(&sVectors[i].load_lock);
}