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/PtrUtil.h" | 8 #include "wtf/OwnPtr.h" |
9 #include "wtf/allocator/Partitions.h" | 9 #include "wtf/allocator/Partitions.h" |
10 #include <memory> | |
11 | 10 |
12 namespace WTF { | 11 namespace WTF { |
13 | 12 |
14 // 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 |
15 // 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 |
16 // check type T must provide isLastInArray method. | 15 // check type T must provide isLastInArray method. |
17 // TerminatedArray<T> can only be constructed by TerminatedArrayBuilder<T>. | 16 // TerminatedArray<T> can only be constructed by TerminatedArrayBuilder<T>. |
18 template<typename T> | 17 template<typename T> |
19 class TerminatedArray { | 18 class TerminatedArray { |
20 DISALLOW_NEW(); | 19 DISALLOW_NEW(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 const_iterator end() const { return const_iterator(0); } | 59 const_iterator end() const { return const_iterator(0); } |
61 | 60 |
62 size_t size() const | 61 size_t size() const |
63 { | 62 { |
64 size_t count = 0; | 63 size_t count = 0; |
65 for (const_iterator it = begin(); it != end(); ++it) | 64 for (const_iterator it = begin(); it != end(); ++it) |
66 count++; | 65 count++; |
67 return count; | 66 return count; |
68 } | 67 } |
69 | 68 |
70 // Match Allocator semantics to be able to use std::unique_ptr<TerminatedArr
ay>. | 69 // Match Allocator semantics to be able to use OwnPtr<TerminatedArray>. |
71 void operator delete(void* p) { ::WTF::Partitions::fastFree(p); } | 70 void operator delete(void* p) { ::WTF::Partitions::fastFree(p); } |
72 | 71 |
73 private: | 72 private: |
74 // Allocator describes how TerminatedArrayBuilder should create new instance
s | 73 // Allocator describes how TerminatedArrayBuilder should create new instance
s |
75 // of TerminateArray and manage their lifetimes. | 74 // of TerminateArray and manage their lifetimes. |
76 struct Allocator { | 75 struct Allocator { |
77 STATIC_ONLY(Allocator); | 76 STATIC_ONLY(Allocator); |
78 using PassPtr = std::unique_ptr<TerminatedArray>; | 77 using PassPtr = PassOwnPtr<TerminatedArray>; |
79 using Ptr = std::unique_ptr<TerminatedArray>; | 78 using Ptr = OwnPtr<TerminatedArray>; |
80 | 79 |
81 static PassPtr release(Ptr& ptr) | 80 static PassPtr release(Ptr& ptr) |
82 { | 81 { |
83 return ptr.release(); | 82 return ptr.release(); |
84 } | 83 } |
85 | 84 |
86 static PassPtr create(size_t capacity) | 85 static PassPtr create(size_t capacity) |
87 { | 86 { |
88 return wrapUnique(static_cast<TerminatedArray*>(WTF::Partitions::fas
tMalloc(capacity * sizeof(T), WTF_HEAP_PROFILER_TYPE_NAME(T)))); | 87 return adoptPtr(static_cast<TerminatedArray*>(WTF::Partitions::fastM
alloc(capacity * sizeof(T), WTF_HEAP_PROFILER_TYPE_NAME(T)))); |
89 } | 88 } |
90 | 89 |
91 static PassPtr resize(Ptr ptr, size_t capacity) | 90 static PassPtr resize(Ptr ptr, size_t capacity) |
92 { | 91 { |
93 return wrapUnique(static_cast<TerminatedArray*>(WTF::Partitions::fas
tRealloc(ptr.release(), capacity * sizeof(T), WTF_HEAP_PROFILER_TYPE_NAME(T)))); | 92 return adoptPtr(static_cast<TerminatedArray*>(WTF::Partitions::fastR
ealloc(ptr.leakPtr(), capacity * sizeof(T), WTF_HEAP_PROFILER_TYPE_NAME(T)))); |
94 } | 93 } |
95 }; | 94 }; |
96 | 95 |
97 // Prohibit construction. Allocator makes TerminatedArray instances for | 96 // Prohibit construction. Allocator makes TerminatedArray instances for |
98 // TerminatedArrayBuilder by pointer casting. | 97 // TerminatedArrayBuilder by pointer casting. |
99 TerminatedArray(); | 98 TerminatedArray(); |
100 | 99 |
101 template<typename, template <typename> class> friend class TerminatedArrayBu
ilder; | 100 template<typename, template <typename> class> friend class TerminatedArrayBu
ilder; |
102 }; | 101 }; |
103 | 102 |
104 } // namespace WTF | 103 } // namespace WTF |
105 | 104 |
106 using WTF::TerminatedArray; | 105 using WTF::TerminatedArray; |
107 | 106 |
108 #endif // TerminatedArray_h | 107 #endif // TerminatedArray_h |
OLD | NEW |