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/utils.h" |
14 #include "vm/allocation.h" | 14 #include "vm/allocation.h" |
15 #include "vm/isolate.h" | 15 #include "vm/isolate.h" |
16 #include "vm/zone.h" | 16 #include "vm/zone.h" |
17 | 17 |
18 namespace dart { | 18 namespace dart { |
19 | 19 |
20 template<typename T, typename B, typename Allocator = Zone> | 20 template <typename T, typename B, typename Allocator = Zone> |
21 class BaseGrowableArray : public B { | 21 class BaseGrowableArray : public B { |
22 public: | 22 public: |
23 explicit BaseGrowableArray(Allocator* allocator) | 23 explicit BaseGrowableArray(Allocator* allocator) |
24 : length_(0), capacity_(0), data_(NULL), allocator_(allocator) {} | 24 : length_(0), capacity_(0), data_(NULL), allocator_(allocator) {} |
25 | 25 |
26 BaseGrowableArray(intptr_t initial_capacity, Allocator* allocator) | 26 BaseGrowableArray(intptr_t initial_capacity, Allocator* allocator) |
27 : length_(0), capacity_(0), data_(NULL), allocator_(allocator) { | 27 : length_(0), capacity_(0), data_(NULL), allocator_(allocator) { |
28 if (initial_capacity > 0) { | 28 if (initial_capacity > 0) { |
29 capacity_ = Utils::RoundUpToPowerOfTwo(initial_capacity); | 29 capacity_ = Utils::RoundUpToPowerOfTwo(initial_capacity); |
30 data_ = allocator_->template Alloc<T>(capacity_); | 30 data_ = allocator_->template Alloc<T>(capacity_); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 ~BaseGrowableArray() { | 34 ~BaseGrowableArray() { allocator_->template Free<T>(data_, capacity_); } |
35 allocator_->template Free<T>(data_, capacity_); | |
36 } | |
37 | 35 |
38 intptr_t length() const { return length_; } | 36 intptr_t length() const { return length_; } |
39 T* data() const { return data_; } | 37 T* data() const { return data_; } |
40 bool is_empty() const { return length_ == 0; } | 38 bool is_empty() const { return length_ == 0; } |
41 | 39 |
42 void TruncateTo(intptr_t length) { | 40 void TruncateTo(intptr_t length) { |
43 ASSERT(length_ >= length); | 41 ASSERT(length_ >= length); |
44 length_ = length; | 42 length_ = length; |
45 } | 43 } |
46 | 44 |
47 void Add(const T& value) { | 45 void Add(const T& value) { |
48 Resize(length() + 1); | 46 Resize(length() + 1); |
49 Last() = value; | 47 Last() = value; |
50 } | 48 } |
51 | 49 |
52 T& RemoveLast() { | 50 T& RemoveLast() { |
53 ASSERT(length_ > 0); | 51 ASSERT(length_ > 0); |
54 T& result = operator[](length_ - 1); | 52 T& result = operator[](length_ - 1); |
55 length_--; | 53 length_--; |
56 return result; | 54 return result; |
57 } | 55 } |
58 | 56 |
59 T& operator[](intptr_t index) const { | 57 T& operator[](intptr_t index) const { |
60 ASSERT(0 <= index); | 58 ASSERT(0 <= index); |
61 ASSERT(index < length_); | 59 ASSERT(index < length_); |
62 ASSERT(length_ <= capacity_); | 60 ASSERT(length_ <= capacity_); |
63 return data_[index]; | 61 return data_[index]; |
64 } | 62 } |
65 | 63 |
66 const T& At(intptr_t index) const { | 64 const T& At(intptr_t index) const { return operator[](index); } |
67 return operator[](index); | |
68 } | |
69 | 65 |
70 T& Last() const { | 66 T& Last() const { |
71 ASSERT(length_ > 0); | 67 ASSERT(length_ > 0); |
72 return operator[](length_ - 1); | 68 return operator[](length_ - 1); |
73 } | 69 } |
74 | 70 |
75 void AddArray(const BaseGrowableArray<T, B>& src) { | 71 void AddArray(const BaseGrowableArray<T, B>& src) { |
76 for (intptr_t i = 0; i < src.length(); i++) { | 72 for (intptr_t i = 0; i < src.length(); i++) { |
77 Add(src[i]); | 73 Add(src[i]); |
78 } | 74 } |
79 } | 75 } |
80 | 76 |
81 void Clear() { | 77 void Clear() { length_ = 0; } |
82 length_ = 0; | |
83 } | |
84 | 78 |
85 void InsertAt(intptr_t idx, const T& value) { | 79 void InsertAt(intptr_t idx, const T& value) { |
86 Resize(length() + 1); | 80 Resize(length() + 1); |
87 for (intptr_t i = length_ - 2; i >= idx; i--) { | 81 for (intptr_t i = length_ - 2; i >= idx; i--) { |
88 data_[i + 1] = data_[i]; | 82 data_[i + 1] = data_[i]; |
89 } | 83 } |
90 data_[idx] = value; | 84 data_[idx] = value; |
91 } | 85 } |
92 | 86 |
93 void Reverse() { | 87 void Reverse() { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 T* data_; | 127 T* data_; |
134 Allocator* allocator_; // Used to (re)allocate the array. | 128 Allocator* allocator_; // Used to (re)allocate the array. |
135 | 129 |
136 // Used for growing the array. | 130 // Used for growing the array. |
137 void Resize(intptr_t new_length); | 131 void Resize(intptr_t new_length); |
138 | 132 |
139 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray); | 133 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray); |
140 }; | 134 }; |
141 | 135 |
142 | 136 |
143 template<typename T, typename B, typename Allocator> | 137 template <typename T, typename B, typename Allocator> |
144 inline void BaseGrowableArray<T, B, Allocator>::Sort( | 138 inline void BaseGrowableArray<T, B, Allocator>::Sort(int compare(const T*, |
145 int compare(const T*, const T*)) { | 139 const T*)) { |
146 typedef int (*CompareFunction)(const void*, const void*); | 140 typedef int (*CompareFunction)(const void*, const void*); |
147 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare)); | 141 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare)); |
148 } | 142 } |
149 | 143 |
150 | 144 |
151 template<typename T, typename B, typename Allocator> | 145 template <typename T, typename B, typename Allocator> |
152 void BaseGrowableArray<T, B, Allocator>::Resize(intptr_t new_length) { | 146 void BaseGrowableArray<T, B, Allocator>::Resize(intptr_t new_length) { |
153 if (new_length > capacity_) { | 147 if (new_length > capacity_) { |
154 intptr_t new_capacity = Utils::RoundUpToPowerOfTwo(new_length); | 148 intptr_t new_capacity = Utils::RoundUpToPowerOfTwo(new_length); |
155 T* new_data = | 149 T* new_data = |
156 allocator_->template Realloc<T>(data_, capacity_, new_capacity); | 150 allocator_->template Realloc<T>(data_, capacity_, new_capacity); |
157 ASSERT(new_data != NULL); | 151 ASSERT(new_data != NULL); |
158 data_ = new_data; | 152 data_ = new_data; |
159 capacity_ = new_capacity; | 153 capacity_ = new_capacity; |
160 } | 154 } |
161 length_ = new_length; | 155 length_ = new_length; |
162 } | 156 } |
163 | 157 |
164 | 158 |
165 template<typename T, typename B, typename Allocator> | 159 template <typename T, typename B, typename Allocator> |
166 void BaseGrowableArray<T, B, Allocator>::SetLength(intptr_t new_length) { | 160 void BaseGrowableArray<T, B, Allocator>::SetLength(intptr_t new_length) { |
167 if (new_length > capacity_) { | 161 if (new_length > capacity_) { |
168 T* new_data = allocator_->template Alloc<T>(new_length); | 162 T* new_data = allocator_->template Alloc<T>(new_length); |
169 ASSERT(new_data != NULL); | 163 ASSERT(new_data != NULL); |
170 data_ = new_data; | 164 data_ = new_data; |
171 capacity_ = new_length; | 165 capacity_ = new_length; |
172 } | 166 } |
173 length_ = new_length; | 167 length_ = new_length; |
174 } | 168 } |
175 | 169 |
176 | 170 |
177 template<typename T> | 171 template <typename T> |
178 class GrowableArray : public BaseGrowableArray<T, ValueObject> { | 172 class GrowableArray : public BaseGrowableArray<T, ValueObject> { |
179 public: | 173 public: |
180 GrowableArray(Zone* zone, intptr_t initial_capacity) | 174 GrowableArray(Zone* zone, intptr_t initial_capacity) |
181 : BaseGrowableArray<T, ValueObject>( | 175 : BaseGrowableArray<T, ValueObject>(initial_capacity, |
182 initial_capacity, ASSERT_NOTNULL(zone)) {} | 176 ASSERT_NOTNULL(zone)) {} |
183 explicit GrowableArray(intptr_t initial_capacity) | 177 explicit GrowableArray(intptr_t initial_capacity) |
184 : BaseGrowableArray<T, ValueObject>( | 178 : BaseGrowableArray<T, ValueObject>( |
185 initial_capacity, | 179 initial_capacity, |
186 ASSERT_NOTNULL(Thread::Current()->zone())) {} | 180 ASSERT_NOTNULL(Thread::Current()->zone())) {} |
187 GrowableArray() | 181 GrowableArray() |
188 : BaseGrowableArray<T, ValueObject>( | 182 : BaseGrowableArray<T, ValueObject>( |
189 ASSERT_NOTNULL(Thread::Current()->zone())) {} | 183 ASSERT_NOTNULL(Thread::Current()->zone())) {} |
190 }; | 184 }; |
191 | 185 |
192 | 186 |
193 template<typename T> | 187 template <typename T> |
194 class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated> { | 188 class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated> { |
195 public: | 189 public: |
196 ZoneGrowableArray(Zone* zone, intptr_t initial_capacity) | 190 ZoneGrowableArray(Zone* zone, intptr_t initial_capacity) |
197 : BaseGrowableArray<T, ZoneAllocated>( | 191 : BaseGrowableArray<T, ZoneAllocated>(initial_capacity, |
198 initial_capacity, ASSERT_NOTNULL(zone)) {} | 192 ASSERT_NOTNULL(zone)) {} |
199 explicit ZoneGrowableArray(intptr_t initial_capacity) | 193 explicit ZoneGrowableArray(intptr_t initial_capacity) |
200 : BaseGrowableArray<T, ZoneAllocated>( | 194 : BaseGrowableArray<T, ZoneAllocated>( |
201 initial_capacity, | 195 initial_capacity, |
202 ASSERT_NOTNULL(Thread::Current()->zone())) {} | 196 ASSERT_NOTNULL(Thread::Current()->zone())) {} |
203 ZoneGrowableArray() | 197 ZoneGrowableArray() |
204 : BaseGrowableArray<T, ZoneAllocated>( | 198 : BaseGrowableArray<T, ZoneAllocated>( |
205 ASSERT_NOTNULL(Thread::Current()->zone())) {} | 199 ASSERT_NOTNULL(Thread::Current()->zone())) {} |
206 }; | 200 }; |
207 | 201 |
208 | 202 |
209 // T must be a Handle type. | 203 // T must be a Handle type. |
210 template<typename T, typename B> | 204 template <typename T, typename B> |
211 class BaseGrowableHandlePtrArray : public B { | 205 class BaseGrowableHandlePtrArray : public B { |
212 public: | 206 public: |
213 BaseGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) | 207 BaseGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) |
214 : zone_(zone), array_(zone, initial_capacity) {} | 208 : zone_(zone), array_(zone, initial_capacity) {} |
215 | 209 |
216 // Use unique zone handles to store objects. | 210 // Use unique zone handles to store objects. |
217 void Add(const T& t) { | 211 void Add(const T& t) { array_.Add(&T::ZoneHandle(zone_, t.raw())); } |
218 array_.Add(&T::ZoneHandle(zone_, t.raw())); | |
219 } | |
220 | 212 |
221 T& operator[](intptr_t index) const { | 213 T& operator[](intptr_t index) const { return *array_[index]; } |
222 return *array_[index]; | |
223 } | |
224 | 214 |
225 const T& At(intptr_t index) const { | 215 const T& At(intptr_t index) const { return operator[](index); } |
226 return operator[](index); | |
227 } | |
228 | 216 |
229 void SetAt(intptr_t index, const T& t) { | 217 void SetAt(intptr_t index, const T& t) { |
230 array_[index] = &T::ZoneHandle(zone_, t.raw()); | 218 array_[index] = &T::ZoneHandle(zone_, t.raw()); |
231 } | 219 } |
232 | 220 |
233 intptr_t length() const { return array_.length(); } | 221 intptr_t length() const { return array_.length(); } |
234 | 222 |
235 const GrowableArray<T*>& growable_array() const { return array_; } | 223 const GrowableArray<T*>& growable_array() const { return array_; } |
236 | 224 |
237 private: | 225 private: |
238 Zone* zone_; | 226 Zone* zone_; |
239 GrowableArray<T*> array_; | 227 GrowableArray<T*> array_; |
240 | 228 |
241 DISALLOW_COPY_AND_ASSIGN(BaseGrowableHandlePtrArray); | 229 DISALLOW_COPY_AND_ASSIGN(BaseGrowableHandlePtrArray); |
242 }; | 230 }; |
243 | 231 |
244 | 232 |
245 template<typename T> | 233 template <typename T> |
246 class GrowableHandlePtrArray : | 234 class GrowableHandlePtrArray |
247 public BaseGrowableHandlePtrArray<T, ValueObject> { | 235 : public BaseGrowableHandlePtrArray<T, ValueObject> { |
248 public: | 236 public: |
249 GrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) | 237 GrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) |
250 : BaseGrowableHandlePtrArray<T, ValueObject>(zone, initial_capacity) {} | 238 : BaseGrowableHandlePtrArray<T, ValueObject>(zone, initial_capacity) {} |
251 }; | 239 }; |
252 | 240 |
253 | 241 |
254 template<typename T> | 242 template <typename T> |
255 class ZoneGrowableHandlePtrArray : | 243 class ZoneGrowableHandlePtrArray |
256 public BaseGrowableHandlePtrArray<T, ZoneAllocated> { | 244 : public BaseGrowableHandlePtrArray<T, ZoneAllocated> { |
257 public: | 245 public: |
258 ZoneGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) | 246 ZoneGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) |
259 : BaseGrowableHandlePtrArray<T, ZoneAllocated>(zone, initial_capacity) {} | 247 : BaseGrowableHandlePtrArray<T, ZoneAllocated>(zone, initial_capacity) {} |
260 }; | 248 }; |
261 | 249 |
262 | 250 |
263 | |
264 class Malloc : public AllStatic { | 251 class Malloc : public AllStatic { |
265 public: | 252 public: |
266 template <class T> | 253 template <class T> |
267 static inline T* Alloc(intptr_t len) { | 254 static inline T* Alloc(intptr_t len) { |
268 return reinterpret_cast<T*>(malloc(len * sizeof(T))); | 255 return reinterpret_cast<T*>(malloc(len * sizeof(T))); |
269 } | 256 } |
270 | 257 |
271 template <class T> | 258 template <class T> |
272 static inline T* Realloc(T* old_array, intptr_t old_len, intptr_t new_len) { | 259 static inline T* Realloc(T* old_array, intptr_t old_len, intptr_t new_len) { |
273 return reinterpret_cast<T*>(realloc(old_array, new_len * sizeof(T))); | 260 return reinterpret_cast<T*>(realloc(old_array, new_len * sizeof(T))); |
274 } | 261 } |
275 | 262 |
276 template <class T> | 263 template <class T> |
277 static inline void Free(T* old_array, intptr_t old_len) { | 264 static inline void Free(T* old_array, intptr_t old_len) { |
278 free(old_array); | 265 free(old_array); |
279 } | 266 } |
280 }; | 267 }; |
281 | 268 |
282 | 269 |
283 class EmptyBase {}; | 270 class EmptyBase {}; |
284 | 271 |
285 | 272 |
286 template<typename T> | 273 template <typename T> |
287 class MallocGrowableArray : public BaseGrowableArray<T, EmptyBase, Malloc> { | 274 class MallocGrowableArray : public BaseGrowableArray<T, EmptyBase, Malloc> { |
288 public: | 275 public: |
289 explicit MallocGrowableArray(intptr_t initial_capacity) | 276 explicit MallocGrowableArray(intptr_t initial_capacity) |
290 : BaseGrowableArray<T, EmptyBase, Malloc>(initial_capacity, NULL) {} | 277 : BaseGrowableArray<T, EmptyBase, Malloc>(initial_capacity, NULL) {} |
291 MallocGrowableArray() | 278 MallocGrowableArray() : BaseGrowableArray<T, EmptyBase, Malloc>(NULL) {} |
292 : BaseGrowableArray<T, EmptyBase, Malloc>(NULL) {} | |
293 }; | 279 }; |
294 | 280 |
295 } // namespace dart | 281 } // namespace dart |
296 | 282 |
297 #endif // RUNTIME_VM_GROWABLE_ARRAY_H_ | 283 #endif // RUNTIME_VM_GROWABLE_ARRAY_H_ |
OLD | NEW |