blob: b68f5c1ca43b76a2875cc5d967e6ce796859dd23 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_HEAP_INL_H_
29#define V8_HEAP_INL_H_
30
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000031#include "heap.h"
32#include "objects.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#include "v8-counters.h"
34
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035namespace v8 {
36namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +000038void Heap::UpdateOldSpaceLimits() {
39 int old_gen_size = PromotedSpaceSize();
40 old_gen_promotion_limit_ =
41 old_gen_size + Max(kMinimumPromotionLimit, old_gen_size / 3);
42 old_gen_allocation_limit_ =
43 old_gen_size + Max(kMinimumAllocationLimit, old_gen_size / 2);
44 old_gen_exhausted_ = false;
45}
46
47
ager@chromium.org5aa501c2009-06-23 07:57:28 +000048int Heap::MaxObjectSizeInPagedSpace() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000049 return Page::kMaxHeapObjectSize;
50}
51
52
ager@chromium.orga74f0da2008-12-03 16:05:52 +000053Object* Heap::AllocateSymbol(Vector<const char> str,
54 int chars,
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000055 uint32_t hash_field) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +000056 unibrow::Utf8InputBuffer<> buffer(str.start(),
57 static_cast<unsigned>(str.length()));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000058 return AllocateInternalSymbol(&buffer, chars, hash_field);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000059}
60
61
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +000062Object* Heap::CopyFixedArray(FixedArray* src) {
63 return CopyFixedArrayWithMap(src, src->map());
64}
65
66
ager@chromium.org9258b6b2008-09-11 09:11:10 +000067Object* Heap::AllocateRaw(int size_in_bytes,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000068 AllocationSpace space,
69 AllocationSpace retry_space) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070 ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000071 ASSERT(space != NEW_SPACE ||
72 retry_space == OLD_POINTER_SPACE ||
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000073 retry_space == OLD_DATA_SPACE ||
74 retry_space == LO_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075#ifdef DEBUG
76 if (FLAG_gc_interval >= 0 &&
77 !disallow_allocation_failure_ &&
78 Heap::allocation_timeout_-- <= 0) {
79 return Failure::RetryAfterGC(size_in_bytes, space);
80 }
81 Counters::objs_since_last_full.Increment();
82 Counters::objs_since_last_young.Increment();
83#endif
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000084 Object* result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000085 if (NEW_SPACE == space) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000086 result = new_space_.AllocateRaw(size_in_bytes);
87 if (always_allocate() && result->IsFailure()) {
88 space = retry_space;
89 } else {
90 return result;
91 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092 }
93
ager@chromium.org9258b6b2008-09-11 09:11:10 +000094 if (OLD_POINTER_SPACE == space) {
95 result = old_pointer_space_->AllocateRaw(size_in_bytes);
96 } else if (OLD_DATA_SPACE == space) {
97 result = old_data_space_->AllocateRaw(size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098 } else if (CODE_SPACE == space) {
99 result = code_space_->AllocateRaw(size_in_bytes);
100 } else if (LO_SPACE == space) {
101 result = lo_space_->AllocateRaw(size_in_bytes);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000102 } else if (CELL_SPACE == space) {
103 result = cell_space_->AllocateRaw(size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104 } else {
105 ASSERT(MAP_SPACE == space);
106 result = map_space_->AllocateRaw(size_in_bytes);
107 }
108 if (result->IsFailure()) old_gen_exhausted_ = true;
109 return result;
110}
111
112
113Object* Heap::NumberFromInt32(int32_t value) {
114 if (Smi::IsValid(value)) return Smi::FromInt(value);
115 // Bypass NumberFromDouble to avoid various redundant checks.
116 return AllocateHeapNumber(FastI2D(value));
117}
118
119
120Object* Heap::NumberFromUint32(uint32_t value) {
121 if ((int32_t)value >= 0 && Smi::IsValid((int32_t)value)) {
122 return Smi::FromInt((int32_t)value);
123 }
124 // Bypass NumberFromDouble to avoid various redundant checks.
125 return AllocateHeapNumber(FastUI2D(value));
126}
127
128
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000129void Heap::FinalizeExternalString(String* string) {
130 ASSERT(string->IsExternalString());
131 v8::String::ExternalStringResourceBase** resource_addr =
132 reinterpret_cast<v8::String::ExternalStringResourceBase**>(
133 reinterpret_cast<byte*>(string) +
134 ExternalString::kResourceOffset -
135 kHeapObjectTag);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000136
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000137 // Dispose of the C++ object if it has not already been disposed.
138 if (*resource_addr != NULL) {
139 (*resource_addr)->Dispose();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000140 }
141
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000142 // Clear the resource pointer in the string.
143 *resource_addr = NULL;
144}
145
146
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000147Object* Heap::AllocateRawMap() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148#ifdef DEBUG
149 Counters::objs_since_last_full.Increment();
150 Counters::objs_since_last_young.Increment();
151#endif
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000152 Object* result = map_space_->AllocateRaw(Map::kSize);
153 if (result->IsFailure()) old_gen_exhausted_ = true;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000154#ifdef DEBUG
155 if (!result->IsFailure()) {
156 // Maps have their own alignment.
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000157 CHECK((reinterpret_cast<intptr_t>(result) & kMapAlignmentMask) ==
158 static_cast<intptr_t>(kHeapObjectTag));
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000159 }
160#endif
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000161 return result;
162}
163
164
165Object* Heap::AllocateRawCell() {
166#ifdef DEBUG
167 Counters::objs_since_last_full.Increment();
168 Counters::objs_since_last_young.Increment();
169#endif
170 Object* result = cell_space_->AllocateRaw(JSGlobalPropertyCell::kSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171 if (result->IsFailure()) old_gen_exhausted_ = true;
172 return result;
173}
174
175
176bool Heap::InNewSpace(Object* object) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000177 bool result = new_space_.Contains(object);
178 ASSERT(!result || // Either not in new space
179 gc_state_ != NOT_IN_GC || // ... or in the middle of GC
180 InToSpace(object)); // ... or in to-space (where we allocate).
181 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182}
183
184
185bool Heap::InFromSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000186 return new_space_.FromSpaceContains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187}
188
189
190bool Heap::InToSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000191 return new_space_.ToSpaceContains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000192}
193
194
195bool Heap::ShouldBePromoted(Address old_address, int object_size) {
196 // An object should be promoted if:
197 // - the object has survived a scavenge operation or
198 // - to space is already 25% full.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000199 return old_address < new_space_.age_mark()
200 || (new_space_.Size() + object_size) >= (new_space_.Capacity() >> 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201}
202
203
204void Heap::RecordWrite(Address address, int offset) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000205 if (new_space_.Contains(address)) return;
206 ASSERT(!new_space_.FromSpaceContains(address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207 SLOW_ASSERT(Contains(address + offset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000208 Page::FromAddress(address)->MarkRegionDirty(address + offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209}
210
211
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000212void Heap::RecordWrites(Address address, int start, int len) {
213 if (new_space_.Contains(address)) return;
214 ASSERT(!new_space_.FromSpaceContains(address));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000215 Page* page = Page::FromAddress(address);
216 page->SetRegionMarks(page->GetRegionMarks() |
217 page->GetRegionMaskForSpan(address + start, len * kPointerSize));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000218}
219
220
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000221OldSpace* Heap::TargetSpace(HeapObject* object) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000222 InstanceType type = object->map()->instance_type();
223 AllocationSpace space = TargetSpaceId(type);
224 return (space == OLD_POINTER_SPACE)
225 ? old_pointer_space_
226 : old_data_space_;
227}
228
229
230AllocationSpace Heap::TargetSpaceId(InstanceType type) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000231 // Heap numbers and sequential strings are promoted to old data space, all
232 // other object types are promoted to old pointer space. We do not use
kasper.lund7276f142008-07-30 08:49:36 +0000233 // object->IsHeapNumber() and object->IsSeqString() because we already
234 // know that object has the heap object tag.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000235
236 // These objects are never allocated in new space.
237 ASSERT(type != MAP_TYPE);
238 ASSERT(type != CODE_TYPE);
239 ASSERT(type != ODDBALL_TYPE);
240 ASSERT(type != JS_GLOBAL_PROPERTY_CELL_TYPE);
241
242 if (type < FIRST_NONSTRING_TYPE) {
243 // There are three string representations: sequential strings, cons
244 // strings, and external strings. Only cons strings contain
245 // non-map-word pointers to heap objects.
246 return ((type & kStringRepresentationMask) == kConsStringTag)
247 ? OLD_POINTER_SPACE
248 : OLD_DATA_SPACE;
249 } else {
250 return (type <= LAST_DATA_TYPE) ? OLD_DATA_SPACE : OLD_POINTER_SPACE;
251 }
kasper.lund7276f142008-07-30 08:49:36 +0000252}
253
254
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000255void Heap::CopyBlock(Address dst, Address src, int byte_size) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000256 ASSERT(IsAligned(byte_size, kPointerSize));
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000257 CopyWords(reinterpret_cast<Object**>(dst),
258 reinterpret_cast<Object**>(src),
259 byte_size / kPointerSize);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000260}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000262
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000263void Heap::CopyBlockToOldSpaceAndUpdateRegionMarks(Address dst,
264 Address src,
265 int byte_size) {
266 ASSERT(IsAligned(byte_size, kPointerSize));
267
268 Page* page = Page::FromAddress(dst);
269 uint32_t marks = page->GetRegionMarks();
270
271 for (int remaining = byte_size / kPointerSize;
272 remaining > 0;
273 remaining--) {
274 Memory::Object_at(dst) = Memory::Object_at(src);
275
276 if (Heap::InNewSpace(Memory::Object_at(dst))) {
277 marks |= page->GetRegionMaskForAddress(dst);
278 }
279
280 dst += kPointerSize;
281 src += kPointerSize;
282 }
283
284 page->SetRegionMarks(marks);
285}
286
287
288void Heap::MoveBlock(Address dst, Address src, int byte_size) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000289 ASSERT(IsAligned(byte_size, kPointerSize));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000290
291 int size_in_words = byte_size / kPointerSize;
292
293 if ((dst < src) || (dst >= (src + size_in_words))) {
294 ASSERT((dst >= (src + size_in_words)) ||
295 ((OffsetFrom(reinterpret_cast<Address>(src)) -
296 OffsetFrom(reinterpret_cast<Address>(dst))) >= kPointerSize));
297
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000298 Object** src_slot = reinterpret_cast<Object**>(src);
299 Object** dst_slot = reinterpret_cast<Object**>(dst);
300 Object** end_slot = src_slot + size_in_words;
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000301
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000302 while (src_slot != end_slot) {
303 *dst_slot++ = *src_slot++;
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000304 }
305 } else {
306 memmove(dst, src, byte_size);
307 }
308}
309
310
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000311void Heap::MoveBlockToOldSpaceAndUpdateRegionMarks(Address dst,
312 Address src,
313 int byte_size) {
314 ASSERT(IsAligned(byte_size, kPointerSize));
315 ASSERT((dst >= (src + byte_size)) ||
316 ((OffsetFrom(src) - OffsetFrom(dst)) >= kPointerSize));
317
318 CopyBlockToOldSpaceAndUpdateRegionMarks(dst, src, byte_size);
319}
320
321
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000322void Heap::ScavengeObject(HeapObject** p, HeapObject* object) {
323 ASSERT(InFromSpace(object));
324
325 // We use the first word (where the map pointer usually is) of a heap
326 // object to record the forwarding pointer. A forwarding pointer can
327 // point to an old space, the code space, or the to space of the new
328 // generation.
329 MapWord first_word = object->map_word();
330
331 // If the first word is a forwarding address, the object has already been
332 // copied.
333 if (first_word.IsForwardingAddress()) {
334 *p = first_word.ToForwardingAddress();
335 return;
336 }
337
338 // Call the slow part of scavenge object.
339 return ScavengeObjectSlow(p, object);
340}
341
342
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000343Object* Heap::PrepareForCompare(String* str) {
344 // Always flatten small strings and force flattening of long strings
345 // after we have accumulated a certain amount we failed to flatten.
346 static const int kMaxAlwaysFlattenLength = 32;
347 static const int kFlattenLongThreshold = 16*KB;
348
349 const int length = str->length();
350 Object* obj = str->TryFlatten();
351 if (length <= kMaxAlwaysFlattenLength ||
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000352 unflattened_strings_length_ >= kFlattenLongThreshold) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000353 return obj;
354 }
355 if (obj->IsFailure()) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000356 unflattened_strings_length_ += length;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000357 }
358 return str;
359}
360
361
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000362int Heap::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) {
363 ASSERT(HasBeenSetup());
364 int amount = amount_of_external_allocated_memory_ + change_in_bytes;
365 if (change_in_bytes >= 0) {
366 // Avoid overflow.
367 if (amount > amount_of_external_allocated_memory_) {
368 amount_of_external_allocated_memory_ = amount;
369 }
370 int amount_since_last_global_gc =
371 amount_of_external_allocated_memory_ -
372 amount_of_external_allocated_memory_at_last_global_gc_;
373 if (amount_since_last_global_gc > external_allocation_limit_) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000374 CollectAllGarbage(false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000375 }
376 } else {
377 // Avoid underflow.
378 if (amount >= 0) {
379 amount_of_external_allocated_memory_ = amount;
380 }
381 }
382 ASSERT(amount_of_external_allocated_memory_ >= 0);
383 return amount_of_external_allocated_memory_;
384}
385
386
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000387void Heap::SetLastScriptId(Object* last_script_id) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000388 roots_[kLastScriptIdRootIndex] = last_script_id;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000389}
390
391
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000392#define GC_GREEDY_CHECK() \
393 ASSERT(!FLAG_gc_greedy || v8::internal::Heap::GarbageCollectionGreedyCheck())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000395
396// Calls the FUNCTION_CALL function and retries it up to three times
397// to guarantee that any allocations performed during the call will
398// succeed if there's enough memory.
399
400// Warning: Do not use the identifiers __object__ or __scope__ in a
401// call to this macro.
402
403#define CALL_AND_RETRY(FUNCTION_CALL, RETURN_VALUE, RETURN_EMPTY) \
404 do { \
405 GC_GREEDY_CHECK(); \
406 Object* __object__ = FUNCTION_CALL; \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000407 if (!__object__->IsFailure()) RETURN_VALUE; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000408 if (__object__->IsOutOfMemoryFailure()) { \
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000409 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_0", true);\
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000410 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000411 if (!__object__->IsRetryAfterGC()) RETURN_EMPTY; \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000412 Heap::CollectGarbage(Failure::cast(__object__)->requested(), \
413 Failure::cast(__object__)->allocation_space()); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000414 __object__ = FUNCTION_CALL; \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000415 if (!__object__->IsFailure()) RETURN_VALUE; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000416 if (__object__->IsOutOfMemoryFailure()) { \
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000417 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_1", true);\
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000418 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000419 if (!__object__->IsRetryAfterGC()) RETURN_EMPTY; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000420 Counters::gc_last_resort_from_handles.Increment(); \
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000421 Heap::CollectAllAvailableGarbage(); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000422 { \
423 AlwaysAllocateScope __scope__; \
424 __object__ = FUNCTION_CALL; \
425 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000426 if (!__object__->IsFailure()) RETURN_VALUE; \
ager@chromium.org9085a012009-05-11 19:22:57 +0000427 if (__object__->IsOutOfMemoryFailure() || \
428 __object__->IsRetryAfterGC()) { \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000429 /* TODO(1181417): Fix this. */ \
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000430 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_2", true);\
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000431 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000432 RETURN_EMPTY; \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 } while (false)
434
435
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000436#define CALL_HEAP_FUNCTION(FUNCTION_CALL, TYPE) \
437 CALL_AND_RETRY(FUNCTION_CALL, \
438 return Handle<TYPE>(TYPE::cast(__object__)), \
439 return Handle<TYPE>())
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000440
441
442#define CALL_HEAP_FUNCTION_VOID(FUNCTION_CALL) \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000443 CALL_AND_RETRY(FUNCTION_CALL, return, return)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000444
445
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446#ifdef DEBUG
447
448inline bool Heap::allow_allocation(bool new_state) {
449 bool old = allocation_allowed_;
450 allocation_allowed_ = new_state;
451 return old;
452}
453
454#endif
455
456
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000457void ExternalStringTable::AddString(String* string) {
458 ASSERT(string->IsExternalString());
459 if (Heap::InNewSpace(string)) {
460 new_space_strings_.Add(string);
461 } else {
462 old_space_strings_.Add(string);
463 }
464}
465
466
467void ExternalStringTable::Iterate(ObjectVisitor* v) {
468 if (!new_space_strings_.is_empty()) {
469 Object** start = &new_space_strings_[0];
470 v->VisitPointers(start, start + new_space_strings_.length());
471 }
472 if (!old_space_strings_.is_empty()) {
473 Object** start = &old_space_strings_[0];
474 v->VisitPointers(start, start + old_space_strings_.length());
475 }
476}
477
478
479// Verify() is inline to avoid ifdef-s around its calls in release
480// mode.
481void ExternalStringTable::Verify() {
482#ifdef DEBUG
483 for (int i = 0; i < new_space_strings_.length(); ++i) {
484 ASSERT(Heap::InNewSpace(new_space_strings_[i]));
485 ASSERT(new_space_strings_[i] != Heap::raw_unchecked_null_value());
486 }
487 for (int i = 0; i < old_space_strings_.length(); ++i) {
488 ASSERT(!Heap::InNewSpace(old_space_strings_[i]));
489 ASSERT(old_space_strings_[i] != Heap::raw_unchecked_null_value());
490 }
491#endif
492}
493
494
495void ExternalStringTable::AddOldString(String* string) {
496 ASSERT(string->IsExternalString());
497 ASSERT(!Heap::InNewSpace(string));
498 old_space_strings_.Add(string);
499}
500
501
502void ExternalStringTable::ShrinkNewStrings(int position) {
503 new_space_strings_.Rewind(position);
504 Verify();
505}
506
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000507} } // namespace v8::internal
508
509#endif // V8_HEAP_INL_H_