OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Defines growable array classes, that differ where they are allocated: | 4 // Defines growable array classes, that differ where they are allocated: |
5 // - GrowableArray: allocated on stack. | 5 // - GrowableArray: allocated on stack. |
6 // - ZoneGrowableArray: allocated in the zone. | 6 // - ZoneGrowableArray: allocated in the zone. |
7 // - MallocGrowableArray: allocates using malloc/realloc; free is only called | 7 // - MallocGrowableArray: allocates using malloc/realloc; free is only called |
8 // at destruction. | 8 // at destruction. |
9 | 9 |
10 #ifndef RUNTIME_VM_GROWABLE_ARRAY_H_ | 10 #ifndef RUNTIME_VM_GROWABLE_ARRAY_H_ |
11 #define RUNTIME_VM_GROWABLE_ARRAY_H_ | 11 #define RUNTIME_VM_GROWABLE_ARRAY_H_ |
12 | 12 |
13 #include "platform/utils.h" | 13 #include "platform/growable_array.h" |
14 #include "vm/allocation.h" | 14 #include "vm/thread.h" |
15 #include "vm/isolate.h" | |
16 #include "vm/zone.h" | 15 #include "vm/zone.h" |
17 | 16 |
18 namespace dart { | 17 namespace dart { |
19 | 18 |
20 template <typename T, typename B, typename Allocator = Zone> | |
21 class BaseGrowableArray : public B { | |
22 public: | |
23 explicit BaseGrowableArray(Allocator* allocator) | |
24 : length_(0), capacity_(0), data_(NULL), allocator_(allocator) {} | |
25 | |
26 BaseGrowableArray(intptr_t initial_capacity, Allocator* allocator) | |
27 : length_(0), capacity_(0), data_(NULL), allocator_(allocator) { | |
28 if (initial_capacity > 0) { | |
29 capacity_ = Utils::RoundUpToPowerOfTwo(initial_capacity); | |
30 data_ = allocator_->template Alloc<T>(capacity_); | |
31 } | |
32 } | |
33 | |
34 ~BaseGrowableArray() { allocator_->template Free<T>(data_, capacity_); } | |
35 | |
36 intptr_t length() const { return length_; } | |
37 T* data() const { return data_; } | |
38 bool is_empty() const { return length_ == 0; } | |
39 | |
40 void TruncateTo(intptr_t length) { | |
41 ASSERT(length_ >= length); | |
42 length_ = length; | |
43 } | |
44 | |
45 void Add(const T& value) { | |
46 Resize(length() + 1); | |
47 Last() = value; | |
48 } | |
49 | |
50 T& RemoveLast() { | |
51 ASSERT(length_ > 0); | |
52 T& result = operator[](length_ - 1); | |
53 length_--; | |
54 return result; | |
55 } | |
56 | |
57 T& operator[](intptr_t index) const { | |
58 ASSERT(0 <= index); | |
59 ASSERT(index < length_); | |
60 ASSERT(length_ <= capacity_); | |
61 return data_[index]; | |
62 } | |
63 | |
64 const T& At(intptr_t index) const { return operator[](index); } | |
65 | |
66 T& Last() const { | |
67 ASSERT(length_ > 0); | |
68 return operator[](length_ - 1); | |
69 } | |
70 | |
71 void AddArray(const BaseGrowableArray<T, B>& src) { | |
72 for (intptr_t i = 0; i < src.length(); i++) { | |
73 Add(src[i]); | |
74 } | |
75 } | |
76 | |
77 void Clear() { length_ = 0; } | |
78 | |
79 void InsertAt(intptr_t idx, const T& value) { | |
80 Resize(length() + 1); | |
81 for (intptr_t i = length_ - 2; i >= idx; i--) { | |
82 data_[i + 1] = data_[i]; | |
83 } | |
84 data_[idx] = value; | |
85 } | |
86 | |
87 void Reverse() { | |
88 for (intptr_t i = 0; i < length_ / 2; i++) { | |
89 const intptr_t j = length_ - 1 - i; | |
90 T temp = data_[i]; | |
91 data_[i] = data_[j]; | |
92 data_[j] = temp; | |
93 } | |
94 } | |
95 | |
96 // Swap entries |i| and |j|. | |
97 void Swap(intptr_t i, intptr_t j) { | |
98 ASSERT(i >= 0); | |
99 ASSERT(j >= 0); | |
100 ASSERT(i < length_); | |
101 ASSERT(j < length_); | |
102 T temp = data_[i]; | |
103 data_[i] = data_[j]; | |
104 data_[j] = temp; | |
105 } | |
106 | |
107 // NOTE: Does not preserve array order. | |
108 void RemoveAt(intptr_t i) { | |
109 ASSERT(i >= 0); | |
110 ASSERT(i < length_); | |
111 intptr_t last = length_ - 1; | |
112 if (i < last) { | |
113 Swap(i, last); | |
114 } | |
115 RemoveLast(); | |
116 } | |
117 | |
118 // The content is uninitialized after calling it. | |
119 void SetLength(intptr_t new_length); | |
120 | |
121 // Sort the array in place. | |
122 inline void Sort(int compare(const T*, const T*)); | |
123 | |
124 private: | |
125 intptr_t length_; | |
126 intptr_t capacity_; | |
127 T* data_; | |
128 Allocator* allocator_; // Used to (re)allocate the array. | |
129 | |
130 // Used for growing the array. | |
131 void Resize(intptr_t new_length); | |
132 | |
133 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray); | |
134 }; | |
135 | |
136 | |
137 template <typename T, typename B, typename Allocator> | |
138 inline void BaseGrowableArray<T, B, Allocator>::Sort(int compare(const T*, | |
139 const T*)) { | |
140 typedef int (*CompareFunction)(const void*, const void*); | |
141 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare)); | |
142 } | |
143 | |
144 | |
145 template <typename T, typename B, typename Allocator> | |
146 void BaseGrowableArray<T, B, Allocator>::Resize(intptr_t new_length) { | |
147 if (new_length > capacity_) { | |
148 intptr_t new_capacity = Utils::RoundUpToPowerOfTwo(new_length); | |
149 T* new_data = | |
150 allocator_->template Realloc<T>(data_, capacity_, new_capacity); | |
151 ASSERT(new_data != NULL); | |
152 data_ = new_data; | |
153 capacity_ = new_capacity; | |
154 } | |
155 length_ = new_length; | |
156 } | |
157 | |
158 | |
159 template <typename T, typename B, typename Allocator> | |
160 void BaseGrowableArray<T, B, Allocator>::SetLength(intptr_t new_length) { | |
161 if (new_length > capacity_) { | |
162 T* new_data = allocator_->template Alloc<T>(new_length); | |
163 ASSERT(new_data != NULL); | |
164 data_ = new_data; | |
165 capacity_ = new_length; | |
166 } | |
167 length_ = new_length; | |
168 } | |
169 | |
170 | |
171 template <typename T> | 19 template <typename T> |
172 class GrowableArray : public BaseGrowableArray<T, ValueObject> { | 20 class GrowableArray : public BaseGrowableArray<T, ValueObject, Zone> { |
173 public: | 21 public: |
174 GrowableArray(Zone* zone, intptr_t initial_capacity) | 22 GrowableArray(Zone* zone, intptr_t initial_capacity) |
175 : BaseGrowableArray<T, ValueObject>(initial_capacity, | 23 : BaseGrowableArray<T, ValueObject, Zone>(initial_capacity, |
176 ASSERT_NOTNULL(zone)) {} | 24 ASSERT_NOTNULL(zone)) {} |
177 explicit GrowableArray(intptr_t initial_capacity) | 25 explicit GrowableArray(intptr_t initial_capacity) |
178 : BaseGrowableArray<T, ValueObject>( | 26 : BaseGrowableArray<T, ValueObject, Zone>( |
179 initial_capacity, | 27 initial_capacity, |
180 ASSERT_NOTNULL(Thread::Current()->zone())) {} | 28 ASSERT_NOTNULL(Thread::Current()->zone())) {} |
181 GrowableArray() | 29 GrowableArray() |
182 : BaseGrowableArray<T, ValueObject>( | 30 : BaseGrowableArray<T, ValueObject, Zone>( |
183 ASSERT_NOTNULL(Thread::Current()->zone())) {} | 31 ASSERT_NOTNULL(Thread::Current()->zone())) {} |
184 }; | 32 }; |
185 | 33 |
186 | 34 |
187 template <typename T> | 35 template <typename T> |
188 class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated> { | 36 class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated, Zone> { |
189 public: | 37 public: |
190 ZoneGrowableArray(Zone* zone, intptr_t initial_capacity) | 38 ZoneGrowableArray(Zone* zone, intptr_t initial_capacity) |
191 : BaseGrowableArray<T, ZoneAllocated>(initial_capacity, | 39 : BaseGrowableArray<T, ZoneAllocated, Zone>(initial_capacity, |
192 ASSERT_NOTNULL(zone)) {} | 40 ASSERT_NOTNULL(zone)) {} |
193 explicit ZoneGrowableArray(intptr_t initial_capacity) | 41 explicit ZoneGrowableArray(intptr_t initial_capacity) |
194 : BaseGrowableArray<T, ZoneAllocated>( | 42 : BaseGrowableArray<T, ZoneAllocated, Zone>( |
195 initial_capacity, | 43 initial_capacity, |
196 ASSERT_NOTNULL(Thread::Current()->zone())) {} | 44 ASSERT_NOTNULL(Thread::Current()->zone())) {} |
197 ZoneGrowableArray() | 45 ZoneGrowableArray() |
198 : BaseGrowableArray<T, ZoneAllocated>( | 46 : BaseGrowableArray<T, ZoneAllocated, Zone>( |
199 ASSERT_NOTNULL(Thread::Current()->zone())) {} | 47 ASSERT_NOTNULL(Thread::Current()->zone())) {} |
200 }; | 48 }; |
201 | 49 |
202 | 50 |
203 // T must be a Handle type. | 51 // T must be a Handle type. |
204 template <typename T, typename B> | 52 template <typename T, typename B> |
205 class BaseGrowableHandlePtrArray : public B { | 53 class BaseGrowableHandlePtrArray : public B { |
206 public: | 54 public: |
207 BaseGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) | 55 BaseGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) |
208 : zone_(zone), array_(zone, initial_capacity) {} | 56 : zone_(zone), array_(zone, initial_capacity) {} |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 | 88 |
241 | 89 |
242 template <typename T> | 90 template <typename T> |
243 class ZoneGrowableHandlePtrArray | 91 class ZoneGrowableHandlePtrArray |
244 : public BaseGrowableHandlePtrArray<T, ZoneAllocated> { | 92 : public BaseGrowableHandlePtrArray<T, ZoneAllocated> { |
245 public: | 93 public: |
246 ZoneGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) | 94 ZoneGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) |
247 : BaseGrowableHandlePtrArray<T, ZoneAllocated>(zone, initial_capacity) {} | 95 : BaseGrowableHandlePtrArray<T, ZoneAllocated>(zone, initial_capacity) {} |
248 }; | 96 }; |
249 | 97 |
250 | |
251 class Malloc : public AllStatic { | |
252 public: | |
253 template <class T> | |
254 static inline T* Alloc(intptr_t len) { | |
255 return reinterpret_cast<T*>(malloc(len * sizeof(T))); | |
256 } | |
257 | |
258 template <class T> | |
259 static inline T* Realloc(T* old_array, intptr_t old_len, intptr_t new_len) { | |
260 return reinterpret_cast<T*>(realloc(old_array, new_len * sizeof(T))); | |
261 } | |
262 | |
263 template <class T> | |
264 static inline void Free(T* old_array, intptr_t old_len) { | |
265 free(old_array); | |
266 } | |
267 }; | |
268 | |
269 | |
270 class EmptyBase {}; | |
271 | |
272 | |
273 template <typename T> | |
274 class MallocGrowableArray : public BaseGrowableArray<T, EmptyBase, Malloc> { | |
275 public: | |
276 explicit MallocGrowableArray(intptr_t initial_capacity) | |
277 : BaseGrowableArray<T, EmptyBase, Malloc>(initial_capacity, NULL) {} | |
278 MallocGrowableArray() : BaseGrowableArray<T, EmptyBase, Malloc>(NULL) {} | |
279 }; | |
280 | |
281 } // namespace dart | 98 } // namespace dart |
282 | 99 |
283 #endif // RUNTIME_VM_GROWABLE_ARRAY_H_ | 100 #endif // RUNTIME_VM_GROWABLE_ARRAY_H_ |
OLD | NEW |