OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv
ed. | 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv
ed. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 26 matching lines...) Expand all Loading... |
37 #include "core/dom/TreeScope.h" | 37 #include "core/dom/TreeScope.h" |
38 #include "core/dom/WebCoreMemoryInstrumentation.h" | 38 #include "core/dom/WebCoreMemoryInstrumentation.h" |
39 #include "core/html/HTMLMapElement.h" | 39 #include "core/html/HTMLMapElement.h" |
40 #include <wtf/MemoryInstrumentationHashCountedSet.h> | 40 #include <wtf/MemoryInstrumentationHashCountedSet.h> |
41 #include <wtf/MemoryInstrumentationHashMap.h> | 41 #include <wtf/MemoryInstrumentationHashMap.h> |
42 | 42 |
43 namespace WebCore { | 43 namespace WebCore { |
44 | 44 |
45 using namespace HTMLNames; | 45 using namespace HTMLNames; |
46 | 46 |
47 inline bool keyMatchesId(AtomicStringImpl* key, Element* element) | 47 inline bool keyMatchesId(StringImpl* key, Element* element) |
48 { | 48 { |
49 return element->getIdAttribute().impl() == key; | 49 return element->getIdAttribute().impl() == key; |
50 } | 50 } |
51 | 51 |
52 inline bool keyMatchesMapName(AtomicStringImpl* key, Element* element) | 52 inline bool keyMatchesMapName(StringImpl* key, Element* element) |
53 { | 53 { |
54 return element->hasTagName(mapTag) && static_cast<HTMLMapElement*>(element)-
>getName().impl() == key; | 54 return element->hasTagName(mapTag) && static_cast<HTMLMapElement*>(element)-
>getName().impl() == key; |
55 } | 55 } |
56 | 56 |
57 inline bool keyMatchesLowercasedMapName(AtomicStringImpl* key, Element* element) | 57 inline bool keyMatchesLowercasedMapName(StringImpl* key, Element* element) |
58 { | 58 { |
59 return element->hasTagName(mapTag) && static_cast<HTMLMapElement*>(element)-
>getName().lower().impl() == key; | 59 return element->hasTagName(mapTag) && static_cast<HTMLMapElement*>(element)-
>getName().lower().impl() == key; |
60 } | 60 } |
61 | 61 |
62 inline bool keyMatchesLabelForAttribute(AtomicStringImpl* key, Element* element) | 62 inline bool keyMatchesLabelForAttribute(StringImpl* key, Element* element) |
63 { | 63 { |
64 return element->hasTagName(labelTag) && element->getAttribute(forAttr).impl(
) == key; | 64 return element->hasTagName(labelTag) && element->getAttribute(forAttr).impl(
) == key; |
65 } | 65 } |
66 | 66 |
67 void DocumentOrderedMap::clear() | 67 void DocumentOrderedMap::clear() |
68 { | 68 { |
69 m_map.clear(); | 69 m_map.clear(); |
70 m_duplicateCounts.clear(); | 70 m_duplicateCounts.clear(); |
71 } | 71 } |
72 | 72 |
73 void DocumentOrderedMap::add(AtomicStringImpl* key, Element* element) | 73 void DocumentOrderedMap::add(StringImpl* key, Element* element) |
74 { | 74 { |
75 ASSERT(key); | 75 ASSERT(key); |
76 ASSERT(element); | 76 ASSERT(element); |
77 | 77 |
78 if (!m_duplicateCounts.contains(key)) { | 78 if (!m_duplicateCounts.contains(key)) { |
79 // Fast path. The key is not already in m_duplicateCounts, so we assume
that it's | 79 // Fast path. The key is not already in m_duplicateCounts, so we assume
that it's |
80 // also not already in m_map and try to add it. If that add succeeds, we
're done. | 80 // also not already in m_map and try to add it. If that add succeeds, we
're done. |
81 Map::AddResult addResult = m_map.add(key, element); | 81 Map::AddResult addResult = m_map.add(key, element); |
82 if (addResult.isNewEntry) | 82 if (addResult.isNewEntry) |
83 return; | 83 return; |
84 | 84 |
85 // The add failed, so this key was already cached in m_map. | 85 // The add failed, so this key was already cached in m_map. |
86 // There are multiple elements with this key. Remove the m_map | 86 // There are multiple elements with this key. Remove the m_map |
87 // cache for this key so get searches for it next time it is called. | 87 // cache for this key so get searches for it next time it is called. |
88 m_map.remove(addResult.iterator); | 88 m_map.remove(addResult.iterator); |
89 m_duplicateCounts.add(key); | 89 m_duplicateCounts.add(key); |
90 } else { | 90 } else { |
91 // There are multiple elements with this key. Remove the m_map | 91 // There are multiple elements with this key. Remove the m_map |
92 // cache for this key so get will search for it next time it is called. | 92 // cache for this key so get will search for it next time it is called. |
93 Map::iterator cachedItem = m_map.find(key); | 93 Map::iterator cachedItem = m_map.find(key); |
94 if (cachedItem != m_map.end()) { | 94 if (cachedItem != m_map.end()) { |
95 m_map.remove(cachedItem); | 95 m_map.remove(cachedItem); |
96 m_duplicateCounts.add(key); | 96 m_duplicateCounts.add(key); |
97 } | 97 } |
98 } | 98 } |
99 | 99 |
100 m_duplicateCounts.add(key); | 100 m_duplicateCounts.add(key); |
101 } | 101 } |
102 | 102 |
103 void DocumentOrderedMap::remove(AtomicStringImpl* key, Element* element) | 103 void DocumentOrderedMap::remove(StringImpl* key, Element* element) |
104 { | 104 { |
105 ASSERT(key); | 105 ASSERT(key); |
106 ASSERT(element); | 106 ASSERT(element); |
107 | 107 |
108 m_map.checkConsistency(); | 108 m_map.checkConsistency(); |
109 Map::iterator cachedItem = m_map.find(key); | 109 Map::iterator cachedItem = m_map.find(key); |
110 if (cachedItem != m_map.end() && cachedItem->value == element) | 110 if (cachedItem != m_map.end() && cachedItem->value == element) |
111 m_map.remove(cachedItem); | 111 m_map.remove(cachedItem); |
112 else | 112 else |
113 m_duplicateCounts.remove(key); | 113 m_duplicateCounts.remove(key); |
114 } | 114 } |
115 | 115 |
116 template<bool keyMatches(AtomicStringImpl*, Element*)> | 116 template<bool keyMatches(StringImpl*, Element*)> |
117 inline Element* DocumentOrderedMap::get(AtomicStringImpl* key, const TreeScope*
scope) const | 117 inline Element* DocumentOrderedMap::get(StringImpl* key, const TreeScope* scope)
const |
118 { | 118 { |
119 ASSERT(key); | 119 ASSERT(key); |
120 ASSERT(scope); | 120 ASSERT(scope); |
121 | 121 |
122 m_map.checkConsistency(); | 122 m_map.checkConsistency(); |
123 | 123 |
124 Element* element = m_map.get(key); | 124 Element* element = m_map.get(key); |
125 if (element) | 125 if (element) |
126 return element; | 126 return element; |
127 | 127 |
128 if (m_duplicateCounts.contains(key)) { | 128 if (m_duplicateCounts.contains(key)) { |
129 // We know there's at least one node that matches; iterate to find the f
irst one. | 129 // We know there's at least one node that matches; iterate to find the f
irst one. |
130 for (element = ElementTraversal::firstWithin(scope->rootNode()); element
; element = ElementTraversal::next(element)) { | 130 for (element = ElementTraversal::firstWithin(scope->rootNode()); element
; element = ElementTraversal::next(element)) { |
131 if (!keyMatches(key, element)) | 131 if (!keyMatches(key, element)) |
132 continue; | 132 continue; |
133 m_duplicateCounts.remove(key); | 133 m_duplicateCounts.remove(key); |
134 m_map.set(key, element); | 134 m_map.set(key, element); |
135 return element; | 135 return element; |
136 } | 136 } |
137 ASSERT_NOT_REACHED(); | 137 ASSERT_NOT_REACHED(); |
138 } | 138 } |
139 | 139 |
140 return 0; | 140 return 0; |
141 } | 141 } |
142 | 142 |
143 Element* DocumentOrderedMap::getElementById(AtomicStringImpl* key, const TreeSco
pe* scope) const | 143 Element* DocumentOrderedMap::getElementById(StringImpl* key, const TreeScope* sc
ope) const |
144 { | 144 { |
145 return get<keyMatchesId>(key, scope); | 145 return get<keyMatchesId>(key, scope); |
146 } | 146 } |
147 | 147 |
148 Element* DocumentOrderedMap::getElementByMapName(AtomicStringImpl* key, const Tr
eeScope* scope) const | 148 Element* DocumentOrderedMap::getElementByMapName(StringImpl* key, const TreeScop
e* scope) const |
149 { | 149 { |
150 return get<keyMatchesMapName>(key, scope); | 150 return get<keyMatchesMapName>(key, scope); |
151 } | 151 } |
152 | 152 |
153 Element* DocumentOrderedMap::getElementByLowercasedMapName(AtomicStringImpl* key
, const TreeScope* scope) const | 153 Element* DocumentOrderedMap::getElementByLowercasedMapName(StringImpl* key, cons
t TreeScope* scope) const |
154 { | 154 { |
155 return get<keyMatchesLowercasedMapName>(key, scope); | 155 return get<keyMatchesLowercasedMapName>(key, scope); |
156 } | 156 } |
157 | 157 |
158 Element* DocumentOrderedMap::getElementByLabelForAttribute(AtomicStringImpl* key
, const TreeScope* scope) const | 158 Element* DocumentOrderedMap::getElementByLabelForAttribute(StringImpl* key, cons
t TreeScope* scope) const |
159 { | 159 { |
160 return get<keyMatchesLabelForAttribute>(key, scope); | 160 return get<keyMatchesLabelForAttribute>(key, scope); |
161 } | 161 } |
162 | 162 |
163 void DocumentOrderedMap::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) c
onst | 163 void DocumentOrderedMap::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) c
onst |
164 { | 164 { |
165 MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::DOM); | 165 MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::DOM); |
166 info.addMember(m_map, "map"); | 166 info.addMember(m_map, "map"); |
167 info.addMember(m_duplicateCounts, "duplicateCounts"); | 167 info.addMember(m_duplicateCounts, "duplicateCounts"); |
168 } | 168 } |
169 | 169 |
170 } // namespace WebCore | 170 } // namespace WebCore |
OLD | NEW |