| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012,2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2012,2013 Google Inc. All rights reserved. |
| 3 * Copyright (C) 2014 Apple Inc. All rights reserved. | 3 * Copyright (C) 2014 Apple Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 */ | 30 */ |
| 31 | 31 |
| 32 #ifndef CollectionItemsCache_h | 32 #ifndef CollectionItemsCache_h |
| 33 #define CollectionItemsCache_h | 33 #define CollectionItemsCache_h |
| 34 | 34 |
| 35 #include "core/html/CollectionIndexCache.h" | 35 #include "core/html/CollectionIndexCache.h" |
| 36 #include "wtf/Vector.h" | 36 #include "wtf/Vector.h" |
| 37 #include <v8.h> | |
| 38 | 37 |
| 39 namespace blink { | 38 namespace blink { |
| 40 | 39 |
| 41 template <typename Collection, typename NodeType> | 40 template <typename Collection, typename NodeType> |
| 42 class CollectionItemsCache : public CollectionIndexCache<Collection, NodeType> { | 41 class CollectionItemsCache : public CollectionIndexCache<Collection, NodeType> { |
| 43 DISALLOW_NEW(); | 42 DISALLOW_NEW(); |
| 44 | 43 |
| 45 typedef CollectionIndexCache<Collection, NodeType> Base; | 44 typedef CollectionIndexCache<Collection, NodeType> Base; |
| 46 | 45 |
| 47 public: | 46 public: |
| 48 CollectionItemsCache(); | 47 CollectionItemsCache(); |
| 49 ~CollectionItemsCache(); | 48 ~CollectionItemsCache(); |
| 50 | 49 |
| 51 DEFINE_INLINE_VIRTUAL_TRACE() | 50 DEFINE_INLINE_VIRTUAL_TRACE() |
| 52 { | 51 { |
| 53 visitor->trace(m_cachedList); | 52 visitor->trace(m_cachedList); |
| 54 Base::trace(visitor); | 53 Base::trace(visitor); |
| 55 } | 54 } |
| 56 | 55 |
| 57 unsigned nodeCount(const Collection&); | 56 unsigned nodeCount(const Collection&); |
| 58 NodeType* nodeAt(const Collection&, unsigned index); | 57 NodeType* nodeAt(const Collection&, unsigned index); |
| 59 void invalidate(); | 58 void invalidate(); |
| 60 | 59 |
| 61 private: | 60 private: |
| 62 ptrdiff_t allocationSize() const { return m_cachedList.capacity() * sizeof(N
odeType*); } | |
| 63 static void reportExtraMemoryCostForCollectionItemsCache(ptrdiff_t diff) | |
| 64 { | |
| 65 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(diff); | |
| 66 } | |
| 67 | |
| 68 bool m_listValid; | 61 bool m_listValid; |
| 69 WillBeHeapVector<RawPtrWillBeMember<NodeType>> m_cachedList; | 62 WillBeHeapVector<RawPtrWillBeMember<NodeType>> m_cachedList; |
| 70 }; | 63 }; |
| 71 | 64 |
| 72 template <typename Collection, typename NodeType> | 65 template <typename Collection, typename NodeType> |
| 73 CollectionItemsCache<Collection, NodeType>::CollectionItemsCache() | 66 CollectionItemsCache<Collection, NodeType>::CollectionItemsCache() |
| 74 : m_listValid(false) | 67 : m_listValid(false) |
| 75 { | 68 { |
| 76 } | 69 } |
| 77 | 70 |
| 78 template <typename Collection, typename NodeType> | 71 template <typename Collection, typename NodeType> |
| 79 CollectionItemsCache<Collection, NodeType>::~CollectionItemsCache() | 72 CollectionItemsCache<Collection, NodeType>::~CollectionItemsCache() |
| 80 { | 73 { |
| 81 if (ptrdiff_t diff = allocationSize()) | |
| 82 reportExtraMemoryCostForCollectionItemsCache(-diff); | |
| 83 } | 74 } |
| 84 | 75 |
| 85 template <typename Collection, typename NodeType> | 76 template <typename Collection, typename NodeType> |
| 86 void CollectionItemsCache<Collection, NodeType>::invalidate() | 77 void CollectionItemsCache<Collection, NodeType>::invalidate() |
| 87 { | 78 { |
| 88 Base::invalidate(); | 79 Base::invalidate(); |
| 89 if (m_listValid) { | 80 if (m_listValid) { |
| 90 m_cachedList.shrink(0); | 81 m_cachedList.shrink(0); |
| 91 m_listValid = false; | 82 m_listValid = false; |
| 92 } | 83 } |
| 93 } | 84 } |
| 94 | 85 |
| 95 template <class Collection, class NodeType> | 86 template <class Collection, class NodeType> |
| 96 unsigned CollectionItemsCache<Collection, NodeType>::nodeCount(const Collection&
collection) | 87 unsigned CollectionItemsCache<Collection, NodeType>::nodeCount(const Collection&
collection) |
| 97 { | 88 { |
| 98 if (this->isCachedNodeCountValid()) | 89 if (this->isCachedNodeCountValid()) |
| 99 return this->cachedNodeCount(); | 90 return this->cachedNodeCount(); |
| 100 | 91 |
| 101 NodeType* currentNode = collection.traverseToFirst(); | 92 NodeType* currentNode = collection.traverseToFirst(); |
| 102 unsigned currentIndex = 0; | 93 unsigned currentIndex = 0; |
| 103 ptrdiff_t oldCapacity = allocationSize(); | |
| 104 while (currentNode) { | 94 while (currentNode) { |
| 105 m_cachedList.append(currentNode); | 95 m_cachedList.append(currentNode); |
| 106 currentNode = collection.traverseForwardToOffset(currentIndex + 1, *curr
entNode, currentIndex); | 96 currentNode = collection.traverseForwardToOffset(currentIndex + 1, *curr
entNode, currentIndex); |
| 107 } | 97 } |
| 108 if (ptrdiff_t diff = allocationSize() - oldCapacity) | |
| 109 reportExtraMemoryCostForCollectionItemsCache(diff); | |
| 110 | 98 |
| 111 this->setCachedNodeCount(m_cachedList.size()); | 99 this->setCachedNodeCount(m_cachedList.size()); |
| 112 m_listValid = true; | 100 m_listValid = true; |
| 113 return this->cachedNodeCount(); | 101 return this->cachedNodeCount(); |
| 114 } | 102 } |
| 115 | 103 |
| 116 template <typename Collection, typename NodeType> | 104 template <typename Collection, typename NodeType> |
| 117 inline NodeType* CollectionItemsCache<Collection, NodeType>::nodeAt(const Collec
tion& collection, unsigned index) | 105 inline NodeType* CollectionItemsCache<Collection, NodeType>::nodeAt(const Collec
tion& collection, unsigned index) |
| 118 { | 106 { |
| 119 if (m_listValid) { | 107 if (m_listValid) { |
| 120 ASSERT(this->isCachedNodeCountValid()); | 108 ASSERT(this->isCachedNodeCountValid()); |
| 121 return index < this->cachedNodeCount() ? m_cachedList[index] : nullptr; | 109 return index < this->cachedNodeCount() ? m_cachedList[index] : nullptr; |
| 122 } | 110 } |
| 123 return Base::nodeAt(collection, index); | 111 return Base::nodeAt(collection, index); |
| 124 } | 112 } |
| 125 | 113 |
| 126 } // namespace blink | 114 } // namespace blink |
| 127 | 115 |
| 128 #endif // CollectionItemsCache_h | 116 #endif // CollectionItemsCache_h |
| OLD | NEW |