blob: 57e6cc4c935d41e4fd130cff4a62d82b1745a2fa [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_HEAP_HEAP_INL_H_
6#define V8_HEAP_HEAP_INL_H_
7
8#include <cmath>
9
10#include "src/base/platform/platform.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011#include "src/counters.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include "src/heap/heap.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013#include "src/heap/incremental-marking-inl.h"
14#include "src/heap/mark-compact.h"
15#include "src/heap/spaces-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016#include "src/heap/store-buffer.h"
17#include "src/heap/store-buffer-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018#include "src/isolate.h"
19#include "src/list-inl.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000020#include "src/log.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021#include "src/msan.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022#include "src/objects-inl.h"
23#include "src/type-feedback-vector-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024
25namespace v8 {
26namespace internal {
27
28void PromotionQueue::insert(HeapObject* target, int size) {
29 if (emergency_stack_ != NULL) {
30 emergency_stack_->Add(Entry(target, size));
31 return;
32 }
33
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034 if ((rear_ - 2) < limit_) {
35 RelocateQueueHead();
36 emergency_stack_->Add(Entry(target, size));
37 return;
38 }
39
40 *(--rear_) = reinterpret_cast<intptr_t>(target);
41 *(--rear_) = size;
42// Assert no overflow into live objects.
43#ifdef DEBUG
44 SemiSpace::AssertValidRange(target->GetIsolate()->heap()->new_space()->top(),
45 reinterpret_cast<Address>(rear_));
46#endif
47}
48
49
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000050#define ROOT_ACCESSOR(type, name, camel_name) \
51 type* Heap::name() { return type::cast(roots_[k##camel_name##RootIndex]); }
52ROOT_LIST(ROOT_ACCESSOR)
53#undef ROOT_ACCESSOR
54
55#define STRUCT_MAP_ACCESSOR(NAME, Name, name) \
56 Map* Heap::name##_map() { return Map::cast(roots_[k##Name##MapRootIndex]); }
57STRUCT_LIST(STRUCT_MAP_ACCESSOR)
58#undef STRUCT_MAP_ACCESSOR
59
60#define STRING_ACCESSOR(name, str) \
61 String* Heap::name() { return String::cast(roots_[k##name##RootIndex]); }
62INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
63#undef STRING_ACCESSOR
64
65#define SYMBOL_ACCESSOR(name) \
66 Symbol* Heap::name() { return Symbol::cast(roots_[k##name##RootIndex]); }
67PRIVATE_SYMBOL_LIST(SYMBOL_ACCESSOR)
68#undef SYMBOL_ACCESSOR
69
70#define SYMBOL_ACCESSOR(name, description) \
71 Symbol* Heap::name() { return Symbol::cast(roots_[k##name##RootIndex]); }
72PUBLIC_SYMBOL_LIST(SYMBOL_ACCESSOR)
73WELL_KNOWN_SYMBOL_LIST(SYMBOL_ACCESSOR)
74#undef SYMBOL_ACCESSOR
75
76#define ROOT_ACCESSOR(type, name, camel_name) \
77 void Heap::set_##name(type* value) { \
78 /* The deserializer makes use of the fact that these common roots are */ \
79 /* never in new space and never on a page that is being compacted. */ \
80 DCHECK(!deserialization_complete() || \
81 RootCanBeWrittenAfterInitialization(k##camel_name##RootIndex)); \
82 DCHECK(k##camel_name##RootIndex >= kOldSpaceRoots || !InNewSpace(value)); \
83 roots_[k##camel_name##RootIndex] = value; \
84 }
85ROOT_LIST(ROOT_ACCESSOR)
86#undef ROOT_ACCESSOR
87
88
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089template <>
90bool inline Heap::IsOneByte(Vector<const char> str, int chars) {
91 // TODO(dcarney): incorporate Latin-1 check when Latin-1 is supported?
92 return chars == str.length();
93}
94
95
96template <>
97bool inline Heap::IsOneByte(String* str, int chars) {
98 return str->IsOneByteRepresentation();
99}
100
101
102AllocationResult Heap::AllocateInternalizedStringFromUtf8(
103 Vector<const char> str, int chars, uint32_t hash_field) {
104 if (IsOneByte(str, chars)) {
105 return AllocateOneByteInternalizedString(Vector<const uint8_t>::cast(str),
106 hash_field);
107 }
108 return AllocateInternalizedStringImpl<false>(str, chars, hash_field);
109}
110
111
112template <typename T>
113AllocationResult Heap::AllocateInternalizedStringImpl(T t, int chars,
114 uint32_t hash_field) {
115 if (IsOneByte(t, chars)) {
116 return AllocateInternalizedStringImpl<true>(t, chars, hash_field);
117 }
118 return AllocateInternalizedStringImpl<false>(t, chars, hash_field);
119}
120
121
122AllocationResult Heap::AllocateOneByteInternalizedString(
123 Vector<const uint8_t> str, uint32_t hash_field) {
124 CHECK_GE(String::kMaxLength, str.length());
125 // Compute map and object size.
126 Map* map = one_byte_internalized_string_map();
127 int size = SeqOneByteString::SizeFor(str.length());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128
129 // Allocate string.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000130 HeapObject* result = nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000132 AllocationResult allocation = AllocateRaw(size, OLD_SPACE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133 if (!allocation.To(&result)) return allocation;
134 }
135
136 // String maps are all immortal immovable objects.
137 result->set_map_no_write_barrier(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
143 DCHECK_EQ(size, answer->Size());
144
145 // Fill in the characters.
146 MemCopy(answer->address() + SeqOneByteString::kHeaderSize, str.start(),
147 str.length());
148
149 return answer;
150}
151
152
153AllocationResult Heap::AllocateTwoByteInternalizedString(Vector<const uc16> str,
154 uint32_t hash_field) {
155 CHECK_GE(String::kMaxLength, str.length());
156 // Compute map and object size.
157 Map* map = internalized_string_map();
158 int size = SeqTwoByteString::SizeFor(str.length());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159
160 // Allocate string.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161 HeapObject* result = nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000163 AllocationResult allocation = AllocateRaw(size, OLD_SPACE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000164 if (!allocation.To(&result)) return allocation;
165 }
166
167 result->set_map(map);
168 // Set length and hash fields of the allocated string.
169 String* answer = String::cast(result);
170 answer->set_length(str.length());
171 answer->set_hash_field(hash_field);
172
173 DCHECK_EQ(size, answer->Size());
174
175 // Fill in the characters.
176 MemCopy(answer->address() + SeqTwoByteString::kHeaderSize, str.start(),
177 str.length() * kUC16Size);
178
179 return answer;
180}
181
182AllocationResult Heap::CopyFixedArray(FixedArray* src) {
183 if (src->length() == 0) return src;
184 return CopyFixedArrayWithMap(src, src->map());
185}
186
187
188AllocationResult Heap::CopyFixedDoubleArray(FixedDoubleArray* src) {
189 if (src->length() == 0) return src;
190 return CopyFixedDoubleArrayWithMap(src, src->map());
191}
192
193
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194AllocationResult Heap::AllocateRaw(int size_in_bytes, AllocationSpace space,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000195 AllocationAlignment alignment) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196 DCHECK(AllowHandleAllocation::IsAllowed());
197 DCHECK(AllowHeapAllocation::IsAllowed());
198 DCHECK(gc_state_ == NOT_IN_GC);
199#ifdef DEBUG
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000200 if (FLAG_gc_interval >= 0 && !always_allocate() &&
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000201 Heap::allocation_timeout_-- <= 0) {
202 return AllocationResult::Retry(space);
203 }
204 isolate_->counters()->objs_since_last_full()->Increment();
205 isolate_->counters()->objs_since_last_young()->Increment();
206#endif
207
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000208 bool large_object = size_in_bytes > Page::kMaxRegularHeapObjectSize;
209 HeapObject* object = nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 AllocationResult allocation;
211 if (NEW_SPACE == space) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000212 if (large_object) {
213 space = LO_SPACE;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000215 allocation = new_space_.AllocateRaw(size_in_bytes, alignment);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000216 if (allocation.To(&object)) {
217 OnAllocationEvent(object, size_in_bytes);
218 }
219 return allocation;
220 }
221 }
222
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000223 // Here we only allocate in the old generation.
224 if (OLD_SPACE == space) {
225 if (large_object) {
226 allocation = lo_space_->AllocateRaw(size_in_bytes, NOT_EXECUTABLE);
227 } else {
228 allocation = old_space_->AllocateRaw(size_in_bytes, alignment);
229 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 } else if (CODE_SPACE == space) {
231 if (size_in_bytes <= code_space()->AreaSize()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000232 allocation = code_space_->AllocateRawUnaligned(size_in_bytes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000234 allocation = lo_space_->AllocateRaw(size_in_bytes, EXECUTABLE);
235 }
236 } else if (LO_SPACE == space) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000237 DCHECK(large_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000238 allocation = lo_space_->AllocateRaw(size_in_bytes, NOT_EXECUTABLE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000239 } else if (MAP_SPACE == space) {
240 allocation = map_space_->AllocateRawUnaligned(size_in_bytes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000241 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000242 // NEW_SPACE is not allowed here.
243 UNREACHABLE();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000244 }
245 if (allocation.To(&object)) {
246 OnAllocationEvent(object, size_in_bytes);
247 } else {
248 old_gen_exhausted_ = true;
249 }
250 return allocation;
251}
252
253
254void Heap::OnAllocationEvent(HeapObject* object, int size_in_bytes) {
255 HeapProfiler* profiler = isolate_->heap_profiler();
256 if (profiler->is_tracking_allocations()) {
257 profiler->AllocationEvent(object->address(), size_in_bytes);
258 }
259
260 if (FLAG_verify_predictable) {
261 ++allocations_count_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000262 // Advance synthetic time by making a time request.
263 MonotonicallyIncreasingTimeInMs();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000264
265 UpdateAllocationsHash(object);
266 UpdateAllocationsHash(size_in_bytes);
267
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000268 if (allocations_count_ % FLAG_dump_allocations_digest_at_alloc == 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000269 PrintAlloctionsHash();
270 }
271 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000272
273 if (FLAG_trace_allocation_stack_interval > 0) {
274 if (!FLAG_verify_predictable) ++allocations_count_;
275 if (allocations_count_ % FLAG_trace_allocation_stack_interval == 0) {
276 isolate()->PrintStack(stdout, Isolate::kPrintStackConcise);
277 }
278 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279}
280
281
282void Heap::OnMoveEvent(HeapObject* target, HeapObject* source,
283 int size_in_bytes) {
284 HeapProfiler* heap_profiler = isolate_->heap_profiler();
285 if (heap_profiler->is_tracking_object_moves()) {
286 heap_profiler->ObjectMoveEvent(source->address(), target->address(),
287 size_in_bytes);
288 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000289 if (target->IsSharedFunctionInfo()) {
290 LOG_CODE_EVENT(isolate_, SharedFunctionInfoMoveEvent(source->address(),
291 target->address()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000292 }
293
294 if (FLAG_verify_predictable) {
295 ++allocations_count_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000296 // Advance synthetic time by making a time request.
297 MonotonicallyIncreasingTimeInMs();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298
299 UpdateAllocationsHash(source);
300 UpdateAllocationsHash(target);
301 UpdateAllocationsHash(size_in_bytes);
302
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000303 if (allocations_count_ % FLAG_dump_allocations_digest_at_alloc == 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000304 PrintAlloctionsHash();
305 }
306 }
307}
308
309
310void Heap::UpdateAllocationsHash(HeapObject* object) {
311 Address object_address = object->address();
312 MemoryChunk* memory_chunk = MemoryChunk::FromAddress(object_address);
313 AllocationSpace allocation_space = memory_chunk->owner()->identity();
314
315 STATIC_ASSERT(kSpaceTagSize + kPageSizeBits <= 32);
316 uint32_t value =
317 static_cast<uint32_t>(object_address - memory_chunk->address()) |
318 (static_cast<uint32_t>(allocation_space) << kPageSizeBits);
319
320 UpdateAllocationsHash(value);
321}
322
323
324void Heap::UpdateAllocationsHash(uint32_t value) {
325 uint16_t c1 = static_cast<uint16_t>(value);
326 uint16_t c2 = static_cast<uint16_t>(value >> 16);
327 raw_allocations_hash_ =
328 StringHasher::AddCharacterCore(raw_allocations_hash_, c1);
329 raw_allocations_hash_ =
330 StringHasher::AddCharacterCore(raw_allocations_hash_, c2);
331}
332
333
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000334void Heap::RegisterExternalString(String* string) {
335 external_string_table_.AddString(string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336}
337
338
339void Heap::FinalizeExternalString(String* string) {
340 DCHECK(string->IsExternalString());
341 v8::String::ExternalStringResourceBase** resource_addr =
342 reinterpret_cast<v8::String::ExternalStringResourceBase**>(
343 reinterpret_cast<byte*>(string) + ExternalString::kResourceOffset -
344 kHeapObjectTag);
345
346 // Dispose of the C++ object if it has not already been disposed.
347 if (*resource_addr != NULL) {
348 (*resource_addr)->Dispose();
349 *resource_addr = NULL;
350 }
351}
352
353
354bool Heap::InNewSpace(Object* object) {
355 bool result = new_space_.Contains(object);
356 DCHECK(!result || // Either not in new space
357 gc_state_ != NOT_IN_GC || // ... or in the middle of GC
358 InToSpace(object)); // ... or in to-space (where we allocate).
359 return result;
360}
361
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000362bool Heap::InFromSpace(Object* object) {
363 return new_space_.FromSpaceContains(object);
364}
365
366
367bool Heap::InToSpace(Object* object) {
368 return new_space_.ToSpaceContains(object);
369}
370
Ben Murdoch097c5b22016-05-18 11:27:45 +0100371bool Heap::InOldSpace(Object* object) { return old_space_->Contains(object); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000372
Ben Murdoch097c5b22016-05-18 11:27:45 +0100373bool Heap::InNewSpaceSlow(Address address) {
374 return new_space_.ContainsSlow(address);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000375}
376
Ben Murdoch097c5b22016-05-18 11:27:45 +0100377bool Heap::InOldSpaceSlow(Address address) {
378 return old_space_->ContainsSlow(address);
379}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000380
381bool Heap::OldGenerationAllocationLimitReached() {
382 if (!incremental_marking()->IsStopped()) return false;
383 return OldGenerationSpaceAvailable() < 0;
384}
385
386
387bool Heap::ShouldBePromoted(Address old_address, int object_size) {
388 NewSpacePage* page = NewSpacePage::FromAddress(old_address);
389 Address age_mark = new_space_.age_mark();
390 return page->IsFlagSet(MemoryChunk::NEW_SPACE_BELOW_AGE_MARK) &&
391 (!page->ContainsLimit(age_mark) || old_address < age_mark);
392}
393
Ben Murdoch097c5b22016-05-18 11:27:45 +0100394void Heap::RecordWrite(Object* object, int offset, Object* o) {
395 if (!InNewSpace(o) || !object->IsHeapObject() || InNewSpace(object)) {
396 return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000397 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100398 Page* page = Page::FromAddress(reinterpret_cast<Address>(object));
399 Address slot = HeapObject::cast(object)->address() + offset;
400 RememberedSet<OLD_TO_NEW>::Insert(page, slot);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000401}
402
403
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404bool Heap::AllowedToBeMigrated(HeapObject* obj, AllocationSpace dst) {
405 // Object migration is governed by the following rules:
406 //
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000407 // 1) Objects in new-space can be migrated to the old space
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000408 // that matches their target space or they stay in new-space.
409 // 2) Objects in old-space stay in the same space when migrating.
410 // 3) Fillers (two or more words) can migrate due to left-trimming of
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000411 // fixed arrays in new-space or old space.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000412 // 4) Fillers (one word) can never migrate, they are skipped by
413 // incremental marking explicitly to prevent invalid pattern.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000414 //
415 // Since this function is used for debugging only, we do not place
416 // asserts here, but check everything explicitly.
417 if (obj->map() == one_pointer_filler_map()) return false;
418 InstanceType type = obj->map()->instance_type();
419 MemoryChunk* chunk = MemoryChunk::FromAddress(obj->address());
420 AllocationSpace src = chunk->owner()->identity();
421 switch (src) {
422 case NEW_SPACE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000423 return dst == src || dst == OLD_SPACE;
424 case OLD_SPACE:
425 return dst == src &&
426 (dst == OLD_SPACE || obj->IsFiller() || obj->IsExternalString());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000427 case CODE_SPACE:
428 return dst == src && type == CODE_TYPE;
429 case MAP_SPACE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000430 case LO_SPACE:
431 return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000432 }
433 UNREACHABLE();
434 return false;
435}
436
437
438void Heap::CopyBlock(Address dst, Address src, int byte_size) {
439 CopyWords(reinterpret_cast<Object**>(dst), reinterpret_cast<Object**>(src),
440 static_cast<size_t>(byte_size / kPointerSize));
441}
442
443
444void Heap::MoveBlock(Address dst, Address src, int byte_size) {
445 DCHECK(IsAligned(byte_size, kPointerSize));
446
447 int size_in_words = byte_size / kPointerSize;
448
449 if ((dst < src) || (dst >= (src + byte_size))) {
450 Object** src_slot = reinterpret_cast<Object**>(src);
451 Object** dst_slot = reinterpret_cast<Object**>(dst);
452 Object** end_slot = src_slot + size_in_words;
453
454 while (src_slot != end_slot) {
455 *dst_slot++ = *src_slot++;
456 }
457 } else {
458 MemMove(dst, src, static_cast<size_t>(byte_size));
459 }
460}
461
Ben Murdoch097c5b22016-05-18 11:27:45 +0100462template <Heap::FindMementoMode mode>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000463AllocationMemento* Heap::FindAllocationMemento(HeapObject* object) {
464 // Check if there is potentially a memento behind the object. If
465 // the last word of the memento is on another page we return
466 // immediately.
467 Address object_address = object->address();
468 Address memento_address = object_address + object->Size();
469 Address last_memento_word_address = memento_address + kPointerSize;
470 if (!NewSpacePage::OnSamePage(object_address, last_memento_word_address)) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100471 return nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000473 HeapObject* candidate = HeapObject::FromAddress(memento_address);
474 Map* candidate_map = candidate->map();
475 // This fast check may peek at an uninitialized word. However, the slow check
476 // below (memento_address == top) ensures that this is safe. Mark the word as
477 // initialized to silence MemorySanitizer warnings.
478 MSAN_MEMORY_IS_INITIALIZED(&candidate_map, sizeof(candidate_map));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100479 if (candidate_map != allocation_memento_map()) {
480 return nullptr;
481 }
482 AllocationMemento* memento_candidate = AllocationMemento::cast(candidate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000483
Ben Murdoch097c5b22016-05-18 11:27:45 +0100484 // Depending on what the memento is used for, we might need to perform
485 // additional checks.
486 Address top;
487 switch (mode) {
488 case Heap::kForGC:
489 return memento_candidate;
490 case Heap::kForRuntime:
491 if (memento_candidate == nullptr) return nullptr;
492 // Either the object is the last object in the new space, or there is
493 // another object of at least word size (the header map word) following
494 // it, so suffices to compare ptr and top here.
495 top = NewSpaceTop();
496 DCHECK(memento_address == top ||
497 memento_address + HeapObject::kHeaderSize <= top ||
498 !NewSpacePage::OnSamePage(memento_address, top - 1));
499 if ((memento_address != top) && memento_candidate->IsValid()) {
500 return memento_candidate;
501 }
502 return nullptr;
503 default:
504 UNREACHABLE();
505 }
506 UNREACHABLE();
507 return nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508}
509
Ben Murdoch097c5b22016-05-18 11:27:45 +0100510template <Heap::UpdateAllocationSiteMode mode>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000511void Heap::UpdateAllocationSite(HeapObject* object,
512 HashMap* pretenuring_feedback) {
513 DCHECK(InFromSpace(object));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000514 if (!FLAG_allocation_site_pretenuring ||
515 !AllocationSite::CanTrack(object->map()->instance_type()))
516 return;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100517 AllocationMemento* memento_candidate = FindAllocationMemento<kForGC>(object);
518 if (memento_candidate == nullptr) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000519
Ben Murdoch097c5b22016-05-18 11:27:45 +0100520 if (mode == kGlobal) {
521 DCHECK_EQ(pretenuring_feedback, global_pretenuring_feedback_);
522 // Entering global pretenuring feedback is only used in the scavenger, where
523 // we are allowed to actually touch the allocation site.
524 if (!memento_candidate->IsValid()) return;
525 AllocationSite* site = memento_candidate->GetAllocationSite();
526 DCHECK(!site->IsZombie());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000527 // For inserting in the global pretenuring storage we need to first
528 // increment the memento found count on the allocation site.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100529 if (site->IncrementMementoFoundCount()) {
530 global_pretenuring_feedback_->LookupOrInsert(site,
531 ObjectHash(site->address()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000532 }
533 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100534 DCHECK_EQ(mode, kCached);
535 DCHECK_NE(pretenuring_feedback, global_pretenuring_feedback_);
536 // Entering cached feedback is used in the parallel case. We are not allowed
537 // to dereference the allocation site and rather have to postpone all checks
538 // till actually merging the data.
539 Address key = memento_candidate->GetAllocationSiteUnchecked();
540 HashMap::Entry* e =
541 pretenuring_feedback->LookupOrInsert(key, ObjectHash(key));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000542 DCHECK(e != nullptr);
543 (*bit_cast<intptr_t*>(&e->value))++;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000544 }
545}
546
547
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000548void Heap::RemoveAllocationSitePretenuringFeedback(AllocationSite* site) {
549 global_pretenuring_feedback_->Remove(
550 site, static_cast<uint32_t>(bit_cast<uintptr_t>(site)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000551}
552
553
554bool Heap::CollectGarbage(AllocationSpace space, const char* gc_reason,
555 const v8::GCCallbackFlags callbackFlags) {
556 const char* collector_reason = NULL;
557 GarbageCollector collector = SelectGarbageCollector(space, &collector_reason);
558 return CollectGarbage(collector, gc_reason, collector_reason, callbackFlags);
559}
560
561
562Isolate* Heap::isolate() {
563 return reinterpret_cast<Isolate*>(
564 reinterpret_cast<intptr_t>(this) -
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400565 reinterpret_cast<size_t>(reinterpret_cast<Isolate*>(16)->heap()) + 16);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000566}
567
568
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000569void Heap::ExternalStringTable::AddString(String* string) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000570 DCHECK(string->IsExternalString());
571 if (heap_->InNewSpace(string)) {
572 new_space_strings_.Add(string);
573 } else {
574 old_space_strings_.Add(string);
575 }
576}
577
578
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000579void Heap::ExternalStringTable::Iterate(ObjectVisitor* v) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000580 if (!new_space_strings_.is_empty()) {
581 Object** start = &new_space_strings_[0];
582 v->VisitPointers(start, start + new_space_strings_.length());
583 }
584 if (!old_space_strings_.is_empty()) {
585 Object** start = &old_space_strings_[0];
586 v->VisitPointers(start, start + old_space_strings_.length());
587 }
588}
589
590
591// Verify() is inline to avoid ifdef-s around its calls in release
592// mode.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000593void Heap::ExternalStringTable::Verify() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000594#ifdef DEBUG
595 for (int i = 0; i < new_space_strings_.length(); ++i) {
596 Object* obj = Object::cast(new_space_strings_[i]);
597 DCHECK(heap_->InNewSpace(obj));
598 DCHECK(obj != heap_->the_hole_value());
599 }
600 for (int i = 0; i < old_space_strings_.length(); ++i) {
601 Object* obj = Object::cast(old_space_strings_[i]);
602 DCHECK(!heap_->InNewSpace(obj));
603 DCHECK(obj != heap_->the_hole_value());
604 }
605#endif
606}
607
608
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000609void Heap::ExternalStringTable::AddOldString(String* string) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000610 DCHECK(string->IsExternalString());
611 DCHECK(!heap_->InNewSpace(string));
612 old_space_strings_.Add(string);
613}
614
615
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000616void Heap::ExternalStringTable::ShrinkNewStrings(int position) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000617 new_space_strings_.Rewind(position);
618#ifdef VERIFY_HEAP
619 if (FLAG_verify_heap) {
620 Verify();
621 }
622#endif
623}
624
Ben Murdoch097c5b22016-05-18 11:27:45 +0100625// static
626int DescriptorLookupCache::Hash(Object* source, Name* name) {
627 DCHECK(name->IsUniqueName());
628 // Uses only lower 32 bits if pointers are larger.
629 uint32_t source_hash =
630 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(source)) >>
631 kPointerSizeLog2;
632 uint32_t name_hash = name->hash_field();
633 return (source_hash ^ name_hash) % kLength;
634}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000635
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000636int DescriptorLookupCache::Lookup(Map* source, Name* name) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000637 int index = Hash(source, name);
638 Key& key = keys_[index];
639 if ((key.source == source) && (key.name == name)) return results_[index];
640 return kAbsent;
641}
642
643
644void DescriptorLookupCache::Update(Map* source, Name* name, int result) {
645 DCHECK(result != kAbsent);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100646 int index = Hash(source, name);
647 Key& key = keys_[index];
648 key.source = source;
649 key.name = name;
650 results_[index] = result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000651}
652
653
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000654void Heap::ClearInstanceofCache() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400655 set_instanceof_cache_function(Smi::FromInt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000656}
657
658
659Object* Heap::ToBoolean(bool condition) {
660 return condition ? true_value() : false_value();
661}
662
663
664void Heap::CompletelyClearInstanceofCache() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400665 set_instanceof_cache_map(Smi::FromInt(0));
666 set_instanceof_cache_function(Smi::FromInt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000667}
668
669
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000670uint32_t Heap::HashSeed() {
671 uint32_t seed = static_cast<uint32_t>(hash_seed()->value());
672 DCHECK(FLAG_randomize_hashes || seed == 0);
673 return seed;
674}
675
676
677int Heap::NextScriptId() {
678 int last_id = last_script_id()->value();
679 if (last_id == Smi::kMaxValue) {
680 last_id = 1;
681 } else {
682 last_id++;
683 }
684 set_last_script_id(Smi::FromInt(last_id));
685 return last_id;
686}
687
688
689void Heap::SetArgumentsAdaptorDeoptPCOffset(int pc_offset) {
690 DCHECK(arguments_adaptor_deopt_pc_offset() == Smi::FromInt(0));
691 set_arguments_adaptor_deopt_pc_offset(Smi::FromInt(pc_offset));
692}
693
694
695void Heap::SetConstructStubDeoptPCOffset(int pc_offset) {
696 DCHECK(construct_stub_deopt_pc_offset() == Smi::FromInt(0));
697 set_construct_stub_deopt_pc_offset(Smi::FromInt(pc_offset));
698}
699
700
701void Heap::SetGetterStubDeoptPCOffset(int pc_offset) {
702 DCHECK(getter_stub_deopt_pc_offset() == Smi::FromInt(0));
703 set_getter_stub_deopt_pc_offset(Smi::FromInt(pc_offset));
704}
705
706
707void Heap::SetSetterStubDeoptPCOffset(int pc_offset) {
708 DCHECK(setter_stub_deopt_pc_offset() == Smi::FromInt(0));
709 set_setter_stub_deopt_pc_offset(Smi::FromInt(pc_offset));
710}
711
712
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000713AlwaysAllocateScope::AlwaysAllocateScope(Isolate* isolate)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000714 : heap_(isolate->heap()) {
715 heap_->always_allocate_scope_count_.Increment(1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000716}
717
718
719AlwaysAllocateScope::~AlwaysAllocateScope() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000720 heap_->always_allocate_scope_count_.Increment(-1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000721}
722
723
724void VerifyPointersVisitor::VisitPointers(Object** start, Object** end) {
725 for (Object** current = start; current < end; current++) {
726 if ((*current)->IsHeapObject()) {
727 HeapObject* object = HeapObject::cast(*current);
728 CHECK(object->GetIsolate()->heap()->Contains(object));
729 CHECK(object->map()->IsMap());
730 }
731 }
732}
733
734
735void VerifySmisVisitor::VisitPointers(Object** start, Object** end) {
736 for (Object** current = start; current < end; current++) {
737 CHECK((*current)->IsSmi());
738 }
739}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000740} // namespace internal
741} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000742
743#endif // V8_HEAP_HEAP_INL_H_