2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
// vim:cindent:ts=2:et:sw=2:
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* 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 ***** */
|
|
|
|
|
|
|
|
#include "nsAtomTable.h"
|
|
|
|
#include "nsStaticAtom.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2007-07-11 13:46:43 -07:00
|
|
|
#include "nsUTF8Utils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "pldhash.h"
|
|
|
|
#include "prenv.h"
|
2007-08-07 12:16:52 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2009-09-18 08:13:10 -07:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2010-03-08 07:44:59 -08:00
|
|
|
#include "nsAutoPtr.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#define PL_ARENA_CONST_ALIGN_MASK 3
|
|
|
|
#include "plarena.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The shared hash table for atom lookups.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* If |gAtomTable.ops| is 0, then the table is uninitialized.
|
|
|
|
*/
|
|
|
|
static PLDHashTable gAtomTable;
|
|
|
|
|
2009-09-18 08:13:10 -07: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.
|
|
|
|
*/
|
|
|
|
static PRBool gStaticAtomTableSealed = PR_FALSE;
|
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
//----------------------------------------------------------------------
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
struct AtomTableEntry : public PLDHashEntryHdr {
|
2010-03-08 07:45:00 -08:00
|
|
|
AtomImpl* mAtom;
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
struct AtomTableKey
|
|
|
|
{
|
|
|
|
AtomTableKey(const PRUnichar* aUTF16String, PRUint32 aLength)
|
|
|
|
: mUTF16String(aUTF16String),
|
|
|
|
mUTF8String(nsnull),
|
|
|
|
mLength(aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
AtomTableKey(const char* aUTF8String, PRUint32 aLength)
|
|
|
|
: mUTF16String(nsnull),
|
|
|
|
mUTF8String(aUTF8String),
|
|
|
|
mLength(aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
const PRUnichar* mUTF16String;
|
|
|
|
const char* mUTF8String;
|
|
|
|
PRUint32 mLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashNumber
|
2007-03-22 10:30:00 -07:00
|
|
|
AtomTableGetHash(PLDHashTable *table, const void *key)
|
|
|
|
{
|
2010-03-08 07:45:00 -08:00
|
|
|
const AtomTableKey *k = static_cast<const AtomTableKey*>(key);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
if (k->mUTF8String) {
|
2010-03-08 07:45:00 -08:00
|
|
|
PRBool err;
|
|
|
|
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;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
return nsCRT::HashCode(k->mUTF16String, k->mLength);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PRBool
|
2007-03-22 10:30:00 -07:00
|
|
|
AtomTableMatchKey(PLDHashTable *table, const PLDHashEntryHdr *entry,
|
|
|
|
const void *key)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
const AtomTableEntry *he = static_cast<const AtomTableEntry*>(entry);
|
2010-03-08 07:45:00 -08:00
|
|
|
const AtomTableKey *k = static_cast<const AtomTableKey*>(key);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
if (k->mUTF8String) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return
|
2010-03-08 07:45:00 -08:00
|
|
|
CompareUTF8toUTF16(nsDependentCSubstring(k->mUTF8String,
|
|
|
|
k->mUTF8String + k->mLength),
|
|
|
|
nsDependentAtomString(he->mAtom)) == 0;
|
2007-07-11 13:46:43 -07:00
|
|
|
}
|
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
PRUint32 length = he->mAtom->GetLength();
|
|
|
|
if (length != k->mLength) {
|
2007-07-11 13:46:43 -07:00
|
|
|
return PR_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
return memcmp(he->mAtom->GetUTF16String(),
|
|
|
|
k->mUTF16String, length * sizeof(PRUnichar)) == 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static void
|
2007-03-22 10:30:00 -07:00
|
|
|
AtomTableClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry)
|
|
|
|
{
|
2010-03-08 07:44:59 -08: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 07:45:00 -08: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 07:44:59 -08:00
|
|
|
delete static_cast<PermanentAtomImpl*>(atom);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PRBool
|
2007-07-11 13:46:43 -07:00
|
|
|
AtomTableInitEntry(PLDHashTable *table, PLDHashEntryHdr *entry,
|
|
|
|
const void *key)
|
|
|
|
{
|
2010-03-08 07:45:00 -08:00
|
|
|
static_cast<AtomTableEntry*>(entry)->mAtom = nsnull;
|
2007-07-11 13:46:43 -07:00
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static const PLDHashTableOps AtomTableOps = {
|
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
|
|
|
AtomTableGetHash,
|
|
|
|
AtomTableMatchKey,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
AtomTableClearEntry,
|
|
|
|
PL_DHashFinalizeStub,
|
2007-07-11 13:46:43 -07:00
|
|
|
AtomTableInitEntry
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashOperator
|
2007-03-22 10:30:00 -07:00
|
|
|
DumpAtomLeaks(PLDHashTable *table, PLDHashEntryHdr *he,
|
|
|
|
PRUint32 index, void *arg)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
AtomTableEntry *entry = static_cast<AtomTableEntry*>(he);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
AtomImpl* atom = entry->mAtom;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!atom->IsPermanent()) {
|
2007-07-08 00:08:04 -07:00
|
|
|
++*static_cast<PRUint32*>(arg);
|
2010-03-08 07:45:00 -08:00
|
|
|
nsCAutoString str;
|
|
|
|
atom->ToUTF8String(str);
|
|
|
|
fputs(str.get(), stdout);
|
2007-03-22 10:30:00 -07:00
|
|
|
fputs("\n", stdout);
|
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NS_PurgeAtomTable()
|
|
|
|
{
|
2009-09-18 08:13:10 -07:00
|
|
|
delete gStaticAtomTable;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gAtomTable.ops) {
|
|
|
|
#ifdef DEBUG
|
2009-06-11 10:47:33 -07:00
|
|
|
const char *dumpAtomLeaks = PR_GetEnv("MOZ_DUMP_ATOM_LEAKS");
|
|
|
|
if (dumpAtomLeaks && *dumpAtomLeaks) {
|
2007-03-22 10:30:00 -07: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);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
PL_DHashTableFinish(&gAtomTable);
|
|
|
|
gAtomTable.entryCount = 0;
|
|
|
|
gAtomTable.ops = nsnull;
|
2010-03-08 07:44:59 -08:00
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
AtomImpl::AtomImpl(const nsAString& aString)
|
2010-03-08 07:44:59 -08:00
|
|
|
{
|
2010-03-08 07:45:00 -08:00
|
|
|
mLength = aString.Length();
|
2010-03-08 07:44:59 -08:00
|
|
|
nsStringBuffer* buf = nsStringBuffer::FromString(aString);
|
|
|
|
if (buf) {
|
|
|
|
buf->AddRef();
|
2010-03-08 07:45:00 -08:00
|
|
|
mString = static_cast<PRUnichar*>(buf->Data());
|
2010-03-08 07:44:59 -08:00
|
|
|
}
|
|
|
|
else {
|
2010-03-08 07:45:00 -08:00
|
|
|
buf = nsStringBuffer::Alloc((mLength + 1) * sizeof(PRUnichar));
|
|
|
|
mString = static_cast<PRUnichar*>(buf->Data());
|
|
|
|
CopyUnicodeTo(aString, 0, mString, mLength);
|
2010-03-08 07:44:59 -08:00
|
|
|
mString[mLength] = PRUnichar(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-03-08 07:45:00 -08: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");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-08 07:44:59 -08:00
|
|
|
AtomImpl::AtomImpl(nsStringBuffer* aStringBuffer, PRUint32 aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-08 07:45:00 -08:00
|
|
|
mLength = aLength;
|
|
|
|
mString = static_cast<PRUnichar*>(aStringBuffer->Data());
|
2010-03-08 07:44:59 -08: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 07:45:00 -08:00
|
|
|
|
|
|
|
NS_ASSERTION(mString[mLength] == PRUnichar(0), "null terminated");
|
2010-03-08 08:03:55 -08:00
|
|
|
NS_ASSERTION(aStringBuffer &&
|
|
|
|
aStringBuffer->StorageSize() == (mLength+1) * sizeof(PRUnichar),
|
|
|
|
"correct storage");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
AtomImpl::~AtomImpl()
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(gAtomTable.ops, "uninitialized atom hashtable");
|
|
|
|
// Permanent atoms are removed from the hashtable at shutdown, and we
|
|
|
|
// don't want to remove them twice. See comment above in
|
|
|
|
// |AtomTableClearEntry|.
|
|
|
|
if (!IsPermanentInDestructor()) {
|
2010-03-08 07:45:00 -08:00
|
|
|
AtomTableKey key(mString, mLength);
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_REMOVE);
|
|
|
|
if (gAtomTable.entryCount == 0) {
|
|
|
|
PL_DHashTableFinish(&gAtomTable);
|
|
|
|
NS_ASSERTION(gAtomTable.entryCount == 0,
|
|
|
|
"PL_DHashTableFinish changed the entry count");
|
|
|
|
}
|
|
|
|
}
|
2010-03-08 07:44:59 -08:00
|
|
|
|
|
|
|
nsStringBuffer::FromData(mString)->Release();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-21 12:39:20 -08:00
|
|
|
NS_IMPL_ISUPPORTS1(AtomImpl, nsIAtom)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
PermanentAtomImpl::~PermanentAtomImpl()
|
|
|
|
{
|
|
|
|
// So we can tell if we were permanent while running the base class dtor.
|
|
|
|
mRefCnt = REFCNT_PERMANENT_SENTINEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt) PermanentAtomImpl::AddRef()
|
|
|
|
{
|
2007-08-07 12:16:52 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "wrong thread");
|
2007-03-22 10:30:00 -07:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt) PermanentAtomImpl::Release()
|
|
|
|
{
|
2007-08-07 12:16:52 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "wrong thread");
|
2007-03-22 10:30:00 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ PRBool
|
|
|
|
AtomImpl::IsPermanent()
|
|
|
|
{
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ PRBool
|
|
|
|
PermanentAtomImpl::IsPermanent()
|
|
|
|
{
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* PermanentAtomImpl::operator new ( size_t size, AtomImpl* aAtom ) CPP_THROW_NEW {
|
|
|
|
NS_ASSERTION(!aAtom->IsPermanent(),
|
|
|
|
"converting atom that's already permanent");
|
|
|
|
|
|
|
|
// Just let the constructor overwrite the vtable pointer.
|
|
|
|
return aAtom;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-03-08 07:45:00 -08:00
|
|
|
AtomImpl::ScriptableToString(nsAString& aBuf)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-08 07:45:00 -08:00
|
|
|
nsStringBuffer::FromData(mString)->ToString(mLength, aBuf);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AtomImpl::ToUTF8String(nsACString& aBuf)
|
|
|
|
{
|
2010-03-08 07:45:00 -08:00
|
|
|
CopyUTF16toUTF8(nsDependentString(mString, mLength), aBuf);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
NS_IMETHODIMP_(PRBool)
|
|
|
|
AtomImpl::EqualsUTF8(const nsACString& aString)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-08 07:45:00 -08:00
|
|
|
return CompareUTF8toUTF16(aString,
|
|
|
|
nsDependentString(mString, mLength)) == 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-03-08 07:45:00 -08:00
|
|
|
AtomImpl::ScriptableEquals(const nsAString& aString, PRBool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-08 07:45:00 -08:00
|
|
|
*aResult = aString.Equals(nsDependentString(mString, mLength));
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-09-18 08:13:10 -07:00
|
|
|
NS_IMETHODIMP_(PRBool)
|
|
|
|
AtomImpl::IsStaticAtom()
|
|
|
|
{
|
2010-03-08 07:44:59 -08:00
|
|
|
return IsPermanent();
|
2009-09-18 08:13:10 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2008-06-03 15:25:31 -07:00
|
|
|
#define ATOM_HASHTABLE_INITIAL_SIZE 4096
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static inline AtomTableEntry*
|
2007-07-11 13:46:43 -07:00
|
|
|
GetAtomHashEntry(const char* aString, PRUint32 aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-07 12:16:52 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "wrong thread");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!gAtomTable.ops &&
|
|
|
|
!PL_DHashTableInit(&gAtomTable, &AtomTableOps, 0,
|
2008-06-03 15:25:31 -07:00
|
|
|
sizeof(AtomTableEntry), ATOM_HASHTABLE_INITIAL_SIZE)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
gAtomTable.ops = nsnull;
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
AtomTableKey key(aString, aLength);
|
2007-07-08 00:08:04 -07:00
|
|
|
return static_cast<AtomTableEntry*>
|
|
|
|
(PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline AtomTableEntry*
|
2007-07-11 13:46:43 -07:00
|
|
|
GetAtomHashEntry(const PRUnichar* aString, PRUint32 aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-07 12:16:52 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "wrong thread");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!gAtomTable.ops &&
|
|
|
|
!PL_DHashTableInit(&gAtomTable, &AtomTableOps, 0,
|
2008-06-03 15:25:31 -07:00
|
|
|
sizeof(AtomTableEntry), ATOM_HASHTABLE_INITIAL_SIZE)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
gAtomTable.ops = nsnull;
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
AtomTableKey key(aString, aLength);
|
2007-07-08 00:08:04 -07:00
|
|
|
return static_cast<AtomTableEntry*>
|
|
|
|
(PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-08 07:44:59 -08: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)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_COM nsresult
|
|
|
|
NS_RegisterStaticAtoms(const nsStaticAtom* aAtoms, PRUint32 aAtomCount)
|
|
|
|
{
|
2009-09-18 08:13:10 -07:00
|
|
|
// this does three things:
|
2007-03-22 10:30:00 -07:00
|
|
|
// 1) wraps each static atom in a wrapper, if necessary
|
|
|
|
// 2) initializes the address pointed to by each mBits slot
|
2009-09-18 08:13:10 -07: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;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (PRUint32 i=0; i<aAtomCount; i++) {
|
2010-03-08 07:45:00 -08:00
|
|
|
#ifdef NS_STATIC_ATOM_USE_WIDE_STRINGS
|
|
|
|
NS_ASSERTION(nsCRT::IsAscii((PRUnichar*)aAtoms[i].mStringBuffer->Data()),
|
2007-03-22 10:30:00 -07:00
|
|
|
"Static atoms must be ASCII!");
|
2007-07-11 13:46:43 -07:00
|
|
|
|
2010-03-08 07:44:59 -08:00
|
|
|
PRUint32 stringLen =
|
2010-03-08 07:45:00 -08:00
|
|
|
aAtoms[i].mStringBuffer->StorageSize() / sizeof(PRUnichar) - 1;
|
2007-07-11 13:46:43 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
AtomTableEntry *he =
|
2010-03-08 07:45:00 -08:00
|
|
|
GetAtomHashEntry((PRUnichar*)aAtoms[i].mStringBuffer->Data(),
|
2010-03-08 07:44:59 -08:00
|
|
|
stringLen);
|
2007-07-11 13:46:43 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
if (he->mAtom) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// there already is an atom with this name in the table.. but we
|
|
|
|
// still have to update mBits
|
2010-03-08 07:45:00 -08:00
|
|
|
if (!he->mAtom->IsPermanent()) {
|
2007-03-22 10:30:00 -07: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 07:45:00 -08:00
|
|
|
PromoteToPermanent(he->mAtom);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
*aAtoms[i].mAtom = he->mAtom;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2010-03-08 07:44:59 -08:00
|
|
|
AtomImpl* atom = new PermanentAtomImpl(aAtoms[i].mStringBuffer,
|
|
|
|
stringLen);
|
2010-03-08 07:45:00 -08:00
|
|
|
he->mAtom = atom;
|
2010-03-08 07:44:59 -08:00
|
|
|
*aAtoms[i].mAtom = atom;
|
|
|
|
|
2009-09-18 08:13:10 -07:00
|
|
|
if (!gStaticAtomTableSealed) {
|
2010-03-08 07:45:00 -08:00
|
|
|
gStaticAtomTable->Put(nsAtomString(atom), atom);
|
2009-09-18 08:13:10 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-03-08 07:45:00 -08: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
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_COM nsIAtom*
|
|
|
|
NS_NewAtom(const char* aUTF8String)
|
|
|
|
{
|
2007-07-11 13:46:43 -07:00
|
|
|
return NS_NewAtom(nsDependentCString(aUTF8String));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_COM nsIAtom*
|
|
|
|
NS_NewAtom(const nsACString& aUTF8String)
|
|
|
|
{
|
2007-09-05 18:13:42 -07:00
|
|
|
AtomTableEntry *he = GetAtomHashEntry(aUTF8String.Data(),
|
2007-07-11 13:46:43 -07:00
|
|
|
aUTF8String.Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
if (he->mAtom) {
|
|
|
|
nsIAtom* atom;
|
|
|
|
NS_ADDREF(atom = he->mAtom);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
return atom;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:45:00 -08: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 07:45:00 -08:00
|
|
|
he->mAtom = atom;
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ADDREF(atom);
|
2010-03-08 07:44:59 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return atom;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_COM nsIAtom*
|
2007-07-11 13:46:43 -07:00
|
|
|
NS_NewAtom(const PRUnichar* aUTF16String)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-11 13:46:43 -07:00
|
|
|
return NS_NewAtom(nsDependentString(aUTF16String));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_COM nsIAtom*
|
2007-07-11 13:46:43 -07:00
|
|
|
NS_NewAtom(const nsAString& aUTF16String)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-09-05 18:13:42 -07:00
|
|
|
AtomTableEntry *he = GetAtomHashEntry(aUTF16String.Data(),
|
2007-07-11 13:46:43 -07:00
|
|
|
aUTF16String.Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
if (he->mAtom) {
|
|
|
|
nsIAtom* atom;
|
|
|
|
NS_ADDREF(atom = he->mAtom);
|
|
|
|
|
|
|
|
return atom;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
AtomImpl* atom = new AtomImpl(aUTF16String);
|
2010-03-08 07:45:00 -08:00
|
|
|
he->mAtom = atom;
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ADDREF(atom);
|
|
|
|
|
2010-03-08 07:44:59 -08:00
|
|
|
return atom;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_COM nsIAtom*
|
2010-03-08 07:45:00 -08:00
|
|
|
NS_NewPermanentAtom(const nsAString& aUTF16String)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-08 07:45:00 -08:00
|
|
|
AtomTableEntry *he = GetAtomHashEntry(aUTF16String.Data(),
|
|
|
|
aUTF16String.Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
AtomImpl* atom = he->mAtom;
|
|
|
|
if (atom) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!atom->IsPermanent()) {
|
|
|
|
PromoteToPermanent(atom);
|
|
|
|
}
|
|
|
|
}
|
2010-03-08 07:45:00 -08:00
|
|
|
else {
|
|
|
|
atom = new PermanentAtomImpl(aUTF16String);
|
|
|
|
he->mAtom = atom;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:44:59 -08:00
|
|
|
// No need to addref since permanent atoms aren't refcounted anyway
|
|
|
|
return atom;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_COM nsrefcnt
|
|
|
|
NS_GetNumberOfAtoms(void)
|
|
|
|
{
|
|
|
|
return gAtomTable.entryCount;
|
|
|
|
}
|
|
|
|
|
2009-09-18 08:13:10 -07:00
|
|
|
NS_COM nsIAtom*
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_COM void
|
|
|
|
NS_SealStaticAtomTable()
|
|
|
|
{
|
|
|
|
gStaticAtomTableSealed = PR_TRUE;
|
|
|
|
}
|