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

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

Issue 1312833009: Added {Zone}GrowableHandlePtrArray types: simplifies code, makes code safer (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 3 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/vm/class_finalizer.cc ('k') | runtime/vm/growable_array_test.cc » ('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) 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 VM_GROWABLE_ARRAY_H_ 10 #ifndef VM_GROWABLE_ARRAY_H_
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 explicit ZoneGrowableArray(intptr_t initial_capacity) 188 explicit ZoneGrowableArray(intptr_t initial_capacity)
189 : BaseGrowableArray<T, ZoneAllocated>( 189 : BaseGrowableArray<T, ZoneAllocated>(
190 initial_capacity, 190 initial_capacity,
191 ASSERT_NOTNULL(Thread::Current()->zone())) {} 191 ASSERT_NOTNULL(Thread::Current()->zone())) {}
192 ZoneGrowableArray() 192 ZoneGrowableArray()
193 : BaseGrowableArray<T, ZoneAllocated>( 193 : BaseGrowableArray<T, ZoneAllocated>(
194 ASSERT_NOTNULL(Thread::Current()->zone())) {} 194 ASSERT_NOTNULL(Thread::Current()->zone())) {}
195 }; 195 };
196 196
197 197
198 // T must be a Handle type.
199 template<typename T, typename B>
200 class BaseGrowableHandlePtrArray : public B {
201 public:
202 BaseGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity)
203 : zone_(zone), array_(zone, initial_capacity) {}
204
205 // Use unique zone handles to store objects.
Florian Schneider 2015/08/31 07:02:59 Why do the handles need to be unique? I think you
srdjan 2015/08/31 14:52:40 Often, e.g., in a loop, we reuse the handles. This
206 void Add(const T& t) {
207 array_.Add(&T::ZoneHandle(zone_, t.raw()));
208 }
209
210 T& operator[](intptr_t index) const {
211 return *array_[index];
212 }
213
214 const T& At(intptr_t index) const {
215 return operator[](index);
216 }
217
218 intptr_t length() const { return array_.length(); }
219
220 const GrowableArray<T*>& growable_array() const { return array_; }
221
222 private:
223 Zone* zone_;
224 GrowableArray<T*> array_;
225
226 DISALLOW_COPY_AND_ASSIGN(BaseGrowableHandlePtrArray);
227 };
228
229
230 template<typename T>
231 class GrowableHandlePtrArray :
232 public BaseGrowableHandlePtrArray<T, ValueObject> {
233 public:
234 GrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity)
235 : BaseGrowableHandlePtrArray<T, ValueObject>(zone, initial_capacity) {}
236 };
237
238
239 template<typename T>
240 class ZoneGrowableHandlePtrArray :
241 public BaseGrowableHandlePtrArray<T, ZoneAllocated> {
242 public:
243 ZoneGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity)
244 : BaseGrowableHandlePtrArray<T, ZoneAllocated>(zone, initial_capacity) {}
245 };
246
247
248
198 class Malloc : public AllStatic { 249 class Malloc : public AllStatic {
199 public: 250 public:
200 template <class T> 251 template <class T>
201 static inline T* Alloc(intptr_t len) { 252 static inline T* Alloc(intptr_t len) {
202 return reinterpret_cast<T*>(malloc(len * sizeof(T))); 253 return reinterpret_cast<T*>(malloc(len * sizeof(T)));
203 } 254 }
204 255
205 template <class T> 256 template <class T>
206 static inline T* Realloc(T* old_array, intptr_t old_len, intptr_t new_len) { 257 static inline T* Realloc(T* old_array, intptr_t old_len, intptr_t new_len) {
207 return reinterpret_cast<T*>(realloc(old_array, new_len * sizeof(T))); 258 return reinterpret_cast<T*>(realloc(old_array, new_len * sizeof(T)));
(...skipping 14 matching lines...) Expand all
222 public: 273 public:
223 explicit MallocGrowableArray(intptr_t initial_capacity) 274 explicit MallocGrowableArray(intptr_t initial_capacity)
224 : BaseGrowableArray<T, EmptyBase, Malloc>(initial_capacity, NULL) {} 275 : BaseGrowableArray<T, EmptyBase, Malloc>(initial_capacity, NULL) {}
225 MallocGrowableArray() 276 MallocGrowableArray()
226 : BaseGrowableArray<T, EmptyBase, Malloc>(NULL) {} 277 : BaseGrowableArray<T, EmptyBase, Malloc>(NULL) {}
227 }; 278 };
228 279
229 } // namespace dart 280 } // namespace dart
230 281
231 #endif // VM_GROWABLE_ARRAY_H_ 282 #endif // VM_GROWABLE_ARRAY_H_
OLDNEW
« no previous file with comments | « runtime/vm/class_finalizer.cc ('k') | runtime/vm/growable_array_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698