1999-07-28 11:56:28 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
1998-03-28 05:44:41 +03:00
|
|
|
/*
|
1999-11-06 06:43:54 +03:00
|
|
|
* 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/
|
1998-03-28 05:44:41 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1998-03-28 05:44:41 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1998-03-28 05:44:41 +03:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2000-05-03 02:38:04 +04:00
|
|
|
* This Original Code has been modified by IBM Corporation.
|
|
|
|
* Modifications made by IBM described herein are
|
|
|
|
* Copyright (c) International Business Machines
|
|
|
|
* Corporation, 2000
|
|
|
|
*
|
|
|
|
* Modifications to Mozilla code or documentation
|
|
|
|
* identified per MPL Section 3.3
|
|
|
|
*
|
|
|
|
* Date Modified by Description of modification
|
|
|
|
* 04/20/2000 IBM Corp. Added PR_CALLBACK for Optlink use in OS2
|
1998-03-28 05:44:41 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "prmem.h"
|
1999-03-30 12:15:39 +04:00
|
|
|
#include "prlog.h"
|
1998-03-28 05:44:41 +03:00
|
|
|
#include "nsHashtable.h"
|
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// These functions really should be part of nspr, and have internal knowledge
|
|
|
|
// of its workings. They allow the PLHashTable to be embedded in the structure
|
|
|
|
// of the nsHashtable, thereby avoiding a secondary allocation. I've added them
|
|
|
|
// here because we don't have the "right" to add anything to nspr at this point.
|
|
|
|
|
|
|
|
#include "prbit.h"
|
|
|
|
|
|
|
|
/* Compute the number of buckets in ht */
|
|
|
|
#define NBUCKETS(ht) (1 << (PL_HASH_BITS - (ht)->shift))
|
|
|
|
|
|
|
|
/* The smallest table has 16 buckets */
|
|
|
|
#define MINBUCKETSLOG2 4
|
|
|
|
#define MINBUCKETS (1 << MINBUCKETSLOG2)
|
|
|
|
|
|
|
|
/* Compute the maximum entries given n buckets that we will tolerate, ~90% */
|
|
|
|
#define OVERLOADED(n) ((n) - ((n) >> 3))
|
|
|
|
|
|
|
|
/* Compute the number of entries below which we shrink the table by half */
|
|
|
|
#define UNDERLOADED(n) (((n) > MINBUCKETS) ? ((n) >> 2) : 0)
|
|
|
|
|
|
|
|
PR_IMPLEMENT(PRStatus)
|
|
|
|
PL_HashTableInit(PLHashTable *ht, PRUint32 n, PLHashFunction keyHash,
|
|
|
|
PLHashComparator keyCompare, PLHashComparator valueCompare,
|
|
|
|
const PLHashAllocOps *allocOps, void *allocPriv)
|
|
|
|
{
|
|
|
|
PRSize nb;
|
|
|
|
|
|
|
|
if (n <= MINBUCKETS) {
|
|
|
|
n = MINBUCKETSLOG2;
|
|
|
|
} else {
|
|
|
|
n = PR_CeilingLog2(n);
|
|
|
|
if ((PRInt32)n < 0)
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0 // if we were in nspr...
|
|
|
|
if (!allocOps) allocOps = &defaultHashAllocOps;
|
|
|
|
#else
|
|
|
|
PR_ASSERT(allocOps);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memset(ht, 0, sizeof *ht);
|
|
|
|
ht->shift = PL_HASH_BITS - n;
|
|
|
|
n = 1 << n;
|
|
|
|
#if defined(WIN16)
|
|
|
|
if (n > 16000) {
|
|
|
|
(*allocOps->freeTable)(allocPriv, ht);
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
|
|
|
#endif /* WIN16 */
|
|
|
|
nb = n * sizeof(PLHashEntry *);
|
|
|
|
ht->buckets = (PLHashEntry**)((*allocOps->allocTable)(allocPriv, nb));
|
|
|
|
if (!ht->buckets) {
|
|
|
|
(*allocOps->freeTable)(allocPriv, ht);
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
|
|
|
memset(ht->buckets, 0, nb);
|
|
|
|
|
|
|
|
ht->keyHash = keyHash;
|
|
|
|
ht->keyCompare = keyCompare;
|
|
|
|
ht->valueCompare = valueCompare;
|
|
|
|
ht->allocOps = allocOps;
|
|
|
|
ht->allocPriv = allocPriv;
|
|
|
|
return PR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_IMPLEMENT(void)
|
|
|
|
PL_HashTableFinalize(PLHashTable *ht)
|
|
|
|
{
|
|
|
|
PRUint32 i, n;
|
|
|
|
PLHashEntry *he, *next;
|
|
|
|
const PLHashAllocOps *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
|
|
|
|
}
|
|
|
|
|
|
|
|
// end of nspr stuff
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
//
|
|
|
|
// Key operations
|
|
|
|
//
|
|
|
|
|
2000-05-03 02:38:04 +04:00
|
|
|
static PLHashNumber PR_CALLBACK _hashValue(const void *key)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2000-08-10 10:19:37 +04:00
|
|
|
return ((const nsHashKey *) key)->HashCode();
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2000-05-03 02:38:04 +04:00
|
|
|
static PRIntn PR_CALLBACK _hashKeyCompare(const void *key1, const void *key2) {
|
1998-05-23 01:56:30 +04:00
|
|
|
return ((const nsHashKey *) key1)->Equals((const nsHashKey *) key2);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2000-05-03 02:38:04 +04:00
|
|
|
static PRIntn PR_CALLBACK _hashValueCompare(const void *value1,
|
1998-03-28 05:44:41 +03:00
|
|
|
const void *value2) {
|
|
|
|
// We're not going to make any assumptions about value equality
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Memory callbacks
|
|
|
|
//
|
|
|
|
|
2000-05-03 02:38:04 +04:00
|
|
|
static void * PR_CALLBACK _hashAllocTable(void *pool, PRSize size) {
|
1998-03-28 05:44:41 +03:00
|
|
|
return PR_MALLOC(size);
|
|
|
|
}
|
|
|
|
|
2000-05-03 02:38:04 +04:00
|
|
|
static void PR_CALLBACK _hashFreeTable(void *pool, void *item) {
|
1998-03-28 05:44:41 +03:00
|
|
|
PR_DELETE(item);
|
|
|
|
}
|
|
|
|
|
2000-05-03 02:38:04 +04:00
|
|
|
static PLHashEntry * PR_CALLBACK _hashAllocEntry(void *pool, const void *key) {
|
1998-03-28 05:44:41 +03:00
|
|
|
return PR_NEW(PLHashEntry);
|
|
|
|
}
|
|
|
|
|
2000-05-03 02:38:04 +04:00
|
|
|
static void PR_CALLBACK _hashFreeEntry(void *pool, PLHashEntry *entry,
|
1998-03-28 05:44:41 +03:00
|
|
|
PRUintn flag) {
|
|
|
|
if (flag == HT_FREE_ENTRY) {
|
|
|
|
delete (nsHashKey *) (entry->key);
|
|
|
|
PR_DELETE(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLHashAllocOps _hashAllocOps = {
|
|
|
|
_hashAllocTable, _hashFreeTable,
|
|
|
|
_hashAllocEntry, _hashFreeEntry
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Enumerator callback
|
|
|
|
//
|
|
|
|
|
1998-09-01 04:16:47 +04:00
|
|
|
struct _HashEnumerateArgs {
|
|
|
|
nsHashtableEnumFunc fn;
|
|
|
|
void* arg;
|
|
|
|
};
|
|
|
|
|
2000-05-03 02:38:04 +04:00
|
|
|
static PRIntn PR_CALLBACK _hashEnumerate(PLHashEntry *he, PRIntn i, void *arg)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
1998-09-01 04:16:47 +04:00
|
|
|
_HashEnumerateArgs* thunk = (_HashEnumerateArgs*)arg;
|
|
|
|
return thunk->fn((nsHashKey *) he->key, he->value, thunk->arg)
|
|
|
|
? HT_ENUMERATE_NEXT
|
|
|
|
: HT_ENUMERATE_STOP;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
1998-05-23 01:56:30 +04:00
|
|
|
//
|
|
|
|
// HashKey
|
|
|
|
//
|
2000-05-15 17:10:18 +04:00
|
|
|
|
2001-01-28 02:06:33 +03:00
|
|
|
MOZ_DECL_CTOR_COUNTER(nsHashKey)
|
2000-05-15 17:10:18 +04:00
|
|
|
|
1998-05-23 01:56:30 +04:00
|
|
|
nsHashKey::nsHashKey(void)
|
|
|
|
{
|
2000-08-10 10:19:37 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = UnknownKey;
|
|
|
|
#endif
|
2000-05-15 17:10:18 +04:00
|
|
|
MOZ_COUNT_CTOR(nsHashKey);
|
1998-05-23 01:56:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHashKey::~nsHashKey(void)
|
|
|
|
{
|
2000-05-15 17:10:18 +04:00
|
|
|
MOZ_COUNT_DTOR(nsHashKey);
|
1998-05-23 01:56:30 +04:00
|
|
|
}
|
|
|
|
|
2001-01-28 02:06:33 +03:00
|
|
|
MOZ_DECL_CTOR_COUNTER(nsHashtable)
|
2000-05-15 17:10:18 +04:00
|
|
|
|
1999-03-30 12:15:39 +04:00
|
|
|
nsHashtable::nsHashtable(PRUint32 aInitSize, PRBool threadSafe)
|
2000-09-01 23:11:24 +04:00
|
|
|
: mLock(NULL), mEnumerating(PR_FALSE)
|
1999-03-30 12:15:39 +04:00
|
|
|
{
|
2000-05-15 17:10:18 +04:00
|
|
|
MOZ_COUNT_CTOR(nsHashtable);
|
2000-08-10 10:19:37 +04:00
|
|
|
PRStatus status = PL_HashTableInit(&mHashtable,
|
|
|
|
aInitSize,
|
|
|
|
_hashValue,
|
|
|
|
_hashKeyCompare,
|
|
|
|
_hashValueCompare,
|
|
|
|
&_hashAllocOps,
|
|
|
|
NULL);
|
|
|
|
PR_ASSERT(status == PR_SUCCESS);
|
1999-03-30 12:15:39 +04:00
|
|
|
if (threadSafe == PR_TRUE)
|
|
|
|
{
|
|
|
|
mLock = PR_NewLock();
|
|
|
|
if (mLock == NULL)
|
|
|
|
{
|
|
|
|
// Cannot create a lock. If running on a multiprocessing system
|
|
|
|
// we are sure to die.
|
|
|
|
PR_ASSERT(mLock != NULL);
|
|
|
|
}
|
|
|
|
}
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2000-08-21 01:29:10 +04:00
|
|
|
#ifdef HASHMETER
|
|
|
|
PRIntn DontEnum(PLHashEntry *he, PRIntn i, void *arg) {
|
|
|
|
return HT_ENUMERATE_STOP;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
nsHashtable::~nsHashtable() {
|
2000-08-21 01:29:10 +04:00
|
|
|
#ifdef HASHMETER
|
|
|
|
PL_HashTableDump(&mHashtable, DontEnum, stdout);
|
|
|
|
#endif
|
2000-05-15 17:10:18 +04:00
|
|
|
MOZ_COUNT_DTOR(nsHashtable);
|
2000-08-10 10:19:37 +04:00
|
|
|
PL_HashTableFinalize(&mHashtable);
|
1999-03-30 12:15:39 +04:00
|
|
|
if (mLock) PR_DestroyLock(mLock);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
1999-03-19 08:51:49 +03:00
|
|
|
PRBool nsHashtable::Exists(nsHashKey *aKey)
|
|
|
|
{
|
2000-08-10 10:19:37 +04:00
|
|
|
PLHashNumber hash = aKey->HashCode();
|
1999-03-30 12:15:39 +04:00
|
|
|
|
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
2000-09-01 23:11:24 +04:00
|
|
|
PLHashEntry *const*hep = mEnumerating ?
|
|
|
|
PL_HashTableRawLookupConst(&mHashtable, hash, (void *) aKey) :
|
|
|
|
PL_HashTableRawLookup(&mHashtable, hash, (void *) aKey);
|
1999-03-19 08:51:49 +03:00
|
|
|
|
1999-03-30 12:15:39 +04:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
1999-03-19 08:51:49 +03:00
|
|
|
return *hep != NULL;
|
|
|
|
}
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
void *nsHashtable::Put(nsHashKey *aKey, void *aData) {
|
|
|
|
void *res = NULL;
|
2000-08-10 10:19:37 +04:00
|
|
|
PLHashNumber hash = aKey->HashCode();
|
1998-03-28 05:44:41 +03:00
|
|
|
PLHashEntry *he;
|
1999-03-30 12:15:39 +04:00
|
|
|
|
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
2000-09-01 23:11:24 +04:00
|
|
|
// shouldn't be adding an item during enumeration
|
|
|
|
PR_ASSERT(!mEnumerating);
|
2000-08-10 10:19:37 +04:00
|
|
|
PLHashEntry **hep = PL_HashTableRawLookup(&mHashtable, hash, (void *) aKey);
|
1998-03-28 05:44:41 +03:00
|
|
|
|
|
|
|
if ((he = *hep) != NULL) {
|
|
|
|
res = he->value;
|
|
|
|
he->value = aData;
|
|
|
|
} else {
|
2000-08-10 10:19:37 +04:00
|
|
|
nsHashKey* key = aKey->Clone();
|
|
|
|
if (key) {
|
|
|
|
PL_HashTableRawAdd(&mHashtable, hep, hash,
|
|
|
|
(void *)key, aData);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res = NULL;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
1999-03-30 12:15:39 +04:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *nsHashtable::Get(nsHashKey *aKey) {
|
1999-03-30 12:15:39 +04:00
|
|
|
|
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
2000-09-01 23:11:24 +04:00
|
|
|
void *ret = mEnumerating ?
|
|
|
|
PL_HashTableLookupConst(&mHashtable, (void *) aKey) :
|
|
|
|
PL_HashTableLookup(&mHashtable, (void *) aKey);
|
1999-03-30 12:15:39 +04:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
|
|
|
return ret;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void *nsHashtable::Remove(nsHashKey *aKey) {
|
2000-08-10 10:19:37 +04:00
|
|
|
PLHashNumber hash = aKey->HashCode();
|
1998-03-28 05:44:41 +03:00
|
|
|
PLHashEntry *he;
|
1999-03-30 12:15:39 +04:00
|
|
|
|
|
|
|
if (mLock) PR_Lock(mLock);
|
2000-09-01 23:11:24 +04:00
|
|
|
|
|
|
|
// shouldn't be adding an item during enumeration
|
|
|
|
PR_ASSERT(!mEnumerating);
|
2000-08-10 10:19:37 +04:00
|
|
|
PLHashEntry **hep = PL_HashTableRawLookup(&mHashtable, hash, (void *) aKey);
|
1998-03-28 05:44:41 +03:00
|
|
|
void *res = NULL;
|
|
|
|
|
|
|
|
if ((he = *hep) != NULL) {
|
|
|
|
res = he->value;
|
2000-08-10 10:19:37 +04:00
|
|
|
PL_HashTableRawRemove(&mHashtable, hep, he);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
1999-03-30 12:15:39 +04:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1999-07-28 11:56:28 +04:00
|
|
|
// XXX This method was called _hashEnumerateCopy, but it didn't copy the element!
|
|
|
|
// I don't know how this was supposed to work since the elements are neither copied
|
|
|
|
// nor refcounted.
|
2000-05-03 02:38:04 +04:00
|
|
|
static PRIntn PR_CALLBACK _hashEnumerateShare(PLHashEntry *he, PRIntn i, void *arg)
|
1998-06-02 02:11:06 +04:00
|
|
|
{
|
|
|
|
nsHashtable *newHashtable = (nsHashtable *)arg;
|
|
|
|
newHashtable->Put((nsHashKey *) he->key, he->value);
|
|
|
|
return HT_ENUMERATE_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable * nsHashtable::Clone() {
|
1999-03-30 12:15:39 +04:00
|
|
|
PRBool threadSafe = PR_FALSE;
|
|
|
|
if (mLock)
|
|
|
|
threadSafe = PR_TRUE;
|
2000-08-10 10:19:37 +04:00
|
|
|
nsHashtable *newHashTable = new nsHashtable(mHashtable.nentries, threadSafe);
|
1999-03-30 12:15:39 +04:00
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
PL_HashTableEnumerateEntries(&mHashtable, _hashEnumerateShare, newHashTable);
|
1998-06-02 02:11:06 +04:00
|
|
|
return newHashTable;
|
|
|
|
}
|
|
|
|
|
1998-09-01 04:16:47 +04:00
|
|
|
void nsHashtable::Enumerate(nsHashtableEnumFunc aEnumFunc, void* closure) {
|
2000-09-01 23:11:24 +04:00
|
|
|
PRBool wasEnumerating = mEnumerating;
|
|
|
|
mEnumerating = PR_TRUE;
|
1998-09-01 04:16:47 +04:00
|
|
|
_HashEnumerateArgs thunk;
|
|
|
|
thunk.fn = aEnumFunc;
|
|
|
|
thunk.arg = closure;
|
2000-08-10 10:19:37 +04:00
|
|
|
PL_HashTableEnumerateEntries(&mHashtable, _hashEnumerate, &thunk);
|
2000-09-01 23:11:24 +04:00
|
|
|
mEnumerating = wasEnumerating;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
1999-02-03 22:35:11 +03:00
|
|
|
|
2000-05-03 02:38:04 +04:00
|
|
|
static PRIntn PR_CALLBACK _hashEnumerateRemove(PLHashEntry *he, PRIntn i, void *arg)
|
1999-02-03 22:35:11 +03:00
|
|
|
{
|
1999-07-31 09:41:54 +04:00
|
|
|
_HashEnumerateArgs* thunk = (_HashEnumerateArgs*)arg;
|
|
|
|
if (thunk)
|
|
|
|
return thunk->fn((nsHashKey *) he->key, he->value, thunk->arg)
|
|
|
|
? HT_ENUMERATE_REMOVE
|
|
|
|
: HT_ENUMERATE_STOP;
|
|
|
|
else
|
|
|
|
return HT_ENUMERATE_REMOVE;
|
1999-02-03 22:35:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsHashtable::Reset() {
|
1999-07-31 09:41:54 +04:00
|
|
|
Reset(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsHashtable::Reset(nsHashtableEnumFunc destroyFunc, void* closure)
|
|
|
|
{
|
|
|
|
if (destroyFunc != NULL)
|
|
|
|
{
|
|
|
|
_HashEnumerateArgs thunk;
|
|
|
|
thunk.fn = destroyFunc;
|
|
|
|
thunk.arg = closure;
|
2000-08-10 10:19:37 +04:00
|
|
|
PL_HashTableEnumerateEntries(&mHashtable, _hashEnumerateRemove, &thunk);
|
1999-07-31 09:41:54 +04:00
|
|
|
}
|
|
|
|
else
|
2000-08-10 10:19:37 +04:00
|
|
|
PL_HashTableEnumerateEntries(&mHashtable, _hashEnumerateRemove, NULL);
|
1999-02-03 22:35:11 +03:00
|
|
|
}
|
|
|
|
|
1999-03-09 17:09:11 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-03-19 09:18:08 +03:00
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
nsCStringKey::nsCStringKey(const nsCString& str)
|
2001-03-02 12:26:57 +03:00
|
|
|
: mStr((char*)str.get()), mStrLen(str.Length()), mOwnership(OWN_CLONE)
|
1999-03-19 09:18:08 +03:00
|
|
|
{
|
2000-08-10 10:19:37 +04:00
|
|
|
NS_ASSERTION(mStr, "null string key");
|
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = CStringKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsCStringKey);
|
1999-07-22 06:11:04 +04:00
|
|
|
}
|
|
|
|
|
2000-08-16 04:32:59 +04:00
|
|
|
nsCStringKey::nsCStringKey(const char* str, PRInt32 strLen, Ownership own)
|
|
|
|
: mStr((char*)str), mStrLen(strLen), mOwnership(own)
|
1999-07-22 06:11:04 +04:00
|
|
|
{
|
2000-08-10 10:19:37 +04:00
|
|
|
NS_ASSERTION(mStr, "null string key");
|
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = CStringKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsCStringKey);
|
1999-07-22 06:11:04 +04:00
|
|
|
}
|
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
nsCStringKey::~nsCStringKey(void)
|
|
|
|
{
|
2000-08-16 04:32:59 +04:00
|
|
|
if (mOwnership == OWN)
|
2000-08-10 10:19:37 +04:00
|
|
|
nsMemory::Free(mStr);
|
|
|
|
MOZ_COUNT_DTOR(nsCStringKey);
|
2000-03-12 14:10:07 +03:00
|
|
|
}
|
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
PRUint32
|
|
|
|
nsCStringKey::HashCode(void) const
|
|
|
|
{
|
2000-08-21 01:29:10 +04:00
|
|
|
return nsCRT::HashCode(mStr, (PRUint32*)&mStrLen);
|
1999-03-19 09:18:08 +03:00
|
|
|
}
|
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
PRBool
|
|
|
|
nsCStringKey::Equals(const nsHashKey* aKey) const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aKey->GetKeyType() == CStringKey, "mismatched key types");
|
|
|
|
nsCStringKey* other = (nsCStringKey*)aKey;
|
2000-08-21 01:29:10 +04:00
|
|
|
NS_ASSERTION(mStrLen != -1, "never called HashCode");
|
|
|
|
NS_ASSERTION(other->mStrLen != -1, "never called HashCode");
|
2000-08-10 10:19:37 +04:00
|
|
|
if (mStrLen != other->mStrLen)
|
|
|
|
return PR_FALSE;
|
2000-08-12 03:46:09 +04:00
|
|
|
return nsCRT::memcmp(mStr, other->mStr, mStrLen * sizeof(char)) == 0;
|
2000-08-10 10:19:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHashKey*
|
|
|
|
nsCStringKey::Clone() const
|
|
|
|
{
|
2000-08-16 04:32:59 +04:00
|
|
|
if (mOwnership == NEVER_OWN)
|
|
|
|
return new nsCStringKey(mStr, mStrLen, NEVER_OWN);
|
|
|
|
|
|
|
|
// Since this might hold binary data OR a string, we ensure that the
|
|
|
|
// clone string is zero terminated, but don't assume that the source
|
|
|
|
// string was so terminated.
|
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
PRUint32 len = mStrLen * sizeof(char);
|
2000-08-16 04:32:59 +04:00
|
|
|
char* str = (char*)nsMemory::Alloc(len + sizeof(char));
|
2000-08-10 10:19:37 +04:00
|
|
|
if (str == NULL)
|
|
|
|
return NULL;
|
|
|
|
nsCRT::memcpy(str, mStr, len);
|
2000-08-16 04:32:59 +04:00
|
|
|
str[len] = 0;
|
|
|
|
return new nsCStringKey(str, mStrLen, OWN);
|
2000-08-10 10:19:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2000-08-23 21:27:06 +04:00
|
|
|
nsStringKey::nsStringKey(const nsAReadableString& str)
|
2001-04-02 23:40:52 +04:00
|
|
|
: mStr(NS_CONST_CAST(PRUnichar*, NS_STATIC_CAST(const PRUnichar *, PromiseFlatString(str).get()))), mStrLen(str.Length()), mOwnership(OWN_CLONE)
|
2000-08-10 10:19:37 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mStr, "null string key");
|
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = StringKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsStringKey);
|
|
|
|
}
|
|
|
|
|
2000-08-16 04:32:59 +04:00
|
|
|
nsStringKey::nsStringKey(const PRUnichar* str, PRInt32 strLen, Ownership own)
|
|
|
|
: mStr((PRUnichar*)str), mStrLen(strLen), mOwnership(own)
|
1999-03-19 09:18:08 +03:00
|
|
|
{
|
2000-08-10 10:19:37 +04:00
|
|
|
NS_ASSERTION(mStr, "null string key");
|
2000-08-21 01:29:10 +04:00
|
|
|
// if (mStrLen == -1)
|
|
|
|
// mStrLen = nsCRT::strlen(str);
|
2000-08-10 10:19:37 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = StringKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsStringKey);
|
1999-03-19 09:18:08 +03:00
|
|
|
}
|
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
nsStringKey::~nsStringKey(void)
|
1999-03-19 09:18:08 +03:00
|
|
|
{
|
2000-08-16 04:32:59 +04:00
|
|
|
if (mOwnership == OWN)
|
2000-08-10 10:19:37 +04:00
|
|
|
nsMemory::Free(mStr);
|
|
|
|
MOZ_COUNT_DTOR(nsStringKey);
|
1999-03-19 09:18:08 +03:00
|
|
|
}
|
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
PRUint32
|
|
|
|
nsStringKey::HashCode(void) const
|
1999-03-19 09:18:08 +03:00
|
|
|
{
|
2000-08-21 01:29:10 +04:00
|
|
|
return nsCRT::HashCode(mStr, (PRUint32*)&mStrLen);
|
1999-03-19 09:18:08 +03:00
|
|
|
}
|
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
PRBool
|
|
|
|
nsStringKey::Equals(const nsHashKey* aKey) const
|
1999-03-19 09:18:08 +03:00
|
|
|
{
|
2000-08-10 10:19:37 +04:00
|
|
|
NS_ASSERTION(aKey->GetKeyType() == StringKey, "mismatched key types");
|
|
|
|
nsStringKey* other = (nsStringKey*)aKey;
|
2000-08-21 01:29:10 +04:00
|
|
|
NS_ASSERTION(mStrLen != -1, "never called HashCode");
|
|
|
|
NS_ASSERTION(other->mStrLen != -1, "never called HashCode");
|
2000-08-10 10:19:37 +04:00
|
|
|
if (mStrLen != other->mStrLen)
|
|
|
|
return PR_FALSE;
|
2000-08-12 03:46:09 +04:00
|
|
|
return nsCRT::memcmp(mStr, other->mStr, mStrLen * sizeof(PRUnichar)) == 0;
|
1999-03-19 09:18:08 +03:00
|
|
|
}
|
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
nsHashKey*
|
|
|
|
nsStringKey::Clone() const
|
1999-08-09 09:02:25 +04:00
|
|
|
{
|
2000-08-16 04:32:59 +04:00
|
|
|
if (mOwnership == NEVER_OWN)
|
|
|
|
return new nsStringKey(mStr, mStrLen, NEVER_OWN);
|
|
|
|
|
|
|
|
PRUint32 len = (mStrLen+1) * sizeof(PRUnichar);
|
2000-08-10 10:19:37 +04:00
|
|
|
PRUnichar* str = (PRUnichar*)nsMemory::Alloc(len);
|
|
|
|
if (str == NULL)
|
|
|
|
return NULL;
|
|
|
|
nsCRT::memcpy(str, mStr, len);
|
2000-08-16 04:32:59 +04:00
|
|
|
return new nsStringKey(str, mStrLen, OWN);
|
2000-08-10 10:19:37 +04:00
|
|
|
}
|
1999-08-09 09:02:25 +04:00
|
|
|
|
2000-08-21 06:37:20 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsOpaqueKey::nsOpaqueKey(const char* str, PRUint32 strLen, Ownership own)
|
|
|
|
: mBuf((char*)str), mBufLen(strLen), mOwnership(own)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mBuf, "null buffer");
|
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = OpaqueKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsOpaqueKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsOpaqueKey::~nsOpaqueKey(void)
|
|
|
|
{
|
|
|
|
if (mOwnership == OWN)
|
|
|
|
nsMemory::Free(mBuf);
|
|
|
|
MOZ_COUNT_DTOR(nsOpaqueKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32
|
|
|
|
nsOpaqueKey::HashCode(void) const
|
|
|
|
{
|
|
|
|
return nsCRT::BufferHashCode(mBuf, mBufLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsOpaqueKey::Equals(const nsHashKey* aKey) const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aKey->GetKeyType() == OpaqueKey, "mismatched key types");
|
|
|
|
nsOpaqueKey* other = (nsOpaqueKey*)aKey;
|
|
|
|
if (mBufLen != other->mBufLen)
|
|
|
|
return PR_FALSE;
|
|
|
|
return nsCRT::memcmp(mBuf, other->mBuf, mBufLen) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashKey*
|
|
|
|
nsOpaqueKey::Clone() const
|
|
|
|
{
|
|
|
|
if (mOwnership == NEVER_OWN)
|
|
|
|
return new nsOpaqueKey(mBuf, mBufLen, NEVER_OWN);
|
|
|
|
|
|
|
|
// Since this might hold binary data OR a string, we ensure that the
|
|
|
|
// clone string is zero terminated, but don't assume that the source
|
|
|
|
// string was so terminated.
|
|
|
|
|
|
|
|
PRUint32 len = mBufLen * sizeof(char);
|
|
|
|
char* str = (char*)nsMemory::Alloc(len + sizeof(char));
|
|
|
|
if (str == NULL)
|
|
|
|
return NULL;
|
|
|
|
nsCRT::memcpy(str, mBuf, len);
|
|
|
|
str[len] = 0;
|
|
|
|
return new nsOpaqueKey(str, mBufLen, OWN);
|
|
|
|
}
|
|
|
|
|
1999-03-19 09:18:08 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-07-28 11:56:28 +04:00
|
|
|
// nsObjectHashtable: an nsHashtable where the elements are C++ objects to be
|
|
|
|
// deleted
|
|
|
|
|
|
|
|
nsObjectHashtable::nsObjectHashtable(nsHashtableCloneElementFunc cloneElementFun,
|
|
|
|
void* cloneElementClosure,
|
|
|
|
nsHashtableEnumFunc destroyElementFun,
|
|
|
|
void* destroyElementClosure,
|
|
|
|
PRUint32 aSize, PRBool threadSafe)
|
|
|
|
: nsHashtable(aSize, threadSafe),
|
|
|
|
mCloneElementFun(cloneElementFun),
|
|
|
|
mCloneElementClosure(cloneElementClosure),
|
|
|
|
mDestroyElementFun(destroyElementFun),
|
|
|
|
mDestroyElementClosure(destroyElementClosure)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsObjectHashtable::~nsObjectHashtable()
|
|
|
|
{
|
1999-07-31 09:50:13 +04:00
|
|
|
Reset();
|
1999-07-28 11:56:28 +04:00
|
|
|
}
|
|
|
|
|
2000-05-03 02:38:04 +04:00
|
|
|
PRIntn PR_CALLBACK
|
1999-07-28 11:56:28 +04:00
|
|
|
nsObjectHashtable::CopyElement(PLHashEntry *he, PRIntn i, void *arg)
|
|
|
|
{
|
|
|
|
nsObjectHashtable *newHashtable = (nsObjectHashtable *)arg;
|
|
|
|
void* newElement =
|
|
|
|
newHashtable->mCloneElementFun((nsHashKey*)he->key, he->value,
|
|
|
|
newHashtable->mCloneElementClosure);
|
|
|
|
if (newElement == nsnull)
|
|
|
|
return HT_ENUMERATE_STOP;
|
|
|
|
newHashtable->Put((nsHashKey*)he->key, newElement);
|
|
|
|
return HT_ENUMERATE_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable*
|
|
|
|
nsObjectHashtable::Clone()
|
|
|
|
{
|
|
|
|
PRBool threadSafe = PR_FALSE;
|
|
|
|
if (mLock)
|
|
|
|
threadSafe = PR_TRUE;
|
|
|
|
nsObjectHashtable* newHashTable =
|
|
|
|
new nsObjectHashtable(mCloneElementFun, mCloneElementClosure,
|
|
|
|
mDestroyElementFun, mDestroyElementClosure,
|
2000-08-10 10:19:37 +04:00
|
|
|
mHashtable.nentries, threadSafe);
|
1999-07-28 11:56:28 +04:00
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
PL_HashTableEnumerateEntries(&mHashtable, CopyElement, newHashTable);
|
1999-07-28 11:56:28 +04:00
|
|
|
return newHashTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsObjectHashtable::Reset()
|
|
|
|
{
|
1999-07-31 09:50:13 +04:00
|
|
|
nsHashtable::Reset(mDestroyElementFun, mDestroyElementClosure);
|
1999-07-28 11:56:28 +04:00
|
|
|
}
|
|
|
|
|
1999-08-03 03:55:03 +04:00
|
|
|
PRBool
|
|
|
|
nsObjectHashtable::RemoveAndDelete(nsHashKey *aKey)
|
|
|
|
{
|
|
|
|
void *value = Remove(aKey);
|
|
|
|
if (value && mDestroyElementFun)
|
|
|
|
{
|
|
|
|
return (*mDestroyElementFun)(aKey, value, mDestroyElementClosure);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-07-28 11:56:28 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsSupportsHashtable: an nsHashtable where the elements are nsISupports*
|
|
|
|
|
2000-06-07 02:06:56 +04:00
|
|
|
PRBool PR_CALLBACK
|
|
|
|
nsSupportsHashtable::ReleaseElement(nsHashKey *aKey, void *aData, void* closure)
|
1999-07-28 11:56:28 +04:00
|
|
|
{
|
|
|
|
nsISupports* element = NS_STATIC_CAST(nsISupports*, aData);
|
1999-11-11 23:41:54 +03:00
|
|
|
NS_IF_RELEASE(element);
|
1999-07-28 11:56:28 +04:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSupportsHashtable::~nsSupportsHashtable()
|
|
|
|
{
|
2000-06-07 02:06:56 +04:00
|
|
|
Enumerate(ReleaseElement, nsnull);
|
1999-07-28 11:56:28 +04:00
|
|
|
}
|
|
|
|
|
2000-06-07 02:06:56 +04:00
|
|
|
// Return if we overwrote something
|
|
|
|
|
|
|
|
PRBool
|
2000-08-10 10:19:37 +04:00
|
|
|
nsSupportsHashtable::Put(nsHashKey *aKey, nsISupports* aData, nsISupports **value)
|
1999-07-28 11:56:28 +04:00
|
|
|
{
|
2000-06-07 02:06:56 +04:00
|
|
|
NS_IF_ADDREF(aData);
|
|
|
|
void *prev = nsHashtable::Put(aKey, aData);
|
|
|
|
nsISupports *old = NS_REINTERPRET_CAST(nsISupports *, prev);
|
|
|
|
if (value) // pass own the ownership to the caller
|
|
|
|
*value = old;
|
|
|
|
else // the caller doesn't care, we do
|
|
|
|
NS_IF_RELEASE(old);
|
|
|
|
return prev != nsnull;
|
1999-07-28 11:56:28 +04:00
|
|
|
}
|
|
|
|
|
2000-06-07 02:06:56 +04:00
|
|
|
nsISupports *
|
1999-07-28 11:56:28 +04:00
|
|
|
nsSupportsHashtable::Get(nsHashKey *aKey)
|
|
|
|
{
|
|
|
|
void* data = nsHashtable::Get(aKey);
|
1999-09-02 11:01:53 +04:00
|
|
|
if (!data)
|
|
|
|
return nsnull;
|
1999-11-20 10:15:23 +03:00
|
|
|
nsISupports* element = NS_REINTERPRET_CAST(nsISupports*, data);
|
1999-11-11 23:41:54 +03:00
|
|
|
NS_IF_ADDREF(element);
|
2000-06-07 02:06:56 +04:00
|
|
|
return element;
|
1999-07-28 11:56:28 +04:00
|
|
|
}
|
|
|
|
|
2000-06-07 02:06:56 +04:00
|
|
|
// Return if we found something (useful for checks)
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsSupportsHashtable::Remove(nsHashKey *aKey, nsISupports **value)
|
1999-07-28 11:56:28 +04:00
|
|
|
{
|
|
|
|
void* data = nsHashtable::Remove(aKey);
|
|
|
|
nsISupports* element = NS_STATIC_CAST(nsISupports*, data);
|
2000-06-07 02:06:56 +04:00
|
|
|
if (value) // caller wants it
|
|
|
|
*value = element;
|
|
|
|
else // caller doesn't care, we do
|
1999-11-11 23:41:54 +03:00
|
|
|
NS_IF_RELEASE(element);
|
2000-06-07 02:06:56 +04:00
|
|
|
return data != nsnull;
|
1999-07-28 11:56:28 +04:00
|
|
|
}
|
|
|
|
|
2000-06-07 02:06:56 +04:00
|
|
|
PRIntn PR_CALLBACK
|
|
|
|
nsSupportsHashtable::EnumerateCopy(PLHashEntry *he, PRIntn i, void *arg)
|
1999-07-28 11:56:28 +04:00
|
|
|
{
|
|
|
|
nsHashtable *newHashtable = (nsHashtable *)arg;
|
|
|
|
nsISupports* element = NS_STATIC_CAST(nsISupports*, he->value);
|
1999-11-11 23:41:54 +03:00
|
|
|
NS_IF_ADDREF(element);
|
1999-07-28 11:56:28 +04:00
|
|
|
newHashtable->Put((nsHashKey*)he->key, he->value);
|
|
|
|
return HT_ENUMERATE_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable*
|
|
|
|
nsSupportsHashtable::Clone()
|
|
|
|
{
|
|
|
|
PRBool threadSafe = PR_FALSE;
|
|
|
|
if (mLock)
|
|
|
|
threadSafe = PR_TRUE;
|
|
|
|
nsSupportsHashtable* newHashTable =
|
2000-08-10 10:19:37 +04:00
|
|
|
new nsSupportsHashtable(mHashtable.nentries, threadSafe);
|
1999-07-28 11:56:28 +04:00
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
PL_HashTableEnumerateEntries(&mHashtable, EnumerateCopy, newHashTable);
|
1999-07-28 11:56:28 +04:00
|
|
|
return newHashTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSupportsHashtable::Reset()
|
|
|
|
{
|
2000-06-07 02:06:56 +04:00
|
|
|
Enumerate(ReleaseElement, nsnull);
|
1999-07-28 11:56:28 +04:00
|
|
|
nsHashtable::Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-11-16 06:10:54 +03:00
|
|
|
|