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: allocate on stack. | 5 // - GrowableArray: allocate on stack. |
6 // - ZoneGrowableArray: allocated in the zone. | 6 // - ZoneGrowableArray: allocated in the zone. |
7 | 7 |
8 #ifndef VM_GROWABLE_ARRAY_H_ | 8 #ifndef VM_GROWABLE_ARRAY_H_ |
9 #define VM_GROWABLE_ARRAY_H_ | 9 #define VM_GROWABLE_ARRAY_H_ |
10 | 10 |
11 #include "platform/utils.h" | 11 #include "platform/utils.h" |
12 #include "vm/allocation.h" | 12 #include "vm/allocation.h" |
13 #include "vm/isolate.h" | 13 #include "vm/isolate.h" |
14 #include "vm/zone.h" | 14 #include "vm/zone.h" |
15 | 15 |
16 namespace dart { | 16 namespace dart { |
17 | 17 |
18 template<typename T, typename B> | 18 template<typename T, typename B> |
19 class BaseGrowableArray : public B { | 19 class BaseGrowableArray : public B { |
20 public: | 20 public: |
21 explicit BaseGrowableArray(BaseZone* zone) | 21 explicit BaseGrowableArray(Zone* zone) |
22 : length_(0), capacity_(0), data_(NULL), zone_(zone) { | 22 : length_(0), capacity_(0), data_(NULL), zone_(zone) { |
23 ASSERT(zone_ != NULL); | 23 ASSERT(zone_ != NULL); |
24 } | 24 } |
25 | 25 |
26 BaseGrowableArray(int initial_capacity, BaseZone* zone) | 26 BaseGrowableArray(int initial_capacity, Zone* zone) |
27 : length_(0), capacity_(0), data_(NULL), zone_(zone) { | 27 : length_(0), capacity_(0), data_(NULL), zone_(zone) { |
28 ASSERT(zone_ != NULL); | 28 ASSERT(zone_ != NULL); |
29 if (initial_capacity > 0) { | 29 if (initial_capacity > 0) { |
30 capacity_ = Utils::RoundUpToPowerOfTwo(initial_capacity); | 30 capacity_ = Utils::RoundUpToPowerOfTwo(initial_capacity); |
31 data_ = zone_->Alloc<T>(capacity_); | 31 data_ = zone_->Alloc<T>(capacity_); |
32 } | 32 } |
33 } | 33 } |
34 | 34 |
35 int length() const { return length_; } | 35 int length() const { return length_; } |
36 T* data() const { return data_; } | 36 T* data() const { return data_; } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 data_[idx] = value; | 81 data_[idx] = value; |
82 } | 82 } |
83 | 83 |
84 // Sort the array in place. | 84 // Sort the array in place. |
85 inline void Sort(int compare(const T*, const T*)); | 85 inline void Sort(int compare(const T*, const T*)); |
86 | 86 |
87 private: | 87 private: |
88 int length_; | 88 int length_; |
89 int capacity_; | 89 int capacity_; |
90 T* data_; | 90 T* data_; |
91 BaseZone* zone_; // Zone in which we are allocating the array. | 91 Zone* zone_; // Zone in which we are allocating the array. |
92 | 92 |
93 void Resize(int new_length); | 93 void Resize(int new_length); |
94 | 94 |
95 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray); | 95 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray); |
96 }; | 96 }; |
97 | 97 |
98 | 98 |
99 template<typename T, typename B> | 99 template<typename T, typename B> |
100 inline void BaseGrowableArray<T, B>::Sort( | 100 inline void BaseGrowableArray<T, B>::Sort( |
101 int compare(const T*, const T*)) { | 101 int compare(const T*, const T*)) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 initial_capacity, | 138 initial_capacity, |
139 Isolate::Current()->current_zone()->GetBaseZone()) {} | 139 Isolate::Current()->current_zone()->GetBaseZone()) {} |
140 ZoneGrowableArray() : | 140 ZoneGrowableArray() : |
141 BaseGrowableArray<T, ZoneAllocated>( | 141 BaseGrowableArray<T, ZoneAllocated>( |
142 Isolate::Current()->current_zone()->GetBaseZone()) {} | 142 Isolate::Current()->current_zone()->GetBaseZone()) {} |
143 }; | 143 }; |
144 | 144 |
145 } // namespace dart | 145 } // namespace dart |
146 | 146 |
147 #endif // VM_GROWABLE_ARRAY_H_ | 147 #endif // VM_GROWABLE_ARRAY_H_ |
OLD | NEW |