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 BaseGrowableArray() : length_(0), capacity_(0), data_(NULL), zone_(NULL) { | 21 explicit BaseGrowableArray(BaseZone* zone) |
22 ASSERT(Isolate::Current() != NULL); | 22 : length_(0), capacity_(0), data_(NULL), zone_(zone) { |
23 zone_ = Isolate::Current()->current_zone(); | 23 ASSERT(zone_ != NULL); |
24 } | 24 } |
25 | 25 |
26 explicit BaseGrowableArray(int initial_capacity) | 26 BaseGrowableArray(int initial_capacity, BaseZone* zone) |
27 : length_(0), capacity_(0), data_(NULL), zone_(NULL) { | 27 : length_(0), capacity_(0), data_(NULL), zone_(zone) { |
28 ASSERT(Isolate::Current() != NULL); | 28 ASSERT(zone_ != NULL); |
29 zone_ = Isolate::Current()->current_zone(); | |
30 if (initial_capacity > 0) { | 29 if (initial_capacity > 0) { |
31 capacity_ = Utils::RoundUpToPowerOfTwo(initial_capacity); | 30 capacity_ = Utils::RoundUpToPowerOfTwo(initial_capacity); |
32 data_ = reinterpret_cast<T*>(zone_->Allocate(capacity_ * sizeof(T))); | 31 data_ = reinterpret_cast<T*>(zone_->Allocate(capacity_ * sizeof(T))); |
33 } | 32 } |
34 } | 33 } |
35 | 34 |
36 int length() const { return length_; } | 35 int length() const { return length_; } |
37 T* data() const { return data_; } | 36 T* data() const { return data_; } |
38 bool is_empty() const { return length_ == 0; } | 37 bool is_empty() const { return length_ == 0; } |
39 | 38 |
(...skipping 29 matching lines...) Expand all Loading... |
69 length_ = 0; | 68 length_ = 0; |
70 } | 69 } |
71 | 70 |
72 // Sort the array in place. | 71 // Sort the array in place. |
73 inline void Sort(int compare(const T*, const T*)); | 72 inline void Sort(int compare(const T*, const T*)); |
74 | 73 |
75 private: | 74 private: |
76 int length_; | 75 int length_; |
77 int capacity_; | 76 int capacity_; |
78 T* data_; | 77 T* data_; |
79 Zone* zone_; // Zone in which we are allocating the array. | 78 BaseZone* zone_; // Zone in which we are allocating the array. |
80 | 79 |
81 void Resize(int new_length); | 80 void Resize(int new_length); |
82 | 81 |
83 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray); | 82 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray); |
84 }; | 83 }; |
85 | 84 |
86 | 85 |
87 template<typename T, typename B> | 86 template<typename T, typename B> |
88 inline void BaseGrowableArray<T, B>::Sort( | 87 inline void BaseGrowableArray<T, B>::Sort( |
89 int compare(const T*, const T*)) { | 88 int compare(const T*, const T*)) { |
90 typedef int (*CompareFunction)(const void*, const void*); | 89 typedef int (*CompareFunction)(const void*, const void*); |
91 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare)); | 90 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare)); |
92 } | 91 } |
93 | 92 |
94 | 93 |
95 template<typename T, typename B> | 94 template<typename T, typename B> |
96 void BaseGrowableArray<T, B>::Resize(int new_length) { | 95 void BaseGrowableArray<T, B>::Resize(int new_length) { |
97 if (new_length > capacity_) { | 96 if (new_length > capacity_) { |
98 ASSERT(Isolate::Current() != NULL); | |
99 // Check that we allocating in the array's zone. | |
100 ASSERT(zone_ == Isolate::Current()->current_zone()); | |
101 int new_capacity = Utils::RoundUpToPowerOfTwo(new_length); | 97 int new_capacity = Utils::RoundUpToPowerOfTwo(new_length); |
102 T* new_data = reinterpret_cast<T*>( | 98 T* new_data = reinterpret_cast<T*>( |
103 zone_->Reallocate(reinterpret_cast<uword>(data_), | 99 zone_->Reallocate(reinterpret_cast<uword>(data_), |
104 capacity_ * sizeof(T), | 100 capacity_ * sizeof(T), |
105 new_capacity * sizeof(T))); | 101 new_capacity * sizeof(T))); |
106 ASSERT(new_data != NULL); | 102 ASSERT(new_data != NULL); |
107 data_ = new_data; | 103 data_ = new_data; |
108 capacity_ = new_capacity; | 104 capacity_ = new_capacity; |
109 } | 105 } |
110 length_ = new_length; | 106 length_ = new_length; |
111 } | 107 } |
112 | 108 |
113 | 109 |
114 template<typename T> | 110 template<typename T> |
115 class GrowableArray : public BaseGrowableArray<T, ValueObject> { | 111 class GrowableArray : public BaseGrowableArray<T, ValueObject> { |
116 public: | 112 public: |
117 explicit GrowableArray(int initial_capacity) | 113 explicit GrowableArray(int initial_capacity) |
118 : BaseGrowableArray<T, ValueObject>(initial_capacity) {} | 114 : BaseGrowableArray<T, ValueObject>( |
119 GrowableArray() : BaseGrowableArray<T, ValueObject>() {} | 115 initial_capacity, |
| 116 Isolate::Current()->current_zone()->GetBaseZone()) {} |
| 117 GrowableArray() |
| 118 : BaseGrowableArray<T, ValueObject>( |
| 119 Isolate::Current()->current_zone()->GetBaseZone()) {} |
120 }; | 120 }; |
121 | 121 |
122 | 122 |
123 template<typename T> | 123 template<typename T> |
124 class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated> { | 124 class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated> { |
125 public: | 125 public: |
126 explicit ZoneGrowableArray(int initial_capacity) | 126 explicit ZoneGrowableArray(int initial_capacity) |
127 : BaseGrowableArray<T, ZoneAllocated>(initial_capacity) {} | 127 : BaseGrowableArray<T, ZoneAllocated>( |
128 ZoneGrowableArray() : BaseGrowableArray<T, ZoneAllocated>() {} | 128 initial_capacity, |
| 129 Isolate::Current()->current_zone()->GetBaseZone()) {} |
| 130 ZoneGrowableArray() : |
| 131 BaseGrowableArray<T, ZoneAllocated>( |
| 132 Isolate::Current()->current_zone()->GetBaseZone()) {} |
129 }; | 133 }; |
130 | 134 |
131 } // namespace dart | 135 } // namespace dart |
132 | 136 |
133 #endif // VM_GROWABLE_ARRAY_H_ | 137 #endif // VM_GROWABLE_ARRAY_H_ |
OLD | NEW |