1999-07-28 11:56:28 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2004-04-18 18:21:17 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1998-03-28 05:44:41 +03:00
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-07-31 23:05:34 +04:00
|
|
|
* Contributor(s):
|
2004-04-18 18:21:17 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK *****
|
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
|
|
|
*/
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
#include <string.h>
|
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"
|
2001-06-30 03:08:23 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2001-07-31 23:05:34 +04:00
|
|
|
#include "nsIObjectInputStream.h"
|
|
|
|
#include "nsIObjectOutputStream.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2002-11-16 05:33:49 +03:00
|
|
|
struct HTEntry : PLDHashEntryHdr
|
2000-08-10 10:19:37 +04:00
|
|
|
{
|
2002-11-16 05:33:49 +03:00
|
|
|
nsHashKey* key;
|
|
|
|
void* value;
|
|
|
|
};
|
2000-08-10 10:19:37 +04:00
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
//
|
|
|
|
// Key operations
|
|
|
|
//
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static PRBool
|
2002-11-16 05:33:49 +03:00
|
|
|
matchKeyEntry(PLDHashTable*, const PLDHashEntryHdr* entry,
|
|
|
|
const void* key)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2002-11-16 05:33:49 +03:00
|
|
|
const HTEntry* hashEntry =
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<const HTEntry*>(entry);
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2002-11-16 05:33:49 +03:00
|
|
|
if (hashEntry->key == key)
|
|
|
|
return PR_TRUE;
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
const nsHashKey* otherKey = reinterpret_cast<const nsHashKey*>(key);
|
2002-11-16 05:33:49 +03:00
|
|
|
return otherKey->Equals(hashEntry->key);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashNumber
|
2002-11-16 05:33:49 +03:00
|
|
|
hashKey(PLDHashTable* table, const void* key)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
const nsHashKey* hashKey = static_cast<const nsHashKey*>(key);
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2002-11-16 05:33:49 +03:00
|
|
|
return hashKey->HashCode();
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static void
|
2002-11-16 05:33:49 +03:00
|
|
|
clearHashEntry(PLDHashTable* table, PLDHashEntryHdr* entry)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
HTEntry* hashEntry = static_cast<HTEntry*>(entry);
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2002-11-16 05:33:49 +03:00
|
|
|
// leave it up to the nsHashKey destructor to free the "value"
|
|
|
|
delete hashEntry->key;
|
|
|
|
hashEntry->key = nsnull;
|
|
|
|
hashEntry->value = nsnull; // probably not necessary, but for
|
|
|
|
// sanity's sake
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-04 19:10:37 +04:00
|
|
|
static const PLDHashTableOps hashtableOps = {
|
2002-11-16 05:33:49 +03:00
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
|
|
|
hashKey,
|
|
|
|
matchKeyEntry,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
clearHashEntry,
|
|
|
|
PL_DHashFinalizeStub,
|
|
|
|
nsnull,
|
1998-03-28 05:44:41 +03:00
|
|
|
};
|
|
|
|
|
2002-11-16 05:33:49 +03:00
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
//
|
|
|
|
// Enumerator callback
|
|
|
|
//
|
|
|
|
|
1998-09-01 04:16:47 +04:00
|
|
|
struct _HashEnumerateArgs {
|
2001-07-31 23:05:34 +04:00
|
|
|
nsHashtableEnumFunc fn;
|
|
|
|
void* arg;
|
1998-09-01 04:16:47 +04:00
|
|
|
};
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashOperator
|
2002-11-16 05:33:49 +03:00
|
|
|
hashEnumerate(PLDHashTable* table, PLDHashEntryHdr* hdr, PRUint32 i, void *arg)
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2001-07-31 23:05:34 +04:00
|
|
|
_HashEnumerateArgs* thunk = (_HashEnumerateArgs*)arg;
|
2007-07-08 11:08:04 +04:00
|
|
|
HTEntry* entry = static_cast<HTEntry*>(hdr);
|
2002-11-16 05:33:49 +03:00
|
|
|
|
|
|
|
switch (thunk->fn(entry->key, entry->value, thunk->arg)) {
|
2002-07-30 02:48:15 +04:00
|
|
|
case kHashEnumerateNext:
|
2002-11-16 05:33:49 +03:00
|
|
|
return PL_DHASH_NEXT;
|
2002-07-30 02:48:15 +04:00
|
|
|
case kHashEnumerateRemove:
|
2002-11-16 05:33:49 +03:00
|
|
|
return PL_DHASH_REMOVE;
|
2002-07-30 02:48:15 +04:00
|
|
|
}
|
2002-11-16 05:33:49 +03:00
|
|
|
return PL_DHASH_STOP;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
1998-05-23 01:56:30 +04:00
|
|
|
//
|
2001-07-31 23:05:34 +04:00
|
|
|
// HashKey
|
1998-05-23 01:56:30 +04:00
|
|
|
//
|
2000-05-15 17:10:18 +04:00
|
|
|
|
1998-05-23 01:56:30 +04:00
|
|
|
nsHashKey::~nsHashKey(void)
|
|
|
|
{
|
2001-07-31 23:05:34 +04:00
|
|
|
MOZ_COUNT_DTOR(nsHashKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHashKey::Write(nsIObjectOutputStream* aStream) const
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("oops");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
1998-05-23 01:56:30 +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
|
|
|
{
|
2001-07-31 23:05:34 +04:00
|
|
|
MOZ_COUNT_CTOR(nsHashtable);
|
2002-11-16 05:33:49 +03:00
|
|
|
|
|
|
|
PRBool result = PL_DHashTableInit(&mHashtable, &hashtableOps, nsnull,
|
|
|
|
sizeof(HTEntry), aInitSize);
|
|
|
|
|
|
|
|
NS_ASSERTION(result, "Hashtable failed to initialize");
|
|
|
|
|
|
|
|
// make sure we detect this later
|
|
|
|
if (!result)
|
|
|
|
mHashtable.ops = nsnull;
|
|
|
|
|
2001-07-31 23:05:34 +04: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 12:15:39 +04:00
|
|
|
}
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2000-08-21 01:29:10 +04:00
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
nsHashtable::~nsHashtable() {
|
2001-07-31 23:05:34 +04:00
|
|
|
MOZ_COUNT_DTOR(nsHashtable);
|
2002-11-16 05:33:49 +03:00
|
|
|
if (mHashtable.ops)
|
|
|
|
PL_DHashTableFinish(&mHashtable);
|
2001-07-31 23:05:34 +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)
|
|
|
|
{
|
2001-07-31 23:05:34 +04:00
|
|
|
if (mLock) PR_Lock(mLock);
|
1999-03-30 12:15:39 +04:00
|
|
|
|
2009-08-12 13:49:53 +04:00
|
|
|
if (!mHashtable.ops) {
|
|
|
|
if (mLock) PR_Unlock(mLock);
|
2002-11-16 05:33:49 +03:00
|
|
|
return PR_FALSE;
|
2009-08-12 13:49:53 +04:00
|
|
|
}
|
2002-11-16 05:33:49 +03:00
|
|
|
|
|
|
|
PLDHashEntryHdr *entry =
|
|
|
|
PL_DHashTableOperate(&mHashtable, aKey, PL_DHASH_LOOKUP);
|
|
|
|
|
|
|
|
PRBool exists = PL_DHASH_ENTRY_IS_BUSY(entry);
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
1999-03-30 12:15:39 +04:00
|
|
|
|
2002-11-16 05:33:49 +03:00
|
|
|
return exists;
|
1999-03-19 08:51:49 +03:00
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
void *nsHashtable::Put(nsHashKey *aKey, void *aData)
|
|
|
|
{
|
|
|
|
void *res = NULL;
|
1999-03-30 12:15:39 +04:00
|
|
|
|
2002-11-16 05:33:49 +03:00
|
|
|
if (!mHashtable.ops) return nsnull;
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
if (mLock) PR_Lock(mLock);
|
1999-03-30 12:15:39 +04:00
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
// shouldn't be adding an item during enumeration
|
|
|
|
PR_ASSERT(!mEnumerating);
|
2002-11-16 05:33:49 +03:00
|
|
|
|
|
|
|
HTEntry* entry =
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<HTEntry*>
|
|
|
|
(PL_DHashTableOperate(&mHashtable, aKey, PL_DHASH_ADD));
|
2002-11-16 05:33:49 +03: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 23:05:34 +04:00
|
|
|
}
|
2000-08-10 10:19:37 +04:00
|
|
|
}
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
1999-03-30 12:15:39 +04:00
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
return res;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
void *nsHashtable::Get(nsHashKey *aKey)
|
|
|
|
{
|
2002-11-16 05:33:49 +03:00
|
|
|
if (!mHashtable.ops) return nsnull;
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
if (mLock) PR_Lock(mLock);
|
1999-03-30 12:15:39 +04:00
|
|
|
|
2002-11-16 05:33:49 +03:00
|
|
|
HTEntry* entry =
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<HTEntry*>
|
|
|
|
(PL_DHashTableOperate(&mHashtable, aKey, PL_DHASH_LOOKUP));
|
2002-11-16 05:33:49 +03:00
|
|
|
void *ret = PL_DHASH_ENTRY_IS_BUSY(entry) ? entry->value : nsnull;
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
1999-03-30 12:15:39 +04:00
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
return ret;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
void *nsHashtable::Remove(nsHashKey *aKey)
|
|
|
|
{
|
2002-11-16 05:33:49 +03:00
|
|
|
if (!mHashtable.ops) return nsnull;
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
if (mLock) PR_Lock(mLock);
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
// shouldn't be adding an item during enumeration
|
|
|
|
PR_ASSERT(!mEnumerating);
|
1999-03-30 12:15:39 +04:00
|
|
|
|
2002-11-16 05:33:49 +03: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 11:08:04 +04:00
|
|
|
static_cast<HTEntry*>
|
|
|
|
(PL_DHashTableOperate(&mHashtable, aKey, PL_DHASH_LOOKUP));
|
2002-11-16 05:33:49 +03:00
|
|
|
void *res;
|
|
|
|
|
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
|
|
|
// value wasn't in the table anyway
|
|
|
|
res = nsnull;
|
|
|
|
} else {
|
|
|
|
res = entry->value;
|
|
|
|
PL_DHashTableRawRemove(&mHashtable, entry);
|
2001-07-31 23:05:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
|
|
|
return res;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
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.
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashOperator
|
2002-11-16 05:33:49 +03:00
|
|
|
hashEnumerateShare(PLDHashTable *table, PLDHashEntryHdr *hdr,
|
|
|
|
PRUint32 i, void *arg)
|
1998-06-02 02:11:06 +04:00
|
|
|
{
|
2001-07-31 23:05:34 +04:00
|
|
|
nsHashtable *newHashtable = (nsHashtable *)arg;
|
2007-07-08 11:08:04 +04:00
|
|
|
HTEntry * entry = static_cast<HTEntry*>(hdr);
|
2002-11-16 05:33:49 +03:00
|
|
|
|
|
|
|
newHashtable->Put(entry->key, entry->value);
|
|
|
|
return PL_DHASH_NEXT;
|
1998-06-02 02:11:06 +04:00
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
nsHashtable * nsHashtable::Clone()
|
|
|
|
{
|
2002-11-16 05:33:49 +03:00
|
|
|
if (!mHashtable.ops) return nsnull;
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
PRBool threadSafe = (mLock != nsnull);
|
2002-11-16 05:33:49 +03:00
|
|
|
nsHashtable *newHashTable = new nsHashtable(mHashtable.entryCount, threadSafe);
|
1999-03-30 12:15:39 +04:00
|
|
|
|
2002-11-16 05:33:49 +03:00
|
|
|
PL_DHashTableEnumerate(&mHashtable, hashEnumerateShare, newHashTable);
|
2001-07-31 23:05:34 +04:00
|
|
|
return newHashTable;
|
1998-06-02 02:11:06 +04:00
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
void nsHashtable::Enumerate(nsHashtableEnumFunc aEnumFunc, void* aClosure)
|
|
|
|
{
|
2002-11-16 05:33:49 +03:00
|
|
|
if (!mHashtable.ops) return;
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
PRBool wasEnumerating = mEnumerating;
|
|
|
|
mEnumerating = PR_TRUE;
|
|
|
|
_HashEnumerateArgs thunk;
|
|
|
|
thunk.fn = aEnumFunc;
|
|
|
|
thunk.arg = aClosure;
|
2002-11-16 05:33:49 +03:00
|
|
|
PL_DHashTableEnumerate(&mHashtable, hashEnumerate, &thunk);
|
2001-07-31 23:05:34 +04:00
|
|
|
mEnumerating = wasEnumerating;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
1999-02-03 22:35:11 +03:00
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashOperator
|
2002-11-16 05:33:49 +03:00
|
|
|
hashEnumerateRemove(PLDHashTable*, PLDHashEntryHdr* hdr, PRUint32 i, void *arg)
|
1999-02-03 22:35:11 +03:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
HTEntry* entry = static_cast<HTEntry*>(hdr);
|
2001-07-31 23:05:34 +04:00
|
|
|
_HashEnumerateArgs* thunk = (_HashEnumerateArgs*)arg;
|
|
|
|
if (thunk) {
|
2002-11-16 05:33:49 +03:00
|
|
|
return thunk->fn(entry->key, entry->value, thunk->arg)
|
|
|
|
? PL_DHASH_REMOVE
|
|
|
|
: PL_DHASH_STOP;
|
2001-07-31 23:05:34 +04:00
|
|
|
}
|
2002-11-16 05:33:49 +03:00
|
|
|
return PL_DHASH_REMOVE;
|
1999-02-03 22:35:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsHashtable::Reset() {
|
2001-07-31 23:05:34 +04:00
|
|
|
Reset(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsHashtable::Reset(nsHashtableEnumFunc destroyFunc, void* aClosure)
|
|
|
|
{
|
2002-11-16 05:33:49 +03:00
|
|
|
if (!mHashtable.ops) return;
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
_HashEnumerateArgs thunk, *thunkp;
|
|
|
|
if (!destroyFunc) {
|
|
|
|
thunkp = nsnull;
|
|
|
|
} else {
|
|
|
|
thunkp = &thunk;
|
|
|
|
thunk.fn = destroyFunc;
|
|
|
|
thunk.arg = aClosure;
|
|
|
|
}
|
2002-11-16 05:33:49 +03:00
|
|
|
PL_DHashTableEnumerate(&mHashtable, hashEnumerateRemove, thunkp);
|
2001-07-31 23:05:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsISerializable helpers
|
|
|
|
|
|
|
|
nsHashtable::nsHashtable(nsIObjectInputStream* aStream,
|
|
|
|
nsHashtableReadEntryFunc aReadEntryFunc,
|
|
|
|
nsHashtableFreeEntryFunc aFreeEntryFunc,
|
|
|
|
nsresult *aRetVal)
|
|
|
|
: mLock(nsnull),
|
|
|
|
mEnumerating(PR_FALSE)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsHashtable);
|
|
|
|
|
|
|
|
PRBool threadSafe;
|
|
|
|
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)) {
|
|
|
|
PRUint32 count;
|
|
|
|
rv = aStream->Read32(&count);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2002-11-16 05:33:49 +03:00
|
|
|
PRBool status =
|
|
|
|
PL_DHashTableInit(&mHashtable, &hashtableOps,
|
|
|
|
nsnull, sizeof(HTEntry), count);
|
|
|
|
if (!status) {
|
|
|
|
mHashtable.ops = nsnull;
|
2001-07-31 23:05:34 +04:00
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else {
|
|
|
|
for (PRUint32 i = 0; i < count; i++) {
|
|
|
|
nsHashKey* key;
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
rv = aReadEntryFunc(aStream, &key, &data);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-10-08 01:00:01 +04:00
|
|
|
Put(key, data);
|
|
|
|
|
|
|
|
// XXXbe must we clone key? can't we hand off
|
|
|
|
aFreeEntryFunc(aStream, key, nsnull);
|
2001-07-31 23:05:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*aRetVal = rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WriteEntryArgs {
|
|
|
|
nsIObjectOutputStream* mStream;
|
|
|
|
nsHashtableWriteDataFunc mWriteDataFunc;
|
|
|
|
nsresult mRetVal;
|
|
|
|
};
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static PRBool
|
2001-07-31 23:05:34 +04: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;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHashtable::Write(nsIObjectOutputStream* aStream,
|
|
|
|
nsHashtableWriteDataFunc aWriteDataFunc) const
|
|
|
|
{
|
2002-11-16 05:33:49 +03:00
|
|
|
if (!mHashtable.ops)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2001-07-31 23:05:34 +04:00
|
|
|
PRBool threadSafe = (mLock != nsnull);
|
|
|
|
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.
|
2002-11-16 05:33:49 +03:00
|
|
|
PRUint32 count = mHashtable.entryCount;
|
2001-07-31 23:05:34 +04: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 11:08:04 +04:00
|
|
|
const_cast<nsHashtable*>(this)->Enumerate(WriteEntry, (void*) &args);
|
2001-07-31 23:05:34 +04:00
|
|
|
return args.mRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsISupportsKey::nsISupportsKey(nsIObjectInputStream* aStream, nsresult *aResult)
|
|
|
|
: mKey(nsnull)
|
|
|
|
{
|
|
|
|
PRBool nonnull;
|
|
|
|
nsresult rv = aStream->ReadBoolean(&nonnull);
|
|
|
|
if (NS_SUCCEEDED(rv) && nonnull)
|
|
|
|
rv = aStream->ReadObject(PR_TRUE, &mKey);
|
|
|
|
*aResult = rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsISupportsKey::Write(nsIObjectOutputStream* aStream) const
|
|
|
|
{
|
|
|
|
PRBool nonnull = (mKey != nsnull);
|
|
|
|
nsresult rv = aStream->WriteBoolean(nonnull);
|
|
|
|
if (NS_SUCCEEDED(rv) && nonnull)
|
|
|
|
rv = aStream->WriteObject(mKey, PR_TRUE);
|
|
|
|
return rv;
|
1999-07-31 09:41:54 +04:00
|
|
|
}
|
|
|
|
|
2002-03-01 12:28:55 +03: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) {
|
|
|
|
PRUint32 len = mStrLen * sizeof(char);
|
2007-07-08 11:08:04 +04:00
|
|
|
char* str = reinterpret_cast<char*>(nsMemory::Alloc(len + sizeof(char)));
|
2002-03-01 12:28:55 +03: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-30 03:08:23 +04:00
|
|
|
nsCStringKey::nsCStringKey(const nsAFlatCString& str)
|
2007-07-08 11:08:04 +04:00
|
|
|
: mStr(const_cast<char*>(str.get())),
|
2001-06-30 03:08:23 +04: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 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");
|
2001-07-31 23:05:34 +04:00
|
|
|
if (mStrLen == PRUint32(-1))
|
2002-02-20 09:38:38 +03:00
|
|
|
mStrLen = strlen(str);
|
2000-08-10 10:19:37 +04:00
|
|
|
#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;
|
2001-07-31 23:05:34 +04:00
|
|
|
NS_ASSERTION(mStrLen != PRUint32(-1), "never called HashCode");
|
|
|
|
NS_ASSERTION(other->mStrLen != PRUint32(-1), "never called HashCode");
|
2000-08-10 10:19:37 +04:00
|
|
|
if (mStrLen != other->mStrLen)
|
|
|
|
return PR_FALSE;
|
2002-01-30 00:30:32 +03:00
|
|
|
return 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
|
2001-07-31 23:05:34 +04:00
|
|
|
// clone string is zero terminated, but don't assume that the source
|
2000-08-16 04:32:59 +04:00
|
|
|
// 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;
|
2002-01-12 06:18:55 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
nsCStringKey::nsCStringKey(nsIObjectInputStream* aStream, nsresult *aResult)
|
|
|
|
: mStr(nsnull), mStrLen(0), mOwnership(OWN)
|
|
|
|
{
|
2002-11-14 21:16:31 +03:00
|
|
|
nsCAutoString str;
|
|
|
|
nsresult rv = aStream->ReadCString(str);
|
|
|
|
mStr = ToNewCString(str);
|
2001-07-31 23:05:34 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-11-14 21:16:31 +03:00
|
|
|
mStrLen = str.Length();
|
2001-07-31 23:05:34 +04:00
|
|
|
*aResult = rv;
|
|
|
|
MOZ_COUNT_CTOR(nsCStringKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCStringKey::Write(nsIObjectOutputStream* aStream) const
|
|
|
|
{
|
|
|
|
return aStream->WriteStringZ(mStr);
|
|
|
|
}
|
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2002-03-01 12:28:55 +03: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) {
|
|
|
|
PRUint32 len = mStrLen * sizeof(PRUnichar);
|
2007-07-08 11:08:04 +04:00
|
|
|
PRUnichar* str = reinterpret_cast<PRUnichar*>(nsMemory::Alloc(len + sizeof(PRUnichar)));
|
2002-03-01 12:28:55 +03: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-30 03:08:23 +04:00
|
|
|
nsStringKey::nsStringKey(const nsAFlatString& str)
|
2007-07-08 11:08:04 +04:00
|
|
|
: mStr(const_cast<PRUnichar*>(str.get())),
|
2001-06-30 03:08:23 +04: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-02 02:34:42 +04:00
|
|
|
mOwnership(OWN)
|
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");
|
2001-07-31 23:05:34 +04:00
|
|
|
if (mStrLen == PRUint32(-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;
|
2001-07-31 23:05:34 +04:00
|
|
|
NS_ASSERTION(mStrLen != PRUint32(-1), "never called HashCode");
|
|
|
|
NS_ASSERTION(other->mStrLen != PRUint32(-1), "never called HashCode");
|
2000-08-10 10:19:37 +04:00
|
|
|
if (mStrLen != other->mStrLen)
|
|
|
|
return PR_FALSE;
|
2002-01-30 00:30:32 +03:00
|
|
|
return 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;
|
2002-01-12 06:18:55 +03:00
|
|
|
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
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
nsStringKey::nsStringKey(nsIObjectInputStream* aStream, nsresult *aResult)
|
|
|
|
: mStr(nsnull), mStrLen(0), mOwnership(OWN)
|
|
|
|
{
|
2002-11-14 21:16:31 +03:00
|
|
|
nsAutoString str;
|
|
|
|
nsresult rv = aStream->ReadString(str);
|
|
|
|
mStr = ToNewUnicode(str);
|
2001-07-31 23:05:34 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-11-14 21:16:31 +03:00
|
|
|
mStrLen = str.Length();
|
2001-07-31 23:05:34 +04:00
|
|
|
*aResult = rv;
|
|
|
|
MOZ_COUNT_CTOR(nsStringKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsStringKey::Write(nsIObjectOutputStream* aStream) const
|
|
|
|
{
|
|
|
|
return aStream->WriteWStringZ(mStr);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-11-16 05:33:49 +03:00
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
PLDHashOperator
|
2002-11-16 05:33:49 +03:00
|
|
|
nsObjectHashtable::CopyElement(PLDHashTable* table,
|
|
|
|
PLDHashEntryHdr* hdr,
|
|
|
|
PRUint32 i, void *arg)
|
1999-07-28 11:56:28 +04:00
|
|
|
{
|
2001-07-31 23:05:34 +04:00
|
|
|
nsObjectHashtable *newHashtable = (nsObjectHashtable *)arg;
|
2007-07-08 11:08:04 +04:00
|
|
|
HTEntry *entry = static_cast<HTEntry*>(hdr);
|
2002-11-16 05:33:49 +03:00
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
void* newElement =
|
2002-11-16 05:33:49 +03:00
|
|
|
newHashtable->mCloneElementFun(entry->key, entry->value,
|
2001-07-31 23:05:34 +04:00
|
|
|
newHashtable->mCloneElementClosure);
|
|
|
|
if (newElement == nsnull)
|
2002-11-16 05:33:49 +03:00
|
|
|
return PL_DHASH_STOP;
|
|
|
|
newHashtable->Put(entry->key, newElement);
|
|
|
|
return PL_DHASH_NEXT;
|
1999-07-28 11:56:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable*
|
|
|
|
nsObjectHashtable::Clone()
|
|
|
|
{
|
2002-11-16 05:33:49 +03:00
|
|
|
if (!mHashtable.ops) return nsnull;
|
|
|
|
|
1999-07-28 11:56:28 +04:00
|
|
|
PRBool threadSafe = PR_FALSE;
|
|
|
|
if (mLock)
|
|
|
|
threadSafe = PR_TRUE;
|
2001-07-31 23:05:34 +04:00
|
|
|
nsObjectHashtable* newHashTable =
|
1999-07-28 11:56:28 +04:00
|
|
|
new nsObjectHashtable(mCloneElementFun, mCloneElementClosure,
|
|
|
|
mDestroyElementFun, mDestroyElementClosure,
|
2002-11-16 05:33:49 +03:00
|
|
|
mHashtable.entryCount, threadSafe);
|
1999-07-28 11:56:28 +04:00
|
|
|
|
2002-11-16 05:33:49 +03:00
|
|
|
PL_DHashTableEnumerate(&mHashtable, CopyElement, newHashTable);
|
1999-07-28 11:56:28 +04:00
|
|
|
return newHashTable;
|
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
void
|
1999-07-28 11:56:28 +04:00
|
|
|
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)
|
2007-10-20 03:26:52 +04:00
|
|
|
return !!(*mDestroyElementFun)(aKey, value, mDestroyElementClosure);
|
2001-07-31 23:05:34 +04:00
|
|
|
return PR_FALSE;
|
1999-08-03 03:55:03 +04:00
|
|
|
}
|
|
|
|
|
1999-07-28 11:56:28 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsSupportsHashtable: an nsHashtable where the elements are nsISupports*
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
PRBool
|
2001-07-31 23:05:34 +04:00
|
|
|
nsSupportsHashtable::ReleaseElement(nsHashKey *aKey, void *aData, void* aClosure)
|
1999-07-28 11:56:28 +04:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsISupports* element = 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
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
// Return true if we overwrote something
|
2000-06-07 02:06:56 +04:00
|
|
|
|
|
|
|
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);
|
2007-07-08 11:08:04 +04:00
|
|
|
nsISupports *old = reinterpret_cast<nsISupports *>(prev);
|
2000-06-07 02:06:56 +04:00
|
|
|
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;
|
2007-07-08 11:08:04 +04:00
|
|
|
nsISupports* element = 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
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
// Return true if we found something (useful for checks)
|
2000-06-07 02:06:56 +04:00
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsSupportsHashtable::Remove(nsHashKey *aKey, nsISupports **value)
|
1999-07-28 11:56:28 +04:00
|
|
|
{
|
|
|
|
void* data = nsHashtable::Remove(aKey);
|
2007-07-08 11:08:04 +04:00
|
|
|
nsISupports* element = 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
|
2001-07-31 23:05:34 +04:00
|
|
|
NS_IF_RELEASE(element);
|
2000-06-07 02:06:56 +04:00
|
|
|
return data != nsnull;
|
1999-07-28 11:56:28 +04:00
|
|
|
}
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
PLDHashOperator
|
2002-11-16 05:33:49 +03:00
|
|
|
nsSupportsHashtable::EnumerateCopy(PLDHashTable*,
|
|
|
|
PLDHashEntryHdr* hdr,
|
|
|
|
PRUint32 i, void *arg)
|
1999-07-28 11:56:28 +04:00
|
|
|
{
|
|
|
|
nsHashtable *newHashtable = (nsHashtable *)arg;
|
2007-07-08 11:08:04 +04:00
|
|
|
HTEntry* entry = static_cast<HTEntry*>(hdr);
|
2002-11-16 05:33:49 +03:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
nsISupports* element = static_cast<nsISupports*>(entry->value);
|
1999-11-11 23:41:54 +03:00
|
|
|
NS_IF_ADDREF(element);
|
2002-11-16 05:33:49 +03:00
|
|
|
newHashtable->Put(entry->key, entry->value);
|
|
|
|
return PL_DHASH_NEXT;
|
1999-07-28 11:56:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable*
|
|
|
|
nsSupportsHashtable::Clone()
|
|
|
|
{
|
2002-11-16 05:33:49 +03:00
|
|
|
if (!mHashtable.ops) return nsnull;
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
PRBool threadSafe = (mLock != nsnull);
|
|
|
|
nsSupportsHashtable* newHashTable =
|
2002-11-16 05:33:49 +03:00
|
|
|
new nsSupportsHashtable(mHashtable.entryCount, threadSafe);
|
1999-07-28 11:56:28 +04:00
|
|
|
|
2002-11-16 05:33:49 +03:00
|
|
|
PL_DHashTableEnumerate(&mHashtable, EnumerateCopy, newHashTable);
|
1999-07-28 11:56:28 +04:00
|
|
|
return newHashTable;
|
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
void
|
1999-07-28 11:56:28 +04:00
|
|
|
nsSupportsHashtable::Reset()
|
|
|
|
{
|
2000-06-07 02:06:56 +04:00
|
|
|
Enumerate(ReleaseElement, nsnull);
|
1999-07-28 11:56:28 +04:00
|
|
|
nsHashtable::Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2001-07-31 23:05:34 +04:00
|
|
|
|