2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2001-10-21 03:19:07 +04:00
|
|
|
// vim:cindent:ts=2:et:sw=2:
|
2012-05-21 15:12:37 +04: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 05:38:36 +04:00
|
|
|
|
2011-12-20 01:58:30 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2012-06-06 03:51:58 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-12-06 21:47:28 +04:00
|
|
|
#include "mozilla/Compiler.h"
|
2012-03-13 02:53:18 +04:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-11-09 03:29:09 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2014-03-15 23:00:16 +04:00
|
|
|
#include "mozilla/unused.h"
|
2011-12-20 01:58:30 +04:00
|
|
|
|
1999-05-26 05:38:36 +04:00
|
|
|
#include "nsAtomTable.h"
|
2003-03-25 21:55:59 +03:00
|
|
|
#include "nsStaticAtom.h"
|
2013-09-23 21:25:00 +04:00
|
|
|
#include "nsString.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
#include "nsCRT.h"
|
2001-10-21 03:19:07 +04:00
|
|
|
#include "pldhash.h"
|
|
|
|
#include "prenv.h"
|
2007-08-07 23:16:52 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2009-09-18 19:13:10 +04:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2010-03-08 18:44:59 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2012-03-13 02:53:18 +04:00
|
|
|
#include "nsUnicharUtils.h"
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-12-06 21:47:28 +04: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-02 00:06:29 +04:00
|
|
|
#endif
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
/**
|
|
|
|
* The shared hash table for atom lookups.
|
2001-10-21 03:19:07 +04:00
|
|
|
*
|
|
|
|
* XXX This should be manipulated in a threadsafe way or we should make
|
|
|
|
* sure it's only manipulated from the main thread. Probably the latter
|
|
|
|
* is better, since the former would hurt performance.
|
|
|
|
*
|
2003-07-03 18:45:01 +04:00
|
|
|
* If |gAtomTable.ops| is 0, then the table is uninitialized.
|
1998-04-14 00:24:54 +04:00
|
|
|
*/
|
2001-10-21 03:19:07 +04:00
|
|
|
static PLDHashTable gAtomTable;
|
|
|
|
|
2009-09-18 19:13:10 +04:00
|
|
|
/**
|
2014-07-09 19:15:21 +04:00
|
|
|
* A hashtable of static atoms that existed at app startup. This hashtable helps
|
2009-09-18 19:13:10 +04:00
|
|
|
* nsHtml5AtomTable.
|
|
|
|
*/
|
|
|
|
static nsDataHashtable<nsStringHashKey, nsIAtom*>* gStaticAtomTable = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether it is still OK to add atoms to gStaticAtomTable.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool gStaticAtomTableSealed = false;
|
2009-09-18 19:13:10 +04:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
//----------------------------------------------------------------------
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2011-12-23 05:24:44 +04:00
|
|
|
/**
|
|
|
|
* Note that AtomImpl objects are sometimes converted into PermanentAtomImpl
|
|
|
|
* objects using placement new and just overwriting the vtable pointer.
|
|
|
|
*/
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
class AtomImpl : public nsIAtom
|
|
|
|
{
|
2011-12-23 05:24:44 +04:00
|
|
|
public:
|
2013-11-09 03:29:09 +04:00
|
|
|
AtomImpl(const nsAString& aString, uint32_t aHash);
|
2011-12-23 05:24:44 +04:00
|
|
|
|
|
|
|
// This is currently only used during startup when creating a permanent atom
|
|
|
|
// from NS_RegisterStaticAtoms
|
2013-11-09 03:29:09 +04:00
|
|
|
AtomImpl(nsStringBuffer* aData, uint32_t aLength, uint32_t aHash);
|
2011-12-23 05:24:44 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// This is only intended to be used when a normal atom is turned into a
|
|
|
|
// permanent one.
|
2014-07-09 19:15:21 +04:00
|
|
|
AtomImpl()
|
|
|
|
{
|
2011-12-23 05:24:44 +04: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 19:02:17 +04:00
|
|
|
NS_ASSERTION((mLength + 1) * sizeof(char16_t) <=
|
2011-12-23 05:24:44 +04: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 10:57:33 +04:00
|
|
|
enum { REFCNT_PERMANENT_SENTINEL = UINT32_MAX };
|
2011-12-23 05:24:44 +04:00
|
|
|
|
|
|
|
virtual bool IsPermanent();
|
|
|
|
|
|
|
|
// We can't use the virtual function in the base class destructor.
|
2014-07-09 19:15:21 +04:00
|
|
|
bool IsPermanentInDestructor()
|
|
|
|
{
|
2011-12-23 05:24:44 +04:00
|
|
|
return mRefCnt == REFCNT_PERMANENT_SENTINEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for |#ifdef NS_BUILD_REFCNT_LOGGING| access to reference count
|
|
|
|
nsrefcnt GetRefCount() { return mRefCnt; }
|
|
|
|
|
2014-07-31 10:19:23 +04:00
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf);
|
2011-12-23 05:24:44 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A non-refcounted implementation of nsIAtom.
|
|
|
|
*/
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
class PermanentAtomImpl MOZ_FINAL : public AtomImpl
|
|
|
|
{
|
2011-12-23 05:24:44 +04:00
|
|
|
public:
|
2012-03-01 06:40:51 +04:00
|
|
|
PermanentAtomImpl(const nsAString& aString, PLDHashNumber aKeyHash)
|
|
|
|
: AtomImpl(aString, aKeyHash)
|
2014-07-09 19:15:21 +04:00
|
|
|
{
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
PermanentAtomImpl(nsStringBuffer* aData, uint32_t aLength,
|
2012-03-01 06:40:51 +04:00
|
|
|
PLDHashNumber aKeyHash)
|
|
|
|
: AtomImpl(aData, aLength, aKeyHash)
|
2014-07-09 19:15:21 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
PermanentAtomImpl() {}
|
2011-12-23 05:24:44 +04:00
|
|
|
|
|
|
|
~PermanentAtomImpl();
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHOD_(MozExternalRefCountType) AddRef();
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType) Release();
|
2011-12-23 05:24:44 +04: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 19:15:21 +04:00
|
|
|
void* operator new(size_t aSize, AtomImpl* aAtom) CPP_THROW_NEW;
|
|
|
|
void* operator new(size_t aSize) CPP_THROW_NEW
|
2011-12-23 05:24:44 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
return ::operator new(aSize);
|
2011-12-23 05:24:44 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
struct AtomTableEntry : public PLDHashEntryHdr
|
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomImpl* mAtom;
|
|
|
|
};
|
2005-11-04 22:52:18 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
struct AtomTableKey
|
|
|
|
{
|
2014-07-31 10:22:40 +04:00
|
|
|
AtomTableKey(const char16_t* aUTF16String, uint32_t aLength, uint32_t aHash)
|
|
|
|
: mUTF16String(aUTF16String)
|
|
|
|
, mUTF8String(nullptr)
|
|
|
|
, mLength(aLength)
|
|
|
|
, mHash(aHash)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mHash == HashString(mUTF16String, mLength));
|
|
|
|
}
|
|
|
|
|
|
|
|
AtomTableKey(const char* aUTF8String, uint32_t aLength, uint32_t aHash)
|
|
|
|
: mUTF16String(nullptr)
|
|
|
|
, mUTF8String(aUTF8String)
|
|
|
|
, mLength(aLength)
|
|
|
|
, mHash(aHash)
|
|
|
|
{
|
|
|
|
mozilla::DebugOnly<bool> err;
|
|
|
|
MOZ_ASSERT(aHash == HashUTF8AsUTF16(mUTF8String, mLength, &err));
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
AtomTableKey(const char16_t* aUTF16String, uint32_t aLength,
|
2014-07-31 10:22:40 +04:00
|
|
|
uint32_t* aHashOut)
|
2014-07-09 19:15:21 +04:00
|
|
|
: mUTF16String(aUTF16String)
|
|
|
|
, mUTF8String(nullptr)
|
|
|
|
, mLength(aLength)
|
2005-11-04 22:52:18 +03:00
|
|
|
{
|
2014-07-31 10:22:40 +04:00
|
|
|
mHash = HashString(mUTF16String, mLength);
|
|
|
|
*aHashOut = mHash;
|
2005-11-04 22:52:18 +03:00
|
|
|
}
|
|
|
|
|
2014-07-31 10:22:40 +04:00
|
|
|
AtomTableKey(const char* aUTF8String, uint32_t aLength, uint32_t* aHashOut)
|
2014-07-09 19:15:21 +04:00
|
|
|
: mUTF16String(nullptr)
|
|
|
|
, mUTF8String(aUTF8String)
|
|
|
|
, mLength(aLength)
|
2005-11-04 22:52:18 +03:00
|
|
|
{
|
2014-07-31 10:22:40 +04:00
|
|
|
bool err;
|
|
|
|
mHash = HashUTF8AsUTF16(mUTF8String, mLength, &err);
|
|
|
|
if (err) {
|
|
|
|
mUTF8String = nullptr;
|
|
|
|
mLength = 0;
|
|
|
|
mHash = 0;
|
2013-11-09 03:29:09 +04:00
|
|
|
}
|
2014-07-31 10:22:40 +04:00
|
|
|
*aHashOut = mHash;
|
2005-11-04 22:52:18 +03:00
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* mUTF16String;
|
2010-03-08 18:45:00 +03:00
|
|
|
const char* mUTF8String;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mLength;
|
2013-11-09 03:29:09 +04:00
|
|
|
uint32_t mHash;
|
2003-03-25 21:55:59 +03:00
|
|
|
};
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashNumber
|
2014-07-09 19:15:21 +04:00
|
|
|
AtomTableGetHash(PLDHashTable* aTable, const void* aKey)
|
2005-11-04 22:52:18 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
const AtomTableKey* k = static_cast<const AtomTableKey*>(aKey);
|
2013-11-09 03:29:09 +04:00
|
|
|
return k->mHash;
|
2001-10-21 03:19:07 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2014-07-09 19:15:21 +04:00
|
|
|
AtomTableMatchKey(PLDHashTable* aTable, const PLDHashEntryHdr* aEntry,
|
|
|
|
const void* aKey)
|
2001-10-21 03:19:07 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
const AtomTableEntry* he = static_cast<const AtomTableEntry*>(aEntry);
|
|
|
|
const AtomTableKey* k = static_cast<const AtomTableKey*>(aKey);
|
2005-11-04 22:52:18 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
if (k->mUTF8String) {
|
2005-11-04 22:52:18 +03:00
|
|
|
return
|
2010-03-08 18:45:00 +03:00
|
|
|
CompareUTF8toUTF16(nsDependentCSubstring(k->mUTF8String,
|
|
|
|
k->mUTF8String + k->mLength),
|
|
|
|
nsDependentAtomString(he->mAtom)) == 0;
|
2007-07-12 00:46:43 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t length = he->mAtom->GetLength();
|
2010-03-08 18:45:00 +03:00
|
|
|
if (length != k->mLength) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-04 22:52:18 +03:00
|
|
|
}
|
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
return memcmp(he->mAtom->GetUTF16String(),
|
2014-01-04 19:02:17 +04:00
|
|
|
k->mUTF16String, length * sizeof(char16_t)) == 0;
|
2001-10-21 03:19:07 +04:00
|
|
|
}
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static void
|
2014-07-09 19:15:21 +04:00
|
|
|
AtomTableClearEntry(PLDHashTable* aTable, PLDHashEntryHdr* aEntry)
|
2001-10-21 03:19:07 +04:00
|
|
|
{
|
2010-03-08 18:44:59 +03:00
|
|
|
// Normal |AtomImpl| atoms are deleted when their refcount hits 0, and
|
|
|
|
// they then remove themselves from the table. In other words, they
|
|
|
|
// are owned by the callers who own references to them.
|
|
|
|
// |PermanentAtomImpl| permanent atoms ignore their refcount and are
|
|
|
|
// deleted when they are removed from the table at table destruction.
|
|
|
|
// In other words, they are owned by the atom table.
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
AtomImpl* atom = static_cast<AtomTableEntry*>(aEntry)->mAtom;
|
2010-03-08 18:45:00 +03:00
|
|
|
if (atom->IsPermanent()) {
|
|
|
|
// Note that the cast here is important since AtomImpls doesn't have a
|
|
|
|
// virtual dtor.
|
2010-03-08 18:44:59 +03:00
|
|
|
delete static_cast<PermanentAtomImpl*>(atom);
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
2001-10-21 03:19:07 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2014-07-09 19:15:21 +04:00
|
|
|
AtomTableInitEntry(PLDHashTable* aTable, PLDHashEntryHdr* aEntry,
|
|
|
|
const void* aKey)
|
2007-07-12 00:46:43 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
static_cast<AtomTableEntry*>(aEntry)->mAtom = nullptr;
|
2007-07-12 00:46:43 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2007-07-12 00:46:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-04 19:10:37 +04:00
|
|
|
static const PLDHashTableOps AtomTableOps = {
|
2001-10-21 03:19:07 +04:00
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
2005-11-04 22:52:18 +03:00
|
|
|
AtomTableGetHash,
|
2001-10-21 03:19:07 +04:00
|
|
|
AtomTableMatchKey,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
AtomTableClearEntry,
|
|
|
|
PL_DHashFinalizeStub,
|
2007-07-12 00:46:43 +04:00
|
|
|
AtomTableInitEntry
|
2001-10-21 03:19:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashOperator
|
2014-07-09 19:15:21 +04:00
|
|
|
DumpAtomLeaks(PLDHashTable* aTable, PLDHashEntryHdr* aEntryHdr,
|
|
|
|
uint32_t aIndex, void* aArg)
|
2001-10-21 03:19:07 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
AtomTableEntry* entry = static_cast<AtomTableEntry*>(aEntryHdr);
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomImpl* atom = entry->mAtom;
|
2001-10-21 03:19:07 +04:00
|
|
|
if (!atom->IsPermanent()) {
|
2014-07-09 19:15:21 +04:00
|
|
|
++*static_cast<uint32_t*>(aArg);
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString str;
|
2010-03-08 18:45:00 +03:00
|
|
|
atom->ToUTF8String(str);
|
|
|
|
fputs(str.get(), stdout);
|
1999-10-01 01:40:00 +04:00
|
|
|
fputs("\n", stdout);
|
|
|
|
}
|
2001-10-21 03:19:07 +04:00
|
|
|
return PL_DHASH_NEXT;
|
1999-10-01 01:40:00 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-03-25 21:55:59 +03:00
|
|
|
static inline
|
2014-07-09 19:15:21 +04:00
|
|
|
void
|
|
|
|
PromoteToPermanent(AtomImpl* aAtom)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
{
|
|
|
|
nsrefcnt refcount = aAtom->GetRefCount();
|
|
|
|
do {
|
|
|
|
NS_LOG_RELEASE(aAtom, --refcount, "AtomImpl");
|
|
|
|
} while (refcount);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
aAtom = new (aAtom) PermanentAtomImpl();
|
|
|
|
}
|
|
|
|
|
2005-11-04 22:52:18 +03:00
|
|
|
void
|
|
|
|
NS_PurgeAtomTable()
|
1999-10-01 01:40:00 +04:00
|
|
|
{
|
2009-09-18 19:13:10 +04:00
|
|
|
delete gStaticAtomTable;
|
|
|
|
|
2003-07-03 18:45:01 +04:00
|
|
|
if (gAtomTable.ops) {
|
2001-10-21 03:19:07 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-09 19:15:21 +04:00
|
|
|
const char* dumpAtomLeaks = PR_GetEnv("MOZ_DUMP_ATOM_LEAKS");
|
2009-06-11 21:47:33 +04:00
|
|
|
if (dumpAtomLeaks && *dumpAtomLeaks) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t leaked = 0;
|
2001-10-21 03:19:07 +04: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-10-01 01:40:00 +04:00
|
|
|
}
|
|
|
|
#endif
|
2001-10-21 03:19:07 +04:00
|
|
|
PL_DHashTableFinish(&gAtomTable);
|
|
|
|
gAtomTable.entryCount = 0;
|
2012-07-30 18:20:58 +04:00
|
|
|
gAtomTable.ops = nullptr;
|
2010-03-08 18:44:59 +03:00
|
|
|
}
|
|
|
|
}
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2013-11-09 03:29:09 +04:00
|
|
|
AtomImpl::AtomImpl(const nsAString& aString, uint32_t aHash)
|
2010-03-08 18:44:59 +03:00
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
mLength = aString.Length();
|
2013-04-22 15:13:22 +04:00
|
|
|
nsRefPtr<nsStringBuffer> buf = nsStringBuffer::FromString(aString);
|
2010-03-08 18:44:59 +03:00
|
|
|
if (buf) {
|
2014-01-04 19:02:17 +04:00
|
|
|
mString = static_cast<char16_t*>(buf->Data());
|
2013-04-22 15:13:22 +04:00
|
|
|
} else {
|
2014-01-04 19:02:17 +04:00
|
|
|
buf = nsStringBuffer::Alloc((mLength + 1) * sizeof(char16_t));
|
|
|
|
mString = static_cast<char16_t*>(buf->Data());
|
2010-03-08 18:45:00 +03:00
|
|
|
CopyUnicodeTo(aString, 0, mString, mLength);
|
2014-01-04 19:02:17 +04:00
|
|
|
mString[mLength] = char16_t(0);
|
1999-10-01 01:40:00 +04:00
|
|
|
}
|
2010-03-08 18:45:00 +03:00
|
|
|
|
2013-11-09 03:29:09 +04:00
|
|
|
mHash = aHash;
|
|
|
|
MOZ_ASSERT(mHash == HashString(mString, mLength));
|
2012-03-01 06:40:51 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
NS_ASSERTION(mString[mLength] == char16_t(0), "null terminated");
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_ASSERTION(buf && buf->StorageSize() >= (mLength + 1) * sizeof(char16_t),
|
2010-03-08 18:45:00 +03:00
|
|
|
"enough storage");
|
|
|
|
NS_ASSERTION(Equals(aString), "correct data");
|
2013-04-22 15:13:22 +04:00
|
|
|
|
|
|
|
// Take ownership of buffer
|
2014-03-15 23:00:16 +04:00
|
|
|
mozilla::unused << buf.forget();
|
1999-10-01 01:40:00 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
AtomImpl::AtomImpl(nsStringBuffer* aStringBuffer, uint32_t aLength,
|
2013-11-09 03:29:09 +04:00
|
|
|
uint32_t aHash)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
mLength = aLength;
|
2014-01-04 19:02:17 +04:00
|
|
|
mString = static_cast<char16_t*>(aStringBuffer->Data());
|
2010-03-08 18:44:59 +03:00
|
|
|
// Technically we could currently avoid doing this addref by instead making
|
|
|
|
// the static atom buffers have an initial refcount of 2.
|
|
|
|
aStringBuffer->AddRef();
|
2010-03-08 18:45:00 +03:00
|
|
|
|
2013-11-09 03:29:09 +04:00
|
|
|
mHash = aHash;
|
|
|
|
MOZ_ASSERT(mHash == HashString(mString, mLength));
|
2012-03-01 06:40:51 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
NS_ASSERTION(mString[mLength] == char16_t(0), "null terminated");
|
2010-03-08 19:03:55 +03:00
|
|
|
NS_ASSERTION(aStringBuffer &&
|
2014-07-09 19:15:21 +04:00
|
|
|
aStringBuffer->StorageSize() == (mLength + 1) * sizeof(char16_t),
|
2010-03-08 19:03:55 +03:00
|
|
|
"correct storage");
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
AtomImpl::~AtomImpl()
|
|
|
|
{
|
2003-07-03 18:45:01 +04:00
|
|
|
NS_PRECONDITION(gAtomTable.ops, "uninitialized atom hashtable");
|
2001-10-21 03:19:07 +04:00
|
|
|
// Permanent atoms are removed from the hashtable at shutdown, and we
|
|
|
|
// don't want to remove them twice. See comment above in
|
|
|
|
// |AtomTableClearEntry|.
|
2006-05-06 21:49:21 +04:00
|
|
|
if (!IsPermanentInDestructor()) {
|
2013-11-09 03:29:09 +04:00
|
|
|
AtomTableKey key(mString, mLength, mHash);
|
2005-11-04 22:52:18 +03:00
|
|
|
PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_REMOVE);
|
2001-10-21 03:19:07 +04:00
|
|
|
if (gAtomTable.entryCount == 0) {
|
|
|
|
PL_DHashTableFinish(&gAtomTable);
|
|
|
|
NS_ASSERTION(gAtomTable.entryCount == 0,
|
|
|
|
"PL_DHashTableFinish changed the entry count");
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-08 18:44:59 +03:00
|
|
|
|
|
|
|
nsStringBuffer::FromData(mString)->Release();
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(AtomImpl, nsIAtom)
|
2008-02-21 23:39:20 +03:00
|
|
|
|
2006-05-06 21:49:21 +04:00
|
|
|
PermanentAtomImpl::~PermanentAtomImpl()
|
|
|
|
{
|
|
|
|
// So we can tell if we were permanent while running the base class dtor.
|
2006-04-20 01:46:47 +04:00
|
|
|
mRefCnt = REFCNT_PERMANENT_SENTINEL;
|
|
|
|
}
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
PermanentAtomImpl::AddRef()
|
2001-10-21 03:19:07 +04:00
|
|
|
{
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
2001-10-21 03:19:07 +04:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
PermanentAtomImpl::Release()
|
2001-10-21 03:19:07 +04:00
|
|
|
{
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
2001-10-21 03:19:07 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
/* virtual */ bool
|
2006-05-06 21:49:21 +04:00
|
|
|
AtomImpl::IsPermanent()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-05-06 21:49:21 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
/* virtual */ bool
|
2006-05-06 21:49:21 +04:00
|
|
|
PermanentAtomImpl::IsPermanent()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2006-05-06 21:49:21 +04:00
|
|
|
}
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
void*
|
|
|
|
PermanentAtomImpl::operator new(size_t aSize, AtomImpl* aAtom) CPP_THROW_NEW
|
|
|
|
{
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(!aAtom->IsPermanent(),
|
|
|
|
"converting atom that's already permanent");
|
2001-10-21 03:19:07 +04:00
|
|
|
|
|
|
|
// Just let the constructor overwrite the vtable pointer.
|
|
|
|
return aAtom;
|
|
|
|
}
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_IMETHODIMP
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomImpl::ScriptableToString(nsAString& aBuf)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
nsStringBuffer::FromData(mString)->ToString(mLength, aBuf);
|
2003-03-25 21:55:59 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AtomImpl::ToUTF8String(nsACString& aBuf)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
CopyUTF16toUTF8(nsDependentString(mString, mLength), aBuf);
|
1999-08-21 11:07:00 +04:00
|
|
|
return NS_OK;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP_(bool)
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomImpl::EqualsUTF8(const nsACString& aString)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
return CompareUTF8toUTF16(aString,
|
|
|
|
nsDependentString(mString, mLength)) == 0;
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
AtomImpl::ScriptableEquals(const nsAString& aString, bool* aResult)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
2010-03-08 18:45:00 +03:00
|
|
|
*aResult = aString.Equals(nsDependentString(mString, mLength));
|
2003-03-25 21:55:59 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP_(bool)
|
2009-09-18 19:13:10 +04:00
|
|
|
AtomImpl::IsStaticAtom()
|
|
|
|
{
|
2010-03-08 18:44:59 +03:00
|
|
|
return IsPermanent();
|
2009-09-18 19:13:10 +04:00
|
|
|
}
|
|
|
|
|
2011-12-23 05:24:43 +04:00
|
|
|
size_t
|
2014-07-31 10:19:23 +04:00
|
|
|
AtomImpl::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf)
|
2011-12-23 05:24:43 +04:00
|
|
|
{
|
2014-07-31 10:19:23 +04:00
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
|
|
|
|
// Don't measure static atoms. Nb: here "static" means "permanent", and while
|
|
|
|
// it's not guaranteed that permanent atoms are actually stored in static
|
|
|
|
// data, it is very likely. And we don't want to call |aMallocSizeOf| on
|
|
|
|
// static data, so we err on the side of caution.
|
|
|
|
if (!IsStaticAtom()) {
|
|
|
|
n += nsStringBuffer::FromData(mString)->SizeOfIncludingThisIfUnshared(
|
2014-07-09 19:15:21 +04:00
|
|
|
aMallocSizeOf);
|
2014-07-31 10:19:23 +04:00
|
|
|
}
|
|
|
|
return n;
|
2011-12-23 05:24:43 +04:00
|
|
|
}
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2011-12-23 05:24:43 +04:00
|
|
|
static size_t
|
2014-07-09 19:15:21 +04:00
|
|
|
SizeOfAtomTableEntryExcludingThis(PLDHashEntryHdr* aHdr,
|
2013-06-23 16:03:39 +04:00
|
|
|
MallocSizeOf aMallocSizeOf,
|
2014-07-09 19:15:21 +04:00
|
|
|
void* aArg)
|
2011-12-23 05:24:43 +04:00
|
|
|
{
|
|
|
|
AtomTableEntry* entry = static_cast<AtomTableEntry*>(aHdr);
|
|
|
|
return entry->mAtom->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2014-07-31 10:06:57 +04:00
|
|
|
void
|
|
|
|
NS_SizeOfAtomTablesIncludingThis(MallocSizeOf aMallocSizeOf,
|
|
|
|
size_t* aMain, size_t* aStatic)
|
|
|
|
{
|
|
|
|
*aMain = gAtomTable.ops
|
|
|
|
? PL_DHashTableSizeOfExcludingThis(&gAtomTable,
|
|
|
|
SizeOfAtomTableEntryExcludingThis,
|
|
|
|
aMallocSizeOf)
|
|
|
|
: 0;
|
|
|
|
|
2014-07-31 10:19:23 +04:00
|
|
|
// The atoms in the this table are almost certainly stored in static data, so
|
|
|
|
// we don't need a SizeOfEntry function.
|
2014-07-31 10:06:57 +04:00
|
|
|
*aStatic = gStaticAtomTable
|
2014-07-31 10:19:23 +04:00
|
|
|
? gStaticAtomTable->SizeOfIncludingThis(nullptr, aMallocSizeOf)
|
2014-07-31 10:06:57 +04:00
|
|
|
: 0;
|
2011-12-23 05:24:43 +04:00
|
|
|
}
|
|
|
|
|
2008-06-04 02:25:31 +04:00
|
|
|
#define ATOM_HASHTABLE_INITIAL_SIZE 4096
|
|
|
|
|
2013-04-09 01:04:21 +04:00
|
|
|
static inline void
|
2011-12-23 05:24:43 +04:00
|
|
|
EnsureTableExists()
|
|
|
|
{
|
2014-02-27 22:04:09 +04:00
|
|
|
if (!gAtomTable.ops) {
|
|
|
|
PL_DHashTableInit(&gAtomTable, &AtomTableOps, 0,
|
|
|
|
sizeof(AtomTableEntry), ATOM_HASHTABLE_INITIAL_SIZE);
|
2011-12-23 05:24:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-04 22:52:18 +03:00
|
|
|
static inline AtomTableEntry*
|
2014-07-31 10:22:40 +04:00
|
|
|
GetAtomHashEntry(const char* aString, uint32_t aLength, uint32_t* aHashOut)
|
2005-11-04 22:52:18 +03:00
|
|
|
{
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
2013-04-09 01:04:21 +04:00
|
|
|
EnsureTableExists();
|
2014-07-31 10:22:40 +04:00
|
|
|
AtomTableKey key(aString, aLength, aHashOut);
|
2014-07-09 19:15:21 +04:00
|
|
|
AtomTableEntry* e = static_cast<AtomTableEntry*>(
|
|
|
|
PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD));
|
2013-04-09 01:04:21 +04:00
|
|
|
if (!e) {
|
2013-11-26 00:06:17 +04:00
|
|
|
NS_ABORT_OOM(gAtomTable.entryCount * gAtomTable.entrySize);
|
2013-04-09 01:04:21 +04:00
|
|
|
}
|
|
|
|
return e;
|
2005-11-04 22:52:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline AtomTableEntry*
|
2014-07-31 10:22:40 +04:00
|
|
|
GetAtomHashEntry(const char16_t* aString, uint32_t aLength, uint32_t* aHashOut)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
2013-04-09 01:04:21 +04:00
|
|
|
EnsureTableExists();
|
2014-07-31 10:22:40 +04:00
|
|
|
AtomTableKey key(aString, aLength, aHashOut);
|
2014-07-09 19:15:21 +04:00
|
|
|
AtomTableEntry* e = static_cast<AtomTableEntry*>(
|
|
|
|
PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD));
|
2013-04-09 01:04:21 +04:00
|
|
|
if (!e) {
|
2013-11-26 00:06:17 +04:00
|
|
|
NS_ABORT_OOM(gAtomTable.entryCount * gAtomTable.entrySize);
|
2013-04-09 01:04:21 +04:00
|
|
|
}
|
|
|
|
return e;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2010-03-08 18:44:59 +03:00
|
|
|
class CheckStaticAtomSizes
|
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
CheckStaticAtomSizes()
|
|
|
|
{
|
2013-07-18 21:59:53 +04: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 18:44:59 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
RegisterStaticAtoms(const nsStaticAtom* aAtoms, uint32_t aAtomCount)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
2009-09-18 19:13:10 +04:00
|
|
|
// this does three things:
|
2003-03-25 21:55:59 +03:00
|
|
|
// 1) wraps each static atom in a wrapper, if necessary
|
2005-11-04 22:52:18 +03:00
|
|
|
// 2) initializes the address pointed to by each mBits slot
|
2009-09-18 19:13:10 +04:00
|
|
|
// 3) puts the atom into the static atom table as well
|
2014-07-09 19:15:21 +04:00
|
|
|
|
2009-09-18 19:13:10 +04:00
|
|
|
if (!gStaticAtomTable && !gStaticAtomTableSealed) {
|
|
|
|
gStaticAtomTable = new nsDataHashtable<nsStringHashKey, nsIAtom*>();
|
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aAtomCount; ++i) {
|
2014-01-04 19:02:17 +04:00
|
|
|
NS_ASSERTION(nsCRT::IsAscii((char16_t*)aAtoms[i].mStringBuffer->Data()),
|
2003-03-25 21:55:59 +03:00
|
|
|
"Static atoms must be ASCII!");
|
2007-07-12 00:46:43 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t stringLen =
|
2014-01-04 19:02:17 +04:00
|
|
|
aAtoms[i].mStringBuffer->StorageSize() / sizeof(char16_t) - 1;
|
2007-07-12 00:46:43 +04:00
|
|
|
|
2014-07-31 10:22:40 +04:00
|
|
|
uint32_t hash;
|
2014-07-09 19:15:21 +04:00
|
|
|
AtomTableEntry* he =
|
2014-01-04 19:02:17 +04:00
|
|
|
GetAtomHashEntry((char16_t*)aAtoms[i].mStringBuffer->Data(),
|
2014-07-31 10:22:40 +04:00
|
|
|
stringLen, &hash);
|
2007-07-12 00:46:43 +04:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
if (he->mAtom) {
|
2003-03-25 21:55:59 +03:00
|
|
|
// there already is an atom with this name in the table.. but we
|
2005-11-04 22:52:18 +03:00
|
|
|
// still have to update mBits
|
2010-03-08 18:45:00 +03:00
|
|
|
if (!he->mAtom->IsPermanent()) {
|
2003-03-25 21:55:59 +03:00
|
|
|
// since we wanted to create a static atom but there is
|
|
|
|
// already one there, we convert it to a non-refcounting
|
|
|
|
// permanent atom
|
2010-03-08 18:45:00 +03:00
|
|
|
PromoteToPermanent(he->mAtom);
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
*aAtoms[i].mAtom = he->mAtom;
|
2014-07-09 19:15:21 +04:00
|
|
|
} else {
|
2010-03-08 18:44:59 +03:00
|
|
|
AtomImpl* atom = new PermanentAtomImpl(aAtoms[i].mStringBuffer,
|
2012-03-01 06:40:51 +04:00
|
|
|
stringLen,
|
2013-11-09 03:29:09 +04:00
|
|
|
hash);
|
2010-03-08 18:45:00 +03:00
|
|
|
he->mAtom = atom;
|
2010-03-08 18:44:59 +03:00
|
|
|
*aAtoms[i].mAtom = atom;
|
|
|
|
|
2009-09-18 19:13:10 +04:00
|
|
|
if (!gStaticAtomTableSealed) {
|
2010-03-08 18:45:00 +03:00
|
|
|
gStaticAtomTable->Put(nsAtomString(atom), atom);
|
2009-09-18 19:13:10 +04:00
|
|
|
}
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-22 15:13:22 +04:00
|
|
|
already_AddRefed<nsIAtom>
|
2005-11-04 22:52:18 +03:00
|
|
|
NS_NewAtom(const char* aUTF8String)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2007-07-12 00:46:43 +04:00
|
|
|
return NS_NewAtom(nsDependentCString(aUTF8String));
|
|
|
|
}
|
|
|
|
|
2013-04-22 15:13:22 +04:00
|
|
|
already_AddRefed<nsIAtom>
|
2007-07-12 00:46:43 +04:00
|
|
|
NS_NewAtom(const nsACString& aUTF8String)
|
|
|
|
{
|
2014-07-31 10:22:40 +04:00
|
|
|
uint32_t hash;
|
2014-07-09 19:15:21 +04:00
|
|
|
AtomTableEntry* he = GetAtomHashEntry(aUTF8String.Data(),
|
2013-11-09 03:29:09 +04:00
|
|
|
aUTF8String.Length(),
|
2014-07-31 10:22:40 +04:00
|
|
|
&hash);
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
if (he->mAtom) {
|
2013-04-22 15:13:22 +04:00
|
|
|
nsCOMPtr<nsIAtom> atom = he->mAtom;
|
2005-11-04 22:52:18 +03:00
|
|
|
|
2013-04-22 15:13:22 +04:00
|
|
|
return atom.forget();
|
2010-03-08 18:45:00 +03:00
|
|
|
}
|
2005-11-04 22:52:18 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
// This results in an extra addref/release of the nsStringBuffer.
|
|
|
|
// Unfortunately there doesn't seem to be any APIs to avoid that.
|
2012-03-01 06:40:51 +04:00
|
|
|
// Actually, now there is, sort of: ForgetSharedBuffer.
|
2010-03-08 18:45:00 +03:00
|
|
|
nsString str;
|
|
|
|
CopyUTF8toUTF16(aUTF8String, str);
|
2013-11-09 03:29:09 +04:00
|
|
|
nsRefPtr<AtomImpl> atom = new AtomImpl(str, hash);
|
2010-03-08 18:45:00 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
he->mAtom = atom;
|
2010-03-08 18:44:59 +03:00
|
|
|
|
2013-04-22 15:13:22 +04:00
|
|
|
return atom.forget();
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
|
|
|
|
2013-04-22 15:13:22 +04:00
|
|
|
already_AddRefed<nsIAtom>
|
2014-01-04 19:02:17 +04:00
|
|
|
NS_NewAtom(const char16_t* aUTF16String)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
2007-07-12 00:46:43 +04:00
|
|
|
return NS_NewAtom(nsDependentString(aUTF16String));
|
2005-11-04 22:52:18 +03:00
|
|
|
}
|
|
|
|
|
2013-04-22 15:13:22 +04:00
|
|
|
already_AddRefed<nsIAtom>
|
2007-07-12 00:46:43 +04:00
|
|
|
NS_NewAtom(const nsAString& aUTF16String)
|
2005-11-04 22:52:18 +03:00
|
|
|
{
|
2014-07-31 10:22:40 +04:00
|
|
|
uint32_t hash;
|
2014-07-09 19:15:21 +04:00
|
|
|
AtomTableEntry* he = GetAtomHashEntry(aUTF16String.Data(),
|
2013-11-09 03:29:09 +04:00
|
|
|
aUTF16String.Length(),
|
2014-07-31 10:22:40 +04:00
|
|
|
&hash);
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
if (he->mAtom) {
|
2013-04-22 15:13:22 +04:00
|
|
|
nsCOMPtr<nsIAtom> atom = he->mAtom;
|
2010-03-08 18:45:00 +03:00
|
|
|
|
2013-04-22 15:13:22 +04:00
|
|
|
return atom.forget();
|
2010-03-08 18:45:00 +03:00
|
|
|
}
|
2003-03-21 09:26:32 +03:00
|
|
|
|
2013-11-09 03:29:09 +04:00
|
|
|
nsRefPtr<AtomImpl> atom = new AtomImpl(aUTF16String, hash);
|
2010-03-08 18:45:00 +03:00
|
|
|
he->mAtom = atom;
|
2000-08-23 21:27:06 +04:00
|
|
|
|
2013-04-22 15:13:22 +04:00
|
|
|
return atom.forget();
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
2000-08-23 21:27:06 +04:00
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsIAtom*
|
2010-03-08 18:45:00 +03:00
|
|
|
NS_NewPermanentAtom(const nsAString& aUTF16String)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
2014-07-31 10:22:40 +04:00
|
|
|
uint32_t hash;
|
2014-07-09 19:15:21 +04:00
|
|
|
AtomTableEntry* he = GetAtomHashEntry(aUTF16String.Data(),
|
2013-11-09 03:29:09 +04:00
|
|
|
aUTF16String.Length(),
|
2014-07-31 10:22:40 +04:00
|
|
|
&hash);
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
AtomImpl* atom = he->mAtom;
|
|
|
|
if (atom) {
|
2001-10-21 03:19:07 +04:00
|
|
|
if (!atom->IsPermanent()) {
|
2003-03-25 21:55:59 +03:00
|
|
|
PromoteToPermanent(atom);
|
2001-10-21 03:19:07 +04:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
} else {
|
2013-11-09 03:29:09 +04:00
|
|
|
atom = new PermanentAtomImpl(aUTF16String, hash);
|
2010-03-08 18:45:00 +03:00
|
|
|
he->mAtom = atom;
|
|
|
|
}
|
2000-02-11 04:59:55 +03:00
|
|
|
|
2010-03-08 18:44:59 +03:00
|
|
|
// No need to addref since permanent atoms aren't refcounted anyway
|
|
|
|
return atom;
|
2001-10-21 03:19:07 +04:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsrefcnt
|
2005-11-04 22:52:18 +03:00
|
|
|
NS_GetNumberOfAtoms(void)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2001-10-21 03:19:07 +04:00
|
|
|
return gAtomTable.entryCount;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsIAtom*
|
2009-09-18 19:13:10 +04:00
|
|
|
NS_GetStaticAtom(const nsAString& aUTF16String)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(gStaticAtomTable, "Static atom table not created yet.");
|
|
|
|
NS_PRECONDITION(gStaticAtomTableSealed, "Static atom table not sealed yet.");
|
|
|
|
nsIAtom* atom;
|
|
|
|
if (!gStaticAtomTable->Get(aUTF16String, &atom)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
atom = nullptr;
|
2009-09-18 19:13:10 +04:00
|
|
|
}
|
|
|
|
return atom;
|
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
void
|
2009-09-18 19:13:10 +04:00
|
|
|
NS_SealStaticAtomTable()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
gStaticAtomTableSealed = true;
|
2009-09-18 19:13:10 +04:00
|
|
|
}
|