blob: 15feb9d5fb9ccceace38f05e9a695c9bdbc64acd [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
ager@chromium.org5aa501c2009-06-23 07:57:28 +000038int Heap::MaxObjectSizeInPagedSpace() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039 return Page::kMaxHeapObjectSize;
40}
41
42
lrn@chromium.org303ada72010-10-27 09:33:13 +000043MaybeObject* Heap::AllocateSymbol(Vector<const char> str,
44 int chars,
45 uint32_t hash_field) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +000046 unibrow::Utf8InputBuffer<> buffer(str.start(),
47 static_cast<unsigned>(str.length()));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000048 return AllocateInternalSymbol(&buffer, chars, hash_field);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000049}
50
51
lrn@chromium.org303ada72010-10-27 09:33:13 +000052MaybeObject* Heap::CopyFixedArray(FixedArray* src) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +000053 return CopyFixedArrayWithMap(src, src->map());
54}
55
56
lrn@chromium.org303ada72010-10-27 09:33:13 +000057MaybeObject* Heap::AllocateRaw(int size_in_bytes,
58 AllocationSpace space,
59 AllocationSpace retry_space) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000060 ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000061 ASSERT(space != NEW_SPACE ||
62 retry_space == OLD_POINTER_SPACE ||
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000063 retry_space == OLD_DATA_SPACE ||
64 retry_space == LO_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065#ifdef DEBUG
66 if (FLAG_gc_interval >= 0 &&
67 !disallow_allocation_failure_ &&
68 Heap::allocation_timeout_-- <= 0) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +000069 return Failure::RetryAfterGC(space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070 }
71 Counters::objs_since_last_full.Increment();
72 Counters::objs_since_last_young.Increment();
73#endif
lrn@chromium.org303ada72010-10-27 09:33:13 +000074 MaybeObject* result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075 if (NEW_SPACE == space) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000076 result = new_space_.AllocateRaw(size_in_bytes);
77 if (always_allocate() && result->IsFailure()) {
78 space = retry_space;
79 } else {
80 return result;
81 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082 }
83
ager@chromium.org9258b6b2008-09-11 09:11:10 +000084 if (OLD_POINTER_SPACE == space) {
85 result = old_pointer_space_->AllocateRaw(size_in_bytes);
86 } else if (OLD_DATA_SPACE == space) {
87 result = old_data_space_->AllocateRaw(size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088 } else if (CODE_SPACE == space) {
89 result = code_space_->AllocateRaw(size_in_bytes);
90 } else if (LO_SPACE == space) {
91 result = lo_space_->AllocateRaw(size_in_bytes);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +000092 } else if (CELL_SPACE == space) {
93 result = cell_space_->AllocateRaw(size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094 } else {
95 ASSERT(MAP_SPACE == space);
96 result = map_space_->AllocateRaw(size_in_bytes);
97 }
98 if (result->IsFailure()) old_gen_exhausted_ = true;
99 return result;
100}
101
102
lrn@chromium.org303ada72010-10-27 09:33:13 +0000103MaybeObject* Heap::NumberFromInt32(int32_t value) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104 if (Smi::IsValid(value)) return Smi::FromInt(value);
105 // Bypass NumberFromDouble to avoid various redundant checks.
106 return AllocateHeapNumber(FastI2D(value));
107}
108
109
lrn@chromium.org303ada72010-10-27 09:33:13 +0000110MaybeObject* Heap::NumberFromUint32(uint32_t value) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000111 if ((int32_t)value >= 0 && Smi::IsValid((int32_t)value)) {
112 return Smi::FromInt((int32_t)value);
113 }
114 // Bypass NumberFromDouble to avoid various redundant checks.
115 return AllocateHeapNumber(FastUI2D(value));
116}
117
118
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000119void Heap::FinalizeExternalString(String* string) {
120 ASSERT(string->IsExternalString());
121 v8::String::ExternalStringResourceBase** resource_addr =
122 reinterpret_cast<v8::String::ExternalStringResourceBase**>(
123 reinterpret_cast<byte*>(string) +
124 ExternalString::kResourceOffset -
125 kHeapObjectTag);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000126
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000127 // Dispose of the C++ object if it has not already been disposed.
128 if (*resource_addr != NULL) {
129 (*resource_addr)->Dispose();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000130 }
131
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000132 // Clear the resource pointer in the string.
133 *resource_addr = NULL;
134}
135
136
lrn@chromium.org303ada72010-10-27 09:33:13 +0000137MaybeObject* Heap::AllocateRawMap() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138#ifdef DEBUG
139 Counters::objs_since_last_full.Increment();
140 Counters::objs_since_last_young.Increment();
141#endif
lrn@chromium.org303ada72010-10-27 09:33:13 +0000142 MaybeObject* result = map_space_->AllocateRaw(Map::kSize);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000143 if (result->IsFailure()) old_gen_exhausted_ = true;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000144#ifdef DEBUG
145 if (!result->IsFailure()) {
146 // Maps have their own alignment.
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000147 CHECK((reinterpret_cast<intptr_t>(result) & kMapAlignmentMask) ==
148 static_cast<intptr_t>(kHeapObjectTag));
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000149 }
150#endif
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000151 return result;
152}
153
154
lrn@chromium.org303ada72010-10-27 09:33:13 +0000155MaybeObject* Heap::AllocateRawCell() {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000156#ifdef DEBUG
157 Counters::objs_since_last_full.Increment();
158 Counters::objs_since_last_young.Increment();
159#endif
lrn@chromium.org303ada72010-10-27 09:33:13 +0000160 MaybeObject* result = cell_space_->AllocateRaw(JSGlobalPropertyCell::kSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161 if (result->IsFailure()) old_gen_exhausted_ = true;
162 return result;
163}
164
165
166bool Heap::InNewSpace(Object* object) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000167 bool result = new_space_.Contains(object);
168 ASSERT(!result || // Either not in new space
169 gc_state_ != NOT_IN_GC || // ... or in the middle of GC
170 InToSpace(object)); // ... or in to-space (where we allocate).
171 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172}
173
174
175bool Heap::InFromSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000176 return new_space_.FromSpaceContains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177}
178
179
180bool Heap::InToSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000181 return new_space_.ToSpaceContains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182}
183
184
185bool Heap::ShouldBePromoted(Address old_address, int object_size) {
186 // An object should be promoted if:
187 // - the object has survived a scavenge operation or
188 // - to space is already 25% full.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000189 return old_address < new_space_.age_mark()
190 || (new_space_.Size() + object_size) >= (new_space_.Capacity() >> 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191}
192
193
194void Heap::RecordWrite(Address address, int offset) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000195 if (new_space_.Contains(address)) return;
196 ASSERT(!new_space_.FromSpaceContains(address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197 SLOW_ASSERT(Contains(address + offset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000198 Page::FromAddress(address)->MarkRegionDirty(address + offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199}
200
201
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000202void Heap::RecordWrites(Address address, int start, int len) {
203 if (new_space_.Contains(address)) return;
204 ASSERT(!new_space_.FromSpaceContains(address));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000205 Page* page = Page::FromAddress(address);
206 page->SetRegionMarks(page->GetRegionMarks() |
207 page->GetRegionMaskForSpan(address + start, len * kPointerSize));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000208}
209
210
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000211OldSpace* Heap::TargetSpace(HeapObject* object) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000212 InstanceType type = object->map()->instance_type();
213 AllocationSpace space = TargetSpaceId(type);
214 return (space == OLD_POINTER_SPACE)
215 ? old_pointer_space_
216 : old_data_space_;
217}
218
219
220AllocationSpace Heap::TargetSpaceId(InstanceType type) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000221 // Heap numbers and sequential strings are promoted to old data space, all
222 // other object types are promoted to old pointer space. We do not use
kasper.lund7276f142008-07-30 08:49:36 +0000223 // object->IsHeapNumber() and object->IsSeqString() because we already
224 // know that object has the heap object tag.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000225
226 // These objects are never allocated in new space.
227 ASSERT(type != MAP_TYPE);
228 ASSERT(type != CODE_TYPE);
229 ASSERT(type != ODDBALL_TYPE);
230 ASSERT(type != JS_GLOBAL_PROPERTY_CELL_TYPE);
231
232 if (type < FIRST_NONSTRING_TYPE) {
233 // There are three string representations: sequential strings, cons
234 // strings, and external strings. Only cons strings contain
235 // non-map-word pointers to heap objects.
236 return ((type & kStringRepresentationMask) == kConsStringTag)
237 ? OLD_POINTER_SPACE
238 : OLD_DATA_SPACE;
239 } else {
240 return (type <= LAST_DATA_TYPE) ? OLD_DATA_SPACE : OLD_POINTER_SPACE;
241 }
kasper.lund7276f142008-07-30 08:49:36 +0000242}
243
244
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000245void Heap::CopyBlock(Address dst, Address src, int byte_size) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000246 ASSERT(IsAligned(byte_size, kPointerSize));
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000247 CopyWords(reinterpret_cast<Object**>(dst),
248 reinterpret_cast<Object**>(src),
249 byte_size / kPointerSize);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000250}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000252
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000253void Heap::CopyBlockToOldSpaceAndUpdateRegionMarks(Address dst,
254 Address src,
255 int byte_size) {
256 ASSERT(IsAligned(byte_size, kPointerSize));
257
258 Page* page = Page::FromAddress(dst);
259 uint32_t marks = page->GetRegionMarks();
260
261 for (int remaining = byte_size / kPointerSize;
262 remaining > 0;
263 remaining--) {
264 Memory::Object_at(dst) = Memory::Object_at(src);
265
266 if (Heap::InNewSpace(Memory::Object_at(dst))) {
267 marks |= page->GetRegionMaskForAddress(dst);
268 }
269
270 dst += kPointerSize;
271 src += kPointerSize;
272 }
273
274 page->SetRegionMarks(marks);
275}
276
277
278void Heap::MoveBlock(Address dst, Address src, int byte_size) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000279 ASSERT(IsAligned(byte_size, kPointerSize));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000280
281 int size_in_words = byte_size / kPointerSize;
282
283 if ((dst < src) || (dst >= (src + size_in_words))) {
284 ASSERT((dst >= (src + size_in_words)) ||
285 ((OffsetFrom(reinterpret_cast<Address>(src)) -
286 OffsetFrom(reinterpret_cast<Address>(dst))) >= kPointerSize));
287
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000288 Object** src_slot = reinterpret_cast<Object**>(src);
289 Object** dst_slot = reinterpret_cast<Object**>(dst);
290 Object** end_slot = src_slot + size_in_words;
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000291
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000292 while (src_slot != end_slot) {
293 *dst_slot++ = *src_slot++;
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000294 }
295 } else {
296 memmove(dst, src, byte_size);
297 }
298}
299
300
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000301void Heap::MoveBlockToOldSpaceAndUpdateRegionMarks(Address dst,
302 Address src,
303 int byte_size) {
304 ASSERT(IsAligned(byte_size, kPointerSize));
305 ASSERT((dst >= (src + byte_size)) ||
306 ((OffsetFrom(src) - OffsetFrom(dst)) >= kPointerSize));
307
308 CopyBlockToOldSpaceAndUpdateRegionMarks(dst, src, byte_size);
309}
310
311
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000312void Heap::ScavengeObject(HeapObject** p, HeapObject* object) {
313 ASSERT(InFromSpace(object));
314
315 // We use the first word (where the map pointer usually is) of a heap
316 // object to record the forwarding pointer. A forwarding pointer can
317 // point to an old space, the code space, or the to space of the new
318 // generation.
319 MapWord first_word = object->map_word();
320
321 // If the first word is a forwarding address, the object has already been
322 // copied.
323 if (first_word.IsForwardingAddress()) {
324 *p = first_word.ToForwardingAddress();
325 return;
326 }
327
328 // Call the slow part of scavenge object.
329 return ScavengeObjectSlow(p, object);
330}
331
332
lrn@chromium.org303ada72010-10-27 09:33:13 +0000333MaybeObject* Heap::PrepareForCompare(String* str) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000334 // Always flatten small strings and force flattening of long strings
335 // after we have accumulated a certain amount we failed to flatten.
336 static const int kMaxAlwaysFlattenLength = 32;
337 static const int kFlattenLongThreshold = 16*KB;
338
339 const int length = str->length();
lrn@chromium.org303ada72010-10-27 09:33:13 +0000340 MaybeObject* obj = str->TryFlatten();
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000341 if (length <= kMaxAlwaysFlattenLength ||
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000342 unflattened_strings_length_ >= kFlattenLongThreshold) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000343 return obj;
344 }
345 if (obj->IsFailure()) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000346 unflattened_strings_length_ += length;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000347 }
348 return str;
349}
350
351
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000352int Heap::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) {
353 ASSERT(HasBeenSetup());
354 int amount = amount_of_external_allocated_memory_ + change_in_bytes;
355 if (change_in_bytes >= 0) {
356 // Avoid overflow.
357 if (amount > amount_of_external_allocated_memory_) {
358 amount_of_external_allocated_memory_ = amount;
359 }
360 int amount_since_last_global_gc =
361 amount_of_external_allocated_memory_ -
362 amount_of_external_allocated_memory_at_last_global_gc_;
363 if (amount_since_last_global_gc > external_allocation_limit_) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000364 CollectAllGarbage(false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000365 }
366 } else {
367 // Avoid underflow.
368 if (amount >= 0) {
369 amount_of_external_allocated_memory_ = amount;
370 }
371 }
372 ASSERT(amount_of_external_allocated_memory_ >= 0);
373 return amount_of_external_allocated_memory_;
374}
375
376
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000377void Heap::SetLastScriptId(Object* last_script_id) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000378 roots_[kLastScriptIdRootIndex] = last_script_id;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000379}
380
381
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000382#ifdef DEBUG
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000383#define GC_GREEDY_CHECK() \
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000384 if (FLAG_gc_greedy) v8::internal::Heap::GarbageCollectionGreedyCheck()
385#else
386#define GC_GREEDY_CHECK() { }
387#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000389
390// Calls the FUNCTION_CALL function and retries it up to three times
391// to guarantee that any allocations performed during the call will
392// succeed if there's enough memory.
393
lrn@chromium.org303ada72010-10-27 09:33:13 +0000394// Warning: Do not use the identifiers __object__, __maybe_object__ or
395// __scope__ in a call to this macro.
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000396
397#define CALL_AND_RETRY(FUNCTION_CALL, RETURN_VALUE, RETURN_EMPTY) \
398 do { \
399 GC_GREEDY_CHECK(); \
lrn@chromium.org303ada72010-10-27 09:33:13 +0000400 MaybeObject* __maybe_object__ = FUNCTION_CALL; \
401 Object* __object__ = NULL; \
402 if (__maybe_object__->ToObject(&__object__)) RETURN_VALUE; \
403 if (__maybe_object__->IsOutOfMemory()) { \
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000404 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_0", true);\
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000405 } \
lrn@chromium.org303ada72010-10-27 09:33:13 +0000406 if (!__maybe_object__->IsRetryAfterGC()) RETURN_EMPTY; \
407 Heap::CollectGarbage(Failure::cast(__maybe_object__)-> \
408 allocation_space()); \
409 __maybe_object__ = FUNCTION_CALL; \
410 if (__maybe_object__->ToObject(&__object__)) RETURN_VALUE; \
411 if (__maybe_object__->IsOutOfMemory()) { \
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000412 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_1", true);\
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000413 } \
lrn@chromium.org303ada72010-10-27 09:33:13 +0000414 if (!__maybe_object__->IsRetryAfterGC()) RETURN_EMPTY; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000415 Counters::gc_last_resort_from_handles.Increment(); \
lrn@chromium.org303ada72010-10-27 09:33:13 +0000416 Heap::CollectAllGarbage(false); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000417 { \
418 AlwaysAllocateScope __scope__; \
lrn@chromium.org303ada72010-10-27 09:33:13 +0000419 __maybe_object__ = FUNCTION_CALL; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000420 } \
lrn@chromium.org303ada72010-10-27 09:33:13 +0000421 if (__maybe_object__->ToObject(&__object__)) RETURN_VALUE; \
422 if (__maybe_object__->IsOutOfMemory() || \
423 __maybe_object__->IsRetryAfterGC()) { \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000424 /* TODO(1181417): Fix this. */ \
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000425 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_2", true);\
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000426 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000427 RETURN_EMPTY; \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428 } while (false)
429
430
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000431#define CALL_HEAP_FUNCTION(FUNCTION_CALL, TYPE) \
432 CALL_AND_RETRY(FUNCTION_CALL, \
433 return Handle<TYPE>(TYPE::cast(__object__)), \
434 return Handle<TYPE>())
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000435
436
437#define CALL_HEAP_FUNCTION_VOID(FUNCTION_CALL) \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000438 CALL_AND_RETRY(FUNCTION_CALL, return, return)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000439
440
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441#ifdef DEBUG
442
443inline bool Heap::allow_allocation(bool new_state) {
444 bool old = allocation_allowed_;
445 allocation_allowed_ = new_state;
446 return old;
447}
448
449#endif
450
451
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000452void ExternalStringTable::AddString(String* string) {
453 ASSERT(string->IsExternalString());
454 if (Heap::InNewSpace(string)) {
455 new_space_strings_.Add(string);
456 } else {
457 old_space_strings_.Add(string);
458 }
459}
460
461
462void ExternalStringTable::Iterate(ObjectVisitor* v) {
463 if (!new_space_strings_.is_empty()) {
464 Object** start = &new_space_strings_[0];
465 v->VisitPointers(start, start + new_space_strings_.length());
466 }
467 if (!old_space_strings_.is_empty()) {
468 Object** start = &old_space_strings_[0];
469 v->VisitPointers(start, start + old_space_strings_.length());
470 }
471}
472
473
474// Verify() is inline to avoid ifdef-s around its calls in release
475// mode.
476void ExternalStringTable::Verify() {
477#ifdef DEBUG
478 for (int i = 0; i < new_space_strings_.length(); ++i) {
479 ASSERT(Heap::InNewSpace(new_space_strings_[i]));
480 ASSERT(new_space_strings_[i] != Heap::raw_unchecked_null_value());
481 }
482 for (int i = 0; i < old_space_strings_.length(); ++i) {
483 ASSERT(!Heap::InNewSpace(old_space_strings_[i]));
484 ASSERT(old_space_strings_[i] != Heap::raw_unchecked_null_value());
485 }
486#endif
487}
488
489
490void ExternalStringTable::AddOldString(String* string) {
491 ASSERT(string->IsExternalString());
492 ASSERT(!Heap::InNewSpace(string));
493 old_space_strings_.Add(string);
494}
495
496
497void ExternalStringTable::ShrinkNewStrings(int position) {
498 new_space_strings_.Rewind(position);
499 Verify();
500}
501
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502} } // namespace v8::internal
503
504#endif // V8_HEAP_INL_H_