1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
28 #ifndef V8_HEAP_INL_H_
29 #define V8_HEAP_INL_H_
35 #include "v8-counters.h"
36 #include "store-buffer.h"
37 #include "store-buffer-inl.h"
42 void PromotionQueue::insert(HeapObject* target, int size) {
43 if (NewSpacePage::IsAtStart(reinterpret_cast<Address>(rear_))) {
44 NewSpacePage* rear_page =
45 NewSpacePage::FromAddress(reinterpret_cast<Address>(rear_));
46 ASSERT(!rear_page->prev_page()->is_anchor());
47 rear_ = reinterpret_cast<intptr_t*>(rear_page->prev_page()->body_limit());
49 *(--rear_) = reinterpret_cast<intptr_t>(target);
51 // Assert no overflow into live objects.
53 SemiSpace::AssertValidRange(HEAP->new_space()->top(),
54 reinterpret_cast<Address>(rear_));
59 int Heap::MaxObjectSizeInPagedSpace() {
60 return Page::kMaxHeapObjectSize;
64 MaybeObject* Heap::AllocateStringFromUtf8(Vector<const char> str,
65 PretenureFlag pretenure) {
66 // Check for ASCII first since this is the common case.
67 if (String::IsAscii(str.start(), str.length())) {
68 // If the string is ASCII, we do not need to convert the characters
69 // since UTF8 is backwards compatible with ASCII.
70 return AllocateStringFromAscii(str, pretenure);
72 // Non-ASCII and we need to decode.
73 return AllocateStringFromUtf8Slow(str, pretenure);
77 MaybeObject* Heap::AllocateSymbol(Vector<const char> str,
79 uint32_t hash_field) {
80 unibrow::Utf8InputBuffer<> buffer(str.start(),
81 static_cast<unsigned>(str.length()));
82 return AllocateInternalSymbol(&buffer, chars, hash_field);
86 MaybeObject* Heap::AllocateAsciiSymbol(Vector<const char> str,
87 uint32_t hash_field) {
88 if (str.length() > SeqAsciiString::kMaxLength) {
89 return Failure::OutOfMemoryException();
91 // Compute map and object size.
92 Map* map = ascii_symbol_map();
93 int size = SeqAsciiString::SizeFor(str.length());
97 { MaybeObject* maybe_result = (size > MaxObjectSizeInPagedSpace())
98 ? lo_space_->AllocateRaw(size, NOT_EXECUTABLE)
99 : old_data_space_->AllocateRaw(size);
100 if (!maybe_result->ToObject(&result)) return maybe_result;
103 reinterpret_cast<HeapObject*>(result)->set_map(map);
104 // Set length and hash fields of the allocated string.
105 String* answer = String::cast(result);
106 answer->set_length(str.length());
107 answer->set_hash_field(hash_field);
108 SeqString::cast(answer)->set_symbol_id(0);
110 ASSERT_EQ(size, answer->Size());
112 // Fill in the characters.
113 memcpy(answer->address() + SeqAsciiString::kHeaderSize,
114 str.start(), str.length());
120 MaybeObject* Heap::AllocateTwoByteSymbol(Vector<const uc16> str,
121 uint32_t hash_field) {
122 if (str.length() > SeqTwoByteString::kMaxLength) {
123 return Failure::OutOfMemoryException();
125 // Compute map and object size.
126 Map* map = symbol_map();
127 int size = SeqTwoByteString::SizeFor(str.length());
131 { MaybeObject* maybe_result = (size > MaxObjectSizeInPagedSpace())
132 ? lo_space_->AllocateRaw(size, NOT_EXECUTABLE)
133 : old_data_space_->AllocateRaw(size);
134 if (!maybe_result->ToObject(&result)) return maybe_result;
137 reinterpret_cast<HeapObject*>(result)->set_map(map);
138 // Set length and hash fields of the allocated string.
139 String* answer = String::cast(result);
140 answer->set_length(str.length());
141 answer->set_hash_field(hash_field);
142 SeqString::cast(answer)->set_symbol_id(0);
144 ASSERT_EQ(size, answer->Size());
146 // Fill in the characters.
147 memcpy(answer->address() + SeqTwoByteString::kHeaderSize,
148 str.start(), str.length() * kUC16Size);
153 MaybeObject* Heap::CopyFixedArray(FixedArray* src) {
154 return CopyFixedArrayWithMap(src, src->map());
158 MaybeObject* Heap::CopyFixedDoubleArray(FixedDoubleArray* src) {
159 return CopyFixedDoubleArrayWithMap(src, src->map());
163 MaybeObject* Heap::AllocateRaw(int size_in_bytes,
164 AllocationSpace space,
165 AllocationSpace retry_space) {
166 ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC);
167 ASSERT(space != NEW_SPACE ||
168 retry_space == OLD_POINTER_SPACE ||
169 retry_space == OLD_DATA_SPACE ||
170 retry_space == LO_SPACE);
172 if (FLAG_gc_interval >= 0 &&
173 !disallow_allocation_failure_ &&
174 Heap::allocation_timeout_-- <= 0) {
175 return Failure::RetryAfterGC(space);
177 isolate_->counters()->objs_since_last_full()->Increment();
178 isolate_->counters()->objs_since_last_young()->Increment();
181 if (NEW_SPACE == space) {
182 result = new_space_.AllocateRaw(size_in_bytes);
183 if (always_allocate() && result->IsFailure()) {
190 if (OLD_POINTER_SPACE == space) {
191 result = old_pointer_space_->AllocateRaw(size_in_bytes);
192 } else if (OLD_DATA_SPACE == space) {
193 result = old_data_space_->AllocateRaw(size_in_bytes);
194 } else if (CODE_SPACE == space) {
195 result = code_space_->AllocateRaw(size_in_bytes);
196 } else if (LO_SPACE == space) {
197 result = lo_space_->AllocateRaw(size_in_bytes, NOT_EXECUTABLE);
198 } else if (CELL_SPACE == space) {
199 result = cell_space_->AllocateRaw(size_in_bytes);
201 ASSERT(MAP_SPACE == space);
202 result = map_space_->AllocateRaw(size_in_bytes);
204 if (result->IsFailure()) old_gen_exhausted_ = true;
209 MaybeObject* Heap::NumberFromInt32(int32_t value) {
210 if (Smi::IsValid(value)) return Smi::FromInt(value);
211 // Bypass NumberFromDouble to avoid various redundant checks.
212 return AllocateHeapNumber(FastI2D(value));
216 MaybeObject* Heap::NumberFromUint32(uint32_t value) {
217 if ((int32_t)value >= 0 && Smi::IsValid((int32_t)value)) {
218 return Smi::FromInt((int32_t)value);
220 // Bypass NumberFromDouble to avoid various redundant checks.
221 return AllocateHeapNumber(FastUI2D(value));
225 void Heap::FinalizeExternalString(String* string) {
226 ASSERT(string->IsExternalString());
227 v8::String::ExternalStringResourceBase** resource_addr =
228 reinterpret_cast<v8::String::ExternalStringResourceBase**>(
229 reinterpret_cast<byte*>(string) +
230 ExternalString::kResourceOffset -
233 // Dispose of the C++ object if it has not already been disposed.
234 if (*resource_addr != NULL) {
235 (*resource_addr)->Dispose();
238 // Clear the resource pointer in the string.
239 *resource_addr = NULL;
243 MaybeObject* Heap::AllocateRawMap() {
245 isolate_->counters()->objs_since_last_full()->Increment();
246 isolate_->counters()->objs_since_last_young()->Increment();
248 MaybeObject* result = map_space_->AllocateRaw(Map::kSize);
249 if (result->IsFailure()) old_gen_exhausted_ = true;
251 if (!result->IsFailure()) {
252 // Maps have their own alignment.
253 CHECK((reinterpret_cast<intptr_t>(result) & kMapAlignmentMask) ==
254 static_cast<intptr_t>(kHeapObjectTag));
261 MaybeObject* Heap::AllocateRawCell() {
263 isolate_->counters()->objs_since_last_full()->Increment();
264 isolate_->counters()->objs_since_last_young()->Increment();
266 MaybeObject* result = cell_space_->AllocateRaw(JSGlobalPropertyCell::kSize);
267 if (result->IsFailure()) old_gen_exhausted_ = true;
272 bool Heap::InNewSpace(Object* object) {
273 bool result = new_space_.Contains(object);
274 ASSERT(!result || // Either not in new space
275 gc_state_ != NOT_IN_GC || // ... or in the middle of GC
276 InToSpace(object)); // ... or in to-space (where we allocate).
281 bool Heap::InNewSpace(Address addr) {
282 return new_space_.Contains(addr);
286 bool Heap::InFromSpace(Object* object) {
287 return new_space_.FromSpaceContains(object);
291 bool Heap::InToSpace(Object* object) {
292 return new_space_.ToSpaceContains(object);
296 bool Heap::OldGenerationAllocationLimitReached() {
297 if (!incremental_marking()->IsStopped()) return false;
298 return OldGenerationSpaceAvailable() < 0;
302 bool Heap::ShouldBePromoted(Address old_address, int object_size) {
303 // An object should be promoted if:
304 // - the object has survived a scavenge operation or
305 // - to space is already 25% full.
306 NewSpacePage* page = NewSpacePage::FromAddress(old_address);
307 Address age_mark = new_space_.age_mark();
308 bool below_mark = page->IsFlagSet(MemoryChunk::NEW_SPACE_BELOW_AGE_MARK) &&
309 (!page->ContainsLimit(age_mark) || old_address < age_mark);
310 return below_mark || (new_space_.Size() + object_size) >=
311 (new_space_.EffectiveCapacity() >> 2);
315 void Heap::RecordWrite(Address address, int offset) {
316 if (!InNewSpace(address)) store_buffer_.Mark(address + offset);
320 void Heap::RecordWrites(Address address, int start, int len) {
321 if (!InNewSpace(address)) {
322 for (int i = 0; i < len; i++) {
323 store_buffer_.Mark(address + start + i * kPointerSize);
329 OldSpace* Heap::TargetSpace(HeapObject* object) {
330 InstanceType type = object->map()->instance_type();
331 AllocationSpace space = TargetSpaceId(type);
332 return (space == OLD_POINTER_SPACE)
338 AllocationSpace Heap::TargetSpaceId(InstanceType type) {
339 // Heap numbers and sequential strings are promoted to old data space, all
340 // other object types are promoted to old pointer space. We do not use
341 // object->IsHeapNumber() and object->IsSeqString() because we already
342 // know that object has the heap object tag.
344 // These objects are never allocated in new space.
345 ASSERT(type != MAP_TYPE);
346 ASSERT(type != CODE_TYPE);
347 ASSERT(type != ODDBALL_TYPE);
348 ASSERT(type != JS_GLOBAL_PROPERTY_CELL_TYPE);
350 if (type < FIRST_NONSTRING_TYPE) {
351 // There are four string representations: sequential strings, external
352 // strings, cons strings, and sliced strings.
353 // Only the latter two contain non-map-word pointers to heap objects.
354 return ((type & kIsIndirectStringMask) == kIsIndirectStringTag)
358 return (type <= LAST_DATA_TYPE) ? OLD_DATA_SPACE : OLD_POINTER_SPACE;
363 void Heap::CopyBlock(Address dst, Address src, int byte_size) {
364 CopyWords(reinterpret_cast<Object**>(dst),
365 reinterpret_cast<Object**>(src),
366 byte_size / kPointerSize);
370 void Heap::MoveBlock(Address dst, Address src, int byte_size) {
371 ASSERT(IsAligned(byte_size, kPointerSize));
373 int size_in_words = byte_size / kPointerSize;
375 if ((dst < src) || (dst >= (src + byte_size))) {
376 Object** src_slot = reinterpret_cast<Object**>(src);
377 Object** dst_slot = reinterpret_cast<Object**>(dst);
378 Object** end_slot = src_slot + size_in_words;
380 while (src_slot != end_slot) {
381 *dst_slot++ = *src_slot++;
384 memmove(dst, src, byte_size);
389 void Heap::ScavengePointer(HeapObject** p) {
390 ScavengeObject(p, *p);
394 void Heap::ScavengeObject(HeapObject** p, HeapObject* object) {
395 ASSERT(HEAP->InFromSpace(object));
397 // We use the first word (where the map pointer usually is) of a heap
398 // object to record the forwarding pointer. A forwarding pointer can
399 // point to an old space, the code space, or the to space of the new
401 MapWord first_word = object->map_word();
403 // If the first word is a forwarding address, the object has already been
405 if (first_word.IsForwardingAddress()) {
406 HeapObject* dest = first_word.ToForwardingAddress();
407 ASSERT(HEAP->InFromSpace(*p));
412 // Call the slow part of scavenge object.
413 return ScavengeObjectSlow(p, object);
417 bool Heap::CollectGarbage(AllocationSpace space) {
418 return CollectGarbage(space, SelectGarbageCollector(space));
422 MaybeObject* Heap::PrepareForCompare(String* str) {
423 // Always flatten small strings and force flattening of long strings
424 // after we have accumulated a certain amount we failed to flatten.
425 static const int kMaxAlwaysFlattenLength = 32;
426 static const int kFlattenLongThreshold = 16*KB;
428 const int length = str->length();
429 MaybeObject* obj = str->TryFlatten();
430 if (length <= kMaxAlwaysFlattenLength ||
431 unflattened_strings_length_ >= kFlattenLongThreshold) {
434 if (obj->IsFailure()) {
435 unflattened_strings_length_ += length;
441 int Heap::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) {
442 ASSERT(HasBeenSetup());
443 int amount = amount_of_external_allocated_memory_ + change_in_bytes;
444 if (change_in_bytes >= 0) {
446 if (amount > amount_of_external_allocated_memory_) {
447 amount_of_external_allocated_memory_ = amount;
449 int amount_since_last_global_gc =
450 amount_of_external_allocated_memory_ -
451 amount_of_external_allocated_memory_at_last_global_gc_;
452 if (amount_since_last_global_gc > external_allocation_limit_) {
453 CollectAllGarbage(kNoGCFlags);
458 amount_of_external_allocated_memory_ = amount;
461 ASSERT(amount_of_external_allocated_memory_ >= 0);
462 return amount_of_external_allocated_memory_;
466 void Heap::SetLastScriptId(Object* last_script_id) {
467 roots_[kLastScriptIdRootIndex] = last_script_id;
471 Isolate* Heap::isolate() {
472 return reinterpret_cast<Isolate*>(reinterpret_cast<intptr_t>(this) -
473 reinterpret_cast<size_t>(reinterpret_cast<Isolate*>(4)->heap()) + 4);
478 #define GC_GREEDY_CHECK() \
479 if (FLAG_gc_greedy) HEAP->GarbageCollectionGreedyCheck()
481 #define GC_GREEDY_CHECK() { }
485 // Calls the FUNCTION_CALL function and retries it up to three times
486 // to guarantee that any allocations performed during the call will
487 // succeed if there's enough memory.
489 // Warning: Do not use the identifiers __object__, __maybe_object__ or
490 // __scope__ in a call to this macro.
492 #define CALL_AND_RETRY(ISOLATE, FUNCTION_CALL, RETURN_VALUE, RETURN_EMPTY)\
495 MaybeObject* __maybe_object__ = FUNCTION_CALL; \
496 Object* __object__ = NULL; \
497 if (__maybe_object__->ToObject(&__object__)) RETURN_VALUE; \
498 if (__maybe_object__->IsOutOfMemory()) { \
499 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_0", true);\
501 if (!__maybe_object__->IsRetryAfterGC()) RETURN_EMPTY; \
502 ISOLATE->heap()->CollectGarbage(Failure::cast(__maybe_object__)-> \
503 allocation_space()); \
504 __maybe_object__ = FUNCTION_CALL; \
505 if (__maybe_object__->ToObject(&__object__)) RETURN_VALUE; \
506 if (__maybe_object__->IsOutOfMemory()) { \
507 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_1", true);\
509 if (!__maybe_object__->IsRetryAfterGC()) RETURN_EMPTY; \
510 ISOLATE->counters()->gc_last_resort_from_handles()->Increment(); \
511 ISOLATE->heap()->CollectAllAvailableGarbage(); \
513 AlwaysAllocateScope __scope__; \
514 __maybe_object__ = FUNCTION_CALL; \
516 if (__maybe_object__->ToObject(&__object__)) RETURN_VALUE; \
517 if (__maybe_object__->IsOutOfMemory() || \
518 __maybe_object__->IsRetryAfterGC()) { \
519 /* TODO(1181417): Fix this. */ \
520 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_2", true);\
526 #define CALL_HEAP_FUNCTION(ISOLATE, FUNCTION_CALL, TYPE) \
527 CALL_AND_RETRY(ISOLATE, \
529 return Handle<TYPE>(TYPE::cast(__object__), ISOLATE), \
530 return Handle<TYPE>())
533 #define CALL_HEAP_FUNCTION_VOID(ISOLATE, FUNCTION_CALL) \
534 CALL_AND_RETRY(ISOLATE, FUNCTION_CALL, return, return)
539 inline bool Heap::allow_allocation(bool new_state) {
540 bool old = allocation_allowed_;
541 allocation_allowed_ = new_state;
548 void ExternalStringTable::AddString(String* string) {
549 ASSERT(string->IsExternalString());
550 if (heap_->InNewSpace(string)) {
551 new_space_strings_.Add(string);
553 old_space_strings_.Add(string);
558 void ExternalStringTable::Iterate(ObjectVisitor* v) {
559 if (!new_space_strings_.is_empty()) {
560 Object** start = &new_space_strings_[0];
561 v->VisitPointers(start, start + new_space_strings_.length());
563 if (!old_space_strings_.is_empty()) {
564 Object** start = &old_space_strings_[0];
565 v->VisitPointers(start, start + old_space_strings_.length());
570 // Verify() is inline to avoid ifdef-s around its calls in release
572 void ExternalStringTable::Verify() {
574 for (int i = 0; i < new_space_strings_.length(); ++i) {
575 ASSERT(heap_->InNewSpace(new_space_strings_[i]));
576 ASSERT(new_space_strings_[i] != HEAP->raw_unchecked_null_value());
578 for (int i = 0; i < old_space_strings_.length(); ++i) {
579 ASSERT(!heap_->InNewSpace(old_space_strings_[i]));
580 ASSERT(old_space_strings_[i] != HEAP->raw_unchecked_null_value());
586 void ExternalStringTable::AddOldString(String* string) {
587 ASSERT(string->IsExternalString());
588 ASSERT(!heap_->InNewSpace(string));
589 old_space_strings_.Add(string);
593 void ExternalStringTable::ShrinkNewStrings(int position) {
594 new_space_strings_.Rewind(position);
595 if (FLAG_verify_heap) {
601 void Heap::ClearInstanceofCache() {
602 set_instanceof_cache_function(the_hole_value());
606 Object* Heap::ToBoolean(bool condition) {
607 return condition ? true_value() : false_value();
611 void Heap::CompletelyClearInstanceofCache() {
612 set_instanceof_cache_map(the_hole_value());
613 set_instanceof_cache_function(the_hole_value());
617 MaybeObject* TranscendentalCache::Get(Type type, double input) {
618 SubCache* cache = caches_[type];
620 caches_[type] = cache = new SubCache(type);
622 return cache->Get(input);
626 Address TranscendentalCache::cache_array_address() {
627 return reinterpret_cast<Address>(caches_);
631 double TranscendentalCache::SubCache::Calculate(double input) {
650 return 0.0; // Never happens.
655 MaybeObject* TranscendentalCache::SubCache::Get(double input) {
659 Element e = elements_[hash];
660 if (e.in[0] == c.integers[0] &&
661 e.in[1] == c.integers[1]) {
662 ASSERT(e.output != NULL);
663 isolate_->counters()->transcendental_cache_hit()->Increment();
666 double answer = Calculate(input);
667 isolate_->counters()->transcendental_cache_miss()->Increment();
669 { MaybeObject* maybe_heap_number =
670 isolate_->heap()->AllocateHeapNumber(answer);
671 if (!maybe_heap_number->ToObject(&heap_number)) return maybe_heap_number;
673 elements_[hash].in[0] = c.integers[0];
674 elements_[hash].in[1] = c.integers[1];
675 elements_[hash].output = heap_number;
680 Heap* _inline_get_heap_() {
685 } } // namespace v8::internal
687 #endif // V8_HEAP_INL_H_