Back to index

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

Go to the source code of this file.

Defines

#define NBUCKETS(ht)   (1 << (JSJ_HASH_BITS - (ht)->shift))
#define MINBUCKETSLOG2   4
#define MINBUCKETS   (1 << MINBUCKETSLOG2)
#define OVERLOADED(n)   ((n) - ((n) >> 3))
#define UNDERLOADED(n)   (((n) > MINBUCKETS) ? ((n) >> 2) : 0)
#define GOLDEN_RATIO   0x9E3779B9U

Functions

static voidDefaultAllocTable (void *pool, size_t size)
static void DefaultFreeTable (void *pool, void *item)
static JSJHashEntryDefaultAllocEntry (void *pool, const void *key)
static void DefaultFreeEntry (void *pool, JSJHashEntry *he, JSUintn flag)
 JSJ_NewHashTable (JSUint32 n, JSJHashFunction keyHash, JSJHashComparator keyCompare, JSJHashComparator valueCompare, JSJHashAllocOps *allocOps, void *allocPriv)
 JSJ_HashTableDestroy (JSJHashTable *ht)
 JSJ_HashTableRawLookup (JSJHashTable *ht, JSJHashNumber keyHash, const void *key, void *arg)
 JSJ_HashTableRawAdd (JSJHashTable *ht, JSJHashEntry **hep, JSJHashNumber keyHash, const void *key, void *value, void *arg)
 JSJ_HashTableAdd (JSJHashTable *ht, const void *key, void *value, void *arg)
 JSJ_HashTableRawRemove (JSJHashTable *ht, JSJHashEntry **hep, JSJHashEntry *he, void *arg)
 JSJ_HashTableRemove (JSJHashTable *ht, const void *key, void *arg)
 JSJ_HashTableLookup (JSJHashTable *ht, const void *key, void *arg)
 JSJ_HashTableEnumerateEntries (JSJHashTable *ht, JSJHashEnumerator f, void *arg)
 JSJ_HashTableDump (JSJHashTable *ht, JSJHashEnumerator dump, FILE *fp)
 JSJ_HashString (const void *key)
 JSJ_CompareStrings (const void *v1, const void *v2)
 JSJ_CompareValues (const void *v1, const void *v2)

Variables

static JSJHashAllocOps defaultHashAllocOps

Define Documentation

#define GOLDEN_RATIO   0x9E3779B9U

Definition at line 178 of file jsj_hash.c.

Definition at line 63 of file jsj_hash.c.

Definition at line 62 of file jsj_hash.c.

#define NBUCKETS (   ht)    (1 << (JSJ_HASH_BITS - (ht)->shift))

Definition at line 59 of file jsj_hash.c.

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

Definition at line 66 of file jsj_hash.c.

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

Definition at line 69 of file jsj_hash.c.


Function Documentation

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

Definition at line 87 of file jsj_hash.c.

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

Definition at line 75 of file jsj_hash.c.

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

Definition at line 93 of file jsj_hash.c.

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

Definition at line 81 of file jsj_hash.c.

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

Definition at line 479 of file jsj_hash.c.

{
    return strcmp(v1, v2) == 0;
}
JSJ_CompareValues ( const void v1,
const void v2 
)

Definition at line 485 of file jsj_hash.c.

{
    return v1 == v2;
}

Definition at line 467 of file jsj_hash.c.

{
    JSJHashNumber h;
    const unsigned char *s;

    h = 0;
    for (s = key; *s; s++)
        h = (h >> 28) ^ (h << 4) ^ *s;
    return h;
}
JSJ_HashTableAdd ( JSJHashTable ht,
const void key,
void value,
void arg 
)

Definition at line 269 of file jsj_hash.c.

