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 BaseGrowableArray() : length_(0), capacity_(0), data_(NULL), zone_(NULL) { |
22 ASSERT(Isolate::Current() != NULL); | 22 ASSERT(Isolate::Current() != NULL); |
23 zone_ = Isolate::Current()->current_zone(); | 23 zone_ = Isolate::Current()->current_zone(); |
24 #ifdef DEBUG | |
25 isolate_zone_ = true; | |
26 #endif | |
24 } | 27 } |
25 | 28 |
26 explicit BaseGrowableArray(int initial_capacity) | 29 explicit BaseGrowableArray(int initial_capacity, Zone* zone = NULL) |
siva
2012/02/04 01:55:43
BaseGrowableArray is not used anywhere but here, w
Søren Gjesse
2012/02/06 16:25:52
Done.
| |
27 : length_(0), capacity_(0), data_(NULL), zone_(NULL) { | 30 : length_(0), capacity_(0), data_(NULL), zone_(zone) { |
28 ASSERT(Isolate::Current() != NULL); | 31 if (zone == NULL) { |
29 zone_ = Isolate::Current()->current_zone(); | 32 ASSERT(Isolate::Current() != NULL); |
33 zone_ = Isolate::Current()->current_zone(); | |
34 } | |
35 #ifdef DEBUG | |
36 if (Isolate::Current() != NULL) { | |
37 isolate_zone_ = (zone_ == Isolate::Current()->current_zone()); | |
38 } else { | |
39 isolate_zone_ = false; | |
40 } | |
41 #endif | |
30 if (initial_capacity > 0) { | 42 if (initial_capacity > 0) { |
31 capacity_ = Utils::RoundUpToPowerOfTwo(initial_capacity); | 43 capacity_ = Utils::RoundUpToPowerOfTwo(initial_capacity); |
32 data_ = reinterpret_cast<T*>(zone_->Allocate(capacity_ * sizeof(T))); | 44 data_ = reinterpret_cast<T*>(zone_->Allocate(capacity_ * sizeof(T))); |
33 } | 45 } |
34 } | 46 } |
35 | 47 |
36 int length() const { return length_; } | 48 int length() const { return length_; } |
37 T* data() const { return data_; } | 49 T* data() const { return data_; } |
38 bool is_empty() const { return length_ == 0; } | 50 bool is_empty() const { return length_ == 0; } |
39 | 51 |
(...skipping 30 matching lines...) Expand all Loading... | |
70 } | 82 } |
71 | 83 |
72 // Sort the array in place. | 84 // Sort the array in place. |
73 inline void Sort(int compare(const T*, const T*)); | 85 inline void Sort(int compare(const T*, const T*)); |
74 | 86 |
75 private: | 87 private: |
76 int length_; | 88 int length_; |
77 int capacity_; | 89 int capacity_; |
78 T* data_; | 90 T* data_; |
79 Zone* zone_; // Zone in which we are allocating the array. | 91 Zone* zone_; // Zone in which we are allocating the array. |
92 #ifdef DEBUG | |
93 bool isolate_zone_; | |
94 #endif | |
80 | 95 |
81 void Resize(int new_length); | 96 void Resize(int new_length); |
82 | 97 |
83 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray); | 98 DISALLOW_COPY_AND_ASSIGN(BaseGrowableArray); |
84 }; | 99 }; |
85 | 100 |
86 | 101 |
87 template<typename T, typename B> | 102 template<typename T, typename B> |
88 inline void BaseGrowableArray<T, B>::Sort( | 103 inline void BaseGrowableArray<T, B>::Sort( |
89 int compare(const T*, const T*)) { | 104 int compare(const T*, const T*)) { |
90 typedef int (*CompareFunction)(const void*, const void*); | 105 typedef int (*CompareFunction)(const void*, const void*); |
91 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare)); | 106 qsort(data_, length_, sizeof(T), reinterpret_cast<CompareFunction>(compare)); |
92 } | 107 } |
93 | 108 |
94 | 109 |
95 template<typename T, typename B> | 110 template<typename T, typename B> |
96 void BaseGrowableArray<T, B>::Resize(int new_length) { | 111 void BaseGrowableArray<T, B>::Resize(int new_length) { |
97 if (new_length > capacity_) { | 112 if (new_length > capacity_) { |
98 ASSERT(Isolate::Current() != NULL); | 113 #ifdef DEBUG |
99 // Check that we allocating in the array's zone. | 114 if (isolate_zone_) { |
100 ASSERT(zone_ == Isolate::Current()->current_zone()); | 115 ASSERT(Isolate::Current() != NULL); |
116 // Check that we allocating in the array's zone. | |
117 ASSERT(zone_ == Isolate::Current()->current_zone()); | |
118 } | |
119 #endif | |
101 int new_capacity = Utils::RoundUpToPowerOfTwo(new_length); | 120 int new_capacity = Utils::RoundUpToPowerOfTwo(new_length); |
102 T* new_data = reinterpret_cast<T*>( | 121 T* new_data = reinterpret_cast<T*>( |
103 zone_->Reallocate(reinterpret_cast<uword>(data_), | 122 zone_->Reallocate(reinterpret_cast<uword>(data_), |
104 capacity_ * sizeof(T), | 123 capacity_ * sizeof(T), |
105 new_capacity * sizeof(T))); | 124 new_capacity * sizeof(T))); |
106 ASSERT(new_data != NULL); | 125 ASSERT(new_data != NULL); |
107 data_ = new_data; | 126 data_ = new_data; |
108 capacity_ = new_capacity; | 127 capacity_ = new_capacity; |
109 } | 128 } |
110 length_ = new_length; | 129 length_ = new_length; |
111 } | 130 } |
112 | 131 |
113 | 132 |
114 template<typename T> | 133 template<typename T> |
115 class GrowableArray : public BaseGrowableArray<T, ValueObject> { | 134 class GrowableArray : public BaseGrowableArray<T, ValueObject> { |
116 public: | 135 public: |
117 explicit GrowableArray(int initial_capacity) | 136 GrowableArray(int initial_capacity, Zone* zone = NULL) |
118 : BaseGrowableArray<T, ValueObject>(initial_capacity) {} | 137 : BaseGrowableArray<T, ValueObject>(initial_capacity, zone) {} |
siva
2012/02/04 01:55:43
Have two versions of the constructor, the one argu
Søren Gjesse
2012/02/06 16:25:52
Done.
| |
119 GrowableArray() : BaseGrowableArray<T, ValueObject>() {} | 138 GrowableArray() : BaseGrowableArray<T, ValueObject>() {} |
120 }; | 139 }; |
121 | 140 |
122 | 141 |
123 template<typename T> | 142 template<typename T> |
124 class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated> { | 143 class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated> { |
125 public: | 144 public: |
126 explicit ZoneGrowableArray(int initial_capacity) | 145 explicit ZoneGrowableArray(int initial_capacity) |
127 : BaseGrowableArray<T, ZoneAllocated>(initial_capacity) {} | 146 : BaseGrowableArray<T, ZoneAllocated>(initial_capacity) {} |
128 ZoneGrowableArray() : BaseGrowableArray<T, ZoneAllocated>() {} | 147 ZoneGrowableArray() : BaseGrowableArray<T, ZoneAllocated>() {} |
129 }; | 148 }; |
130 | 149 |
131 } // namespace dart | 150 } // namespace dart |
132 | 151 |
133 #endif // VM_GROWABLE_ARRAY_H_ | 152 #endif // VM_GROWABLE_ARRAY_H_ |
OLD | NEW |