Line data Source code
1 : /* This Source Code Form is subject to the terms of the Mozilla Public
2 : * License, v. 2.0. If a copy of the MPL was not distributed with this
3 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 :
5 : #include "nsHtml5AtomTable.h"
6 : #include "nsHtml5Atom.h"
7 : #include "nsThreadUtils.h"
8 :
9 0 : nsHtml5AtomEntry::nsHtml5AtomEntry(KeyTypePointer aStr)
10 : : nsStringHashKey(aStr)
11 0 : , mAtom(new nsHtml5Atom(*aStr))
12 : {
13 0 : }
14 :
15 0 : nsHtml5AtomEntry::nsHtml5AtomEntry(const nsHtml5AtomEntry& aOther)
16 : : nsStringHashKey(aOther)
17 0 : , mAtom(nullptr)
18 : {
19 0 : NS_NOTREACHED("nsHtml5AtomTable is broken and tried to copy an entry");
20 0 : }
21 :
22 0 : nsHtml5AtomEntry::~nsHtml5AtomEntry()
23 : {
24 0 : }
25 :
26 4 : nsHtml5AtomTable::nsHtml5AtomTable()
27 4 : : mRecentlyUsedParserAtoms{}
28 : {
29 : #ifdef DEBUG
30 4 : mPermittedLookupEventTarget = mozilla::GetCurrentThreadSerialEventTarget();
31 : #endif
32 4 : }
33 :
34 1 : nsHtml5AtomTable::~nsHtml5AtomTable()
35 : {
36 1 : }
37 :
38 : nsIAtom*
39 1 : nsHtml5AtomTable::GetAtom(const nsAString& aKey)
40 : {
41 : #ifdef DEBUG
42 : {
43 1 : MOZ_ASSERT(mPermittedLookupEventTarget->IsOnCurrentThread());
44 : }
45 : #endif
46 :
47 1 : uint32_t index = mozilla::HashString(aKey) % RECENTLY_USED_PARSER_ATOMS_SIZE;
48 1 : nsIAtom* cachedAtom = mRecentlyUsedParserAtoms[index];
49 1 : if (cachedAtom && cachedAtom->Equals(aKey)) {
50 0 : return cachedAtom;
51 : }
52 :
53 1 : nsIAtom* atom = NS_GetStaticAtom(aKey);
54 1 : if (atom) {
55 1 : mRecentlyUsedParserAtoms[index] = atom;
56 1 : return atom;
57 : }
58 0 : nsHtml5AtomEntry* entry = mTable.PutEntry(aKey);
59 0 : if (!entry) {
60 0 : return nullptr;
61 : }
62 :
63 0 : mRecentlyUsedParserAtoms[index] = entry->GetAtom();
64 0 : return entry->GetAtom();
65 : }
|