| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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_PLATFORM_GROWABLE_ARRAY_H_ | 10 #ifndef RUNTIME_PLATFORM_GROWABLE_ARRAY_H_ |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 intptr_t capacity_; | 124 intptr_t capacity_; |
| 125 T* data_; | 125 T* data_; |
| 126 Allocator* allocator_; // Used to (re)allocate the array. | 126 Allocator* allocator_; // Used to (re)allocate the array. |
| 127 | 127 |
| 128 // Used for growing the array. | 128 // Used for growing the array. |
| 129 void Resize(intptr_t new_length); | 129 void Resize(intptr_t new_length); |
| 130 | 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray); | 131 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray); |
| 132 }; | 132 }; |
| 133 | 133 |
| 134 | |
| 135 template <typename T, typename B, typename Allocator> | 134 template <typename T, typename B, typename Allocator> |
| 136 inline void BaseGrowableArray<T, B, Allocator>::Sort(int compare(const T*, | 135 inline void BaseGrowableArray<T, B, Allocator>::Sort(int compare(const T*, |
| 137 const T*)) { | 136 const T*)) { |
| 138 typedef int (*CompareFunction)(const void*, const void*); | 137 typedef int (*CompareFunction)(const void*, const void*); |
| 139 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare)); | 138 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare)); |
| 140 } | 139 } |
| 141 | 140 |
| 142 | |
| 143 template <typename T, typename B, typename Allocator> | 141 template <typename T, typename B, typename Allocator> |
| 144 void BaseGrowableArray<T, B, Allocator>::Resize(intptr_t new_length) { | 142 void BaseGrowableArray<T, B, Allocator>::Resize(intptr_t new_length) { |
| 145 if (new_length > capacity_) { | 143 if (new_length > capacity_) { |
| 146 intptr_t new_capacity = Utils::RoundUpToPowerOfTwo(new_length); | 144 intptr_t new_capacity = Utils::RoundUpToPowerOfTwo(new_length); |
| 147 T* new_data = | 145 T* new_data = |
| 148 allocator_->template Realloc<T>(data_, capacity_, new_capacity); | 146 allocator_->template Realloc<T>(data_, capacity_, new_capacity); |
| 149 ASSERT(new_data != NULL); | 147 ASSERT(new_data != NULL); |
| 150 data_ = new_data; | 148 data_ = new_data; |
| 151 capacity_ = new_capacity; | 149 capacity_ = new_capacity; |
| 152 } | 150 } |
| 153 length_ = new_length; | 151 length_ = new_length; |
| 154 } | 152 } |
| 155 | 153 |
| 156 | |
| 157 template <typename T, typename B, typename Allocator> | 154 template <typename T, typename B, typename Allocator> |
| 158 void BaseGrowableArray<T, B, Allocator>::SetLength(intptr_t new_length) { | 155 void BaseGrowableArray<T, B, Allocator>::SetLength(intptr_t new_length) { |
| 159 if (new_length > capacity_) { | 156 if (new_length > capacity_) { |
| 160 T* new_data = allocator_->template Alloc<T>(new_length); | 157 T* new_data = allocator_->template Alloc<T>(new_length); |
| 161 ASSERT(new_data != NULL); | 158 ASSERT(new_data != NULL); |
| 162 data_ = new_data; | 159 data_ = new_data; |
| 163 capacity_ = new_length; | 160 capacity_ = new_length; |
| 164 } | 161 } |
| 165 length_ = new_length; | 162 length_ = new_length; |
| 166 } | 163 } |
| 167 | 164 |
| 168 | |
| 169 class Malloc : public AllStatic { | 165 class Malloc : public AllStatic { |
| 170 public: | 166 public: |
| 171 template <class T> | 167 template <class T> |
| 172 static inline T* Alloc(intptr_t len) { | 168 static inline T* Alloc(intptr_t len) { |
| 173 return reinterpret_cast<T*>(malloc(len * sizeof(T))); | 169 return reinterpret_cast<T*>(malloc(len * sizeof(T))); |
| 174 } | 170 } |
| 175 | 171 |
| 176 template <class T> | 172 template <class T> |
| 177 static inline T* Realloc(T* old_array, intptr_t old_len, intptr_t new_len) { | 173 static inline T* Realloc(T* old_array, intptr_t old_len, intptr_t new_len) { |
| 178 return reinterpret_cast<T*>(realloc(old_array, new_len * sizeof(T))); | 174 return reinterpret_cast<T*>(realloc(old_array, new_len * sizeof(T))); |
| 179 } | 175 } |
| 180 | 176 |
| 181 template <class T> | 177 template <class T> |
| 182 static inline void Free(T* old_array, intptr_t old_len) { | 178 static inline void Free(T* old_array, intptr_t old_len) { |
| 183 free(old_array); | 179 free(old_array); |
| 184 } | 180 } |
| 185 }; | 181 }; |
| 186 | 182 |
| 187 | |
| 188 class EmptyBase {}; | 183 class EmptyBase {}; |
| 189 | 184 |
| 190 | |
| 191 template <typename T> | 185 template <typename T> |
| 192 class MallocGrowableArray : public BaseGrowableArray<T, EmptyBase, Malloc> { | 186 class MallocGrowableArray : public BaseGrowableArray<T, EmptyBase, Malloc> { |
| 193 public: | 187 public: |
| 194 explicit MallocGrowableArray(intptr_t initial_capacity) | 188 explicit MallocGrowableArray(intptr_t initial_capacity) |
| 195 : BaseGrowableArray<T, EmptyBase, Malloc>(initial_capacity, NULL) {} | 189 : BaseGrowableArray<T, EmptyBase, Malloc>(initial_capacity, NULL) {} |
| 196 MallocGrowableArray() : BaseGrowableArray<T, EmptyBase, Malloc>(NULL) {} | 190 MallocGrowableArray() : BaseGrowableArray<T, EmptyBase, Malloc>(NULL) {} |
| 197 }; | 191 }; |
| 198 | 192 |
| 199 } // namespace dart | 193 } // namespace dart |
| 200 | 194 |
| 201 #endif // RUNTIME_PLATFORM_GROWABLE_ARRAY_H_ | 195 #endif // RUNTIME_PLATFORM_GROWABLE_ARRAY_H_ |
| OLD | NEW |