| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 #ifndef TerminatedArray_h | 4 #ifndef TerminatedArray_h |
| 5 #define TerminatedArray_h | 5 #define TerminatedArray_h |
| 6 | 6 |
| 7 #include "wtf/Allocator.h" | 7 #include "wtf/Allocator.h" |
| 8 #include "wtf/OwnPtr.h" | 8 #include "wtf/OwnPtr.h" |
| 9 #include "wtf/Partitions.h" | 9 #include "wtf/Partitions.h" |
| 10 | 10 |
| 11 namespace WTF { | 11 namespace WTF { |
| 12 | 12 |
| 13 // TerminatedArray<T> represents a sequence of elements of type T in which each | 13 // TerminatedArray<T> represents a sequence of elements of type T in which each |
| 14 // element knows whether it is the last element in the sequence or not. For this | 14 // element knows whether it is the last element in the sequence or not. For this |
| 15 // check type T must provide isLastInArray method. | 15 // check type T must provide isLastInArray method. |
| 16 // TerminatedArray<T> can only be constructed by TerminatedArrayBuilder<T>. | 16 // TerminatedArray<T> can only be constructed by TerminatedArrayBuilder<T>. |
| 17 template<typename T> | 17 template <typename T> |
| 18 class TerminatedArray { | 18 class TerminatedArray { |
| 19 DISALLOW_NEW(); | 19 DISALLOW_NEW(); |
| 20 WTF_MAKE_NONCOPYABLE(TerminatedArray); | 20 WTF_MAKE_NONCOPYABLE(TerminatedArray); |
| 21 public: | |
| 22 T& at(size_t index) { return reinterpret_cast<T*>(this)[index]; } | |
| 23 const T& at(size_t index) const { return reinterpret_cast<const T*>(this)[in
dex]; } | |
| 24 | 21 |
| 25 template<typename U> | 22 public: |
| 26 class iterator_base final { | 23 T& at(size_t index) { return reinterpret_cast<T*>(this)[index]; } |
| 27 STACK_ALLOCATED(); | 24 const T& at(size_t index) const { |
| 28 public: | 25 return reinterpret_cast<const T*>(this)[index]; |
| 29 iterator_base& operator++() | 26 } |
| 30 { | |
| 31 if (m_val->isLastInArray()) { | |
| 32 m_val = 0; | |
| 33 } else { | |
| 34 ++m_val; | |
| 35 } | |
| 36 return *this; | |
| 37 } | |
| 38 | 27 |
| 39 U& operator*() const { return *m_val; } | 28 template <typename U> |
| 29 class iterator_base final { |
| 30 STACK_ALLOCATED(); |
| 40 | 31 |
| 41 bool operator==(const iterator_base& other) const { return m_val == othe
r.m_val; } | 32 public: |
| 42 bool operator!=(const iterator_base& other) const { return !(*this == ot
her); } | 33 iterator_base& operator++() { |
| 43 | 34 if (m_val->isLastInArray()) { |
| 44 private: | 35 m_val = 0; |
| 45 iterator_base(U* val) : m_val(val) { } | 36 } else { |
| 46 | 37 ++m_val; |
| 47 U* m_val; | 38 } |
| 48 | 39 return *this; |
| 49 friend class TerminatedArray; | |
| 50 }; | |
| 51 | |
| 52 typedef iterator_base<T> iterator; | |
| 53 typedef iterator_base<const T> const_iterator; | |
| 54 | |
| 55 iterator begin() { return iterator(reinterpret_cast<T*>(this)); } | |
| 56 const_iterator begin() const { return const_iterator(reinterpret_cast<const
T*>(this)); } | |
| 57 | |
| 58 iterator end() { return iterator(0); } | |
| 59 const_iterator end() const { return const_iterator(0); } | |
| 60 | |
| 61 size_t size() const | |
| 62 { | |
| 63 size_t count = 0; | |
| 64 for (const_iterator it = begin(); it != end(); ++it) | |
| 65 count++; | |
| 66 return count; | |
| 67 } | 40 } |
| 68 | 41 |
| 69 // Match Allocator semantics to be able to use OwnPtr<TerminatedArray>. | 42 U& operator*() const { return *m_val; } |
| 70 void operator delete(void* p) { ::WTF::Partitions::fastFree(p); } | |
| 71 | 43 |
| 72 private: | 44 bool operator==(const iterator_base& other) const { |
| 73 // Allocator describes how TerminatedArrayBuilder should create new instance
s | 45 return m_val == other.m_val; |
| 74 // of TerminateArray and manage their lifetimes. | 46 } |
| 75 struct Allocator { | 47 bool operator!=(const iterator_base& other) const { |
| 76 STATIC_ONLY(Allocator); | 48 return !(*this == other); |
| 77 typedef PassOwnPtr<TerminatedArray> PassPtr; | 49 } |
| 78 typedef OwnPtr<TerminatedArray> Ptr; | |
| 79 | 50 |
| 80 static PassPtr create(size_t capacity) | 51 private: |
| 81 { | 52 iterator_base(U* val) : m_val(val) {} |
| 82 return adoptPtr(static_cast<TerminatedArray*>(WTF::Partitions::fastM
alloc(capacity * sizeof(T), WTF_HEAP_PROFILER_TYPE_NAME(T)))); | |
| 83 } | |
| 84 | 53 |
| 85 static PassPtr resize(PassPtr ptr, size_t capacity) | 54 U* m_val; |
| 86 { | |
| 87 return adoptPtr(static_cast<TerminatedArray*>(WTF::Partitions::fastR
ealloc(ptr.leakPtr(), capacity * sizeof(T), WTF_HEAP_PROFILER_TYPE_NAME(T)))); | |
| 88 } | |
| 89 }; | |
| 90 | 55 |
| 91 // Prohibit construction. Allocator makes TerminatedArray instances for | 56 friend class TerminatedArray; |
| 92 // TerminatedArrayBuilder by pointer casting. | 57 }; |
| 93 TerminatedArray(); | |
| 94 | 58 |
| 95 template<typename, template <typename> class> friend class TerminatedArrayBu
ilder; | 59 typedef iterator_base<T> iterator; |
| 60 typedef iterator_base<const T> const_iterator; |
| 61 |
| 62 iterator begin() { return iterator(reinterpret_cast<T*>(this)); } |
| 63 const_iterator begin() const { |
| 64 return const_iterator(reinterpret_cast<const T*>(this)); |
| 65 } |
| 66 |
| 67 iterator end() { return iterator(0); } |
| 68 const_iterator end() const { return const_iterator(0); } |
| 69 |
| 70 size_t size() const { |
| 71 size_t count = 0; |
| 72 for (const_iterator it = begin(); it != end(); ++it) |
| 73 count++; |
| 74 return count; |
| 75 } |
| 76 |
| 77 // Match Allocator semantics to be able to use OwnPtr<TerminatedArray>. |
| 78 void operator delete(void* p) { ::WTF::Partitions::fastFree(p); } |
| 79 |
| 80 private: |
| 81 // Allocator describes how TerminatedArrayBuilder should create new instances |
| 82 // of TerminateArray and manage their lifetimes. |
| 83 struct Allocator { |
| 84 STATIC_ONLY(Allocator); |
| 85 typedef PassOwnPtr<TerminatedArray> PassPtr; |
| 86 typedef OwnPtr<TerminatedArray> Ptr; |
| 87 |
| 88 static PassPtr create(size_t capacity) { |
| 89 return adoptPtr(static_cast<TerminatedArray*>(WTF::Partitions::fastMalloc( |
| 90 capacity * sizeof(T), WTF_HEAP_PROFILER_TYPE_NAME(T)))); |
| 91 } |
| 92 |
| 93 static PassPtr resize(PassPtr ptr, size_t capacity) { |
| 94 return adoptPtr(static_cast<TerminatedArray*>( |
| 95 WTF::Partitions::fastRealloc(ptr.leakPtr(), capacity * sizeof(T), |
| 96 WTF_HEAP_PROFILER_TYPE_NAME(T)))); |
| 97 } |
| 98 }; |
| 99 |
| 100 // Prohibit construction. Allocator makes TerminatedArray instances for |
| 101 // TerminatedArrayBuilder by pointer casting. |
| 102 TerminatedArray(); |
| 103 |
| 104 template <typename, template <typename> class> |
| 105 friend class TerminatedArrayBuilder; |
| 96 }; | 106 }; |
| 97 | 107 |
| 98 } // namespace WTF | 108 } // namespace WTF |
| 99 | 109 |
| 100 using WTF::TerminatedArray; | 110 using WTF::TerminatedArray; |
| 101 | 111 |
| 102 #endif // TerminatedArray_h | 112 #endif // TerminatedArray_h |
| OLD | NEW |