| 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" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 | 155 |
| 156 | 156 |
| 157 class StackZone : public StackResource { | 157 class StackZone : public StackResource { |
| 158 public: | 158 public: |
| 159 // Create an empty zone and set is at the current zone for the Isolate. | 159 // Create an empty zone and set is at the current zone for the Isolate. |
| 160 explicit StackZone(BaseIsolate* isolate) | 160 explicit StackZone(BaseIsolate* isolate) |
| 161 : StackResource(isolate), | 161 : StackResource(isolate), |
| 162 zone_() { | 162 zone_() { |
| 163 #ifdef DEBUG | 163 #ifdef DEBUG |
| 164 if (FLAG_trace_zones) { | 164 if (FLAG_trace_zones) { |
| 165 OS::PrintErr("*** Starting a new Stack zone 0x%"Px"(0x%"Px")\n", | 165 OS::PrintErr("*** Starting a new Stack zone 0x%" Px "(0x%" Px ")\n", |
| 166 reinterpret_cast<intptr_t>(this), | 166 reinterpret_cast<intptr_t>(this), |
| 167 reinterpret_cast<intptr_t>(&zone_)); | 167 reinterpret_cast<intptr_t>(&zone_)); |
| 168 } | 168 } |
| 169 #endif | 169 #endif |
| 170 zone_.Link(isolate->current_zone()); | 170 zone_.Link(isolate->current_zone()); |
| 171 isolate->set_current_zone(&zone_); | 171 isolate->set_current_zone(&zone_); |
| 172 } | 172 } |
| 173 | 173 |
| 174 // Delete all memory associated with the zone. | 174 // Delete all memory associated with the zone. |
| 175 ~StackZone() { | 175 ~StackZone() { |
| 176 ASSERT(isolate()->current_zone() == &zone_); | 176 ASSERT(isolate()->current_zone() == &zone_); |
| 177 isolate()->set_current_zone(zone_.previous_); | 177 isolate()->set_current_zone(zone_.previous_); |
| 178 #ifdef DEBUG | 178 #ifdef DEBUG |
| 179 if (FLAG_trace_zones) { | 179 if (FLAG_trace_zones) { |
| 180 OS::PrintErr("*** Deleting Stack zone 0x%"Px"(0x%"Px")\n", | 180 OS::PrintErr("*** Deleting Stack zone 0x%" Px "(0x%" Px ")\n", |
| 181 reinterpret_cast<intptr_t>(this), | 181 reinterpret_cast<intptr_t>(this), |
| 182 reinterpret_cast<intptr_t>(&zone_)); | 182 reinterpret_cast<intptr_t>(&zone_)); |
| 183 } | 183 } |
| 184 #endif | 184 #endif |
| 185 } | 185 } |
| 186 | 186 |
| 187 // Compute the total size of this zone. This includes wasted space that is | 187 // Compute the total size of this zone. This includes wasted space that is |
| 188 // due to internal fragmentation in the segments. | 188 // due to internal fragmentation in the segments. |
| 189 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } | 189 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } |
| 190 | 190 |
| 191 Zone* GetZone() { return &zone_; } | 191 Zone* GetZone() { return &zone_; } |
| 192 | 192 |
| 193 private: | 193 private: |
| 194 Zone zone_; | 194 Zone zone_; |
| 195 | 195 |
| 196 template<typename T> friend class GrowableArray; | 196 template<typename T> friend class GrowableArray; |
| 197 template<typename T> friend class ZoneGrowableArray; | 197 template<typename T> friend class ZoneGrowableArray; |
| 198 | 198 |
| 199 DISALLOW_IMPLICIT_CONSTRUCTORS(StackZone); | 199 DISALLOW_IMPLICIT_CONSTRUCTORS(StackZone); |
| 200 }; | 200 }; |
| 201 | 201 |
| 202 inline uword Zone::AllocUnsafe(intptr_t size) { | 202 inline uword Zone::AllocUnsafe(intptr_t size) { |
| 203 ASSERT(size >= 0); | 203 ASSERT(size >= 0); |
| 204 | 204 |
| 205 // Round up the requested size to fit the alignment. | 205 // Round up the requested size to fit the alignment. |
| 206 if (size > (kIntptrMax - kAlignment)) { | 206 if (size > (kIntptrMax - kAlignment)) { |
| 207 FATAL1("Zone::Alloc: 'size' is too large: size=%"Pd"", size); | 207 FATAL1("Zone::Alloc: 'size' is too large: size=%" Pd "", size); |
| 208 } | 208 } |
| 209 size = Utils::RoundUp(size, kAlignment); | 209 size = Utils::RoundUp(size, kAlignment); |
| 210 | 210 |
| 211 // Check if the requested size is available without expanding. | 211 // Check if the requested size is available without expanding. |
| 212 uword result; | 212 uword result; |
| 213 intptr_t free_size = (limit_ - position_); | 213 intptr_t free_size = (limit_ - position_); |
| 214 if (free_size >= size) { | 214 if (free_size >= size) { |
| 215 result = position_; | 215 result = position_; |
| 216 position_ += size; | 216 position_ += size; |
| 217 } else { | 217 } else { |
| 218 result = AllocateExpand(size); | 218 result = AllocateExpand(size); |
| 219 } | 219 } |
| 220 | 220 |
| 221 // Check that the result has the proper alignment and return it. | 221 // Check that the result has the proper alignment and return it. |
| 222 ASSERT(Utils::IsAligned(result, kAlignment)); | 222 ASSERT(Utils::IsAligned(result, kAlignment)); |
| 223 return result; | 223 return result; |
| 224 } | 224 } |
| 225 | 225 |
| 226 template <class ElementType> | 226 template <class ElementType> |
| 227 inline ElementType* Zone::Alloc(intptr_t len) { | 227 inline ElementType* Zone::Alloc(intptr_t len) { |
| 228 const intptr_t element_size = sizeof(ElementType); | 228 const intptr_t element_size = sizeof(ElementType); |
| 229 if (len > (kIntptrMax / element_size)) { | 229 if (len > (kIntptrMax / element_size)) { |
| 230 FATAL2("Zone::Alloc: 'len' is too large: len=%"Pd", element_size=%"Pd, | 230 FATAL2("Zone::Alloc: 'len' is too large: len=%" Pd ", element_size=%" Pd, |
| 231 len, element_size); | 231 len, element_size); |
| 232 } | 232 } |
| 233 return reinterpret_cast<ElementType*>(AllocUnsafe(len * element_size)); | 233 return reinterpret_cast<ElementType*>(AllocUnsafe(len * element_size)); |
| 234 } | 234 } |
| 235 | 235 |
| 236 template <class ElementType> | 236 template <class ElementType> |
| 237 inline ElementType* Zone::Realloc(ElementType* old_data, | 237 inline ElementType* Zone::Realloc(ElementType* old_data, |
| 238 intptr_t old_len, | 238 intptr_t old_len, |
| 239 intptr_t new_len) { | 239 intptr_t new_len) { |
| 240 ElementType* new_data = Alloc<ElementType>(new_len); | 240 ElementType* new_data = Alloc<ElementType>(new_len); |
| 241 if (old_data != 0) { | 241 if (old_data != 0) { |
| 242 memmove(reinterpret_cast<void*>(new_data), | 242 memmove(reinterpret_cast<void*>(new_data), |
| 243 reinterpret_cast<void*>(old_data), | 243 reinterpret_cast<void*>(old_data), |
| 244 Utils::Minimum(old_len * sizeof(ElementType), | 244 Utils::Minimum(old_len * sizeof(ElementType), |
| 245 new_len * sizeof(ElementType))); | 245 new_len * sizeof(ElementType))); |
| 246 } | 246 } |
| 247 return new_data; | 247 return new_data; |
| 248 } | 248 } |
| 249 | 249 |
| 250 } // namespace dart | 250 } // namespace dart |
| 251 | 251 |
| 252 #endif // VM_ZONE_H_ | 252 #endif // VM_ZONE_H_ |
| OLD | NEW |