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