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.
|
|
|
|
//
|
|
|
|
// - DynamicAtom: the atom itself is heap allocated, as is the nsStringBuffer it
|
2016-05-27 03:55:53 +03:00
|
|
|
// points to. |gAtomTable| holds weak references to them DynamicAtoms. When
|
|
|
|
// the refcount of a DynamicAtom drops to zero, we increment a static counter.
|
|
|
|
// When that counter reaches a certain threshold, we iterate over the atom
|
|
|
|
// table, removing and deleting DynamicAtoms with refcount zero. This allows
|
|
|
|
// 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
|
|
|
//
|
|
|
|
// - StaticAtom: the atom itself is heap allocated, but it points to a static
|
|
|
|
// nsStringBuffer. |gAtomTable| effectively owns StaticAtoms, because such
|
|
|
|
// atoms ignore all AddRef/Release calls, which ensures they stay alive until
|
|
|
|
// |gAtomTable| itself is destroyed whereupon they are explicitly deleted.
|
2016-05-26 04:50:37 +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
|
|
|
|
2016-05-27 03:55:53 +03:00
|
|
|
static Atomic<uint32_t, ReleaseAcquire> gUnusedAtomCount(0);
|
|
|
|
|
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
|
|
|
class DynamicAtom final : public nsIAtom
|
2014-07-09 19:15:21 +04:00
|
|
|
{
|
2011-12-23 05:24:44 +04:00
|
|
|
public:
|
2016-05-27 03:55:53 +03:00
|
|
|
static already_AddRefed<DynamicAtom> Create(const nsAString& aString, uint32_t aHash)
|
|
|
|
{
|
|
|
|
// The refcount is appropriately initialized in the constructor.
|
|
|
|
return dont_AddRef(new DynamicAtom(aString, aHash));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GCAtomTable();
|
|
|
|
|
2017-01-26 23:43:38 +03:00
|
|
|
enum class GCKind {
|
|
|
|
RegularOperation,
|
|
|
|
Shutdown,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void GCAtomTableLocked(const MutexAutoLock& aProofOfLock,
|
|
|
|
GCKind aKind);
|
|
|
|
|
2016-05-27 03:55:53 +03:00
|
|
|
private:
|
2016-04-06 04:28:39 +03:00
|
|
|
DynamicAtom(const nsAString& aString, uint32_t aHash)
|
2016-05-27 03:55:53 +03:00
|
|
|
: mRefCnt(1)
|
2016-04-06 04:28:39 +03:00
|
|
|
{
|
|
|
|
mLength = aString.Length();
|
2016-04-06 04:28:40 +03:00
|
|
|
mIsStatic = false;
|
2016-04-06 04:28:39 +03:00
|
|
|
RefPtr<nsStringBuffer> buf = nsStringBuffer::FromString(aString);
|
|
|
|
if (buf) {
|
|
|
|
mString = static_cast<char16_t*>(buf->Data());
|
|
|
|
} else {
|
2016-04-07 02:35:50 +03:00
|
|
|
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
|
|
|
mString = static_cast<char16_t*>(buf->Data());
|
|
|
|
CopyUnicodeTo(aString, 0, mString, mLength);
|
|
|
|
mString[mLength] = char16_t(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
mHash = aHash;
|
|
|
|
MOZ_ASSERT(mHash == HashString(mString, mLength));
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
// Take ownership of buffer
|
|
|
|
mozilla::Unused << buf.forget();
|
|
|
|
}
|
2011-12-23 05:24:44 +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
|
|
|
private:
|
|
|
|
// We don't need a virtual destructor because we always delete via a
|
2016-05-27 03:55:53 +03:00
|
|
|
// DynamicAtom* pointer (in GCAtomTable()), not an nsIAtom* pointer.
|
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
|
|
|
~DynamicAtom();
|
2011-12-23 05:24:44 +04:00
|
|
|
|
|
|
|
public:
|
2016-05-27 03:55:53 +03:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2011-12-23 05:24:44 +04:00
|
|
|
NS_DECL_NSIATOM
|
|
|
|
};
|
|
|
|
|
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)
|
|
|
|
// 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
|
|
|
|
|
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
|
|
|
class StaticAtom final : public nsIAtom
|
|
|
|
{
|
|
|
|
public:
|
2016-04-06 04:28:39 +03:00
|
|
|
StaticAtom(nsStringBuffer* aStringBuffer, uint32_t aLength, uint32_t aHash)
|
|
|
|
{
|
|
|
|
mLength = aLength;
|
2016-04-06 04:28:40 +03:00
|
|
|
mIsStatic = true;
|
2016-04-06 04:28:39 +03:00
|
|
|
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)
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!gFakeBuffers) {
|
|
|
|
gFakeBuffers = MakeUnique<nsTArray<FakeBufferRefcountHelper>>();
|
|
|
|
}
|
|
|
|
gFakeBuffers->AppendElement(aStringBuffer);
|
|
|
|
#endif
|
|
|
|
|
2016-04-06 04:28:39 +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();
|
|
|
|
|
|
|
|
mHash = aHash;
|
|
|
|
MOZ_ASSERT(mHash == HashString(mString, mLength));
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
// We don't need a virtual destructor because we always delete via a
|
|
|
|
// StaticAtom* pointer (in AtomTableClearEntry()), not an nsIAtom* pointer.
|
2016-04-06 04:28:39 +03:00
|
|
|
~StaticAtom() {}
|
2015-01-18 04:06:03 +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
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIATOM
|
2011-12-23 05:24:44 +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
|
|
|
NS_IMPL_QUERY_INTERFACE(StaticAtom, nsIAtom)
|
|
|
|
|
2016-04-06 05:01:24 +03:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
StaticAtom::AddRef()
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
2011-12-23 05:24:44 +04:00
|
|
|
|
2016-04-06 05:01:24 +03:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
StaticAtom::Release()
|
2014-07-09 19:15:21 +04:00
|
|
|
{
|
2016-04-06 05:01:24 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DynamicAtom::ScriptableToString(nsAString& aBuf)
|
|
|
|
{
|
|
|
|
nsStringBuffer::FromData(mString)->ToString(mLength, aBuf);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StaticAtom::ScriptableToString(nsAString& aBuf)
|
|
|
|
{
|
|
|
|
nsStringBuffer::FromData(mString)->ToString(mLength, aBuf);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DynamicAtom::ToUTF8String(nsACString& aBuf)
|
|
|
|
{
|
|
|
|
CopyUTF16toUTF8(nsDependentString(mString, mLength), aBuf);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StaticAtom::ToUTF8String(nsACString& aBuf)
|
|
|
|
{
|
|
|
|
CopyUTF16toUTF8(nsDependentString(mString, mLength), aBuf);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DynamicAtom::ScriptableEquals(const nsAString& aString, bool* aResult)
|
|
|
|
{
|
|
|
|
*aResult = aString.Equals(nsDependentString(mString, mLength));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StaticAtom::ScriptableEquals(const nsAString& aString, bool* aResult)
|
|
|
|
{
|
|
|
|
*aResult = aString.Equals(nsDependentString(mString, mLength));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(size_t)
|
|
|
|
DynamicAtom::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf)
|
|
|
|
{
|
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
n += nsStringBuffer::FromData(mString)->SizeOfIncludingThisIfUnshared(
|
|
|
|
aMallocSizeOf);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(size_t)
|
|
|
|
StaticAtom::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf)
|
|
|
|
{
|
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
// Don't measure the string buffer pointed to by the StaticAtom because it's
|
|
|
|
// in static memory.
|
|
|
|
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
|
|
|
|
{
|
|
|
|
// These references are either to DynamicAtoms, in which case they are
|
|
|
|
// non-owning, or they are to StaticAtoms, which aren't really refcounted.
|
|
|
|
// See the comment at the top of this file for more details.
|
|
|
|
nsIAtom* MOZ_NON_OWNING_REF mAtom;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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);
|
|
|
|
nsIAtom* atom = entry->mAtom;
|
|
|
|
if (atom->IsStaticAtom()) {
|
|
|
|
// This case -- when the entry being cleared holds a StaticAtom -- only
|
|
|
|
// occurs when gAtomTable is destroyed, whereupon all StaticAtoms within it
|
|
|
|
// must be explicitly deleted. The cast is required because StaticAtom
|
|
|
|
// doesn't have a virtual destructor.
|
|
|
|
delete static_cast<StaticAtom*>(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,
|
2001-10-21 03:19:07 +04:00
|
|
|
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
|
|
|
|
static nsIAtom*
|
|
|
|
sRecentlyUsedMainThreadAtoms[RECENTLY_USED_MAIN_THREAD_ATOM_CACHE_SIZE] = {};
|
|
|
|
|
2016-05-27 03:55:53 +03:00
|
|
|
void
|
|
|
|
DynamicAtom::GCAtomTable()
|
2016-04-06 05:01:24 +03:00
|
|
|
{
|
2017-04-03 23:13:18 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
MutexAutoLock lock(*gAtomTableLock);
|
|
|
|
GCAtomTableLocked(lock, GCKind::RegularOperation);
|
|
|
|
}
|
2017-01-26 23:43:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DynamicAtom::GCAtomTableLocked(const MutexAutoLock& aProofOfLock,
|
|
|
|
GCKind aKind)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-27 03:55:53 +03:00
|
|
|
uint32_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto atom = static_cast<DynamicAtom*>(entry->mAtom);
|
|
|
|
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
|
|
|
|
|
|
|
// During the course of this function, the atom table is locked. This means
|
|
|
|
// that, barring refcounting bugs in consumers, an atom can never go from
|
|
|
|
// refcount == 0 to refcount != 0 during a GC. However, an atom _can_ go from
|
|
|
|
// refcount != 0 to refcount == 0 if a Release() occurs in parallel with GC.
|
|
|
|
// This means that we cannot assert that gUnusedAtomCount == removedCount, and
|
|
|
|
// thus that there are no unused atoms at the end of a GC. We can and do,
|
|
|
|
// however, assert this after the last GC at shutdown.
|
2017-01-26 23:43:38 +03:00
|
|
|
if (aKind == GCKind::RegularOperation) {
|
|
|
|
MOZ_ASSERT(removedCount <= gUnusedAtomCount);
|
|
|
|
} else {
|
|
|
|
// Complain if somebody adds new GCKind enums.
|
|
|
|
MOZ_ASSERT(aKind == GCKind::Shutdown);
|
|
|
|
// Our unused atom count should be accurate.
|
|
|
|
MOZ_ASSERT(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
|
|
|
|
2016-05-27 03:55:53 +03:00
|
|
|
NS_IMPL_QUERY_INTERFACE(DynamicAtom, nsIAtom)
|
2016-04-06 05:01:24 +03:00
|
|
|
|
2016-05-27 03:55:53 +03:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
DynamicAtom::AddRef(void)
|
|
|
|
{
|
|
|
|
nsrefcnt count = ++mRefCnt;
|
|
|
|
if (count == 1) {
|
|
|
|
MOZ_ASSERT(gUnusedAtomCount > 0);
|
|
|
|
gUnusedAtomCount--;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// We set a lower GC threshold for atoms in debug builds so that we exercise
|
|
|
|
// the GC machinery more often.
|
|
|
|
static const uint32_t kAtomGCThreshold = 20;
|
|
|
|
#else
|
|
|
|
static const uint32_t kAtomGCThreshold = 10000;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
DynamicAtom::Release(void)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mRefCnt > 0);
|
|
|
|
nsrefcnt count = --mRefCnt;
|
|
|
|
if (count == 0) {
|
|
|
|
if (++gUnusedAtomCount >= kAtomGCThreshold) {
|
|
|
|
GCAtomTable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicAtom::~DynamicAtom()
|
|
|
|
{
|
2016-04-06 05:01:24 +03:00
|
|
|
nsStringBuffer::FromData(mString)->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
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 };
|
|
|
|
|
|
|
|
// StaticAtoms aren't really refcounted. Because these entries live in a
|
|
|
|
// global hashtable, this reference is essentially owning.
|
|
|
|
StaticAtom* MOZ_OWNING_REF mAtom;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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, "");
|
|
|
|
static nsIAtom* empty_atom = nullptr;
|
|
|
|
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);
|
|
|
|
DynamicAtom::GCAtomTableLocked(lock, DynamicAtom::GCKind::Shutdown);
|
|
|
|
}
|
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
|
2012-08-22 19:56:38 +04:00
|
|
|
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;
|
|
|
|
nsIAtom** atomp = aAtoms[i].mAtom;
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
nsIAtom* 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 {
|
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
|
|
|
atom = new StaticAtom(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());
|
|
|
|
entry->mAtom = static_cast<StaticAtom*>(atom);
|
2003-03-25 21:55:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-22 15:13:22 +04:00
|
|
|
already_AddRefed<nsIAtom>
|
2016-03-29 02:09:43 +03:00
|
|
|
NS_Atomize(const char* aUTF8String)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2016-03-29 02:09:43 +03:00
|
|
|
return NS_Atomize(nsDependentCString(aUTF8String));
|
2007-07-12 00:46:43 +04:00
|
|
|
}
|
|
|
|
|
2013-04-22 15:13:22 +04:00
|
|
|
already_AddRefed<nsIAtom>
|
2016-03-29 02:09:43 +03:00
|
|
|
NS_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) {
|
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);
|
2016-05-27 03:55:53 +03:00
|
|
|
RefPtr<DynamicAtom> atom = DynamicAtom::Create(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>
|
2016-03-29 02:09:43 +03:00
|
|
|
NS_Atomize(const char16_t* aUTF16String)
|
2003-03-25 21:55:59 +03:00
|
|
|
{
|
2016-03-29 02:09:43 +03:00
|
|
|
return NS_Atomize(nsDependentString(aUTF16String));
|
2005-11-04 22:52:18 +03:00
|
|
|
}
|
|
|
|
|
2013-04-22 15:13:22 +04:00
|
|
|
already_AddRefed<nsIAtom>
|
2016-03-29 02:09:43 +03:00
|
|
|
NS_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) {
|
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
|
|
|
|
2016-05-27 03:55:53 +03:00
|
|
|
RefPtr<DynamicAtom> atom = DynamicAtom::Create(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-04-03 23:13:18 +03:00
|
|
|
already_AddRefed<nsIAtom>
|
|
|
|
NS_AtomizeMainThread(const nsAString& aUTF16String)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsCOMPtr<nsIAtom> retVal;
|
|
|
|
uint32_t hash;
|
|
|
|
AtomTableKey key(aUTF16String.Data(), aUTF16String.Length(), &hash);
|
|
|
|
uint32_t index = hash % RECENTLY_USED_MAIN_THREAD_ATOM_CACHE_SIZE;
|
|
|
|
nsIAtom* atom =
|
|
|
|
sRecentlyUsedMainThreadAtoms[index];
|
|
|
|
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 {
|
|
|
|
retVal = DynamicAtom::Create(aUTF16String, hash);
|
|
|
|
he->mAtom = retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
sRecentlyUsedMainThreadAtoms[index] = retVal;
|
|
|
|
return retVal.forget();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-05-27 03:55:53 +03:00
|
|
|
DynamicAtom::GCAtomTable(); // Trigger a GC so that 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
|
|
|
|
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.");
|
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
|
|
|
}
|