1999-07-28 07:56:28 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2000-05-02 22:38:04 +00: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 02:44:41 +00:00
|
|
|
*/
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
#include <string.h>
|
2013-07-11 20:21:45 +00:00
|
|
|
#include "pratom.h"
|
1999-03-30 08:15:39 +00:00
|
|
|
#include "prlog.h"
|
1998-03-28 02:44:41 +00:00
|
|
|
#include "nsHashtable.h"
|
2001-06-29 23:08:23 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2001-07-31 19:05:34 +00:00
|
|
|
#include "nsIObjectInputStream.h"
|
|
|
|
#include "nsIObjectOutputStream.h"
|
2002-05-15 18:55:21 +00:00
|
|
|
#include "nsCRT.h"
|
2012-03-12 22:53:18 +00:00
|
|
|
#include "mozilla/HashFunctions.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2002-11-16 02:33:49 +00:00
|
|
|
struct HTEntry : PLDHashEntryHdr
|
2000-08-10 06:19:37 +00:00
|
|
|
{
|
2002-11-16 02:33:49 +00:00
|
|
|
nsHashKey* key;
|
|
|
|
void* value;
|
|
|
|
};
|
2000-08-10 06:19:37 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
//
|
|
|
|
// Key operations
|
|
|
|
//
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool
|
2002-11-16 02:33:49 +00:00
|
|
|
matchKeyEntry(PLDHashTable*, const PLDHashEntryHdr* entry,
|
|
|
|
const void* key)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2002-11-16 02:33:49 +00:00
|
|
|
const HTEntry* hashEntry =
|
2007-07-08 07:08:04 +00:00
|
|
|
static_cast<const HTEntry*>(entry);
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2002-11-16 02:33:49 +00:00
|
|
|
if (hashEntry->key == key)
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2007-07-08 07:08:04 +00:00
|
|
|
const nsHashKey* otherKey = reinterpret_cast<const nsHashKey*>(key);
|
2002-11-16 02:33:49 +00:00
|
|
|
return otherKey->Equals(hashEntry->key);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashNumber
|
2002-11-16 02:33:49 +00:00
|
|
|
hashKey(PLDHashTable* table, const void* key)
|
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
const nsHashKey* hashKey = static_cast<const nsHashKey*>(key);
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2002-11-16 02:33:49 +00:00
|
|
|
return hashKey->HashCode();
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static void
|
2002-11-16 02:33:49 +00:00
|
|
|
clearHashEntry(PLDHashTable* table, PLDHashEntryHdr* entry)
|
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
HTEntry* hashEntry = static_cast<HTEntry*>(entry);
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2002-11-16 02:33:49 +00:00
|
|
|
// leave it up to the nsHashKey destructor to free the "value"
|
|
|
|
delete hashEntry->key;
|
2012-07-30 14:20:58 +00:00
|
|
|
hashEntry->key = nullptr;
|
|
|
|
hashEntry->value = nullptr; // probably not necessary, but for
|
2002-11-16 02:33:49 +00:00
|
|
|
// sanity's sake
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-04 15:10:37 +00:00
|
|
|
static const PLDHashTableOps hashtableOps = {
|
2002-11-16 02:33:49 +00:00
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
|
|
|
hashKey,
|
|
|
|
matchKeyEntry,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
clearHashEntry,
|
|
|
|
PL_DHashFinalizeStub,
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr,
|
1998-03-28 02:44:41 +00:00
|
|
|
};
|
|
|
|
|
2002-11-16 02:33:49 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
//
|
|
|
|
// Enumerator callback
|
|
|
|
//
|
|
|
|
|
1998-09-01 00:16:47 +00:00
|
|
|
struct _HashEnumerateArgs {
|
2001-07-31 19:05:34 +00:00
|
|
|
nsHashtableEnumFunc fn;
|
|
|
|
void* arg;
|
1998-09-01 00:16:47 +00:00
|
|
|
};
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashOperator
|
2012-08-22 15:56:38 +00:00
|
|
|
hashEnumerate(PLDHashTable* table, PLDHashEntryHdr* hdr, uint32_t i, void *arg)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2001-07-31 19:05:34 +00:00
|
|
|
_HashEnumerateArgs* thunk = (_HashEnumerateArgs*)arg;
|
2007-07-08 07:08:04 +00:00
|
|
|
HTEntry* entry = static_cast<HTEntry*>(hdr);
|
2002-11-16 02:33:49 +00:00
|
|
|
|
2011-08-23 02:15:54 +00:00
|
|
|
if (thunk->fn(entry->key, entry->value, thunk->arg))
|
2002-11-16 02:33:49 +00:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
return PL_DHASH_STOP;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
1998-05-22 21:56:30 +00:00
|
|
|
//
|
2001-07-31 19:05:34 +00:00
|
|
|
// HashKey
|
1998-05-22 21:56:30 +00:00
|
|
|
//
|
2000-05-15 13:10:18 +00:00
|
|
|
|
1998-05-22 21:56:30 +00:00
|
|
|
nsHashKey::~nsHashKey(void)
|
|
|
|
{
|
2001-07-31 19:05:34 +00:00
|
|
|
MOZ_COUNT_DTOR(nsHashKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHashKey::Write(nsIObjectOutputStream* aStream) const
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("oops");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
1998-05-22 21:56:30 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
nsHashtable::nsHashtable(uint32_t aInitSize, bool threadSafe)
|
2011-10-17 14:59:28 +00:00
|
|
|
: mLock(NULL), mEnumerating(false)
|
1999-03-30 08:15:39 +00:00
|
|
|
{
|
2001-07-31 19:05:34 +00:00
|
|
|
MOZ_COUNT_CTOR(nsHashtable);
|
2002-11-16 02:33:49 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
bool result = PL_DHashTableInit(&mHashtable, &hashtableOps, nullptr,
|
2002-11-16 02:33:49 +00:00
|
|
|
sizeof(HTEntry), aInitSize);
|
|
|
|
|
|
|
|
NS_ASSERTION(result, "Hashtable failed to initialize");
|
|
|
|
|
|
|
|
// make sure we detect this later
|
|
|
|
if (!result)
|
2012-07-30 14:20:58 +00:00
|
|
|
mHashtable.ops = nullptr;
|
2002-11-16 02:33:49 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
if (threadSafe) {
|
|
|
|
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);
|
|
|
|
}
|
1999-03-30 08:15:39 +00:00
|
|
|
}
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2000-08-20 21:29:10 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
nsHashtable::~nsHashtable() {
|
2001-07-31 19:05:34 +00:00
|
|
|
MOZ_COUNT_DTOR(nsHashtable);
|
2002-11-16 02:33:49 +00:00
|
|
|
if (mHashtable.ops)
|
|
|
|
PL_DHashTableFinish(&mHashtable);
|
2001-07-31 19:05:34 +00:00
|
|
|
if (mLock) PR_DestroyLock(mLock);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool nsHashtable::Exists(nsHashKey *aKey)
|
1999-03-19 05:51:49 +00:00
|
|
|
{
|
2001-07-31 19:05:34 +00:00
|
|
|
if (mLock) PR_Lock(mLock);
|
1999-03-30 08:15:39 +00:00
|
|
|
|
2009-08-12 09:49:53 +00:00
|
|
|
if (!mHashtable.ops) {
|
|
|
|
if (mLock) PR_Unlock(mLock);
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2009-08-12 09:49:53 +00:00
|
|
|
}
|
2002-11-16 02:33:49 +00:00
|
|
|
|
|
|
|
PLDHashEntryHdr *entry =
|
|
|
|
PL_DHashTableOperate(&mHashtable, aKey, PL_DHASH_LOOKUP);
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool exists = PL_DHASH_ENTRY_IS_BUSY(entry);
|
2002-11-16 02:33:49 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
1999-03-30 08:15:39 +00:00
|
|
|
|
2002-11-16 02:33:49 +00:00
|
|
|
return exists;
|
1999-03-19 05:51:49 +00:00
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
void *nsHashtable::Put(nsHashKey *aKey, void *aData)
|
|
|
|
{
|
|
|
|
void *res = NULL;
|
1999-03-30 08:15:39 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
if (!mHashtable.ops) return nullptr;
|
2002-11-16 02:33:49 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
if (mLock) PR_Lock(mLock);
|
1999-03-30 08:15:39 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
// shouldn't be adding an item during enumeration
|
|
|
|
PR_ASSERT(!mEnumerating);
|
2002-11-16 02:33:49 +00:00
|
|
|
|
|
|
|
HTEntry* entry =
|
2007-07-08 07:08:04 +00:00
|
|
|
static_cast<HTEntry*>
|
|
|
|
(PL_DHashTableOperate(&mHashtable, aKey, PL_DHASH_ADD));
|
2002-11-16 02:33:49 +00:00
|
|
|
|
|
|
|
if (entry) { // don't return early, or you'll be locked!
|
|
|
|
if (entry->key) {
|
|
|
|
// existing entry, need to boot the old value
|
|
|
|
res = entry->value;
|
|
|
|
entry->value = aData;
|
|
|
|
} else {
|
|
|
|
// new entry (leave res == null)
|
|
|
|
entry->key = aKey->Clone();
|
|
|
|
entry->value = aData;
|
2001-07-31 19:05:34 +00:00
|
|
|
}
|
2000-08-10 06:19:37 +00:00
|
|
|
}
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
1999-03-30 08:15:39 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
return res;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
void *nsHashtable::Get(nsHashKey *aKey)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
if (!mHashtable.ops) return nullptr;
|
2002-11-16 02:33:49 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
if (mLock) PR_Lock(mLock);
|
1999-03-30 08:15:39 +00:00
|
|
|
|
2002-11-16 02:33:49 +00:00
|
|
|
HTEntry* entry =
|
2007-07-08 07:08:04 +00:00
|
|
|
static_cast<HTEntry*>
|
|
|
|
(PL_DHashTableOperate(&mHashtable, aKey, PL_DHASH_LOOKUP));
|
2012-07-30 14:20:58 +00:00
|
|
|
void *ret = PL_DHASH_ENTRY_IS_BUSY(entry) ? entry->value : nullptr;
|
2002-11-16 02:33:49 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
1999-03-30 08:15:39 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
return ret;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
void *nsHashtable::Remove(nsHashKey *aKey)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
if (!mHashtable.ops) return nullptr;
|
2002-11-16 02:33:49 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
if (mLock) PR_Lock(mLock);
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
// shouldn't be adding an item during enumeration
|
|
|
|
PR_ASSERT(!mEnumerating);
|
1999-03-30 08:15:39 +00:00
|
|
|
|
2002-11-16 02:33:49 +00:00
|
|
|
|
|
|
|
// need to see if the entry is actually there, in order to get the
|
|
|
|
// old value for the result
|
|
|
|
HTEntry* entry =
|
2007-07-08 07:08:04 +00:00
|
|
|
static_cast<HTEntry*>
|
|
|
|
(PL_DHashTableOperate(&mHashtable, aKey, PL_DHASH_LOOKUP));
|
2002-11-16 02:33:49 +00:00
|
|
|
void *res;
|
|
|
|
|
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
|
|
|
// value wasn't in the table anyway
|
2012-07-30 14:20:58 +00:00
|
|
|
res = nullptr;
|
2002-11-16 02:33:49 +00:00
|
|
|
} else {
|
|
|
|
res = entry->value;
|
|
|
|
PL_DHashTableRawRemove(&mHashtable, entry);
|
2001-07-31 19:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
|
|
|
return res;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
1999-07-28 07:56:28 +00: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.
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashOperator
|
2002-11-16 02:33:49 +00:00
|
|
|
hashEnumerateShare(PLDHashTable *table, PLDHashEntryHdr *hdr,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t i, void *arg)
|
1998-06-01 22:11:06 +00:00
|
|
|
{
|
2001-07-31 19:05:34 +00:00
|
|
|
nsHashtable *newHashtable = (nsHashtable *)arg;
|
2007-07-08 07:08:04 +00:00
|
|
|
HTEntry * entry = static_cast<HTEntry*>(hdr);
|
2002-11-16 02:33:49 +00:00
|
|
|
|
|
|
|
newHashtable->Put(entry->key, entry->value);
|
|
|
|
return PL_DHASH_NEXT;
|
1998-06-01 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
nsHashtable * nsHashtable::Clone()
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
if (!mHashtable.ops) return nullptr;
|
2002-11-16 02:33:49 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
bool threadSafe = (mLock != nullptr);
|
2002-11-16 02:33:49 +00:00
|
|
|
nsHashtable *newHashTable = new nsHashtable(mHashtable.entryCount, threadSafe);
|
1999-03-30 08:15:39 +00:00
|
|
|
|
2002-11-16 02:33:49 +00:00
|
|
|
PL_DHashTableEnumerate(&mHashtable, hashEnumerateShare, newHashTable);
|
2001-07-31 19:05:34 +00:00
|
|
|
return newHashTable;
|
1998-06-01 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
void nsHashtable::Enumerate(nsHashtableEnumFunc aEnumFunc, void* aClosure)
|
|
|
|
{
|
2002-11-16 02:33:49 +00:00
|
|
|
if (!mHashtable.ops) return;
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool wasEnumerating = mEnumerating;
|
2011-10-17 14:59:28 +00:00
|
|
|
mEnumerating = true;
|
2001-07-31 19:05:34 +00:00
|
|
|
_HashEnumerateArgs thunk;
|
|
|
|
thunk.fn = aEnumFunc;
|
|
|
|
thunk.arg = aClosure;
|
2002-11-16 02:33:49 +00:00
|
|
|
PL_DHashTableEnumerate(&mHashtable, hashEnumerate, &thunk);
|
2001-07-31 19:05:34 +00:00
|
|
|
mEnumerating = wasEnumerating;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
1999-02-03 19:35:11 +00:00
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashOperator
|
2012-08-22 15:56:38 +00:00
|
|
|
hashEnumerateRemove(PLDHashTable*, PLDHashEntryHdr* hdr, uint32_t i, void *arg)
|
1999-02-03 19:35:11 +00:00
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
HTEntry* entry = static_cast<HTEntry*>(hdr);
|
2001-07-31 19:05:34 +00:00
|
|
|
_HashEnumerateArgs* thunk = (_HashEnumerateArgs*)arg;
|
|
|
|
if (thunk) {
|
2002-11-16 02:33:49 +00:00
|
|
|
return thunk->fn(entry->key, entry->value, thunk->arg)
|
|
|
|
? PL_DHASH_REMOVE
|
|
|
|
: PL_DHASH_STOP;
|
2001-07-31 19:05:34 +00:00
|
|
|
}
|
2002-11-16 02:33:49 +00:00
|
|
|
return PL_DHASH_REMOVE;
|
1999-02-03 19:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsHashtable::Reset() {
|
2001-07-31 19:05:34 +00:00
|
|
|
Reset(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsHashtable::Reset(nsHashtableEnumFunc destroyFunc, void* aClosure)
|
|
|
|
{
|
2002-11-16 02:33:49 +00:00
|
|
|
if (!mHashtable.ops) return;
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
_HashEnumerateArgs thunk, *thunkp;
|
|
|
|
if (!destroyFunc) {
|
2012-07-30 14:20:58 +00:00
|
|
|
thunkp = nullptr;
|
2001-07-31 19:05:34 +00:00
|
|
|
} else {
|
|
|
|
thunkp = &thunk;
|
|
|
|
thunk.fn = destroyFunc;
|
|
|
|
thunk.arg = aClosure;
|
|
|
|
}
|
2002-11-16 02:33:49 +00:00
|
|
|
PL_DHashTableEnumerate(&mHashtable, hashEnumerateRemove, thunkp);
|
2001-07-31 19:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsISerializable helpers
|
|
|
|
|
|
|
|
nsHashtable::nsHashtable(nsIObjectInputStream* aStream,
|
|
|
|
nsHashtableReadEntryFunc aReadEntryFunc,
|
|
|
|
nsHashtableFreeEntryFunc aFreeEntryFunc,
|
|
|
|
nsresult *aRetVal)
|
2012-07-30 14:20:58 +00:00
|
|
|
: mLock(nullptr),
|
2011-10-17 14:59:28 +00:00
|
|
|
mEnumerating(false)
|
2001-07-31 19:05:34 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsHashtable);
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool threadSafe;
|
2001-07-31 19:05:34 +00:00
|
|
|
nsresult rv = aStream->ReadBoolean(&threadSafe);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (threadSafe) {
|
|
|
|
mLock = PR_NewLock();
|
|
|
|
if (!mLock)
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count;
|
2001-07-31 19:05:34 +00:00
|
|
|
rv = aStream->Read32(&count);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool status =
|
2002-11-16 02:33:49 +00:00
|
|
|
PL_DHashTableInit(&mHashtable, &hashtableOps,
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr, sizeof(HTEntry), count);
|
2002-11-16 02:33:49 +00:00
|
|
|
if (!status) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mHashtable.ops = nullptr;
|
2001-07-31 19:05:34 +00:00
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else {
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2001-07-31 19:05:34 +00:00
|
|
|
nsHashKey* key;
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
rv = aReadEntryFunc(aStream, &key, &data);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-10-07 21:00:01 +00:00
|
|
|
Put(key, data);
|
|
|
|
|
|
|
|
// XXXbe must we clone key? can't we hand off
|
2012-07-30 14:20:58 +00:00
|
|
|
aFreeEntryFunc(aStream, key, nullptr);
|
2001-07-31 19:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*aRetVal = rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WriteEntryArgs {
|
|
|
|
nsIObjectOutputStream* mStream;
|
|
|
|
nsHashtableWriteDataFunc mWriteDataFunc;
|
|
|
|
nsresult mRetVal;
|
|
|
|
};
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool
|
2001-07-31 19:05:34 +00:00
|
|
|
WriteEntry(nsHashKey *aKey, void *aData, void* aClosure)
|
|
|
|
{
|
|
|
|
WriteEntryArgs* args = (WriteEntryArgs*) aClosure;
|
|
|
|
nsIObjectOutputStream* stream = args->mStream;
|
|
|
|
|
|
|
|
nsresult rv = aKey->Write(stream);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = args->mWriteDataFunc(stream, aData);
|
|
|
|
|
|
|
|
args->mRetVal = rv;
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2001-07-31 19:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHashtable::Write(nsIObjectOutputStream* aStream,
|
|
|
|
nsHashtableWriteDataFunc aWriteDataFunc) const
|
|
|
|
{
|
2002-11-16 02:33:49 +00:00
|
|
|
if (!mHashtable.ops)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2012-07-30 14:20:58 +00:00
|
|
|
bool threadSafe = (mLock != nullptr);
|
2001-07-31 19:05:34 +00:00
|
|
|
nsresult rv = aStream->WriteBoolean(threadSafe);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Write the entry count first, so we know how many key/value pairs to read.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count = mHashtable.entryCount;
|
2001-07-31 19:05:34 +00:00
|
|
|
rv = aStream->Write32(count);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Write all key/value pairs in the table.
|
|
|
|
WriteEntryArgs args = {aStream, aWriteDataFunc};
|
2007-07-08 07:08:04 +00:00
|
|
|
const_cast<nsHashtable*>(this)->Enumerate(WriteEntry, (void*) &args);
|
2001-07-31 19:05:34 +00:00
|
|
|
return args.mRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsISupportsKey::nsISupportsKey(nsIObjectInputStream* aStream, nsresult *aResult)
|
2012-07-30 14:20:58 +00:00
|
|
|
: mKey(nullptr)
|
2001-07-31 19:05:34 +00:00
|
|
|
{
|
2011-09-29 06:19:26 +00:00
|
|
|
bool nonnull;
|
2001-07-31 19:05:34 +00:00
|
|
|
nsresult rv = aStream->ReadBoolean(&nonnull);
|
|
|
|
if (NS_SUCCEEDED(rv) && nonnull)
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = aStream->ReadObject(true, &mKey);
|
2001-07-31 19:05:34 +00:00
|
|
|
*aResult = rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsISupportsKey::Write(nsIObjectOutputStream* aStream) const
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
bool nonnull = (mKey != nullptr);
|
2001-07-31 19:05:34 +00:00
|
|
|
nsresult rv = aStream->WriteBoolean(nonnull);
|
|
|
|
if (NS_SUCCEEDED(rv) && nonnull)
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = aStream->WriteObject(mKey, true);
|
2001-07-31 19:05:34 +00:00
|
|
|
return rv;
|
1999-07-31 05:41:54 +00:00
|
|
|
}
|
|
|
|
|
2002-03-01 09:28:55 +00:00
|
|
|
// Copy Constructor
|
|
|
|
// We need to free mStr if the object is passed with mOwnership as OWN. As the
|
|
|
|
// destructor here is freeing mStr in that case, mStr is NOT getting leaked here.
|
|
|
|
|
|
|
|
nsCStringKey::nsCStringKey(const nsCStringKey& aKey)
|
|
|
|
: mStr(aKey.mStr), mStrLen(aKey.mStrLen), mOwnership(aKey.mOwnership)
|
|
|
|
{
|
|
|
|
if (mOwnership != NEVER_OWN) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t len = mStrLen * sizeof(char);
|
2007-07-08 07:08:04 +00:00
|
|
|
char* str = reinterpret_cast<char*>(nsMemory::Alloc(len + sizeof(char)));
|
2002-03-01 09:28:55 +00:00
|
|
|
if (!str) {
|
|
|
|
// Pray we don't dangle!
|
|
|
|
mOwnership = NEVER_OWN;
|
|
|
|
} else {
|
|
|
|
// Use memcpy in case there are embedded NULs.
|
|
|
|
memcpy(str, mStr, len);
|
|
|
|
str[mStrLen] = '\0';
|
|
|
|
mStr = str;
|
|
|
|
mOwnership = OWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = CStringKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsCStringKey);
|
|
|
|
}
|
|
|
|
|
2001-06-29 23:08:23 +00:00
|
|
|
nsCStringKey::nsCStringKey(const nsAFlatCString& str)
|
2007-07-08 07:08:04 +00:00
|
|
|
: mStr(const_cast<char*>(str.get())),
|
2001-06-29 23:08:23 +00:00
|
|
|
mStrLen(str.Length()),
|
|
|
|
mOwnership(OWN_CLONE)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mStr, "null string key");
|
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = CStringKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsCStringKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCStringKey::nsCStringKey(const nsACString& str)
|
|
|
|
: mStr(ToNewCString(str)),
|
|
|
|
mStrLen(str.Length()),
|
|
|
|
mOwnership(OWN)
|
1999-03-19 06:18:08 +00:00
|
|
|
{
|
2000-08-10 06:19:37 +00:00
|
|
|
NS_ASSERTION(mStr, "null string key");
|
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = CStringKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsCStringKey);
|
1999-07-22 02:11:04 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
nsCStringKey::nsCStringKey(const char* str, int32_t strLen, Ownership own)
|
2000-08-16 00:32:59 +00:00
|
|
|
: mStr((char*)str), mStrLen(strLen), mOwnership(own)
|
1999-07-22 02:11:04 +00:00
|
|
|
{
|
2000-08-10 06:19:37 +00:00
|
|
|
NS_ASSERTION(mStr, "null string key");
|
2012-08-22 15:56:38 +00:00
|
|
|
if (mStrLen == uint32_t(-1))
|
2002-02-20 06:38:38 +00:00
|
|
|
mStrLen = strlen(str);
|
2000-08-10 06:19:37 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = CStringKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsCStringKey);
|
1999-07-22 02:11:04 +00:00
|
|
|
}
|
|
|
|
|
2000-08-10 06:19:37 +00:00
|
|
|
nsCStringKey::~nsCStringKey(void)
|
|
|
|
{
|
2000-08-16 00:32:59 +00:00
|
|
|
if (mOwnership == OWN)
|
2000-08-10 06:19:37 +00:00
|
|
|
nsMemory::Free(mStr);
|
|
|
|
MOZ_COUNT_DTOR(nsCStringKey);
|
2000-03-12 11:10:07 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t
|
2000-08-10 06:19:37 +00:00
|
|
|
nsCStringKey::HashCode(void) const
|
|
|
|
{
|
2012-03-12 22:53:18 +00:00
|
|
|
return HashString(mStr, mStrLen);
|
1999-03-19 06:18:08 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2000-08-10 06:19:37 +00:00
|
|
|
nsCStringKey::Equals(const nsHashKey* aKey) const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aKey->GetKeyType() == CStringKey, "mismatched key types");
|
|
|
|
nsCStringKey* other = (nsCStringKey*)aKey;
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_ASSERTION(mStrLen != uint32_t(-1), "never called HashCode");
|
|
|
|
NS_ASSERTION(other->mStrLen != uint32_t(-1), "never called HashCode");
|
2000-08-10 06:19:37 +00:00
|
|
|
if (mStrLen != other->mStrLen)
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2002-01-29 21:30:32 +00:00
|
|
|
return memcmp(mStr, other->mStr, mStrLen * sizeof(char)) == 0;
|
2000-08-10 06:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHashKey*
|
|
|
|
nsCStringKey::Clone() const
|
|
|
|
{
|
2000-08-16 00:32:59 +00: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
|
2001-07-31 19:05:34 +00:00
|
|
|
// clone string is zero terminated, but don't assume that the source
|
2000-08-16 00:32:59 +00:00
|
|
|
// string was so terminated.
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t len = mStrLen * sizeof(char);
|
2000-08-16 00:32:59 +00:00
|
|
|
char* str = (char*)nsMemory::Alloc(len + sizeof(char));
|
2000-08-10 06:19:37 +00:00
|
|
|
if (str == NULL)
|
|
|
|
return NULL;
|
2002-01-12 03:18:55 +00:00
|
|
|
memcpy(str, mStr, len);
|
2000-08-16 00:32:59 +00:00
|
|
|
str[len] = 0;
|
|
|
|
return new nsCStringKey(str, mStrLen, OWN);
|
2000-08-10 06:19:37 +00:00
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
nsCStringKey::nsCStringKey(nsIObjectInputStream* aStream, nsresult *aResult)
|
2012-07-30 14:20:58 +00:00
|
|
|
: mStr(nullptr), mStrLen(0), mOwnership(OWN)
|
2001-07-31 19:05:34 +00:00
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString str;
|
2002-11-14 18:16:31 +00:00
|
|
|
nsresult rv = aStream->ReadCString(str);
|
|
|
|
mStr = ToNewCString(str);
|
2001-07-31 19:05:34 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-11-14 18:16:31 +00:00
|
|
|
mStrLen = str.Length();
|
2001-07-31 19:05:34 +00:00
|
|
|
*aResult = rv;
|
|
|
|
MOZ_COUNT_CTOR(nsCStringKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCStringKey::Write(nsIObjectOutputStream* aStream) const
|
|
|
|
{
|
|
|
|
return aStream->WriteStringZ(mStr);
|
|
|
|
}
|
|
|
|
|
2000-08-10 06:19:37 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2002-03-01 09:28:55 +00:00
|
|
|
// Copy Constructor
|
|
|
|
// We need to free mStr if the object is passed with mOwnership as OWN. As the
|
|
|
|
// destructor here is freeing mStr in that case, mStr is NOT getting leaked here.
|
|
|
|
|
|
|
|
nsStringKey::nsStringKey(const nsStringKey& aKey)
|
|
|
|
: mStr(aKey.mStr), mStrLen(aKey.mStrLen), mOwnership(aKey.mOwnership)
|
|
|
|
{
|
|
|
|
if (mOwnership != NEVER_OWN) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t len = mStrLen * sizeof(PRUnichar);
|
2007-07-08 07:08:04 +00:00
|
|
|
PRUnichar* str = reinterpret_cast<PRUnichar*>(nsMemory::Alloc(len + sizeof(PRUnichar)));
|
2002-03-01 09:28:55 +00:00
|
|
|
if (!str) {
|
|
|
|
// Pray we don't dangle!
|
|
|
|
mOwnership = NEVER_OWN;
|
|
|
|
} else {
|
|
|
|
// Use memcpy in case there are embedded NULs.
|
|
|
|
memcpy(str, mStr, len);
|
|
|
|
str[mStrLen] = 0;
|
|
|
|
mStr = str;
|
|
|
|
mOwnership = OWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = StringKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsStringKey);
|
|
|
|
}
|
|
|
|
|
2001-06-29 23:08:23 +00:00
|
|
|
nsStringKey::nsStringKey(const nsAFlatString& str)
|
2007-07-08 07:08:04 +00:00
|
|
|
: mStr(const_cast<PRUnichar*>(str.get())),
|
2001-06-29 23:08:23 +00:00
|
|
|
mStrLen(str.Length()),
|
|
|
|
mOwnership(OWN_CLONE)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mStr, "null string key");
|
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = StringKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsStringKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsStringKey::nsStringKey(const nsAString& str)
|
|
|
|
: mStr(ToNewUnicode(str)),
|
|
|
|
mStrLen(str.Length()),
|
2001-08-01 22:34:42 +00:00
|
|
|
mOwnership(OWN)
|
2000-08-10 06:19:37 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mStr, "null string key");
|
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = StringKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsStringKey);
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStringKey::nsStringKey(const PRUnichar* str, int32_t strLen, Ownership own)
|
2000-08-16 00:32:59 +00:00
|
|
|
: mStr((PRUnichar*)str), mStrLen(strLen), mOwnership(own)
|
1999-03-19 06:18:08 +00:00
|
|
|
{
|
2000-08-10 06:19:37 +00:00
|
|
|
NS_ASSERTION(mStr, "null string key");
|
2012-08-22 15:56:38 +00:00
|
|
|
if (mStrLen == uint32_t(-1))
|
2012-05-30 17:43:39 +00:00
|
|
|
mStrLen = NS_strlen(str);
|
2000-08-10 06:19:37 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = StringKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsStringKey);
|
1999-03-19 06:18:08 +00:00
|
|
|
}
|
|
|
|
|
2000-08-10 06:19:37 +00:00
|
|
|
nsStringKey::~nsStringKey(void)
|
1999-03-19 06:18:08 +00:00
|
|
|
{
|
2000-08-16 00:32:59 +00:00
|
|
|
if (mOwnership == OWN)
|
2000-08-10 06:19:37 +00:00
|
|
|
nsMemory::Free(mStr);
|
|
|
|
MOZ_COUNT_DTOR(nsStringKey);
|
1999-03-19 06:18:08 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t
|
2000-08-10 06:19:37 +00:00
|
|
|
nsStringKey::HashCode(void) const
|
1999-03-19 06:18:08 +00:00
|
|
|
{
|
2012-03-12 22:53:18 +00:00
|
|
|
return HashString(mStr, mStrLen);
|
1999-03-19 06:18:08 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2000-08-10 06:19:37 +00:00
|
|
|
nsStringKey::Equals(const nsHashKey* aKey) const
|
1999-03-19 06:18:08 +00:00
|
|
|
{
|
2000-08-10 06:19:37 +00:00
|
|
|
NS_ASSERTION(aKey->GetKeyType() == StringKey, "mismatched key types");
|
|
|
|
nsStringKey* other = (nsStringKey*)aKey;
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_ASSERTION(mStrLen != uint32_t(-1), "never called HashCode");
|
|
|
|
NS_ASSERTION(other->mStrLen != uint32_t(-1), "never called HashCode");
|
2000-08-10 06:19:37 +00:00
|
|
|
if (mStrLen != other->mStrLen)
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2002-01-29 21:30:32 +00:00
|
|
|
return memcmp(mStr, other->mStr, mStrLen * sizeof(PRUnichar)) == 0;
|
1999-03-19 06:18:08 +00:00
|
|
|
}
|
|
|
|
|
2000-08-10 06:19:37 +00:00
|
|
|
nsHashKey*
|
|
|
|
nsStringKey::Clone() const
|
1999-08-09 05:02:25 +00:00
|
|
|
{
|
2000-08-16 00:32:59 +00:00
|
|
|
if (mOwnership == NEVER_OWN)
|
|
|
|
return new nsStringKey(mStr, mStrLen, NEVER_OWN);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t len = (mStrLen+1) * sizeof(PRUnichar);
|
2000-08-10 06:19:37 +00:00
|
|
|
PRUnichar* str = (PRUnichar*)nsMemory::Alloc(len);
|
|
|
|
if (str == NULL)
|
|
|
|
return NULL;
|
2002-01-12 03:18:55 +00:00
|
|
|
memcpy(str, mStr, len);
|
2000-08-16 00:32:59 +00:00
|
|
|
return new nsStringKey(str, mStrLen, OWN);
|
2000-08-10 06:19:37 +00:00
|
|
|
}
|
1999-08-09 05:02:25 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
nsStringKey::nsStringKey(nsIObjectInputStream* aStream, nsresult *aResult)
|
2012-07-30 14:20:58 +00:00
|
|
|
: mStr(nullptr), mStrLen(0), mOwnership(OWN)
|
2001-07-31 19:05:34 +00:00
|
|
|
{
|
2002-11-14 18:16:31 +00:00
|
|
|
nsAutoString str;
|
|
|
|
nsresult rv = aStream->ReadString(str);
|
|
|
|
mStr = ToNewUnicode(str);
|
2001-07-31 19:05:34 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-11-14 18:16:31 +00:00
|
|
|
mStrLen = str.Length();
|
2001-07-31 19:05:34 +00:00
|
|
|
*aResult = rv;
|
|
|
|
MOZ_COUNT_CTOR(nsStringKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsStringKey::Write(nsIObjectOutputStream* aStream) const
|
|
|
|
{
|
|
|
|
return aStream->WriteWStringZ(mStr);
|
|
|
|
}
|
|
|
|
|
1999-03-19 06:18:08 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-07-28 07:56:28 +00:00
|
|
|
// nsObjectHashtable: an nsHashtable where the elements are C++ objects to be
|
|
|
|
// deleted
|
|
|
|
|
|
|
|
nsObjectHashtable::nsObjectHashtable(nsHashtableCloneElementFunc cloneElementFun,
|
|
|
|
void* cloneElementClosure,
|
|
|
|
nsHashtableEnumFunc destroyElementFun,
|
|
|
|
void* destroyElementClosure,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aSize, bool threadSafe)
|
1999-07-28 07:56:28 +00:00
|
|
|
: nsHashtable(aSize, threadSafe),
|
|
|
|
mCloneElementFun(cloneElementFun),
|
|
|
|
mCloneElementClosure(cloneElementClosure),
|
|
|
|
mDestroyElementFun(destroyElementFun),
|
|
|
|
mDestroyElementClosure(destroyElementClosure)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsObjectHashtable::~nsObjectHashtable()
|
|
|
|
{
|
1999-07-31 05:50:13 +00:00
|
|
|
Reset();
|
1999-07-28 07:56:28 +00:00
|
|
|
}
|
|
|
|
|
2002-11-16 02:33:49 +00:00
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
PLDHashOperator
|
2002-11-16 02:33:49 +00:00
|
|
|
nsObjectHashtable::CopyElement(PLDHashTable* table,
|
|
|
|
PLDHashEntryHdr* hdr,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t i, void *arg)
|
1999-07-28 07:56:28 +00:00
|
|
|
{
|
2001-07-31 19:05:34 +00:00
|
|
|
nsObjectHashtable *newHashtable = (nsObjectHashtable *)arg;
|
2007-07-08 07:08:04 +00:00
|
|
|
HTEntry *entry = static_cast<HTEntry*>(hdr);
|
2002-11-16 02:33:49 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
void* newElement =
|
2002-11-16 02:33:49 +00:00
|
|
|
newHashtable->mCloneElementFun(entry->key, entry->value,
|
2001-07-31 19:05:34 +00:00
|
|
|
newHashtable->mCloneElementClosure);
|
2012-07-30 14:20:58 +00:00
|
|
|
if (newElement == nullptr)
|
2002-11-16 02:33:49 +00:00
|
|
|
return PL_DHASH_STOP;
|
|
|
|
newHashtable->Put(entry->key, newElement);
|
|
|
|
return PL_DHASH_NEXT;
|
1999-07-28 07:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable*
|
|
|
|
nsObjectHashtable::Clone()
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
if (!mHashtable.ops) return nullptr;
|
2002-11-16 02:33:49 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool threadSafe = false;
|
1999-07-28 07:56:28 +00:00
|
|
|
if (mLock)
|
2011-10-17 14:59:28 +00:00
|
|
|
threadSafe = true;
|
2001-07-31 19:05:34 +00:00
|
|
|
nsObjectHashtable* newHashTable =
|
1999-07-28 07:56:28 +00:00
|
|
|
new nsObjectHashtable(mCloneElementFun, mCloneElementClosure,
|
|
|
|
mDestroyElementFun, mDestroyElementClosure,
|
2002-11-16 02:33:49 +00:00
|
|
|
mHashtable.entryCount, threadSafe);
|
1999-07-28 07:56:28 +00:00
|
|
|
|
2002-11-16 02:33:49 +00:00
|
|
|
PL_DHashTableEnumerate(&mHashtable, CopyElement, newHashTable);
|
1999-07-28 07:56:28 +00:00
|
|
|
return newHashTable;
|
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
void
|
1999-07-28 07:56:28 +00:00
|
|
|
nsObjectHashtable::Reset()
|
|
|
|
{
|
1999-07-31 05:50:13 +00:00
|
|
|
nsHashtable::Reset(mDestroyElementFun, mDestroyElementClosure);
|
1999-07-28 07:56:28 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
1999-08-02 23:55:03 +00:00
|
|
|
nsObjectHashtable::RemoveAndDelete(nsHashKey *aKey)
|
|
|
|
{
|
|
|
|
void *value = Remove(aKey);
|
|
|
|
if (value && mDestroyElementFun)
|
2007-10-19 23:26:52 +00:00
|
|
|
return !!(*mDestroyElementFun)(aKey, value, mDestroyElementClosure);
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
1999-08-02 23:55:03 +00:00
|
|
|
}
|
|
|
|
|
1999-07-28 07:56:28 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsSupportsHashtable: an nsHashtable where the elements are nsISupports*
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2001-07-31 19:05:34 +00:00
|
|
|
nsSupportsHashtable::ReleaseElement(nsHashKey *aKey, void *aData, void* aClosure)
|
1999-07-28 07:56:28 +00:00
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
nsISupports* element = static_cast<nsISupports*>(aData);
|
1999-11-11 20:41:54 +00:00
|
|
|
NS_IF_RELEASE(element);
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
1999-07-28 07:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsSupportsHashtable::~nsSupportsHashtable()
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
Enumerate(ReleaseElement, nullptr);
|
1999-07-28 07:56:28 +00:00
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
// Return true if we overwrote something
|
2000-06-06 22:06:56 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2000-08-10 06:19:37 +00:00
|
|
|
nsSupportsHashtable::Put(nsHashKey *aKey, nsISupports* aData, nsISupports **value)
|
1999-07-28 07:56:28 +00:00
|
|
|
{
|
2000-06-06 22:06:56 +00:00
|
|
|
NS_IF_ADDREF(aData);
|
|
|
|
void *prev = nsHashtable::Put(aKey, aData);
|
2007-07-08 07:08:04 +00:00
|
|
|
nsISupports *old = reinterpret_cast<nsISupports *>(prev);
|
2000-06-06 22:06:56 +00:00
|
|
|
if (value) // pass own the ownership to the caller
|
|
|
|
*value = old;
|
|
|
|
else // the caller doesn't care, we do
|
|
|
|
NS_IF_RELEASE(old);
|
2012-07-30 14:20:58 +00:00
|
|
|
return prev != nullptr;
|
1999-07-28 07:56:28 +00:00
|
|
|
}
|
|
|
|
|
2000-06-06 22:06:56 +00:00
|
|
|
nsISupports *
|
1999-07-28 07:56:28 +00:00
|
|
|
nsSupportsHashtable::Get(nsHashKey *aKey)
|
|
|
|
{
|
|
|
|
void* data = nsHashtable::Get(aKey);
|
1999-09-02 07:01:53 +00:00
|
|
|
if (!data)
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2007-07-08 07:08:04 +00:00
|
|
|
nsISupports* element = reinterpret_cast<nsISupports*>(data);
|
1999-11-11 20:41:54 +00:00
|
|
|
NS_IF_ADDREF(element);
|
2000-06-06 22:06:56 +00:00
|
|
|
return element;
|
1999-07-28 07:56:28 +00:00
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
// Return true if we found something (useful for checks)
|
2000-06-06 22:06:56 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2000-06-06 22:06:56 +00:00
|
|
|
nsSupportsHashtable::Remove(nsHashKey *aKey, nsISupports **value)
|
1999-07-28 07:56:28 +00:00
|
|
|
{
|
|
|
|
void* data = nsHashtable::Remove(aKey);
|
2007-07-08 07:08:04 +00:00
|
|
|
nsISupports* element = static_cast<nsISupports*>(data);
|
2000-06-06 22:06:56 +00:00
|
|
|
if (value) // caller wants it
|
|
|
|
*value = element;
|
|
|
|
else // caller doesn't care, we do
|
2001-07-31 19:05:34 +00:00
|
|
|
NS_IF_RELEASE(element);
|
2012-07-30 14:20:58 +00:00
|
|
|
return data != nullptr;
|
1999-07-28 07:56:28 +00:00
|
|
|
}
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
PLDHashOperator
|
2002-11-16 02:33:49 +00:00
|
|
|
nsSupportsHashtable::EnumerateCopy(PLDHashTable*,
|
|
|
|
PLDHashEntryHdr* hdr,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t i, void *arg)
|
1999-07-28 07:56:28 +00:00
|
|
|
{
|
|
|
|
nsHashtable *newHashtable = (nsHashtable *)arg;
|
2007-07-08 07:08:04 +00:00
|
|
|
HTEntry* entry = static_cast<HTEntry*>(hdr);
|
2002-11-16 02:33:49 +00:00
|
|
|
|
2007-07-08 07:08:04 +00:00
|
|
|
nsISupports* element = static_cast<nsISupports*>(entry->value);
|
1999-11-11 20:41:54 +00:00
|
|
|
NS_IF_ADDREF(element);
|
2002-11-16 02:33:49 +00:00
|
|
|
newHashtable->Put(entry->key, entry->value);
|
|
|
|
return PL_DHASH_NEXT;
|
1999-07-28 07:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable*
|
|
|
|
nsSupportsHashtable::Clone()
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
if (!mHashtable.ops) return nullptr;
|
2002-11-16 02:33:49 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
bool threadSafe = (mLock != nullptr);
|
2001-07-31 19:05:34 +00:00
|
|
|
nsSupportsHashtable* newHashTable =
|
2002-11-16 02:33:49 +00:00
|
|
|
new nsSupportsHashtable(mHashtable.entryCount, threadSafe);
|
1999-07-28 07:56:28 +00:00
|
|
|
|
2002-11-16 02:33:49 +00:00
|
|
|
PL_DHashTableEnumerate(&mHashtable, EnumerateCopy, newHashTable);
|
1999-07-28 07:56:28 +00:00
|
|
|
return newHashTable;
|
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
void
|
1999-07-28 07:56:28 +00:00
|
|
|
nsSupportsHashtable::Reset()
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
Enumerate(ReleaseElement, nullptr);
|
1999-07-28 07:56:28 +00:00
|
|
|
nsHashtable::Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2001-07-31 19:05:34 +00:00
|
|
|
|