Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(916)

Side by Side Diff: third_party/WebKit/Source/wtf/TerminatedArrayBuilder.h

Issue 1650123002: Keep (Heap)TerminatedArray in a consistent state while building. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/platform/heap/HeapTest.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 TerminatedArrayBuilder_h 4 #ifndef TerminatedArrayBuilder_h
5 #define TerminatedArrayBuilder_h 5 #define TerminatedArrayBuilder_h
6 6
7 #include "wtf/OwnPtr.h" 7 #include "wtf/OwnPtr.h"
8 8
9 namespace WTF { 9 namespace WTF {
10 10
11 template<typename T, template <typename> class ArrayType = TerminatedArray> 11 template<typename T, template <typename> class ArrayType = TerminatedArray>
12 class TerminatedArrayBuilder { 12 class TerminatedArrayBuilder {
13 DISALLOW_NEW(); 13 DISALLOW_NEW();
14 WTF_MAKE_NONCOPYABLE(TerminatedArrayBuilder); 14 WTF_MAKE_NONCOPYABLE(TerminatedArrayBuilder);
15 public: 15 public:
16 explicit TerminatedArrayBuilder(typename ArrayType<T>::Allocator::PassPtr ar ray) 16 explicit TerminatedArrayBuilder(typename ArrayType<T>::Allocator::PassPtr ar ray)
17 : m_array(array) 17 : m_array(array)
18 , m_count(0) 18 , m_count(0)
19 , m_capacity(0) 19 , m_capacity(0)
20 { 20 {
21 if (!m_array) 21 if (!m_array)
22 return; 22 return;
23 m_capacity = m_count = m_array->size(); 23 m_capacity = m_count = m_array->size();
24 ASSERT(m_array->at(m_count - 1).isLastInArray());
24 } 25 }
25 26
26 void grow(size_t count) 27 void grow(size_t count)
27 { 28 {
28 ASSERT(count); 29 ASSERT(count);
29 if (!m_array) { 30 if (!m_array) {
30 ASSERT(!m_count); 31 ASSERT(!m_count);
31 ASSERT(!m_capacity); 32 ASSERT(!m_capacity);
32 m_capacity = count; 33 m_capacity = count;
33 m_array = ArrayType<T>::Allocator::create(m_capacity); 34 m_array = ArrayType<T>::Allocator::create(m_capacity);
haraken 2016/01/29 16:00:32 What happens if a conservative GC hits during the
sof 2016/01/29 16:28:43 m_array will be empty, so there'll be a null point
34 return; 35 } else {
haraken 2016/01/29 16:00:32 Add: ASSERT(m_array->at(m_count - 1).isLastInAr
sof 2016/01/29 16:28:43 Done.
36 m_capacity += count;
37 m_array = ArrayType<T>::Allocator::resize(m_array.release(), m_capac ity);
38 m_array->at(m_count - 1).setLastInArray(false);
35 } 39 }
36 m_capacity += count; 40 m_array->at(m_capacity - 1).setLastInArray(true);
37 m_array = ArrayType<T>::Allocator::resize(m_array.release(), m_capacity) ;
38 m_array->at(m_count - 1).setLastInArray(false);
39 } 41 }
40 42
41 void append(const T& item) 43 void append(const T& item)
42 { 44 {
43 RELEASE_ASSERT(m_count < m_capacity); 45 RELEASE_ASSERT(m_count < m_capacity);
44 ASSERT(!item.isLastInArray()); 46 ASSERT(!item.isLastInArray());
45 m_array->at(m_count++) = item; 47 m_array->at(m_count++) = item;
48 if (m_count == m_capacity)
49 m_array->at(m_capacity - 1).setLastInArray(true);
46 } 50 }
47 51
48 typename ArrayType<T>::Allocator::PassPtr release() 52 typename ArrayType<T>::Allocator::PassPtr release()
49 { 53 {
50 RELEASE_ASSERT(m_count == m_capacity); 54 RELEASE_ASSERT(m_count == m_capacity);
51 if (m_array)
52 m_array->at(m_count - 1).setLastInArray(true);
53 assertValid(); 55 assertValid();
54 return m_array.release(); 56 return m_array.release();
55 } 57 }
56 58
57 private: 59 private:
58 #if ENABLE(ASSERT) 60 #if ENABLE(ASSERT)
59 void assertValid() 61 void assertValid()
60 { 62 {
61 for (size_t i = 0; i < m_count; ++i) { 63 for (size_t i = 0; i < m_count; ++i) {
62 bool isLastInArray = (i + 1 == m_count); 64 bool isLastInArray = (i + 1 == m_count);
63 ASSERT(m_array->at(i).isLastInArray() == isLastInArray); 65 ASSERT(m_array->at(i).isLastInArray() == isLastInArray);
64 } 66 }
65 } 67 }
66 #else 68 #else
67 void assertValid() { } 69 void assertValid() { }
68 #endif 70 #endif
69 71
70 typename ArrayType<T>::Allocator::Ptr m_array; 72 typename ArrayType<T>::Allocator::Ptr m_array;
71 size_t m_count; 73 size_t m_count;
72 size_t m_capacity; 74 size_t m_capacity;
73 }; 75 };
74 76
75 } // namespace WTF 77 } // namespace WTF
76 78
77 using WTF::TerminatedArrayBuilder; 79 using WTF::TerminatedArrayBuilder;
78 80
79 #endif // TerminatedArrayBuilder_h 81 #endif // TerminatedArrayBuilder_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/heap/HeapTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698