From 5018bb4f9b042166fb69d76184e3cc8c430dc1e9 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 21 Sep 2017 14:02:05 +1000 Subject: [PATCH] Bug 1401873 - Remove nsHtml5Atom. r=froydnj,hsivonen. nsHtml5Atoms are very similar to dynamic nsAtoms. This patch removes the former in favour of the latter, which leaves nsAtom as the only subclass of nsIAtom. nsAtom::mKind is still used to distinguish dynamic atoms from HTML5 atoms, and the HTML5 parser still uses manual memory management to handle its HTML5 atoms. nsHtml5AtomEntry::mAtom had to be changed from an nsAutoPtr to a raw pointer because nsAtom's destructor is private. MozReview-Commit-ID: 1pBzwkog3ut --HG-- extra : rebase_source : fbb819e527cb30606348da9ce3eede62e00fb936 --- parser/html/moz.build | 1 - parser/html/nsHtml5Atom.cpp | 63 -------------------------- parser/html/nsHtml5Atom.h | 28 ------------ parser/html/nsHtml5AtomTable.cpp | 4 +- parser/html/nsHtml5AtomTable.h | 10 +--- parser/html/nsHtml5SpeculativeLoad.cpp | 1 + xpcom/ds/nsAtomTable.cpp | 29 +++++++----- xpcom/ds/nsIAtom.h | 9 +++- 8 files changed, 29 insertions(+), 116 deletions(-) delete mode 100644 parser/html/nsHtml5Atom.cpp delete mode 100644 parser/html/nsHtml5Atom.h diff --git a/parser/html/moz.build b/parser/html/moz.build index 256fa9f42e6a..457e07e69829 100644 --- a/parser/html/moz.build +++ b/parser/html/moz.build @@ -55,7 +55,6 @@ EXPORTS += [ ] UNIFIED_SOURCES += [ - 'nsHtml5Atom.cpp', 'nsHtml5AtomTable.cpp', 'nsHtml5AttributeName.cpp', 'nsHtml5DependentUTF16Buffer.cpp', diff --git a/parser/html/nsHtml5Atom.cpp b/parser/html/nsHtml5Atom.cpp deleted file mode 100644 index 19d1aa7d1e2c..000000000000 --- a/parser/html/nsHtml5Atom.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/* 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/. */ - -#include "nsHtml5Atom.h" -#include "nsAutoPtr.h" -#include "mozilla/Unused.h" - -nsHtml5Atom::nsHtml5Atom(const nsAString& aString) -{ - mLength = aString.Length(); - SetKind(AtomKind::HTML5Atom); - RefPtr buf = nsStringBuffer::FromString(aString); - if (buf) { - mString = static_cast(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); - } - mString = static_cast(buf->Data()); - CopyUnicodeTo(aString, 0, mString, mLength); - mString[mLength] = char16_t(0); - } - - 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(); -} - -nsHtml5Atom::~nsHtml5Atom() -{ - nsStringBuffer::FromData(mString)->Release(); -} - -NS_IMETHODIMP -nsHtml5Atom::QueryInterface(REFNSIID aIID, void** aInstancePtr) -{ - NS_NOTREACHED("Attempt to call QueryInterface an nsHtml5Atom."); - return NS_ERROR_UNEXPECTED; -} - -NS_IMETHODIMP -nsHtml5Atom::ToUTF8String(nsACString& aReturn) -{ - NS_NOTREACHED("Should not attempt to convert to an UTF-8 string."); - return NS_ERROR_NOT_IMPLEMENTED; -} - -NS_IMETHODIMP_(size_t) -nsHtml5Atom::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) -{ - NS_NOTREACHED("Should not call SizeOfIncludingThis."); - return 0; -} - diff --git a/parser/html/nsHtml5Atom.h b/parser/html/nsHtml5Atom.h deleted file mode 100644 index be56b5ed6bab..000000000000 --- a/parser/html/nsHtml5Atom.h +++ /dev/null @@ -1,28 +0,0 @@ -/* 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/. */ - -#ifndef nsHtml5Atom_h -#define nsHtml5Atom_h - -#include "nsIAtom.h" -#include "mozilla/Attributes.h" - -/** - * A dynamic atom implementation meant for use within the nsHtml5Tokenizer and - * nsHtml5TreeBuilder owned by one nsHtml5Parser or nsHtml5StreamParser - * instance. - * - * Usage is documented in nsHtml5AtomTable and nsIAtom. - */ -class nsHtml5Atom final : public nsIAtom -{ - public: - NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) final; - NS_DECL_NSIATOM - - explicit nsHtml5Atom(const nsAString& aString); - ~nsHtml5Atom(); -}; - -#endif // nsHtml5Atom_h diff --git a/parser/html/nsHtml5AtomTable.cpp b/parser/html/nsHtml5AtomTable.cpp index b8f967929838..ed149c056f2c 100644 --- a/parser/html/nsHtml5AtomTable.cpp +++ b/parser/html/nsHtml5AtomTable.cpp @@ -3,12 +3,11 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "nsHtml5AtomTable.h" -#include "nsHtml5Atom.h" #include "nsThreadUtils.h" nsHtml5AtomEntry::nsHtml5AtomEntry(KeyTypePointer aStr) : nsStringHashKey(aStr) - , mAtom(new nsHtml5Atom(*aStr)) + , mAtom(new nsAtom(nsAtom::AtomKind::HTML5Atom, *aStr, 0)) { } @@ -21,6 +20,7 @@ nsHtml5AtomEntry::nsHtml5AtomEntry(const nsHtml5AtomEntry& aOther) nsHtml5AtomEntry::~nsHtml5AtomEntry() { + delete mAtom; } nsHtml5AtomTable::nsHtml5AtomTable() diff --git a/parser/html/nsHtml5AtomTable.h b/parser/html/nsHtml5AtomTable.h index 48153c6ec12f..91e3469cbe81 100644 --- a/parser/html/nsHtml5AtomTable.h +++ b/parser/html/nsHtml5AtomTable.h @@ -7,26 +7,20 @@ #include "nsHashKeys.h" #include "nsTHashtable.h" -#include "nsAutoPtr.h" #include "nsIAtom.h" #include "nsISerialEventTarget.h" #define RECENTLY_USED_PARSER_ATOMS_SIZE 31 -class nsHtml5Atom; - class nsHtml5AtomEntry : public nsStringHashKey { public: explicit nsHtml5AtomEntry(KeyTypePointer aStr); nsHtml5AtomEntry(const nsHtml5AtomEntry& aOther); ~nsHtml5AtomEntry(); - inline nsHtml5Atom* GetAtom() - { - return mAtom; - } + inline nsAtom* GetAtom() { return mAtom; } private: - nsAutoPtr mAtom; + nsAtom* mAtom; }; /** diff --git a/parser/html/nsHtml5SpeculativeLoad.cpp b/parser/html/nsHtml5SpeculativeLoad.cpp index 82ebb916a54c..069e746f90e6 100644 --- a/parser/html/nsHtml5SpeculativeLoad.cpp +++ b/parser/html/nsHtml5SpeculativeLoad.cpp @@ -4,6 +4,7 @@ #include "nsHtml5SpeculativeLoad.h" #include "nsHtml5TreeOpExecutor.h" +#include "mozilla/Encoding.h" nsHtml5SpeculativeLoad::nsHtml5SpeculativeLoad() : diff --git a/xpcom/ds/nsAtomTable.cpp b/xpcom/ds/nsAtomTable.cpp index 4470a143de8f..6d3cd4ef8309 100644 --- a/xpcom/ds/nsAtomTable.cpp +++ b/xpcom/ds/nsAtomTable.cpp @@ -135,12 +135,13 @@ private: UniquePtr> gFakeBuffers; #endif -// This constructor is for dynamic atoms. -nsAtom::nsAtom(const nsAString& aString, uint32_t aHash) +// This constructor is for dynamic atoms and HTML5 atoms. +nsAtom::nsAtom(AtomKind aKind, const nsAString& aString, uint32_t aHash) : mRefCnt(1) { mLength = aString.Length(); - SetKind(AtomKind::DynamicAtom); + SetKind(aKind); + MOZ_ASSERT(IsDynamicAtom() || IsHTML5Atom()); RefPtr buf = nsStringBuffer::FromString(aString); if (buf) { mString = static_cast(buf->Data()); @@ -158,7 +159,7 @@ nsAtom::nsAtom(const nsAString& aString, uint32_t aHash) } mHash = aHash; - MOZ_ASSERT(mHash == HashString(mString, mLength)); + MOZ_ASSERT_IF(IsDynamicAtom(), mHash == HashString(mString, mLength)); NS_ASSERTION(mString[mLength] == char16_t(0), "null terminated"); NS_ASSERTION(buf && buf->StorageSize() >= (mLength + 1) * sizeof(char16_t), @@ -202,10 +203,9 @@ nsAtom::nsAtom(nsStringBuffer* aStringBuffer, uint32_t aLength, uint32_t aHash) // GCAtomTableLocked() for dynamic atoms), not an nsIAtom* pointer. nsAtom::~nsAtom() { - if (IsDynamicAtom()) { + if (!IsStaticAtom()) { + MOZ_ASSERT(IsDynamicAtom() || IsHTML5Atom()); nsStringBuffer::FromData(mString)->Release(); - } else { - MOZ_ASSERT(IsStaticAtom()); } } @@ -214,6 +214,7 @@ NS_IMPL_QUERY_INTERFACE(nsAtom, nsIAtom); NS_IMETHODIMP nsAtom::ToUTF8String(nsACString& aBuf) { + MOZ_ASSERT(!IsHTML5Atom(), "Called ToUTF8String() on an HTML5 atom"); CopyUTF16toUTF8(nsDependentString(mString, mLength), aBuf); return NS_OK; } @@ -221,6 +222,7 @@ nsAtom::ToUTF8String(nsACString& aBuf) NS_IMETHODIMP_(size_t) nsAtom::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) { + MOZ_ASSERT(!IsHTML5Atom(), "Called SizeOfIncludingThis() on an HTML5 atom"); size_t n = aMallocSizeOf(this); // String buffers pointed to by static atoms are in static memory, and so // are not measured here. @@ -238,7 +240,7 @@ nsAtom::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) NS_IMETHODIMP_(MozExternalRefCountType) nsIAtom::AddRef() { - MOZ_ASSERT(!IsHTML5Atom(), "Attempt to AddRef an nsHtml5Atom"); + MOZ_ASSERT(!IsHTML5Atom(), "Attempt to AddRef an HTML5 atom"); if (!IsDynamicAtom()) { MOZ_ASSERT(IsStaticAtom()); return 2; @@ -249,7 +251,7 @@ nsIAtom::AddRef() NS_IMETHODIMP_(MozExternalRefCountType) nsIAtom::Release() { - MOZ_ASSERT(!IsHTML5Atom(), "Attempt to Release an nsHtml5Atom"); + MOZ_ASSERT(!IsHTML5Atom(), "Attempt to Release an HTML5 atom"); if (!IsDynamicAtom()) { MOZ_ASSERT(IsStaticAtom()); return 1; @@ -721,7 +723,8 @@ nsAtomFriend::Atomize(const nsACString& aUTF8String) // Actually, now there is, sort of: ForgetSharedBuffer. nsString str; CopyUTF8toUTF16(aUTF8String, str); - RefPtr atom = dont_AddRef(new nsAtom(str, hash)); + RefPtr atom = + dont_AddRef(new nsAtom(nsAtom::AtomKind::DynamicAtom, str, hash)); he->mAtom = atom; @@ -755,7 +758,8 @@ nsAtomFriend::Atomize(const nsAString& aUTF16String) return atom.forget(); } - RefPtr atom = dont_AddRef(new nsAtom(aUTF16String, hash)); + RefPtr atom = + dont_AddRef(new nsAtom(nsAtom::AtomKind::DynamicAtom, aUTF16String, hash)); he->mAtom = atom; return atom.forget(); @@ -792,7 +796,8 @@ nsAtomFriend::AtomizeMainThread(const nsAString& aUTF16String) if (he->mAtom) { retVal = he->mAtom; } else { - RefPtr newAtom = dont_AddRef(new nsAtom(aUTF16String, hash)); + RefPtr newAtom = dont_AddRef( + new nsAtom(nsAtom::AtomKind::DynamicAtom, aUTF16String, hash)); he->mAtom = newAtom; retVal = newAtom.forget(); } diff --git a/xpcom/ds/nsIAtom.h b/xpcom/ds/nsIAtom.h index 201c2ff97190..33a461ac12d7 100644 --- a/xpcom/ds/nsIAtom.h +++ b/xpcom/ds/nsIAtom.h @@ -79,7 +79,11 @@ public: // A hashcode that is better distributed than the actual atom pointer, for // use in situations that need a well-distributed hashcode. - uint32_t hash() const { return mHash; } + uint32_t hash() const + { + MOZ_ASSERT(!IsHTML5Atom()); + return mHash; + } protected: uint32_t mLength: 30; @@ -107,9 +111,10 @@ public: private: friend class nsIAtom; friend class nsAtomFriend; + friend class nsHtml5AtomEntry; // Construction and destruction is done entirely by |friend|s. - nsAtom(const nsAString& aString, uint32_t aHash); + nsAtom(AtomKind aKind, const nsAString& aString, uint32_t aHash); nsAtom(nsStringBuffer* aStringBuffer, uint32_t aLength, uint32_t aHash); ~nsAtom();