Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: Source/core/html/HTMLCollection.h

Issue 280123002: Oilpan: move LiveNodeList collections to the heap. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rebase needed Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All r ights reserved. 4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All r ights reserved.
5 * Copyright (C) 2014 Samsung Electronics. All rights reserved. 5 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
6 * 6 *
7 * This library is free software; you can redistribute it and/or 7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public 8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version. 10 * version 2 of the License, or (at your option) any later version.
(...skipping 15 matching lines...) Expand all
26 26
27 #include "core/dom/LiveNodeListBase.h" 27 #include "core/dom/LiveNodeListBase.h"
28 #include "core/html/CollectionIndexCache.h" 28 #include "core/html/CollectionIndexCache.h"
29 #include "core/html/CollectionType.h" 29 #include "core/html/CollectionType.h"
30 #include "wtf/Forward.h" 30 #include "wtf/Forward.h"
31 #include "wtf/HashMap.h" 31 #include "wtf/HashMap.h"
32 #include "wtf/Vector.h" 32 #include "wtf/Vector.h"
33 33
34 namespace WebCore { 34 namespace WebCore {
35 35
36 class HTMLCollection : public ScriptWrappable, public RefCounted<HTMLCollection> , public LiveNodeListBase { 36 class HTMLCollection : public RefCountedWillBeGarbageCollectedFinalized<HTMLColl ection>, public ScriptWrappable, public LiveNodeListBase {
37 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(HTMLCollection);
37 public: 38 public:
38 enum ItemAfterOverrideType { 39 enum ItemAfterOverrideType {
39 OverridesItemAfter, 40 OverridesItemAfter,
40 DoesNotOverrideItemAfter, 41 DoesNotOverrideItemAfter,
41 }; 42 };
42 43
43 static PassRefPtr<HTMLCollection> create(ContainerNode& base, CollectionType ); 44 static PassRefPtrWillBeRawPtr<HTMLCollection> create(ContainerNode& base, Co llectionType);
44 virtual ~HTMLCollection(); 45 virtual ~HTMLCollection();
45 virtual void invalidateCache(Document* oldDocument = 0) const OVERRIDE; 46 virtual void invalidateCache(Document* oldDocument = 0) const OVERRIDE;
46 void invalidateCacheForAttribute(const QualifiedName*) const; 47 void invalidateCacheForAttribute(const QualifiedName*) const;
47 48
48 // DOM API 49 // DOM API
49 unsigned length() const { return m_collectionIndexCache.nodeCount(*this); } 50 unsigned length() const { return m_collectionIndexCache.nodeCount(*this); }
50 Element* item(unsigned offset) const { return m_collectionIndexCache.nodeAt( *this, offset); } 51 Element* item(unsigned offset) const { return m_collectionIndexCache.nodeAt( *this, offset); }
51 virtual Element* namedItem(const AtomicString& name) const; 52 virtual Element* namedItem(const AtomicString& name) const;
52 bool namedPropertyQuery(const AtomicString&, ExceptionState&); 53 bool namedPropertyQuery(const AtomicString&, ExceptionState&);
53 void namedPropertyEnumerator(Vector<String>& names, ExceptionState&); 54 void namedPropertyEnumerator(Vector<String>& names, ExceptionState&);
54 55
55 // Non-DOM API 56 // Non-DOM API
56 void namedItems(const AtomicString& name, Vector<RefPtr<Element> >&) const; 57 void namedItems(const AtomicString& name, WillBeHeapVector<RefPtrWillBeMembe r<Element> >&) const;
57 bool isEmpty() const { return m_collectionIndexCache.isEmpty(*this); } 58 bool isEmpty() const { return m_collectionIndexCache.isEmpty(*this); }
58 bool hasExactlyOneItem() const { return m_collectionIndexCache.hasExactlyOne Node(*this); } 59 bool hasExactlyOneItem() const { return m_collectionIndexCache.hasExactlyOne Node(*this); }
59 60
60 // CollectionIndexCache API. 61 // CollectionIndexCache API.
61 bool canTraverseBackward() const { return !overridesItemAfter(); } 62 bool canTraverseBackward() const { return !overridesItemAfter(); }
62 Element* traverseToFirstElement() const; 63 Element* traverseToFirstElement() const;
63 Element* traverseToLastElement() const; 64 Element* traverseToLastElement() const;
64 Element* traverseForwardToOffset(unsigned offset, Element& currentElement, u nsigned& currentOffset) const; 65 Element* traverseForwardToOffset(unsigned offset, Element& currentElement, u nsigned& currentOffset) const;
65 Element* traverseBackwardToOffset(unsigned offset, Element& currentElement, unsigned& currentOffset) const; 66 Element* traverseBackwardToOffset(unsigned offset, Element& currentElement, unsigned& currentOffset) const;
66 67
68 virtual void trace(Visitor*);
69
67 protected: 70 protected:
68 HTMLCollection(ContainerNode& base, CollectionType, ItemAfterOverrideType); 71 HTMLCollection(ContainerNode& base, CollectionType, ItemAfterOverrideType);
69 72
70 class NamedItemCache { 73 class NamedItemCache FINAL : public NoBaseWillBeGarbageCollected<NamedItemCa che> {
71 public: 74 public:
72 static PassOwnPtr<NamedItemCache> create() 75 static PassOwnPtrWillBeRawPtr<NamedItemCache> create()
73 { 76 {
74 return adoptPtr(new NamedItemCache); 77 return adoptPtrWillBeNoop(new NamedItemCache);
75 } 78 }
76 79
77 Vector<Element*>* getElementsById(const AtomicString& id) const { return m_idCache.get(id.impl()); } 80 WillBeHeapVector<RawPtrWillBeMember<Element> >* getElementsById(const At omicString& id) const { return m_idCache.get(id.impl()); }
78 Vector<Element*>* getElementsByName(const AtomicString& name) const { re turn m_nameCache.get(name.impl()); } 81 WillBeHeapVector<RawPtrWillBeMember<Element> >* getElementsByName(const AtomicString& name) const { return m_nameCache.get(name.impl()); }
79 void addElementWithId(const AtomicString& id, Element* element) { addEle mentToMap(m_idCache, id, element); } 82 void addElementWithId(const AtomicString& id, Element* element) { addEle mentToMap(m_idCache, id, element); }
80 void addElementWithName(const AtomicString& name, Element* element) { ad dElementToMap(m_nameCache, name, element); } 83 void addElementWithName(const AtomicString& name, Element* element) { ad dElementToMap(m_nameCache, name, element); }
81 84
85 void trace(Visitor* visitor)
86 {
87 visitor->trace(m_idCache);
88 visitor->trace(m_nameCache);
89 }
90
82 private: 91 private:
83 NamedItemCache(); 92 NamedItemCache();
84 typedef HashMap<StringImpl*, OwnPtr<Vector<Element*> > > StringToElement sMap; 93 typedef WillBeHeapHashMap<StringImpl*, OwnPtrWillBeMember<WillBeHeapVect or<RawPtrWillBeMember<Element> > > > StringToElementsMap;
85 static void addElementToMap(StringToElementsMap& map, const AtomicString & key, Element* element) 94 static void addElementToMap(StringToElementsMap& map, const AtomicString & key, Element* element)
86 { 95 {
87 OwnPtr<Vector<Element*> >& vector = map.add(key.impl(), nullptr).sto redValue->value; 96 OwnPtrWillBeMember<WillBeHeapVector<RawPtrWillBeMember<Element> > >& vector = map.add(key.impl(), nullptr).storedValue->value;
88 if (!vector) 97 if (!vector)
89 vector = adoptPtr(new Vector<Element*>); 98 vector = adoptPtrWillBeNoop(new WillBeHeapVector<RawPtrWillBeMem ber<Element> >);
90 vector->append(element); 99 vector->append(element);
91 } 100 }
92 101
93 StringToElementsMap m_idCache; 102 StringToElementsMap m_idCache;
94 StringToElementsMap m_nameCache; 103 StringToElementsMap m_nameCache;
95 }; 104 };
96 105
97 bool overridesItemAfter() const { return m_overridesItemAfter; } 106 bool overridesItemAfter() const { return m_overridesItemAfter; }
98 virtual Element* virtualItemAfter(Element*) const; 107 virtual Element* virtualItemAfter(Element*) const;
99 bool shouldOnlyIncludeDirectChildren() const { return m_shouldOnlyIncludeDir ectChildren; } 108 bool shouldOnlyIncludeDirectChildren() const { return m_shouldOnlyIncludeDir ectChildren; }
100 virtual void supportedPropertyNames(Vector<String>& names); 109 virtual void supportedPropertyNames(Vector<String>& names);
101 110
102 virtual void updateIdNameCache() const; 111 virtual void updateIdNameCache() const;
103 bool hasValidIdNameCache() const { return m_namedItemCache; } 112 bool hasValidIdNameCache() const { return m_namedItemCache; }
104 113
105 void setNamedItemCache(PassOwnPtr<NamedItemCache> cache) const 114 void setNamedItemCache(PassOwnPtrWillBeRawPtr<NamedItemCache> cache) const
106 { 115 {
107 ASSERT(!m_namedItemCache); 116 ASSERT(!m_namedItemCache);
117 #if ENABLE(OILPAN)
118 document().incrementNodeListWithIdNameCacheCount(this);
119 #else
108 document().incrementNodeListWithIdNameCacheCount(); 120 document().incrementNodeListWithIdNameCacheCount();
121 #endif
109 m_namedItemCache = cache; 122 m_namedItemCache = cache;
110 } 123 }
111 124
112 NamedItemCache& namedItemCache() const 125 NamedItemCache& namedItemCache() const
113 { 126 {
114 ASSERT(m_namedItemCache); 127 ASSERT(m_namedItemCache);
115 return *m_namedItemCache; 128 return *m_namedItemCache;
116 } 129 }
117 130
118 private: 131 private:
119 Element* traverseNextElement(Element& previous) const; 132 Element* traverseNextElement(Element& previous) const;
120 133
121 void invalidateIdNameCacheMaps(Document* oldDocument = 0) const 134 void invalidateIdNameCacheMaps(Document* oldDocument = 0) const
122 { 135 {
123 if (!hasValidIdNameCache()) 136 if (!hasValidIdNameCache())
124 return; 137 return;
125 138
126 // Make sure we decrement the NodeListWithIdNameCache count from 139 // Make sure we decrement the NodeListWithIdNameCache count from
127 // the old document instead of the new one in the case the collection 140 // the old document instead of the new one in the case the collection
128 // is moved to a new document. 141 // is moved to a new document.
129 unregisterIdNameCacheFromDocument(oldDocument ? *oldDocument : document( )); 142 unregisterIdNameCacheFromDocument(oldDocument ? *oldDocument : document( ));
130 143
131 m_namedItemCache.clear(); 144 m_namedItemCache.clear();
132 } 145 }
133 146
134 void unregisterIdNameCacheFromDocument(Document& document) const 147 void unregisterIdNameCacheFromDocument(Document& document) const
135 { 148 {
136 ASSERT(hasValidIdNameCache()); 149 ASSERT(hasValidIdNameCache());
150 #if ENABLE(OILPAN)
151 document.decrementNodeListWithIdNameCacheCount(this);
152 #else
137 document.decrementNodeListWithIdNameCacheCount(); 153 document.decrementNodeListWithIdNameCacheCount();
154 #endif
138 } 155 }
139 156
140 const unsigned m_overridesItemAfter : 1; 157 const unsigned m_overridesItemAfter : 1;
141 const unsigned m_shouldOnlyIncludeDirectChildren : 1; 158 const unsigned m_shouldOnlyIncludeDirectChildren : 1;
142 mutable OwnPtr<NamedItemCache> m_namedItemCache; 159 mutable OwnPtrWillBeMember<NamedItemCache> m_namedItemCache;
143 mutable CollectionIndexCache<HTMLCollection, Element> m_collectionIndexCache ; 160 mutable CollectionIndexCache<HTMLCollection, Element> m_collectionIndexCache ;
144 }; 161 };
145 162
146 DEFINE_TYPE_CASTS(HTMLCollection, LiveNodeListBase, collection, isHTMLCollection Type(collection->type()), isHTMLCollectionType(collection.type())); 163 DEFINE_TYPE_CASTS(HTMLCollection, LiveNodeListBase, collection, isHTMLCollection Type(collection->type()), isHTMLCollectionType(collection.type()));
147 164
148 inline void HTMLCollection::invalidateCacheForAttribute(const QualifiedName* att rName) const 165 inline void HTMLCollection::invalidateCacheForAttribute(const QualifiedName* att rName) const
149 { 166 {
150 if (!attrName || shouldInvalidateTypeOnAttributeChange(invalidationType(), * attrName)) 167 if (!attrName || shouldInvalidateTypeOnAttributeChange(invalidationType(), * attrName))
151 invalidateCache(); 168 invalidateCache();
152 else if (*attrName == HTMLNames::idAttr || *attrName == HTMLNames::nameAttr) 169 else if (*attrName == HTMLNames::idAttr || *attrName == HTMLNames::nameAttr)
153 invalidateIdNameCacheMaps(); 170 invalidateIdNameCacheMaps();
154 } 171 }
155 172
156 } // namespace 173 } // namespace
157 174
158 #endif 175 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698