| 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 |
| 8 // at destruction. |
| 7 | 9 |
| 8 #ifndef VM_GROWABLE_ARRAY_H_ | 10 #ifndef VM_GROWABLE_ARRAY_H_ |
| 9 #define VM_GROWABLE_ARRAY_H_ | 11 #define VM_GROWABLE_ARRAY_H_ |
| 10 | 12 |
| 11 #include "platform/utils.h" | 13 #include "platform/utils.h" |
| 12 #include "vm/allocation.h" | 14 #include "vm/allocation.h" |
| 13 #include "vm/isolate.h" | 15 #include "vm/isolate.h" |
| 14 #include "vm/zone.h" | 16 #include "vm/zone.h" |
| 15 | 17 |
| 16 namespace dart { | 18 namespace dart { |
| 17 | 19 |
| 18 template<typename T, typename B> | 20 template<typename T, typename B, typename Allocator = Zone> |
| 19 class BaseGrowableArray : public B { | 21 class BaseGrowableArray : public B { |
| 20 public: | 22 public: |
| 21 explicit BaseGrowableArray(Zone* zone) | 23 explicit BaseGrowableArray(Allocator* allocator) |
| 22 : length_(0), capacity_(0), data_(NULL), zone_(zone) { | 24 : length_(0), capacity_(0), data_(NULL), allocator_(allocator) {} |
| 23 ASSERT(zone_ != NULL); | 25 |
| 26 BaseGrowableArray(intptr_t initial_capacity, Allocator* allocator) |
| 27 : length_(0), capacity_(0), data_(NULL), allocator_(allocator) { |
| 28 if (initial_capacity > 0) { |
| 29 capacity_ = Utils::RoundUpToPowerOfTwo(initial_capacity); |
| 30 data_ = allocator_->template Alloc<T>(capacity_); |
| 31 } |
| 24 } | 32 } |
| 25 | 33 |
| 26 BaseGrowableArray(intptr_t initial_capacity, Zone* zone) | 34 ~BaseGrowableArray() { |
| 27 : length_(0), capacity_(0), data_(NULL), zone_(zone) { | 35 allocator_->template Free<T>(data_, capacity_); |
| 28 ASSERT(zone_ != NULL); | |
| 29 if (initial_capacity > 0) { | |
| 30 capacity_ = Utils::RoundUpToPowerOfTwo(initial_capacity); | |
| 31 data_ = zone_->Alloc<T>(capacity_); | |
| 32 } | |
| 33 } | 36 } |
| 34 | 37 |
| 35 intptr_t length() const { return length_; } | 38 intptr_t length() const { return length_; } |
| 36 T* data() const { return data_; } | 39 T* data() const { return data_; } |
| 37 bool is_empty() const { return length_ == 0; } | 40 bool is_empty() const { return length_ == 0; } |
| 38 | 41 |
| 39 void TruncateTo(intptr_t length) { | 42 void TruncateTo(intptr_t length) { |
| 40 ASSERT(length_ >= length); | 43 ASSERT(length_ >= length); |
| 41 length_ = length; | 44 length_ = length; |
| 42 } | 45 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 // The content is uninitialized after calling it. | 93 // The content is uninitialized after calling it. |
| 91 void SetLength(intptr_t new_length); | 94 void SetLength(intptr_t new_length); |
| 92 | 95 |
| 93 // Sort the array in place. | 96 // Sort the array in place. |
| 94 inline void Sort(int compare(const T*, const T*)); | 97 inline void Sort(int compare(const T*, const T*)); |
| 95 | 98 |
| 96 private: | 99 private: |
| 97 intptr_t length_; | 100 intptr_t length_; |
| 98 intptr_t capacity_; | 101 intptr_t capacity_; |
| 99 T* data_; | 102 T* data_; |
| 100 Zone* zone_; // Zone in which we are allocating the array. | 103 Allocator* allocator_; // Used to (re)allocate the array. |
| 101 | 104 |
| 102 // Used for growing the array. | 105 // Used for growing the array. |
| 103 void Resize(intptr_t new_length); | 106 void Resize(intptr_t new_length); |
| 104 | 107 |
| 105 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray); | 108 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray); |
| 106 }; | 109 }; |
| 107 | 110 |
| 108 | 111 |
| 109 template<typename T, typename B> | 112 template<typename T, typename B, typename Allocator> |
| 110 inline void BaseGrowableArray<T, B>::Sort( | 113 inline void BaseGrowableArray<T, B, Allocator>::Sort( |
| 111 int compare(const T*, const T*)) { | 114 int compare(const T*, const T*)) { |
| 112 typedef int (*CompareFunction)(const void*, const void*); | 115 typedef int (*CompareFunction)(const void*, const void*); |
| 113 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare)); | 116 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare)); |
| 114 } | 117 } |
| 115 | 118 |
| 116 | 119 |
| 117 template<typename T, typename B> | 120 template<typename T, typename B, typename Allocator> |
| 118 void BaseGrowableArray<T, B>::Resize(intptr_t new_length) { | 121 void BaseGrowableArray<T, B, Allocator>::Resize(intptr_t new_length) { |
| 119 if (new_length > capacity_) { | 122 if (new_length > capacity_) { |
| 120 intptr_t new_capacity = Utils::RoundUpToPowerOfTwo(new_length); | 123 intptr_t new_capacity = Utils::RoundUpToPowerOfTwo(new_length); |
| 121 T* new_data = zone_->Realloc<T>(data_, capacity_, new_capacity); | 124 T* new_data = |
| 125 allocator_->template Realloc<T>(data_, capacity_, new_capacity); |
| 122 ASSERT(new_data != NULL); | 126 ASSERT(new_data != NULL); |
| 123 data_ = new_data; | 127 data_ = new_data; |
| 124 capacity_ = new_capacity; | 128 capacity_ = new_capacity; |
| 125 } | 129 } |
| 126 length_ = new_length; | 130 length_ = new_length; |
| 127 } | 131 } |
| 128 | 132 |
| 129 | 133 |
| 130 template<typename T, typename B> | 134 template<typename T, typename B, typename Allocator> |
| 131 void BaseGrowableArray<T, B>::SetLength(intptr_t new_length) { | 135 void BaseGrowableArray<T, B, Allocator>::SetLength(intptr_t new_length) { |
| 132 if (new_length > capacity_) { | 136 if (new_length > capacity_) { |
| 133 T* new_data = zone_->Alloc<T>(new_length); | 137 T* new_data = allocator_->template Alloc<T>(new_length); |
| 134 ASSERT(new_data != NULL); | 138 ASSERT(new_data != NULL); |
| 135 data_ = new_data; | 139 data_ = new_data; |
| 136 capacity_ = new_length; | 140 capacity_ = new_length; |
| 137 } | 141 } |
| 138 length_ = new_length; | 142 length_ = new_length; |
| 139 } | 143 } |
| 140 | 144 |
| 141 | 145 |
| 142 template<typename T> | 146 template<typename T> |
| 143 class GrowableArray : public BaseGrowableArray<T, ValueObject> { | 147 class GrowableArray : public BaseGrowableArray<T, ValueObject> { |
| 144 public: | 148 public: |
| 145 GrowableArray(Isolate* isolate, intptr_t initial_capacity) | 149 GrowableArray(Isolate* isolate, intptr_t initial_capacity) |
| 146 : BaseGrowableArray<T, ValueObject>( | 150 : BaseGrowableArray<T, ValueObject>( |
| 147 initial_capacity, isolate->current_zone()) {} | 151 initial_capacity, ASSERT_NOTNULL(isolate->current_zone())) {} |
| 148 explicit GrowableArray(intptr_t initial_capacity) | 152 explicit GrowableArray(intptr_t initial_capacity) |
| 149 : BaseGrowableArray<T, ValueObject>( | 153 : BaseGrowableArray<T, ValueObject>( |
| 150 initial_capacity, Isolate::Current()->current_zone()) {} | 154 initial_capacity, |
| 155 ASSERT_NOTNULL(Isolate::Current()->current_zone())) {} |
| 151 GrowableArray() | 156 GrowableArray() |
| 152 : BaseGrowableArray<T, ValueObject>( | 157 : BaseGrowableArray<T, ValueObject>( |
| 153 Isolate::Current()->current_zone()) {} | 158 ASSERT_NOTNULL(Isolate::Current()->current_zone())) {} |
| 154 }; | 159 }; |
| 155 | 160 |
| 156 | 161 |
| 157 template<typename T> | 162 template<typename T> |
| 158 class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated> { | 163 class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated> { |
| 159 public: | 164 public: |
| 160 ZoneGrowableArray(Isolate* isolate, intptr_t initial_capacity) | 165 ZoneGrowableArray(Isolate* isolate, intptr_t initial_capacity) |
| 161 : BaseGrowableArray<T, ZoneAllocated>( | 166 : BaseGrowableArray<T, ZoneAllocated>( |
| 162 initial_capacity, isolate->current_zone()) {} | 167 initial_capacity, ASSERT_NOTNULL(isolate->current_zone())) {} |
| 163 explicit ZoneGrowableArray(intptr_t initial_capacity) | 168 explicit ZoneGrowableArray(intptr_t initial_capacity) |
| 164 : BaseGrowableArray<T, ZoneAllocated>( | 169 : BaseGrowableArray<T, ZoneAllocated>( |
| 165 initial_capacity, | 170 initial_capacity, |
| 166 Isolate::Current()->current_zone()) {} | 171 ASSERT_NOTNULL(Isolate::Current()->current_zone())) {} |
| 167 ZoneGrowableArray() : | 172 ZoneGrowableArray() |
| 168 BaseGrowableArray<T, ZoneAllocated>( | 173 : BaseGrowableArray<T, ZoneAllocated>( |
| 169 Isolate::Current()->current_zone()) {} | 174 ASSERT_NOTNULL(Isolate::Current()->current_zone())) {} |
| 175 }; |
| 176 |
| 177 |
| 178 class Malloc : public AllStatic { |
| 179 public: |
| 180 template <class T> |
| 181 static inline T* Alloc(intptr_t len) { |
| 182 return reinterpret_cast<T*>(malloc(len * sizeof(T))); |
| 183 } |
| 184 |
| 185 template <class T> |
| 186 static inline T* Realloc(T* old_array, intptr_t old_len, intptr_t new_len) { |
| 187 return reinterpret_cast<T*>(realloc(old_array, new_len * sizeof(T))); |
| 188 } |
| 189 |
| 190 template <class T> |
| 191 static inline void Free(T* old_array, intptr_t old_len) { |
| 192 free(old_array); |
| 193 } |
| 194 }; |
| 195 |
| 196 |
| 197 class EmptyBase {}; |
| 198 |
| 199 |
| 200 template<typename T> |
| 201 class MallocGrowableArray : public BaseGrowableArray<T, EmptyBase, Malloc> { |
| 202 public: |
| 203 explicit MallocGrowableArray(intptr_t initial_capacity) |
| 204 : BaseGrowableArray<T, EmptyBase, Malloc>(initial_capacity, NULL) {} |
| 205 MallocGrowableArray() |
| 206 : BaseGrowableArray<T, EmptyBase, Malloc>(NULL) {} |
| 170 }; | 207 }; |
| 171 | 208 |
| 172 } // namespace dart | 209 } // namespace dart |
| 173 | 210 |
| 174 #endif // VM_GROWABLE_ARRAY_H_ | 211 #endif // VM_GROWABLE_ARRAY_H_ |
| OLD | NEW |