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

Side by Side Diff: runtime/platform/growable_array.h

Issue 2715463003: Add option to gen_snapshot for creating a Makefile describing a snapshot's dependencies. (Closed)
Patch Set: . Created 3 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 | « runtime/platform/allocation.h ('k') | runtime/platform/platform_headers.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, 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_PLATFORM_GROWABLE_ARRAY_H_
11 #define RUNTIME_VM_GROWABLE_ARRAY_H_ 11 #define RUNTIME_PLATFORM_GROWABLE_ARRAY_H_
12 12
13 #include "platform/allocation.h"
13 #include "platform/utils.h" 14 #include "platform/utils.h"
14 #include "vm/allocation.h"
15 #include "vm/isolate.h"
16 #include "vm/zone.h"
17 15
18 namespace dart { 16 namespace dart {
19 17
20 template <typename T, typename B, typename Allocator = Zone> 18 template <typename T, typename B, typename Allocator>
21 class BaseGrowableArray : public B { 19 class BaseGrowableArray : public B {
22 public: 20 public:
23 explicit BaseGrowableArray(Allocator* allocator) 21 explicit BaseGrowableArray(Allocator* allocator)
24 : length_(0), capacity_(0), data_(NULL), allocator_(allocator) {} 22 : length_(0), capacity_(0), data_(NULL), allocator_(allocator) {}
25 23
26 BaseGrowableArray(intptr_t initial_capacity, Allocator* allocator) 24 BaseGrowableArray(intptr_t initial_capacity, Allocator* allocator)
27 : length_(0), capacity_(0), data_(NULL), allocator_(allocator) { 25 : length_(0), capacity_(0), data_(NULL), allocator_(allocator) {
28 if (initial_capacity > 0) { 26 if (initial_capacity > 0) {
29 capacity_ = Utils::RoundUpToPowerOfTwo(initial_capacity); 27 capacity_ = Utils::RoundUpToPowerOfTwo(initial_capacity);
30 data_ = allocator_->template Alloc<T>(capacity_); 28 data_ = allocator_->template Alloc<T>(capacity_);
(...skipping 30 matching lines...) Expand all
61 return data_[index]; 59 return data_[index];
62 } 60 }
63 61
64 const T& At(intptr_t index) const { return operator[](index); } 62 const T& At(intptr_t index) const { return operator[](index); }
65 63
66 T& Last() const { 64 T& Last() const {
67 ASSERT(length_ > 0); 65 ASSERT(length_ > 0);
68 return operator[](length_ - 1); 66 return operator[](length_ - 1);
69 } 67 }
70 68
71 void AddArray(const BaseGrowableArray<T, B>& src) { 69 void AddArray(const BaseGrowableArray<T, B, Allocator>& src) {
72 for (intptr_t i = 0; i < src.length(); i++) { 70 for (intptr_t i = 0; i < src.length(); i++) {
73 Add(src[i]); 71 Add(src[i]);
74 } 72 }
75 } 73 }
76 74
77 void Clear() { length_ = 0; } 75 void Clear() { length_ = 0; }
78 76
79 void InsertAt(intptr_t idx, const T& value) { 77 void InsertAt(intptr_t idx, const T& value) {
80 Resize(length() + 1); 78 Resize(length() + 1);
81 for (intptr_t i = length_ - 2; i >= idx; i--) { 79 for (intptr_t i = length_ - 2; i >= idx; i--) {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 if (new_length > capacity_) { 159 if (new_length > capacity_) {
162 T* new_data = allocator_->template Alloc<T>(new_length); 160 T* new_data = allocator_->template Alloc<T>(new_length);
163 ASSERT(new_data != NULL); 161 ASSERT(new_data != NULL);
164 data_ = new_data; 162 data_ = new_data;
165 capacity_ = new_length; 163 capacity_ = new_length;
166 } 164 }
167 length_ = new_length; 165 length_ = new_length;
168 } 166 }
169 167
170 168
171 template <typename T>
172 class GrowableArray : public BaseGrowableArray<T, ValueObject> {
173 public:
174 GrowableArray(Zone* zone, intptr_t initial_capacity)
175 : BaseGrowableArray<T, ValueObject>(initial_capacity,
176 ASSERT_NOTNULL(zone)) {}
177 explicit GrowableArray(intptr_t initial_capacity)
178 : BaseGrowableArray<T, ValueObject>(
179 initial_capacity,
180 ASSERT_NOTNULL(Thread::Current()->zone())) {}
181 GrowableArray()
182 : BaseGrowableArray<T, ValueObject>(
183 ASSERT_NOTNULL(Thread::Current()->zone())) {}
184 };
185
186
187 template <typename T>
188 class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated> {
189 public:
190 ZoneGrowableArray(Zone* zone, intptr_t initial_capacity)
191 : BaseGrowableArray<T, ZoneAllocated>(initial_capacity,
192 ASSERT_NOTNULL(zone)) {}
193 explicit ZoneGrowableArray(intptr_t initial_capacity)
194 : BaseGrowableArray<T, ZoneAllocated>(
195 initial_capacity,
196 ASSERT_NOTNULL(Thread::Current()->zone())) {}
197 ZoneGrowableArray()
198 : BaseGrowableArray<T, ZoneAllocated>(
199 ASSERT_NOTNULL(Thread::Current()->zone())) {}
200 };
201
202
203 // T must be a Handle type.
204 template <typename T, typename B>
205 class BaseGrowableHandlePtrArray : public B {
206 public:
207 BaseGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity)
208 : zone_(zone), array_(zone, initial_capacity) {}
209
210 // Use unique zone handles to store objects.
211 void Add(const T& t) { array_.Add(&T::ZoneHandle(zone_, t.raw())); }
212
213 T& operator[](intptr_t index) const { return *array_[index]; }
214
215 const T& At(intptr_t index) const { return operator[](index); }
216
217 void SetAt(intptr_t index, const T& t) {
218 array_[index] = &T::ZoneHandle(zone_, t.raw());
219 }
220
221 intptr_t length() const { return array_.length(); }
222
223 const GrowableArray<T*>& growable_array() const { return array_; }
224
225 private:
226 Zone* zone_;
227 GrowableArray<T*> array_;
228
229 DISALLOW_COPY_AND_ASSIGN(BaseGrowableHandlePtrArray);
230 };
231
232
233 template <typename T>
234 class GrowableHandlePtrArray
235 : public BaseGrowableHandlePtrArray<T, ValueObject> {
236 public:
237 GrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity)
238 : BaseGrowableHandlePtrArray<T, ValueObject>(zone, initial_capacity) {}
239 };
240
241
242 template <typename T>
243 class ZoneGrowableHandlePtrArray
244 : public BaseGrowableHandlePtrArray<T, ZoneAllocated> {
245 public:
246 ZoneGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity)
247 : BaseGrowableHandlePtrArray<T, ZoneAllocated>(zone, initial_capacity) {}
248 };
249
250
251 class Malloc : public AllStatic { 169 class Malloc : public AllStatic {
252 public: 170 public:
253 template <class T> 171 template <class T>
254 static inline T* Alloc(intptr_t len) { 172 static inline T* Alloc(intptr_t len) {
255 return reinterpret_cast<T*>(malloc(len * sizeof(T))); 173 return reinterpret_cast<T*>(malloc(len * sizeof(T)));
256 } 174 }
257 175
258 template <class T> 176 template <class T>
259 static inline T* Realloc(T* old_array, intptr_t old_len, intptr_t new_len) { 177 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))); 178 return reinterpret_cast<T*>(realloc(old_array, new_len * sizeof(T)));
(...skipping 12 matching lines...) Expand all
273 template <typename T> 191 template <typename T>
274 class MallocGrowableArray : public BaseGrowableArray<T, EmptyBase, Malloc> { 192 class MallocGrowableArray : public BaseGrowableArray<T, EmptyBase, Malloc> {
275 public: 193 public:
276 explicit MallocGrowableArray(intptr_t initial_capacity) 194 explicit MallocGrowableArray(intptr_t initial_capacity)
277 : BaseGrowableArray<T, EmptyBase, Malloc>(initial_capacity, NULL) {} 195 : BaseGrowableArray<T, EmptyBase, Malloc>(initial_capacity, NULL) {}
278 MallocGrowableArray() : BaseGrowableArray<T, EmptyBase, Malloc>(NULL) {} 196 MallocGrowableArray() : BaseGrowableArray<T, EmptyBase, Malloc>(NULL) {}
279 }; 197 };
280 198
281 } // namespace dart 199 } // namespace dart
282 200
283 #endif // RUNTIME_VM_GROWABLE_ARRAY_H_ 201 #endif // RUNTIME_PLATFORM_GROWABLE_ARRAY_H_
OLDNEW
« no previous file with comments | « runtime/platform/allocation.h ('k') | runtime/platform/platform_headers.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698