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:
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-19 13:58:30 -08:00
|
|
|
#include "mozilla/Assertions.h"
|
2012-06-05 16:51:58 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-12-06 09:47:28 -08:00
|
|
|
#include "mozilla/Compiler.h"
|
2012-03-12 15:53:18 -07:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-11-08 15:29:09 -08:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2014-03-15 12:00:16 -07:00
|
|
|
#include "mozilla/unused.h"
|
2011-12-19 13:58:30 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsAtomTable.h"
|
|
|
|
#include "nsStaticAtom.h"
|
2013-09-23 10:25:00 -07:00
|
|
|
#include "nsString.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"
|
2012-03-12 15:53:18 -07:00
|
|
|
#include "nsUnicharUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-12 15:53:18 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-12-06 09:47:28 -08: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 12:06:29 -08:00
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
/**
|
2014-07-09 08:15:21 -07:00
|
|
|
* A hashtable of static atoms that existed at app startup. This hashtable helps
|
2009-09-18 08:13:10 -07:00
|
|
|
* nsHtml5AtomTable.
|
|
|
|
*/
|
|
|
|
static nsDataHashtable<nsStringHashKey, nsIAtom*>* gStaticAtomTable = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether it is still OK to add atoms to gStaticAtomTable.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool gStaticAtomTableSealed = false;
|
2009-09-18 08:13:10 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
//----------------------------------------------------------------------
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-22 17:24:44 -08:00
|
|
|
/**
|
|
|
|
* Note that AtomImpl objects are sometimes converted into PermanentAtomImpl
|
|
|
|
* objects using placement new and just overwriting the vtable pointer.
|
|
|
|
*/
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
class AtomImpl : public nsIAtom
|
|
|
|
{
|
2011-12-22 17:24:44 -08:00
|
|
|
public:
|
2013-11-08 15:29:09 -08:00
|
|
|
AtomImpl(const nsAString& aString, uint32_t aHash);
|
2011-12-22 17:24:44 -08:00
|
|
|
|
|
|
|
// This is currently only used during startup when creating a permanent atom
|
|
|
|
// from NS_RegisterStaticAtoms
|
2013-11-08 15:29:09 -08:00
|
|
|
AtomImpl(nsStringBuffer* aData, uint32_t aLength, uint32_t aHash);
|
2011-12-22 17:24:44 -08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// This is only intended to be used when a normal atom is turned into a
|
|
|
|
// permanent one.
|
2014-07-09 08:15:21 -07:00
|
|
|
AtomImpl()
|
|
|
|
{
|
2011-12-22 17:24:44 -08:00
|
|
|
// 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 07:02:17 -08:00
|
|
|
NS_ASSERTION((mLength + 1) * sizeof(char16_t) <=
|
2011-12-22 17:24:44 -08: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-27 23:57:33 -07:00
|
|
|
enum { REFCNT_PERMANENT_SENTINEL = UINT32_MAX };
|
2011-12-22 17:24:44 -08:00
|
|
|
|
|
|
|
virtual bool IsPermanent();
|
|
|
|
|
|
|
|
// We can't use the virtual function in the base class destructor.
|
2014-07-09 08:15:21 -07:00
|
|
|
bool IsPermanentInDestructor()
|
|
|
|
{
|
2011-12-22 17:24:44 -08:00
|
|
|
return mRefCnt == REFCNT_PERMANENT_SENTINEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for |#ifdef NS_BUILD_REFCNT_LOGGING| access to reference count
|
|
|
|
nsrefcnt GetRefCount() { return mRefCnt; }
|
|
|
|
|
2013-06-23 05:03:39 -07:00
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
|
2011-12-22 17:24:44 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A non-refcounted implementation of nsIAtom.
|
|
|
|
*/
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
class PermanentAtomImpl MOZ_FINAL : public AtomImpl
|
|
|
|
{
|
2011-12-22 17:24:44 -08:00
|
|
|
public:
|
2012-02-29 18:40:51 -08:00
|
|
|
PermanentAtomImpl(const nsAString& aString, PLDHashNumber aKeyHash)
|
|
|
|
: AtomImpl(aString, aKeyHash)
|
2014-07-09 08:15:21 -07:00
|
|
|
{
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
PermanentAtomImpl(nsStringBuffer* aData, uint32_t aLength,
|
2012-02-29 18:40:51 -08:00
|
|
|
PLDHashNumber aKeyHash)
|
|
|
|
: AtomImpl(aData, aLength, aKeyHash)
|
2014-07-09 08:15:21 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
PermanentAtomImpl() {}
|
2011-12-22 17:24:44 -08:00
|
|
|
|
|
|
|
~PermanentAtomImpl();
|
2014-03-27 13:38:33 -07:00
|
|
|
NS_IMETHOD_(MozExternalRefCountType) AddRef();
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType) Release();
|
2011-12-22 17:24:44 -08:00
|
|
|
|
|
|
|
virtual bool IsPermanent();
|
|
|
|
|
|
|
|
// SizeOfIncludingThis() isn't needed -- the one inherited from AtomImpl is
|
|
|
|
// good enough, because PermanentAtomImpl doesn't add any new data members.
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
void* operator new(size_t aSize, AtomImpl* aAtom) CPP_THROW_NEW;
|
|
|
|
void* operator new(size_t aSize) CPP_THROW_NEW
|
2011-12-22 17:24:44 -08:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
return ::operator new(aSize);
|
2011-12-22 17:24:44 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2014-07-09 08:15:21 -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
|
|
|
|
{
|
2014-01-04 07:02:17 -08:00
|
|
|
AtomTableKey(const char16_t* aUTF16String, uint32_t aLength,
|
2013-11-08 15:29:09 -08:00
|
|
|
/*inout*/ uint32_t& aHash)
|
2014-07-09 08:15:21 -07:00
|
|
|
: mUTF16String(aUTF16String)
|
|
|
|
, mUTF8String(nullptr)
|
|
|
|
, mLength(aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-08 15:29:09 -08:00
|
|
|
if (aHash) {
|
|
|
|
MOZ_ASSERT(aHash == HashString(mUTF16String, mLength));
|
|
|
|
mHash = aHash;
|
|
|
|
} else {
|
|
|
|
UpdateHashKey();
|
|
|
|
aHash = mHash;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-11-08 15:29:09 -08:00
|
|
|
AtomTableKey(const char* aUTF8String, uint32_t aLength,
|
|
|
|
/*inout*/ uint32_t& aHash)
|
2014-07-09 08:15:21 -07:00
|
|
|
: mUTF16String(nullptr)
|
|
|
|
, mUTF8String(aUTF8String)
|
|
|
|
, mLength(aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-08 15:29:09 -08:00
|
|
|
if (aHash) {
|
|
|
|
mozilla::DebugOnly<bool> err;
|
|
|
|
MOZ_ASSERT(aHash == HashUTF8AsUTF16(mUTF8String, mLength, &err));
|
|
|
|
mHash = aHash;
|
|
|
|
} else {
|
|
|
|
UpdateHashKey();
|
|
|
|
aHash = mHash;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t* mUTF16String;
|
2010-03-08 07:45:00 -08:00
|
|
|
const char* mUTF8String;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mLength;
|
2013-11-08 15:29:09 -08: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);
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashNumber
|
2014-07-09 08:15:21 -07:00
|
|
|
AtomTableGetHash(PLDHashTable* aTable, const void* aKey)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
const AtomTableKey* k = static_cast<const AtomTableKey*>(aKey);
|
2013-11-08 15:29:09 -08:00
|
|
|
return k->mHash;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2014-07-09 08:15:21 -07:00
|
|
|
AtomTableMatchKey(PLDHashTable* aTable, const PLDHashEntryHdr* aEntry,
|
|
|
|
const void* aKey)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
const AtomTableEntry* he = static_cast<const AtomTableEntry*>(aEntry);
|
|
|
|
const AtomTableKey* k = static_cast<const AtomTableKey*>(aKey);
|
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
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length = he->mAtom->GetLength();
|
2010-03-08 07:45:00 -08:00
|
|
|
if (length != k->mLength) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
return memcmp(he->mAtom->GetUTF16String(),
|
2014-01-04 07:02:17 -08:00
|
|
|
k->mUTF16String, length * sizeof(char16_t)) == 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static void
|
2014-07-09 08:15:21 -07:00
|
|
|
AtomTableClearEntry(PLDHashTable* aTable, PLDHashEntryHdr* aEntry)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
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.
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
AtomImpl* atom = static_cast<AtomTableEntry*>(aEntry)->mAtom;
|
2010-03-08 07:45:00 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2014-07-09 08:15:21 -07:00
|
|
|
AtomTableInitEntry(PLDHashTable* aTable, PLDHashEntryHdr* aEntry,
|
|
|
|
const void* aKey)
|
2007-07-11 13:46:43 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
static_cast<AtomTableEntry*>(aEntry)->mAtom = nullptr;
|
2007-07-11 13:46:43 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-07-11 13:46:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
2014-07-09 08:15:21 -07:00
|
|
|
DumpAtomLeaks(PLDHashTable* aTable, PLDHashEntryHdr* aEntryHdr,
|
|
|
|
uint32_t aIndex, void* aArg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
AtomTableEntry* entry = static_cast<AtomTableEntry*>(aEntryHdr);
|
2010-03-08 07:45:00 -08:00
|
|
|
AtomImpl* atom = entry->mAtom;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!atom->IsPermanent()) {
|
2014-07-09 08:15:21 -07:00
|
|
|
++*static_cast<uint32_t*>(aArg);
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString str;
|
2010-03-08 07:45:00 -08:00
|
|
|
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
|
2014-07-09 08:15:21 -07:00
|
|
|
void
|
|
|
|
PromoteToPermanent(AtomImpl* aAtom)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
#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
|
2014-07-09 08:15:21 -07:00
|
|
|
const char* dumpAtomLeaks = PR_GetEnv("MOZ_DUMP_ATOM_LEAKS");
|
2009-06-11 10:47:33 -07:00
|
|
|
if (dumpAtomLeaks && *dumpAtomLeaks) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t leaked = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
2012-07-30 07:20:58 -07:00
|
|
|
gAtomTable.ops = nullptr;
|
2010-03-08 07:44:59 -08:00
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-11-08 15:29:09 -08:00
|
|
|
AtomImpl::AtomImpl(const nsAString& aString, uint32_t aHash)
|
2010-03-08 07:44:59 -08:00
|
|
|
{
|
2010-03-08 07:45:00 -08:00
|
|
|
mLength = aString.Length();
|
2013-04-22 04:13:22 -07:00
|
|
|
nsRefPtr<nsStringBuffer> buf = nsStringBuffer::FromString(aString);
|
2010-03-08 07:44:59 -08:00
|
|
|
if (buf) {
|
2014-01-04 07:02:17 -08:00
|
|
|
mString = static_cast<char16_t*>(buf->Data());
|
2013-04-22 04:13:22 -07:00
|
|
|
} else {
|
2014-01-04 07:02:17 -08:00
|
|
|
buf = nsStringBuffer::Alloc((mLength + 1) * sizeof(char16_t));
|
|
|
|
mString = static_cast<char16_t*>(buf->Data());
|
2010-03-08 07:45:00 -08:00
|
|
|
CopyUnicodeTo(aString, 0, mString, mLength);
|
2014-01-04 07:02:17 -08:00
|
|
|
mString[mLength] = char16_t(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-03-08 07:45:00 -08:00
|
|
|
|
2013-11-08 15:29:09 -08:00
|
|
|
mHash = aHash;
|
|
|
|
MOZ_ASSERT(mHash == HashString(mString, mLength));
|
2012-02-29 18:40:51 -08:00
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
NS_ASSERTION(mString[mLength] == char16_t(0), "null terminated");
|
2014-07-09 08:15:21 -07:00
|
|
|
NS_ASSERTION(buf && buf->StorageSize() >= (mLength + 1) * sizeof(char16_t),
|
2010-03-08 07:45:00 -08:00
|
|
|
"enough storage");
|
|
|
|
NS_ASSERTION(Equals(aString), "correct data");
|
2013-04-22 04:13:22 -07:00
|
|
|
|
|
|
|
// Take ownership of buffer
|
2014-03-15 12:00:16 -07:00
|
|
|
mozilla::unused << buf.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
AtomImpl::AtomImpl(nsStringBuffer* aStringBuffer, uint32_t aLength,
|
2013-11-08 15:29:09 -08:00
|
|
|
uint32_t aHash)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-08 07:45:00 -08:00
|
|
|
mLength = aLength;
|
2014-01-04 07:02:17 -08:00
|
|
|
mString = static_cast<char16_t*>(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
|
|
|
|
2013-11-08 15:29:09 -08:00
|
|
|
mHash = aHash;
|
|
|
|
MOZ_ASSERT(mHash == HashString(mString, mLength));
|
2012-02-29 18:40:51 -08:00
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
NS_ASSERTION(mString[mLength] == char16_t(0), "null terminated");
|
2010-03-08 08:03:55 -08:00
|
|
|
NS_ASSERTION(aStringBuffer &&
|
2014-07-09 08:15:21 -07:00
|
|
|
aStringBuffer->StorageSize() == (mLength + 1) * sizeof(char16_t),
|
2010-03-08 08:03:55 -08:00
|
|
|
"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()) {
|
2013-11-08 15:29:09 -08:00
|
|
|
AtomTableKey key(mString, mLength, mHash);
|
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
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(AtomImpl, nsIAtom)
|
2008-02-21 12:39:20 -08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
PermanentAtomImpl::AddRef()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-02-07 21:54:20 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
2007-03-22 10:30:00 -07:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
PermanentAtomImpl::Release()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-02-07 21:54:20 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
2007-03-22 10:30:00 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
/* virtual */ bool
|
2007-03-22 10:30:00 -07:00
|
|
|
AtomImpl::IsPermanent()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
/* virtual */ bool
|
2007-03-22 10:30:00 -07:00
|
|
|
PermanentAtomImpl::IsPermanent()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
void*
|
|
|
|
PermanentAtomImpl::operator new(size_t aSize, AtomImpl* aAtom) CPP_THROW_NEW
|
|
|
|
{
|
2013-02-07 21:54:20 -08:00
|
|
|
MOZ_ASSERT(!aAtom->IsPermanent(),
|
|
|
|
"converting atom that's already permanent");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Just let the constructor overwrite the vtable pointer.
|
|
|
|
return aAtom;
|
|
|
|
}
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHODIMP_(bool)
|
2010-03-08 07:45:00 -08:00
|
|
|
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
|
2011-09-28 23:19:26 -07:00
|
|
|
AtomImpl::ScriptableEquals(const nsAString& aString, bool* 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;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHODIMP_(bool)
|
2009-09-18 08:13:10 -07:00
|
|
|
AtomImpl::IsStaticAtom()
|
|
|
|
{
|
2010-03-08 07:44:59 -08:00
|
|
|
return IsPermanent();
|
2009-09-18 08:13:10 -07:00
|
|
|
}
|
|
|
|
|
2011-12-22 17:24:43 -08:00
|
|
|
size_t
|
2013-06-23 05:03:39 -07:00
|
|
|
AtomImpl::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
2011-12-22 17:24:43 -08:00
|
|
|
{
|
2012-01-25 00:52:51 -08:00
|
|
|
return aMallocSizeOf(this) +
|
2014-07-09 08:15:21 -07:00
|
|
|
nsStringBuffer::FromData(mString)->SizeOfIncludingThisIfUnshared(
|
|
|
|
aMallocSizeOf);
|
2011-12-22 17:24:43 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2011-12-22 17:24:43 -08:00
|
|
|
static size_t
|
2014-07-09 08:15:21 -07:00
|
|
|
SizeOfAtomTableEntryExcludingThis(PLDHashEntryHdr* aHdr,
|
2013-06-23 05:03:39 -07:00
|
|
|
MallocSizeOf aMallocSizeOf,
|
2014-07-09 08:15:21 -07:00
|
|
|
void* aArg)
|
2011-12-22 17:24:43 -08:00
|
|
|
{
|
|
|
|
AtomTableEntry* entry = static_cast<AtomTableEntry*>(aHdr);
|
|
|
|
return entry->mAtom->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2012-06-15 12:51:42 -07:00
|
|
|
static size_t
|
|
|
|
SizeOfStaticAtomTableEntryExcludingThis(const nsAString& aKey,
|
|
|
|
nsIAtom* const& aData,
|
2013-06-23 05:03:39 -07:00
|
|
|
MallocSizeOf aMallocSizeOf,
|
2012-06-15 12:51:42 -07:00
|
|
|
void* aArg)
|
|
|
|
{
|
|
|
|
return aKey.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2014-07-09 08:15:21 -07:00
|
|
|
NS_SizeOfAtomTablesIncludingThis(MallocSizeOf aMallocSizeOf)
|
|
|
|
{
|
2012-06-15 12:51:42 -07:00
|
|
|
size_t n = 0;
|
2011-12-22 17:24:43 -08:00
|
|
|
if (gAtomTable.ops) {
|
2014-07-09 08:15:21 -07:00
|
|
|
n += PL_DHashTableSizeOfExcludingThis(&gAtomTable,
|
|
|
|
SizeOfAtomTableEntryExcludingThis,
|
|
|
|
aMallocSizeOf);
|
2012-06-15 12:51:42 -07:00
|
|
|
}
|
|
|
|
if (gStaticAtomTable) {
|
|
|
|
n += gStaticAtomTable->SizeOfIncludingThis(SizeOfStaticAtomTableEntryExcludingThis,
|
|
|
|
aMallocSizeOf);
|
2011-12-22 17:24:43 -08:00
|
|
|
}
|
2012-11-09 00:46:34 -08:00
|
|
|
return n;
|
2011-12-22 17:24:43 -08:00
|
|
|
}
|
|
|
|
|
2008-06-03 15:25:31 -07:00
|
|
|
#define ATOM_HASHTABLE_INITIAL_SIZE 4096
|
|
|
|
|
2013-04-08 14:04:21 -07:00
|
|
|
static inline void
|
2011-12-22 17:24:43 -08:00
|
|
|
EnsureTableExists()
|
|
|
|
{
|
2014-02-27 10:04:09 -08:00
|
|
|
if (!gAtomTable.ops) {
|
|
|
|
PL_DHashTableInit(&gAtomTable, &AtomTableOps, 0,
|
|
|
|
sizeof(AtomTableEntry), ATOM_HASHTABLE_INITIAL_SIZE);
|
2011-12-22 17:24:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static inline AtomTableEntry*
|
2013-11-08 15:29:09 -08:00
|
|
|
GetAtomHashEntry(const char* aString, uint32_t aLength, uint32_t& aHash)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-02-07 21:54:20 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
2013-04-08 14:04:21 -07:00
|
|
|
EnsureTableExists();
|
2013-11-08 15:29:09 -08:00
|
|
|
AtomTableKey key(aString, aLength, aHash);
|
2014-07-09 08:15:21 -07:00
|
|
|
AtomTableEntry* e = static_cast<AtomTableEntry*>(
|
|
|
|
PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD));
|
2013-04-08 14:04:21 -07:00
|
|
|
if (!e) {
|
2013-11-25 12:06:17 -08:00
|
|
|
NS_ABORT_OOM(gAtomTable.entryCount * gAtomTable.entrySize);
|
2013-04-08 14:04:21 -07:00
|
|
|
}
|
|
|
|
return e;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline AtomTableEntry*
|
2014-01-04 07:02:17 -08:00
|
|
|
GetAtomHashEntry(const char16_t* aString, uint32_t aLength, uint32_t& aHash)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-02-07 21:54:20 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
2013-04-08 14:04:21 -07:00
|
|
|
EnsureTableExists();
|
2013-11-08 15:29:09 -08:00
|
|
|
AtomTableKey key(aString, aLength, aHash);
|
2014-07-09 08:15:21 -07:00
|
|
|
AtomTableEntry* e = static_cast<AtomTableEntry*>(
|
|
|
|
PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD));
|
2013-04-08 14:04:21 -07:00
|
|
|
if (!e) {
|
2013-11-25 12:06:17 -08:00
|
|
|
NS_ABORT_OOM(gAtomTable.entryCount * gAtomTable.entrySize);
|
2013-04-08 14:04:21 -07:00
|
|
|
}
|
|
|
|
return e;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-08 07:44:59 -08:00
|
|
|
class CheckStaticAtomSizes
|
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
CheckStaticAtomSizes()
|
|
|
|
{
|
2013-07-18 10:59:53 -07: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 07:44:59 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
RegisterStaticAtoms(const nsStaticAtom* aAtoms, uint32_t aAtomCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
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
|
2014-07-09 08:15:21 -07:00
|
|
|
|
2009-09-18 08:13:10 -07:00
|
|
|
if (!gStaticAtomTable && !gStaticAtomTableSealed) {
|
|
|
|
gStaticAtomTable = new nsDataHashtable<nsStringHashKey, nsIAtom*>();
|
|
|
|
}
|
2014-07-09 08:15:21 -07:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aAtomCount; ++i) {
|
2014-01-04 07:02:17 -08:00
|
|
|
NS_ASSERTION(nsCRT::IsAscii((char16_t*)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
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t stringLen =
|
2014-01-04 07:02:17 -08:00
|
|
|
aAtoms[i].mStringBuffer->StorageSize() / sizeof(char16_t) - 1;
|
2007-07-11 13:46:43 -07:00
|
|
|
|
2013-11-08 15:29:09 -08:00
|
|
|
uint32_t hash = 0;
|
2014-07-09 08:15:21 -07:00
|
|
|
AtomTableEntry* he =
|
2014-01-04 07:02:17 -08:00
|
|
|
GetAtomHashEntry((char16_t*)aAtoms[i].mStringBuffer->Data(),
|
2013-11-08 15:29:09 -08:00
|
|
|
stringLen, hash);
|
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
|
|
|
}
|
2014-07-09 08:15:21 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
*aAtoms[i].mAtom = he->mAtom;
|
2014-07-09 08:15:21 -07:00
|
|
|
} else {
|
2010-03-08 07:44:59 -08:00
|
|
|
AtomImpl* atom = new PermanentAtomImpl(aAtoms[i].mStringBuffer,
|
2012-02-29 18:40:51 -08:00
|
|
|
stringLen,
|
2013-11-08 15:29:09 -08:00
|
|
|
hash);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-22 04:13:22 -07:00
|
|
|
already_AddRefed<nsIAtom>
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_NewAtom(const char* aUTF8String)
|
|
|
|
{
|
2007-07-11 13:46:43 -07:00
|
|
|
return NS_NewAtom(nsDependentCString(aUTF8String));
|
|
|
|
}
|
|
|
|
|
2013-04-22 04:13:22 -07:00
|
|
|
already_AddRefed<nsIAtom>
|
2007-07-11 13:46:43 -07:00
|
|
|
NS_NewAtom(const nsACString& aUTF8String)
|
|
|
|
{
|
2013-11-08 15:29:09 -08:00
|
|
|
uint32_t hash = 0;
|
2014-07-09 08:15:21 -07:00
|
|
|
AtomTableEntry* he = GetAtomHashEntry(aUTF8String.Data(),
|
2013-11-08 15:29:09 -08:00
|
|
|
aUTF8String.Length(),
|
|
|
|
hash);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
if (he->mAtom) {
|
2013-04-22 04:13:22 -07:00
|
|
|
nsCOMPtr<nsIAtom> atom = he->mAtom;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-22 04:13:22 -07:00
|
|
|
return atom.forget();
|
2010-03-08 07:45:00 -08:00
|
|
|
}
|
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.
|
2012-02-29 18:40:51 -08:00
|
|
|
// Actually, now there is, sort of: ForgetSharedBuffer.
|
2010-03-08 07:45:00 -08:00
|
|
|
nsString str;
|
|
|
|
CopyUTF8toUTF16(aUTF8String, str);
|
2013-11-08 15:29:09 -08:00
|
|
|
nsRefPtr<AtomImpl> atom = new AtomImpl(str, hash);
|
2010-03-08 07:45:00 -08:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
he->mAtom = atom;
|
2010-03-08 07:44:59 -08:00
|
|
|
|
2013-04-22 04:13:22 -07:00
|
|
|
return atom.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-22 04:13:22 -07:00
|
|
|
already_AddRefed<nsIAtom>
|
2014-01-04 07:02:17 -08:00
|
|
|
NS_NewAtom(const char16_t* 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
|
|
|
}
|
|
|
|
|
2013-04-22 04:13:22 -07:00
|
|
|
already_AddRefed<nsIAtom>
|
2007-07-11 13:46:43 -07:00
|
|
|
NS_NewAtom(const nsAString& aUTF16String)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-08 15:29:09 -08:00
|
|
|
uint32_t hash = 0;
|
2014-07-09 08:15:21 -07:00
|
|
|
AtomTableEntry* he = GetAtomHashEntry(aUTF16String.Data(),
|
2013-11-08 15:29:09 -08:00
|
|
|
aUTF16String.Length(),
|
|
|
|
hash);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
if (he->mAtom) {
|
2013-04-22 04:13:22 -07:00
|
|
|
nsCOMPtr<nsIAtom> atom = he->mAtom;
|
2010-03-08 07:45:00 -08:00
|
|
|
|
2013-04-22 04:13:22 -07:00
|
|
|
return atom.forget();
|
2010-03-08 07:45:00 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-11-08 15:29:09 -08:00
|
|
|
nsRefPtr<AtomImpl> atom = new AtomImpl(aUTF16String, hash);
|
2010-03-08 07:45:00 -08:00
|
|
|
he->mAtom = atom;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-22 04:13:22 -07:00
|
|
|
return atom.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
nsIAtom*
|
2010-03-08 07:45:00 -08:00
|
|
|
NS_NewPermanentAtom(const nsAString& aUTF16String)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-08 15:29:09 -08:00
|
|
|
uint32_t hash = 0;
|
2014-07-09 08:15:21 -07:00
|
|
|
AtomTableEntry* he = GetAtomHashEntry(aUTF16String.Data(),
|
2013-11-08 15:29:09 -08:00
|
|
|
aUTF16String.Length(),
|
|
|
|
hash);
|
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);
|
|
|
|
}
|
2014-07-09 08:15:21 -07:00
|
|
|
} else {
|
2013-11-08 15:29:09 -08:00
|
|
|
atom = new PermanentAtomImpl(aUTF16String, hash);
|
2010-03-08 07:45:00 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
nsrefcnt
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_GetNumberOfAtoms(void)
|
|
|
|
{
|
|
|
|
return gAtomTable.entryCount;
|
|
|
|
}
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
nsIAtom*
|
2009-09-18 08:13:10 -07: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 07:20:58 -07:00
|
|
|
atom = nullptr;
|
2009-09-18 08:13:10 -07:00
|
|
|
}
|
|
|
|
return atom;
|
|
|
|
}
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
void
|
2009-09-18 08:13:10 -07:00
|
|
|
NS_SealStaticAtomTable()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
gStaticAtomTableSealed = true;
|
2009-09-18 08:13:10 -07:00
|
|
|
}
|