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_ |
(...skipping 13 matching lines...) Expand all Loading... |
24 ASSERT_NOTNULL(zone)) {} | 24 ASSERT_NOTNULL(zone)) {} |
25 explicit GrowableArray(intptr_t initial_capacity) | 25 explicit GrowableArray(intptr_t initial_capacity) |
26 : BaseGrowableArray<T, ValueObject, Zone>( | 26 : BaseGrowableArray<T, ValueObject, Zone>( |
27 initial_capacity, | 27 initial_capacity, |
28 ASSERT_NOTNULL(Thread::Current()->zone())) {} | 28 ASSERT_NOTNULL(Thread::Current()->zone())) {} |
29 GrowableArray() | 29 GrowableArray() |
30 : BaseGrowableArray<T, ValueObject, Zone>( | 30 : BaseGrowableArray<T, ValueObject, Zone>( |
31 ASSERT_NOTNULL(Thread::Current()->zone())) {} | 31 ASSERT_NOTNULL(Thread::Current()->zone())) {} |
32 }; | 32 }; |
33 | 33 |
34 | |
35 template <typename T> | 34 template <typename T> |
36 class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated, Zone> { | 35 class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated, Zone> { |
37 public: | 36 public: |
38 ZoneGrowableArray(Zone* zone, intptr_t initial_capacity) | 37 ZoneGrowableArray(Zone* zone, intptr_t initial_capacity) |
39 : BaseGrowableArray<T, ZoneAllocated, Zone>(initial_capacity, | 38 : BaseGrowableArray<T, ZoneAllocated, Zone>(initial_capacity, |
40 ASSERT_NOTNULL(zone)) {} | 39 ASSERT_NOTNULL(zone)) {} |
41 explicit ZoneGrowableArray(intptr_t initial_capacity) | 40 explicit ZoneGrowableArray(intptr_t initial_capacity) |
42 : BaseGrowableArray<T, ZoneAllocated, Zone>( | 41 : BaseGrowableArray<T, ZoneAllocated, Zone>( |
43 initial_capacity, | 42 initial_capacity, |
44 ASSERT_NOTNULL(Thread::Current()->zone())) {} | 43 ASSERT_NOTNULL(Thread::Current()->zone())) {} |
45 ZoneGrowableArray() | 44 ZoneGrowableArray() |
46 : BaseGrowableArray<T, ZoneAllocated, Zone>( | 45 : BaseGrowableArray<T, ZoneAllocated, Zone>( |
47 ASSERT_NOTNULL(Thread::Current()->zone())) {} | 46 ASSERT_NOTNULL(Thread::Current()->zone())) {} |
48 }; | 47 }; |
49 | 48 |
50 | |
51 // T must be a Handle type. | 49 // T must be a Handle type. |
52 template <typename T, typename B> | 50 template <typename T, typename B> |
53 class BaseGrowableHandlePtrArray : public B { | 51 class BaseGrowableHandlePtrArray : public B { |
54 public: | 52 public: |
55 BaseGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) | 53 BaseGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) |
56 : zone_(zone), array_(zone, initial_capacity) {} | 54 : zone_(zone), array_(zone, initial_capacity) {} |
57 | 55 |
58 // Use unique zone handles to store objects. | 56 // Use unique zone handles to store objects. |
59 void Add(const T& t) { array_.Add(&T::ZoneHandle(zone_, t.raw())); } | 57 void Add(const T& t) { array_.Add(&T::ZoneHandle(zone_, t.raw())); } |
60 | 58 |
61 T& operator[](intptr_t index) const { return *array_[index]; } | 59 T& operator[](intptr_t index) const { return *array_[index]; } |
62 | 60 |
63 const T& At(intptr_t index) const { return operator[](index); } | 61 const T& At(intptr_t index) const { return operator[](index); } |
64 | 62 |
65 void SetAt(intptr_t index, const T& t) { | 63 void SetAt(intptr_t index, const T& t) { |
66 array_[index] = &T::ZoneHandle(zone_, t.raw()); | 64 array_[index] = &T::ZoneHandle(zone_, t.raw()); |
67 } | 65 } |
68 | 66 |
69 intptr_t length() const { return array_.length(); } | 67 intptr_t length() const { return array_.length(); } |
70 | 68 |
71 const GrowableArray<T*>& growable_array() const { return array_; } | 69 const GrowableArray<T*>& growable_array() const { return array_; } |
72 | 70 |
73 private: | 71 private: |
74 Zone* zone_; | 72 Zone* zone_; |
75 GrowableArray<T*> array_; | 73 GrowableArray<T*> array_; |
76 | 74 |
77 DISALLOW_COPY_AND_ASSIGN(BaseGrowableHandlePtrArray); | 75 DISALLOW_COPY_AND_ASSIGN(BaseGrowableHandlePtrArray); |
78 }; | 76 }; |
79 | 77 |
80 | |
81 template <typename T> | 78 template <typename T> |
82 class GrowableHandlePtrArray | 79 class GrowableHandlePtrArray |
83 : public BaseGrowableHandlePtrArray<T, ValueObject> { | 80 : public BaseGrowableHandlePtrArray<T, ValueObject> { |
84 public: | 81 public: |
85 GrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) | 82 GrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) |
86 : BaseGrowableHandlePtrArray<T, ValueObject>(zone, initial_capacity) {} | 83 : BaseGrowableHandlePtrArray<T, ValueObject>(zone, initial_capacity) {} |
87 }; | 84 }; |
88 | 85 |
89 | |
90 template <typename T> | 86 template <typename T> |
91 class ZoneGrowableHandlePtrArray | 87 class ZoneGrowableHandlePtrArray |
92 : public BaseGrowableHandlePtrArray<T, ZoneAllocated> { | 88 : public BaseGrowableHandlePtrArray<T, ZoneAllocated> { |
93 public: | 89 public: |
94 ZoneGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) | 90 ZoneGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity) |
95 : BaseGrowableHandlePtrArray<T, ZoneAllocated>(zone, initial_capacity) {} | 91 : BaseGrowableHandlePtrArray<T, ZoneAllocated>(zone, initial_capacity) {} |
96 }; | 92 }; |
97 | 93 |
98 } // namespace dart | 94 } // namespace dart |
99 | 95 |
100 #endif // RUNTIME_VM_GROWABLE_ARRAY_H_ | 96 #endif // RUNTIME_VM_GROWABLE_ARRAY_H_ |
OLD | NEW |