Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
jshash.c File Reference
#include "jsstddef.h"
#include <stdlib.h>
#include <string.h>
#include "jstypes.h"
#include "jsbit.h"
#include "jsutil.h"
#include "jshash.h"

Go to the source code of this file.

Defines

#define NBUCKETS(ht)   JS_BIT(JS_HASH_BITS - (ht)->shift)
#define MINBUCKETSLOG2   4
#define MINBUCKETS   JS_BIT(MINBUCKETSLOG2)
#define OVERLOADED(n)   ((n) - ((n) >> 3))
#define UNDERLOADED(n)   (((n) > MINBUCKETS) ? ((n) >> 2) : 0)
#define BUCKET_HEAD(ht, keyHash)   (&(ht)->buckets[((keyHash) * JS_GOLDEN_RATIO) >> (ht)->shift])

Functions

static voidDefaultAllocTable (void *pool, size_t size)
static void DefaultFreeTable (void *pool, void *item)
static JSHashEntryDefaultAllocEntry (void *pool, const void *key)
static void DefaultFreeEntry (void *pool, JSHashEntry *he, uintN flag)
 JS_NewHashTable (uint32 n, JSHashFunction keyHash, JSHashComparator keyCompare, JSHashComparator valueCompare, JSHashAllocOps *allocOps, void *allocPriv)
 JS_HashTableDestroy (JSHashTable *ht)
 JS_HashTableRawLookup (JSHashTable *ht, JSHashNumber keyHash, const void *key)
static JSBool Resize (JSHashTable *ht, uint32 newshift)
 JS_HashTableRawAdd (JSHashTable *ht, JSHashEntry **hep, JSHashNumber keyHash, const void *key, void *value)
 JS_HashTableAdd (JSHashTable *ht, const void *key, void *value)
 JS_HashTableRawRemove (JSHashTable *ht, JSHashEntry **hep, JSHashEntry *he)
 JS_HashTableRemove (JSHashTable *ht, const void *key)
 JS_HashTableLookup (JSHashTable *ht, const void *key)
 JS_HashTableEnumerateEntries (JSHashTable *ht, JSHashEnumerator f, void *arg)
 JS_HashTableDump (JSHashTable *ht, JSHashEnumerator dump, FILE *fp)
 JS_HashString (const void *key)
 JS_CompareValues (const void *v1, const void *v2)

Variables

static JSHashAllocOps defaultHashAllocOps

Define Documentation

#define BUCKET_HEAD (   ht,
  keyHash 
)    (&(ht)->buckets[((keyHash) * JS_GOLDEN_RATIO) >> (ht)->shift])

Definition at line 166 of file jshash.c.

Definition at line 56 of file jshash.c.

Definition at line 55 of file jshash.c.

#define NBUCKETS (   ht)    JS_BIT(JS_HASH_BITS - (ht)->shift)

Definition at line 52 of file jshash.c.

#define OVERLOADED (   n)    ((n) - ((n) >> 3))

Definition at line 59 of file jshash.c.

#define UNDERLOADED (   n)    (((n) > MINBUCKETS) ? ((n) >> 2) : 0)

Definition at line 62 of file jshash.c.


Function Documentation

static JSHashEntry* DefaultAllocEntry ( void pool,
const void key 
) [static]

Definition at line 80 of file jshash.c.

{
    return (JSHashEntry*) malloc(sizeof(JSHashEntry));
}
static void* DefaultAllocTable ( void pool,
size_t  size 
) [static]

Definition at line 68 of file jshash.c.

{
    return malloc(size);
}
static void DefaultFreeEntry ( void pool,
JSHashEntry he,
uintN  flag 
) [static]

Definition at line 86 of file jshash.c.

{
    if (flag == HT_FREE_ENTRY)
        free(he);
}
static void DefaultFreeTable ( void pool,
void item 
) [static]

Definition at line 74 of file jshash.c.

{
    free(item);
}
JS_CompareValues ( const void v1,
const void v2 
)

Definition at line 480 of file jshash.c.

{
    return v1 == v2;
}

Here is the caller graph for this function:

JS_HashString ( const void key)

Definition at line 468 of file jshash.c.

{
    JSHashNumber h;
    const unsigned char *s;

    h = 0;
    for (s = (const unsigned char *)key; *s; s++)
        h = (h >> (JS_HASH_BITS - 4)) ^ (h << 4) ^ *s;
    return h;
}

Here is the caller graph for this function:

JS_HashTableAdd ( JSHashTable ht,
const void key,
void value 
)

Definition at line 279 of file jshash.c.

