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 |
(...skipping 10 matching lines...) Expand all Loading... |
21 explicit BaseGrowableArray(BaseZone* zone) | 21 explicit BaseGrowableArray(BaseZone* 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, BaseZone* 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_ = reinterpret_cast<T*>(zone_->Allocate(capacity_ * sizeof(T))); | 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_; } |
37 bool is_empty() const { return length_ == 0; } | 37 bool is_empty() const { return length_ == 0; } |
38 | 38 |
39 void TruncateTo(intptr_t length) { | 39 void TruncateTo(intptr_t length) { |
40 ASSERT(length_ >= length); | 40 ASSERT(length_ >= length); |
41 length_ = length; | 41 length_ = length; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 int compare(const T*, const T*)) { | 109 int compare(const T*, const T*)) { |
110 typedef int (*CompareFunction)(const void*, const void*); | 110 typedef int (*CompareFunction)(const void*, const void*); |
111 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare)); | 111 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare)); |
112 } | 112 } |
113 | 113 |
114 | 114 |
115 template<typename T, typename B> | 115 template<typename T, typename B> |
116 void BaseGrowableArray<T, B>::Resize(int new_length) { | 116 void BaseGrowableArray<T, B>::Resize(int new_length) { |
117 if (new_length > capacity_) { | 117 if (new_length > capacity_) { |
118 int new_capacity = Utils::RoundUpToPowerOfTwo(new_length); | 118 int new_capacity = Utils::RoundUpToPowerOfTwo(new_length); |
119 T* new_data = reinterpret_cast<T*>( | 119 T* new_data = zone_->Realloc<T>(data_, capacity_, new_capacity); |
120 zone_->Reallocate(reinterpret_cast<uword>(data_), | |
121 capacity_ * sizeof(T), | |
122 new_capacity * sizeof(T))); | |
123 ASSERT(new_data != NULL); | 120 ASSERT(new_data != NULL); |
124 data_ = new_data; | 121 data_ = new_data; |
125 capacity_ = new_capacity; | 122 capacity_ = new_capacity; |
126 } | 123 } |
127 length_ = new_length; | 124 length_ = new_length; |
128 } | 125 } |
129 | 126 |
130 | 127 |
131 template<typename T> | 128 template<typename T> |
132 class GrowableArray : public BaseGrowableArray<T, ValueObject> { | 129 class GrowableArray : public BaseGrowableArray<T, ValueObject> { |
(...skipping 16 matching lines...) Expand all Loading... |
149 initial_capacity, | 146 initial_capacity, |
150 Isolate::Current()->current_zone()->GetBaseZone()) {} | 147 Isolate::Current()->current_zone()->GetBaseZone()) {} |
151 ZoneGrowableArray() : | 148 ZoneGrowableArray() : |
152 BaseGrowableArray<T, ZoneAllocated>( | 149 BaseGrowableArray<T, ZoneAllocated>( |
153 Isolate::Current()->current_zone()->GetBaseZone()) {} | 150 Isolate::Current()->current_zone()->GetBaseZone()) {} |
154 }; | 151 }; |
155 | 152 |
156 } // namespace dart | 153 } // namespace dart |
157 | 154 |
158 #endif // VM_GROWABLE_ARRAY_H_ | 155 #endif // VM_GROWABLE_ARRAY_H_ |
OLD | NEW |