2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2001-10-21 03:19:07 +04:00
|
|
|
// vim:cindent:ts=2:et:sw=2:
|
2001-09-29 00:14:13 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1998-04-14 00:24:54 +04: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/
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1998-04-14 00:24:54 +04: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
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 18:21:17 +04:00
|
|
|
* 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"),
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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
|
2004-04-18 18:21:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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
|
2004-04-18 18:21:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-05-26 05:38:36 +04:00
|
|
|
|
|
|
|
#include "nsAtomTable.h"
|
2003-03-25 21:55:59 +03:00
|
|
|
#include "nsStaticAtom.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
#include "nsString.h"
|
2003-03-25 21:55:59 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2007-07-12 00:46:43 +04:00
|
|
|
#include "nsUTF8Utils.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
#include "nsCRT.h"
|
2001-10-21 03:19:07 +04:00
|
|
|
#include "pldhash.h"
|
|
|
|
#include "prenv.h"
|
2007-08-07 23:16:52 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2009-09-18 19:13:10 +04:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2010-03-08 18:44:59 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2003-03-25 21:55:59 +03:00
|
|
|
|
|
|
|
#define PL_ARENA_CONST_ALIGN_MASK 3
|
|
|
|
#include "plarena.h"
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
/**
|
|
|
|
* The shared hash table for atom lookups.
|
2001-10-21 03:19:07 +04:00
|
|
|
*
|
|
|
|
* XXX This should be manipulated in a threadsafe way or we should make
|
|
|
|
* sure it's only manipulated from the main thread. Probably the latter
|
|
|
|
* is better, since the former would hurt performance.
|
|
|
|
*
|
2003-07-03 18:45:01 +04:00
|
|
|
* If |gAtomTable.ops| is 0, then the table is uninitialized.
|
1998-04-14 00:24:54 +04:00
|
|
|
*/
|
2001-10-21 03:19:07 +04:00
|
|
|
static PLDHashTable gAtomTable;
|
|
|
|
|
2009-09-18 19:13:10 +04:00
|
|
|
/**
|
|
|
|
* A hashtable of static atoms that existed at app startup. This hashtable helps
|
|
|
|
* nsHtml5AtomTable.
|
|
|
|
*/
|
|
|
|
static nsDataHashtable<nsStringHashKey, nsIAtom*>* gStaticAtomTable = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether it is still OK to add atoms to gStaticAtomTable.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool gStaticAtomTableSealed = false;
|
2009-09-18 19:13:10 +04:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
//----------------------------------------------------------------------
|
2003-03-25 21:55:59 +03:00
|
|
|
|
|
|
|
struct AtomTableEntry : public PLDHashEntryHdr {
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomImpl* mAtom;
|
|
|
|
};
|
2005-11-04 22:52:18 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
struct AtomTableKey
|
|
|
|
{
|
|
|
|
AtomTableKey(const PRUnichar* aUTF16String, PRUint32 aLength)
|
|
|
|
: mUTF16String(aUTF16String),
|
|
|
|
mUTF8String(nsnull),
|
|
|
|
mLength(aLength)
|
2005-11-04 22:52:18 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomTableKey(const char* aUTF8String, PRUint32 aLength)
|
|
|
|
: mUTF16String(nsnull),
|
|
|
|
mUTF8String(aUTF8String),
|
|
|
|
mLength(aLength)
|
2005-11-04 22:52:18 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
const PRUnichar* mUTF16String;
|
|
|
|
const char* mUTF8String;
|
|
|
|
PRUint32 mLength;
|
2003-03-25 21:55:59 +03:00
|
|
|
};
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashNumber
|
2005-11-04 22:52:18 +03:00
|
|
|
AtomTableGetHash(PLDHashTable *table, const void *key)
|
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
const AtomTableKey *k = static_cast<const AtomTableKey*>(key);
|
2005-11-04 22:52:18 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
if (k->mUTF8String) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool err;
|
2010-03-08 18:45:00 +03:00
|
|
|
PRUint32 hash = nsCRT::HashCodeAsUTF16(k->mUTF8String, k->mLength, &err);
|
|
|
|
if (err) {
|
|
|
|
AtomTableKey* mutableKey = const_cast<AtomTableKey*>(k);
|
|
|
|
mutableKey->mUTF8String = nsnull;
|
|
|
|
mutableKey->mLength = 0;
|
|
|
|
hash = 0;
|
|
|
|
}
|
|
|
|
return hash;
|
2005-11-04 22:52:18 +03:00
|
|
|
}
|
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
return nsCRT::HashCode(k->mUTF16String, k->mLength);
|
2001-10-21 03:19:07 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2005-11-04 22:52:18 +03:00
|
|
|
AtomTableMatchKey(PLDHashTable *table, const PLDHashEntryHdr *entry,
|
2001-10-21 03:19:07 +04:00
|
|
|
const void *key)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
const AtomTableEntry *he = static_cast<const AtomTableEntry*>(entry);
|
2010-03-08 18:45:00 +03:00
|
|
|
const AtomTableKey *k = static_cast<const AtomTableKey*>(key);
|
2005-11-04 22:52:18 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
if (k->mUTF8String) {
|
2005-11-04 22:52:18 +03:00
|
|
|
return
|
2010-03-08 18:45:00 +03:00
|
|
|
CompareUTF8toUTF16(nsDependentCSubstring(k->mUTF8String,
|
|
|
|
k->mUTF8String + k->mLength),
|
|
|
|
nsDependentAtomString(he->mAtom)) == 0;
|
2007-07-12 00:46:43 +04:00
|
|
|
}
|
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
PRUint32 length = he->mAtom->GetLength();
|
|
|
|
if (length != k->mLength) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-04 22:52:18 +03:00
|
|
|
}
|
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
return memcmp(he->mAtom->GetUTF16String(),
|
|
|
|
k->mUTF16String, length * sizeof(PRUnichar)) == 0;
|
2001-10-21 03:19:07 +04:00
|
|
|
}
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static void
|
2001-10-21 03:19:07 +04:00
|
|
|
AtomTableClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry)
|
|
|
|
{
|
2010-03-08 18:44:59 +03:00
|
|
|
// Normal |AtomImpl| atoms are deleted when their refcount hits 0, and
|
|
|
|
// they then remove themselves from the table. In other words, they
|
|
|
|
// are owned by the callers who own references to them.
|
|
|
|
// |PermanentAtomImpl| permanent atoms ignore their refcount and are
|
|
|
|
// deleted when they are removed from the table at table destruction.
|
|
|
|
// In other words, they are owned by the atom table.
|
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomImpl *atom = static_cast<AtomTableEntry*>(entry)->mAtom;
|
|
|
|
if (atom->IsPermanent()) {
|
|
|
|
// Note that the cast here is important since AtomImpls doesn't have a
|
|
|
|
// virtual dtor.
|
2010-03-08 18:44:59 +03:00
|
|
|
delete static_cast<PermanentAtomImpl*>(atom);
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
2001-10-21 03:19:07 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2007-07-12 00:46:43 +04:00
|
|
|
AtomTableInitEntry(PLDHashTable *table, PLDHashEntryHdr *entry,
|
|
|
|
const void *key)
|
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
static_cast<AtomTableEntry*>(entry)->mAtom = nsnull;
|
2007-07-12 00:46:43 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2007-07-12 00:46:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-04 19:10:37 +04:00
|
|
|
static const PLDHashTableOps AtomTableOps = {
|
2001-10-21 03:19:07 +04:00
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
2005-11-04 22:52:18 +03:00
|
|
|
AtomTableGetHash,
|
2001-10-21 03:19:07 +04:00
|
|
|
AtomTableMatchKey,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
AtomTableClearEntry,
|
|
|
|
PL_DHashFinalizeStub,
|
2007-07-12 00:46:43 +04:00
|
|
|
AtomTableInitEntry
|
2001-10-21 03:19:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashOperator
|
2001-10-21 03:19:07 +04:00
|
|
|
DumpAtomLeaks(PLDHashTable *table, PLDHashEntryHdr *he,
|
|
|
|
PRUint32 index, void *arg)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
AtomTableEntry *entry = static_cast<AtomTableEntry*>(he);
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomImpl* atom = entry->mAtom;
|
2001-10-21 03:19:07 +04:00
|
|
|
if (!atom->IsPermanent()) {
|
2007-07-08 11:08:04 +04:00
|
|
|
++*static_cast<PRUint32*>(arg);
|
2010-03-08 18:45:00 +03:00
|
|
|
nsCAutoString str;
|
|
|
|
atom->ToUTF8String(str);
|
|
|
|
fputs(str.get(), stdout);
|
1999-10-01 01:40:00 +04:00
|
|
|
fputs("\n", stdout);
|
|
|
|
}
|
2001-10-21 03:19:07 +04:00
|
|
|
return PL_DHASH_NEXT;
|
1999-10-01 01:40:00 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-03-25 21:55:59 +03:00
|
|
|
static inline
|
|
|
|
void PromoteToPermanent(AtomImpl* aAtom)
|
|
|
|
{
|
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
{
|
|
|
|
nsrefcnt refcount = aAtom->GetRefCount();
|
|
|
|
do {
|
|
|
|
NS_LOG_RELEASE(aAtom, --refcount, "AtomImpl");
|
|
|
|
} while (refcount);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
aAtom = new (aAtom) PermanentAtomImpl();
|
|
|
|
}
|
|
|
|
|
2005-11-04 22:52:18 +03:00
|
|
|
void
|
|
|
|
NS_PurgeAtomTable()
|
1999-10-01 01:40:00 +04:00
|
|
|
{
|
2009-09-18 19:13:10 +04:00
|
|
|
delete gStaticAtomTable;
|
|
|
|
|
2003-07-03 18:45:01 +04:00
|
|
|
if (gAtomTable.ops) {
|
2001-10-21 03:19:07 +04:00
|
|
|
#ifdef DEBUG
|
2009-06-11 21:47:33 +04:00
|
|
|
const char *dumpAtomLeaks = PR_GetEnv("MOZ_DUMP_ATOM_LEAKS");
|
|
|
|
if (dumpAtomLeaks && *dumpAtomLeaks) {
|
2001-10-21 03:19:07 +04:00
|
|
|
PRUint32 leaked = 0;
|
|
|
|
printf("*** %d atoms still exist (including permanent):\n",
|
|
|
|
gAtomTable.entryCount);
|
|
|
|
PL_DHashTableEnumerate(&gAtomTable, DumpAtomLeaks, &leaked);
|
|
|
|
printf("*** %u non-permanent atoms leaked\n", leaked);
|
1999-10-01 01:40:00 +04:00
|
|
|
}
|
|
|
|
#endif
|
2001-10-21 03:19:07 +04:00
|
|
|
PL_DHashTableFinish(&gAtomTable);
|
|
|
|
gAtomTable.entryCount = 0;
|
2003-07-03 18:45:01 +04:00
|
|
|
gAtomTable.ops = nsnull;
|
2010-03-08 18:44:59 +03:00
|
|
|
}
|
|
|
|
}
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomImpl::AtomImpl(const nsAString& aString)
|
2010-03-08 18:44:59 +03:00
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
mLength = aString.Length();
|
2010-03-08 18:44:59 +03:00
|
|
|
nsStringBuffer* buf = nsStringBuffer::FromString(aString);
|
|
|
|
if (buf) {
|
|
|
|
buf->AddRef();
|
2010-03-08 18:45:00 +03:00
|
|
|
mString = static_cast<PRUnichar*>(buf->Data());
|
2010-03-08 18:44:59 +03:00
|
|
|
}
|
|
|
|
else {
|
2010-03-08 18:45:00 +03:00
|
|
|
buf = nsStringBuffer::Alloc((mLength + 1) * sizeof(PRUnichar));
|
|
|
|
mString = static_cast<PRUnichar*>(buf->Data());
|
|
|
|
CopyUnicodeTo(aString, 0, mString, mLength);
|
2010-03-08 18:44:59 +03:00
|
|
|
mString[mLength] = PRUnichar(0);
|
1999-10-01 01:40:00 +04:00
|
|
|
}
|
2010-03-08 18:45:00 +03:00
|
|
|
|
|
|
|
NS_ASSERTION(mString[mLength] == PRUnichar(0), "null terminated");
|
|
|
|
NS_ASSERTION(buf && buf->StorageSize() >= (mLength+1) * sizeof(PRUnichar),
|
|
|
|
"enough storage");
|
|
|
|
NS_ASSERTION(Equals(aString), "correct data");
|
1999-10-01 01:40:00 +04:00
|
|
|
}
|
|
|
|
|
2010-03-08 18:44:59 +03:00
|
|
|
AtomImpl::AtomImpl(nsStringBuffer* aStringBuffer, PRUint32 aLength)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
mLength = aLength;
|
|
|
|
mString = static_cast<PRUnichar*>(aStringBuffer->Data());
|
2010-03-08 18:44:59 +03:00
|
|
|
// Technically we could currently avoid doing this addref by instead making
|
|
|
|
// the static atom buffers have an initial refcount of 2.
|
|
|
|
aStringBuffer->AddRef();
|
2010-03-08 18:45:00 +03:00
|
|
|
|
|
|
|
NS_ASSERTION(mString[mLength] == PRUnichar(0), "null terminated");
|
2010-03-08 19:03:55 +03:00
|
|
|
NS_ASSERTION(aStringBuffer &&
|
|
|
|
aStringBuffer->StorageSize() == (mLength+1) * sizeof(PRUnichar),
|
|
|
|
"correct storage");
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
AtomImpl::~AtomImpl()
|
|
|
|
{
|
2003-07-03 18:45:01 +04:00
|
|
|
NS_PRECONDITION(gAtomTable.ops, "uninitialized atom hashtable");
|
2001-10-21 03:19:07 +04:00
|
|
|
// Permanent atoms are removed from the hashtable at shutdown, and we
|
|
|
|
// don't want to remove them twice. See comment above in
|
|
|
|
// |AtomTableClearEntry|.
|
2006-05-06 21:49:21 +04:00
|
|
|
if (!IsPermanentInDestructor()) {
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomTableKey key(mString, mLength);
|
2005-11-04 22:52:18 +03:00
|
|
|
PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_REMOVE);
|
2001-10-21 03:19:07 +04:00
|
|
|
if (gAtomTable.entryCount == 0) {
|
|
|
|
PL_DHashTableFinish(&gAtomTable);
|
|
|
|
NS_ASSERTION(gAtomTable.entryCount == 0,
|
|
|
|
"PL_DHashTableFinish changed the entry count");
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-08 18:44:59 +03:00
|
|
|
|
|
|
|
nsStringBuffer::FromData(mString)->Release();
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2008-02-21 23:39:20 +03:00
|
|
|
NS_IMPL_ISUPPORTS1(AtomImpl, nsIAtom)
|
|
|
|
|
2006-05-06 21:49:21 +04:00
|
|
|
PermanentAtomImpl::~PermanentAtomImpl()
|
|
|
|
{
|
|
|
|
// So we can tell if we were permanent while running the base class dtor.
|
2006-04-20 01:46:47 +04:00
|
|
|
mRefCnt = REFCNT_PERMANENT_SENTINEL;
|
|
|
|
}
|
|
|
|
|
2001-10-21 03:19:07 +04:00
|
|
|
NS_IMETHODIMP_(nsrefcnt) PermanentAtomImpl::AddRef()
|
|
|
|
{
|
2007-08-07 23:16:52 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "wrong thread");
|
2001-10-21 03:19:07 +04:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt) PermanentAtomImpl::Release()
|
|
|
|
{
|
2007-08-07 23:16:52 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "wrong thread");
|
2001-10-21 03:19:07 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
/* virtual */ bool
|
2006-05-06 21:49:21 +04:00
|
|
|
AtomImpl::IsPermanent()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-05-06 21:49:21 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
/* virtual */ bool
|
2006-05-06 21:49:21 +04:00
|
|
|
PermanentAtomImpl::IsPermanent()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2006-05-06 21:49:21 +04:00
|
|
|
}
|
|
|
|
|
2002-07-03 00:25:30 +04:00
|
|
|
void* PermanentAtomImpl::operator new ( size_t size, AtomImpl* aAtom ) CPP_THROW_NEW {
|
2001-10-21 03:19:07 +04:00
|
|
|
NS_ASSERTION(!aAtom->IsPermanent(),
|
|
|
|
"converting atom that's already permanent");
|
|
|
|
|
|
|
|
// Just let the constructor overwrite the vtable pointer.
|
|
|
|
return aAtom;
|
|
|
|
}
|
|
|
|
|
1999-08-21 11:07:00 +04:00
|
|
|
NS_IMETHODIMP
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomImpl::ScriptableToString(nsAString& aBuf)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
nsStringBuffer::FromData(mString)->ToString(mLength, aBuf);
|
2003-03-25 21:55:59 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AtomImpl::ToUTF8String(nsACString& aBuf)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
CopyUTF16toUTF8(nsDependentString(mString, mLength), aBuf);
|
1999-08-21 11:07:00 +04:00
|
|
|
return NS_OK;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP_(bool)
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomImpl::EqualsUTF8(const nsACString& aString)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
return CompareUTF8toUTF16(aString,
|
|
|
|
nsDependentString(mString, mLength)) == 0;
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
AtomImpl::ScriptableEquals(const nsAString& aString, bool* aResult)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
*aResult = aString.Equals(nsDependentString(mString, mLength));
|
2003-03-25 21:55:59 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP_(bool)
|
2009-09-18 19:13:10 +04:00
|
|
|
AtomImpl::IsStaticAtom()
|
|
|
|
{
|
2010-03-08 18:44:59 +03:00
|
|
|
return IsPermanent();
|
2009-09-18 19:13:10 +04:00
|
|
|
}
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2008-06-04 02:25:31 +04:00
|
|
|
#define ATOM_HASHTABLE_INITIAL_SIZE 4096
|
|
|
|
|
2005-11-04 22:52:18 +03:00
|
|
|
static inline AtomTableEntry*
|
2007-07-12 00:46:43 +04:00
|
|
|
GetAtomHashEntry(const char* aString, PRUint32 aLength)
|
2005-11-04 22:52:18 +03:00
|
|
|
{
|
2007-08-07 23:16:52 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "wrong thread");
|
2005-11-04 22:52:18 +03:00
|
|
|
if (!gAtomTable.ops &&
|
|
|
|
!PL_DHashTableInit(&gAtomTable, &AtomTableOps, 0,
|
2008-06-04 02:25:31 +04:00
|
|
|
sizeof(AtomTableEntry), ATOM_HASHTABLE_INITIAL_SIZE)) {
|
2005-11-04 22:52:18 +03:00
|
|
|
gAtomTable.ops = nsnull;
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomTableKey key(aString, aLength);
|
2007-07-08 11:08:04 +04:00
|
|
|
return static_cast<AtomTableEntry*>
|
|
|
|
(PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD));
|
2005-11-04 22:52:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline AtomTableEntry*
|
2007-07-12 00:46:43 +04:00
|
|
|
GetAtomHashEntry(const PRUnichar* aString, PRUint32 aLength)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2007-08-07 23:16:52 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "wrong thread");
|
2003-07-03 18:45:01 +04:00
|
|
|
if (!gAtomTable.ops &&
|
|
|
|
!PL_DHashTableInit(&gAtomTable, &AtomTableOps, 0,
|
2008-06-04 02:25:31 +04:00
|
|
|
sizeof(AtomTableEntry), ATOM_HASHTABLE_INITIAL_SIZE)) {
|
2003-07-03 18:45:01 +04:00
|
|
|
gAtomTable.ops = nsnull;
|
|
|
|
return nsnull;
|
|
|
|
}
|
2005-11-04 22:52:18 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomTableKey key(aString, aLength);
|
2007-07-08 11:08:04 +04:00
|
|
|
return static_cast<AtomTableEntry*>
|
|
|
|
(PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD));
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2010-03-08 18:44:59 +03:00
|
|
|
class CheckStaticAtomSizes
|
|
|
|
{
|
|
|
|
CheckStaticAtomSizes() {
|
|
|
|
PR_STATIC_ASSERT((sizeof(nsFakeStringBuffer<1>().mRefCnt) ==
|
|
|
|
sizeof(nsStringBuffer().mRefCount)) &&
|
|
|
|
(sizeof(nsFakeStringBuffer<1>().mSize) ==
|
|
|
|
sizeof(nsStringBuffer().mStorageSize)) &&
|
|
|
|
(offsetof(nsFakeStringBuffer<1>, mRefCnt) ==
|
|
|
|
offsetof(nsStringBuffer, mRefCount)) &&
|
|
|
|
(offsetof(nsFakeStringBuffer<1>, mSize) ==
|
|
|
|
offsetof(nsStringBuffer, mStorageSize)) &&
|
|
|
|
(offsetof(nsFakeStringBuffer<1>, mStringData) ==
|
|
|
|
sizeof(nsStringBuffer)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2003-03-25 21:55:59 +03:00
|
|
|
NS_RegisterStaticAtoms(const nsStaticAtom* aAtoms, PRUint32 aAtomCount)
|
|
|
|
{
|
2009-09-18 19:13:10 +04:00
|
|
|
// this does three things:
|
2003-03-25 21:55:59 +03:00
|
|
|
// 1) wraps each static atom in a wrapper, if necessary
|
2005-11-04 22:52:18 +03:00
|
|
|
// 2) initializes the address pointed to by each mBits slot
|
2009-09-18 19:13:10 +04:00
|
|
|
// 3) puts the atom into the static atom table as well
|
|
|
|
|
|
|
|
if (!gStaticAtomTable && !gStaticAtomTableSealed) {
|
|
|
|
gStaticAtomTable = new nsDataHashtable<nsStringHashKey, nsIAtom*>();
|
|
|
|
if (!gStaticAtomTable || !gStaticAtomTable->Init()) {
|
|
|
|
delete gStaticAtomTable;
|
|
|
|
gStaticAtomTable = nsnull;
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
2003-03-25 21:55:59 +03:00
|
|
|
|
|
|
|
for (PRUint32 i=0; i<aAtomCount; i++) {
|
2010-03-08 18:45:00 +03:00
|
|
|
#ifdef NS_STATIC_ATOM_USE_WIDE_STRINGS
|
|
|
|
NS_ASSERTION(nsCRT::IsAscii((PRUnichar*)aAtoms[i].mStringBuffer->Data()),
|
2003-03-25 21:55:59 +03:00
|
|
|
"Static atoms must be ASCII!");
|
2007-07-12 00:46:43 +04:00
|
|
|
|
2010-03-08 18:44:59 +03:00
|
|
|
PRUint32 stringLen =
|
2010-03-08 18:45:00 +03:00
|
|
|
aAtoms[i].mStringBuffer->StorageSize() / sizeof(PRUnichar) - 1;
|
2007-07-12 00:46:43 +04:00
|
|
|
|
2003-03-25 21:55:59 +03:00
|
|
|
AtomTableEntry *he =
|
2010-03-08 18:45:00 +03:00
|
|
|
GetAtomHashEntry((PRUnichar*)aAtoms[i].mStringBuffer->Data(),
|
2010-03-08 18:44:59 +03:00
|
|
|
stringLen);
|
2007-07-12 00:46:43 +04:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
if (he->mAtom) {
|
2003-03-25 21:55:59 +03:00
|
|
|
// there already is an atom with this name in the table.. but we
|
2005-11-04 22:52:18 +03:00
|
|
|
// still have to update mBits
|
2010-03-08 18:45:00 +03:00
|
|
|
if (!he->mAtom->IsPermanent()) {
|
2003-03-25 21:55:59 +03:00
|
|
|
// since we wanted to create a static atom but there is
|
|
|
|
// already one there, we convert it to a non-refcounting
|
|
|
|
// permanent atom
|
2010-03-08 18:45:00 +03:00
|
|
|
PromoteToPermanent(he->mAtom);
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
*aAtoms[i].mAtom = he->mAtom;
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
|
|
|
else {
|
2010-03-08 18:44:59 +03:00
|
|
|
AtomImpl* atom = new PermanentAtomImpl(aAtoms[i].mStringBuffer,
|
|
|
|
stringLen);
|
2010-03-08 18:45:00 +03:00
|
|
|
he->mAtom = atom;
|
2010-03-08 18:44:59 +03:00
|
|
|
*aAtoms[i].mAtom = atom;
|
|
|
|
|
2009-09-18 19:13:10 +04:00
|
|
|
if (!gStaticAtomTableSealed) {
|
2010-03-08 18:45:00 +03:00
|
|
|
gStaticAtomTable->Put(nsAtomString(atom), atom);
|
2009-09-18 19:13:10 +04:00
|
|
|
}
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
2010-03-08 18:45:00 +03:00
|
|
|
#else // NS_STATIC_ATOM_USE_WIDE_STRINGS
|
|
|
|
NS_ASSERTION(nsCRT::IsAscii((char*)aAtoms[i].mStringBuffer->Data()),
|
|
|
|
"Static atoms must be ASCII!");
|
|
|
|
|
|
|
|
PRUint32 stringLen = aAtoms[i].mStringBuffer->StorageSize() - 1;
|
|
|
|
|
|
|
|
NS_ConvertASCIItoUTF16 str((char*)aAtoms[i].mStringBuffer->Data(),
|
|
|
|
stringLen);
|
|
|
|
nsIAtom* atom = NS_NewPermanentAtom(str);
|
|
|
|
*aAtoms[i].mAtom = atom;
|
|
|
|
|
|
|
|
if (!gStaticAtomTableSealed) {
|
|
|
|
gStaticAtomTable->Put(str, atom);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsIAtom*
|
2005-11-04 22:52:18 +03:00
|
|
|
NS_NewAtom(const char* aUTF8String)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2007-07-12 00:46:43 +04:00
|
|
|
return NS_NewAtom(nsDependentCString(aUTF8String));
|
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsIAtom*
|
2007-07-12 00:46:43 +04:00
|
|
|
NS_NewAtom(const nsACString& aUTF8String)
|
|
|
|
{
|
2007-09-06 05:13:42 +04:00
|
|
|
AtomTableEntry *he = GetAtomHashEntry(aUTF8String.Data(),
|
2007-07-12 00:46:43 +04:00
|
|
|
aUTF8String.Length());
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
if (he->mAtom) {
|
|
|
|
nsIAtom* atom;
|
|
|
|
NS_ADDREF(atom = he->mAtom);
|
2005-11-04 22:52:18 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
return atom;
|
|
|
|
}
|
2005-11-04 22:52:18 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
// This results in an extra addref/release of the nsStringBuffer.
|
|
|
|
// Unfortunately there doesn't seem to be any APIs to avoid that.
|
|
|
|
nsString str;
|
|
|
|
CopyUTF8toUTF16(aUTF8String, str);
|
|
|
|
AtomImpl* atom = new AtomImpl(str);
|
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
he->mAtom = atom;
|
2005-11-04 22:52:18 +03:00
|
|
|
NS_ADDREF(atom);
|
2010-03-08 18:44:59 +03:00
|
|
|
|
2005-11-04 22:52:18 +03:00
|
|
|
return atom;
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsIAtom*
|
2007-07-12 00:46:43 +04:00
|
|
|
NS_NewAtom(const PRUnichar* aUTF16String)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
2007-07-12 00:46:43 +04:00
|
|
|
return NS_NewAtom(nsDependentString(aUTF16String));
|
2005-11-04 22:52:18 +03:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsIAtom*
|
2007-07-12 00:46:43 +04:00
|
|
|
NS_NewAtom(const nsAString& aUTF16String)
|
2005-11-04 22:52:18 +03:00
|
|
|
{
|
2007-09-06 05:13:42 +04:00
|
|
|
AtomTableEntry *he = GetAtomHashEntry(aUTF16String.Data(),
|
2007-07-12 00:46:43 +04:00
|
|
|
aUTF16String.Length());
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
if (he->mAtom) {
|
|
|
|
nsIAtom* atom;
|
|
|
|
NS_ADDREF(atom = he->mAtom);
|
|
|
|
|
|
|
|
return atom;
|
|
|
|
}
|
2003-03-21 09:26:32 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomImpl* atom = new AtomImpl(aUTF16String);
|
2010-03-08 18:45:00 +03:00
|
|
|
he->mAtom = atom;
|
2001-10-21 03:19:07 +04:00
|
|
|
NS_ADDREF(atom);
|
2000-08-23 21:27:06 +04:00
|
|
|
|
2010-03-08 18:44:59 +03:00
|
|
|
return atom;
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
2000-08-23 21:27:06 +04:00
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsIAtom*
|
2010-03-08 18:45:00 +03:00
|
|
|
NS_NewPermanentAtom(const nsAString& aUTF16String)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomTableEntry *he = GetAtomHashEntry(aUTF16String.Data(),
|
|
|
|
aUTF16String.Length());
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomImpl* atom = he->mAtom;
|
|
|
|
if (atom) {
|
2001-10-21 03:19:07 +04:00
|
|
|
if (!atom->IsPermanent()) {
|
2003-03-25 21:55:59 +03:00
|
|
|
PromoteToPermanent(atom);
|
2001-10-21 03:19:07 +04:00
|
|
|
}
|
2000-08-23 21:27:06 +04:00
|
|
|
}
|
2010-03-08 18:45:00 +03:00
|
|
|
else {
|
|
|
|
atom = new PermanentAtomImpl(aUTF16String);
|
|
|
|
he->mAtom = atom;
|
|
|
|
}
|
2000-02-11 04:59:55 +03:00
|
|
|
|
2010-03-08 18:44:59 +03:00
|
|
|
// No need to addref since permanent atoms aren't refcounted anyway
|
|
|
|
return atom;
|
2001-10-21 03:19:07 +04:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsrefcnt
|
2005-11-04 22:52:18 +03:00
|
|
|
NS_GetNumberOfAtoms(void)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2001-10-21 03:19:07 +04:00
|
|
|
return gAtomTable.entryCount;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsIAtom*
|
2009-09-18 19:13:10 +04:00
|
|
|
NS_GetStaticAtom(const nsAString& aUTF16String)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(gStaticAtomTable, "Static atom table not created yet.");
|
|
|
|
NS_PRECONDITION(gStaticAtomTableSealed, "Static atom table not sealed yet.");
|
|
|
|
nsIAtom* atom;
|
|
|
|
if (!gStaticAtomTable->Get(aUTF16String, &atom)) {
|
|
|
|
atom = nsnull;
|
|
|
|
}
|
|
|
|
return atom;
|
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
void
|
2009-09-18 19:13:10 +04:00
|
|
|
NS_SealStaticAtomTable()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
gStaticAtomTableSealed = true;
|
2009-09-18 19:13:10 +04:00
|
|
|
}
|