2015-04-09 20:25:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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"
|
2012-03-13 02:53:18 +04:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2016-05-26 04:50:37 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2013-11-09 03:29:09 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2017-01-26 23:43:38 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
2016-08-23 07:09:32 +03: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"
|
2015-09-16 06:49:53 +03:00
|
|
|
#include "PLDHashTable.h"
|
2001-10-21 03:19:07 +04:00
|
|
|
#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"
|
2017-01-26 23:43:38 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2003-03-25 21:55:59 +03:00
|
|
|
|
Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm.
This patch changes things so that dynamic atoms and static atoms have distinct
implementations. This is a step towards allowing dynamic atoms and static atoms
to have different layouts in memory, which will allow static atoms to be
represented more compactly.
Specifically, the patch does the following.
- It renames AtomImpl as DynamicAtom and PermanentAtomImpl as StaticAtom, and
the latter is no longer a subclass of the former. This required duplicating
some methods from the former into the latter: ScriptableToString(),
ToUTF8String(), ScriptableEquals(), IsStaticAtom(). (This duplication will
disappear in the future if the representations of dynamic atoms and static
atoms diverge. Indeed, SizeOfIncludingThis() is already different in the two
classes.)
- It replaces all mentions of "permanent"/"non-permanent" atoms with
"static"/"dynamic".
- In ~DynamicAtom() it removes the check that causes gAtomTable to be deleted
when it becomes empty. This will only happen at shutdown and so doesn't seem
useful.
- It documents better various things, especially the basics of the
dynamic/static split, the transmutation of dynamic atoms to static atoms, and
the details of the SizeOf functions.
--HG--
extra : rebase_source : dbf903012e70ebf1a43de1e1088db1bc1b8dd4f4
2016-04-01 03:18:06 +03:00
|
|
|
// There are two kinds of atoms handled by this module.
|
|
|
|
//
|
2017-09-21 05:36:53 +03:00
|
|
|
// - Dynamic: the atom itself is heap allocated, as is the nsStringBuffer it
|
|
|
|
// points to. |gAtomTable| holds weak references to dynamic atoms. When the
|
|
|
|
// refcount of a dynamic atom drops to zero, we increment a static counter.
|
|
|
|
// When that counter reaches a certain threshold, we iterate over the atom
|
|
|
|
// table, removing and deleting dynamic atoms with refcount zero. This allows
|
2016-05-27 03:55:53 +03:00
|
|
|
// us to avoid acquiring the atom table lock during normal refcounting.
|
Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm.
This patch changes things so that dynamic atoms and static atoms have distinct
implementations. This is a step towards allowing dynamic atoms and static atoms
to have different layouts in memory, which will allow static atoms to be
represented more compactly.
Specifically, the patch does the following.
- It renames AtomImpl as DynamicAtom and PermanentAtomImpl as StaticAtom, and
the latter is no longer a subclass of the former. This required duplicating
some methods from the former into the latter: ScriptableToString(),
ToUTF8String(), ScriptableEquals(), IsStaticAtom(). (This duplication will
disappear in the future if the representations of dynamic atoms and static
atoms diverge. Indeed, SizeOfIncludingThis() is already different in the two
classes.)
- It replaces all mentions of "permanent"/"non-permanent" atoms with
"static"/"dynamic".
- In ~DynamicAtom() it removes the check that causes gAtomTable to be deleted
when it becomes empty. This will only happen at shutdown and so doesn't seem
useful.
- It documents better various things, especially the basics of the
dynamic/static split, the transmutation of dynamic atoms to static atoms, and
the details of the SizeOf functions.
--HG--
extra : rebase_source : dbf903012e70ebf1a43de1e1088db1bc1b8dd4f4
2016-04-01 03:18:06 +03:00
|
|
|
//
|
2017-09-21 05:36:53 +03:00
|
|
|
// - Static: the atom itself is heap allocated, but it points to a static
|
|
|
|
// nsStringBuffer. |gAtomTable| effectively owns static atoms, because such
|
|
|
|
// atoms ignore all AddRef/Release calls, which ensures they stay alive until
|
Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm.
This patch changes things so that dynamic atoms and static atoms have distinct
implementations. This is a step towards allowing dynamic atoms and static atoms
to have different layouts in memory, which will allow static atoms to be
represented more compactly.
Specifically, the patch does the following.
- It renames AtomImpl as DynamicAtom and PermanentAtomImpl as StaticAtom, and
the latter is no longer a subclass of the former. This required duplicating
some methods from the former into the latter: ScriptableToString(),
ToUTF8String(), ScriptableEquals(), IsStaticAtom(). (This duplication will
disappear in the future if the representations of dynamic atoms and static
atoms diverge. Indeed, SizeOfIncludingThis() is already different in the two
classes.)
- It replaces all mentions of "permanent"/"non-permanent" atoms with
"static"/"dynamic".
- In ~DynamicAtom() it removes the check that causes gAtomTable to be deleted
when it becomes empty. This will only happen at shutdown and so doesn't seem
useful.
- It documents better various things, especially the basics of the
dynamic/static split, the transmutation of dynamic atoms to static atoms, and
the details of the SizeOf functions.
--HG--
extra : rebase_source : dbf903012e70ebf1a43de1e1088db1bc1b8dd4f4
2016-04-01 03:18:06 +03:00
|
|
|
// |gAtomTable| itself is destroyed whereupon they are explicitly deleted.
|
2016-05-26 04:50:37 +03:00
|
|
|
//
|
2017-08-24 04:10:04 +03:00
|
|
|
// Note that gAtomTable is used on multiple threads, and callers must acquire
|
|
|
|
// gAtomTableLock before touching it.
|
Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm.
This patch changes things so that dynamic atoms and static atoms have distinct
implementations. This is a step towards allowing dynamic atoms and static atoms
to have different layouts in memory, which will allow static atoms to be
represented more compactly.
Specifically, the patch does the following.
- It renames AtomImpl as DynamicAtom and PermanentAtomImpl as StaticAtom, and
the latter is no longer a subclass of the former. This required duplicating
some methods from the former into the latter: ScriptableToString(),
ToUTF8String(), ScriptableEquals(), IsStaticAtom(). (This duplication will
disappear in the future if the representations of dynamic atoms and static
atoms diverge. Indeed, SizeOfIncludingThis() is already different in the two
classes.)
- It replaces all mentions of "permanent"/"non-permanent" atoms with
"static"/"dynamic".
- In ~DynamicAtom() it removes the check that causes gAtomTable to be deleted
when it becomes empty. This will only happen at shutdown and so doesn't seem
useful.
- It documents better various things, especially the basics of the
dynamic/static split, the transmutation of dynamic atoms to static atoms, and
the details of the SizeOf functions.
--HG--
extra : rebase_source : dbf903012e70ebf1a43de1e1088db1bc1b8dd4f4
2016-04-01 03:18:06 +03:00
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2016-04-06 05:01:24 +03:00
|
|
|
//----------------------------------------------------------------------
|
2001-10-21 03:19:07 +04:00
|
|
|
|
2016-04-06 05:01:24 +03:00
|
|
|
class CheckStaticAtomSizes
|
2014-08-01 09:57:13 +04:00
|
|
|
{
|
2016-04-06 05:01:24 +03:00
|
|
|
CheckStaticAtomSizes()
|
2014-08-01 09:57:13 +04:00
|
|
|
{
|
2016-04-06 05:01:24 +03: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");
|
2014-08-01 09:57:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
//----------------------------------------------------------------------
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2017-09-25 09:30:32 +03:00
|
|
|
enum class GCKind {
|
|
|
|
RegularOperation,
|
|
|
|
Shutdown,
|
|
|
|
};
|
|
|
|
|
|
|
|
// This class encapsulates the functions that need access to nsAtom's private
|
|
|
|
// members.
|
|
|
|
class nsAtomFriend
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void RegisterStaticAtoms(const nsStaticAtom* aAtoms,
|
|
|
|
uint32_t aAtomCount);
|
|
|
|
|
|
|
|
static void AtomTableClearEntry(PLDHashTable* aTable,
|
|
|
|
PLDHashEntryHdr* aEntry);
|
|
|
|
|
|
|
|
static void GCAtomTableLocked(const MutexAutoLock& aProofOfLock,
|
|
|
|
GCKind aKind);
|
|
|
|
|
2017-09-26 01:33:21 +03:00
|
|
|
static already_AddRefed<nsAtom> Atomize(const nsACString& aUTF8String);
|
|
|
|
static already_AddRefed<nsAtom> Atomize(const nsAString& aUTF16String);
|
|
|
|
static already_AddRefed<nsAtom> AtomizeMainThread(const nsAString& aUTF16Str);
|
2017-09-25 09:30:32 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2017-09-06 08:06:16 +03:00
|
|
|
// gUnusedAtomCount is incremented when an atom loses its last reference
|
|
|
|
// (and thus turned into unused state), and decremented when an unused
|
|
|
|
// atom gets a reference again. The atom table relies on this value to
|
|
|
|
// schedule GC. This value can temporarily go below zero when multiple
|
|
|
|
// threads are operating the same atom, so it has to be signed so that
|
|
|
|
// we wouldn't use overflow value for comparison.
|
2017-09-26 01:33:21 +03:00
|
|
|
// See nsAtom::AddRef() and nsAtom::Release().
|
2017-09-06 08:06:16 +03:00
|
|
|
static Atomic<int32_t, ReleaseAcquire> gUnusedAtomCount(0);
|
2016-05-27 03:55:53 +03:00
|
|
|
|
2017-08-24 04:10:04 +03:00
|
|
|
#if defined(NS_BUILD_REFCNT_LOGGING)
|
|
|
|
// nsFakeStringBuffers don't really use the refcounting system, but we
|
|
|
|
// have to give a coherent series of addrefs and releases to the
|
|
|
|
// refcount logging system, or we'll hit assertions when running with
|
|
|
|
// XPCOM_MEM_LOG_CLASSES=nsStringBuffer.
|
|
|
|
class FakeBufferRefcountHelper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit FakeBufferRefcountHelper(nsStringBuffer* aBuffer)
|
|
|
|
: mBuffer(aBuffer)
|
|
|
|
{
|
|
|
|
// Account for the initial static refcount of 1, so that we don't
|
|
|
|
// hit a refcount logging assertion when this object first appears
|
|
|
|
// with a refcount of 2.
|
|
|
|
NS_LOG_ADDREF(aBuffer, 1, "nsStringBuffer", sizeof(nsStringBuffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
~FakeBufferRefcountHelper()
|
|
|
|
{
|
|
|
|
// We told the refcount logging system in the ctor that this
|
|
|
|
// object was created, so now we have to tell it that it was
|
|
|
|
// destroyed, to avoid leak reports. This may cause odd the
|
|
|
|
// refcount isn't actually 0.
|
|
|
|
NS_LOG_RELEASE(mBuffer, 0, "nsStringBuffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsStringBuffer* mBuffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
UniquePtr<nsTArray<FakeBufferRefcountHelper>> gFakeBuffers;
|
|
|
|
#endif
|
|
|
|
|
2017-09-21 07:02:05 +03:00
|
|
|
// This constructor is for dynamic atoms and HTML5 atoms.
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom::nsAtom(AtomKind aKind, const nsAString& aString, uint32_t aHash)
|
2017-09-25 09:30:32 +03:00
|
|
|
: mRefCnt(1)
|
2014-07-09 19:15:21 +04:00
|
|
|
{
|
2017-09-25 09:30:32 +03:00
|
|
|
mLength = aString.Length();
|
2017-09-21 07:02:05 +03:00
|
|
|
SetKind(aKind);
|
|
|
|
MOZ_ASSERT(IsDynamicAtom() || IsHTML5Atom());
|
2017-09-25 09:30:32 +03:00
|
|
|
RefPtr<nsStringBuffer> buf = nsStringBuffer::FromString(aString);
|
|
|
|
if (buf) {
|
|
|
|
mString = static_cast<char16_t*>(buf->Data());
|
|
|
|
} else {
|
|
|
|
const size_t size = (mLength + 1) * sizeof(char16_t);
|
|
|
|
buf = nsStringBuffer::Alloc(size);
|
|
|
|
if (MOZ_UNLIKELY(!buf)) {
|
|
|
|
// We OOM because atom allocations should be small and it's hard to
|
|
|
|
// handle them more gracefully in a constructor.
|
|
|
|
NS_ABORT_OOM(size);
|
2016-04-06 04:28:39 +03:00
|
|
|
}
|
2017-09-25 09:30:32 +03:00
|
|
|
mString = static_cast<char16_t*>(buf->Data());
|
|
|
|
CopyUnicodeTo(aString, 0, mString, mLength);
|
|
|
|
mString[mLength] = char16_t(0);
|
|
|
|
}
|
2016-04-06 04:28:39 +03:00
|
|
|
|
2017-09-25 09:30:32 +03:00
|
|
|
mHash = aHash;
|
2017-09-21 07:02:05 +03:00
|
|
|
MOZ_ASSERT_IF(IsDynamicAtom(), mHash == HashString(mString, mLength));
|
2016-04-06 04:28:39 +03:00
|
|
|
|
2017-09-25 09:30:32 +03:00
|
|
|
NS_ASSERTION(mString[mLength] == char16_t(0), "null terminated");
|
|
|
|
NS_ASSERTION(buf && buf->StorageSize() >= (mLength + 1) * sizeof(char16_t),
|
|
|
|
"enough storage");
|
|
|
|
NS_ASSERTION(Equals(aString), "correct data");
|
2016-04-06 04:28:39 +03:00
|
|
|
|
2017-09-25 09:30:32 +03:00
|
|
|
// Take ownership of buffer
|
|
|
|
mozilla::Unused << buf.forget();
|
|
|
|
}
|
2011-12-23 05:24:44 +04:00
|
|
|
|
2017-09-25 09:30:32 +03:00
|
|
|
// This constructor is for static atoms.
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom::nsAtom(nsStringBuffer* aStringBuffer, uint32_t aLength, uint32_t aHash)
|
2017-09-25 09:30:32 +03:00
|
|
|
{
|
|
|
|
mLength = aLength;
|
|
|
|
SetKind(AtomKind::StaticAtom);
|
|
|
|
mString = static_cast<char16_t*>(aStringBuffer->Data());
|
Bug 1350729 - Implement fake refcount logging for nsFakeStringBuffer. r=dbaron
Running with XPCOM_MEM_LOG_CLASSES=nsStringBuffer triggers an
assertion in refcount logging for nsFakeStringBuffers. These are given
an initial refcount of 1, without calling NS_LOG_ADDREF. Then,
AddRef() is called on these objects in StaticAtom::StaticAtom(), and
we tell the refcount logging system about the fake buffer, and that it
has a refcount of 0, triggering the assertion.
The first part of the fix is to call NS_LOG_ADDREF for this initial
refcount, in StaticAtom().
This first fix causes refcount logging to start reporting that the
fake string buffers leak, when XPCOM_MEM_LOG_CLASSES is not set. This
is because refcount logging is now getting told about these objects
being AddRefed at 1, which it takes to mean that an object is created.
To work around this issue, I add an array gFakeBuffers that contains
every fake string buffer we create, and tell the refcount logging
system that these objects are all being destroyed, when the atom table
is being shut down. This could result in some bogosity if the fake
buffers are "leaked" but hopefully this is still an improvement over
the current state.
MozReview-Commit-ID: 5AxoBYAlYRU
--HG--
extra : rebase_source : ba0763cb494894918141774025db525cea9f9c75
2017-07-14 20:20:23 +03:00
|
|
|
|
|
|
|
#if defined(NS_BUILD_REFCNT_LOGGING)
|
2017-09-25 09:30:32 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!gFakeBuffers) {
|
|
|
|
gFakeBuffers = MakeUnique<nsTArray<FakeBufferRefcountHelper>>();
|
|
|
|
}
|
|
|
|
gFakeBuffers->AppendElement(aStringBuffer);
|
Bug 1350729 - Implement fake refcount logging for nsFakeStringBuffer. r=dbaron
Running with XPCOM_MEM_LOG_CLASSES=nsStringBuffer triggers an
assertion in refcount logging for nsFakeStringBuffers. These are given
an initial refcount of 1, without calling NS_LOG_ADDREF. Then,
AddRef() is called on these objects in StaticAtom::StaticAtom(), and
we tell the refcount logging system about the fake buffer, and that it
has a refcount of 0, triggering the assertion.
The first part of the fix is to call NS_LOG_ADDREF for this initial
refcount, in StaticAtom().
This first fix causes refcount logging to start reporting that the
fake string buffers leak, when XPCOM_MEM_LOG_CLASSES is not set. This
is because refcount logging is now getting told about these objects
being AddRefed at 1, which it takes to mean that an object is created.
To work around this issue, I add an array gFakeBuffers that contains
every fake string buffer we create, and tell the refcount logging
system that these objects are all being destroyed, when the atom table
is being shut down. This could result in some bogosity if the fake
buffers are "leaked" but hopefully this is still an improvement over
the current state.
MozReview-Commit-ID: 5AxoBYAlYRU
--HG--
extra : rebase_source : ba0763cb494894918141774025db525cea9f9c75
2017-07-14 20:20:23 +03:00
|
|
|
#endif
|
|
|
|
|
2017-09-25 09:30:32 +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();
|
2016-04-06 04:28:39 +03:00
|
|
|
|
2017-09-25 09:30:32 +03:00
|
|
|
mHash = aHash;
|
|
|
|
MOZ_ASSERT(mHash == HashString(mString, mLength));
|
2016-04-06 04:28:39 +03:00
|
|
|
|
2017-09-25 09:30:32 +03:00
|
|
|
MOZ_ASSERT(mString[mLength] == char16_t(0), "null terminated");
|
|
|
|
MOZ_ASSERT(aStringBuffer &&
|
|
|
|
aStringBuffer->StorageSize() == (mLength + 1) * sizeof(char16_t),
|
|
|
|
"correct storage");
|
|
|
|
}
|
2011-12-23 05:24:44 +04:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom::~nsAtom()
|
2017-09-25 09:30:32 +03:00
|
|
|
{
|
2017-09-21 07:02:05 +03:00
|
|
|
if (!IsStaticAtom()) {
|
|
|
|
MOZ_ASSERT(IsDynamicAtom() || IsHTML5Atom());
|
2017-09-25 09:30:32 +03:00
|
|
|
nsStringBuffer::FromData(mString)->Release();
|
2017-08-24 04:10:04 +03:00
|
|
|
}
|
2017-09-25 09:30:32 +03:00
|
|
|
}
|
2016-04-06 05:01:24 +03:00
|
|
|
|
2017-09-26 01:33:21 +03:00
|
|
|
void
|
|
|
|
nsAtom::ToUTF8String(nsACString& aBuf) const
|
2016-04-06 05:01:24 +03:00
|
|
|
{
|
2017-09-21 07:02:05 +03:00
|
|
|
MOZ_ASSERT(!IsHTML5Atom(), "Called ToUTF8String() on an HTML5 atom");
|
2016-04-06 05:01:24 +03:00
|
|
|
CopyUTF16toUTF8(nsDependentString(mString, mLength), aBuf);
|
|
|
|
}
|
|
|
|
|
2017-09-26 01:33:21 +03:00
|
|
|
size_t
|
|
|
|
nsAtom::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
2016-04-06 05:01:24 +03:00
|
|
|
{
|
2017-09-21 07:02:05 +03:00
|
|
|
MOZ_ASSERT(!IsHTML5Atom(), "Called SizeOfIncludingThis() on an HTML5 atom");
|
2016-04-06 05:01:24 +03:00
|
|
|
size_t n = aMallocSizeOf(this);
|
2017-08-24 04:10:04 +03:00
|
|
|
// String buffers pointed to by static atoms are in static memory, and so
|
|
|
|
// are not measured here.
|
|
|
|
if (IsDynamicAtom()) {
|
|
|
|
n += nsStringBuffer::FromData(mString)->SizeOfIncludingThisIfUnshared(
|
|
|
|
aMallocSizeOf);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(IsStaticAtom());
|
|
|
|
}
|
2016-04-06 05:01:24 +03:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The shared hash table for atom lookups.
|
|
|
|
*
|
2016-05-26 04:50:37 +03:00
|
|
|
* Callers must hold gAtomTableLock before manipulating the table.
|
2016-04-06 05:01:24 +03:00
|
|
|
*/
|
|
|
|
static PLDHashTable* gAtomTable;
|
2016-05-26 04:50:37 +03:00
|
|
|
static Mutex* gAtomTableLock;
|
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
|
|
|
};
|
|
|
|
|
2016-04-06 05:01:24 +03:00
|
|
|
struct AtomTableEntry : public PLDHashEntryHdr
|
|
|
|
{
|
2017-09-21 05:36:53 +03:00
|
|
|
// These references are either to dynamic atoms, in which case they are
|
|
|
|
// non-owning, or they are to static atoms, which aren't really refcounted.
|
2016-04-06 05:01:24 +03:00
|
|
|
// See the comment at the top of this file for more details.
|
2017-09-21 05:36:53 +03:00
|
|
|
nsAtom* MOZ_NON_OWNING_REF mAtom;
|
2016-04-06 05:01:24 +03:00
|
|
|
};
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashNumber
|
2016-03-16 07:33:44 +03:00
|
|
|
AtomTableGetHash(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
|
2016-03-16 07:33:44 +03:00
|
|
|
AtomTableMatchKey(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
|
|
|
}
|
|
|
|
|
2017-04-02 22:40:06 +03:00
|
|
|
return he->mAtom->Equals(k->mUTF16String, k->mLength);
|
2001-10-21 03:19:07 +04:00
|
|
|
}
|
|
|
|
|
2017-09-25 09:30:32 +03:00
|
|
|
void
|
|
|
|
nsAtomFriend::AtomTableClearEntry(PLDHashTable* aTable, PLDHashEntryHdr* aEntry)
|
2001-10-21 03:19:07 +04:00
|
|
|
{
|
Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm.
This patch changes things so that dynamic atoms and static atoms have distinct
implementations. This is a step towards allowing dynamic atoms and static atoms
to have different layouts in memory, which will allow static atoms to be
represented more compactly.
Specifically, the patch does the following.
- It renames AtomImpl as DynamicAtom and PermanentAtomImpl as StaticAtom, and
the latter is no longer a subclass of the former. This required duplicating
some methods from the former into the latter: ScriptableToString(),
ToUTF8String(), ScriptableEquals(), IsStaticAtom(). (This duplication will
disappear in the future if the representations of dynamic atoms and static
atoms diverge. Indeed, SizeOfIncludingThis() is already different in the two
classes.)
- It replaces all mentions of "permanent"/"non-permanent" atoms with
"static"/"dynamic".
- In ~DynamicAtom() it removes the check that causes gAtomTable to be deleted
when it becomes empty. This will only happen at shutdown and so doesn't seem
useful.
- It documents better various things, especially the basics of the
dynamic/static split, the transmutation of dynamic atoms to static atoms, and
the details of the SizeOf functions.
--HG--
extra : rebase_source : dbf903012e70ebf1a43de1e1088db1bc1b8dd4f4
2016-04-01 03:18:06 +03:00
|
|
|
auto entry = static_cast<AtomTableEntry*>(aEntry);
|
2017-09-21 05:36:53 +03:00
|
|
|
nsAtom* atom = entry->mAtom;
|
Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm.
This patch changes things so that dynamic atoms and static atoms have distinct
implementations. This is a step towards allowing dynamic atoms and static atoms
to have different layouts in memory, which will allow static atoms to be
represented more compactly.
Specifically, the patch does the following.
- It renames AtomImpl as DynamicAtom and PermanentAtomImpl as StaticAtom, and
the latter is no longer a subclass of the former. This required duplicating
some methods from the former into the latter: ScriptableToString(),
ToUTF8String(), ScriptableEquals(), IsStaticAtom(). (This duplication will
disappear in the future if the representations of dynamic atoms and static
atoms diverge. Indeed, SizeOfIncludingThis() is already different in the two
classes.)
- It replaces all mentions of "permanent"/"non-permanent" atoms with
"static"/"dynamic".
- In ~DynamicAtom() it removes the check that causes gAtomTable to be deleted
when it becomes empty. This will only happen at shutdown and so doesn't seem
useful.
- It documents better various things, especially the basics of the
dynamic/static split, the transmutation of dynamic atoms to static atoms, and
the details of the SizeOf functions.
--HG--
extra : rebase_source : dbf903012e70ebf1a43de1e1088db1bc1b8dd4f4
2016-04-01 03:18:06 +03:00
|
|
|
if (atom->IsStaticAtom()) {
|
2017-09-21 05:36:53 +03:00
|
|
|
// This case -- when the entry being cleared holds a static atom -- only
|
|
|
|
// occurs when gAtomTable is destroyed, whereupon all static atoms within it
|
2017-08-24 04:10:04 +03:00
|
|
|
// must be explicitly deleted.
|
|
|
|
delete atom;
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
2001-10-21 03:19:07 +04:00
|
|
|
}
|
|
|
|
|
2015-02-11 20:46:40 +03:00
|
|
|
static void
|
|
|
|
AtomTableInitEntry(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
|
|
|
}
|
|
|
|
|
2003-04-04 19:10:37 +04:00
|
|
|
static const PLDHashTableOps AtomTableOps = {
|
2005-11-04 22:52:18 +03:00
|
|
|
AtomTableGetHash,
|
2001-10-21 03:19:07 +04:00
|
|
|
AtomTableMatchKey,
|
2015-09-15 00:23:47 +03:00
|
|
|
PLDHashTable::MoveEntryStub,
|
2017-09-25 09:30:32 +03:00
|
|
|
nsAtomFriend::AtomTableClearEntry,
|
2007-07-12 00:46:43 +04:00
|
|
|
AtomTableInitEntry
|
2001-10-21 03:19:07 +04:00
|
|
|
};
|
|
|
|
|
2016-04-06 05:01:24 +03:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2017-04-03 23:13:18 +03:00
|
|
|
#define RECENTLY_USED_MAIN_THREAD_ATOM_CACHE_SIZE 31
|
2017-09-21 05:36:53 +03:00
|
|
|
static nsAtom*
|
2017-04-03 23:13:18 +03:00
|
|
|
sRecentlyUsedMainThreadAtoms[RECENTLY_USED_MAIN_THREAD_ATOM_CACHE_SIZE] = {};
|
|
|
|
|
2016-05-27 03:55:53 +03:00
|
|
|
void
|
2017-09-25 09:30:32 +03:00
|
|
|
nsAtomFriend::GCAtomTableLocked(const MutexAutoLock& aProofOfLock, GCKind aKind)
|
2017-01-26 23:43:38 +03:00
|
|
|
{
|
2017-04-03 23:13:18 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
for (uint32_t i = 0; i < RECENTLY_USED_MAIN_THREAD_ATOM_CACHE_SIZE; ++i) {
|
|
|
|
sRecentlyUsedMainThreadAtoms[i] = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-09-06 08:06:16 +03:00
|
|
|
int32_t removedCount = 0; // Use a non-atomic temporary for cheaper increments.
|
2017-02-01 19:47:00 +03:00
|
|
|
nsAutoCString nonZeroRefcountAtoms;
|
|
|
|
uint32_t nonZeroRefcountAtomsCount = 0;
|
2016-05-27 03:55:53 +03:00
|
|
|
for (auto i = gAtomTable->Iter(); !i.Done(); i.Next()) {
|
|
|
|
auto entry = static_cast<AtomTableEntry*>(i.Get());
|
2017-01-26 23:43:38 +03:00
|
|
|
if (entry->mAtom->IsStaticAtom()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-21 05:36:53 +03:00
|
|
|
nsAtom* atom = entry->mAtom;
|
2017-01-26 23:43:38 +03:00
|
|
|
if (atom->mRefCnt == 0) {
|
|
|
|
i.Remove();
|
|
|
|
delete atom;
|
|
|
|
++removedCount;
|
2017-03-06 22:50:30 +03:00
|
|
|
}
|
|
|
|
#ifdef NS_FREE_PERMANENT_DATA
|
|
|
|
else if (aKind == GCKind::Shutdown && PR_GetEnv("XPCOM_MEM_BLOAT_LOG")) {
|
|
|
|
// Only report leaking atoms in leak-checking builds in a run
|
|
|
|
// where we are checking for leaks, during shutdown. If
|
|
|
|
// something is anomalous, then we'll assert later in this
|
|
|
|
// function.
|
2017-01-26 23:43:38 +03:00
|
|
|
nsAutoCString name;
|
|
|
|
atom->ToUTF8String(name);
|
2017-02-01 19:47:00 +03:00
|
|
|
if (nonZeroRefcountAtomsCount == 0) {
|
|
|
|
nonZeroRefcountAtoms = name;
|
|
|
|
} else if (nonZeroRefcountAtomsCount < 20) {
|
|
|
|
nonZeroRefcountAtoms += NS_LITERAL_CSTRING(",") + name;
|
|
|
|
} else if (nonZeroRefcountAtomsCount == 20) {
|
|
|
|
nonZeroRefcountAtoms += NS_LITERAL_CSTRING(",...");
|
|
|
|
}
|
|
|
|
nonZeroRefcountAtomsCount++;
|
2016-05-27 03:55:53 +03:00
|
|
|
}
|
2017-03-06 22:50:30 +03:00
|
|
|
#endif
|
|
|
|
|
2016-05-27 03:55:53 +03:00
|
|
|
}
|
2017-02-01 19:47:00 +03:00
|
|
|
if (nonZeroRefcountAtomsCount) {
|
|
|
|
nsPrintfCString msg("%d dynamic atom(s) with non-zero refcount: %s",
|
|
|
|
nonZeroRefcountAtomsCount, nonZeroRefcountAtoms.get());
|
|
|
|
NS_ASSERTION(nonZeroRefcountAtomsCount == 0, msg.get());
|
|
|
|
}
|
2016-05-27 03:55:53 +03:00
|
|
|
|
2017-07-29 06:57:32 +03:00
|
|
|
// We would like to assert that gUnusedAtomCount matches the number of atoms
|
|
|
|
// we found in the table which we removed. During the course of this function,
|
|
|
|
// the atom table is locked, but this lock is not acquired for AddRef() and
|
|
|
|
// Release() calls. This means we might see a gUnusedAtomCount value in
|
|
|
|
// between, say, AddRef() incrementing mRefCnt and it decrementing
|
|
|
|
// gUnusedAtomCount. So, we don't bother asserting that there are no unused
|
|
|
|
// atoms at the end of a regular GC. But we can (and do) assert thist just
|
|
|
|
// after the last GC at shutdown.
|
|
|
|
//
|
|
|
|
// Note that, barring refcounting bugs, an atom can only go from a zero
|
|
|
|
// refcount to a non-zero refcount while the atom table lock is held, so
|
|
|
|
// so we won't try to resurrect a zero refcount atom while trying to delete
|
|
|
|
// it.
|
|
|
|
|
|
|
|
MOZ_ASSERT_IF(aKind == GCKind::Shutdown, removedCount == gUnusedAtomCount);
|
2016-05-27 03:55:53 +03:00
|
|
|
|
2017-01-26 23:43:38 +03:00
|
|
|
gUnusedAtomCount -= removedCount;
|
2016-05-27 03:55:53 +03:00
|
|
|
}
|
2016-04-06 05:01:24 +03:00
|
|
|
|
2017-09-25 09:30:32 +03:00
|
|
|
static void
|
|
|
|
GCAtomTable()
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
MutexAutoLock lock(*gAtomTableLock);
|
|
|
|
nsAtomFriend::GCAtomTableLocked(lock, GCKind::RegularOperation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 15:20:17 +03:00
|
|
|
MozExternalRefCountType
|
2017-09-26 01:33:21 +03:00
|
|
|
nsAtom::AddRef()
|
2016-05-27 03:55:53 +03:00
|
|
|
{
|
2017-09-26 01:33:21 +03:00
|
|
|
MOZ_ASSERT(!IsHTML5Atom(), "Attempt to AddRef an HTML5 atom");
|
|
|
|
if (!IsDynamicAtom()) {
|
|
|
|
MOZ_ASSERT(IsStaticAtom());
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt");
|
2016-05-27 03:55:53 +03:00
|
|
|
nsrefcnt count = ++mRefCnt;
|
|
|
|
if (count == 1) {
|
|
|
|
gUnusedAtomCount--;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-08-09 15:20:17 +03:00
|
|
|
MozExternalRefCountType
|
2017-09-26 01:33:21 +03:00
|
|
|
nsAtom::Release()
|
2016-05-27 03:55:53 +03:00
|
|
|
{
|
2017-09-26 01:33:21 +03:00
|
|
|
MOZ_ASSERT(!IsHTML5Atom(), "Attempt to Release an HTML5 atom");
|
|
|
|
if (!IsDynamicAtom()) {
|
|
|
|
MOZ_ASSERT(IsStaticAtom());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// We set a lower GC threshold for atoms in debug builds so that we exercise
|
|
|
|
// the GC machinery more often.
|
|
|
|
static const int32_t kAtomGCThreshold = 20;
|
|
|
|
#else
|
|
|
|
static const int32_t kAtomGCThreshold = 10000;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release");
|
2016-05-27 03:55:53 +03:00
|
|
|
nsrefcnt count = --mRefCnt;
|
|
|
|
if (count == 0) {
|
|
|
|
if (++gUnusedAtomCount >= kAtomGCThreshold) {
|
|
|
|
GCAtomTable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2016-04-06 05:01:24 +03:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class StaticAtomEntry : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef const nsAString& KeyType;
|
|
|
|
typedef const nsAString* KeyTypePointer;
|
|
|
|
|
|
|
|
explicit StaticAtomEntry(KeyTypePointer aKey) {}
|
|
|
|
StaticAtomEntry(const StaticAtomEntry& aOther) : mAtom(aOther.mAtom) {}
|
|
|
|
|
|
|
|
// We do not delete the atom because that's done when gAtomTable is
|
|
|
|
// destroyed -- which happens immediately after gStaticAtomTable is destroyed
|
|
|
|
// -- in NS_PurgeAtomTable().
|
|
|
|
~StaticAtomEntry() {}
|
|
|
|
|
|
|
|
bool KeyEquals(KeyTypePointer aKey) const
|
|
|
|
{
|
|
|
|
return mAtom->Equals(*aKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{
|
|
|
|
return HashString(*aKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
2017-09-21 05:36:53 +03:00
|
|
|
// Static atoms aren't really refcounted. Because these entries live in a
|
2016-04-06 05:01:24 +03:00
|
|
|
// global hashtable, this reference is essentially owning.
|
2017-09-21 05:36:53 +03:00
|
|
|
nsAtom* MOZ_OWNING_REF mAtom;
|
2016-04-06 05:01:24 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A hashtable of static atoms that existed at app startup. This hashtable
|
|
|
|
* helps nsHtml5AtomTable.
|
|
|
|
*/
|
|
|
|
typedef nsTHashtable<StaticAtomEntry> StaticAtomTable;
|
|
|
|
static StaticAtomTable* gStaticAtomTable = nullptr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether it is still OK to add atoms to gStaticAtomTable.
|
|
|
|
*/
|
|
|
|
static bool gStaticAtomTableSealed = false;
|
|
|
|
|
2016-05-26 04:45:17 +03:00
|
|
|
// The atom table very quickly gets 10,000+ entries in it (or even 100,000+).
|
|
|
|
// But choosing the best initial length has some subtleties: we add ~2700
|
|
|
|
// static atoms to the table at start-up, and then we start adding and removing
|
|
|
|
// dynamic atoms. If we make the table too big to start with, when the first
|
|
|
|
// dynamic atom gets removed the load factor will be < 25% and so we will
|
|
|
|
// shrink it to 4096 entries.
|
|
|
|
//
|
|
|
|
// By choosing an initial length of 4096, we get an initial capacity of 8192.
|
|
|
|
// That's the biggest initial capacity that will let us be > 25% full when the
|
|
|
|
// first dynamic atom is removed (when the count is ~2700), thus avoiding any
|
|
|
|
// shrinking.
|
|
|
|
#define ATOM_HASHTABLE_INITIAL_LENGTH 4096
|
2016-04-06 05:01:24 +03:00
|
|
|
|
2005-11-04 22:52:18 +03:00
|
|
|
void
|
2016-05-26 04:45:17 +03:00
|
|
|
NS_InitAtomTable()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!gAtomTable);
|
|
|
|
gAtomTable = new PLDHashTable(&AtomTableOps, sizeof(AtomTableEntry),
|
|
|
|
ATOM_HASHTABLE_INITIAL_LENGTH);
|
2016-05-26 04:50:37 +03:00
|
|
|
gAtomTableLock = new Mutex("Atom Table Lock");
|
2017-02-25 01:02:44 +03:00
|
|
|
|
|
|
|
// Bug 1340710 has caused us to generate an empty atom at arbitrary times
|
|
|
|
// after startup. If we end up creating one before nsGkAtoms::_empty is
|
|
|
|
// registered, we get an assertion about transmuting a dynamic atom into a
|
|
|
|
// static atom. In order to avoid that, we register an empty string static
|
|
|
|
// atom as soon as we initialize the atom table to guarantee that the empty
|
|
|
|
// string atom will always be static.
|
|
|
|
NS_STATIC_ATOM_BUFFER(empty, "");
|
2017-09-26 01:33:21 +03:00
|
|
|
static nsAtom* empty_atom = nullptr;
|
2017-02-25 01:02:44 +03:00
|
|
|
static const nsStaticAtom default_atoms[] = {
|
|
|
|
NS_STATIC_ATOM(empty, &empty_atom)
|
|
|
|
};
|
|
|
|
NS_RegisterStaticAtoms(default_atoms);
|
2016-05-26 04:45:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NS_ShutdownAtomTable()
|
1999-10-01 01:40:00 +04:00
|
|
|
{
|
Bug 1350729 - Implement fake refcount logging for nsFakeStringBuffer. r=dbaron
Running with XPCOM_MEM_LOG_CLASSES=nsStringBuffer triggers an
assertion in refcount logging for nsFakeStringBuffers. These are given
an initial refcount of 1, without calling NS_LOG_ADDREF. Then,
AddRef() is called on these objects in StaticAtom::StaticAtom(), and
we tell the refcount logging system about the fake buffer, and that it
has a refcount of 0, triggering the assertion.
The first part of the fix is to call NS_LOG_ADDREF for this initial
refcount, in StaticAtom().
This first fix causes refcount logging to start reporting that the
fake string buffers leak, when XPCOM_MEM_LOG_CLASSES is not set. This
is because refcount logging is now getting told about these objects
being AddRefed at 1, which it takes to mean that an object is created.
To work around this issue, I add an array gFakeBuffers that contains
every fake string buffer we create, and tell the refcount logging
system that these objects are all being destroyed, when the atom table
is being shut down. This could result in some bogosity if the fake
buffers are "leaked" but hopefully this is still an improvement over
the current state.
MozReview-Commit-ID: 5AxoBYAlYRU
--HG--
extra : rebase_source : ba0763cb494894918141774025db525cea9f9c75
2017-07-14 20:20:23 +03:00
|
|
|
#if defined(NS_BUILD_REFCNT_LOGGING)
|
|
|
|
gFakeBuffers = nullptr;
|
|
|
|
#endif
|
|
|
|
|
2009-09-18 19:13:10 +04:00
|
|
|
delete gStaticAtomTable;
|
2015-05-18 10:52:08 +03:00
|
|
|
gStaticAtomTable = nullptr;
|
2009-09-18 19:13:10 +04:00
|
|
|
|
2016-05-27 03:55:53 +03:00
|
|
|
#ifdef NS_FREE_PERMANENT_DATA
|
|
|
|
// Do a final GC to satisfy leak checking. We skip this step in release
|
|
|
|
// builds.
|
2017-01-26 23:43:38 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(*gAtomTableLock);
|
2017-09-25 09:30:32 +03:00
|
|
|
nsAtomFriend::GCAtomTableLocked(lock, GCKind::Shutdown);
|
2017-01-26 23:43:38 +03:00
|
|
|
}
|
2016-05-27 03:55:53 +03:00
|
|
|
#endif
|
|
|
|
|
2016-05-26 04:45:17 +03:00
|
|
|
delete gAtomTable;
|
|
|
|
gAtomTable = nullptr;
|
2016-05-26 04:50:37 +03:00
|
|
|
delete gAtomTableLock;
|
|
|
|
gAtomTableLock = nullptr;
|
2010-03-08 18:44:59 +03:00
|
|
|
}
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2014-07-31 10:06:57 +04:00
|
|
|
void
|
|
|
|
NS_SizeOfAtomTablesIncludingThis(MallocSizeOf aMallocSizeOf,
|
|
|
|
size_t* aMain, size_t* aStatic)
|
|
|
|
{
|
2016-05-26 04:50:37 +03:00
|
|
|
MutexAutoLock lock(*gAtomTableLock);
|
2016-05-26 04:45:17 +03:00
|
|
|
*aMain = gAtomTable->ShallowSizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
for (auto iter = gAtomTable->Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto entry = static_cast<AtomTableEntry*>(iter.Get());
|
|
|
|
*aMain += entry->mAtom->SizeOfIncludingThis(aMallocSizeOf);
|
2015-07-30 08:28:20 +03:00
|
|
|
}
|
2014-07-31 10:06:57 +04:00
|
|
|
|
Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm.
This patch changes things so that dynamic atoms and static atoms have distinct
implementations. This is a step towards allowing dynamic atoms and static atoms
to have different layouts in memory, which will allow static atoms to be
represented more compactly.
Specifically, the patch does the following.
- It renames AtomImpl as DynamicAtom and PermanentAtomImpl as StaticAtom, and
the latter is no longer a subclass of the former. This required duplicating
some methods from the former into the latter: ScriptableToString(),
ToUTF8String(), ScriptableEquals(), IsStaticAtom(). (This duplication will
disappear in the future if the representations of dynamic atoms and static
atoms diverge. Indeed, SizeOfIncludingThis() is already different in the two
classes.)
- It replaces all mentions of "permanent"/"non-permanent" atoms with
"static"/"dynamic".
- In ~DynamicAtom() it removes the check that causes gAtomTable to be deleted
when it becomes empty. This will only happen at shutdown and so doesn't seem
useful.
- It documents better various things, especially the basics of the
dynamic/static split, the transmutation of dynamic atoms to static atoms, and
the details of the SizeOf functions.
--HG--
extra : rebase_source : dbf903012e70ebf1a43de1e1088db1bc1b8dd4f4
2016-04-01 03:18:06 +03:00
|
|
|
// The atoms pointed to by gStaticAtomTable are also pointed to by gAtomTable,
|
|
|
|
// and they're measured by the loop above. So no need to measure them here.
|
2014-07-31 10:06:57 +04:00
|
|
|
*aStatic = gStaticAtomTable
|
2015-07-29 11:50:52 +03:00
|
|
|
? gStaticAtomTable->ShallowSizeOfIncludingThis(aMallocSizeOf)
|
2014-07-31 10:06:57 +04:00
|
|
|
: 0;
|
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
|
|
|
{
|
2016-05-26 04:50:37 +03:00
|
|
|
gAtomTableLock->AssertCurrentThreadOwns();
|
2014-07-31 10:22:40 +04:00
|
|
|
AtomTableKey key(aString, aLength, aHashOut);
|
2015-02-03 01:48:58 +03:00
|
|
|
// This is an infallible add.
|
2015-09-15 00:23:12 +03:00
|
|
|
return static_cast<AtomTableEntry*>(gAtomTable->Add(&key));
|
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
|
|
|
{
|
2016-05-26 04:50:37 +03:00
|
|
|
gAtomTableLock->AssertCurrentThreadOwns();
|
2014-07-31 10:22:40 +04:00
|
|
|
AtomTableKey key(aString, aLength, aHashOut);
|
2015-02-03 01:48:58 +03:00
|
|
|
// This is an infallible add.
|
2015-09-15 00:23:12 +03:00
|
|
|
return static_cast<AtomTableEntry*>(gAtomTable->Add(&key));
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2016-03-18 11:03:43 +03:00
|
|
|
void
|
2017-09-25 09:30:32 +03:00
|
|
|
nsAtomFriend::RegisterStaticAtoms(const nsStaticAtom* aAtoms,
|
|
|
|
uint32_t aAtomCount)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
2016-05-26 04:50:37 +03:00
|
|
|
MutexAutoLock lock(*gAtomTableLock);
|
2017-01-26 23:43:38 +03:00
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(!gStaticAtomTableSealed,
|
|
|
|
"Atom table has already been sealed!");
|
|
|
|
|
|
|
|
if (!gStaticAtomTable) {
|
2014-08-01 09:57:13 +04:00
|
|
|
gStaticAtomTable = new StaticAtomTable();
|
2009-09-18 19:13:10 +04:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aAtomCount; ++i) {
|
Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm.
This patch changes things so that dynamic atoms and static atoms have distinct
implementations. This is a step towards allowing dynamic atoms and static atoms
to have different layouts in memory, which will allow static atoms to be
represented more compactly.
Specifically, the patch does the following.
- It renames AtomImpl as DynamicAtom and PermanentAtomImpl as StaticAtom, and
the latter is no longer a subclass of the former. This required duplicating
some methods from the former into the latter: ScriptableToString(),
ToUTF8String(), ScriptableEquals(), IsStaticAtom(). (This duplication will
disappear in the future if the representations of dynamic atoms and static
atoms diverge. Indeed, SizeOfIncludingThis() is already different in the two
classes.)
- It replaces all mentions of "permanent"/"non-permanent" atoms with
"static"/"dynamic".
- In ~DynamicAtom() it removes the check that causes gAtomTable to be deleted
when it becomes empty. This will only happen at shutdown and so doesn't seem
useful.
- It documents better various things, especially the basics of the
dynamic/static split, the transmutation of dynamic atoms to static atoms, and
the details of the SizeOf functions.
--HG--
extra : rebase_source : dbf903012e70ebf1a43de1e1088db1bc1b8dd4f4
2016-04-01 03:18:06 +03:00
|
|
|
nsStringBuffer* stringBuffer = aAtoms[i].mStringBuffer;
|
2017-09-26 01:33:21 +03:00
|
|
|
nsAtom** atomp = aAtoms[i].mAtom;
|
Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm.
This patch changes things so that dynamic atoms and static atoms have distinct
implementations. This is a step towards allowing dynamic atoms and static atoms
to have different layouts in memory, which will allow static atoms to be
represented more compactly.
Specifically, the patch does the following.
- It renames AtomImpl as DynamicAtom and PermanentAtomImpl as StaticAtom, and
the latter is no longer a subclass of the former. This required duplicating
some methods from the former into the latter: ScriptableToString(),
ToUTF8String(), ScriptableEquals(), IsStaticAtom(). (This duplication will
disappear in the future if the representations of dynamic atoms and static
atoms diverge. Indeed, SizeOfIncludingThis() is already different in the two
classes.)
- It replaces all mentions of "permanent"/"non-permanent" atoms with
"static"/"dynamic".
- In ~DynamicAtom() it removes the check that causes gAtomTable to be deleted
when it becomes empty. This will only happen at shutdown and so doesn't seem
useful.
- It documents better various things, especially the basics of the
dynamic/static split, the transmutation of dynamic atoms to static atoms, and
the details of the SizeOf functions.
--HG--
extra : rebase_source : dbf903012e70ebf1a43de1e1088db1bc1b8dd4f4
2016-04-01 03:18:06 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(nsCRT::IsAscii(static_cast<char16_t*>(stringBuffer->Data())));
|
2007-07-12 00:46:43 +04:00
|
|
|
|
Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm.
This patch changes things so that dynamic atoms and static atoms have distinct
implementations. This is a step towards allowing dynamic atoms and static atoms
to have different layouts in memory, which will allow static atoms to be
represented more compactly.
Specifically, the patch does the following.
- It renames AtomImpl as DynamicAtom and PermanentAtomImpl as StaticAtom, and
the latter is no longer a subclass of the former. This required duplicating
some methods from the former into the latter: ScriptableToString(),
ToUTF8String(), ScriptableEquals(), IsStaticAtom(). (This duplication will
disappear in the future if the representations of dynamic atoms and static
atoms diverge. Indeed, SizeOfIncludingThis() is already different in the two
classes.)
- It replaces all mentions of "permanent"/"non-permanent" atoms with
"static"/"dynamic".
- In ~DynamicAtom() it removes the check that causes gAtomTable to be deleted
when it becomes empty. This will only happen at shutdown and so doesn't seem
useful.
- It documents better various things, especially the basics of the
dynamic/static split, the transmutation of dynamic atoms to static atoms, and
the details of the SizeOf functions.
--HG--
extra : rebase_source : dbf903012e70ebf1a43de1e1088db1bc1b8dd4f4
2016-04-01 03:18:06 +03:00
|
|
|
uint32_t stringLen = stringBuffer->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 =
|
Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm.
This patch changes things so that dynamic atoms and static atoms have distinct
implementations. This is a step towards allowing dynamic atoms and static atoms
to have different layouts in memory, which will allow static atoms to be
represented more compactly.
Specifically, the patch does the following.
- It renames AtomImpl as DynamicAtom and PermanentAtomImpl as StaticAtom, and
the latter is no longer a subclass of the former. This required duplicating
some methods from the former into the latter: ScriptableToString(),
ToUTF8String(), ScriptableEquals(), IsStaticAtom(). (This duplication will
disappear in the future if the representations of dynamic atoms and static
atoms diverge. Indeed, SizeOfIncludingThis() is already different in the two
classes.)
- It replaces all mentions of "permanent"/"non-permanent" atoms with
"static"/"dynamic".
- In ~DynamicAtom() it removes the check that causes gAtomTable to be deleted
when it becomes empty. This will only happen at shutdown and so doesn't seem
useful.
- It documents better various things, especially the basics of the
dynamic/static split, the transmutation of dynamic atoms to static atoms, and
the details of the SizeOf functions.
--HG--
extra : rebase_source : dbf903012e70ebf1a43de1e1088db1bc1b8dd4f4
2016-04-01 03:18:06 +03:00
|
|
|
GetAtomHashEntry(static_cast<char16_t*>(stringBuffer->Data()),
|
2014-07-31 10:22:40 +04:00
|
|
|
stringLen, &hash);
|
2007-07-12 00:46:43 +04:00
|
|
|
|
2017-09-21 05:36:53 +03:00
|
|
|
nsAtom* atom = he->mAtom;
|
2014-07-31 10:22:42 +04:00
|
|
|
if (atom) {
|
2017-01-26 23:43:38 +03:00
|
|
|
// Disallow creating a dynamic atom, and then later, while the
|
|
|
|
// dynamic atom is still alive, registering that same atom as a
|
|
|
|
// static atom. It causes subtle bugs, and we're programming in
|
|
|
|
// C++ here, not Smalltalk.
|
Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm.
This patch changes things so that dynamic atoms and static atoms have distinct
implementations. This is a step towards allowing dynamic atoms and static atoms
to have different layouts in memory, which will allow static atoms to be
represented more compactly.
Specifically, the patch does the following.
- It renames AtomImpl as DynamicAtom and PermanentAtomImpl as StaticAtom, and
the latter is no longer a subclass of the former. This required duplicating
some methods from the former into the latter: ScriptableToString(),
ToUTF8String(), ScriptableEquals(), IsStaticAtom(). (This duplication will
disappear in the future if the representations of dynamic atoms and static
atoms diverge. Indeed, SizeOfIncludingThis() is already different in the two
classes.)
- It replaces all mentions of "permanent"/"non-permanent" atoms with
"static"/"dynamic".
- In ~DynamicAtom() it removes the check that causes gAtomTable to be deleted
when it becomes empty. This will only happen at shutdown and so doesn't seem
useful.
- It documents better various things, especially the basics of the
dynamic/static split, the transmutation of dynamic atoms to static atoms, and
the details of the SizeOf functions.
--HG--
extra : rebase_source : dbf903012e70ebf1a43de1e1088db1bc1b8dd4f4
2016-04-01 03:18:06 +03:00
|
|
|
if (!atom->IsStaticAtom()) {
|
2017-01-26 23:43:38 +03:00
|
|
|
nsAutoCString name;
|
|
|
|
atom->ToUTF8String(name);
|
2017-02-21 20:02:03 +03:00
|
|
|
MOZ_CRASH_UNSAFE_PRINTF(
|
|
|
|
"Static atom registration for %s should be pushed back", name.get());
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
} else {
|
2017-09-25 09:30:32 +03:00
|
|
|
atom = new nsAtom(stringBuffer, stringLen, hash);
|
2010-03-08 18:45:00 +03:00
|
|
|
he->mAtom = atom;
|
2014-07-31 10:22:42 +04:00
|
|
|
}
|
Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm.
This patch changes things so that dynamic atoms and static atoms have distinct
implementations. This is a step towards allowing dynamic atoms and static atoms
to have different layouts in memory, which will allow static atoms to be
represented more compactly.
Specifically, the patch does the following.
- It renames AtomImpl as DynamicAtom and PermanentAtomImpl as StaticAtom, and
the latter is no longer a subclass of the former. This required duplicating
some methods from the former into the latter: ScriptableToString(),
ToUTF8String(), ScriptableEquals(), IsStaticAtom(). (This duplication will
disappear in the future if the representations of dynamic atoms and static
atoms diverge. Indeed, SizeOfIncludingThis() is already different in the two
classes.)
- It replaces all mentions of "permanent"/"non-permanent" atoms with
"static"/"dynamic".
- In ~DynamicAtom() it removes the check that causes gAtomTable to be deleted
when it becomes empty. This will only happen at shutdown and so doesn't seem
useful.
- It documents better various things, especially the basics of the
dynamic/static split, the transmutation of dynamic atoms to static atoms, and
the details of the SizeOf functions.
--HG--
extra : rebase_source : dbf903012e70ebf1a43de1e1088db1bc1b8dd4f4
2016-04-01 03:18:06 +03:00
|
|
|
*atomp = atom;
|
2010-03-08 18:44:59 +03:00
|
|
|
|
2014-07-31 10:22:42 +04:00
|
|
|
if (!gStaticAtomTableSealed) {
|
2014-08-01 09:57:13 +04:00
|
|
|
StaticAtomEntry* entry =
|
|
|
|
gStaticAtomTable->PutEntry(nsDependentAtomString(atom));
|
Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm.
This patch changes things so that dynamic atoms and static atoms have distinct
implementations. This is a step towards allowing dynamic atoms and static atoms
to have different layouts in memory, which will allow static atoms to be
represented more compactly.
Specifically, the patch does the following.
- It renames AtomImpl as DynamicAtom and PermanentAtomImpl as StaticAtom, and
the latter is no longer a subclass of the former. This required duplicating
some methods from the former into the latter: ScriptableToString(),
ToUTF8String(), ScriptableEquals(), IsStaticAtom(). (This duplication will
disappear in the future if the representations of dynamic atoms and static
atoms diverge. Indeed, SizeOfIncludingThis() is already different in the two
classes.)
- It replaces all mentions of "permanent"/"non-permanent" atoms with
"static"/"dynamic".
- In ~DynamicAtom() it removes the check that causes gAtomTable to be deleted
when it becomes empty. This will only happen at shutdown and so doesn't seem
useful.
- It documents better various things, especially the basics of the
dynamic/static split, the transmutation of dynamic atoms to static atoms, and
the details of the SizeOf functions.
--HG--
extra : rebase_source : dbf903012e70ebf1a43de1e1088db1bc1b8dd4f4
2016-04-01 03:18:06 +03:00
|
|
|
MOZ_ASSERT(atom->IsStaticAtom());
|
2017-08-24 04:10:04 +03:00
|
|
|
entry->mAtom = atom;
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 09:30:32 +03:00
|
|
|
void
|
|
|
|
RegisterStaticAtoms(const nsStaticAtom* aAtoms, uint32_t aAtomCount)
|
|
|
|
{
|
|
|
|
nsAtomFriend::RegisterStaticAtoms(aAtoms, aAtomCount);
|
|
|
|
}
|
|
|
|
|
2017-09-26 01:33:21 +03:00
|
|
|
already_AddRefed<nsAtom>
|
2016-03-29 02:09:43 +03:00
|
|
|
NS_Atomize(const char* aUTF8String)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2017-09-25 09:30:32 +03:00
|
|
|
return nsAtomFriend::Atomize(nsDependentCString(aUTF8String));
|
2007-07-12 00:46:43 +04:00
|
|
|
}
|
|
|
|
|
2017-09-26 01:33:21 +03:00
|
|
|
already_AddRefed<nsAtom>
|
2017-09-25 09:30:32 +03:00
|
|
|
nsAtomFriend::Atomize(const nsACString& aUTF8String)
|
2007-07-12 00:46:43 +04:00
|
|
|
{
|
2016-05-26 04:50:37 +03:00
|
|
|
MutexAutoLock lock(*gAtomTableLock);
|
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) {
|
2017-09-26 01:33:21 +03:00
|
|
|
RefPtr<nsAtom> 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);
|
2017-09-21 07:02:05 +03:00
|
|
|
RefPtr<nsAtom> atom =
|
|
|
|
dont_AddRef(new nsAtom(nsAtom::AtomKind::DynamicAtom, 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
|
|
|
}
|
|
|
|
|
2017-09-26 01:33:21 +03:00
|
|
|
already_AddRefed<nsAtom>
|
2017-09-25 09:30:32 +03:00
|
|
|
NS_Atomize(const nsACString& aUTF8String)
|
|
|
|
{
|
|
|
|
return nsAtomFriend::Atomize(aUTF8String);
|
|
|
|
}
|
|
|
|
|
2017-09-26 01:33:21 +03:00
|
|
|
already_AddRefed<nsAtom>
|
2016-03-29 02:09:43 +03:00
|
|
|
NS_Atomize(const char16_t* aUTF16String)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
2017-09-25 09:30:32 +03:00
|
|
|
return nsAtomFriend::Atomize(nsDependentString(aUTF16String));
|
2005-11-04 22:52:18 +03:00
|
|
|
}
|
|
|
|
|
2017-09-26 01:33:21 +03:00
|
|
|
already_AddRefed<nsAtom>
|
2017-09-25 09:30:32 +03:00
|
|
|
nsAtomFriend::Atomize(const nsAString& aUTF16String)
|
2005-11-04 22:52:18 +03:00
|
|
|
{
|
2016-05-26 04:50:37 +03:00
|
|
|
MutexAutoLock lock(*gAtomTableLock);
|
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) {
|
2017-09-26 01:33:21 +03:00
|
|
|
RefPtr<nsAtom> 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
|
|
|
|
2017-09-21 07:02:05 +03:00
|
|
|
RefPtr<nsAtom> atom =
|
|
|
|
dont_AddRef(new nsAtom(nsAtom::AtomKind::DynamicAtom, 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
|
|
|
|
2017-09-26 01:33:21 +03:00
|
|
|
already_AddRefed<nsAtom>
|
2017-09-25 09:30:32 +03:00
|
|
|
NS_Atomize(const nsAString& aUTF16String)
|
|
|
|
{
|
|
|
|
return nsAtomFriend::Atomize(aUTF16String);
|
|
|
|
}
|
|
|
|
|
2017-09-26 01:33:21 +03:00
|
|
|
already_AddRefed<nsAtom>
|
2017-09-25 09:30:32 +03:00
|
|
|
nsAtomFriend::AtomizeMainThread(const nsAString& aUTF16String)
|
2017-04-03 23:13:18 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-09-26 01:33:21 +03:00
|
|
|
RefPtr<nsAtom> retVal;
|
2017-04-03 23:13:18 +03:00
|
|
|
uint32_t hash;
|
|
|
|
AtomTableKey key(aUTF16String.Data(), aUTF16String.Length(), &hash);
|
|
|
|
uint32_t index = hash % RECENTLY_USED_MAIN_THREAD_ATOM_CACHE_SIZE;
|
2017-09-21 05:36:53 +03:00
|
|
|
nsAtom* atom = sRecentlyUsedMainThreadAtoms[index];
|
2017-04-03 23:13:18 +03:00
|
|
|
if (atom) {
|
|
|
|
uint32_t length = atom->GetLength();
|
|
|
|
if (length == key.mLength &&
|
|
|
|
(memcmp(atom->GetUTF16String(),
|
|
|
|
key.mUTF16String, length * sizeof(char16_t)) == 0)) {
|
|
|
|
retVal = atom;
|
|
|
|
return retVal.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MutexAutoLock lock(*gAtomTableLock);
|
|
|
|
AtomTableEntry* he = static_cast<AtomTableEntry*>(gAtomTable->Add(&key));
|
|
|
|
|
|
|
|
if (he->mAtom) {
|
|
|
|
retVal = he->mAtom;
|
|
|
|
} else {
|
2017-09-21 07:02:05 +03:00
|
|
|
RefPtr<nsAtom> newAtom = dont_AddRef(
|
|
|
|
new nsAtom(nsAtom::AtomKind::DynamicAtom, aUTF16String, hash));
|
2017-08-24 04:10:04 +03:00
|
|
|
he->mAtom = newAtom;
|
|
|
|
retVal = newAtom.forget();
|
2017-04-03 23:13:18 +03:00
|
|
|
}
|
|
|
|
|
2017-08-24 04:10:04 +03:00
|
|
|
sRecentlyUsedMainThreadAtoms[index] = he->mAtom;
|
2017-04-03 23:13:18 +03:00
|
|
|
return retVal.forget();
|
|
|
|
}
|
|
|
|
|
2017-09-26 01:33:21 +03:00
|
|
|
already_AddRefed<nsAtom>
|
2017-09-25 09:30:32 +03:00
|
|
|
NS_AtomizeMainThread(const nsAString& aUTF16String)
|
|
|
|
{
|
|
|
|
return nsAtomFriend::AtomizeMainThread(aUTF16String);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-09-25 09:30:32 +03:00
|
|
|
GCAtomTable(); // Trigger a GC so we return a deterministic result.
|
2016-05-26 04:50:37 +03:00
|
|
|
MutexAutoLock lock(*gAtomTableLock);
|
2015-05-18 10:52:08 +03:00
|
|
|
return gAtomTable->EntryCount();
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2003-03-25 21:55:59 +03:00
|
|
|
|
2017-09-06 08:06:16 +03:00
|
|
|
int32_t
|
2017-09-07 03:08:31 +03:00
|
|
|
NS_GetUnusedAtomCount(void)
|
|
|
|
{
|
|
|
|
return gUnusedAtomCount;
|
|
|
|
}
|
|
|
|
|
2017-09-26 01:33:21 +03:00
|
|
|
nsAtom*
|
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.");
|
2014-08-01 09:57:13 +04:00
|
|
|
StaticAtomEntry* entry = gStaticAtomTable->GetEntry(aUTF16String);
|
|
|
|
return entry ? entry->mAtom : nullptr;
|
2009-09-18 19:13:10 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|