OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 | |
5 #define TerminatedArray_h | |
6 | 4 |
7 #include "wtf/Allocator.h" | 5 #include "platform/wtf/TerminatedArray.h" |
8 #include "wtf/PtrUtil.h" | |
9 #include "wtf/VectorTraits.h" | |
10 #include "wtf/allocator/Partitions.h" | |
11 #include <memory> | |
12 | 6 |
13 namespace WTF { | 7 // The contents of this header was moved to platform/wtf as part of |
14 | 8 // WTF migration project. See the following post for details: |
15 // TerminatedArray<T> represents a sequence of elements of type T in which each | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
16 // element knows whether it is the last element in the sequence or not. For this | |
17 // check type T must provide isLastInArray method. | |
18 // TerminatedArray<T> can only be constructed by TerminatedArrayBuilder<T>. | |
19 template <typename T> | |
20 class TerminatedArray { | |
21 DISALLOW_NEW(); | |
22 WTF_MAKE_NONCOPYABLE(TerminatedArray); | |
23 | |
24 public: | |
25 // When TerminatedArray::Allocator implementations grow the backing | |
26 // store, old is copied into the new and larger block. | |
27 static_assert(VectorTraits<T>::canCopyWithMemcpy, | |
28 "Array elements must be memory copyable"); | |
29 | |
30 T& at(size_t index) { return reinterpret_cast<T*>(this)[index]; } | |
31 const T& at(size_t index) const { | |
32 return reinterpret_cast<const T*>(this)[index]; | |
33 } | |
34 | |
35 template <typename U> | |
36 class iterator_base final { | |
37 STACK_ALLOCATED(); | |
38 | |
39 public: | |
40 iterator_base& operator++() { | |
41 if (m_val->isLastInArray()) { | |
42 m_val = 0; | |
43 } else { | |
44 ++m_val; | |
45 } | |
46 return *this; | |
47 } | |
48 | |
49 U& operator*() const { return *m_val; } | |
50 | |
51 bool operator==(const iterator_base& other) const { | |
52 return m_val == other.m_val; | |
53 } | |
54 bool operator!=(const iterator_base& other) const { | |
55 return !(*this == other); | |
56 } | |
57 | |
58 private: | |
59 iterator_base(U* val) : m_val(val) {} | |
60 | |
61 U* m_val; | |
62 | |
63 friend class TerminatedArray; | |
64 }; | |
65 | |
66 typedef iterator_base<T> iterator; | |
67 typedef iterator_base<const T> const_iterator; | |
68 | |
69 iterator begin() { return iterator(reinterpret_cast<T*>(this)); } | |
70 const_iterator begin() const { | |
71 return const_iterator(reinterpret_cast<const T*>(this)); | |
72 } | |
73 | |
74 iterator end() { return iterator(0); } | |
75 const_iterator end() const { return const_iterator(0); } | |
76 | |
77 size_t size() const { | |
78 size_t count = 0; | |
79 for (const_iterator it = begin(); it != end(); ++it) | |
80 count++; | |
81 return count; | |
82 } | |
83 | |
84 // Match Allocator semantics to be able to use | |
85 // std::unique_ptr<TerminatedArray>. | |
86 void operator delete(void* p) { ::WTF::Partitions::fastFree(p); } | |
87 | |
88 private: | |
89 // Allocator describes how TerminatedArrayBuilder should create new instances | |
90 // of TerminateArray and manage their lifetimes. | |
91 struct Allocator { | |
92 STATIC_ONLY(Allocator); | |
93 using PassPtr = std::unique_ptr<TerminatedArray>; | |
94 using Ptr = std::unique_ptr<TerminatedArray>; | |
95 | |
96 static PassPtr release(Ptr& ptr) { return ptr.release(); } | |
97 | |
98 static PassPtr create(size_t capacity) { | |
99 return WTF::wrapUnique( | |
100 static_cast<TerminatedArray*>(WTF::Partitions::fastMalloc( | |
101 capacity * sizeof(T), WTF_HEAP_PROFILER_TYPE_NAME(T)))); | |
102 } | |
103 | |
104 static PassPtr resize(Ptr ptr, size_t capacity) { | |
105 return WTF::wrapUnique(static_cast<TerminatedArray*>( | |
106 WTF::Partitions::fastRealloc(ptr.release(), capacity * sizeof(T), | |
107 WTF_HEAP_PROFILER_TYPE_NAME(T)))); | |
108 } | |
109 }; | |
110 | |
111 // Prohibit construction. Allocator makes TerminatedArray instances for | |
112 // TerminatedArrayBuilder by pointer casting. | |
113 TerminatedArray(); | |
114 | |
115 template <typename, template <typename> class> | |
116 friend class TerminatedArrayBuilder; | |
117 }; | |
118 | |
119 } // namespace WTF | |
120 | |
121 using WTF::TerminatedArray; | |
122 | |
123 #endif // TerminatedArray_h | |
OLD | NEW |