{
    JSHashNumber keyHash;
    JSHashEntry *he, **hep;

    keyHash = ht->keyHash(key);
    hep = JS_HashTableRawLookup(ht, keyHash, key);
    if ((he = *hep) != NULL) {
        /* Hit; see if values match */
        if (ht->valueCompare(he->value, value)) {
            /* key,value pair is already present in table */
            return he;
        }
        if (he->value)
            ht->allocOps->freeEntry(ht->allocPriv, he, HT_FREE_VALUE);
        he->value = value;
        return he;
    }
    return JS_HashTableRawAdd(ht, hep, keyHash, key, value);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 138 of file jshash.c.

{
    uint32 i, n;
    JSHashEntry *he, **hep;
    JSHashAllocOps *allocOps = ht->allocOps;
    void *allocPriv = ht->allocPriv;

    n = NBUCKETS(ht);
    for (i = 0; i < n; i++) {
        hep = &ht->buckets[i];
        while ((he = *hep) != NULL) {
            *hep = he->next;
            allocOps->freeEntry(allocPriv, he, HT_FREE_ENTRY);
        }
    }
#ifdef DEBUG
    memset(ht->buckets, 0xDB, n * sizeof ht->buckets[0]);
#endif
    allocOps->freeTable(allocPriv, ht->buckets);
#ifdef DEBUG
    memset(ht, 0xDB, sizeof *ht);
#endif
    allocOps->freeTable(allocPriv, ht);
}

Here is the call graph for this function:

Here is the caller graph for this function:

JS_HashTableDump ( JSHashTable ht,
JSHashEnumerator  dump,
FILE fp 
)

Definition at line 456 of file jshash.c.

{
    int count;

    count = JS_HashTableEnumerateEntries(ht, dump, fp);
#ifdef HASHMETER
    JS_HashTableDumpMeter(ht, dump, fp);
#endif
    return count;
}

Here is the call graph for this function:

JS_HashTableEnumerateEntries ( JSHashTable ht,
JSHashEnumerator  f,
void arg 
)

Definition at line 354 of file jshash.c.

{
    JSHashEntry *he, **hep, **bucket;
    uint32 nlimit, n, nbuckets, newlog2;
    int rv;

    nlimit = ht->nentries;
    n = 0;
    for (bucket = ht->buckets; n != nlimit; ++bucket) {
        hep = bucket;
        while ((he = *hep) != NULL) {
            JS_ASSERT(n < nlimit);
            rv = f(he, n, arg);
            n++;
            if (rv & HT_ENUMERATE_REMOVE) {
                *hep = he->next;
                ht->allocOps->freeEntry(ht->allocPriv, he, HT_FREE_ENTRY);
                --ht->nentries;
            } else {
                hep = &he->next;
            }
            if (rv & HT_ENUMERATE_STOP) {
                goto out;
            }
        }
    }

out:
    /* Shrink table if removal of entries made it underloaded */
    if (ht->nentries != nlimit) {
        JS_ASSERT(ht->nentries < nlimit);
        nbuckets = NBUCKETS(ht);
        if (MINBUCKETS < nbuckets && ht->nentries < UNDERLOADED(nbuckets)) {
            newlog2 = JS_CeilingLog2(ht->nentries);
            if (newlog2 < MINBUCKETSLOG2)
                newlog2 = MINBUCKETSLOG2;

            /*  Check that we really shrink the table. */
            JS_ASSERT(JS_HASH_BITS - ht->shift > newlog2);
            Resize(ht, JS_HASH_BITS - newlog2);
        }
    }
    return (int)n;
}

Here is the call graph for this function:

Here is the caller graph for this function:

JS_HashTableLookup ( JSHashTable ht,
const void key 
)

Definition at line 335 of file jshash.c.

{
    JSHashNumber keyHash;
    JSHashEntry *he, **hep;

    keyHash = ht->keyHash(key);
    hep = JS_HashTableRawLookup(ht, keyHash, key);
    if ((he = *hep) != NULL) {
        return he->value;
    }
    return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

JS_HashTableRawAdd ( JSHashTable ht,
JSHashEntry **  hep,
JSHashNumber  keyHash,
const void key,
void value 
)

Definition at line 248 of file jshash.c.

{
    uint32 n;
    JSHashEntry *he;

    /* Grow the table if it is overloaded */
    n = NBUCKETS(ht);
    if (ht->nentries >= OVERLOADED(n)) {
        if (!Resize(ht, ht->shift - 1))
            return NULL;
#ifdef HASHMETER
        ht->ngrows++;
#endif
        hep = JS_HashTableRawLookup(ht, keyHash, key);
    }

    /* Make a new key value entry */
    he = ht->allocOps->allocEntry(ht->allocPriv, key);
    if (!he)
        return NULL;
    he->keyHash = keyHash;
    he->key = key;
    he->value = value;
    he->next = *hep;
    *hep = he;
    ht->nentries++;
    return he;
}

Here is the call graph for this function:

Here is the caller graph for this function:

JS_HashTableRawLookup ( JSHashTable ht,
JSHashNumber  keyHash,
const void key 
)

Definition at line 170 of file jshash.c.

{
    JSHashEntry *he, **hep, **hep0;

#ifdef HASHMETER
    ht->nlookups++;
#endif
    hep = hep0 = BUCKET_HEAD(ht, keyHash);
    while ((he = *hep) != NULL) {
        if (he->keyHash == keyHash && ht->keyCompare(key, he->key)) {
            /* Move to front of chain if not already there */
            if (hep != hep0) {
                *hep = he->next;
                he->next = *hep0;
                *hep0 = he;
            }
            return hep0;
        }
        hep = &he->next;
#ifdef HASHMETER
        ht->nsteps++;
#endif
    }
    return hep;
}

Here is the caller graph for this function:

JS_HashTableRawRemove ( JSHashTable ht,
JSHashEntry **  hep,
JSHashEntry he 
)

Definition at line 301 of file jshash.c.

{
    uint32 n;

    *hep = he->next;
    ht->allocOps->freeEntry(ht->allocPriv, he, HT_FREE_ENTRY);

    /* Shrink table if it's underloaded */
    n = NBUCKETS(ht);
    if (--ht->nentries < UNDERLOADED(n)) {
        Resize(ht, ht->shift + 1);
#ifdef HASHMETER
        ht->nshrinks++;
#endif
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

JS_HashTableRemove ( JSHashTable ht,
const void key 
)

Definition at line 319 of file jshash.c.

{
    JSHashNumber keyHash;
    JSHashEntry *he, **hep;

    keyHash = ht->keyHash(key);
    hep = JS_HashTableRawLookup(ht, keyHash, key);
    if ((he = *hep) == NULL)
        return JS_FALSE;

    /* Hit; remove element */
    JS_HashTableRawRemove(ht, hep, he);
    return JS_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

JS_NewHashTable ( uint32  n,
JSHashFunction  keyHash,
JSHashComparator  keyCompare,
JSHashComparator  valueCompare,
JSHashAllocOps allocOps,
void allocPriv 
)

Definition at line 98 of file jshash.c.

{
    JSHashTable *ht;
    size_t nb;

    if (n <= MINBUCKETS) {
        n = MINBUCKETSLOG2;
    } else {
        n = JS_CeilingLog2(n);
        if ((int32)n < 0)
            return NULL;
    }

    if (!allocOps) allocOps = &defaultHashAllocOps;

    ht = (JSHashTable*) allocOps->allocTable(allocPriv, sizeof *ht);
    if (!ht)
        return NULL;
    memset(ht, 0, sizeof *ht);
    ht->shift = JS_HASH_BITS - n;
    n = JS_BIT(n);
    nb = n * sizeof(JSHashEntry *);
    ht->buckets = (JSHashEntry**) allocOps->allocTable(allocPriv, nb);
    if (!ht->buckets) {
        allocOps->freeTable(allocPriv, ht);
        return NULL;
    }
    memset(ht->buckets, 0, nb);

    ht->keyHash = keyHash;
    ht->keyCompare = keyCompare;
    ht->valueCompare = valueCompare;
    ht->allocOps = allocOps;
    ht->allocPriv = allocPriv;
    return ht;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static JSBool Resize ( JSHashTable ht,
uint32  newshift 
) [static]

Definition at line 197 of file jshash.c.

{
    size_t nb, nentries, i;
    JSHashEntry **oldbuckets, *he, *next, **hep;
#ifdef DEBUG
    size_t nold = NBUCKETS(ht);
#endif

    JS_ASSERT(newshift < JS_HASH_BITS);

    nb = (size_t)1 << (JS_HASH_BITS - newshift);

    /* Integer overflow protection. */
    if (nb > (size_t)-1 / sizeof(JSHashEntry*))
        return JS_FALSE;
    nb *= sizeof(JSHashEntry*);

    oldbuckets = ht->buckets;
    ht->buckets = (JSHashEntry**)ht->allocOps->allocTable(ht->allocPriv, nb);
    if (!ht->buckets) {
        ht->buckets = oldbuckets;
        return JS_FALSE;
    }
    memset(ht->buckets, 0, nb);

    ht->shift = newshift;
    nentries = ht->nentries;

    for (i = 0; nentries != 0; i++) {
        for (he = oldbuckets[i]; he; he = next) {
            JS_ASSERT(nentries != 0);
            --nentries;
            next = he->next;
            hep = BUCKET_HEAD(ht, he->keyHash);

            /*
             * Since he comes from the old table, it must be unique and we
             * simply add it to the head of bucket chain without chain lookup.
             */
            he->next = *hep;
            *hep = he;
        }
    }
#ifdef DEBUG
    memset(oldbuckets, 0xDB, nold * sizeof oldbuckets[0]);
#endif
    ht->allocOps->freeTable(ht->allocPriv, oldbuckets);
    return JS_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Initial value:

Definition at line 92 of file jshash.c.