зеркало из https://github.com/mozilla/pjs.git
Add pldhash.[ch], sed-translated from js/src/jsdhash.[ch].
This commit is contained in:
Родитель
c1a33908b2
Коммит
6bcb3c764d
|
@ -32,6 +32,7 @@ LIBRARY_NAME = xpcomds_s
|
|||
|
||||
|
||||
CSRCS = \
|
||||
pldhash.c \
|
||||
plvector.c \
|
||||
$(NULL)
|
||||
|
||||
|
@ -109,6 +110,7 @@ EXPORTS = \
|
|||
nsVoidArray.h \
|
||||
nsVoidBTree.h \
|
||||
nsXPIDLString.h \
|
||||
pldhash.h \
|
||||
plvector.h \
|
||||
nsTextFormatter.h \
|
||||
$(NULL)
|
||||
|
|
|
@ -64,6 +64,7 @@ EXPORTS = \
|
|||
nsVoidArray.h \
|
||||
nsVoidBTree.h \
|
||||
nsXPIDLString.h \
|
||||
pldhash.h \
|
||||
plvector.h \
|
||||
$(NULL)
|
||||
|
||||
|
@ -131,6 +132,7 @@ CPP_OBJS = \
|
|||
.\$(OBJDIR)\nsVoidArray.obj \
|
||||
.\$(OBJDIR)\nsVoidBTree.obj \
|
||||
.\$(OBJDIR)\nsXPIDLString.obj \
|
||||
.\$(OBJDIR)\pldhash.obj \
|
||||
.\$(OBJDIR)\plvector.obj \
|
||||
$(NULL)
|
||||
|
||||
|
|
|
@ -0,0 +1,508 @@
|
|||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is Mozilla JavaScript code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1999,2000 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the
|
||||
* terms of the GNU Public License (the "GPL"), in which case the
|
||||
* provisions of the GPL are applicable instead of those above.
|
||||
* If you wish to allow use of your version of this file only
|
||||
* under the terms of the GPL and not to allow others to use your
|
||||
* version of this file under the NPL, indicate your decision by
|
||||
* deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this
|
||||
* file under either the NPL or the GPL.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Double hashing implementation.
|
||||
* GENERATED BY js/src/plify_jsdhash.sed -- DO NOT EDIT!!!
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "prbit.h"
|
||||
#include "pldhash.h"
|
||||
#include "prlog.h" /* for PR_ASSERT */
|
||||
|
||||
#ifdef PL_DHASHMETER
|
||||
# define METER(x) x
|
||||
#else
|
||||
# define METER(x) /* nothing */
|
||||
#endif
|
||||
|
||||
PR_IMPLEMENT(void *)
|
||||
PL_DHashAllocTable(PLDHashTable *table, PRUint32 nbytes)
|
||||
{
|
||||
return malloc(nbytes);
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(void)
|
||||
PL_DHashFreeTable(PLDHashTable *table, void *ptr)
|
||||
{
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(PLDHashNumber)
|
||||
PL_DHashStringKey(PLDHashTable *table, const void *key)
|
||||
{
|
||||
const char *s;
|
||||
size_t n, m;
|
||||
PLDHashNumber h;
|
||||
|
||||
s = key;
|
||||
n = strlen(s);
|
||||
h = 0;
|
||||
if (n < 16) {
|
||||
/* Hash every char in a short string. */
|
||||
for (; n; s++, n--)
|
||||
h = (h >> 28) ^ (h << 4) ^ *s;
|
||||
} else {
|
||||
/* Sample a la java.lang.String.hash(). */
|
||||
for (m = n / 8; n >= m; s += m, n -= m)
|
||||
h = (h >> 28) ^ (h << 4) ^ *s;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(const void *)
|
||||
PL_DHashGetKeyStub(PLDHashTable *table, PLDHashEntryHdr *entry)
|
||||
{
|
||||
PLDHashEntryStub *stub = (PLDHashEntryStub *)entry;
|
||||
|
||||
return stub->key;
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(PLDHashNumber)
|
||||
PL_DHashVoidPtrKeyStub(PLDHashTable *table, const void *key)
|
||||
{
|
||||
return (PLDHashNumber)key >> 2;
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(PRBool)
|
||||
PL_DHashMatchEntryStub(PLDHashTable *table,
|
||||
const PLDHashEntryHdr *entry,
|
||||
const void *key)
|
||||
{
|
||||
PLDHashEntryStub *stub = (PLDHashEntryStub *)entry;
|
||||
|
||||
return stub->key == key;
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(void)
|
||||
PL_DHashMoveEntryStub(PLDHashTable *table,
|
||||
const PLDHashEntryHdr *from,
|
||||
PLDHashEntryHdr *to)
|
||||
{
|
||||
memcpy(to, from, table->entrySize);
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(void)
|
||||
PL_DHashClearEntryStub(PLDHashTable *table, PLDHashEntryHdr *entry)
|
||||
{
|
||||
memset(entry, 0, table->entrySize);
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(void)
|
||||
PL_DHashFinalizeStub(PLDHashTable *table)
|
||||
{
|
||||
}
|
||||
|
||||
static PLDHashTableOps stub_ops = {
|
||||
PL_DHashAllocTable,
|
||||
PL_DHashFreeTable,
|
||||
PL_DHashGetKeyStub,
|
||||
PL_DHashVoidPtrKeyStub,
|
||||
PL_DHashMatchEntryStub,
|
||||
PL_DHashMoveEntryStub,
|
||||
PL_DHashClearEntryStub,
|
||||
PL_DHashFinalizeStub
|
||||
};
|
||||
|
||||
PR_IMPLEMENT(PLDHashTableOps *)
|
||||
PL_DHashGetStubOps(void)
|
||||
{
|
||||
return &stub_ops;
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(PLDHashTable *)
|
||||
PR_NewDHashTable(PLDHashTableOps *ops, void *data, PRUint32 entrySize,
|
||||
PRUint32 capacity)
|
||||
{
|
||||
PLDHashTable *table;
|
||||
|
||||
table = (PLDHashTable *) malloc(sizeof *table);
|
||||
if (!table)
|
||||
return NULL;
|
||||
if (!PL_DHashTableInit(table, ops, data, entrySize, capacity)) {
|
||||
free(table);
|
||||
return NULL;
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(void)
|
||||
PL_DHashTableDestroy(PLDHashTable *table)
|
||||
{
|
||||
PL_DHashTableFinish(table);
|
||||
free(table);
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(PRBool)
|
||||
PL_DHashTableInit(PLDHashTable *table, PLDHashTableOps *ops, void *data,
|
||||
PRUint32 entrySize, PRUint32 capacity)
|
||||
{
|
||||
int log2;
|
||||
PRUint32 nbytes;
|
||||
|
||||
table->ops = ops;
|
||||
table->data = data;
|
||||
if (capacity < PL_DHASH_MIN_SIZE)
|
||||
capacity = PL_DHASH_MIN_SIZE;
|
||||
log2 = PR_CeilingLog2(capacity);
|
||||
table->hashShift = PL_DHASH_BITS - log2;
|
||||
table->sizeLog2 = log2;
|
||||
table->sizeMask = PR_BITMASK(table->sizeLog2);
|
||||
table->entrySize = entrySize;
|
||||
table->entryCount = table->removedCount = 0;
|
||||
nbytes = capacity * entrySize;
|
||||
table->entryStore = ops->allocTable(table, nbytes);
|
||||
if (!table->entryStore)
|
||||
return PR_FALSE;
|
||||
memset(table->entryStore, 0, nbytes);
|
||||
METER(memset(&table->stats, 0, sizeof table->stats));
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(void)
|
||||
PL_DHashTableFinish(PLDHashTable *table)
|
||||
{
|
||||
table->ops->finalize(table);
|
||||
table->ops->freeTable(table, table->entryStore);
|
||||
}
|
||||
|
||||
/*
|
||||
* Double hashing needs the second hash code to be relatively prime to table
|
||||
* size, so we simply make hash2 odd.
|
||||
*/
|
||||
#define HASH1(hash0, shift) ((hash0) >> (shift))
|
||||
#define HASH2(hash0,log2,shift) ((((hash0) << (log2)) >> (shift)) | 1)
|
||||
|
||||
/* Reserve keyHash 0 for free entries and 1 for removed-entry sentinels. */
|
||||
#define MARK_ENTRY_FREE(entry) ((entry)->keyHash = 0)
|
||||
#define MARK_ENTRY_REMOVED(entry) ((entry)->keyHash = 1)
|
||||
#define ENTRY_IS_LIVE(entry) ((entry)->keyHash >= 2)
|
||||
#define ENSURE_LIVE_KEYHASH(hash0) if (hash0 < 2) hash0 -= 2; else (void)0
|
||||
|
||||
/* Compute the address of the indexed entry in table. */
|
||||
#define ADDRESS_ENTRY(table, index) \
|
||||
((PLDHashEntryHdr *)((table)->entryStore + (index) * (table)->entrySize))
|
||||
|
||||
static PLDHashEntryHdr *
|
||||
SearchTable(PLDHashTable *table, const void *key, PLDHashNumber keyHash)
|
||||
{
|
||||
PLDHashNumber hash1, hash2;
|
||||
int hashShift;
|
||||
PLDHashEntryHdr *entry;
|
||||
PLDHashMatchEntry matchEntry;
|
||||
|
||||
METER(table->stats.searches++);
|
||||
|
||||
/* Compute the primary hash address. */
|
||||
hashShift = table->hashShift;
|
||||
hash1 = HASH1(keyHash, hashShift);
|
||||
entry = ADDRESS_ENTRY(table, hash1);
|
||||
|
||||
/* Miss: return space for a new entry. */
|
||||
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
||||
METER(table->stats.misses++);
|
||||
return entry;
|
||||
}
|
||||
|
||||
/* Hit: return entry. */
|
||||
matchEntry = table->ops->matchEntry;
|
||||
if (entry->keyHash == keyHash && matchEntry(table, entry, key)) {
|
||||
METER(table->stats.hits++);
|
||||
return entry;
|
||||
}
|
||||
|
||||
/* Collision: double hash. */
|
||||
hash2 = HASH2(keyHash, table->sizeLog2, hashShift);
|
||||
do {
|
||||
METER(table->stats.steps++);
|
||||
hash1 -= hash2;
|
||||
hash1 &= table->sizeMask;
|
||||
entry = ADDRESS_ENTRY(table, hash1);
|
||||
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
||||
METER(table->stats.misses++);
|
||||
return entry;
|
||||
}
|
||||
} while (entry->keyHash != keyHash || !matchEntry(table, entry, key));
|
||||
|
||||
METER(table->stats.hits++);
|
||||
return entry;
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(PLDHashEntryHdr *)
|
||||
PL_DHashTableOperate(PLDHashTable *table, const void *key, PLDHashOperator op)
|
||||
{
|
||||
int change;
|
||||
PLDHashNumber keyHash;
|
||||
PRUint32 i, size, capacity, nbytes, entrySize;
|
||||
PLDHashEntryHdr *entry, *oldEntry, *newEntry;
|
||||
char *entryStore, *newEntryStore, *entryAddr;
|
||||
PLDHashGetKey getKey;
|
||||
PLDHashMoveEntry moveEntry;
|
||||
|
||||
/* Usually we don't grow or shrink the table. */
|
||||
change = 0;
|
||||
|
||||
/* Avoid 0 and 1 hash codes, they indicate free and deleted entries. */
|
||||
keyHash = table->ops->hashKey(table, key);
|
||||
ENSURE_LIVE_KEYHASH(keyHash);
|
||||
keyHash *= PL_DHASH_GOLDEN_RATIO;
|
||||
entry = SearchTable(table, key, keyHash);
|
||||
|
||||
switch (op) {
|
||||
case PL_DHASH_LOOKUP:
|
||||
METER(table->stats.lookups++);
|
||||
break;
|
||||
|
||||
case PL_DHASH_ADD:
|
||||
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
||||
/* Initialize the entry, indicating that it's no longer free. */
|
||||
METER(table->stats.addMisses++);
|
||||
entry->keyHash = keyHash;
|
||||
table->entryCount++;
|
||||
|
||||
/* If alpha is >= .75, set change to trigger table growth below. */
|
||||
size = PR_BIT(table->sizeLog2);
|
||||
if (table->entryCount + table->removedCount >= size - (size >> 2)) {
|
||||
METER(table->stats.grows++);
|
||||
change = 1;
|
||||
capacity = size << 1;
|
||||
}
|
||||
}
|
||||
METER(else table->stats.addHits++);
|
||||
break;
|
||||
|
||||
case PL_DHASH_REMOVE:
|
||||
if (PL_DHASH_ENTRY_IS_BUSY(entry)) {
|
||||
/* Clear this entry and mark it as "removed". */
|
||||
METER(table->stats.removeHits++);
|
||||
PL_DHashTableRawRemove(table, entry);
|
||||
|
||||
/* Shrink if alpha is <= .25 and table isn't too small already. */
|
||||
size = PR_BIT(table->sizeLog2);
|
||||
if (size > PL_DHASH_MIN_SIZE && table->entryCount <= size >> 2) {
|
||||
METER(table->stats.shrinks++);
|
||||
change = -1;
|
||||
capacity = size >> 1;
|
||||
}
|
||||
}
|
||||
METER(else table->stats.removeMisses++);
|
||||
entry = NULL;
|
||||
break;
|
||||
|
||||
default:
|
||||
PR_ASSERT(0);
|
||||
}
|
||||
|
||||
if (change) {
|
||||
entrySize = table->entrySize;
|
||||
nbytes = capacity * entrySize;
|
||||
newEntryStore = table->ops->allocTable(table, nbytes);
|
||||
if (!newEntryStore) {
|
||||
/* If we just grabbed the last free entry, undo and fail hard. */
|
||||
if (op == PL_DHASH_ADD &&
|
||||
table->entryCount + table->removedCount == size) {
|
||||
METER(table->stats.addFailures++);
|
||||
MARK_ENTRY_FREE(entry);
|
||||
table->entryCount--;
|
||||
entry = NULL;
|
||||
}
|
||||
} else {
|
||||
memset(newEntryStore, 0, nbytes);
|
||||
entryStore = table->entryStore;
|
||||
table->entryStore = newEntryStore;
|
||||
|
||||
table->sizeLog2 += change;
|
||||
table->sizeMask = PR_BITMASK(table->sizeLog2);
|
||||
table->hashShift = PL_DHASH_BITS - table->sizeLog2;
|
||||
table->removedCount = 0;
|
||||
|
||||
getKey = table->ops->getKey;
|
||||
moveEntry = table->ops->moveEntry;
|
||||
entryAddr = entryStore;
|
||||
for (i = 0; i < size; i++) {
|
||||
oldEntry = (PLDHashEntryHdr *)entryAddr;
|
||||
if (oldEntry != entry && ENTRY_IS_LIVE(oldEntry)) {
|
||||
newEntry = SearchTable(table, getKey(table,oldEntry),
|
||||
oldEntry->keyHash);
|
||||
PR_ASSERT(PL_DHASH_ENTRY_IS_FREE(newEntry));
|
||||
moveEntry(table, oldEntry, newEntry);
|
||||
newEntry->keyHash = oldEntry->keyHash;
|
||||
}
|
||||
entryAddr += entrySize;
|
||||
}
|
||||
table->ops->freeTable(table, entryStore);
|
||||
|
||||
if (op == PL_DHASH_ADD) {
|
||||
entry = SearchTable(table, key, keyHash);
|
||||
PR_ASSERT(PL_DHASH_ENTRY_IS_FREE(entry));
|
||||
entry->keyHash = keyHash;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(void)
|
||||
PL_DHashTableRawRemove(PLDHashTable *table, PLDHashEntryHdr *entry)
|
||||
{
|
||||
table->ops->clearEntry(table, entry);
|
||||
MARK_ENTRY_REMOVED(entry);
|
||||
table->removedCount++;
|
||||
table->entryCount--;
|
||||
}
|
||||
|
||||
PR_IMPLEMENT(PRUint32)
|
||||
PL_DHashTableEnumerate(PLDHashTable *table, PLDHashEnumerator etor, void *arg)
|
||||
{
|
||||
char *entryAddr;
|
||||
PRUint32 i, j, n, entrySize;
|
||||
PLDHashEntryHdr *entry;
|
||||
PLDHashOperator op;
|
||||
|
||||
entryAddr = table->entryStore;
|
||||
entrySize = table->entrySize;
|
||||
n = PR_BIT(table->sizeLog2);
|
||||
for (i = j = 0; i < n; i++) {
|
||||
entry = (PLDHashEntryHdr *)entryAddr;
|
||||
if (ENTRY_IS_LIVE(entry)) {
|
||||
op = etor(table, entry, j++, arg);
|
||||
if (op & PL_DHASH_REMOVE) {
|
||||
METER(table->stats.removeEnums++);
|
||||
PL_DHashTableRawRemove(table, entry);
|
||||
}
|
||||
if (op & PL_DHASH_STOP)
|
||||
break;
|
||||
}
|
||||
entryAddr += entrySize;
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
#ifdef PL_DHASHMETER
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
PR_IMPLEMENT(void)
|
||||
PL_DHashTableDumpMeter(PLDHashTable *table, PLDHashEnumerator dump, FILE *fp)
|
||||
{
|
||||
char *entryAddr;
|
||||
PRUint32 entrySize, entryCount;
|
||||
PRUint32 i, tableSize, chainLen, maxChainLen, chainCount;
|
||||
PLDHashNumber hash1, hash2, saveHash1, maxChainHash1, maxChainHash2;
|
||||
double sqsum, mean, variance, sigma;
|
||||
PLDHashEntryHdr *entry, *probe;
|
||||
|
||||
entryAddr = table->entryStore;
|
||||
entrySize = table->entrySize;
|
||||
tableSize = PR_BIT(table->sizeLog2);
|
||||
chainCount = maxChainLen = 0;
|
||||
hash2 = 0;
|
||||
sqsum = 0;
|
||||
|
||||
for (i = 0; i < tableSize; i++) {
|
||||
entry = (PLDHashEntryHdr *)entryAddr;
|
||||
entryAddr += entrySize;
|
||||
if (!ENTRY_IS_LIVE(entry))
|
||||
continue;
|
||||
hash1 = saveHash1 = HASH1(entry->keyHash, table->hashShift);
|
||||
probe = ADDRESS_ENTRY(table, hash1);
|
||||
chainLen = 1;
|
||||
if (probe == entry) {
|
||||
/* Start of a (possibly unit-length) chain. */
|
||||
chainCount++;
|
||||
} else {
|
||||
hash2 = HASH2(entry->keyHash, table->sizeLog2, table->hashShift);
|
||||
do {
|
||||
chainLen++;
|
||||
hash1 -= hash2;
|
||||
hash1 &= table->sizeMask;
|
||||
probe = ADDRESS_ENTRY(table, hash1);
|
||||
} while (probe != entry);
|
||||
}
|
||||
sqsum += chainLen * chainLen;
|
||||
if (chainLen > maxChainLen) {
|
||||
maxChainLen = chainLen;
|
||||
maxChainHash1 = saveHash1;
|
||||
maxChainHash2 = hash2;
|
||||
}
|
||||
}
|
||||
|
||||
entryCount = table->entryCount;
|
||||
mean = (double)entryCount / chainCount;
|
||||
variance = chainCount * sqsum - entryCount * entryCount;
|
||||
if (variance < 0 || chainCount == 1)
|
||||
variance = 0;
|
||||
else
|
||||
variance /= chainCount * (chainCount - 1);
|
||||
sigma = sqrt(variance);
|
||||
|
||||
fprintf(fp, "Double hashing statistics:\n");
|
||||
fprintf(fp, " table size (in entries): %u\n", tableSize);
|
||||
fprintf(fp, " number of entries: %u\n", table->entryCount);
|
||||
fprintf(fp, " number of removed entries: %u\n", table->removedCount);
|
||||
fprintf(fp, " number of searches: %u\n", table->stats.searches);
|
||||
fprintf(fp, " number of hits: %u\n", table->stats.hits);
|
||||
fprintf(fp, " number of misses: %u\n", table->stats.misses);
|
||||
fprintf(fp, " mean steps per search: %g\n", (double)table->stats.steps
|
||||
/ table->stats.searches);
|
||||
fprintf(fp, " mean hash chain length: %g\n", mean);
|
||||
fprintf(fp, " standard deviation: %g\n", sigma);
|
||||
fprintf(fp, " maximum hash chain length: %u\n", maxChainLen);
|
||||
fprintf(fp, " number of lookups: %u\n", table->stats.lookups);
|
||||
fprintf(fp, " adds that made a new entry: %u\n", table->stats.addMisses);
|
||||
fprintf(fp, " adds that found an entry: %u\n", table->stats.addHits);
|
||||
fprintf(fp, " add failures: %u\n", table->stats.addFailures);
|
||||
fprintf(fp, " useful removes: %u\n", table->stats.removeHits);
|
||||
fprintf(fp, " useless removes: %u\n", table->stats.removeMisses);
|
||||
fprintf(fp, " removes while enumerating: %u\n", table->stats.removeEnums);
|
||||
fprintf(fp, " number of grows: %u\n", table->stats.grows);
|
||||
fprintf(fp, " number of shrinks: %u\n", table->stats.shrinks);
|
||||
|
||||
if (maxChainLen && hash2) {
|
||||
fputs("Maximum hash chain:\n", fp);
|
||||
hash1 = maxChainHash1;
|
||||
hash2 = maxChainHash2;
|
||||
entry = ADDRESS_ENTRY(table, hash1);
|
||||
i = 0;
|
||||
do {
|
||||
if (dump(table, entry, i++, fp) != PL_DHASH_NEXT)
|
||||
break;
|
||||
hash1 -= hash2;
|
||||
hash1 &= table->sizeMask;
|
||||
entry = ADDRESS_ENTRY(table, hash1);
|
||||
} while (PL_DHASH_ENTRY_IS_BUSY(entry));
|
||||
}
|
||||
}
|
||||
#endif /* PL_DHASHMETER */
|
|
@ -0,0 +1,382 @@
|
|||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is Mozilla JavaScript code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1999,2000 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the
|
||||
* terms of the GNU Public License (the "GPL"), in which case the
|
||||
* provisions of the GPL are applicable instead of those above.
|
||||
* If you wish to allow use of your version of this file only
|
||||
* under the terms of the GPL and not to allow others to use your
|
||||
* version of this file under the NPL, indicate your decision by
|
||||
* deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this
|
||||
* file under either the NPL or the GPL.
|
||||
*/
|
||||
|
||||
#ifndef pldhash_h___
|
||||
#define pldhash_h___
|
||||
/*
|
||||
* Double hashing, a la Knuth 6.
|
||||
* GENERATED BY js/src/plify_jsdhash.sed -- DO NOT EDIT!!!
|
||||
*/
|
||||
#include "prtypes.h"
|
||||
|
||||
PR_BEGIN_EXTERN_C
|
||||
|
||||
/* Minimum table size, or gross entry count (net is at most .75 loaded). */
|
||||
#ifndef PL_DHASH_MIN_SIZE
|
||||
#define PL_DHASH_MIN_SIZE 16
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Multiplicative hash uses an unsigned 32 bit integer and the golden ratio,
|
||||
* expressed as a fixed-point 32-bit fraction.
|
||||
*/
|
||||
#define PL_DHASH_BITS 32
|
||||
#define PL_DHASH_GOLDEN_RATIO 0x9E3779B9U
|
||||
|
||||
/* Primitive and forward-struct typedefs. */
|
||||
typedef PRUint32 PLDHashNumber;
|
||||
typedef struct PLDHashEntryHdr PLDHashEntryHdr;
|
||||
typedef struct PLDHashEntryStub PLDHashEntryStub;
|
||||
typedef struct PLDHashTable PLDHashTable;
|
||||
typedef struct PLDHashTableOps PLDHashTableOps;
|
||||
|
||||
/*
|
||||
* Table entry header structure.
|
||||
*
|
||||
* In order to allow in-line allocation of key and value, we do not declare
|
||||
* either here. Instead, the API uses const void *key as a formal parameter,
|
||||
* and asks each entry for its key when necessary via a getKey callback, used
|
||||
* when growing or shrinking the table. Other callback types are defined
|
||||
* below and grouped into the PLDHashTableOps structure, for single static
|
||||
* initialization per hash table sub-type.
|
||||
*
|
||||
* Each hash table sub-type should nest the PLDHashEntryHdr structure at the
|
||||
* front of its particular entry type. The keyHash member contains the result
|
||||
* of multiplying the hash code returned from the hashKey callback (see below)
|
||||
* by PL_DHASH_GOLDEN_RATIO. Its value is table size invariant. keyHash is
|
||||
* maintained automatically by PL_DHashTableOperate -- users should never set
|
||||
* it, and its only uses should be via the entry macros below.
|
||||
*/
|
||||
struct PLDHashEntryHdr {
|
||||
PLDHashNumber keyHash; /* every entry must begin like this */
|
||||
};
|
||||
|
||||
#define PL_DHASH_ENTRY_IS_FREE(entry) ((entry)->keyHash == 0)
|
||||
#define PL_DHASH_ENTRY_IS_BUSY(entry) (!PL_DHASH_ENTRY_IS_FREE(entry))
|
||||
|
||||
/*
|
||||
* A PLDHashTable is currently 8 words (without the PL_DHASHMETER overhead)
|
||||
* on most architectures, and may be allocated on the stack or within another
|
||||
* structure or class (see below for the Init and Finish functions to use).
|
||||
*/
|
||||
struct PLDHashTable {
|
||||
PLDHashTableOps *ops; /* virtual operations, see below */
|
||||
void *data; /* ops- and instance-specific data */
|
||||
PRInt16 hashShift; /* multiplicative hash shift */
|
||||
PRInt16 sizeLog2; /* log2(table size) */
|
||||
PRUint32 sizeMask; /* PR_BITMASK(log2(table size)) */
|
||||
PRUint32 entrySize; /* number of bytes in an entry */
|
||||
PRUint32 entryCount; /* number of entries in table */
|
||||
PRUint32 removedCount; /* removed entry sentinels in table */
|
||||
char *entryStore; /* entry storage */
|
||||
#ifdef PL_DHASHMETER
|
||||
struct PLDHashStats {
|
||||
PRUint32 searches; /* total number of table searches */
|
||||
PRUint32 steps; /* hash chain links traversed */
|
||||
PRUint32 hits; /* searches that found key */
|
||||
PRUint32 misses; /* searches that didn't find key */
|
||||
PRUint32 lookups; /* number of PL_DHASH_LOOKUPs */
|
||||
PRUint32 addMisses; /* adds that miss, and do work */
|
||||
PRUint32 addHits; /* adds that hit an existing entry */
|
||||
PRUint32 addFailures; /* out-of-memory during add growth */
|
||||
PRUint32 removeHits; /* removes that hit, and do work */
|
||||
PRUint32 removeMisses; /* useless removes that miss */
|
||||
PRUint32 removeEnums; /* removes done by Enumerate */
|
||||
PRUint32 grows; /* table expansions */
|
||||
PRUint32 shrinks; /* table contractions */
|
||||
} stats;
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifndef CRT_CALL
|
||||
#ifdef XP_OS2_VACPP
|
||||
#define CRT_CALL _Optlink
|
||||
#else
|
||||
#define CRT_CALL
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Table space at entryStore is allocated and freed using these callbacks.
|
||||
* The allocator should return null on error only (not if called with nbytes
|
||||
* equal to 0; but note that jsdhash.c code will never call with 0 nbytes).
|
||||
*/
|
||||
typedef void *
|
||||
(* CRT_CALL PLDHashAllocTable)(PLDHashTable *table, PRUint32 nbytes);
|
||||
|
||||
typedef void
|
||||
(* CRT_CALL PLDHashFreeTable) (PLDHashTable *table, void *ptr);
|
||||
|
||||
/*
|
||||
* When a table grows or shrinks, each entry is queried for its key using this
|
||||
* callback. NB: in that event, entry is not in table any longer; it's in the
|
||||
* old entryStore vector, which is due to be freed once all entries have been
|
||||
* moved via moveEntry callbacks.
|
||||
*/
|
||||
typedef const void *
|
||||
(* CRT_CALL PLDHashGetKey) (PLDHashTable *table, PLDHashEntryHdr *entry);
|
||||
|
||||
/*
|
||||
* Compute the hash code for a given key to be looked up, added, or removed
|
||||
* from table. A hash code may have any PLDHashNumber value.
|
||||
*/
|
||||
typedef PLDHashNumber
|
||||
(* CRT_CALL PLDHashHashKey) (PLDHashTable *table, const void *key);
|
||||
|
||||
/*
|
||||
* Compare the key identifying entry in table with the provided key parameter.
|
||||
* Return PR_TRUE if keys match, PR_FALSE otherwise.
|
||||
*/
|
||||
typedef PRBool
|
||||
(* CRT_CALL PLDHashMatchEntry)(PLDHashTable *table,
|
||||
const PLDHashEntryHdr *entry,
|
||||
const void *key);
|
||||
|
||||
/*
|
||||
* Copy the data starting at from to the new entry storage at to. Do not add
|
||||
* reference counts for any strong references in the entry, however, as this
|
||||
* is a "move" operation: the old entry storage at from will be freed without
|
||||
* any reference-decrementing callback shortly.
|
||||
*/
|
||||
typedef void
|
||||
(* CRT_CALL PLDHashMoveEntry)(PLDHashTable *table,
|
||||
const PLDHashEntryHdr *from,
|
||||
PLDHashEntryHdr *to);
|
||||
|
||||
/*
|
||||
* Clear the entry and drop any strong references it holds. This callback is
|
||||
* invoked during a PL_DHASH_REMOVE operation (see below for operation codes),
|
||||
* but only if the given key is found in the table.
|
||||
*/
|
||||
typedef void
|
||||
(* CRT_CALL PLDHashClearEntry)(PLDHashTable *table, PLDHashEntryHdr *entry);
|
||||
|
||||
/*
|
||||
* Called when a table (whether allocated dynamically by itself, or nested in
|
||||
* a larger structure, or allocated on the stack) is finished. This callback
|
||||
* allows table->ops-specific code to finalize table->data.
|
||||
*/
|
||||
typedef void
|
||||
(* CRT_CALL PLDHashFinalize) (PLDHashTable *table);
|
||||
|
||||
/* Finally, the "vtable" structure for PLDHashTable. */
|
||||
struct PLDHashTableOps {
|
||||
PLDHashAllocTable allocTable;
|
||||
PLDHashFreeTable freeTable;
|
||||
PLDHashGetKey getKey;
|
||||
PLDHashHashKey hashKey;
|
||||
PLDHashMatchEntry matchEntry;
|
||||
PLDHashMoveEntry moveEntry;
|
||||
PLDHashClearEntry clearEntry;
|
||||
PLDHashFinalize finalize;
|
||||
};
|
||||
|
||||
/*
|
||||
* Default implementations for the above ops.
|
||||
*/
|
||||
PR_EXTERN(void *)
|
||||
PL_DHashAllocTable(PLDHashTable *table, PRUint32 nbytes);
|
||||
|
||||
PR_EXTERN(void)
|
||||
PL_DHashFreeTable(PLDHashTable *table, void *ptr);
|
||||
|
||||
PR_EXTERN(PLDHashNumber)
|
||||
PL_DHashStringKey(PLDHashTable *table, const void *key);
|
||||
|
||||
/* A minimal entry contains a keyHash header and a void key pointer. */
|
||||
struct PLDHashEntryStub {
|
||||
PLDHashEntryHdr hdr;
|
||||
const void *key;
|
||||
};
|
||||
|
||||
PR_EXTERN(const void *)
|
||||
PL_DHashGetKeyStub(PLDHashTable *table, PLDHashEntryHdr *entry);
|
||||
|
||||
PR_EXTERN(PLDHashNumber)
|
||||
PL_DHashVoidPtrKeyStub(PLDHashTable *table, const void *key);
|
||||
|
||||
PR_EXTERN(PRBool)
|
||||
PL_DHashMatchEntryStub(PLDHashTable *table,
|
||||
const PLDHashEntryHdr *entry,
|
||||
const void *key);
|
||||
|
||||
PR_EXTERN(void)
|
||||
PL_DHashMoveEntryStub(PLDHashTable *table,
|
||||
const PLDHashEntryHdr *from,
|
||||
PLDHashEntryHdr *to);
|
||||
|
||||
PR_EXTERN(void)
|
||||
PL_DHashClearEntryStub(PLDHashTable *table, PLDHashEntryHdr *entry);
|
||||
|
||||
PR_EXTERN(void)
|
||||
PL_DHashFinalizeStub(PLDHashTable *table);
|
||||
|
||||
/*
|
||||
* If you use PLDHashEntryStub or a subclass of it as your entry struct, and
|
||||
* if your entries move via memcpy and clear via memset(0), you can use these
|
||||
* stub operations.
|
||||
*/
|
||||
PR_EXTERN(PLDHashTableOps *)
|
||||
PL_DHashGetStubOps(void);
|
||||
|
||||
/*
|
||||
* Dynamically allocate a new PLDHashTable using malloc, initialize it using
|
||||
* PL_DHashTableInit, and return its address. Return null on malloc failure.
|
||||
* Note that the entry storage at table->entryStore will be allocated using
|
||||
* the ops->allocTable callback.
|
||||
*/
|
||||
PR_EXTERN(PLDHashTable *)
|
||||
PR_NewDHashTable(PLDHashTableOps *ops, void *data, PRUint32 entrySize,
|
||||
PRUint32 capacity);
|
||||
|
||||
/*
|
||||
* Finalize table's data, free its entry storage (via table->ops->freeTable),
|
||||
* and return the memory starting at table to the malloc heap.
|
||||
*/
|
||||
PR_EXTERN(void)
|
||||
PL_DHashTableDestroy(PLDHashTable *table);
|
||||
|
||||
/*
|
||||
* Initialize table with ops, data, entrySize, and capacity. Capacity is a
|
||||
* guess for the smallest table size at which the table will usually be less
|
||||
* than 75% loaded (the table will grow or shrink as needed; capacity serves
|
||||
* only to avoid inevitable early growth from PL_DHASH_MIN_SIZE).
|
||||
*/
|
||||
PR_EXTERN(PRBool)
|
||||
PL_DHashTableInit(PLDHashTable *table, PLDHashTableOps *ops, void *data,
|
||||
PRUint32 entrySize, PRUint32 capacity);
|
||||
|
||||
/*
|
||||
* Finalize table's data, free its entry storage using table->ops->freeTable,
|
||||
* and leave its members unchanged from their last live values (which leaves
|
||||
* pointers dangling). If you want to burn cycles clearing table, it's up to
|
||||
* your code to call memset.
|
||||
*/
|
||||
PR_EXTERN(void)
|
||||
PL_DHashTableFinish(PLDHashTable *table);
|
||||
|
||||
/*
|
||||
* To consolidate keyHash computation and table grow/shrink code, we use a
|
||||
* single entry point for lookup, add, and remove operations. The operation
|
||||
* codes are declared here, along with codes returned by PLDHashEnumerator
|
||||
* functions, which control PL_DHashTableEnumerate's behavior.
|
||||
*/
|
||||
typedef enum PLDHashOperator {
|
||||
PL_DHASH_LOOKUP = 0, /* lookup entry */
|
||||
PL_DHASH_ADD = 1, /* add entry */
|
||||
PL_DHASH_REMOVE = 2, /* remove entry, or enumerator says remove */
|
||||
PL_DHASH_NEXT = 0, /* enumerator says continue */
|
||||
PL_DHASH_STOP = 1 /* enumerator says stop */
|
||||
} PLDHashOperator;
|
||||
|
||||
/*
|
||||
* To lookup a key in table, call:
|
||||
*
|
||||
* entry = PL_DHashTableOperate(table, key, PL_DHASH_LOOKUP);
|
||||
*
|
||||
* If PL_DHASH_ENTRY_IS_BUSY(entry) is true, key was found and it identifies
|
||||
* entry. If PL_DHASH_ENTRY_IS_FREE(entry) is true, key was not found.
|
||||
*
|
||||
* To add an entry identified by key to table, call:
|
||||
*
|
||||
* entry = PL_DHashTableOperate(table, key, PL_DHASH_ADD);
|
||||
*
|
||||
* If entry is null upon return, the table is severely overloaded, and new
|
||||
* memory can't be allocated for new entry storage via table->ops->allocTable.
|
||||
* Otherwise, entry->keyHash has been set so that PL_DHASH_ENTRY_IS_BUSY(entry)
|
||||
* is true, and it is up to the caller to initialize the key and value parts
|
||||
* of the entry sub-type, if they have not been set already (i.e. if entry was
|
||||
* not already in the table).
|
||||
*
|
||||
* To remove an entry identified by key from table, call:
|
||||
*
|
||||
* (void) PL_DHashTableOperate(table, key, PL_DHASH_REMOVE);
|
||||
*
|
||||
* If key's entry is found, it is cleared (via table->ops->clearEntry) and
|
||||
* the entry is marked so that PL_DHASH_ENTRY_IS_FREE(entry). This operation
|
||||
* returns null unconditionally; you should ignore its return value.
|
||||
*/
|
||||
PR_EXTERN(PLDHashEntryHdr *)
|
||||
PL_DHashTableOperate(PLDHashTable *table, const void *key, PLDHashOperator op);
|
||||
|
||||
/*
|
||||
* Remove an entry already accessed via LOOKUP or ADD.
|
||||
*
|
||||
* NB: this is a "raw" or low-level routine, intended to be used only where
|
||||
* the inefficiency of a full PL_DHashTableOperate (which rehashes in order
|
||||
* to find the entry given its key) is not tolerable. This function does not
|
||||
* shrink the table if it is underloaded. It does not update stats #ifdef
|
||||
* PL_DHASHMETER, either.
|
||||
*/
|
||||
PR_EXTERN(void)
|
||||
PL_DHashTableRawRemove(PLDHashTable *table, PLDHashEntryHdr *entry);
|
||||
|
||||
/*
|
||||
* Enumerate entries in table using etor:
|
||||
*
|
||||
* count = PL_DHashTableEnumerate(table, etor, arg);
|
||||
*
|
||||
* PL_DHashTableEnumerate calls etor like so:
|
||||
*
|
||||
* op = etor(table, entry, number, arg);
|
||||
*
|
||||
* where number is a zero-based ordinal assigned to live entries according to
|
||||
* their order in table->entryStore.
|
||||
*
|
||||
* The return value, op, is treated as a set of flags. If op is PL_DHASH_NEXT,
|
||||
* then continue enumerating. If op contains PL_DHASH_REMOVE, then clear (via
|
||||
* table->ops->clearEntry) and free entry. Then we check whether op contains
|
||||
* PL_DHASH_STOP; if so, stop enumerating and return the number of live entries
|
||||
* that were enumerated so far. Return the total number of live entries when
|
||||
* enumeration completes normally.
|
||||
*
|
||||
* If etor calls PL_DHashTableOperate on table, it must return PL_DHASH_STOP;
|
||||
* otherwise undefined behavior results.
|
||||
*/
|
||||
typedef PLDHashOperator
|
||||
(* CRT_CALL PLDHashEnumerator)(PLDHashTable *table, PLDHashEntryHdr *hdr,
|
||||
PRUint32 number, void *arg);
|
||||
|
||||
PR_EXTERN(PRUint32)
|
||||
PL_DHashTableEnumerate(PLDHashTable *table, PLDHashEnumerator etor, void *arg);
|
||||
|
||||
#ifdef PL_DHASHMETER
|
||||
#include <stdio.h>
|
||||
|
||||
PR_EXTERN(void)
|
||||
PL_DHashTableDumpMeter(PLDHashTable *table, PLDHashEnumerator dump, FILE *fp);
|
||||
#endif
|
||||
|
||||
PR_END_EXTERN_C
|
||||
|
||||
#endif /* pldhash_h___ */
|
Загрузка…
Ссылка в новой задаче