2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2001-10-20 23:19:07 +00:00
|
|
|
// vim:cindent:ts=2:et:sw=2:
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-05-26 01:38:36 +00:00
|
|
|
|
2011-12-19 21:58:30 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
2012-06-05 23:51:58 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-12-06 17:47:28 +00:00
|
|
|
#include "mozilla/Compiler.h"
|
2012-03-12 22:53:18 +00:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2013-06-23 12:03:39 +00:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-11-08 23:29:09 +00:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2011-12-19 21:58:30 +00:00
|
|
|
|
1999-05-26 01:38:36 +00:00
|
|
|
#include "nsAtomTable.h"
|
2003-03-25 18:55:59 +00:00
|
|
|
#include "nsStaticAtom.h"
|
2013-09-23 17:25:00 +00:00
|
|
|
#include "nsString.h"
|
1998-04-13 20:24:54 +00:00
|
|
|
#include "nsCRT.h"
|
2001-10-20 23:19:07 +00:00
|
|
|
#include "pldhash.h"
|
|
|
|
#include "prenv.h"
|
2007-08-07 19:16:52 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2009-09-18 15:13:10 +00:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2010-03-08 15:44:59 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2012-03-12 22:53:18 +00:00
|
|
|
#include "nsUnicharUtils.h"
|
2003-03-25 18:55:59 +00:00
|
|
|
|
2012-03-12 22:53:18 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-12-06 17:47:28 +00:00
|
|
|
#if defined(__clang__)
|
|
|
|
# pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
|
|
|
|
#elif MOZ_IS_GCC
|
|
|
|
# if MOZ_GCC_VERSION_AT_LEAST(4, 7, 0)
|
|
|
|
# pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
|
|
|
|
# endif
|
2013-12-01 20:06:29 +00:00
|
|
|
#endif
|
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
/**
|
|
|
|
* The shared hash table for atom lookups.
|
2001-10-20 23:19:07 +00: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 14:45:01 +00:00
|
|
|
* If |gAtomTable.ops| is 0, then the table is uninitialized.
|
1998-04-13 20:24:54 +00:00
|
|
|
*/
|
2001-10-20 23:19:07 +00:00
|
|
|
static PLDHashTable gAtomTable;
|
|
|
|
|
2009-09-18 15:13:10 +00: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 06:19:26 +00:00
|
|
|
static bool gStaticAtomTableSealed = false;
|
2009-09-18 15:13:10 +00:00
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
//----------------------------------------------------------------------
|
2003-03-25 18:55:59 +00:00
|
|
|
|
2011-12-23 01:24:44 +00:00
|
|
|
/**
|
|
|
|
* Note that AtomImpl objects are sometimes converted into PermanentAtomImpl
|
|
|
|
* objects using placement new and just overwriting the vtable pointer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class AtomImpl : public nsIAtom {
|
|
|
|
public:
|
2013-11-08 23:29:09 +00:00
|
|
|
AtomImpl(const nsAString& aString, uint32_t aHash);
|
2011-12-23 01:24:44 +00:00
|
|
|
|
|
|
|
// This is currently only used during startup when creating a permanent atom
|
|
|
|
// from NS_RegisterStaticAtoms
|
2013-11-08 23:29:09 +00:00
|
|
|
AtomImpl(nsStringBuffer* aData, uint32_t aLength, uint32_t aHash);
|
2011-12-23 01:24:44 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// This is only intended to be used when a normal atom is turned into a
|
|
|
|
// permanent one.
|
|
|
|
AtomImpl() {
|
|
|
|
// We can't really assert that mString is a valid nsStringBuffer string,
|
|
|
|
// so do the best we can do and check for some consistencies.
|
2014-01-04 15:02:17 +00:00
|
|
|
NS_ASSERTION((mLength + 1) * sizeof(char16_t) <=
|
2011-12-23 01:24:44 +00:00
|
|
|
nsStringBuffer::FromData(mString)->StorageSize() &&
|
|
|
|
mString[mLength] == 0,
|
|
|
|
"Not initialized atom");
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need a virtual destructor here because PermanentAtomImpl
|
|
|
|
// deletions aren't handled through Release().
|
|
|
|
~AtomImpl();
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIATOM
|
|
|
|
|
2012-09-28 06:57:33 +00:00
|
|
|
enum { REFCNT_PERMANENT_SENTINEL = UINT32_MAX };
|
2011-12-23 01:24:44 +00:00
|
|
|
|
|
|
|
virtual bool IsPermanent();
|
|
|
|
|
|
|
|
// We can't use the virtual function in the base class destructor.
|
|
|
|
bool IsPermanentInDestructor() {
|
|
|
|
return mRefCnt == REFCNT_PERMANENT_SENTINEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for |#ifdef NS_BUILD_REFCNT_LOGGING| access to reference count
|
|
|
|
nsrefcnt GetRefCount() { return mRefCnt; }
|
|
|
|
|
2013-06-23 12:03:39 +00:00
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
|
2011-12-23 01:24:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A non-refcounted implementation of nsIAtom.
|
|
|
|
*/
|
|
|
|
|
2012-06-05 23:51:58 +00:00
|
|
|
class PermanentAtomImpl MOZ_FINAL : public AtomImpl {
|
2011-12-23 01:24:44 +00:00
|
|
|
public:
|
2012-03-01 02:40:51 +00:00
|
|
|
PermanentAtomImpl(const nsAString& aString, PLDHashNumber aKeyHash)
|
|
|
|
: AtomImpl(aString, aKeyHash)
|
2011-12-23 01:24:44 +00:00
|
|
|
{}
|
2012-08-22 15:56:38 +00:00
|
|
|
PermanentAtomImpl(nsStringBuffer* aData, uint32_t aLength,
|
2012-03-01 02:40:51 +00:00
|
|
|
PLDHashNumber aKeyHash)
|
|
|
|
: AtomImpl(aData, aLength, aKeyHash)
|
2011-12-23 01:24:44 +00:00
|
|
|
{}
|
|
|
|
PermanentAtomImpl()
|
|
|
|
{}
|
|
|
|
|
|
|
|
~PermanentAtomImpl();
|
|
|
|
NS_IMETHOD_(nsrefcnt) AddRef();
|
|
|
|
NS_IMETHOD_(nsrefcnt) Release();
|
|
|
|
|
|
|
|
virtual bool IsPermanent();
|
|
|
|
|
|
|
|
// SizeOfIncludingThis() isn't needed -- the one inherited from AtomImpl is
|
|
|
|
// good enough, because PermanentAtomImpl doesn't add any new data members.
|
|
|
|
|
|
|
|
void* operator new(size_t size, AtomImpl* aAtom) CPP_THROW_NEW;
|
|
|
|
void* operator new(size_t size) CPP_THROW_NEW
|
|
|
|
{
|
|
|
|
return ::operator new(size);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2003-03-25 18:55:59 +00:00
|
|
|
struct AtomTableEntry : public PLDHashEntryHdr {
|
2010-03-08 15:45:00 +00:00
|
|
|
AtomImpl* mAtom;
|
|
|
|
};
|
2005-11-04 19:52:18 +00:00
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
struct AtomTableKey
|
|
|
|
{
|
2014-01-04 15:02:17 +00:00
|
|
|
AtomTableKey(const char16_t* aUTF16String, uint32_t aLength,
|
2013-11-08 23:29:09 +00:00
|
|
|
/*inout*/ uint32_t& aHash)
|
2010-03-08 15:45:00 +00:00
|
|
|
: mUTF16String(aUTF16String),
|
2012-07-30 14:20:58 +00:00
|
|
|
mUTF8String(nullptr),
|
2010-03-08 15:45:00 +00:00
|
|
|
mLength(aLength)
|
2005-11-04 19:52:18 +00:00
|
|
|
{
|
2013-11-08 23:29:09 +00:00
|
|
|
if (aHash) {
|
|
|
|
MOZ_ASSERT(aHash == HashString(mUTF16String, mLength));
|
|
|
|
mHash = aHash;
|
|
|
|
} else {
|
|
|
|
UpdateHashKey();
|
|
|
|
aHash = mHash;
|
|
|
|
}
|
2005-11-04 19:52:18 +00:00
|
|
|
}
|
|
|
|
|
2013-11-08 23:29:09 +00:00
|
|
|
AtomTableKey(const char* aUTF8String, uint32_t aLength,
|
|
|
|
/*inout*/ uint32_t& aHash)
|
2012-07-30 14:20:58 +00:00
|
|
|
: mUTF16String(nullptr),
|
2010-03-08 15:45:00 +00:00
|
|
|
mUTF8String(aUTF8String),
|
|
|
|
mLength(aLength)
|
2005-11-04 19:52:18 +00:00
|
|
|
{
|
2013-11-08 23:29:09 +00:00
|
|
|
if (aHash) {
|
|
|
|
mozilla::DebugOnly<bool> err;
|
|
|
|
MOZ_ASSERT(aHash == HashUTF8AsUTF16(mUTF8String, mLength, &err));
|
|
|
|
mHash = aHash;
|
|
|
|
} else {
|
|
|
|
UpdateHashKey();
|
|
|
|
aHash = mHash;
|
|
|
|
}
|
2005-11-04 19:52:18 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t* mUTF16String;
|
2010-03-08 15:45:00 +00:00
|
|
|
const char* mUTF8String;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mLength;
|
2013-11-08 23:29:09 +00:00
|
|
|
uint32_t mHash;
|
|
|
|
|
|
|
|
void UpdateHashKey()
|
|
|
|
{
|
|
|
|
if (mUTF8String) {
|
|
|
|
bool err;
|
|
|
|
mHash = HashUTF8AsUTF16(mUTF8String, mLength, &err);
|
|
|
|
if (err) {
|
|
|
|
mUTF8String = nullptr;
|
|
|
|
mLength = 0;
|
|
|
|
mHash = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mHash = HashString(mUTF16String, mLength);
|
|
|
|
}
|
|
|
|
}
|
2003-03-25 18:55:59 +00:00
|
|
|
};
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashNumber
|
2005-11-04 19:52:18 +00:00
|
|
|
AtomTableGetHash(PLDHashTable *table, const void *key)
|
|
|
|
{
|
2010-03-08 15:45:00 +00:00
|
|
|
const AtomTableKey *k = static_cast<const AtomTableKey*>(key);
|
2013-11-08 23:29:09 +00:00
|
|
|
return k->mHash;
|
2001-10-20 23:19:07 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool
|
2005-11-04 19:52:18 +00:00
|
|
|
AtomTableMatchKey(PLDHashTable *table, const PLDHashEntryHdr *entry,
|
2001-10-20 23:19:07 +00:00
|
|
|
const void *key)
|
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
const AtomTableEntry *he = static_cast<const AtomTableEntry*>(entry);
|
2010-03-08 15:45:00 +00:00
|
|
|
const AtomTableKey *k = static_cast<const AtomTableKey*>(key);
|
2005-11-04 19:52:18 +00:00
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
if (k->mUTF8String) {
|
2005-11-04 19:52:18 +00:00
|
|
|
return
|
2010-03-08 15:45:00 +00:00
|
|
|
CompareUTF8toUTF16(nsDependentCSubstring(k->mUTF8String,
|
|
|
|
k->mUTF8String + k->mLength),
|
|
|
|
nsDependentAtomString(he->mAtom)) == 0;
|
2007-07-11 20:46:43 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length = he->mAtom->GetLength();
|
2010-03-08 15:45:00 +00:00
|
|
|
if (length != k->mLength) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2005-11-04 19:52:18 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
return memcmp(he->mAtom->GetUTF16String(),
|
2014-01-04 15:02:17 +00:00
|
|
|
k->mUTF16String, length * sizeof(char16_t)) == 0;
|
2001-10-20 23:19:07 +00:00
|
|
|
}
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static void
|
2001-10-20 23:19:07 +00:00
|
|
|
AtomTableClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry)
|
|
|
|
{
|
2010-03-08 15:44:59 +00: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 15:45:00 +00: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 15:44:59 +00:00
|
|
|
delete static_cast<PermanentAtomImpl*>(atom);
|
2003-03-25 18:55:59 +00:00
|
|
|
}
|
2001-10-20 23:19:07 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool
|
2007-07-11 20:46:43 +00:00
|
|
|
AtomTableInitEntry(PLDHashTable *table, PLDHashEntryHdr *entry,
|
|
|
|
const void *key)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
static_cast<AtomTableEntry*>(entry)->mAtom = nullptr;
|
2007-07-11 20:46:43 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2007-07-11 20:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-04 15:10:37 +00:00
|
|
|
static const PLDHashTableOps AtomTableOps = {
|
2001-10-20 23:19:07 +00:00
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
2005-11-04 19:52:18 +00:00
|
|
|
AtomTableGetHash,
|
2001-10-20 23:19:07 +00:00
|
|
|
AtomTableMatchKey,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
AtomTableClearEntry,
|
|
|
|
PL_DHashFinalizeStub,
|
2007-07-11 20:46:43 +00:00
|
|
|
AtomTableInitEntry
|
2001-10-20 23:19:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashOperator
|
2001-10-20 23:19:07 +00:00
|
|
|
DumpAtomLeaks(PLDHashTable *table, PLDHashEntryHdr *he,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t index, void *arg)
|
2001-10-20 23:19:07 +00:00
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
AtomTableEntry *entry = static_cast<AtomTableEntry*>(he);
|
2003-03-25 18:55:59 +00:00
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
AtomImpl* atom = entry->mAtom;
|
2001-10-20 23:19:07 +00:00
|
|
|
if (!atom->IsPermanent()) {
|
2012-08-22 15:56:38 +00:00
|
|
|
++*static_cast<uint32_t*>(arg);
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString str;
|
2010-03-08 15:45:00 +00:00
|
|
|
atom->ToUTF8String(str);
|
|
|
|
fputs(str.get(), stdout);
|
1999-09-30 21:40:00 +00:00
|
|
|
fputs("\n", stdout);
|
|
|
|
}
|
2001-10-20 23:19:07 +00:00
|
|
|
return PL_DHASH_NEXT;
|
1999-09-30 21:40:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-03-25 18:55:59 +00: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 19:52:18 +00:00
|
|
|
void
|
|
|
|
NS_PurgeAtomTable()
|
1999-09-30 21:40:00 +00:00
|
|
|
{
|
2009-09-18 15:13:10 +00:00
|
|
|
delete gStaticAtomTable;
|
|
|
|
|
2003-07-03 14:45:01 +00:00
|
|
|
if (gAtomTable.ops) {
|
2001-10-20 23:19:07 +00:00
|
|
|
#ifdef DEBUG
|
2009-06-11 17:47:33 +00:00
|
|
|
const char *dumpAtomLeaks = PR_GetEnv("MOZ_DUMP_ATOM_LEAKS");
|
|
|
|
if (dumpAtomLeaks && *dumpAtomLeaks) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t leaked = 0;
|
2001-10-20 23:19:07 +00:00
|
|
|
printf("*** %d atoms still exist (including permanent):\n",
|
|
|
|
gAtomTable.entryCount);
|
|
|
|
PL_DHashTableEnumerate(&gAtomTable, DumpAtomLeaks, &leaked);
|
|
|
|
printf("*** %u non-permanent atoms leaked\n", leaked);
|
1999-09-30 21:40:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
2001-10-20 23:19:07 +00:00
|
|
|
PL_DHashTableFinish(&gAtomTable);
|
|
|
|
gAtomTable.entryCount = 0;
|
2012-07-30 14:20:58 +00:00
|
|
|
gAtomTable.ops = nullptr;
|
2010-03-08 15:44:59 +00:00
|
|
|
}
|
|
|
|
}
|
2003-03-25 18:55:59 +00:00
|
|
|
|
2013-11-08 23:29:09 +00:00
|
|
|
AtomImpl::AtomImpl(const nsAString& aString, uint32_t aHash)
|
2010-03-08 15:44:59 +00:00
|
|
|
{
|
2010-03-08 15:45:00 +00:00
|
|
|
mLength = aString.Length();
|
2013-04-22 11:13:22 +00:00
|
|
|
nsRefPtr<nsStringBuffer> buf = nsStringBuffer::FromString(aString);
|
2010-03-08 15:44:59 +00:00
|
|
|
if (buf) {
|
2014-01-04 15:02:17 +00:00
|
|
|
mString = static_cast<char16_t*>(buf->Data());
|
2013-04-22 11:13:22 +00:00
|
|
|
} else {
|
2014-01-04 15:02:17 +00:00
|
|
|
buf = nsStringBuffer::Alloc((mLength + 1) * sizeof(char16_t));
|
|
|
|
mString = static_cast<char16_t*>(buf->Data());
|
2010-03-08 15:45:00 +00:00
|
|
|
CopyUnicodeTo(aString, 0, mString, mLength);
|
2014-01-04 15:02:17 +00:00
|
|
|
mString[mLength] = char16_t(0);
|
1999-09-30 21:40:00 +00:00
|
|
|
}
|
2010-03-08 15:45:00 +00:00
|
|
|
|
2013-11-08 23:29:09 +00:00
|
|
|
mHash = aHash;
|
|
|
|
MOZ_ASSERT(mHash == HashString(mString, mLength));
|
2012-03-01 02:40:51 +00:00
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
NS_ASSERTION(mString[mLength] == char16_t(0), "null terminated");
|
|
|
|
NS_ASSERTION(buf && buf->StorageSize() >= (mLength+1) * sizeof(char16_t),
|
2010-03-08 15:45:00 +00:00
|
|
|
"enough storage");
|
|
|
|
NS_ASSERTION(Equals(aString), "correct data");
|
2013-04-22 11:13:22 +00:00
|
|
|
|
|
|
|
// Take ownership of buffer
|
|
|
|
buf.forget();
|
1999-09-30 21:40:00 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
AtomImpl::AtomImpl(nsStringBuffer* aStringBuffer, uint32_t aLength,
|
2013-11-08 23:29:09 +00:00
|
|
|
uint32_t aHash)
|
1998-04-13 20:24:54 +00:00
|
|
|
{
|
2010-03-08 15:45:00 +00:00
|
|
|
mLength = aLength;
|
2014-01-04 15:02:17 +00:00
|
|
|
mString = static_cast<char16_t*>(aStringBuffer->Data());
|
2010-03-08 15:44:59 +00: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 15:45:00 +00:00
|
|
|
|
2013-11-08 23:29:09 +00:00
|
|
|
mHash = aHash;
|
|
|
|
MOZ_ASSERT(mHash == HashString(mString, mLength));
|
2012-03-01 02:40:51 +00:00
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
NS_ASSERTION(mString[mLength] == char16_t(0), "null terminated");
|
2010-03-08 16:03:55 +00:00
|
|
|
NS_ASSERTION(aStringBuffer &&
|
2014-01-04 15:02:17 +00:00
|
|
|
aStringBuffer->StorageSize() == (mLength+1) * sizeof(char16_t),
|
2010-03-08 16:03:55 +00:00
|
|
|
"correct storage");
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AtomImpl::~AtomImpl()
|
|
|
|
{
|
2003-07-03 14:45:01 +00:00
|
|
|
NS_PRECONDITION(gAtomTable.ops, "uninitialized atom hashtable");
|
2001-10-20 23:19:07 +00: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 17:49:21 +00:00
|
|
|
if (!IsPermanentInDestructor()) {
|
2013-11-08 23:29:09 +00:00
|
|
|
AtomTableKey key(mString, mLength, mHash);
|
2005-11-04 19:52:18 +00:00
|
|
|
PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_REMOVE);
|
2001-10-20 23:19:07 +00:00
|
|
|
if (gAtomTable.entryCount == 0) {
|
|
|
|
PL_DHashTableFinish(&gAtomTable);
|
|
|
|
NS_ASSERTION(gAtomTable.entryCount == 0,
|
|
|
|
"PL_DHashTableFinish changed the entry count");
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-08 15:44:59 +00:00
|
|
|
|
|
|
|
nsStringBuffer::FromData(mString)->Release();
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
2008-02-21 20:39:20 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(AtomImpl, nsIAtom)
|
|
|
|
|
2006-05-06 17:49:21 +00:00
|
|
|
PermanentAtomImpl::~PermanentAtomImpl()
|
|
|
|
{
|
|
|
|
// So we can tell if we were permanent while running the base class dtor.
|
2006-04-19 21:46:47 +00:00
|
|
|
mRefCnt = REFCNT_PERMANENT_SENTINEL;
|
|
|
|
}
|
|
|
|
|
2001-10-20 23:19:07 +00:00
|
|
|
NS_IMETHODIMP_(nsrefcnt) PermanentAtomImpl::AddRef()
|
|
|
|
{
|
2013-02-08 05:54:20 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
2001-10-20 23:19:07 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt) PermanentAtomImpl::Release()
|
|
|
|
{
|
2013-02-08 05:54:20 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
2001-10-20 23:19:07 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
/* virtual */ bool
|
2006-05-06 17:49:21 +00:00
|
|
|
AtomImpl::IsPermanent()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2006-05-06 17:49:21 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
/* virtual */ bool
|
2006-05-06 17:49:21 +00:00
|
|
|
PermanentAtomImpl::IsPermanent()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2006-05-06 17:49:21 +00:00
|
|
|
}
|
|
|
|
|
2002-07-02 20:25:30 +00:00
|
|
|
void* PermanentAtomImpl::operator new ( size_t size, AtomImpl* aAtom ) CPP_THROW_NEW {
|
2013-02-08 05:54:20 +00:00
|
|
|
MOZ_ASSERT(!aAtom->IsPermanent(),
|
|
|
|
"converting atom that's already permanent");
|
2001-10-20 23:19:07 +00:00
|
|
|
|
|
|
|
// Just let the constructor overwrite the vtable pointer.
|
|
|
|
return aAtom;
|
|
|
|
}
|
|
|
|
|
1999-08-21 07:07:00 +00:00
|
|
|
NS_IMETHODIMP
|
2010-03-08 15:45:00 +00:00
|
|
|
AtomImpl::ScriptableToString(nsAString& aBuf)
|
2003-03-25 18:55:59 +00:00
|
|
|
{
|
2010-03-08 15:45:00 +00:00
|
|
|
nsStringBuffer::FromData(mString)->ToString(mLength, aBuf);
|
2003-03-25 18:55:59 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AtomImpl::ToUTF8String(nsACString& aBuf)
|
1998-04-13 20:24:54 +00:00
|
|
|
{
|
2010-03-08 15:45:00 +00:00
|
|
|
CopyUTF16toUTF8(nsDependentString(mString, mLength), aBuf);
|
1999-08-21 07:07:00 +00:00
|
|
|
return NS_OK;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHODIMP_(bool)
|
2010-03-08 15:45:00 +00:00
|
|
|
AtomImpl::EqualsUTF8(const nsACString& aString)
|
2003-03-25 18:55:59 +00:00
|
|
|
{
|
2010-03-08 15:45:00 +00:00
|
|
|
return CompareUTF8toUTF16(aString,
|
|
|
|
nsDependentString(mString, mLength)) == 0;
|
2003-03-25 18:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
AtomImpl::ScriptableEquals(const nsAString& aString, bool* aResult)
|
2003-03-25 18:55:59 +00:00
|
|
|
{
|
2010-03-08 15:45:00 +00:00
|
|
|
*aResult = aString.Equals(nsDependentString(mString, mLength));
|
2003-03-25 18:55:59 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHODIMP_(bool)
|
2009-09-18 15:13:10 +00:00
|
|
|
AtomImpl::IsStaticAtom()
|
|
|
|
{
|
2010-03-08 15:44:59 +00:00
|
|
|
return IsPermanent();
|
2009-09-18 15:13:10 +00:00
|
|
|
}
|
|
|
|
|
2011-12-23 01:24:43 +00:00
|
|
|
size_t
|
2013-06-23 12:03:39 +00:00
|
|
|
AtomImpl::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
2011-12-23 01:24:43 +00:00
|
|
|
{
|
2012-01-25 08:52:51 +00:00
|
|
|
return aMallocSizeOf(this) +
|
2011-12-23 01:24:43 +00:00
|
|
|
nsStringBuffer::FromData(mString)->
|
|
|
|
SizeOfIncludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2011-12-23 01:24:43 +00:00
|
|
|
static size_t
|
|
|
|
SizeOfAtomTableEntryExcludingThis(PLDHashEntryHdr *aHdr,
|
2013-06-23 12:03:39 +00:00
|
|
|
MallocSizeOf aMallocSizeOf,
|
2011-12-23 01:24:43 +00:00
|
|
|
void *aArg)
|
|
|
|
{
|
|
|
|
AtomTableEntry* entry = static_cast<AtomTableEntry*>(aHdr);
|
|
|
|
return entry->mAtom->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2012-06-15 19:51:42 +00:00
|
|
|
static size_t
|
|
|
|
SizeOfStaticAtomTableEntryExcludingThis(const nsAString& aKey,
|
|
|
|
nsIAtom* const& aData,
|
2013-06-23 12:03:39 +00:00
|
|
|
MallocSizeOf aMallocSizeOf,
|
2012-06-15 19:51:42 +00:00
|
|
|
void* aArg)
|
|
|
|
{
|
|
|
|
return aKey.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2013-06-23 12:03:39 +00:00
|
|
|
NS_SizeOfAtomTablesIncludingThis(MallocSizeOf aMallocSizeOf) {
|
2012-06-15 19:51:42 +00:00
|
|
|
size_t n = 0;
|
2011-12-23 01:24:43 +00:00
|
|
|
if (gAtomTable.ops) {
|
2012-06-15 19:51:42 +00:00
|
|
|
n += PL_DHashTableSizeOfExcludingThis(&gAtomTable,
|
|
|
|
SizeOfAtomTableEntryExcludingThis,
|
|
|
|
aMallocSizeOf);
|
|
|
|
}
|
|
|
|
if (gStaticAtomTable) {
|
|
|
|
n += gStaticAtomTable->SizeOfIncludingThis(SizeOfStaticAtomTableEntryExcludingThis,
|
|
|
|
aMallocSizeOf);
|
2011-12-23 01:24:43 +00:00
|
|
|
}
|
2012-11-09 08:46:34 +00:00
|
|
|
return n;
|
2011-12-23 01:24:43 +00:00
|
|
|
}
|
|
|
|
|
2008-06-03 22:25:31 +00:00
|
|
|
#define ATOM_HASHTABLE_INITIAL_SIZE 4096
|
|
|
|
|
2013-04-08 21:04:21 +00:00
|
|
|
static inline void
|
2011-12-23 01:24:43 +00:00
|
|
|
EnsureTableExists()
|
|
|
|
{
|
2013-04-08 21:04:21 +00:00
|
|
|
if (!gAtomTable.ops &&
|
|
|
|
!PL_DHashTableInit(&gAtomTable, &AtomTableOps, 0,
|
|
|
|
sizeof(AtomTableEntry), ATOM_HASHTABLE_INITIAL_SIZE)) {
|
|
|
|
// Initialization failed.
|
2013-11-25 20:06:17 +00:00
|
|
|
NS_ABORT_OOM(ATOM_HASHTABLE_INITIAL_SIZE * sizeof(AtomTableEntry));
|
2011-12-23 01:24:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-04 19:52:18 +00:00
|
|
|
static inline AtomTableEntry*
|
2013-11-08 23:29:09 +00:00
|
|
|
GetAtomHashEntry(const char* aString, uint32_t aLength, uint32_t& aHash)
|
2005-11-04 19:52:18 +00:00
|
|
|
{
|
2013-02-08 05:54:20 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
2013-04-08 21:04:21 +00:00
|
|
|
EnsureTableExists();
|
2013-11-08 23:29:09 +00:00
|
|
|
AtomTableKey key(aString, aLength, aHash);
|
2013-04-08 21:04:21 +00:00
|
|
|
AtomTableEntry* e =
|
|
|
|
static_cast<AtomTableEntry*>
|
|
|
|
(PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD));
|
|
|
|
if (!e) {
|
2013-11-25 20:06:17 +00:00
|
|
|
NS_ABORT_OOM(gAtomTable.entryCount * gAtomTable.entrySize);
|
2013-04-08 21:04:21 +00:00
|
|
|
}
|
|
|
|
return e;
|
2005-11-04 19:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline AtomTableEntry*
|
2014-01-04 15:02:17 +00:00
|
|
|
GetAtomHashEntry(const char16_t* aString, uint32_t aLength, uint32_t& aHash)
|
1998-04-13 20:24:54 +00:00
|
|
|
{
|
2013-02-08 05:54:20 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
2013-04-08 21:04:21 +00:00
|
|
|
EnsureTableExists();
|
2013-11-08 23:29:09 +00:00
|
|
|
AtomTableKey key(aString, aLength, aHash);
|
2013-04-08 21:04:21 +00:00
|
|
|
AtomTableEntry* e =
|
|
|
|
static_cast<AtomTableEntry*>
|
|
|
|
(PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD));
|
|
|
|
if (!e) {
|
2013-11-25 20:06:17 +00:00
|
|
|
NS_ABORT_OOM(gAtomTable.entryCount * gAtomTable.entrySize);
|
2013-04-08 21:04:21 +00:00
|
|
|
}
|
|
|
|
return e;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 15:44:59 +00:00
|
|
|
class CheckStaticAtomSizes
|
|
|
|
{
|
|
|
|
CheckStaticAtomSizes() {
|
2013-07-18 17:59:53 +00:00
|
|
|
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)),
|
|
|
|
"mocked-up strings' representations should be compatible");
|
2010-03-08 15:44:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
RegisterStaticAtoms(const nsStaticAtom* aAtoms, uint32_t aAtomCount)
|
2003-03-25 18:55:59 +00:00
|
|
|
{
|
2009-09-18 15:13:10 +00:00
|
|
|
// this does three things:
|
2003-03-25 18:55:59 +00:00
|
|
|
// 1) wraps each static atom in a wrapper, if necessary
|
2005-11-04 19:52:18 +00:00
|
|
|
// 2) initializes the address pointed to by each mBits slot
|
2009-09-18 15:13:10 +00:00
|
|
|
// 3) puts the atom into the static atom table as well
|
|
|
|
|
|
|
|
if (!gStaticAtomTable && !gStaticAtomTableSealed) {
|
|
|
|
gStaticAtomTable = new nsDataHashtable<nsStringHashKey, nsIAtom*>();
|
|
|
|
}
|
2003-03-25 18:55:59 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i=0; i<aAtomCount; i++) {
|
2014-01-04 15:02:17 +00:00
|
|
|
NS_ASSERTION(nsCRT::IsAscii((char16_t*)aAtoms[i].mStringBuffer->Data()),
|
2003-03-25 18:55:59 +00:00
|
|
|
"Static atoms must be ASCII!");
|
2007-07-11 20:46:43 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t stringLen =
|
2014-01-04 15:02:17 +00:00
|
|
|
aAtoms[i].mStringBuffer->StorageSize() / sizeof(char16_t) - 1;
|
2007-07-11 20:46:43 +00:00
|
|
|
|
2013-11-08 23:29:09 +00:00
|
|
|
uint32_t hash = 0;
|
2003-03-25 18:55:59 +00:00
|
|
|
AtomTableEntry *he =
|
2014-01-04 15:02:17 +00:00
|
|
|
GetAtomHashEntry((char16_t*)aAtoms[i].mStringBuffer->Data(),
|
2013-11-08 23:29:09 +00:00
|
|
|
stringLen, hash);
|
2007-07-11 20:46:43 +00:00
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
if (he->mAtom) {
|
2003-03-25 18:55:59 +00:00
|
|
|
// there already is an atom with this name in the table.. but we
|
2005-11-04 19:52:18 +00:00
|
|
|
// still have to update mBits
|
2010-03-08 15:45:00 +00:00
|
|
|
if (!he->mAtom->IsPermanent()) {
|
2003-03-25 18:55:59 +00: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 15:45:00 +00:00
|
|
|
PromoteToPermanent(he->mAtom);
|
2003-03-25 18:55:59 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
*aAtoms[i].mAtom = he->mAtom;
|
2003-03-25 18:55:59 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-03-08 15:44:59 +00:00
|
|
|
AtomImpl* atom = new PermanentAtomImpl(aAtoms[i].mStringBuffer,
|
2012-03-01 02:40:51 +00:00
|
|
|
stringLen,
|
2013-11-08 23:29:09 +00:00
|
|
|
hash);
|
2010-03-08 15:45:00 +00:00
|
|
|
he->mAtom = atom;
|
2010-03-08 15:44:59 +00:00
|
|
|
*aAtoms[i].mAtom = atom;
|
|
|
|
|
2009-09-18 15:13:10 +00:00
|
|
|
if (!gStaticAtomTableSealed) {
|
2010-03-08 15:45:00 +00:00
|
|
|
gStaticAtomTable->Put(nsAtomString(atom), atom);
|
2009-09-18 15:13:10 +00:00
|
|
|
}
|
2003-03-25 18:55:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-22 11:13:22 +00:00
|
|
|
already_AddRefed<nsIAtom>
|
2005-11-04 19:52:18 +00:00
|
|
|
NS_NewAtom(const char* aUTF8String)
|
1998-04-13 20:24:54 +00:00
|
|
|
{
|
2007-07-11 20:46:43 +00:00
|
|
|
return NS_NewAtom(nsDependentCString(aUTF8String));
|
|
|
|
}
|
|
|
|
|
2013-04-22 11:13:22 +00:00
|
|
|
already_AddRefed<nsIAtom>
|
2007-07-11 20:46:43 +00:00
|
|
|
NS_NewAtom(const nsACString& aUTF8String)
|
|
|
|
{
|
2013-11-08 23:29:09 +00:00
|
|
|
uint32_t hash = 0;
|
2007-09-06 01:13:42 +00:00
|
|
|
AtomTableEntry *he = GetAtomHashEntry(aUTF8String.Data(),
|
2013-11-08 23:29:09 +00:00
|
|
|
aUTF8String.Length(),
|
|
|
|
hash);
|
2003-03-25 18:55:59 +00:00
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
if (he->mAtom) {
|
2013-04-22 11:13:22 +00:00
|
|
|
nsCOMPtr<nsIAtom> atom = he->mAtom;
|
2005-11-04 19:52:18 +00:00
|
|
|
|
2013-04-22 11:13:22 +00:00
|
|
|
return atom.forget();
|
2010-03-08 15:45:00 +00:00
|
|
|
}
|
2005-11-04 19:52:18 +00:00
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
// This results in an extra addref/release of the nsStringBuffer.
|
|
|
|
// Unfortunately there doesn't seem to be any APIs to avoid that.
|
2012-03-01 02:40:51 +00:00
|
|
|
// Actually, now there is, sort of: ForgetSharedBuffer.
|
2010-03-08 15:45:00 +00:00
|
|
|
nsString str;
|
|
|
|
CopyUTF8toUTF16(aUTF8String, str);
|
2013-11-08 23:29:09 +00:00
|
|
|
nsRefPtr<AtomImpl> atom = new AtomImpl(str, hash);
|
2010-03-08 15:45:00 +00:00
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
he->mAtom = atom;
|
2010-03-08 15:44:59 +00:00
|
|
|
|
2013-04-22 11:13:22 +00:00
|
|
|
return atom.forget();
|
2003-03-25 18:55:59 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 11:13:22 +00:00
|
|
|
already_AddRefed<nsIAtom>
|
2014-01-04 15:02:17 +00:00
|
|
|
NS_NewAtom(const char16_t* aUTF16String)
|
2003-03-25 18:55:59 +00:00
|
|
|
{
|
2007-07-11 20:46:43 +00:00
|
|
|
return NS_NewAtom(nsDependentString(aUTF16String));
|
2005-11-04 19:52:18 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 11:13:22 +00:00
|
|
|
already_AddRefed<nsIAtom>
|
2007-07-11 20:46:43 +00:00
|
|
|
NS_NewAtom(const nsAString& aUTF16String)
|
2005-11-04 19:52:18 +00:00
|
|
|
{
|
2013-11-08 23:29:09 +00:00
|
|
|
uint32_t hash = 0;
|
2007-09-06 01:13:42 +00:00
|
|
|
AtomTableEntry *he = GetAtomHashEntry(aUTF16String.Data(),
|
2013-11-08 23:29:09 +00:00
|
|
|
aUTF16String.Length(),
|
|
|
|
hash);
|
2003-03-25 18:55:59 +00:00
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
if (he->mAtom) {
|
2013-04-22 11:13:22 +00:00
|
|
|
nsCOMPtr<nsIAtom> atom = he->mAtom;
|
2010-03-08 15:45:00 +00:00
|
|
|
|
2013-04-22 11:13:22 +00:00
|
|
|
return atom.forget();
|
2010-03-08 15:45:00 +00:00
|
|
|
}
|
2003-03-21 06:26:32 +00:00
|
|
|
|
2013-11-08 23:29:09 +00:00
|
|
|
nsRefPtr<AtomImpl> atom = new AtomImpl(aUTF16String, hash);
|
2010-03-08 15:45:00 +00:00
|
|
|
he->mAtom = atom;
|
2000-08-23 17:27:06 +00:00
|
|
|
|
2013-04-22 11:13:22 +00:00
|
|
|
return atom.forget();
|
2003-03-25 18:55:59 +00:00
|
|
|
}
|
2000-08-23 17:27:06 +00:00
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
nsIAtom*
|
2010-03-08 15:45:00 +00:00
|
|
|
NS_NewPermanentAtom(const nsAString& aUTF16String)
|
2003-03-25 18:55:59 +00:00
|
|
|
{
|
2013-11-08 23:29:09 +00:00
|
|
|
uint32_t hash = 0;
|
2010-03-08 15:45:00 +00:00
|
|
|
AtomTableEntry *he = GetAtomHashEntry(aUTF16String.Data(),
|
2013-11-08 23:29:09 +00:00
|
|
|
aUTF16String.Length(),
|
|
|
|
hash);
|
2003-03-25 18:55:59 +00:00
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
AtomImpl* atom = he->mAtom;
|
|
|
|
if (atom) {
|
2001-10-20 23:19:07 +00:00
|
|
|
if (!atom->IsPermanent()) {
|
2003-03-25 18:55:59 +00:00
|
|
|
PromoteToPermanent(atom);
|
2001-10-20 23:19:07 +00:00
|
|
|
}
|
2000-08-23 17:27:06 +00:00
|
|
|
}
|
2010-03-08 15:45:00 +00:00
|
|
|
else {
|
2013-11-08 23:29:09 +00:00
|
|
|
atom = new PermanentAtomImpl(aUTF16String, hash);
|
2010-03-08 15:45:00 +00:00
|
|
|
he->mAtom = atom;
|
|
|
|
}
|
2000-02-11 01:59:55 +00:00
|
|
|
|
2010-03-08 15:44:59 +00:00
|
|
|
// No need to addref since permanent atoms aren't refcounted anyway
|
|
|
|
return atom;
|
2001-10-20 23:19:07 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
nsrefcnt
|
2005-11-04 19:52:18 +00:00
|
|
|
NS_GetNumberOfAtoms(void)
|
1998-04-13 20:24:54 +00:00
|
|
|
{
|
2001-10-20 23:19:07 +00:00
|
|
|
return gAtomTable.entryCount;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
2003-03-25 18:55:59 +00:00
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
nsIAtom*
|
2009-09-18 15:13:10 +00: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)) {
|
2012-07-30 14:20:58 +00:00
|
|
|
atom = nullptr;
|
2009-09-18 15:13:10 +00:00
|
|
|
}
|
|
|
|
return atom;
|
|
|
|
}
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
void
|
2009-09-18 15:13:10 +00:00
|
|
|
NS_SealStaticAtomTable()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
gStaticAtomTableSealed = true;
|
2009-09-18 15:13:10 +00:00
|
|
|
}
|