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 RUNTIME_VM_ZONE_H_ | 5 #ifndef RUNTIME_VM_ZONE_H_ |
6 #define RUNTIME_VM_ZONE_H_ | 6 #define RUNTIME_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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 // TODO(zra): Remove these calls and replace them with calls to OS::SCreate | 57 // TODO(zra): Remove these calls and replace them with calls to OS::SCreate |
58 // and OS::VSCreate. | 58 // and OS::VSCreate. |
59 // These calls are deprecated. Do not add further calls to these functions. | 59 // These calls are deprecated. Do not add further calls to these functions. |
60 // instead use OS::SCreate and OS::VSCreate. | 60 // instead use OS::SCreate and OS::VSCreate. |
61 // Make a zone-allocated string based on printf format and args. | 61 // Make a zone-allocated string based on printf format and args. |
62 char* PrintToString(const char* format, ...) PRINTF_ATTRIBUTE(2, 3); | 62 char* PrintToString(const char* format, ...) PRINTF_ATTRIBUTE(2, 3); |
63 char* VPrint(const char* format, va_list args); | 63 char* VPrint(const char* format, va_list args); |
64 | 64 |
65 // Compute the total size of this zone. This includes wasted space that is | 65 // Compute the total size of this zone. This includes wasted space that is |
66 // due to internal fragmentation in the segments. | 66 // due to internal fragmentation in the segments. |
67 intptr_t SizeInBytes() const; | 67 uintptr_t SizeInBytes() const; |
68 | 68 |
69 // Computes the amount of space used in the zone. | 69 // Computes the amount of space used in the zone. |
70 intptr_t CapacityInBytes() const; | 70 uintptr_t CapacityInBytes() const; |
71 | 71 |
72 // Structure for managing handles allocation. | 72 // Structure for managing handles allocation. |
73 VMHandles* handles() { return &handles_; } | 73 VMHandles* handles() { return &handles_; } |
74 | 74 |
75 void VisitObjectPointers(ObjectPointerVisitor* visitor); | 75 void VisitObjectPointers(ObjectPointerVisitor* visitor); |
76 | 76 |
77 Zone* previous() const { return previous_; } | 77 Zone* previous() const { return previous_; } |
78 | 78 |
79 #ifndef PRODUCT | |
80 void PrintJSON(JSONStream* stream) const; | |
81 #endif | |
82 | |
83 private: | 79 private: |
84 Zone(); | 80 Zone(); |
85 ~Zone(); // Delete all memory associated with the zone. | 81 ~Zone(); // Delete all memory associated with the zone. |
86 | 82 |
87 // All pointers returned from AllocateUnsafe() and New() have this alignment. | 83 // All pointers returned from AllocateUnsafe() and New() have this alignment. |
88 static const intptr_t kAlignment = kDoubleSize; | 84 static const intptr_t kAlignment = kDoubleSize; |
89 | 85 |
90 // Default initial chunk size. | 86 // Default initial chunk size. |
91 static const intptr_t kInitialChunkSize = 1 * KB; | 87 static const intptr_t kInitialChunkSize = 1 * KB; |
92 | 88 |
93 // Default segment size. | 89 // Default segment size. |
94 static const intptr_t kSegmentSize = 64 * KB; | 90 static const intptr_t kSegmentSize = 64 * KB; |
95 | 91 |
96 // Zap value used to indicate deleted zone area (debug purposes). | 92 // Zap value used to indicate deleted zone area (debug purposes). |
97 static const unsigned char kZapDeletedByte = 0x42; | 93 static const unsigned char kZapDeletedByte = 0x42; |
98 | 94 |
99 // Zap value used to indicate uninitialized zone area (debug purposes). | 95 // Zap value used to indicate uninitialized zone area (debug purposes). |
100 static const unsigned char kZapUninitializedByte = 0xab; | 96 static const unsigned char kZapUninitializedByte = 0xab; |
101 | 97 |
| 98 // Zone memory statistic update methods. |
| 99 void IncrementMemoryCapacity(uintptr_t size); |
| 100 void DecrementMemoryCapacity(uintptr_t size); |
| 101 |
102 // Expand the zone to accommodate an allocation of 'size' bytes. | 102 // Expand the zone to accommodate an allocation of 'size' bytes. |
103 uword AllocateExpand(intptr_t size); | 103 uword AllocateExpand(intptr_t size); |
104 | 104 |
105 // Allocate a large segment. | 105 // Allocate a large segment. |
106 uword AllocateLargeSegment(intptr_t size); | 106 uword AllocateLargeSegment(intptr_t size); |
107 | 107 |
108 // Insert zone into zone chain, after current_zone. | 108 // Insert zone into zone chain, after current_zone. |
109 void Link(Zone* current_zone) { previous_ = current_zone; } | 109 void Link(Zone* current_zone) { previous_ = current_zone; } |
110 | 110 |
111 // Delete all objects and free all memory allocated in the zone. | 111 // Delete all objects and free all memory allocated in the zone. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 class StackZone : public StackResource { | 173 class StackZone : public StackResource { |
174 public: | 174 public: |
175 // Create an empty zone and set is at the current zone for the Thread. | 175 // Create an empty zone and set is at the current zone for the Thread. |
176 explicit StackZone(Thread* thread); | 176 explicit StackZone(Thread* thread); |
177 | 177 |
178 // Delete all memory associated with the zone. | 178 // Delete all memory associated with the zone. |
179 ~StackZone(); | 179 ~StackZone(); |
180 | 180 |
181 // Compute the total size of this zone. This includes wasted space that is | 181 // Compute the total size of this zone. This includes wasted space that is |
182 // due to internal fragmentation in the segments. | 182 // due to internal fragmentation in the segments. |
183 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } | 183 uintptr_t SizeInBytes() const { return zone_.SizeInBytes(); } |
184 | 184 |
185 // Computes the used space in the zone. | 185 // Computes the used space in the zone. |
186 intptr_t CapacityInBytes() const { return zone_.CapacityInBytes(); } | 186 intptr_t CapacityInBytes() const { return zone_.CapacityInBytes(); } |
187 | 187 |
188 Zone* GetZone() { return &zone_; } | 188 Zone* GetZone() { return &zone_; } |
189 | 189 |
190 private: | 190 private: |
191 Zone zone_; | 191 Zone zone_; |
192 | 192 |
193 template <typename T> | 193 template <typename T> |
194 friend class GrowableArray; | 194 friend class GrowableArray; |
195 template <typename T> | 195 template <typename T> |
196 friend class ZoneGrowableArray; | 196 friend class ZoneGrowableArray; |
197 | 197 |
198 DISALLOW_IMPLICIT_CONSTRUCTORS(StackZone); | 198 DISALLOW_IMPLICIT_CONSTRUCTORS(StackZone); |
199 }; | 199 }; |
200 | 200 |
201 inline uword Zone::AllocUnsafe(intptr_t size) { | 201 inline uword Zone::AllocUnsafe(intptr_t size) { |
202 ASSERT(size >= 0); | 202 ASSERT(size >= 0); |
203 | |
204 // Round up the requested size to fit the alignment. | 203 // Round up the requested size to fit the alignment. |
205 if (size > (kIntptrMax - kAlignment)) { | 204 if (size > (kIntptrMax - kAlignment)) { |
206 FATAL1("Zone::Alloc: 'size' is too large: size=%" Pd "", size); | 205 FATAL1("Zone::Alloc: 'size' is too large: size=%" Pd "", size); |
207 } | 206 } |
208 size = Utils::RoundUp(size, kAlignment); | 207 size = Utils::RoundUp(size, kAlignment); |
209 | 208 |
210 // Check if the requested size is available without expanding. | 209 // Check if the requested size is available without expanding. |
211 uword result; | 210 uword result; |
212 intptr_t free_size = (limit_ - position_); | 211 intptr_t free_size = (limit_ - position_); |
213 if (free_size >= size) { | 212 if (free_size >= size) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 intptr_t new_len) { | 244 intptr_t new_len) { |
246 CheckLength<ElementType>(new_len); | 245 CheckLength<ElementType>(new_len); |
247 const intptr_t kElementSize = sizeof(ElementType); | 246 const intptr_t kElementSize = sizeof(ElementType); |
248 uword old_end = reinterpret_cast<uword>(old_data) + (old_len * kElementSize); | 247 uword old_end = reinterpret_cast<uword>(old_data) + (old_len * kElementSize); |
249 // Resize existing allocation if nothing was allocated in between... | 248 // Resize existing allocation if nothing was allocated in between... |
250 if (Utils::RoundUp(old_end, kAlignment) == position_) { | 249 if (Utils::RoundUp(old_end, kAlignment) == position_) { |
251 uword new_end = | 250 uword new_end = |
252 reinterpret_cast<uword>(old_data) + (new_len * kElementSize); | 251 reinterpret_cast<uword>(old_data) + (new_len * kElementSize); |
253 // ...and there is sufficient space. | 252 // ...and there is sufficient space. |
254 if (new_end <= limit_) { | 253 if (new_end <= limit_) { |
| 254 ASSERT(new_len >= old_len); |
255 position_ = Utils::RoundUp(new_end, kAlignment); | 255 position_ = Utils::RoundUp(new_end, kAlignment); |
256 return old_data; | 256 return old_data; |
257 } | 257 } |
258 } | 258 } |
259 if (new_len <= old_len) { | 259 if (new_len <= old_len) { |
260 return old_data; | 260 return old_data; |
261 } | 261 } |
262 ElementType* new_data = Alloc<ElementType>(new_len); | 262 ElementType* new_data = Alloc<ElementType>(new_len); |
263 if (old_data != 0) { | 263 if (old_data != 0) { |
264 memmove(reinterpret_cast<void*>(new_data), | 264 memmove(reinterpret_cast<void*>(new_data), |
265 reinterpret_cast<void*>(old_data), old_len * kElementSize); | 265 reinterpret_cast<void*>(old_data), old_len * kElementSize); |
266 } | 266 } |
267 return new_data; | 267 return new_data; |
268 } | 268 } |
269 | 269 |
270 } // namespace dart | 270 } // namespace dart |
271 | 271 |
272 #endif // RUNTIME_VM_ZONE_H_ | 272 #endif // RUNTIME_VM_ZONE_H_ |
OLD | NEW |