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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 // Compute the total size of this zone. This includes wasted space that is | 145 // Compute the total size of this zone. This includes wasted space that is |
146 // due to internal fragmentation in the segments. | 146 // due to internal fragmentation in the segments. |
147 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } | 147 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } |
148 | 148 |
149 // Make a copy of the string in the zone allocated area. | 149 // Make a copy of the string in the zone allocated area. |
150 char* MakeCopyOfString(const char* str) { | 150 char* MakeCopyOfString(const char* str) { |
151 return zone_.MakeCopyOfString(str); | 151 return zone_.MakeCopyOfString(str); |
152 } | 152 } |
153 | 153 |
154 // Make a zone-allocated string based on printf format and args. | 154 // Make a zone-allocated string based on printf format and args. |
155 char* PrintToString(const char* format, ...); | 155 char* PrintToString(const char* format, ...) PRINTF_ATTRIBUTE(2, 3); |
156 | 156 |
157 VMHandles* handles() { return &handles_; } | 157 VMHandles* handles() { return &handles_; } |
158 | 158 |
159 void VisitObjectPointers(ObjectPointerVisitor* visitor); | 159 void VisitObjectPointers(ObjectPointerVisitor* visitor); |
160 | 160 |
161 private: | 161 private: |
162 BaseZone* GetBaseZone() { return &zone_; } | 162 BaseZone* GetBaseZone() { return &zone_; } |
163 | 163 |
164 BaseZone zone_; | 164 BaseZone zone_; |
165 | 165 |
166 // Structure for managing handles allocation. | 166 // Structure for managing handles allocation. |
167 VMHandles handles_; | 167 VMHandles handles_; |
168 | 168 |
169 // Used for chaining zones in order to allow unwinding of stacks. | 169 // Used for chaining zones in order to allow unwinding of stacks. |
170 Zone* previous_; | 170 Zone* previous_; |
171 | 171 |
172 template<typename T> friend class GrowableArray; | 172 template<typename T> friend class GrowableArray; |
173 template<typename T> friend class ZoneGrowableArray; | 173 template<typename T> friend class ZoneGrowableArray; |
174 | 174 |
175 DISALLOW_IMPLICIT_CONSTRUCTORS(Zone); | 175 DISALLOW_IMPLICIT_CONSTRUCTORS(Zone); |
176 }; | 176 }; |
177 | 177 |
178 inline uword BaseZone::AllocUnsafe(intptr_t size) { | 178 inline uword BaseZone::AllocUnsafe(intptr_t size) { |
179 ASSERT(size >= 0); | 179 ASSERT(size >= 0); |
180 | 180 |
181 // Round up the requested size to fit the alignment. | 181 // Round up the requested size to fit the alignment. |
182 if (size > (kIntptrMax - kAlignment)) { | 182 if (size > (kIntptrMax - kAlignment)) { |
183 FATAL1("BaseZone::Alloc: 'size' is too large: size=%ld", size); | 183 FATAL1("BaseZone::Alloc: 'size' is too large: size=%"Pd"", size); |
184 } | 184 } |
185 size = Utils::RoundUp(size, kAlignment); | 185 size = Utils::RoundUp(size, kAlignment); |
186 | 186 |
187 // Check if the requested size is available without expanding. | 187 // Check if the requested size is available without expanding. |
188 uword result; | 188 uword result; |
189 intptr_t free_size = (limit_ - position_); | 189 intptr_t free_size = (limit_ - position_); |
190 if (free_size >= size) { | 190 if (free_size >= size) { |
191 result = position_; | 191 result = position_; |
192 position_ += size; | 192 position_ += size; |
193 } else { | 193 } else { |
194 result = AllocateExpand(size); | 194 result = AllocateExpand(size); |
195 } | 195 } |
196 | 196 |
197 // Check that the result has the proper alignment and return it. | 197 // Check that the result has the proper alignment and return it. |
198 ASSERT(Utils::IsAligned(result, kAlignment)); | 198 ASSERT(Utils::IsAligned(result, kAlignment)); |
199 return result; | 199 return result; |
200 } | 200 } |
201 | 201 |
202 template <class ElementType> | 202 template <class ElementType> |
203 inline ElementType* BaseZone::Alloc(intptr_t len) { | 203 inline ElementType* BaseZone::Alloc(intptr_t len) { |
204 const intptr_t element_size = sizeof(ElementType); | 204 const intptr_t element_size = sizeof(ElementType); |
205 if (len > (kIntptrMax / element_size)) { | 205 if (len > (kIntptrMax / element_size)) { |
206 FATAL2("BaseZone::Alloc: 'len' is too large: len=%ld, element_size=%ld", | 206 FATAL2("BaseZone::Alloc: 'len' is too large: len=%"Pd", element_size=%"Pd, |
207 len, element_size); | 207 len, element_size); |
208 } | 208 } |
209 return reinterpret_cast<ElementType*>(AllocUnsafe(len * element_size)); | 209 return reinterpret_cast<ElementType*>(AllocUnsafe(len * element_size)); |
210 } | 210 } |
211 | 211 |
212 template <class ElementType> | 212 template <class ElementType> |
213 inline ElementType* BaseZone::Realloc(ElementType* old_data, | 213 inline ElementType* BaseZone::Realloc(ElementType* old_data, |
214 intptr_t old_len, | 214 intptr_t old_len, |
215 intptr_t new_len) { | 215 intptr_t new_len) { |
216 ElementType* new_data = Alloc<ElementType>(new_len); | 216 ElementType* new_data = Alloc<ElementType>(new_len); |
217 if (old_data != 0) { | 217 if (old_data != 0) { |
218 memmove(reinterpret_cast<void*>(new_data), | 218 memmove(reinterpret_cast<void*>(new_data), |
219 reinterpret_cast<void*>(old_data), | 219 reinterpret_cast<void*>(old_data), |
220 Utils::Minimum(old_len * sizeof(ElementType), | 220 Utils::Minimum(old_len * sizeof(ElementType), |
221 new_len * sizeof(ElementType))); | 221 new_len * sizeof(ElementType))); |
222 } | 222 } |
223 return new_data; | 223 return new_data; |
224 } | 224 } |
225 | 225 |
226 } // namespace dart | 226 } // namespace dart |
227 | 227 |
228 #endif // VM_ZONE_H_ | 228 #endif // VM_ZONE_H_ |
OLD | NEW |