2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07: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/.
|
2007-03-22 10:30:00 -07: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "prlog.h"
|
2013-09-19 12:31:21 -07:00
|
|
|
#include "prlock.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsHashtable.h"
|
|
|
|
#include "nsIObjectInputStream.h"
|
|
|
|
#include "nsIObjectOutputStream.h"
|
2013-09-19 12:31:21 -07:00
|
|
|
#include "nsCRTGlue.h"
|
2012-03-12 15:53:18 -07:00
|
|
|
#include "mozilla/HashFunctions.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
struct HTEntry : PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
nsHashKey* key;
|
|
|
|
void* value;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Key operations
|
|
|
|
//
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
matchKeyEntry(PLDHashTable*, const PLDHashEntryHdr* entry,
|
|
|
|
const void* key)
|
|
|
|
{
|
|
|
|
const HTEntry* hashEntry =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<const HTEntry*>(entry);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (hashEntry->key == key)
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
const nsHashKey* otherKey = reinterpret_cast<const nsHashKey*>(key);
|
2007-03-22 10:30:00 -07:00
|
|
|
return otherKey->Equals(hashEntry->key);
|
|
|
|
}
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashNumber
|
2007-03-22 10:30:00 -07:00
|
|
|
hashKey(PLDHashTable* table, const void* key)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
const nsHashKey* hashKey = static_cast<const nsHashKey*>(key);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return hashKey->HashCode();
|
|
|
|
}
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static void
|
2007-03-22 10:30:00 -07:00
|
|
|
clearHashEntry(PLDHashTable* table, PLDHashEntryHdr* entry)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
HTEntry* hashEntry = static_cast<HTEntry*>(entry);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// leave it up to the nsHashKey destructor to free the "value"
|
|
|
|
delete hashEntry->key;
|
2012-07-30 07:20:58 -07:00
|
|
|
hashEntry->key = nullptr;
|
|
|
|
hashEntry->value = nullptr; // probably not necessary, but for
|
2007-03-22 10:30:00 -07:00
|
|
|
// sanity's sake
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const PLDHashTableOps hashtableOps = {
|
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
|
|
|
hashKey,
|
|
|
|
matchKeyEntry,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
clearHashEntry,
|
|
|
|
PL_DHashFinalizeStub,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr,
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Enumerator callback
|
|
|
|
//
|
|
|
|
|
|
|
|
struct _HashEnumerateArgs {
|
|
|
|
nsHashtableEnumFunc fn;
|
|
|
|
void* arg;
|
|
|
|
};
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashOperator
|
2012-08-22 08:56:38 -07:00
|
|
|
hashEnumerate(PLDHashTable* table, PLDHashEntryHdr* hdr, uint32_t i, void *arg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
_HashEnumerateArgs* thunk = (_HashEnumerateArgs*)arg;
|
2007-07-08 00:08:04 -07:00
|
|
|
HTEntry* entry = static_cast<HTEntry*>(hdr);
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2011-08-22 19:15:54 -07:00
|
|
|
if (thunk->fn(entry->key, entry->value, thunk->arg))
|
2007-03-22 10:30:00 -07:00
|
|
|
return PL_DHASH_NEXT;
|
2014-02-06 07:54:53 -08:00
|
|
|
return PL_DHASH_STOP;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// HashKey
|
|
|
|
//
|
|
|
|
|
|
|
|
nsHashKey::~nsHashKey(void)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsHashKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHashKey::Write(nsIObjectOutputStream* aStream) const
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("oops");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2014-02-27 10:04:09 -08:00
|
|
|
nsHashtable::nsHashtable(uint32_t aInitSize, bool aThreadSafe)
|
2013-10-10 13:41:00 -07:00
|
|
|
: mLock(nullptr), mEnumerating(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsHashtable);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
bool result = PL_DHashTableInit(&mHashtable, &hashtableOps, nullptr,
|
2014-02-27 10:04:09 -08:00
|
|
|
sizeof(HTEntry), aInitSize, fallible_t());
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(result, "Hashtable failed to initialize");
|
|
|
|
|
|
|
|
// make sure we detect this later
|
|
|
|
if (!result)
|
2012-07-30 07:20:58 -07:00
|
|
|
mHashtable.ops = nullptr;
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2014-02-27 10:04:09 -08:00
|
|
|
if (aThreadSafe) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mLock = PR_NewLock();
|
2013-10-10 13:41:00 -07:00
|
|
|
if (mLock == nullptr) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Cannot create a lock. If running on a multiprocessing system
|
|
|
|
// we are sure to die.
|
2013-10-10 13:41:00 -07:00
|
|
|
PR_ASSERT(mLock != nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable::~nsHashtable() {
|
|
|
|
MOZ_COUNT_DTOR(nsHashtable);
|
|
|
|
if (mHashtable.ops)
|
|
|
|
PL_DHashTableFinish(&mHashtable);
|
|
|
|
if (mLock) PR_DestroyLock(mLock);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsHashtable::Exists(nsHashKey *aKey)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
2009-08-12 02:49:53 -07:00
|
|
|
if (!mHashtable.ops) {
|
|
|
|
if (mLock) PR_Unlock(mLock);
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-08-12 02:49:53 -07:00
|
|
|
}
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
PLDHashEntryHdr *entry =
|
|
|
|
PL_DHashTableOperate(&mHashtable, aKey, PL_DHASH_LOOKUP);
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool exists = PL_DHASH_ENTRY_IS_BUSY(entry);
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
|
|
|
return exists;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *nsHashtable::Put(nsHashKey *aKey, void *aData)
|
|
|
|
{
|
2013-10-10 13:41:00 -07:00
|
|
|
void *res = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (!mHashtable.ops) return nullptr;
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
|
|
|
// shouldn't be adding an item during enumeration
|
|
|
|
PR_ASSERT(!mEnumerating);
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
HTEntry* entry =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<HTEntry*>
|
|
|
|
(PL_DHashTableOperate(&mHashtable, aKey, PL_DHASH_ADD));
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2007-03-22 10:30:00 -07: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *nsHashtable::Get(nsHashKey *aKey)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
if (!mHashtable.ops) return nullptr;
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
|
|
|
HTEntry* entry =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<HTEntry*>
|
|
|
|
(PL_DHashTableOperate(&mHashtable, aKey, PL_DHASH_LOOKUP));
|
2012-07-30 07:20:58 -07:00
|
|
|
void *ret = PL_DHASH_ENTRY_IS_BUSY(entry) ? entry->value : nullptr;
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *nsHashtable::Remove(nsHashKey *aKey)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
if (!mHashtable.ops) return nullptr;
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
|
|
|
// shouldn't be adding an item during enumeration
|
|
|
|
PR_ASSERT(!mEnumerating);
|
|
|
|
|
|
|
|
|
|
|
|
// need to see if the entry is actually there, in order to get the
|
|
|
|
// old value for the result
|
|
|
|
HTEntry* entry =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<HTEntry*>
|
|
|
|
(PL_DHashTableOperate(&mHashtable, aKey, PL_DHASH_LOOKUP));
|
2007-03-22 10:30:00 -07:00
|
|
|
void *res;
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
|
|
|
// value wasn't in the table anyway
|
2012-07-30 07:20:58 -07:00
|
|
|
res = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
res = entry->value;
|
|
|
|
PL_DHashTableRawRemove(&mHashtable, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 08:04:34 -07:00
|
|
|
static PLDHashOperator
|
2007-03-22 10:30:00 -07:00
|
|
|
hashEnumerateShare(PLDHashTable *table, PLDHashEntryHdr *hdr,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i, void *arg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsHashtable *newHashtable = (nsHashtable *)arg;
|
2007-07-08 00:08:04 -07:00
|
|
|
HTEntry * entry = static_cast<HTEntry*>(hdr);
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
newHashtable->Put(entry->key, entry->value);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable * nsHashtable::Clone()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
if (!mHashtable.ops) return nullptr;
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
bool threadSafe = (mLock != nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHashtable *newHashTable = new nsHashtable(mHashtable.entryCount, threadSafe);
|
|
|
|
|
|
|
|
PL_DHashTableEnumerate(&mHashtable, hashEnumerateShare, newHashTable);
|
|
|
|
return newHashTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsHashtable::Enumerate(nsHashtableEnumFunc aEnumFunc, void* aClosure)
|
|
|
|
{
|
|
|
|
if (!mHashtable.ops) return;
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool wasEnumerating = mEnumerating;
|
2011-10-17 07:59:28 -07:00
|
|
|
mEnumerating = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
_HashEnumerateArgs thunk;
|
|
|
|
thunk.fn = aEnumFunc;
|
|
|
|
thunk.arg = aClosure;
|
|
|
|
PL_DHashTableEnumerate(&mHashtable, hashEnumerate, &thunk);
|
|
|
|
mEnumerating = wasEnumerating;
|
|
|
|
}
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashOperator
|
2012-08-22 08:56:38 -07:00
|
|
|
hashEnumerateRemove(PLDHashTable*, PLDHashEntryHdr* hdr, uint32_t i, void *arg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
HTEntry* entry = static_cast<HTEntry*>(hdr);
|
2007-03-22 10:30:00 -07:00
|
|
|
_HashEnumerateArgs* thunk = (_HashEnumerateArgs*)arg;
|
|
|
|
if (thunk) {
|
|
|
|
return thunk->fn(entry->key, entry->value, thunk->arg)
|
|
|
|
? PL_DHASH_REMOVE
|
|
|
|
: PL_DHASH_STOP;
|
|
|
|
}
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsHashtable::Reset() {
|
2013-10-10 13:41:00 -07:00
|
|
|
Reset(nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsHashtable::Reset(nsHashtableEnumFunc destroyFunc, void* aClosure)
|
|
|
|
{
|
|
|
|
if (!mHashtable.ops) return;
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
_HashEnumerateArgs thunk, *thunkp;
|
|
|
|
if (!destroyFunc) {
|
2012-07-30 07:20:58 -07:00
|
|
|
thunkp = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
thunkp = &thunk;
|
|
|
|
thunk.fn = destroyFunc;
|
|
|
|
thunk.arg = aClosure;
|
|
|
|
}
|
|
|
|
PL_DHashTableEnumerate(&mHashtable, hashEnumerateRemove, thunkp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsISerializable helpers
|
|
|
|
|
|
|
|
nsHashtable::nsHashtable(nsIObjectInputStream* aStream,
|
|
|
|
nsHashtableReadEntryFunc aReadEntryFunc,
|
|
|
|
nsHashtableFreeEntryFunc aFreeEntryFunc,
|
|
|
|
nsresult *aRetVal)
|
2012-07-30 07:20:58 -07:00
|
|
|
: mLock(nullptr),
|
2011-10-17 07:59:28 -07:00
|
|
|
mEnumerating(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsHashtable);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool threadSafe;
|
2007-03-22 10:30:00 -07: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 08:56:38 -07:00
|
|
|
uint32_t count;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = aStream->Read32(&count);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool status =
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHashTableInit(&mHashtable, &hashtableOps,
|
2014-02-27 10:04:09 -08:00
|
|
|
nullptr, sizeof(HTEntry), count,
|
|
|
|
fallible_t());
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!status) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mHashtable.ops = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHashKey* key;
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
rv = aReadEntryFunc(aStream, &key, &data);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-10-07 14:00:01 -07:00
|
|
|
Put(key, data);
|
|
|
|
|
|
|
|
// XXXbe must we clone key? can't we hand off
|
2012-07-30 07:20:58 -07:00
|
|
|
aFreeEntryFunc(aStream, key, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*aRetVal = rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WriteEntryArgs {
|
|
|
|
nsIObjectOutputStream* mStream;
|
|
|
|
nsHashtableWriteDataFunc mWriteDataFunc;
|
|
|
|
nsresult mRetVal;
|
|
|
|
};
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07: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 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHashtable::Write(nsIObjectOutputStream* aStream,
|
|
|
|
nsHashtableWriteDataFunc aWriteDataFunc) const
|
|
|
|
{
|
|
|
|
if (!mHashtable.ops)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2012-07-30 07:20:58 -07:00
|
|
|
bool threadSafe = (mLock != nullptr);
|
2007-03-22 10:30:00 -07: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 08:56:38 -07:00
|
|
|
uint32_t count = mHashtable.entryCount;
|
2007-03-22 10:30:00 -07: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 00:08:04 -07:00
|
|
|
const_cast<nsHashtable*>(this)->Enumerate(WriteEntry, (void*) &args);
|
2007-03-22 10:30:00 -07:00
|
|
|
return args.mRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Copy Constructor
|
2014-02-06 07:54:53 -08:00
|
|
|
// We need to free mStr if the object is passed with mOwnership as OWN. As the
|
2007-03-22 10:30:00 -07:00
|
|
|
// 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 08:56:38 -07:00
|
|
|
uint32_t len = mStrLen * sizeof(char);
|
2007-07-08 00:08:04 -07:00
|
|
|
char* str = reinterpret_cast<char*>(nsMemory::Alloc(len + sizeof(char)));
|
2007-03-22 10:30:00 -07: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCStringKey::nsCStringKey(const nsAFlatCString& str)
|
2007-07-08 00:08:04 -07:00
|
|
|
: mStr(const_cast<char*>(str.get())),
|
2007-03-22 10:30:00 -07: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)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mStr, "null string key");
|
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = CStringKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsCStringKey);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsCStringKey::nsCStringKey(const char* str, int32_t strLen, Ownership own)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mStr((char*)str), mStrLen(strLen), mOwnership(own)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mStr, "null string key");
|
2012-08-22 08:56:38 -07:00
|
|
|
if (mStrLen == uint32_t(-1))
|
2007-03-22 10:30:00 -07:00
|
|
|
mStrLen = strlen(str);
|
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = CStringKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsCStringKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCStringKey::~nsCStringKey(void)
|
|
|
|
{
|
|
|
|
if (mOwnership == OWN)
|
|
|
|
nsMemory::Free(mStr);
|
|
|
|
MOZ_COUNT_DTOR(nsCStringKey);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCStringKey::HashCode(void) const
|
|
|
|
{
|
2012-03-12 15:53:18 -07:00
|
|
|
return HashString(mStr, mStrLen);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCStringKey::Equals(const nsHashKey* aKey) const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aKey->GetKeyType() == CStringKey, "mismatched key types");
|
|
|
|
nsCStringKey* other = (nsCStringKey*)aKey;
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_ASSERTION(mStrLen != uint32_t(-1), "never called HashCode");
|
|
|
|
NS_ASSERTION(other->mStrLen != uint32_t(-1), "never called HashCode");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mStrLen != other->mStrLen)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return memcmp(mStr, other->mStr, mStrLen * sizeof(char)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashKey*
|
|
|
|
nsCStringKey::Clone() const
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len = mStrLen * sizeof(char);
|
2007-03-22 10:30:00 -07:00
|
|
|
char* str = (char*)nsMemory::Alloc(len + sizeof(char));
|
2013-10-10 13:41:00 -07:00
|
|
|
if (str == nullptr)
|
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
memcpy(str, mStr, len);
|
|
|
|
str[len] = 0;
|
|
|
|
return new nsCStringKey(str, mStrLen, OWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCStringKey::nsCStringKey(nsIObjectInputStream* aStream, nsresult *aResult)
|
2012-07-30 07:20:58 -07:00
|
|
|
: mStr(nullptr), mStrLen(0), mOwnership(OWN)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString str;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = aStream->ReadCString(str);
|
|
|
|
mStr = ToNewCString(str);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
mStrLen = str.Length();
|
|
|
|
*aResult = rv;
|
|
|
|
MOZ_COUNT_CTOR(nsCStringKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCStringKey::Write(nsIObjectOutputStream* aStream) const
|
|
|
|
{
|
|
|
|
return aStream->WriteStringZ(mStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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 08:56:38 -07:00
|
|
|
uint32_t aSize, bool threadSafe)
|
2007-03-22 10:30:00 -07:00
|
|
|
: nsHashtable(aSize, threadSafe),
|
|
|
|
mCloneElementFun(cloneElementFun),
|
|
|
|
mCloneElementClosure(cloneElementClosure),
|
|
|
|
mDestroyElementFun(destroyElementFun),
|
|
|
|
mDestroyElementClosure(destroyElementClosure)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsObjectHashtable::~nsObjectHashtable()
|
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
PLDHashOperator
|
2007-03-22 10:30:00 -07:00
|
|
|
nsObjectHashtable::CopyElement(PLDHashTable* table,
|
|
|
|
PLDHashEntryHdr* hdr,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i, void *arg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsObjectHashtable *newHashtable = (nsObjectHashtable *)arg;
|
2007-07-08 00:08:04 -07:00
|
|
|
HTEntry *entry = static_cast<HTEntry*>(hdr);
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void* newElement =
|
|
|
|
newHashtable->mCloneElementFun(entry->key, entry->value,
|
|
|
|
newHashtable->mCloneElementClosure);
|
2012-07-30 07:20:58 -07:00
|
|
|
if (newElement == nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
return PL_DHASH_STOP;
|
|
|
|
newHashtable->Put(entry->key, newElement);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable*
|
|
|
|
nsObjectHashtable::Clone()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
if (!mHashtable.ops) return nullptr;
|
2014-02-06 07:54:53 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool threadSafe = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mLock)
|
2011-10-17 07:59:28 -07:00
|
|
|
threadSafe = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsObjectHashtable* newHashTable =
|
|
|
|
new nsObjectHashtable(mCloneElementFun, mCloneElementClosure,
|
|
|
|
mDestroyElementFun, mDestroyElementClosure,
|
|
|
|
mHashtable.entryCount, threadSafe);
|
|
|
|
|
|
|
|
PL_DHashTableEnumerate(&mHashtable, CopyElement, newHashTable);
|
|
|
|
return newHashTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsObjectHashtable::Reset()
|
|
|
|
{
|
|
|
|
nsHashtable::Reset(mDestroyElementFun, mDestroyElementClosure);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsObjectHashtable::RemoveAndDelete(nsHashKey *aKey)
|
|
|
|
{
|
|
|
|
void *value = Remove(aKey);
|
|
|
|
if (value && mDestroyElementFun)
|
2007-10-19 16:26:52 -07:00
|
|
|
return !!(*mDestroyElementFun)(aKey, value, mDestroyElementClosure);
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|