{
    JSJHashNumber keyHash;
    JSJHashEntry *he, **hep;

    keyHash = (*ht->keyHash)(key, arg);
    hep = JSJ_HashTableRawLookup(ht, keyHash, key, arg);
    if ((he = *hep) != 0) {
        /* Hit; see if values match */
        if ((*ht->valueCompare)(he->value, value, arg)) {
            /* 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 JSJ_HashTableRawAdd(ht, hep, keyHash, key, value, arg);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 151 of file jsj_hash.c.

{
    JSUint32 i, n;
    JSJHashEntry *he, *next;
    JSJHashAllocOps *allocOps = ht->allocOps;
    void *allocPriv = ht->allocPriv;

    n = NBUCKETS(ht);
    for (i = 0; i < n; i++) {
        for (he = ht->buckets[i]; he; he = next) {
            next = 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:

JSJ_HashTableDump ( JSJHashTable ht,
JSJHashEnumerator  dump,
FILE fp 
)

Definition at line 455 of file jsj_hash.c.

{
    int count;

    count = JSJ_HashTableEnumerateEntries(ht, dump, fp);
#ifdef HASHMETER
    JSJ_HashTableDumpMeter(ht, dump, fp);
#endif
    return count;
}

Here is the call graph for this function:

JSJ_HashTableEnumerateEntries ( JSJHashTable ht,
JSJHashEnumerator  f,
void arg 
)

Definition at line 368 of file jsj_hash.c.

{
    JSJHashEntry *he, **hep;
    JSUint32 i, nbuckets;
    int rv, n = 0;
    JSJHashEntry *todo = 0;

    nbuckets = NBUCKETS(ht);
    for (i = 0; i < nbuckets; i++) {
        hep = &ht->buckets[i];
        while ((he = *hep) != 0) {
            rv = (*f)(he, n, arg);
            n++;
            if (rv & (HT_ENUMERATE_REMOVE | HT_ENUMERATE_UNHASH)) {
                *hep = he->next;
                if (rv & HT_ENUMERATE_REMOVE) {
                    he->next = todo;
                    todo = he;
                }
            } else {
                hep = &he->next;
            }
            if (rv & HT_ENUMERATE_STOP) {
                goto out;
            }
        }
    }

out:
    hep = &todo;
    while ((he = *hep) != 0) {
        JSJ_HashTableRawRemove(ht, hep, he, arg);
    }
    return n;
}

Here is the call graph for this function:

Here is the caller graph for this function:

JSJ_HashTableLookup ( JSJHashTable ht,
const void key,
void arg 
)

Definition at line 349 of file jsj_hash.c.

{
    JSJHashNumber keyHash;
    JSJHashEntry *he, **hep;

    keyHash = (*ht->keyHash)(key, arg);
    hep = JSJ_HashTableRawLookup(ht, keyHash, key, arg);
    if ((he = *hep) != 0) {
        return he->value;
    }
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

JSJ_HashTableRawAdd ( JSJHashTable ht,
JSJHashEntry **  hep,
JSJHashNumber  keyHash,
const void key,
void value,
void arg 
)

Definition at line 211 of file jsj_hash.c.

{
    JSUint32 i, n;
    JSJHashEntry *he, *next, **oldbuckets;
    JSUint32 nb;

    /* Grow the table if it is overloaded */
    n = NBUCKETS(ht);
    if (ht->nentries >= OVERLOADED(n)) {
#ifdef HASHMETER
        ht->ngrows++;
#endif
        ht->shift--;
        oldbuckets = ht->buckets;
#if (defined(XP_WIN) || defined(XP_OS2)) && !defined(_WIN32)
        if (2 * n > 16000)
            return 0;
#endif  /* WIN16 */
        nb = 2 * n * sizeof(JSJHashEntry *);
        ht->buckets = (*ht->allocOps->allocTable)(ht->allocPriv, nb);
        if (!ht->buckets) {
            ht->buckets = oldbuckets;
            return 0;
        }
        memset(ht->buckets, 0, nb);

        for (i = 0; i < n; i++) {
            for (he = oldbuckets[i]; he; he = next) {
                next = he->next;
                hep = JSJ_HashTableRawLookup(ht, he->keyHash, he->key, arg);
                JS_ASSERT(*hep == 0);
                he->next = 0;
                *hep = he;
            }
        }
#ifdef DEBUG
        memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
#endif
        (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
        hep = JSJ_HashTableRawLookup(ht, keyHash, key, arg);
    }

    /* Make a new key value entry */
    he = (*ht->allocOps->allocEntry)(ht->allocPriv, key);
    if (!he)
    return 0;
    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:

JSJ_HashTableRawLookup ( JSJHashTable ht,
JSJHashNumber  keyHash,
const void key,
void arg 
)

Definition at line 181 of file jsj_hash.c.

{
    JSJHashEntry *he, **hep, **hep0;
    JSJHashNumber h;

#ifdef HASHMETER
    ht->nlookups++;
#endif
    h = keyHash * GOLDEN_RATIO;
    h >>= ht->shift;
    hep = hep0 = &ht->buckets[h];
    while ((he = *hep) != 0) {
        if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key, arg)) {
            /* 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:

JSJ_HashTableRawRemove ( JSJHashTable ht,
JSJHashEntry **  hep,
JSJHashEntry he,
void arg 
)

Definition at line 291 of file jsj_hash.c.

{
    JSUint32 i, n;
    JSJHashEntry *next, **oldbuckets;
    JSUint32 nb;

    *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)) {
#ifdef HASHMETER
        ht->nshrinks++;
#endif
        ht->shift++;
        oldbuckets = ht->buckets;
        nb = n * sizeof(JSJHashEntry*) / 2;
        ht->buckets = (*ht->allocOps->allocTable)(ht->allocPriv, nb);
        if (!ht->buckets) {
            ht->buckets = oldbuckets;
            return;
        }
        memset(ht->buckets, 0, nb);

        for (i = 0; i < n; i++) {
            for (he = oldbuckets[i]; he; he = next) {
                next = he->next;
                hep = JSJ_HashTableRawLookup(ht, he->keyHash, he->key, arg);
                JS_ASSERT(*hep == 0);
                he->next = 0;
                *hep = he;
            }
        }
#ifdef DEBUG
        memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
#endif
        (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

JSJ_HashTableRemove ( JSJHashTable ht,
const void key,
void arg 
)

Definition at line 333 of file jsj_hash.c.

{
    JSJHashNumber keyHash;
    JSJHashEntry *he, **hep;

    keyHash = (*ht->keyHash)(key, arg);
    hep = JSJ_HashTableRawLookup(ht, keyHash, key, arg);
    if ((he = *hep) == 0)
        return JS_FALSE;

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

Here is the call graph for this function:

Here is the caller graph for this function:

JSJ_NewHashTable ( JSUint32  n,
JSJHashFunction  keyHash,
JSJHashComparator  keyCompare,
JSJHashComparator  valueCompare,
JSJHashAllocOps allocOps,
void allocPriv 
)

Definition at line 105 of file jsj_hash.c.

{
    JSJHashTable *ht;
    JSUint32 nb;

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

    if (!allocOps) allocOps = &defaultHashAllocOps;

    ht = (*allocOps->allocTable)(allocPriv, sizeof *ht);
    if (!ht)
    return 0;
    memset(ht, 0, sizeof *ht);
    ht->shift = JSJ_HASH_BITS - n;
    n = 1 << n;
#if (defined(XP_WIN) || defined(XP_OS2)) && !defined(_WIN32)
    if (n > 16000) {
        (*allocOps->freeTable)(allocPriv, ht);
        return 0;
    }
#endif  /* WIN16 */
    nb = n * sizeof(JSJHashEntry *);
    ht->buckets = (*allocOps->allocTable)(allocPriv, nb);
    if (!ht->buckets) {
        (*allocOps->freeTable)(allocPriv, ht);
        return 0;
    }
    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:


Variable Documentation

Initial value:

Definition at line 99 of file jsj_hash.c.