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 | 4 |
5 #ifndef VM_ZONE_H_ | 5 #ifndef VM_ZONE_H_ |
6 #define VM_ZONE_H_ | 6 #define VM_ZONE_H_ |
7 | 7 |
8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
9 #include "vm/allocation.h" | 9 #include "vm/allocation.h" |
10 #include "vm/handles.h" | 10 #include "vm/handles.h" |
11 #include "vm/thread.h" | 11 #include "vm/thread.h" |
12 #include "vm/memory_region.h" | 12 #include "vm/memory_region.h" |
13 | 13 |
14 namespace dart { | 14 namespace dart { |
15 | 15 |
16 DECLARE_DEBUG_FLAG(bool, trace_zones); | |
17 | |
18 // Zones support very fast allocation of small chunks of memory. The | 16 // Zones support very fast allocation of small chunks of memory. The |
19 // chunks cannot be deallocated individually, but instead zones | 17 // chunks cannot be deallocated individually, but instead zones |
20 // support deallocating all chunks in one fast operation. | 18 // support deallocating all chunks in one fast operation. |
21 | 19 |
22 class Zone { | 20 class Zone { |
23 public: | 21 public: |
24 // Allocate an array sized to hold 'len' elements of type | 22 // Allocate an array sized to hold 'len' elements of type |
25 // 'ElementType'. Checks for integer overflow when performing the | 23 // 'ElementType'. Checks for integer overflow when performing the |
26 // size computation. | 24 // size computation. |
27 template <class ElementType> | 25 template <class ElementType> |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 previous_(NULL) { | 77 previous_(NULL) { |
80 ASSERT(Utils::IsAligned(position_, kAlignment)); | 78 ASSERT(Utils::IsAligned(position_, kAlignment)); |
81 #ifdef DEBUG | 79 #ifdef DEBUG |
82 // Zap the entire initial buffer. | 80 // Zap the entire initial buffer. |
83 memset(initial_buffer_.pointer(), kZapUninitializedByte, | 81 memset(initial_buffer_.pointer(), kZapUninitializedByte, |
84 initial_buffer_.size()); | 82 initial_buffer_.size()); |
85 #endif | 83 #endif |
86 } | 84 } |
87 | 85 |
88 ~Zone() { // Delete all memory associated with the zone. | 86 ~Zone() { // Delete all memory associated with the zone. |
89 #if defined(DEBUG) | |
90 if (FLAG_trace_zones) { | 87 if (FLAG_trace_zones) { |
91 DumpZoneSizes(); | 88 DumpZoneSizes(); |
92 } | 89 } |
93 #endif | |
94 DeleteAll(); | 90 DeleteAll(); |
95 } | 91 } |
96 | 92 |
97 // All pointers returned from AllocateUnsafe() and New() have this alignment. | 93 // All pointers returned from AllocateUnsafe() and New() have this alignment. |
98 static const intptr_t kAlignment = kDoubleSize; | 94 static const intptr_t kAlignment = kDoubleSize; |
99 | 95 |
100 // Default initial chunk size. | 96 // Default initial chunk size. |
101 static const intptr_t kInitialChunkSize = 1 * KB; | 97 static const intptr_t kInitialChunkSize = 1 * KB; |
102 | 98 |
103 // Default segment size. | 99 // Default segment size. |
(...skipping 23 matching lines...) Expand all Loading... |
127 // BaseGrowableArray to use different allocators. | 123 // BaseGrowableArray to use different allocators. |
128 template <class ElementType> | 124 template <class ElementType> |
129 void Free(ElementType* old_array, intptr_t len) { | 125 void Free(ElementType* old_array, intptr_t len) { |
130 #ifdef DEBUG | 126 #ifdef DEBUG |
131 if (len > 0) { | 127 if (len > 0) { |
132 memset(old_array, kZapUninitializedByte, len * sizeof(ElementType)); | 128 memset(old_array, kZapUninitializedByte, len * sizeof(ElementType)); |
133 } | 129 } |
134 #endif | 130 #endif |
135 } | 131 } |
136 | 132 |
137 #if defined(DEBUG) | |
138 // Dump the current allocated sizes in the zone object. | 133 // Dump the current allocated sizes in the zone object. |
139 void DumpZoneSizes(); | 134 void DumpZoneSizes(); |
140 #endif | |
141 | 135 |
142 // Overflow check (FATAL) for array length. | 136 // Overflow check (FATAL) for array length. |
143 template <class ElementType> | 137 template <class ElementType> |
144 static inline void CheckLength(intptr_t len); | 138 static inline void CheckLength(intptr_t len); |
145 | 139 |
146 // This buffer is used for allocation before any segments. | 140 // This buffer is used for allocation before any segments. |
147 // This would act as the initial stack allocated chunk so that we don't | 141 // This would act as the initial stack allocated chunk so that we don't |
148 // end up calling malloc/free on zone scopes that allocate less than | 142 // end up calling malloc/free on zone scopes that allocate less than |
149 // kChunkSize | 143 // kChunkSize |
150 COMPILE_ASSERT(kAlignment <= 8); | 144 COMPILE_ASSERT(kAlignment <= 8); |
(...skipping 28 matching lines...) Expand all Loading... |
179 template<typename T, typename B, typename Allocator> | 173 template<typename T, typename B, typename Allocator> |
180 friend class BaseGrowableArray; | 174 friend class BaseGrowableArray; |
181 DISALLOW_COPY_AND_ASSIGN(Zone); | 175 DISALLOW_COPY_AND_ASSIGN(Zone); |
182 }; | 176 }; |
183 | 177 |
184 | 178 |
185 class StackZone : public StackResource { | 179 class StackZone : public StackResource { |
186 public: | 180 public: |
187 // Create an empty zone and set is at the current zone for the Thread. | 181 // Create an empty zone and set is at the current zone for the Thread. |
188 explicit StackZone(Thread* thread) : StackResource(thread), zone_() { | 182 explicit StackZone(Thread* thread) : StackResource(thread), zone_() { |
189 #ifdef DEBUG | |
190 if (FLAG_trace_zones) { | 183 if (FLAG_trace_zones) { |
191 OS::PrintErr("*** Starting a new Stack zone 0x%" Px "(0x%" Px ")\n", | 184 OS::PrintErr("*** Starting a new Stack zone 0x%" Px "(0x%" Px ")\n", |
192 reinterpret_cast<intptr_t>(this), | 185 reinterpret_cast<intptr_t>(this), |
193 reinterpret_cast<intptr_t>(&zone_)); | 186 reinterpret_cast<intptr_t>(&zone_)); |
194 } | 187 } |
195 #endif | |
196 zone_.Link(thread->zone()); | 188 zone_.Link(thread->zone()); |
197 thread->set_zone(&zone_); | 189 thread->set_zone(&zone_); |
198 } | 190 } |
199 | 191 |
200 // Delete all memory associated with the zone. | 192 // Delete all memory associated with the zone. |
201 ~StackZone() { | 193 ~StackZone() { |
202 ASSERT(thread()->zone() == &zone_); | 194 ASSERT(thread()->zone() == &zone_); |
203 thread()->set_zone(zone_.previous_); | 195 thread()->set_zone(zone_.previous_); |
204 #ifdef DEBUG | |
205 if (FLAG_trace_zones) { | 196 if (FLAG_trace_zones) { |
206 OS::PrintErr("*** Deleting Stack zone 0x%" Px "(0x%" Px ")\n", | 197 OS::PrintErr("*** Deleting Stack zone 0x%" Px "(0x%" Px ")\n", |
207 reinterpret_cast<intptr_t>(this), | 198 reinterpret_cast<intptr_t>(this), |
208 reinterpret_cast<intptr_t>(&zone_)); | 199 reinterpret_cast<intptr_t>(&zone_)); |
209 } | 200 } |
210 #endif | |
211 } | 201 } |
212 | 202 |
213 // Compute the total size of this zone. This includes wasted space that is | 203 // Compute the total size of this zone. This includes wasted space that is |
214 // due to internal fragmentation in the segments. | 204 // due to internal fragmentation in the segments. |
215 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } | 205 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } |
216 | 206 |
217 Zone* GetZone() { return &zone_; } | 207 Zone* GetZone() { return &zone_; } |
218 | 208 |
219 private: | 209 private: |
220 Zone zone_; | 210 Zone zone_; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 memmove(reinterpret_cast<void*>(new_data), | 281 memmove(reinterpret_cast<void*>(new_data), |
292 reinterpret_cast<void*>(old_data), | 282 reinterpret_cast<void*>(old_data), |
293 old_len * kElementSize); | 283 old_len * kElementSize); |
294 } | 284 } |
295 return new_data; | 285 return new_data; |
296 } | 286 } |
297 | 287 |
298 } // namespace dart | 288 } // namespace dart |
299 | 289 |
300 #endif // VM_ZONE_H_ | 290 #endif // VM_ZONE_H_ |
OLD | NEW |