1 // 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 #include "src/heap/heap.h" 6 7 #include "src/accessors.h" 8 #include "src/api.h" 9 #include "src/ast/scopeinfo.h" 10 #include "src/base/bits.h" 11 #include "src/base/once.h" 12 #include "src/base/utils/random-number-generator.h" 13 #include "src/bootstrapper.h" 14 #include "src/codegen.h" 15 #include "src/compilation-cache.h" 16 #include "src/conversions.h" 17 #include "src/debug/debug.h" 18 #include "src/deoptimizer.h" 19 #include "src/global-handles.h" 20 #include "src/heap/array-buffer-tracker-inl.h" 21 #include "src/heap/gc-idle-time-handler.h" 22 #include "src/heap/gc-tracer.h" 23 #include "src/heap/incremental-marking.h" 24 #include "src/heap/mark-compact-inl.h" 25 #include "src/heap/mark-compact.h" 26 #include "src/heap/memory-reducer.h" 27 #include "src/heap/object-stats.h" 28 #include "src/heap/objects-visiting-inl.h" 29 #include "src/heap/objects-visiting.h" 30 #include "src/heap/remembered-set.h" 31 #include "src/heap/scavenge-job.h" 32 #include "src/heap/scavenger-inl.h" 33 #include "src/heap/store-buffer.h" 34 #include "src/interpreter/interpreter.h" 35 #include "src/regexp/jsregexp.h" 36 #include "src/runtime-profiler.h" 37 #include "src/snapshot/natives.h" 38 #include "src/snapshot/serializer-common.h" 39 #include "src/snapshot/snapshot.h" 40 #include "src/tracing/trace-event.h" 41 #include "src/type-feedback-vector.h" 42 #include "src/utils.h" 43 #include "src/v8.h" 44 #include "src/v8threads.h" 45 #include "src/vm-state-inl.h" 46 47 namespace v8 { 48 namespace internal { 49 50 51 struct Heap::StrongRootsList { 52 Object** start; 53 Object** end; 54 StrongRootsList* next; 55 }; 56 57 class IdleScavengeObserver : public AllocationObserver { 58 public: 59 IdleScavengeObserver(Heap& heap, intptr_t step_size) 60 : AllocationObserver(step_size), heap_(heap) {} 61 62 void Step(int bytes_allocated, Address, size_t) override { 63 heap_.ScheduleIdleScavengeIfNeeded(bytes_allocated); 64 } 65 66 private: 67 Heap& heap_; 68 }; 69 70 Heap::Heap() 71 : external_memory_(0), 72 external_memory_limit_(kExternalAllocationLimit), 73 external_memory_at_last_mark_compact_(0), 74 isolate_(nullptr), 75 code_range_size_(0), 76 // semispace_size_ should be a power of 2 and old_generation_size_ should 77 // be a multiple of Page::kPageSize. 78 max_semi_space_size_(8 * (kPointerSize / 4) * MB), 79 initial_semispace_size_(Page::kPageSize), 80 max_old_generation_size_(700ul * (kPointerSize / 4) * MB), 81 initial_old_generation_size_(max_old_generation_size_ / 82 kInitalOldGenerationLimitFactor), 83 old_generation_size_configured_(false), 84 max_executable_size_(256ul * (kPointerSize / 4) * MB), 85 // Variables set based on semispace_size_ and old_generation_size_ in 86 // ConfigureHeap. 87 // Will be 4 * reserved_semispace_size_ to ensure that young 88 // generation can be aligned to its size. 89 maximum_committed_(0), 90 survived_since_last_expansion_(0), 91 survived_last_scavenge_(0), 92 always_allocate_scope_count_(0), 93 memory_pressure_level_(MemoryPressureLevel::kNone), 94 contexts_disposed_(0), 95 number_of_disposed_maps_(0), 96 global_ic_age_(0), 97 new_space_(this), 98 old_space_(NULL), 99 code_space_(NULL), 100 map_space_(NULL), 101 lo_space_(NULL), 102 gc_state_(NOT_IN_GC), 103 gc_post_processing_depth_(0), 104 allocations_count_(0), 105 raw_allocations_hash_(0), 106 ms_count_(0), 107 gc_count_(0), 108 remembered_unmapped_pages_index_(0), 109 #ifdef DEBUG 110 allocation_timeout_(0), 111 #endif // DEBUG 112 old_generation_allocation_limit_(initial_old_generation_size_), 113 old_gen_exhausted_(false), 114 optimize_for_memory_usage_(false), 115 inline_allocation_disabled_(false), 116 total_regexp_code_generated_(0), 117 tracer_(nullptr), 118 high_survival_rate_period_length_(0), 119 promoted_objects_size_(0), 120 promotion_ratio_(0), 121 semi_space_copied_object_size_(0), 122 previous_semi_space_copied_object_size_(0), 123 semi_space_copied_rate_(0), 124 nodes_died_in_new_space_(0), 125 nodes_copied_in_new_space_(0), 126 nodes_promoted_(0), 127 maximum_size_scavenges_(0), 128 max_gc_pause_(0.0), 129 total_gc_time_ms_(0.0), 130 max_alive_after_gc_(0), 131 min_in_mutator_(kMaxInt), 132 marking_time_(0.0), 133 sweeping_time_(0.0), 134 last_idle_notification_time_(0.0), 135 last_gc_time_(0.0), 136 scavenge_collector_(nullptr), 137 mark_compact_collector_(nullptr), 138 memory_allocator_(nullptr), 139 store_buffer_(this), 140 incremental_marking_(nullptr), 141 gc_idle_time_handler_(nullptr), 142 memory_reducer_(nullptr), 143 object_stats_(nullptr), 144 scavenge_job_(nullptr), 145 idle_scavenge_observer_(nullptr), 146 full_codegen_bytes_generated_(0), 147 crankshaft_codegen_bytes_generated_(0), 148 new_space_allocation_counter_(0), 149 old_generation_allocation_counter_(0), 150 old_generation_size_at_last_gc_(0), 151 gcs_since_last_deopt_(0), 152 global_pretenuring_feedback_(nullptr), 153 ring_buffer_full_(false), 154 ring_buffer_end_(0), 155 promotion_queue_(this), 156 configured_(false), 157 current_gc_flags_(Heap::kNoGCFlags), 158 current_gc_callback_flags_(GCCallbackFlags::kNoGCCallbackFlags), 159 external_string_table_(this), 160 gc_callbacks_depth_(0), 161 deserialization_complete_(false), 162 strong_roots_list_(NULL), 163 heap_iterator_depth_(0), 164 force_oom_(false) { 165 // Allow build-time customization of the max semispace size. Building 166 // V8 with snapshots and a non-default max semispace size is much 167 // easier if you can define it as part of the build environment. 168 #if defined(V8_MAX_SEMISPACE_SIZE) 169 max_semi_space_size_ = reserved_semispace_size_ = V8_MAX_SEMISPACE_SIZE; 170 #endif 171 172 // Ensure old_generation_size_ is a multiple of kPageSize. 173 DCHECK((max_old_generation_size_ & (Page::kPageSize - 1)) == 0); 174 175 memset(roots_, 0, sizeof(roots_[0]) * kRootListLength); 176 set_native_contexts_list(NULL); 177 set_allocation_sites_list(Smi::FromInt(0)); 178 set_encountered_weak_collections(Smi::FromInt(0)); 179 set_encountered_weak_cells(Smi::FromInt(0)); 180 set_encountered_transition_arrays(Smi::FromInt(0)); 181 // Put a dummy entry in the remembered pages so we can find the list the 182 // minidump even if there are no real unmapped pages. 183 RememberUnmappedPage(NULL, false); 184 } 185 186 187 intptr_t Heap::Capacity() { 188 if (!HasBeenSetUp()) return 0; 189 190 return new_space_.Capacity() + OldGenerationCapacity(); 191 } 192 193 intptr_t Heap::OldGenerationCapacity() { 194 if (!HasBeenSetUp()) return 0; 195 196 return old_space_->Capacity() + code_space_->Capacity() + 197 map_space_->Capacity() + lo_space_->SizeOfObjects(); 198 } 199 200 201 intptr_t Heap::CommittedOldGenerationMemory() { 202 if (!HasBeenSetUp()) return 0; 203 204 return old_space_->CommittedMemory() + code_space_->CommittedMemory() + 205 map_space_->CommittedMemory() + lo_space_->Size(); 206 } 207 208 209 intptr_t Heap::CommittedMemory() { 210 if (!HasBeenSetUp()) return 0; 211 212 return new_space_.CommittedMemory() + CommittedOldGenerationMemory(); 213 } 214 215 216 size_t Heap::CommittedPhysicalMemory() { 217 if (!HasBeenSetUp()) return 0; 218 219 return new_space_.CommittedPhysicalMemory() + 220 old_space_->CommittedPhysicalMemory() + 221 code_space_->CommittedPhysicalMemory() + 222 map_space_->CommittedPhysicalMemory() + 223 lo_space_->CommittedPhysicalMemory(); 224 } 225 226 227 intptr_t Heap::CommittedMemoryExecutable() { 228 if (!HasBeenSetUp()) return 0; 229 230 return memory_allocator()->SizeExecutable(); 231 } 232 233 234 void Heap::UpdateMaximumCommitted() { 235 if (!HasBeenSetUp()) return; 236 237 intptr_t current_committed_memory = CommittedMemory(); 238 if (current_committed_memory > maximum_committed_) { 239 maximum_committed_ = current_committed_memory; 240 } 241 } 242 243 244 intptr_t Heap::Available() { 245 if (!HasBeenSetUp()) return 0; 246 247 intptr_t total = 0; 248 AllSpaces spaces(this); 249 for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { 250 total += space->Available(); 251 } 252 return total; 253 } 254 255 256 bool Heap::HasBeenSetUp() { 257 return old_space_ != NULL && code_space_ != NULL && map_space_ != NULL && 258 lo_space_ != NULL; 259 } 260 261 262 GarbageCollector Heap::SelectGarbageCollector(AllocationSpace space, 263 const char** reason) { 264 // Is global GC requested? 265 if (space != NEW_SPACE) { 266 isolate_->counters()->gc_compactor_caused_by_request()->Increment(); 267 *reason = "GC in old space requested"; 268 return MARK_COMPACTOR; 269 } 270 271 if (FLAG_gc_global || (FLAG_stress_compaction && (gc_count_ & 1) != 0)) { 272 *reason = "GC in old space forced by flags"; 273 return MARK_COMPACTOR; 274 } 275 276 // Is enough data promoted to justify a global GC? 277 if (OldGenerationAllocationLimitReached()) { 278 isolate_->counters()->gc_compactor_caused_by_promoted_data()->Increment(); 279 *reason = "promotion limit reached"; 280 return MARK_COMPACTOR; 281 } 282 283 // Have allocation in OLD and LO failed? 284 if (old_gen_exhausted_) { 285 isolate_->counters() 286 ->gc_compactor_caused_by_oldspace_exhaustion() 287 ->Increment(); 288 *reason = "old generations exhausted"; 289 return MARK_COMPACTOR; 290 } 291 292 // Is there enough space left in OLD to guarantee that a scavenge can 293 // succeed? 294 // 295 // Note that MemoryAllocator->MaxAvailable() undercounts the memory available 296 // for object promotion. It counts only the bytes that the memory 297 // allocator has not yet allocated from the OS and assigned to any space, 298 // and does not count available bytes already in the old space or code 299 // space. Undercounting is safe---we may get an unrequested full GC when 300 // a scavenge would have succeeded. 301 if (memory_allocator()->MaxAvailable() <= new_space_.Size()) { 302 isolate_->counters() 303 ->gc_compactor_caused_by_oldspace_exhaustion() 304 ->Increment(); 305 *reason = "scavenge might not succeed"; 306 return MARK_COMPACTOR; 307 } 308 309 // Default 310 *reason = NULL; 311 return SCAVENGER; 312 } 313 314 315 // TODO(1238405): Combine the infrastructure for --heap-stats and 316 // --log-gc to avoid the complicated preprocessor and flag testing. 317 void Heap::ReportStatisticsBeforeGC() { 318 // Heap::ReportHeapStatistics will also log NewSpace statistics when 319 // compiled --log-gc is set. The following logic is used to avoid 320 // double logging. 321 #ifdef DEBUG 322 if (FLAG_heap_stats || FLAG_log_gc) new_space_.CollectStatistics(); 323 if (FLAG_heap_stats) { 324 ReportHeapStatistics("Before GC"); 325 } else if (FLAG_log_gc) { 326 new_space_.ReportStatistics(); 327 } 328 if (FLAG_heap_stats || FLAG_log_gc) new_space_.ClearHistograms(); 329 #else 330 if (FLAG_log_gc) { 331 new_space_.CollectStatistics(); 332 new_space_.ReportStatistics(); 333 new_space_.ClearHistograms(); 334 } 335 #endif // DEBUG 336 } 337 338 339 void Heap::PrintShortHeapStatistics() { 340 if (!FLAG_trace_gc_verbose) return; 341 PrintIsolate(isolate_, "Memory allocator, used: %6" V8PRIdPTR 342 " KB, available: %6" V8PRIdPTR " KB\n", 343 memory_allocator()->Size() / KB, 344 memory_allocator()->Available() / KB); 345 PrintIsolate(isolate_, "New space, used: %6" V8PRIdPTR 346 " KB" 347 ", available: %6" V8PRIdPTR 348 " KB" 349 ", committed: %6" V8PRIdPTR " KB\n", 350 new_space_.Size() / KB, new_space_.Available() / KB, 351 new_space_.CommittedMemory() / KB); 352 PrintIsolate(isolate_, "Old space, used: %6" V8PRIdPTR 353 " KB" 354 ", available: %6" V8PRIdPTR 355 " KB" 356 ", committed: %6" V8PRIdPTR " KB\n", 357 old_space_->SizeOfObjects() / KB, old_space_->Available() / KB, 358 old_space_->CommittedMemory() / KB); 359 PrintIsolate(isolate_, "Code space, used: %6" V8PRIdPTR 360 " KB" 361 ", available: %6" V8PRIdPTR 362 " KB" 363 ", committed: %6" V8PRIdPTR " KB\n", 364 code_space_->SizeOfObjects() / KB, code_space_->Available() / KB, 365 code_space_->CommittedMemory() / KB); 366 PrintIsolate(isolate_, "Map space, used: %6" V8PRIdPTR 367 " KB" 368 ", available: %6" V8PRIdPTR 369 " KB" 370 ", committed: %6" V8PRIdPTR " KB\n", 371 map_space_->SizeOfObjects() / KB, map_space_->Available() / KB, 372 map_space_->CommittedMemory() / KB); 373 PrintIsolate(isolate_, "Large object space, used: %6" V8PRIdPTR 374 " KB" 375 ", available: %6" V8PRIdPTR 376 " KB" 377 ", committed: %6" V8PRIdPTR " KB\n", 378 lo_space_->SizeOfObjects() / KB, lo_space_->Available() / KB, 379 lo_space_->CommittedMemory() / KB); 380 PrintIsolate(isolate_, "All spaces, used: %6" V8PRIdPTR 381 " KB" 382 ", available: %6" V8PRIdPTR 383 " KB" 384 ", committed: %6" V8PRIdPTR " KB\n", 385 this->SizeOfObjects() / KB, this->Available() / KB, 386 this->CommittedMemory() / KB); 387 PrintIsolate(isolate_, "External memory reported: %6" V8PRIdPTR " KB\n", 388 static_cast<intptr_t>(external_memory_ / KB)); 389 PrintIsolate(isolate_, "Total time spent in GC : %.1f ms\n", 390 total_gc_time_ms_); 391 } 392 393 // TODO(1238405): Combine the infrastructure for --heap-stats and 394 // --log-gc to avoid the complicated preprocessor and flag testing. 395 void Heap::ReportStatisticsAfterGC() { 396 // Similar to the before GC, we use some complicated logic to ensure that 397 // NewSpace statistics are logged exactly once when --log-gc is turned on. 398 #if defined(DEBUG) 399 if (FLAG_heap_stats) { 400 new_space_.CollectStatistics(); 401 ReportHeapStatistics("After GC"); 402 } else if (FLAG_log_gc) { 403 new_space_.ReportStatistics(); 404 } 405 #else 406 if (FLAG_log_gc) new_space_.ReportStatistics(); 407 #endif // DEBUG 408 for (int i = 0; i < static_cast<int>(v8::Isolate::kUseCounterFeatureCount); 409 ++i) { 410 int count = deferred_counters_[i]; 411 deferred_counters_[i] = 0; 412 while (count > 0) { 413 count--; 414 isolate()->CountUsage(static_cast<v8::Isolate::UseCounterFeature>(i)); 415 } 416 } 417 } 418 419 420 void Heap::IncrementDeferredCount(v8::Isolate::UseCounterFeature feature) { 421 deferred_counters_[feature]++; 422 } 423 424 425 void Heap::GarbageCollectionPrologue() { 426 { 427 AllowHeapAllocation for_the_first_part_of_prologue; 428 gc_count_++; 429 430 #ifdef VERIFY_HEAP 431 if (FLAG_verify_heap) { 432 Verify(); 433 } 434 #endif 435 } 436 437 // Reset GC statistics. 438 promoted_objects_size_ = 0; 439 previous_semi_space_copied_object_size_ = semi_space_copied_object_size_; 440 semi_space_copied_object_size_ = 0; 441 nodes_died_in_new_space_ = 0; 442 nodes_copied_in_new_space_ = 0; 443 nodes_promoted_ = 0; 444 445 UpdateMaximumCommitted(); 446 447 #ifdef DEBUG 448 DCHECK(!AllowHeapAllocation::IsAllowed() && gc_state_ == NOT_IN_GC); 449 450 if (FLAG_gc_verbose) Print(); 451 452 ReportStatisticsBeforeGC(); 453 #endif // DEBUG 454 455 if (new_space_.IsAtMaximumCapacity()) { 456 maximum_size_scavenges_++; 457 } else { 458 maximum_size_scavenges_ = 0; 459 } 460 CheckNewSpaceExpansionCriteria(); 461 UpdateNewSpaceAllocationCounter(); 462 store_buffer()->MoveEntriesToRememberedSet(); 463 } 464 465 466 intptr_t Heap::SizeOfObjects() { 467 intptr_t total = 0; 468 AllSpaces spaces(this); 469 for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { 470 total += space->SizeOfObjects(); 471 } 472 return total; 473 } 474 475 476 const char* Heap::GetSpaceName(int idx) { 477 switch (idx) { 478 case NEW_SPACE: 479 return "new_space"; 480 case OLD_SPACE: 481 return "old_space"; 482 case MAP_SPACE: 483 return "map_space"; 484 case CODE_SPACE: 485 return "code_space"; 486 case LO_SPACE: 487 return "large_object_space"; 488 default: 489 UNREACHABLE(); 490 } 491 return nullptr; 492 } 493 494 495 void Heap::RepairFreeListsAfterDeserialization() { 496 PagedSpaces spaces(this); 497 for (PagedSpace* space = spaces.next(); space != NULL; 498 space = spaces.next()) { 499 space->RepairFreeListsAfterDeserialization(); 500 } 501 } 502 503 void Heap::MergeAllocationSitePretenuringFeedback( 504 const base::HashMap& local_pretenuring_feedback) { 505 AllocationSite* site = nullptr; 506 for (base::HashMap::Entry* local_entry = local_pretenuring_feedback.Start(); 507 local_entry != nullptr; 508 local_entry = local_pretenuring_feedback.Next(local_entry)) { 509 site = reinterpret_cast<AllocationSite*>(local_entry->key); 510 MapWord map_word = site->map_word(); 511 if (map_word.IsForwardingAddress()) { 512 site = AllocationSite::cast(map_word.ToForwardingAddress()); 513 } 514 515 // We have not validated the allocation site yet, since we have not 516 // dereferenced the site during collecting information. 517 // This is an inlined check of AllocationMemento::IsValid. 518 if (!site->IsAllocationSite() || site->IsZombie()) continue; 519 520 int value = 521 static_cast<int>(reinterpret_cast<intptr_t>(local_entry->value)); 522 DCHECK_GT(value, 0); 523 524 if (site->IncrementMementoFoundCount(value)) { 525 global_pretenuring_feedback_->LookupOrInsert(site, 526 ObjectHash(site->address())); 527 } 528 } 529 } 530 531 532 class Heap::PretenuringScope { 533 public: 534 explicit PretenuringScope(Heap* heap) : heap_(heap) { 535 heap_->global_pretenuring_feedback_ = new base::HashMap( 536 base::HashMap::PointersMatch, kInitialFeedbackCapacity); 537 } 538 539 ~PretenuringScope() { 540 delete heap_->global_pretenuring_feedback_; 541 heap_->global_pretenuring_feedback_ = nullptr; 542 } 543 544 private: 545 Heap* heap_; 546 }; 547 548 549 void Heap::ProcessPretenuringFeedback() { 550 bool trigger_deoptimization = false; 551 if (FLAG_allocation_site_pretenuring) { 552 int tenure_decisions = 0; 553 int dont_tenure_decisions = 0; 554 int allocation_mementos_found = 0; 555 int allocation_sites = 0; 556 int active_allocation_sites = 0; 557 558 AllocationSite* site = nullptr; 559 560 // Step 1: Digest feedback for recorded allocation sites. 561 bool maximum_size_scavenge = MaximumSizeScavenge(); 562 for (base::HashMap::Entry* e = global_pretenuring_feedback_->Start(); 563 e != nullptr; e = global_pretenuring_feedback_->Next(e)) { 564 allocation_sites++; 565 site = reinterpret_cast<AllocationSite*>(e->key); 566 int found_count = site->memento_found_count(); 567 // An entry in the storage does not imply that the count is > 0 because 568 // allocation sites might have been reset due to too many objects dying 569 // in old space. 570 if (found_count > 0) { 571 DCHECK(site->IsAllocationSite()); 572 active_allocation_sites++; 573 allocation_mementos_found += found_count; 574 if (site->DigestPretenuringFeedback(maximum_size_scavenge)) { 575 trigger_deoptimization = true; 576 } 577 if (site->GetPretenureMode() == TENURED) { 578 tenure_decisions++; 579 } else { 580 dont_tenure_decisions++; 581 } 582 } 583 } 584 585 // Step 2: Deopt maybe tenured allocation sites if necessary. 586 bool deopt_maybe_tenured = DeoptMaybeTenuredAllocationSites(); 587 if (deopt_maybe_tenured) { 588 Object* list_element = allocation_sites_list(); 589 while (list_element->IsAllocationSite()) { 590 site = AllocationSite::cast(list_element); 591 DCHECK(site->IsAllocationSite()); 592 allocation_sites++; 593 if (site->IsMaybeTenure()) { 594 site->set_deopt_dependent_code(true); 595 trigger_deoptimization = true; 596 } 597 list_element = site->weak_next(); 598 } 599 } 600 601 if (trigger_deoptimization) { 602 isolate_->stack_guard()->RequestDeoptMarkedAllocationSites(); 603 } 604 605 if (FLAG_trace_pretenuring_statistics && 606 (allocation_mementos_found > 0 || tenure_decisions > 0 || 607 dont_tenure_decisions > 0)) { 608 PrintIsolate(isolate(), 609 "pretenuring: deopt_maybe_tenured=%d visited_sites=%d " 610 "active_sites=%d " 611 "mementos=%d tenured=%d not_tenured=%d\n", 612 deopt_maybe_tenured ? 1 : 0, allocation_sites, 613 active_allocation_sites, allocation_mementos_found, 614 tenure_decisions, dont_tenure_decisions); 615 } 616 } 617 } 618 619 620 void Heap::DeoptMarkedAllocationSites() { 621 // TODO(hpayer): If iterating over the allocation sites list becomes a 622 // performance issue, use a cache data structure in heap instead. 623 Object* list_element = allocation_sites_list(); 624 while (list_element->IsAllocationSite()) { 625 AllocationSite* site = AllocationSite::cast(list_element); 626 if (site->deopt_dependent_code()) { 627 site->dependent_code()->MarkCodeForDeoptimization( 628 isolate_, DependentCode::kAllocationSiteTenuringChangedGroup); 629 site->set_deopt_dependent_code(false); 630 } 631 list_element = site->weak_next(); 632 } 633 Deoptimizer::DeoptimizeMarkedCode(isolate_); 634 } 635 636 637 void Heap::GarbageCollectionEpilogue() { 638 // In release mode, we only zap the from space under heap verification. 639 if (Heap::ShouldZapGarbage()) { 640 ZapFromSpace(); 641 } 642 643 #ifdef VERIFY_HEAP 644 if (FLAG_verify_heap) { 645 Verify(); 646 } 647 #endif 648 649 AllowHeapAllocation for_the_rest_of_the_epilogue; 650 651 #ifdef DEBUG 652 if (FLAG_print_global_handles) isolate_->global_handles()->Print(); 653 if (FLAG_print_handles) PrintHandles(); 654 if (FLAG_gc_verbose) Print(); 655 if (FLAG_code_stats) ReportCodeStatistics("After GC"); 656 if (FLAG_check_handle_count) CheckHandleCount(); 657 #endif 658 if (FLAG_deopt_every_n_garbage_collections > 0) { 659 // TODO(jkummerow/ulan/jarin): This is not safe! We can't assume that 660 // the topmost optimized frame can be deoptimized safely, because it 661 // might not have a lazy bailout point right after its current PC. 662 if (++gcs_since_last_deopt_ == FLAG_deopt_every_n_garbage_collections) { 663 Deoptimizer::DeoptimizeAll(isolate()); 664 gcs_since_last_deopt_ = 0; 665 } 666 } 667 668 UpdateMaximumCommitted(); 669 670 isolate_->counters()->alive_after_last_gc()->Set( 671 static_cast<int>(SizeOfObjects())); 672 673 isolate_->counters()->string_table_capacity()->Set( 674 string_table()->Capacity()); 675 isolate_->counters()->number_of_symbols()->Set( 676 string_table()->NumberOfElements()); 677 678 if (full_codegen_bytes_generated_ + crankshaft_codegen_bytes_generated_ > 0) { 679 isolate_->counters()->codegen_fraction_crankshaft()->AddSample( 680 static_cast<int>((crankshaft_codegen_bytes_generated_ * 100.0) / 681 (crankshaft_codegen_bytes_generated_ + 682 full_codegen_bytes_generated_))); 683 } 684 685 if (CommittedMemory() > 0) { 686 isolate_->counters()->external_fragmentation_total()->AddSample( 687 static_cast<int>(100 - (SizeOfObjects() * 100.0) / CommittedMemory())); 688 689 isolate_->counters()->heap_fraction_new_space()->AddSample(static_cast<int>( 690 (new_space()->CommittedMemory() * 100.0) / CommittedMemory())); 691 isolate_->counters()->heap_fraction_old_space()->AddSample(static_cast<int>( 692 (old_space()->CommittedMemory() * 100.0) / CommittedMemory())); 693 isolate_->counters()->heap_fraction_code_space()->AddSample( 694 static_cast<int>((code_space()->CommittedMemory() * 100.0) / 695 CommittedMemory())); 696 isolate_->counters()->heap_fraction_map_space()->AddSample(static_cast<int>( 697 (map_space()->CommittedMemory() * 100.0) / CommittedMemory())); 698 isolate_->counters()->heap_fraction_lo_space()->AddSample(static_cast<int>( 699 (lo_space()->CommittedMemory() * 100.0) / CommittedMemory())); 700 701 isolate_->counters()->heap_sample_total_committed()->AddSample( 702 static_cast<int>(CommittedMemory() / KB)); 703 isolate_->counters()->heap_sample_total_used()->AddSample( 704 static_cast<int>(SizeOfObjects() / KB)); 705 isolate_->counters()->heap_sample_map_space_committed()->AddSample( 706 static_cast<int>(map_space()->CommittedMemory() / KB)); 707 isolate_->counters()->heap_sample_code_space_committed()->AddSample( 708 static_cast<int>(code_space()->CommittedMemory() / KB)); 709 710 isolate_->counters()->heap_sample_maximum_committed()->AddSample( 711 static_cast<int>(MaximumCommittedMemory() / KB)); 712 } 713 714 #define UPDATE_COUNTERS_FOR_SPACE(space) \ 715 isolate_->counters()->space##_bytes_available()->Set( \ 716 static_cast<int>(space()->Available())); \ 717 isolate_->counters()->space##_bytes_committed()->Set( \ 718 static_cast<int>(space()->CommittedMemory())); \ 719 isolate_->counters()->space##_bytes_used()->Set( \ 720 static_cast<int>(space()->SizeOfObjects())); 721 #define UPDATE_FRAGMENTATION_FOR_SPACE(space) \ 722 if (space()->CommittedMemory() > 0) { \ 723 isolate_->counters()->external_fragmentation_##space()->AddSample( \ 724 static_cast<int>(100 - \ 725 (space()->SizeOfObjects() * 100.0) / \ 726 space()->CommittedMemory())); \ 727 } 728 #define UPDATE_COUNTERS_AND_FRAGMENTATION_FOR_SPACE(space) \ 729 UPDATE_COUNTERS_FOR_SPACE(space) \ 730 UPDATE_FRAGMENTATION_FOR_SPACE(space) 731 732 UPDATE_COUNTERS_FOR_SPACE(new_space) 733 UPDATE_COUNTERS_AND_FRAGMENTATION_FOR_SPACE(old_space) 734 UPDATE_COUNTERS_AND_FRAGMENTATION_FOR_SPACE(code_space) 735 UPDATE_COUNTERS_AND_FRAGMENTATION_FOR_SPACE(map_space) 736 UPDATE_COUNTERS_AND_FRAGMENTATION_FOR_SPACE(lo_space) 737 #undef UPDATE_COUNTERS_FOR_SPACE 738 #undef UPDATE_FRAGMENTATION_FOR_SPACE 739 #undef UPDATE_COUNTERS_AND_FRAGMENTATION_FOR_SPACE 740 741 #ifdef DEBUG 742 ReportStatisticsAfterGC(); 743 #endif // DEBUG 744 745 // Remember the last top pointer so that we can later find out 746 // whether we allocated in new space since the last GC. 747 new_space_top_after_last_gc_ = new_space()->top(); 748 last_gc_time_ = MonotonicallyIncreasingTimeInMs(); 749 750 ReduceNewSpaceSize(); 751 } 752 753 754 void Heap::PreprocessStackTraces() { 755 WeakFixedArray::Iterator iterator(weak_stack_trace_list()); 756 FixedArray* elements; 757 while ((elements = iterator.Next<FixedArray>())) { 758 for (int j = 1; j < elements->length(); j += 4) { 759 Object* maybe_code = elements->get(j + 2); 760 // If GC happens while adding a stack trace to the weak fixed array, 761 // which has been copied into a larger backing store, we may run into 762 // a stack trace that has already been preprocessed. Guard against this. 763 if (!maybe_code->IsAbstractCode()) break; 764 AbstractCode* abstract_code = AbstractCode::cast(maybe_code); 765 int offset = Smi::cast(elements->get(j + 3))->value(); 766 int pos = abstract_code->SourcePosition(offset); 767 elements->set(j + 2, Smi::FromInt(pos)); 768 } 769 } 770 // We must not compact the weak fixed list here, as we may be in the middle 771 // of writing to it, when the GC triggered. Instead, we reset the root value. 772 set_weak_stack_trace_list(Smi::FromInt(0)); 773 } 774 775 776 class GCCallbacksScope { 777 public: 778 explicit GCCallbacksScope(Heap* heap) : heap_(heap) { 779 heap_->gc_callbacks_depth_++; 780 } 781 ~GCCallbacksScope() { heap_->gc_callbacks_depth_--; } 782 783 bool CheckReenter() { return heap_->gc_callbacks_depth_ == 1; } 784 785 private: 786 Heap* heap_; 787 }; 788 789 790 void Heap::HandleGCRequest() { 791 if (HighMemoryPressure()) { 792 incremental_marking()->reset_request_type(); 793 CheckMemoryPressure(); 794 } else if (incremental_marking()->request_type() == 795 IncrementalMarking::COMPLETE_MARKING) { 796 incremental_marking()->reset_request_type(); 797 CollectAllGarbage(current_gc_flags_, "GC interrupt", 798 current_gc_callback_flags_); 799 } else if (incremental_marking()->request_type() == 800 IncrementalMarking::FINALIZATION && 801 incremental_marking()->IsMarking() && 802 !incremental_marking()->finalize_marking_completed()) { 803 incremental_marking()->reset_request_type(); 804 FinalizeIncrementalMarking("GC interrupt: finalize incremental marking"); 805 } 806 } 807 808 809 void Heap::ScheduleIdleScavengeIfNeeded(int bytes_allocated) { 810 scavenge_job_->ScheduleIdleTaskIfNeeded(this, bytes_allocated); 811 } 812 813 814 void Heap::FinalizeIncrementalMarking(const char* gc_reason) { 815 if (FLAG_trace_incremental_marking) { 816 PrintF("[IncrementalMarking] (%s).\n", gc_reason); 817 } 818 819 HistogramTimerScope incremental_marking_scope( 820 isolate()->counters()->gc_incremental_marking_finalize()); 821 TRACE_EVENT0("v8", "V8.GCIncrementalMarkingFinalize"); 822 TRACE_GC(tracer(), GCTracer::Scope::MC_INCREMENTAL_FINALIZE); 823 824 { 825 GCCallbacksScope scope(this); 826 if (scope.CheckReenter()) { 827 AllowHeapAllocation allow_allocation; 828 TRACE_GC(tracer(), GCTracer::Scope::MC_INCREMENTAL_EXTERNAL_PROLOGUE); 829 VMState<EXTERNAL> state(isolate_); 830 HandleScope handle_scope(isolate_); 831 CallGCPrologueCallbacks(kGCTypeIncrementalMarking, kNoGCCallbackFlags); 832 } 833 } 834 incremental_marking()->FinalizeIncrementally(); 835 { 836 GCCallbacksScope scope(this); 837 if (scope.CheckReenter()) { 838 AllowHeapAllocation allow_allocation; 839 TRACE_GC(tracer(), GCTracer::Scope::MC_INCREMENTAL_EXTERNAL_EPILOGUE); 840 VMState<EXTERNAL> state(isolate_); 841 HandleScope handle_scope(isolate_); 842 CallGCEpilogueCallbacks(kGCTypeIncrementalMarking, kNoGCCallbackFlags); 843 } 844 } 845 } 846 847 848 HistogramTimer* Heap::GCTypeTimer(GarbageCollector collector) { 849 if (collector == SCAVENGER) { 850 return isolate_->counters()->gc_scavenger(); 851 } else { 852 if (!incremental_marking()->IsStopped()) { 853 if (ShouldReduceMemory()) { 854 return isolate_->counters()->gc_finalize_reduce_memory(); 855 } else { 856 return isolate_->counters()->gc_finalize(); 857 } 858 } else { 859 return isolate_->counters()->gc_compactor(); 860 } 861 } 862 } 863 864 void Heap::CollectAllGarbage(int flags, const char* gc_reason, 865 const v8::GCCallbackFlags gc_callback_flags) { 866 // Since we are ignoring the return value, the exact choice of space does 867 // not matter, so long as we do not specify NEW_SPACE, which would not 868 // cause a full GC. 869 set_current_gc_flags(flags); 870 CollectGarbage(OLD_SPACE, gc_reason, gc_callback_flags); 871 set_current_gc_flags(kNoGCFlags); 872 } 873 874 875 void Heap::CollectAllAvailableGarbage(const char* gc_reason) { 876 // Since we are ignoring the return value, the exact choice of space does 877 // not matter, so long as we do not specify NEW_SPACE, which would not 878 // cause a full GC. 879 // Major GC would invoke weak handle callbacks on weakly reachable 880 // handles, but won't collect weakly reachable objects until next 881 // major GC. Therefore if we collect aggressively and weak handle callback 882 // has been invoked, we rerun major GC to release objects which become 883 // garbage. 884 // Note: as weak callbacks can execute arbitrary code, we cannot 885 // hope that eventually there will be no weak callbacks invocations. 886 // Therefore stop recollecting after several attempts. 887 if (isolate()->concurrent_recompilation_enabled()) { 888 // The optimizing compiler may be unnecessarily holding on to memory. 889 DisallowHeapAllocation no_recursive_gc; 890 isolate()->optimizing_compile_dispatcher()->Flush(); 891 } 892 isolate()->ClearSerializerData(); 893 set_current_gc_flags(kMakeHeapIterableMask | kReduceMemoryFootprintMask); 894 isolate_->compilation_cache()->Clear(); 895 const int kMaxNumberOfAttempts = 7; 896 const int kMinNumberOfAttempts = 2; 897 for (int attempt = 0; attempt < kMaxNumberOfAttempts; attempt++) { 898 if (!CollectGarbage(MARK_COMPACTOR, gc_reason, NULL, 899 v8::kGCCallbackFlagCollectAllAvailableGarbage) && 900 attempt + 1 >= kMinNumberOfAttempts) { 901 break; 902 } 903 } 904 set_current_gc_flags(kNoGCFlags); 905 new_space_.Shrink(); 906 UncommitFromSpace(); 907 } 908 909 910 void Heap::ReportExternalMemoryPressure(const char* gc_reason) { 911 if (incremental_marking()->IsStopped()) { 912 if (incremental_marking()->CanBeActivated()) { 913 StartIncrementalMarking( 914 i::Heap::kNoGCFlags, 915 kGCCallbackFlagSynchronousPhantomCallbackProcessing, gc_reason); 916 } else { 917 CollectAllGarbage(i::Heap::kNoGCFlags, gc_reason, 918 kGCCallbackFlagSynchronousPhantomCallbackProcessing); 919 } 920 } else { 921 // Incremental marking is turned on an has already been started. 922 923 // TODO(mlippautz): Compute the time slice for incremental marking based on 924 // memory pressure. 925 double deadline = MonotonicallyIncreasingTimeInMs() + 926 FLAG_external_allocation_limit_incremental_time; 927 incremental_marking()->AdvanceIncrementalMarking( 928 deadline, 929 IncrementalMarking::StepActions(IncrementalMarking::GC_VIA_STACK_GUARD, 930 IncrementalMarking::FORCE_MARKING, 931 IncrementalMarking::FORCE_COMPLETION)); 932 } 933 } 934 935 936 void Heap::EnsureFillerObjectAtTop() { 937 // There may be an allocation memento behind objects in new space. Upon 938 // evacuation of a non-full new space (or if we are on the last page) there 939 // may be uninitialized memory behind top. We fill the remainder of the page 940 // with a filler. 941 Address to_top = new_space_.top(); 942 Page* page = Page::FromAddress(to_top - kPointerSize); 943 if (page->Contains(to_top)) { 944 int remaining_in_page = static_cast<int>(page->area_end() - to_top); 945 CreateFillerObjectAt(to_top, remaining_in_page, ClearRecordedSlots::kNo); 946 } 947 } 948 949 950 bool Heap::CollectGarbage(GarbageCollector collector, const char* gc_reason, 951 const char* collector_reason, 952 const v8::GCCallbackFlags gc_callback_flags) { 953 // The VM is in the GC state until exiting this function. 954 VMState<GC> state(isolate_); 955 956 #ifdef DEBUG 957 // Reset the allocation timeout to the GC interval, but make sure to 958 // allow at least a few allocations after a collection. The reason 959 // for this is that we have a lot of allocation sequences and we 960 // assume that a garbage collection will allow the subsequent 961 // allocation attempts to go through. 962 allocation_timeout_ = Max(6, FLAG_gc_interval); 963 #endif 964 965 EnsureFillerObjectAtTop(); 966 967 if (collector == SCAVENGER && !incremental_marking()->IsStopped()) { 968 if (FLAG_trace_incremental_marking) { 969 PrintF("[IncrementalMarking] Scavenge during marking.\n"); 970 } 971 } 972 973 if (collector == MARK_COMPACTOR && !ShouldFinalizeIncrementalMarking() && 974 !ShouldAbortIncrementalMarking() && !incremental_marking()->IsStopped() && 975 !incremental_marking()->should_hurry() && FLAG_incremental_marking && 976 OldGenerationAllocationLimitReached()) { 977 if (!incremental_marking()->IsComplete() && 978 !mark_compact_collector()->marking_deque_.IsEmpty() && 979 !FLAG_gc_global) { 980 if (FLAG_trace_incremental_marking) { 981 PrintF("[IncrementalMarking] Delaying MarkSweep.\n"); 982 } 983 collector = SCAVENGER; 984 collector_reason = "incremental marking delaying mark-sweep"; 985 } 986 } 987 988 bool next_gc_likely_to_collect_more = false; 989 intptr_t committed_memory_before = 0; 990 991 if (collector == MARK_COMPACTOR) { 992 committed_memory_before = CommittedOldGenerationMemory(); 993 } 994 995 { 996 tracer()->Start(collector, gc_reason, collector_reason); 997 DCHECK(AllowHeapAllocation::IsAllowed()); 998 DisallowHeapAllocation no_allocation_during_gc; 999 GarbageCollectionPrologue(); 1000 1001 { 1002 HistogramTimer* gc_type_timer = GCTypeTimer(collector); 1003 HistogramTimerScope histogram_timer_scope(gc_type_timer); 1004 TRACE_EVENT0("v8", gc_type_timer->name()); 1005 1006 next_gc_likely_to_collect_more = 1007 PerformGarbageCollection(collector, gc_callback_flags); 1008 } 1009 1010 GarbageCollectionEpilogue(); 1011 if (collector == MARK_COMPACTOR && FLAG_track_detached_contexts) { 1012 isolate()->CheckDetachedContextsAfterGC(); 1013 } 1014 1015 if (collector == MARK_COMPACTOR) { 1016 intptr_t committed_memory_after = CommittedOldGenerationMemory(); 1017 intptr_t used_memory_after = PromotedSpaceSizeOfObjects(); 1018 MemoryReducer::Event event; 1019 event.type = MemoryReducer::kMarkCompact; 1020 event.time_ms = MonotonicallyIncreasingTimeInMs(); 1021 // Trigger one more GC if 1022 // - this GC decreased committed memory, 1023 // - there is high fragmentation, 1024 // - there are live detached contexts. 1025 event.next_gc_likely_to_collect_more = 1026 (committed_memory_before - committed_memory_after) > MB || 1027 HasHighFragmentation(used_memory_after, committed_memory_after) || 1028 (detached_contexts()->length() > 0); 1029 if (deserialization_complete_) { 1030 memory_reducer_->NotifyMarkCompact(event); 1031 } 1032 memory_pressure_level_.SetValue(MemoryPressureLevel::kNone); 1033 } 1034 1035 tracer()->Stop(collector); 1036 } 1037 1038 if (collector == MARK_COMPACTOR && 1039 (gc_callback_flags & (kGCCallbackFlagForced | 1040 kGCCallbackFlagCollectAllAvailableGarbage)) != 0) { 1041 isolate()->CountUsage(v8::Isolate::kForcedGC); 1042 } 1043 1044 // Start incremental marking for the next cycle. The heap snapshot 1045 // generator needs incremental marking to stay off after it aborted. 1046 if (!ShouldAbortIncrementalMarking() && incremental_marking()->IsStopped() && 1047 incremental_marking()->ShouldActivateEvenWithoutIdleNotification()) { 1048 StartIncrementalMarking(kNoGCFlags, kNoGCCallbackFlags, "GC epilogue"); 1049 } 1050 1051 return next_gc_likely_to_collect_more; 1052 } 1053 1054 1055 int Heap::NotifyContextDisposed(bool dependant_context) { 1056 if (!dependant_context) { 1057 tracer()->ResetSurvivalEvents(); 1058 old_generation_size_configured_ = false; 1059 MemoryReducer::Event event; 1060 event.type = MemoryReducer::kPossibleGarbage; 1061 event.time_ms = MonotonicallyIncreasingTimeInMs(); 1062 memory_reducer_->NotifyPossibleGarbage(event); 1063 } 1064 if (isolate()->concurrent_recompilation_enabled()) { 1065 // Flush the queued recompilation tasks. 1066 isolate()->optimizing_compile_dispatcher()->Flush(); 1067 } 1068 AgeInlineCaches(); 1069 number_of_disposed_maps_ = retained_maps()->Length(); 1070 tracer()->AddContextDisposalTime(MonotonicallyIncreasingTimeInMs()); 1071 return ++contexts_disposed_; 1072 } 1073 1074 1075 void Heap::StartIncrementalMarking(int gc_flags, 1076 const GCCallbackFlags gc_callback_flags, 1077 const char* reason) { 1078 DCHECK(incremental_marking()->IsStopped()); 1079 set_current_gc_flags(gc_flags); 1080 current_gc_callback_flags_ = gc_callback_flags; 1081 incremental_marking()->Start(reason); 1082 } 1083 1084 1085 void Heap::StartIdleIncrementalMarking() { 1086 gc_idle_time_handler_->ResetNoProgressCounter(); 1087 StartIncrementalMarking(kReduceMemoryFootprintMask, kNoGCCallbackFlags, 1088 "idle"); 1089 } 1090 1091 1092 void Heap::MoveElements(FixedArray* array, int dst_index, int src_index, 1093 int len) { 1094 if (len == 0) return; 1095 1096 DCHECK(array->map() != fixed_cow_array_map()); 1097 Object** dst_objects = array->data_start() + dst_index; 1098 MemMove(dst_objects, array->data_start() + src_index, len * kPointerSize); 1099 FIXED_ARRAY_ELEMENTS_WRITE_BARRIER(this, array, dst_index, len); 1100 } 1101 1102 1103 #ifdef VERIFY_HEAP 1104 // Helper class for verifying the string table. 1105 class StringTableVerifier : public ObjectVisitor { 1106 public: 1107 void VisitPointers(Object** start, Object** end) override { 1108 // Visit all HeapObject pointers in [start, end). 1109 for (Object** p = start; p < end; p++) { 1110 if ((*p)->IsHeapObject()) { 1111 HeapObject* object = HeapObject::cast(*p); 1112 Isolate* isolate = object->GetIsolate(); 1113 // Check that the string is actually internalized. 1114 CHECK(object->IsTheHole(isolate) || object->IsUndefined(isolate) || 1115 object->IsInternalizedString()); 1116 } 1117 } 1118 } 1119 }; 1120 1121 1122 static void VerifyStringTable(Heap* heap) { 1123 StringTableVerifier verifier; 1124 heap->string_table()->IterateElements(&verifier); 1125 } 1126 #endif // VERIFY_HEAP 1127 1128 1129 bool Heap::ReserveSpace(Reservation* reservations) { 1130 bool gc_performed = true; 1131 int counter = 0; 1132 static const int kThreshold = 20; 1133 while (gc_performed && counter++ < kThreshold) { 1134 gc_performed = false; 1135 for (int space = NEW_SPACE; space < SerializerDeserializer::kNumberOfSpaces; 1136 space++) { 1137 Reservation* reservation = &reservations[space]; 1138 DCHECK_LE(1, reservation->length()); 1139 if (reservation->at(0).size == 0) continue; 1140 bool perform_gc = false; 1141 if (space == LO_SPACE) { 1142 DCHECK_EQ(1, reservation->length()); 1143 perform_gc = !CanExpandOldGeneration(reservation->at(0).size); 1144 } else { 1145 for (auto& chunk : *reservation) { 1146 AllocationResult allocation; 1147 int size = chunk.size; 1148 DCHECK_LE(size, MemoryAllocator::PageAreaSize( 1149 static_cast<AllocationSpace>(space))); 1150 if (space == NEW_SPACE) { 1151 allocation = new_space()->AllocateRawUnaligned(size); 1152 } else { 1153 // The deserializer will update the skip list. 1154 allocation = paged_space(space)->AllocateRawUnaligned( 1155 size, PagedSpace::IGNORE_SKIP_LIST); 1156 } 1157 HeapObject* free_space = nullptr; 1158 if (allocation.To(&free_space)) { 1159 // Mark with a free list node, in case we have a GC before 1160 // deserializing. 1161 Address free_space_address = free_space->address(); 1162 CreateFillerObjectAt(free_space_address, size, 1163 ClearRecordedSlots::kNo); 1164 DCHECK(space < SerializerDeserializer::kNumberOfPreallocatedSpaces); 1165 chunk.start = free_space_address; 1166 chunk.end = free_space_address + size; 1167 } else { 1168 perform_gc = true; 1169 break; 1170 } 1171 } 1172 } 1173 if (perform_gc) { 1174 if (space == NEW_SPACE) { 1175 CollectGarbage(NEW_SPACE, "failed to reserve space in the new space"); 1176 } else { 1177 if (counter > 1) { 1178 CollectAllGarbage( 1179 kReduceMemoryFootprintMask | kAbortIncrementalMarkingMask, 1180 "failed to reserve space in paged or large " 1181 "object space, trying to reduce memory footprint"); 1182 } else { 1183 CollectAllGarbage( 1184 kAbortIncrementalMarkingMask, 1185 "failed to reserve space in paged or large object space"); 1186 } 1187 } 1188 gc_performed = true; 1189 break; // Abort for-loop over spaces and retry. 1190 } 1191 } 1192 } 1193 1194 return !gc_performed; 1195 } 1196 1197 1198 void Heap::EnsureFromSpaceIsCommitted() { 1199 if (new_space_.CommitFromSpaceIfNeeded()) return; 1200 1201 // Committing memory to from space failed. 1202 // Memory is exhausted and we will die. 1203 V8::FatalProcessOutOfMemory("Committing semi space failed."); 1204 } 1205 1206 1207 void Heap::ClearNormalizedMapCaches() { 1208 if (isolate_->bootstrapper()->IsActive() && 1209 !incremental_marking()->IsMarking()) { 1210 return; 1211 } 1212 1213 Object* context = native_contexts_list(); 1214 while (!context->IsUndefined(isolate())) { 1215 // GC can happen when the context is not fully initialized, 1216 // so the cache can be undefined. 1217 Object* cache = 1218 Context::cast(context)->get(Context::NORMALIZED_MAP_CACHE_INDEX); 1219 if (!cache->IsUndefined(isolate())) { 1220 NormalizedMapCache::cast(cache)->Clear(); 1221 } 1222 context = Context::cast(context)->next_context_link(); 1223 } 1224 } 1225 1226 1227 void Heap::UpdateSurvivalStatistics(int start_new_space_size) { 1228 if (start_new_space_size == 0) return; 1229 1230 promotion_ratio_ = (static_cast<double>(promoted_objects_size_) / 1231 static_cast<double>(start_new_space_size) * 100); 1232 1233 if (previous_semi_space_copied_object_size_ > 0) { 1234 promotion_rate_ = 1235 (static_cast<double>(promoted_objects_size_) / 1236 static_cast<double>(previous_semi_space_copied_object_size_) * 100); 1237 } else { 1238 promotion_rate_ = 0; 1239 } 1240 1241 semi_space_copied_rate_ = 1242 (static_cast<double>(semi_space_copied_object_size_) / 1243 static_cast<double>(start_new_space_size) * 100); 1244 1245 double survival_rate = promotion_ratio_ + semi_space_copied_rate_; 1246 tracer()->AddSurvivalRatio(survival_rate); 1247 if (survival_rate > kYoungSurvivalRateHighThreshold) { 1248 high_survival_rate_period_length_++; 1249 } else { 1250 high_survival_rate_period_length_ = 0; 1251 } 1252 } 1253 1254 bool Heap::PerformGarbageCollection( 1255 GarbageCollector collector, const v8::GCCallbackFlags gc_callback_flags) { 1256 int freed_global_handles = 0; 1257 1258 if (collector != SCAVENGER) { 1259 PROFILE(isolate_, CodeMovingGCEvent()); 1260 } 1261 1262 #ifdef VERIFY_HEAP 1263 if (FLAG_verify_heap) { 1264 VerifyStringTable(this); 1265 } 1266 #endif 1267 1268 GCType gc_type = 1269 collector == MARK_COMPACTOR ? kGCTypeMarkSweepCompact : kGCTypeScavenge; 1270 1271 { 1272 GCCallbacksScope scope(this); 1273 if (scope.CheckReenter()) { 1274 AllowHeapAllocation allow_allocation; 1275 TRACE_GC(tracer(), collector == MARK_COMPACTOR 1276 ? GCTracer::Scope::MC_EXTERNAL_PROLOGUE 1277 : GCTracer::Scope::SCAVENGER_EXTERNAL_PROLOGUE); 1278 VMState<EXTERNAL> state(isolate_); 1279 HandleScope handle_scope(isolate_); 1280 CallGCPrologueCallbacks(gc_type, kNoGCCallbackFlags); 1281 } 1282 } 1283 1284 EnsureFromSpaceIsCommitted(); 1285 1286 int start_new_space_size = Heap::new_space()->SizeAsInt(); 1287 1288 if (IsHighSurvivalRate()) { 1289 // We speed up the incremental marker if it is running so that it 1290 // does not fall behind the rate of promotion, which would cause a 1291 // constantly growing old space. 1292 incremental_marking()->NotifyOfHighPromotionRate(); 1293 } 1294 1295 { 1296 Heap::PretenuringScope pretenuring_scope(this); 1297 1298 if (collector == MARK_COMPACTOR) { 1299 UpdateOldGenerationAllocationCounter(); 1300 // Perform mark-sweep with optional compaction. 1301 MarkCompact(); 1302 old_gen_exhausted_ = false; 1303 old_generation_size_configured_ = true; 1304 // This should be updated before PostGarbageCollectionProcessing, which 1305 // can cause another GC. Take into account the objects promoted during GC. 1306 old_generation_allocation_counter_ += 1307 static_cast<size_t>(promoted_objects_size_); 1308 old_generation_size_at_last_gc_ = PromotedSpaceSizeOfObjects(); 1309 } else { 1310 Scavenge(); 1311 } 1312 1313 ProcessPretenuringFeedback(); 1314 } 1315 1316 UpdateSurvivalStatistics(start_new_space_size); 1317 ConfigureInitialOldGenerationSize(); 1318 1319 isolate_->counters()->objs_since_last_young()->Set(0); 1320 1321 gc_post_processing_depth_++; 1322 { 1323 AllowHeapAllocation allow_allocation; 1324 TRACE_GC(tracer(), GCTracer::Scope::EXTERNAL_WEAK_GLOBAL_HANDLES); 1325 freed_global_handles = 1326 isolate_->global_handles()->PostGarbageCollectionProcessing( 1327 collector, gc_callback_flags); 1328 } 1329 gc_post_processing_depth_--; 1330 1331 isolate_->eternal_handles()->PostGarbageCollectionProcessing(this); 1332 1333 // Update relocatables. 1334 Relocatable::PostGarbageCollectionProcessing(isolate_); 1335 1336 double gc_speed = tracer()->CombinedMarkCompactSpeedInBytesPerMillisecond(); 1337 double mutator_speed = 1338 tracer()->CurrentOldGenerationAllocationThroughputInBytesPerMillisecond(); 1339 intptr_t old_gen_size = PromotedSpaceSizeOfObjects(); 1340 if (collector == MARK_COMPACTOR) { 1341 // Register the amount of external allocated memory. 1342 external_memory_at_last_mark_compact_ = external_memory_; 1343 external_memory_limit_ = external_memory_ + kExternalAllocationLimit; 1344 SetOldGenerationAllocationLimit(old_gen_size, gc_speed, mutator_speed); 1345 } else if (HasLowYoungGenerationAllocationRate() && 1346 old_generation_size_configured_) { 1347 DampenOldGenerationAllocationLimit(old_gen_size, gc_speed, mutator_speed); 1348 } 1349 1350 { 1351 GCCallbacksScope scope(this); 1352 if (scope.CheckReenter()) { 1353 AllowHeapAllocation allow_allocation; 1354 TRACE_GC(tracer(), collector == MARK_COMPACTOR 1355 ? GCTracer::Scope::MC_EXTERNAL_EPILOGUE 1356 : GCTracer::Scope::SCAVENGER_EXTERNAL_EPILOGUE); 1357 VMState<EXTERNAL> state(isolate_); 1358 HandleScope handle_scope(isolate_); 1359 CallGCEpilogueCallbacks(gc_type, gc_callback_flags); 1360 } 1361 } 1362 1363 #ifdef VERIFY_HEAP 1364 if (FLAG_verify_heap) { 1365 VerifyStringTable(this); 1366 } 1367 #endif 1368 1369 return freed_global_handles > 0; 1370 } 1371 1372 1373 void Heap::CallGCPrologueCallbacks(GCType gc_type, GCCallbackFlags flags) { 1374 for (int i = 0; i < gc_prologue_callbacks_.length(); ++i) { 1375 if (gc_type & gc_prologue_callbacks_[i].gc_type) { 1376 if (!gc_prologue_callbacks_[i].pass_isolate) { 1377 v8::GCCallback callback = reinterpret_cast<v8::GCCallback>( 1378 gc_prologue_callbacks_[i].callback); 1379 callback(gc_type, flags); 1380 } else { 1381 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(this->isolate()); 1382 gc_prologue_callbacks_[i].callback(isolate, gc_type, flags); 1383 } 1384 } 1385 } 1386 if (FLAG_trace_object_groups && (gc_type == kGCTypeIncrementalMarking || 1387 gc_type == kGCTypeMarkSweepCompact)) { 1388 isolate_->global_handles()->PrintObjectGroups(); 1389 } 1390 } 1391 1392 1393 void Heap::CallGCEpilogueCallbacks(GCType gc_type, 1394 GCCallbackFlags gc_callback_flags) { 1395 for (int i = 0; i < gc_epilogue_callbacks_.length(); ++i) { 1396 if (gc_type & gc_epilogue_callbacks_[i].gc_type) { 1397 if (!gc_epilogue_callbacks_[i].pass_isolate) { 1398 v8::GCCallback callback = reinterpret_cast<v8::GCCallback>( 1399 gc_epilogue_callbacks_[i].callback); 1400 callback(gc_type, gc_callback_flags); 1401 } else { 1402 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(this->isolate()); 1403 gc_epilogue_callbacks_[i].callback(isolate, gc_type, gc_callback_flags); 1404 } 1405 } 1406 } 1407 } 1408 1409 1410 void Heap::MarkCompact() { 1411 PauseAllocationObserversScope pause_observers(this); 1412 1413 gc_state_ = MARK_COMPACT; 1414 LOG(isolate_, ResourceEvent("markcompact", "begin")); 1415 1416 uint64_t size_of_objects_before_gc = SizeOfObjects(); 1417 1418 mark_compact_collector()->Prepare(); 1419 1420 ms_count_++; 1421 1422 MarkCompactPrologue(); 1423 1424 mark_compact_collector()->CollectGarbage(); 1425 1426 LOG(isolate_, ResourceEvent("markcompact", "end")); 1427 1428 MarkCompactEpilogue(); 1429 1430 if (FLAG_allocation_site_pretenuring) { 1431 EvaluateOldSpaceLocalPretenuring(size_of_objects_before_gc); 1432 } 1433 } 1434 1435 1436 void Heap::MarkCompactEpilogue() { 1437 gc_state_ = NOT_IN_GC; 1438 1439 isolate_->counters()->objs_since_last_full()->Set(0); 1440 1441 incremental_marking()->Epilogue(); 1442 1443 PreprocessStackTraces(); 1444 DCHECK(incremental_marking()->IsStopped()); 1445 1446 // We finished a marking cycle. We can uncommit the marking deque until 1447 // we start marking again. 1448 mark_compact_collector()->marking_deque()->Uninitialize(); 1449 mark_compact_collector()->EnsureMarkingDequeIsCommitted( 1450 MarkCompactCollector::kMinMarkingDequeSize); 1451 } 1452 1453 1454 void Heap::MarkCompactPrologue() { 1455 // At any old GC clear the keyed lookup cache to enable collection of unused 1456 // maps. 1457 isolate_->keyed_lookup_cache()->Clear(); 1458 isolate_->context_slot_cache()->Clear(); 1459 isolate_->descriptor_lookup_cache()->Clear(); 1460 RegExpResultsCache::Clear(string_split_cache()); 1461 RegExpResultsCache::Clear(regexp_multiple_cache()); 1462 1463 isolate_->compilation_cache()->MarkCompactPrologue(); 1464 1465 CompletelyClearInstanceofCache(); 1466 1467 FlushNumberStringCache(); 1468 ClearNormalizedMapCaches(); 1469 } 1470 1471 1472 #ifdef VERIFY_HEAP 1473 // Visitor class to verify pointers in code or data space do not point into 1474 // new space. 1475 class VerifyNonPointerSpacePointersVisitor : public ObjectVisitor { 1476 public: 1477 explicit VerifyNonPointerSpacePointersVisitor(Heap* heap) : heap_(heap) {} 1478 1479 void VisitPointers(Object** start, Object** end) override { 1480 for (Object** current = start; current < end; current++) { 1481 if ((*current)->IsHeapObject()) { 1482 CHECK(!heap_->InNewSpace(HeapObject::cast(*current))); 1483 } 1484 } 1485 } 1486 1487 private: 1488 Heap* heap_; 1489 }; 1490 1491 1492 static void VerifyNonPointerSpacePointers(Heap* heap) { 1493 // Verify that there are no pointers to new space in spaces where we 1494 // do not expect them. 1495 VerifyNonPointerSpacePointersVisitor v(heap); 1496 HeapObjectIterator code_it(heap->code_space()); 1497 for (HeapObject* object = code_it.Next(); object != NULL; 1498 object = code_it.Next()) 1499 object->Iterate(&v); 1500 } 1501 #endif // VERIFY_HEAP 1502 1503 1504 void Heap::CheckNewSpaceExpansionCriteria() { 1505 if (FLAG_experimental_new_space_growth_heuristic) { 1506 if (new_space_.TotalCapacity() < new_space_.MaximumCapacity() && 1507 survived_last_scavenge_ * 100 / new_space_.TotalCapacity() >= 10) { 1508 // Grow the size of new space if there is room to grow, and more than 10% 1509 // have survived the last scavenge. 1510 new_space_.Grow(); 1511 survived_since_last_expansion_ = 0; 1512 } 1513 } else if (new_space_.TotalCapacity() < new_space_.MaximumCapacity() && 1514 survived_since_last_expansion_ > new_space_.TotalCapacity()) { 1515 // Grow the size of new space if there is room to grow, and enough data 1516 // has survived scavenge since the last expansion. 1517 new_space_.Grow(); 1518 survived_since_last_expansion_ = 0; 1519 } 1520 } 1521 1522 1523 static bool IsUnscavengedHeapObject(Heap* heap, Object** p) { 1524 return heap->InNewSpace(*p) && 1525 !HeapObject::cast(*p)->map_word().IsForwardingAddress(); 1526 } 1527 1528 1529 static bool IsUnmodifiedHeapObject(Object** p) { 1530 Object* object = *p; 1531 if (object->IsSmi()) return false; 1532 HeapObject* heap_object = HeapObject::cast(object); 1533 if (!object->IsJSObject()) return false; 1534 JSObject* js_object = JSObject::cast(object); 1535 if (!js_object->WasConstructedFromApiFunction()) return false; 1536 JSFunction* constructor = 1537 JSFunction::cast(js_object->map()->GetConstructor()); 1538 1539 return constructor->initial_map() == heap_object->map(); 1540 } 1541 1542 1543 void PromotionQueue::Initialize() { 1544 // The last to-space page may be used for promotion queue. On promotion 1545 // conflict, we use the emergency stack. 1546 DCHECK((Page::kPageSize - MemoryChunk::kBodyOffset) % (2 * kPointerSize) == 1547 0); 1548 front_ = rear_ = 1549 reinterpret_cast<struct Entry*>(heap_->new_space()->ToSpaceEnd()); 1550 limit_ = reinterpret_cast<struct Entry*>( 1551 Page::FromAllocationAreaAddress(reinterpret_cast<Address>(rear_)) 1552 ->area_start()); 1553 emergency_stack_ = NULL; 1554 } 1555 1556 1557 void PromotionQueue::RelocateQueueHead() { 1558 DCHECK(emergency_stack_ == NULL); 1559 1560 Page* p = Page::FromAllocationAreaAddress(reinterpret_cast<Address>(rear_)); 1561 struct Entry* head_start = rear_; 1562 struct Entry* head_end = 1563 Min(front_, reinterpret_cast<struct Entry*>(p->area_end())); 1564 1565 int entries_count = 1566 static_cast<int>(head_end - head_start) / sizeof(struct Entry); 1567 1568 emergency_stack_ = new List<Entry>(2 * entries_count); 1569 1570 while (head_start != head_end) { 1571 struct Entry* entry = head_start++; 1572 // New space allocation in SemiSpaceCopyObject marked the region 1573 // overlapping with promotion queue as uninitialized. 1574 MSAN_MEMORY_IS_INITIALIZED(entry, sizeof(struct Entry)); 1575 emergency_stack_->Add(*entry); 1576 } 1577 rear_ = head_end; 1578 } 1579 1580 1581 class ScavengeWeakObjectRetainer : public WeakObjectRetainer { 1582 public: 1583 explicit ScavengeWeakObjectRetainer(Heap* heap) : heap_(heap) {} 1584 1585 virtual Object* RetainAs(Object* object) { 1586 if (!heap_->InFromSpace(object)) { 1587 return object; 1588 } 1589 1590 MapWord map_word = HeapObject::cast(object)->map_word(); 1591 if (map_word.IsForwardingAddress()) { 1592 return map_word.ToForwardingAddress(); 1593 } 1594 return NULL; 1595 } 1596 1597 private: 1598 Heap* heap_; 1599 }; 1600 1601 1602 void Heap::Scavenge() { 1603 TRACE_GC(tracer(), GCTracer::Scope::SCAVENGER_SCAVENGE); 1604 RelocationLock relocation_lock(this); 1605 // There are soft limits in the allocation code, designed to trigger a mark 1606 // sweep collection by failing allocations. There is no sense in trying to 1607 // trigger one during scavenge: scavenges allocation should always succeed. 1608 AlwaysAllocateScope scope(isolate()); 1609 1610 // Bump-pointer allocations done during scavenge are not real allocations. 1611 // Pause the inline allocation steps. 1612 PauseAllocationObserversScope pause_observers(this); 1613 1614 mark_compact_collector()->sweeper().EnsureNewSpaceCompleted(); 1615 1616 #ifdef VERIFY_HEAP 1617 if (FLAG_verify_heap) VerifyNonPointerSpacePointers(this); 1618 #endif 1619 1620 gc_state_ = SCAVENGE; 1621 1622 // Implements Cheney's copying algorithm 1623 LOG(isolate_, ResourceEvent("scavenge", "begin")); 1624 1625 // Used for updating survived_since_last_expansion_ at function end. 1626 intptr_t survived_watermark = PromotedSpaceSizeOfObjects(); 1627 1628 scavenge_collector_->SelectScavengingVisitorsTable(); 1629 1630 if (UsingEmbedderHeapTracer()) { 1631 // Register found wrappers with embedder so it can add them to its marking 1632 // deque and correctly manage the case when v8 scavenger collects the 1633 // wrappers by either keeping wrappables alive, or cleaning marking deque. 1634 mark_compact_collector()->RegisterWrappersWithEmbedderHeapTracer(); 1635 } 1636 1637 // Flip the semispaces. After flipping, to space is empty, from space has 1638 // live objects. 1639 new_space_.Flip(); 1640 new_space_.ResetAllocationInfo(); 1641 1642 // We need to sweep newly copied objects which can be either in the 1643 // to space or promoted to the old generation. For to-space 1644 // objects, we treat the bottom of the to space as a queue. Newly 1645 // copied and unswept objects lie between a 'front' mark and the 1646 // allocation pointer. 1647 // 1648 // Promoted objects can go into various old-generation spaces, and 1649 // can be allocated internally in the spaces (from the free list). 1650 // We treat the top of the to space as a queue of addresses of 1651 // promoted objects. The addresses of newly promoted and unswept 1652 // objects lie between a 'front' mark and a 'rear' mark that is 1653 // updated as a side effect of promoting an object. 1654 // 1655 // There is guaranteed to be enough room at the top of the to space 1656 // for the addresses of promoted objects: every object promoted 1657 // frees up its size in bytes from the top of the new space, and 1658 // objects are at least one pointer in size. 1659 Address new_space_front = new_space_.ToSpaceStart(); 1660 promotion_queue_.Initialize(); 1661 1662 PromotionMode promotion_mode = CurrentPromotionMode(); 1663 ScavengeVisitor scavenge_visitor(this); 1664 1665 if (FLAG_scavenge_reclaim_unmodified_objects) { 1666 isolate()->global_handles()->IdentifyWeakUnmodifiedObjects( 1667 &IsUnmodifiedHeapObject); 1668 } 1669 1670 { 1671 // Copy roots. 1672 TRACE_GC(tracer(), GCTracer::Scope::SCAVENGER_ROOTS); 1673 IterateRoots(&scavenge_visitor, VISIT_ALL_IN_SCAVENGE); 1674 } 1675 1676 { 1677 // Copy objects reachable from the old generation. 1678 TRACE_GC(tracer(), GCTracer::Scope::SCAVENGER_OLD_TO_NEW_POINTERS); 1679 RememberedSet<OLD_TO_NEW>::Iterate(this, [this](Address addr) { 1680 return Scavenger::CheckAndScavengeObject(this, addr); 1681 }); 1682 1683 RememberedSet<OLD_TO_NEW>::IterateTyped( 1684 this, [this](SlotType type, Address host_addr, Address addr) { 1685 return UpdateTypedSlotHelper::UpdateTypedSlot( 1686 isolate(), type, addr, [this](Object** addr) { 1687 // We expect that objects referenced by code are long living. 1688 // If we do not force promotion, then we need to clear 1689 // old_to_new slots in dead code objects after mark-compact. 1690 return Scavenger::CheckAndScavengeObject( 1691 this, reinterpret_cast<Address>(addr)); 1692 }); 1693 }); 1694 } 1695 1696 { 1697 TRACE_GC(tracer(), GCTracer::Scope::SCAVENGER_WEAK); 1698 // Copy objects reachable from the encountered weak collections list. 1699 scavenge_visitor.VisitPointer(&encountered_weak_collections_); 1700 // Copy objects reachable from the encountered weak cells. 1701 scavenge_visitor.VisitPointer(&encountered_weak_cells_); 1702 } 1703 1704 { 1705 // Copy objects reachable from the code flushing candidates list. 1706 TRACE_GC(tracer(), GCTracer::Scope::SCAVENGER_CODE_FLUSH_CANDIDATES); 1707 MarkCompactCollector* collector = mark_compact_collector(); 1708 if (collector->is_code_flushing_enabled()) { 1709 collector->code_flusher()->IteratePointersToFromSpace(&scavenge_visitor); 1710 } 1711 } 1712 1713 { 1714 TRACE_GC(tracer(), GCTracer::Scope::SCAVENGER_SEMISPACE); 1715 new_space_front = 1716 DoScavenge(&scavenge_visitor, new_space_front, promotion_mode); 1717 } 1718 1719 if (FLAG_scavenge_reclaim_unmodified_objects) { 1720 isolate()->global_handles()->MarkNewSpaceWeakUnmodifiedObjectsPending( 1721 &IsUnscavengedHeapObject); 1722 1723 isolate()->global_handles()->IterateNewSpaceWeakUnmodifiedRoots( 1724 &scavenge_visitor); 1725 new_space_front = 1726 DoScavenge(&scavenge_visitor, new_space_front, promotion_mode); 1727 } else { 1728 TRACE_GC(tracer(), GCTracer::Scope::SCAVENGER_OBJECT_GROUPS); 1729 while (isolate()->global_handles()->IterateObjectGroups( 1730 &scavenge_visitor, &IsUnscavengedHeapObject)) { 1731 new_space_front = 1732 DoScavenge(&scavenge_visitor, new_space_front, promotion_mode); 1733 } 1734 isolate()->global_handles()->RemoveObjectGroups(); 1735 isolate()->global_handles()->RemoveImplicitRefGroups(); 1736 1737 isolate()->global_handles()->IdentifyNewSpaceWeakIndependentHandles( 1738 &IsUnscavengedHeapObject); 1739 1740 isolate()->global_handles()->IterateNewSpaceWeakIndependentRoots( 1741 &scavenge_visitor); 1742 new_space_front = 1743 DoScavenge(&scavenge_visitor, new_space_front, promotion_mode); 1744 } 1745 1746 UpdateNewSpaceReferencesInExternalStringTable( 1747 &UpdateNewSpaceReferenceInExternalStringTableEntry); 1748 1749 promotion_queue_.Destroy(); 1750 1751 incremental_marking()->UpdateMarkingDequeAfterScavenge(); 1752 1753 ScavengeWeakObjectRetainer weak_object_retainer(this); 1754 ProcessYoungWeakReferences(&weak_object_retainer); 1755 1756 DCHECK(new_space_front == new_space_.top()); 1757 1758 // Set age mark. 1759 new_space_.set_age_mark(new_space_.top()); 1760 1761 ArrayBufferTracker::FreeDeadInNewSpace(this); 1762 1763 // Update how much has survived scavenge. 1764 IncrementYoungSurvivorsCounter(static_cast<int>( 1765 (PromotedSpaceSizeOfObjects() - survived_watermark) + new_space_.Size())); 1766 1767 LOG(isolate_, ResourceEvent("scavenge", "end")); 1768 1769 gc_state_ = NOT_IN_GC; 1770 } 1771 1772 1773 String* Heap::UpdateNewSpaceReferenceInExternalStringTableEntry(Heap* heap, 1774 Object** p) { 1775 MapWord first_word = HeapObject::cast(*p)->map_word(); 1776 1777 if (!first_word.IsForwardingAddress()) { 1778 // Unreachable external string can be finalized. 1779 heap->FinalizeExternalString(String::cast(*p)); 1780 return NULL; 1781 } 1782 1783 // String is still reachable. 1784 return String::cast(first_word.ToForwardingAddress()); 1785 } 1786 1787 1788 void Heap::UpdateNewSpaceReferencesInExternalStringTable( 1789 ExternalStringTableUpdaterCallback updater_func) { 1790 if (external_string_table_.new_space_strings_.is_empty()) return; 1791 1792 Object** start = &external_string_table_.new_space_strings_[0]; 1793 Object** end = start + external_string_table_.new_space_strings_.length(); 1794 Object** last = start; 1795 1796 for (Object** p = start; p < end; ++p) { 1797 String* target = updater_func(this, p); 1798 1799 if (target == NULL) continue; 1800 1801 DCHECK(target->IsExternalString()); 1802 1803 if (InNewSpace(target)) { 1804 // String is still in new space. Update the table entry. 1805 *last = target; 1806 ++last; 1807 } else { 1808 // String got promoted. Move it to the old string list. 1809 external_string_table_.AddOldString(target); 1810 } 1811 } 1812 1813 DCHECK(last <= end); 1814 external_string_table_.ShrinkNewStrings(static_cast<int>(last - start)); 1815 } 1816 1817 1818 void Heap::UpdateReferencesInExternalStringTable( 1819 ExternalStringTableUpdaterCallback updater_func) { 1820 // Update old space string references. 1821 if (external_string_table_.old_space_strings_.length() > 0) { 1822 Object** start = &external_string_table_.old_space_strings_[0]; 1823 Object** end = start + external_string_table_.old_space_strings_.length(); 1824 for (Object** p = start; p < end; ++p) *p = updater_func(this, p); 1825 } 1826 1827 UpdateNewSpaceReferencesInExternalStringTable(updater_func); 1828 } 1829 1830 1831 void Heap::ProcessAllWeakReferences(WeakObjectRetainer* retainer) { 1832 ProcessNativeContexts(retainer); 1833 ProcessAllocationSites(retainer); 1834 } 1835 1836 1837 void Heap::ProcessYoungWeakReferences(WeakObjectRetainer* retainer) { 1838 ProcessNativeContexts(retainer); 1839 } 1840 1841 1842 void Heap::ProcessNativeContexts(WeakObjectRetainer* retainer) { 1843 Object* head = VisitWeakList<Context>(this, native_contexts_list(), retainer); 1844 // Update the head of the list of contexts. 1845 set_native_contexts_list(head); 1846 } 1847 1848 1849 void Heap::ProcessAllocationSites(WeakObjectRetainer* retainer) { 1850 Object* allocation_site_obj = 1851 VisitWeakList<AllocationSite>(this, allocation_sites_list(), retainer); 1852 set_allocation_sites_list(allocation_site_obj); 1853 } 1854 1855 void Heap::ProcessWeakListRoots(WeakObjectRetainer* retainer) { 1856 set_native_contexts_list(retainer->RetainAs(native_contexts_list())); 1857 set_allocation_sites_list(retainer->RetainAs(allocation_sites_list())); 1858 } 1859 1860 void Heap::ResetAllAllocationSitesDependentCode(PretenureFlag flag) { 1861 DisallowHeapAllocation no_allocation_scope; 1862 Object* cur = allocation_sites_list(); 1863 bool marked = false; 1864 while (cur->IsAllocationSite()) { 1865 AllocationSite* casted = AllocationSite::cast(cur); 1866 if (casted->GetPretenureMode() == flag) { 1867 casted->ResetPretenureDecision(); 1868 casted->set_deopt_dependent_code(true); 1869 marked = true; 1870 RemoveAllocationSitePretenuringFeedback(casted); 1871 } 1872 cur = casted->weak_next(); 1873 } 1874 if (marked) isolate_->stack_guard()->RequestDeoptMarkedAllocationSites(); 1875 } 1876 1877 1878 void Heap::EvaluateOldSpaceLocalPretenuring( 1879 uint64_t size_of_objects_before_gc) { 1880 uint64_t size_of_objects_after_gc = SizeOfObjects(); 1881 double old_generation_survival_rate = 1882 (static_cast<double>(size_of_objects_after_gc) * 100) / 1883 static_cast<double>(size_of_objects_before_gc); 1884 1885 if (old_generation_survival_rate < kOldSurvivalRateLowThreshold) { 1886 // Too many objects died in the old generation, pretenuring of wrong 1887 // allocation sites may be the cause for that. We have to deopt all 1888 // dependent code registered in the allocation sites to re-evaluate 1889 // our pretenuring decisions. 1890 ResetAllAllocationSitesDependentCode(TENURED); 1891 if (FLAG_trace_pretenuring) { 1892 PrintF( 1893 "Deopt all allocation sites dependent code due to low survival " 1894 "rate in the old generation %f\n", 1895 old_generation_survival_rate); 1896 } 1897 } 1898 } 1899 1900 1901 void Heap::VisitExternalResources(v8::ExternalResourceVisitor* visitor) { 1902 DisallowHeapAllocation no_allocation; 1903 // All external strings are listed in the external string table. 1904 1905 class ExternalStringTableVisitorAdapter : public ObjectVisitor { 1906 public: 1907 explicit ExternalStringTableVisitorAdapter( 1908 v8::ExternalResourceVisitor* visitor) 1909 : visitor_(visitor) {} 1910 virtual void VisitPointers(Object** start, Object** end) { 1911 for (Object** p = start; p < end; p++) { 1912 DCHECK((*p)->IsExternalString()); 1913 visitor_->VisitExternalString( 1914 Utils::ToLocal(Handle<String>(String::cast(*p)))); 1915 } 1916 } 1917 1918 private: 1919 v8::ExternalResourceVisitor* visitor_; 1920 } external_string_table_visitor(visitor); 1921 1922 external_string_table_.Iterate(&external_string_table_visitor); 1923 } 1924 1925 Address Heap::DoScavenge(ObjectVisitor* scavenge_visitor, 1926 Address new_space_front, 1927 PromotionMode promotion_mode) { 1928 do { 1929 SemiSpace::AssertValidRange(new_space_front, new_space_.top()); 1930 // The addresses new_space_front and new_space_.top() define a 1931 // queue of unprocessed copied objects. Process them until the 1932 // queue is empty. 1933 while (new_space_front != new_space_.top()) { 1934 if (!Page::IsAlignedToPageSize(new_space_front)) { 1935 HeapObject* object = HeapObject::FromAddress(new_space_front); 1936 if (promotion_mode == PROMOTE_MARKED) { 1937 new_space_front += StaticScavengeVisitor<PROMOTE_MARKED>::IterateBody( 1938 object->map(), object); 1939 } else { 1940 new_space_front += 1941 StaticScavengeVisitor<DEFAULT_PROMOTION>::IterateBody( 1942 object->map(), object); 1943 } 1944 } else { 1945 new_space_front = Page::FromAllocationAreaAddress(new_space_front) 1946 ->next_page() 1947 ->area_start(); 1948 } 1949 } 1950 1951 // Promote and process all the to-be-promoted objects. 1952 { 1953 while (!promotion_queue()->is_empty()) { 1954 HeapObject* target; 1955 int32_t size; 1956 bool was_marked_black; 1957 promotion_queue()->remove(&target, &size, &was_marked_black); 1958 1959 // Promoted object might be already partially visited 1960 // during old space pointer iteration. Thus we search specifically 1961 // for pointers to from semispace instead of looking for pointers 1962 // to new space. 1963 DCHECK(!target->IsMap()); 1964 1965 IteratePromotedObject(target, static_cast<int>(size), was_marked_black, 1966 &Scavenger::ScavengeObject); 1967 } 1968 } 1969 1970 // Take another spin if there are now unswept objects in new space 1971 // (there are currently no more unswept promoted objects). 1972 } while (new_space_front != new_space_.top()); 1973 1974 return new_space_front; 1975 } 1976 1977 1978 STATIC_ASSERT((FixedDoubleArray::kHeaderSize & kDoubleAlignmentMask) == 1979 0); // NOLINT 1980 STATIC_ASSERT((FixedTypedArrayBase::kDataOffset & kDoubleAlignmentMask) == 1981 0); // NOLINT 1982 #ifdef V8_HOST_ARCH_32_BIT 1983 STATIC_ASSERT((HeapNumber::kValueOffset & kDoubleAlignmentMask) != 1984 0); // NOLINT 1985 #endif 1986 1987 1988 int Heap::GetMaximumFillToAlign(AllocationAlignment alignment) { 1989 switch (alignment) { 1990 case kWordAligned: 1991 return 0; 1992 case kDoubleAligned: 1993 case kDoubleUnaligned: 1994 return kDoubleSize - kPointerSize; 1995 case kSimd128Unaligned: 1996 return kSimd128Size - kPointerSize; 1997 default: 1998 UNREACHABLE(); 1999 } 2000 return 0; 2001 } 2002 2003 2004 int Heap::GetFillToAlign(Address address, AllocationAlignment alignment) { 2005 intptr_t offset = OffsetFrom(address); 2006 if (alignment == kDoubleAligned && (offset & kDoubleAlignmentMask) != 0) 2007 return kPointerSize; 2008 if (alignment == kDoubleUnaligned && (offset & kDoubleAlignmentMask) == 0) 2009 return kDoubleSize - kPointerSize; // No fill if double is always aligned. 2010 if (alignment == kSimd128Unaligned) { 2011 return (kSimd128Size - (static_cast<int>(offset) + kPointerSize)) & 2012 kSimd128AlignmentMask; 2013 } 2014 return 0; 2015 } 2016 2017 2018 HeapObject* Heap::PrecedeWithFiller(HeapObject* object, int filler_size) { 2019 CreateFillerObjectAt(object->address(), filler_size, ClearRecordedSlots::kNo); 2020 return HeapObject::FromAddress(object->address() + filler_size); 2021 } 2022 2023 2024 HeapObject* Heap::AlignWithFiller(HeapObject* object, int object_size, 2025 int allocation_size, 2026 AllocationAlignment alignment) { 2027 int filler_size = allocation_size - object_size; 2028 DCHECK(filler_size > 0); 2029 int pre_filler = GetFillToAlign(object->address(), alignment); 2030 if (pre_filler) { 2031 object = PrecedeWithFiller(object, pre_filler); 2032 filler_size -= pre_filler; 2033 } 2034 if (filler_size) 2035 CreateFillerObjectAt(object->address() + object_size, filler_size, 2036 ClearRecordedSlots::kNo); 2037 return object; 2038 } 2039 2040 2041 HeapObject* Heap::DoubleAlignForDeserialization(HeapObject* object, int size) { 2042 return AlignWithFiller(object, size - kPointerSize, size, kDoubleAligned); 2043 } 2044 2045 2046 void Heap::RegisterNewArrayBuffer(JSArrayBuffer* buffer) { 2047 ArrayBufferTracker::RegisterNew(this, buffer); 2048 } 2049 2050 2051 void Heap::UnregisterArrayBuffer(JSArrayBuffer* buffer) { 2052 ArrayBufferTracker::Unregister(this, buffer); 2053 } 2054 2055 2056 void Heap::ConfigureInitialOldGenerationSize() { 2057 if (!old_generation_size_configured_ && tracer()->SurvivalEventsRecorded()) { 2058 old_generation_allocation_limit_ = 2059 Max(kMinimumOldGenerationAllocationLimit, 2060 static_cast<intptr_t>( 2061 static_cast<double>(old_generation_allocation_limit_) * 2062 (tracer()->AverageSurvivalRatio() / 100))); 2063 } 2064 } 2065 2066 2067 AllocationResult Heap::AllocatePartialMap(InstanceType instance_type, 2068 int instance_size) { 2069 Object* result = nullptr; 2070 AllocationResult allocation = AllocateRaw(Map::kSize, MAP_SPACE); 2071 if (!allocation.To(&result)) return allocation; 2072 2073 // Map::cast cannot be used due to uninitialized map field. 2074 reinterpret_cast<Map*>(result)->set_map( 2075 reinterpret_cast<Map*>(root(kMetaMapRootIndex))); 2076 reinterpret_cast<Map*>(result)->set_instance_type(instance_type); 2077 reinterpret_cast<Map*>(result)->set_instance_size(instance_size); 2078 // Initialize to only containing tagged fields. 2079 reinterpret_cast<Map*>(result)->set_visitor_id( 2080 StaticVisitorBase::GetVisitorId(instance_type, instance_size, false)); 2081 if (FLAG_unbox_double_fields) { 2082 reinterpret_cast<Map*>(result) 2083 ->set_layout_descriptor(LayoutDescriptor::FastPointerLayout()); 2084 } 2085 reinterpret_cast<Map*>(result)->clear_unused(); 2086 reinterpret_cast<Map*>(result) 2087 ->set_inobject_properties_or_constructor_function_index(0); 2088 reinterpret_cast<Map*>(result)->set_unused_property_fields(0); 2089 reinterpret_cast<Map*>(result)->set_bit_field(0); 2090 reinterpret_cast<Map*>(result)->set_bit_field2(0); 2091 int bit_field3 = Map::EnumLengthBits::encode(kInvalidEnumCacheSentinel) | 2092 Map::OwnsDescriptors::encode(true) | 2093 Map::ConstructionCounter::encode(Map::kNoSlackTracking); 2094 reinterpret_cast<Map*>(result)->set_bit_field3(bit_field3); 2095 reinterpret_cast<Map*>(result)->set_weak_cell_cache(Smi::FromInt(0)); 2096 return result; 2097 } 2098 2099 2100 AllocationResult Heap::AllocateMap(InstanceType instance_type, 2101 int instance_size, 2102 ElementsKind elements_kind) { 2103 HeapObject* result = nullptr; 2104 AllocationResult allocation = AllocateRaw(Map::kSize, MAP_SPACE); 2105 if (!allocation.To(&result)) return allocation; 2106 2107 isolate()->counters()->maps_created()->Increment(); 2108 result->set_map_no_write_barrier(meta_map()); 2109 Map* map = Map::cast(result); 2110 map->set_instance_type(instance_type); 2111 map->set_prototype(null_value(), SKIP_WRITE_BARRIER); 2112 map->set_constructor_or_backpointer(null_value(), SKIP_WRITE_BARRIER); 2113 map->set_instance_size(instance_size); 2114 map->clear_unused(); 2115 map->set_inobject_properties_or_constructor_function_index(0); 2116 map->set_code_cache(empty_fixed_array(), SKIP_WRITE_BARRIER); 2117 map->set_dependent_code(DependentCode::cast(empty_fixed_array()), 2118 SKIP_WRITE_BARRIER); 2119 map->set_weak_cell_cache(Smi::FromInt(0)); 2120 map->set_raw_transitions(Smi::FromInt(0)); 2121 map->set_unused_property_fields(0); 2122 map->set_instance_descriptors(empty_descriptor_array()); 2123 if (FLAG_unbox_double_fields) { 2124 map->set_layout_descriptor(LayoutDescriptor::FastPointerLayout()); 2125 } 2126 // Must be called only after |instance_type|, |instance_size| and 2127 // |layout_descriptor| are set. 2128 map->set_visitor_id(Heap::GetStaticVisitorIdForMap(map)); 2129 map->set_bit_field(0); 2130 map->set_bit_field2(1 << Map::kIsExtensible); 2131 int bit_field3 = Map::EnumLengthBits::encode(kInvalidEnumCacheSentinel) | 2132 Map::OwnsDescriptors::encode(true) | 2133 Map::ConstructionCounter::encode(Map::kNoSlackTracking); 2134 map->set_bit_field3(bit_field3); 2135 map->set_elements_kind(elements_kind); 2136 map->set_new_target_is_base(true); 2137 2138 return map; 2139 } 2140 2141 2142 AllocationResult Heap::AllocateFillerObject(int size, bool double_align, 2143 AllocationSpace space) { 2144 HeapObject* obj = nullptr; 2145 { 2146 AllocationAlignment align = double_align ? kDoubleAligned : kWordAligned; 2147 AllocationResult allocation = AllocateRaw(size, space, align); 2148 if (!allocation.To(&obj)) return allocation; 2149 } 2150 #ifdef DEBUG 2151 MemoryChunk* chunk = MemoryChunk::FromAddress(obj->address()); 2152 DCHECK(chunk->owner()->identity() == space); 2153 #endif 2154 CreateFillerObjectAt(obj->address(), size, ClearRecordedSlots::kNo); 2155 return obj; 2156 } 2157 2158 2159 const Heap::StringTypeTable Heap::string_type_table[] = { 2160 #define STRING_TYPE_ELEMENT(type, size, name, camel_name) \ 2161 { type, size, k##camel_name##MapRootIndex } \ 2162 , 2163 STRING_TYPE_LIST(STRING_TYPE_ELEMENT) 2164 #undef STRING_TYPE_ELEMENT 2165 }; 2166 2167 2168 const Heap::ConstantStringTable Heap::constant_string_table[] = { 2169 {"", kempty_stringRootIndex}, 2170 #define CONSTANT_STRING_ELEMENT(name, contents) \ 2171 { contents, k##name##RootIndex } \ 2172 , 2173 INTERNALIZED_STRING_LIST(CONSTANT_STRING_ELEMENT) 2174 #undef CONSTANT_STRING_ELEMENT 2175 }; 2176 2177 2178 const Heap::StructTable Heap::struct_table[] = { 2179 #define STRUCT_TABLE_ELEMENT(NAME, Name, name) \ 2180 { NAME##_TYPE, Name::kSize, k##Name##MapRootIndex } \ 2181 , 2182 STRUCT_LIST(STRUCT_TABLE_ELEMENT) 2183 #undef STRUCT_TABLE_ELEMENT 2184 }; 2185 2186 namespace { 2187 2188 void FinalizePartialMap(Heap* heap, Map* map) { 2189 map->set_code_cache(heap->empty_fixed_array()); 2190 map->set_dependent_code(DependentCode::cast(heap->empty_fixed_array())); 2191 map->set_raw_transitions(Smi::FromInt(0)); 2192 map->set_instance_descriptors(heap->empty_descriptor_array()); 2193 if (FLAG_unbox_double_fields) { 2194 map->set_layout_descriptor(LayoutDescriptor::FastPointerLayout()); 2195 } 2196 map->set_prototype(heap->null_value()); 2197 map->set_constructor_or_backpointer(heap->null_value()); 2198 } 2199 2200 } // namespace 2201 2202 bool Heap::CreateInitialMaps() { 2203 HeapObject* obj = nullptr; 2204 { 2205 AllocationResult allocation = AllocatePartialMap(MAP_TYPE, Map::kSize); 2206 if (!allocation.To(&obj)) return false; 2207 } 2208 // Map::cast cannot be used due to uninitialized map field. 2209 Map* new_meta_map = reinterpret_cast<Map*>(obj); 2210 set_meta_map(new_meta_map); 2211 new_meta_map->set_map(new_meta_map); 2212 2213 { // Partial map allocation 2214 #define ALLOCATE_PARTIAL_MAP(instance_type, size, field_name) \ 2215 { \ 2216 Map* map; \ 2217 if (!AllocatePartialMap((instance_type), (size)).To(&map)) return false; \ 2218 set_##field_name##_map(map); \ 2219 } 2220 2221 ALLOCATE_PARTIAL_MAP(FIXED_ARRAY_TYPE, kVariableSizeSentinel, fixed_array); 2222 fixed_array_map()->set_elements_kind(FAST_HOLEY_ELEMENTS); 2223 ALLOCATE_PARTIAL_MAP(ODDBALL_TYPE, Oddball::kSize, undefined); 2224 ALLOCATE_PARTIAL_MAP(ODDBALL_TYPE, Oddball::kSize, null); 2225 ALLOCATE_PARTIAL_MAP(ODDBALL_TYPE, Oddball::kSize, the_hole); 2226 2227 #undef ALLOCATE_PARTIAL_MAP 2228 } 2229 2230 // Allocate the empty array. 2231 { 2232 AllocationResult allocation = AllocateEmptyFixedArray(); 2233 if (!allocation.To(&obj)) return false; 2234 } 2235 set_empty_fixed_array(FixedArray::cast(obj)); 2236 2237 { 2238 AllocationResult allocation = Allocate(null_map(), OLD_SPACE); 2239 if (!allocation.To(&obj)) return false; 2240 } 2241 set_null_value(Oddball::cast(obj)); 2242 Oddball::cast(obj)->set_kind(Oddball::kNull); 2243 2244 { 2245 AllocationResult allocation = Allocate(undefined_map(), OLD_SPACE); 2246 if (!allocation.To(&obj)) return false; 2247 } 2248 set_undefined_value(Oddball::cast(obj)); 2249 Oddball::cast(obj)->set_kind(Oddball::kUndefined); 2250 DCHECK(!InNewSpace(undefined_value())); 2251 { 2252 AllocationResult allocation = Allocate(the_hole_map(), OLD_SPACE); 2253 if (!allocation.To(&obj)) return false; 2254 } 2255 set_the_hole_value(Oddball::cast(obj)); 2256 Oddball::cast(obj)->set_kind(Oddball::kTheHole); 2257 2258 // Set preliminary exception sentinel value before actually initializing it. 2259 set_exception(null_value()); 2260 2261 // Allocate the empty descriptor array. 2262 { 2263 AllocationResult allocation = AllocateEmptyFixedArray(); 2264 if (!allocation.To(&obj)) return false; 2265 } 2266 set_empty_descriptor_array(DescriptorArray::cast(obj)); 2267 2268 // Fix the instance_descriptors for the existing maps. 2269 FinalizePartialMap(this, meta_map()); 2270 FinalizePartialMap(this, fixed_array_map()); 2271 FinalizePartialMap(this, undefined_map()); 2272 undefined_map()->set_is_undetectable(); 2273 FinalizePartialMap(this, null_map()); 2274 null_map()->set_is_undetectable(); 2275 FinalizePartialMap(this, the_hole_map()); 2276 2277 { // Map allocation 2278 #define ALLOCATE_MAP(instance_type, size, field_name) \ 2279 { \ 2280 Map* map; \ 2281 if (!AllocateMap((instance_type), size).To(&map)) return false; \ 2282 set_##field_name##_map(map); \ 2283 } 2284 2285 #define ALLOCATE_VARSIZE_MAP(instance_type, field_name) \ 2286 ALLOCATE_MAP(instance_type, kVariableSizeSentinel, field_name) 2287 2288 #define ALLOCATE_PRIMITIVE_MAP(instance_type, size, field_name, \ 2289 constructor_function_index) \ 2290 { \ 2291 ALLOCATE_MAP((instance_type), (size), field_name); \ 2292 field_name##_map()->SetConstructorFunctionIndex( \ 2293 (constructor_function_index)); \ 2294 } 2295 2296 ALLOCATE_VARSIZE_MAP(FIXED_ARRAY_TYPE, fixed_cow_array) 2297 fixed_cow_array_map()->set_elements_kind(FAST_HOLEY_ELEMENTS); 2298 DCHECK_NE(fixed_array_map(), fixed_cow_array_map()); 2299 2300 ALLOCATE_VARSIZE_MAP(FIXED_ARRAY_TYPE, scope_info) 2301 ALLOCATE_PRIMITIVE_MAP(HEAP_NUMBER_TYPE, HeapNumber::kSize, heap_number, 2302 Context::NUMBER_FUNCTION_INDEX) 2303 ALLOCATE_MAP(MUTABLE_HEAP_NUMBER_TYPE, HeapNumber::kSize, 2304 mutable_heap_number) 2305 ALLOCATE_PRIMITIVE_MAP(SYMBOL_TYPE, Symbol::kSize, symbol, 2306 Context::SYMBOL_FUNCTION_INDEX) 2307 #define ALLOCATE_SIMD128_MAP(TYPE, Type, type, lane_count, lane_type) \ 2308 ALLOCATE_PRIMITIVE_MAP(SIMD128_VALUE_TYPE, Type::kSize, type, \ 2309 Context::TYPE##_FUNCTION_INDEX) 2310 SIMD128_TYPES(ALLOCATE_SIMD128_MAP) 2311 #undef ALLOCATE_SIMD128_MAP 2312 ALLOCATE_MAP(FOREIGN_TYPE, Foreign::kSize, foreign) 2313 2314 ALLOCATE_PRIMITIVE_MAP(ODDBALL_TYPE, Oddball::kSize, boolean, 2315 Context::BOOLEAN_FUNCTION_INDEX); 2316 ALLOCATE_MAP(ODDBALL_TYPE, Oddball::kSize, uninitialized); 2317 ALLOCATE_MAP(ODDBALL_TYPE, Oddball::kSize, arguments_marker); 2318 ALLOCATE_MAP(ODDBALL_TYPE, Oddball::kSize, no_interceptor_result_sentinel); 2319 ALLOCATE_MAP(ODDBALL_TYPE, Oddball::kSize, exception); 2320 ALLOCATE_MAP(ODDBALL_TYPE, Oddball::kSize, termination_exception); 2321 ALLOCATE_MAP(ODDBALL_TYPE, Oddball::kSize, optimized_out); 2322 ALLOCATE_MAP(ODDBALL_TYPE, Oddball::kSize, stale_register); 2323 2324 for (unsigned i = 0; i < arraysize(string_type_table); i++) { 2325 const StringTypeTable& entry = string_type_table[i]; 2326 { 2327 AllocationResult allocation = AllocateMap(entry.type, entry.size); 2328 if (!allocation.To(&obj)) return false; 2329 } 2330 Map* map = Map::cast(obj); 2331 map->SetConstructorFunctionIndex(Context::STRING_FUNCTION_INDEX); 2332 // Mark cons string maps as unstable, because their objects can change 2333 // maps during GC. 2334 if (StringShape(entry.type).IsCons()) map->mark_unstable(); 2335 roots_[entry.index] = map; 2336 } 2337 2338 { // Create a separate external one byte string map for native sources. 2339 AllocationResult allocation = 2340 AllocateMap(SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE, 2341 ExternalOneByteString::kShortSize); 2342 if (!allocation.To(&obj)) return false; 2343 Map* map = Map::cast(obj); 2344 map->SetConstructorFunctionIndex(Context::STRING_FUNCTION_INDEX); 2345 set_native_source_string_map(map); 2346 } 2347 2348 ALLOCATE_VARSIZE_MAP(FIXED_DOUBLE_ARRAY_TYPE, fixed_double_array) 2349 fixed_double_array_map()->set_elements_kind(FAST_HOLEY_DOUBLE_ELEMENTS); 2350 ALLOCATE_VARSIZE_MAP(BYTE_ARRAY_TYPE, byte_array) 2351 ALLOCATE_VARSIZE_MAP(BYTECODE_ARRAY_TYPE, bytecode_array) 2352 ALLOCATE_VARSIZE_MAP(FREE_SPACE_TYPE, free_space) 2353 2354 #define ALLOCATE_FIXED_TYPED_ARRAY_MAP(Type, type, TYPE, ctype, size) \ 2355 ALLOCATE_VARSIZE_MAP(FIXED_##TYPE##_ARRAY_TYPE, fixed_##type##_array) 2356 2357 TYPED_ARRAYS(ALLOCATE_FIXED_TYPED_ARRAY_MAP) 2358 #undef ALLOCATE_FIXED_TYPED_ARRAY_MAP 2359 2360 ALLOCATE_VARSIZE_MAP(FIXED_ARRAY_TYPE, sloppy_arguments_elements) 2361 2362 ALLOCATE_VARSIZE_MAP(CODE_TYPE, code) 2363 2364 ALLOCATE_MAP(CELL_TYPE, Cell::kSize, cell) 2365 ALLOCATE_MAP(PROPERTY_CELL_TYPE, PropertyCell::kSize, global_property_cell) 2366 ALLOCATE_MAP(WEAK_CELL_TYPE, WeakCell::kSize, weak_cell) 2367 ALLOCATE_MAP(FILLER_TYPE, kPointerSize, one_pointer_filler) 2368 ALLOCATE_MAP(FILLER_TYPE, 2 * kPointerSize, two_pointer_filler) 2369 2370 ALLOCATE_VARSIZE_MAP(TRANSITION_ARRAY_TYPE, transition_array) 2371 2372 for (unsigned i = 0; i < arraysize(struct_table); i++) { 2373 const StructTable& entry = struct_table[i]; 2374 Map* map; 2375 if (!AllocateMap(entry.type, entry.size).To(&map)) return false; 2376 roots_[entry.index] = map; 2377 } 2378 2379 ALLOCATE_VARSIZE_MAP(FIXED_ARRAY_TYPE, hash_table) 2380 ALLOCATE_VARSIZE_MAP(FIXED_ARRAY_TYPE, ordered_hash_table) 2381 2382 ALLOCATE_VARSIZE_MAP(FIXED_ARRAY_TYPE, function_context) 2383 ALLOCATE_VARSIZE_MAP(FIXED_ARRAY_TYPE, catch_context) 2384 ALLOCATE_VARSIZE_MAP(FIXED_ARRAY_TYPE, with_context) 2385 ALLOCATE_VARSIZE_MAP(FIXED_ARRAY_TYPE, debug_evaluate_context) 2386 ALLOCATE_VARSIZE_MAP(FIXED_ARRAY_TYPE, block_context) 2387 ALLOCATE_VARSIZE_MAP(FIXED_ARRAY_TYPE, module_context) 2388 ALLOCATE_VARSIZE_MAP(FIXED_ARRAY_TYPE, script_context) 2389 ALLOCATE_VARSIZE_MAP(FIXED_ARRAY_TYPE, script_context_table) 2390 2391 ALLOCATE_VARSIZE_MAP(FIXED_ARRAY_TYPE, native_context) 2392 native_context_map()->set_dictionary_map(true); 2393 native_context_map()->set_visitor_id( 2394 StaticVisitorBase::kVisitNativeContext); 2395 2396 ALLOCATE_MAP(SHARED_FUNCTION_INFO_TYPE, SharedFunctionInfo::kAlignedSize, 2397 shared_function_info) 2398 2399 ALLOCATE_MAP(JS_MESSAGE_OBJECT_TYPE, JSMessageObject::kSize, message_object) 2400 ALLOCATE_MAP(JS_OBJECT_TYPE, JSObject::kHeaderSize + kPointerSize, external) 2401 external_map()->set_is_extensible(false); 2402 #undef ALLOCATE_PRIMITIVE_MAP 2403 #undef ALLOCATE_VARSIZE_MAP 2404 #undef ALLOCATE_MAP 2405 } 2406 2407 { 2408 AllocationResult allocation = Allocate(boolean_map(), OLD_SPACE); 2409 if (!allocation.To(&obj)) return false; 2410 } 2411 set_true_value(Oddball::cast(obj)); 2412 Oddball::cast(obj)->set_kind(Oddball::kTrue); 2413 2414 { 2415 AllocationResult allocation = Allocate(boolean_map(), OLD_SPACE); 2416 if (!allocation.To(&obj)) return false; 2417 } 2418 set_false_value(Oddball::cast(obj)); 2419 Oddball::cast(obj)->set_kind(Oddball::kFalse); 2420 2421 { // Empty arrays 2422 { 2423 ByteArray* byte_array; 2424 if (!AllocateByteArray(0, TENURED).To(&byte_array)) return false; 2425 set_empty_byte_array(byte_array); 2426 } 2427 2428 #define ALLOCATE_EMPTY_FIXED_TYPED_ARRAY(Type, type, TYPE, ctype, size) \ 2429 { \ 2430 FixedTypedArrayBase* obj; \ 2431 if (!AllocateEmptyFixedTypedArray(kExternal##Type##Array).To(&obj)) \ 2432 return false; \ 2433 set_empty_fixed_##type##_array(obj); \ 2434 } 2435 2436 TYPED_ARRAYS(ALLOCATE_EMPTY_FIXED_TYPED_ARRAY) 2437 #undef ALLOCATE_EMPTY_FIXED_TYPED_ARRAY 2438 } 2439 DCHECK(!InNewSpace(empty_fixed_array())); 2440 return true; 2441 } 2442 2443 2444 AllocationResult Heap::AllocateHeapNumber(double value, MutableMode mode, 2445 PretenureFlag pretenure) { 2446 // Statically ensure that it is safe to allocate heap numbers in paged 2447 // spaces. 2448 int size = HeapNumber::kSize; 2449 STATIC_ASSERT(HeapNumber::kSize <= Page::kMaxRegularHeapObjectSize); 2450 2451 AllocationSpace space = SelectSpace(pretenure); 2452 2453 HeapObject* result = nullptr; 2454 { 2455 AllocationResult allocation = AllocateRaw(size, space, kDoubleUnaligned); 2456 if (!allocation.To(&result)) return allocation; 2457 } 2458 2459 Map* map = mode == MUTABLE ? mutable_heap_number_map() : heap_number_map(); 2460 HeapObject::cast(result)->set_map_no_write_barrier(map); 2461 HeapNumber::cast(result)->set_value(value); 2462 return result; 2463 } 2464 2465 #define SIMD_ALLOCATE_DEFINITION(TYPE, Type, type, lane_count, lane_type) \ 2466 AllocationResult Heap::Allocate##Type(lane_type lanes[lane_count], \ 2467 PretenureFlag pretenure) { \ 2468 int size = Type::kSize; \ 2469 STATIC_ASSERT(Type::kSize <= Page::kMaxRegularHeapObjectSize); \ 2470 \ 2471 AllocationSpace space = SelectSpace(pretenure); \ 2472 \ 2473 HeapObject* result = nullptr; \ 2474 { \ 2475 AllocationResult allocation = \ 2476 AllocateRaw(size, space, kSimd128Unaligned); \ 2477 if (!allocation.To(&result)) return allocation; \ 2478 } \ 2479 \ 2480 result->set_map_no_write_barrier(type##_map()); \ 2481 Type* instance = Type::cast(result); \ 2482 for (int i = 0; i < lane_count; i++) { \ 2483 instance->set_lane(i, lanes[i]); \ 2484 } \ 2485 return result; \ 2486 } 2487 SIMD128_TYPES(SIMD_ALLOCATE_DEFINITION) 2488 #undef SIMD_ALLOCATE_DEFINITION 2489 2490 2491 AllocationResult Heap::AllocateCell(Object* value) { 2492 int size = Cell::kSize; 2493 STATIC_ASSERT(Cell::kSize <= Page::kMaxRegularHeapObjectSize); 2494 2495 HeapObject* result = nullptr; 2496 { 2497 AllocationResult allocation = AllocateRaw(size, OLD_SPACE); 2498 if (!allocation.To(&result)) return allocation; 2499 } 2500 result->set_map_no_write_barrier(cell_map()); 2501 Cell::cast(result)->set_value(value); 2502 return result; 2503 } 2504 2505 2506 AllocationResult Heap::AllocatePropertyCell() { 2507 int size = PropertyCell::kSize; 2508 STATIC_ASSERT(PropertyCell::kSize <= Page::kMaxRegularHeapObjectSize); 2509 2510 HeapObject* result = nullptr; 2511 AllocationResult allocation = AllocateRaw(size, OLD_SPACE); 2512 if (!allocation.To(&result)) return allocation; 2513 2514 result->set_map_no_write_barrier(global_property_cell_map()); 2515 PropertyCell* cell = PropertyCell::cast(result); 2516 cell->set_dependent_code(DependentCode::cast(empty_fixed_array()), 2517 SKIP_WRITE_BARRIER); 2518 cell->set_property_details(PropertyDetails(Smi::FromInt(0))); 2519 cell->set_value(the_hole_value()); 2520 return result; 2521 } 2522 2523 2524 AllocationResult Heap::AllocateWeakCell(HeapObject* value) { 2525 int size = WeakCell::kSize; 2526 STATIC_ASSERT(WeakCell::kSize <= Page::kMaxRegularHeapObjectSize); 2527 HeapObject* result = nullptr; 2528 { 2529 AllocationResult allocation = AllocateRaw(size, OLD_SPACE); 2530 if (!allocation.To(&result)) return allocation; 2531 } 2532 result->set_map_no_write_barrier(weak_cell_map()); 2533 WeakCell::cast(result)->initialize(value); 2534 WeakCell::cast(result)->clear_next(the_hole_value()); 2535 return result; 2536 } 2537 2538 2539 AllocationResult Heap::AllocateTransitionArray(int capacity) { 2540 DCHECK(capacity > 0); 2541 HeapObject* raw_array = nullptr; 2542 { 2543 AllocationResult allocation = AllocateRawFixedArray(capacity, TENURED); 2544 if (!allocation.To(&raw_array)) return allocation; 2545 } 2546 raw_array->set_map_no_write_barrier(transition_array_map()); 2547 TransitionArray* array = TransitionArray::cast(raw_array); 2548 array->set_length(capacity); 2549 MemsetPointer(array->data_start(), undefined_value(), capacity); 2550 // Transition arrays are tenured. When black allocation is on we have to 2551 // add the transition array to the list of encountered_transition_arrays. 2552 if (incremental_marking()->black_allocation()) { 2553 array->set_next_link(encountered_transition_arrays(), 2554 UPDATE_WEAK_WRITE_BARRIER); 2555 set_encountered_transition_arrays(array); 2556 } else { 2557 array->set_next_link(undefined_value(), SKIP_WRITE_BARRIER); 2558 } 2559 return array; 2560 } 2561 2562 2563 void Heap::CreateApiObjects() { 2564 HandleScope scope(isolate()); 2565 Factory* factory = isolate()->factory(); 2566 Handle<Map> new_neander_map = 2567 factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); 2568 2569 // Don't use Smi-only elements optimizations for objects with the neander 2570 // map. There are too many cases where element values are set directly with a 2571 // bottleneck to trap the Smi-only -> fast elements transition, and there 2572 // appears to be no benefit for optimize this case. 2573 new_neander_map->set_elements_kind(TERMINAL_FAST_ELEMENTS_KIND); 2574 set_neander_map(*new_neander_map); 2575 2576 Handle<JSObject> listeners = factory->NewNeanderObject(); 2577 Handle<FixedArray> elements = factory->NewFixedArray(2); 2578 elements->set(0, Smi::FromInt(0)); 2579 listeners->set_elements(*elements); 2580 set_message_listeners(*listeners); 2581 } 2582 2583 2584 void Heap::CreateJSEntryStub() { 2585 JSEntryStub stub(isolate(), StackFrame::ENTRY); 2586 set_js_entry_code(*stub.GetCode()); 2587 } 2588 2589 2590 void Heap::CreateJSConstructEntryStub() { 2591 JSEntryStub stub(isolate(), StackFrame::ENTRY_CONSTRUCT); 2592 set_js_construct_entry_code(*stub.GetCode()); 2593 } 2594 2595 2596 void Heap::CreateFixedStubs() { 2597 // Here we create roots for fixed stubs. They are needed at GC 2598 // for cooking and uncooking (check out frames.cc). 2599 // The eliminates the need for doing dictionary lookup in the 2600 // stub cache for these stubs. 2601 HandleScope scope(isolate()); 2602 2603 // Create stubs that should be there, so we don't unexpectedly have to 2604 // create them if we need them during the creation of another stub. 2605 // Stub creation mixes raw pointers and handles in an unsafe manner so 2606 // we cannot create stubs while we are creating stubs. 2607 CodeStub::GenerateStubsAheadOfTime(isolate()); 2608 2609 // MacroAssembler::Abort calls (usually enabled with --debug-code) depend on 2610 // CEntryStub, so we need to call GenerateStubsAheadOfTime before JSEntryStub 2611 // is created. 2612 2613 // gcc-4.4 has problem generating correct code of following snippet: 2614 // { JSEntryStub stub; 2615 // js_entry_code_ = *stub.GetCode(); 2616 // } 2617 // { JSConstructEntryStub stub; 2618 // js_construct_entry_code_ = *stub.GetCode(); 2619 // } 2620 // To workaround the problem, make separate functions without inlining. 2621 Heap::CreateJSEntryStub(); 2622 Heap::CreateJSConstructEntryStub(); 2623 } 2624 2625 2626 void Heap::CreateInitialObjects() { 2627 HandleScope scope(isolate()); 2628 Factory* factory = isolate()->factory(); 2629 2630 // The -0 value must be set before NewNumber works. 2631 set_minus_zero_value(*factory->NewHeapNumber(-0.0, IMMUTABLE, TENURED)); 2632 DCHECK(std::signbit(minus_zero_value()->Number()) != 0); 2633 2634 set_nan_value(*factory->NewHeapNumber( 2635 std::numeric_limits<double>::quiet_NaN(), IMMUTABLE, TENURED)); 2636 set_infinity_value(*factory->NewHeapNumber(V8_INFINITY, IMMUTABLE, TENURED)); 2637 set_minus_infinity_value( 2638 *factory->NewHeapNumber(-V8_INFINITY, IMMUTABLE, TENURED)); 2639 2640 // Allocate initial string table. 2641 set_string_table(*StringTable::New(isolate(), kInitialStringTableSize)); 2642 2643 // Allocate 2644 2645 // Finish initializing oddballs after creating the string table. 2646 Oddball::Initialize(isolate(), factory->undefined_value(), "undefined", 2647 factory->nan_value(), false, "undefined", 2648 Oddball::kUndefined); 2649 2650 // Initialize the null_value. 2651 Oddball::Initialize(isolate(), factory->null_value(), "null", 2652 handle(Smi::FromInt(0), isolate()), false, "object", 2653 Oddball::kNull); 2654 2655 // Initialize the_hole_value. 2656 Oddball::Initialize(isolate(), factory->the_hole_value(), "hole", 2657 handle(Smi::FromInt(-1), isolate()), false, "undefined", 2658 Oddball::kTheHole); 2659 2660 // Initialize the true_value. 2661 Oddball::Initialize(isolate(), factory->true_value(), "true", 2662 handle(Smi::FromInt(1), isolate()), true, "boolean", 2663 Oddball::kTrue); 2664 2665 // Initialize the false_value. 2666 Oddball::Initialize(isolate(), factory->false_value(), "false", 2667 handle(Smi::FromInt(0), isolate()), false, "boolean", 2668 Oddball::kFalse); 2669 2670 set_uninitialized_value( 2671 *factory->NewOddball(factory->uninitialized_map(), "uninitialized", 2672 handle(Smi::FromInt(-1), isolate()), false, 2673 "undefined", Oddball::kUninitialized)); 2674 2675 set_arguments_marker( 2676 *factory->NewOddball(factory->arguments_marker_map(), "arguments_marker", 2677 handle(Smi::FromInt(-4), isolate()), false, 2678 "undefined", Oddball::kArgumentsMarker)); 2679 2680 set_no_interceptor_result_sentinel(*factory->NewOddball( 2681 factory->no_interceptor_result_sentinel_map(), 2682 "no_interceptor_result_sentinel", handle(Smi::FromInt(-2), isolate()), 2683 false, "undefined", Oddball::kOther)); 2684 2685 set_termination_exception(*factory->NewOddball( 2686 factory->termination_exception_map(), "termination_exception", 2687 handle(Smi::FromInt(-3), isolate()), false, "undefined", 2688 Oddball::kOther)); 2689 2690 set_exception(*factory->NewOddball(factory->exception_map(), "exception", 2691 handle(Smi::FromInt(-5), isolate()), false, 2692 "undefined", Oddball::kException)); 2693 2694 set_optimized_out( 2695 *factory->NewOddball(factory->optimized_out_map(), "optimized_out", 2696 handle(Smi::FromInt(-6), isolate()), false, 2697 "undefined", Oddball::kOptimizedOut)); 2698 2699 set_stale_register( 2700 *factory->NewOddball(factory->stale_register_map(), "stale_register", 2701 handle(Smi::FromInt(-7), isolate()), false, 2702 "undefined", Oddball::kStaleRegister)); 2703 2704 for (unsigned i = 0; i < arraysize(constant_string_table); i++) { 2705 Handle<String> str = 2706 factory->InternalizeUtf8String(constant_string_table[i].contents); 2707 roots_[constant_string_table[i].index] = *str; 2708 } 2709 2710 // Create the code_stubs dictionary. The initial size is set to avoid 2711 // expanding the dictionary during bootstrapping. 2712 set_code_stubs(*UnseededNumberDictionary::New(isolate(), 128)); 2713 2714 set_instanceof_cache_function(Smi::FromInt(0)); 2715 set_instanceof_cache_map(Smi::FromInt(0)); 2716 set_instanceof_cache_answer(Smi::FromInt(0)); 2717 2718 { 2719 HandleScope scope(isolate()); 2720 #define SYMBOL_INIT(name) \ 2721 { \ 2722 Handle<String> name##d = factory->NewStringFromStaticChars(#name); \ 2723 Handle<Symbol> symbol(isolate()->factory()->NewPrivateSymbol()); \ 2724 symbol->set_name(*name##d); \ 2725 roots_[k##name##RootIndex] = *symbol; \ 2726 } 2727 PRIVATE_SYMBOL_LIST(SYMBOL_INIT) 2728 #undef SYMBOL_INIT 2729 } 2730 2731 { 2732 HandleScope scope(isolate()); 2733 #define SYMBOL_INIT(name, description) \ 2734 Handle<Symbol> name = factory->NewSymbol(); \ 2735 Handle<String> name##d = factory->NewStringFromStaticChars(#description); \ 2736 name->set_name(*name##d); \ 2737 roots_[k##name##RootIndex] = *name; 2738 PUBLIC_SYMBOL_LIST(SYMBOL_INIT) 2739 #undef SYMBOL_INIT 2740 2741 #define SYMBOL_INIT(name, description) \ 2742 Handle<Symbol> name = factory->NewSymbol(); \ 2743 Handle<String> name##d = factory->NewStringFromStaticChars(#description); \ 2744 name->set_is_well_known_symbol(true); \ 2745 name->set_name(*name##d); \ 2746 roots_[k##name##RootIndex] = *name; 2747 WELL_KNOWN_SYMBOL_LIST(SYMBOL_INIT) 2748 #undef SYMBOL_INIT 2749 } 2750 2751 // Allocate the dictionary of intrinsic function names. 2752 Handle<NameDictionary> intrinsic_names = 2753 NameDictionary::New(isolate(), Runtime::kNumFunctions, TENURED); 2754 Runtime::InitializeIntrinsicFunctionNames(isolate(), intrinsic_names); 2755 set_intrinsic_function_names(*intrinsic_names); 2756 2757 Handle<NameDictionary> empty_properties_dictionary = 2758 NameDictionary::New(isolate(), 0, TENURED); 2759 empty_properties_dictionary->SetRequiresCopyOnCapacityChange(); 2760 set_empty_properties_dictionary(*empty_properties_dictionary); 2761 2762 set_number_string_cache( 2763 *factory->NewFixedArray(kInitialNumberStringCacheSize * 2, TENURED)); 2764 2765 // Allocate cache for single character one byte strings. 2766 set_single_character_string_cache( 2767 *factory->NewFixedArray(String::kMaxOneByteCharCode + 1, TENURED)); 2768 2769 // Allocate cache for string split and regexp-multiple. 2770 set_string_split_cache(*factory->NewFixedArray( 2771 RegExpResultsCache::kRegExpResultsCacheSize, TENURED)); 2772 set_regexp_multiple_cache(*factory->NewFixedArray( 2773 RegExpResultsCache::kRegExpResultsCacheSize, TENURED)); 2774 2775 // Allocate cache for external strings pointing to native source code. 2776 set_natives_source_cache( 2777 *factory->NewFixedArray(Natives::GetBuiltinsCount())); 2778 2779 set_experimental_natives_source_cache( 2780 *factory->NewFixedArray(ExperimentalNatives::GetBuiltinsCount())); 2781 2782 set_extra_natives_source_cache( 2783 *factory->NewFixedArray(ExtraNatives::GetBuiltinsCount())); 2784 2785 set_experimental_extra_natives_source_cache( 2786 *factory->NewFixedArray(ExperimentalExtraNatives::GetBuiltinsCount())); 2787 2788 set_undefined_cell(*factory->NewCell(factory->undefined_value())); 2789 2790 // The symbol registry is initialized lazily. 2791 set_symbol_registry(Smi::FromInt(0)); 2792 2793 // Microtask queue uses the empty fixed array as a sentinel for "empty". 2794 // Number of queued microtasks stored in Isolate::pending_microtask_count(). 2795 set_microtask_queue(empty_fixed_array()); 2796 2797 { 2798 StaticFeedbackVectorSpec spec; 2799 FeedbackVectorSlot load_ic_slot = spec.AddLoadICSlot(); 2800 FeedbackVectorSlot keyed_load_ic_slot = spec.AddKeyedLoadICSlot(); 2801 FeedbackVectorSlot store_ic_slot = spec.AddStoreICSlot(); 2802 FeedbackVectorSlot keyed_store_ic_slot = spec.AddKeyedStoreICSlot(); 2803 2804 DCHECK_EQ(load_ic_slot, 2805 FeedbackVectorSlot(TypeFeedbackVector::kDummyLoadICSlot)); 2806 DCHECK_EQ(keyed_load_ic_slot, 2807 FeedbackVectorSlot(TypeFeedbackVector::kDummyKeyedLoadICSlot)); 2808 DCHECK_EQ(store_ic_slot, 2809 FeedbackVectorSlot(TypeFeedbackVector::kDummyStoreICSlot)); 2810 DCHECK_EQ(keyed_store_ic_slot, 2811 FeedbackVectorSlot(TypeFeedbackVector::kDummyKeyedStoreICSlot)); 2812 2813 Handle<TypeFeedbackMetadata> dummy_metadata = 2814 TypeFeedbackMetadata::New(isolate(), &spec); 2815 Handle<TypeFeedbackVector> dummy_vector = 2816 TypeFeedbackVector::New(isolate(), dummy_metadata); 2817 2818 Object* megamorphic = *TypeFeedbackVector::MegamorphicSentinel(isolate()); 2819 dummy_vector->Set(load_ic_slot, megamorphic, SKIP_WRITE_BARRIER); 2820 dummy_vector->Set(keyed_load_ic_slot, megamorphic, SKIP_WRITE_BARRIER); 2821 dummy_vector->Set(store_ic_slot, megamorphic, SKIP_WRITE_BARRIER); 2822 dummy_vector->Set(keyed_store_ic_slot, megamorphic, SKIP_WRITE_BARRIER); 2823 2824 set_dummy_vector(*dummy_vector); 2825 } 2826 2827 { 2828 Handle<FixedArray> empty_literals_array = 2829 factory->NewFixedArray(1, TENURED); 2830 empty_literals_array->set(0, *factory->empty_fixed_array()); 2831 set_empty_literals_array(*empty_literals_array); 2832 } 2833 2834 { 2835 Handle<FixedArray> empty_sloppy_arguments_elements = 2836 factory->NewFixedArray(2, TENURED); 2837 empty_sloppy_arguments_elements->set_map(sloppy_arguments_elements_map()); 2838 set_empty_sloppy_arguments_elements(*empty_sloppy_arguments_elements); 2839 } 2840 2841 { 2842 Handle<WeakCell> cell = factory->NewWeakCell(factory->undefined_value()); 2843 set_empty_weak_cell(*cell); 2844 cell->clear(); 2845 2846 Handle<FixedArray> cleared_optimized_code_map = 2847 factory->NewFixedArray(SharedFunctionInfo::kEntriesStart, TENURED); 2848 cleared_optimized_code_map->set(SharedFunctionInfo::kSharedCodeIndex, 2849 *cell); 2850 STATIC_ASSERT(SharedFunctionInfo::kEntriesStart == 1 && 2851 SharedFunctionInfo::kSharedCodeIndex == 0); 2852 set_cleared_optimized_code_map(*cleared_optimized_code_map); 2853 } 2854 2855 set_detached_contexts(empty_fixed_array()); 2856 set_retained_maps(ArrayList::cast(empty_fixed_array())); 2857 2858 set_weak_object_to_code_table( 2859 *WeakHashTable::New(isolate(), 16, USE_DEFAULT_MINIMUM_CAPACITY, 2860 TENURED)); 2861 2862 set_script_list(Smi::FromInt(0)); 2863 2864 Handle<SeededNumberDictionary> slow_element_dictionary = 2865 SeededNumberDictionary::New(isolate(), 0, TENURED); 2866 slow_element_dictionary->set_requires_slow_elements(); 2867 set_empty_slow_element_dictionary(*slow_element_dictionary); 2868 2869 set_materialized_objects(*factory->NewFixedArray(0, TENURED)); 2870 2871 // Handling of script id generation is in Heap::NextScriptId(). 2872 set_last_script_id(Smi::FromInt(v8::UnboundScript::kNoScriptId)); 2873 set_next_template_serial_number(Smi::FromInt(0)); 2874 2875 // Allocate the empty script. 2876 Handle<Script> script = factory->NewScript(factory->empty_string()); 2877 script->set_type(Script::TYPE_NATIVE); 2878 set_empty_script(*script); 2879 2880 Handle<PropertyCell> cell = factory->NewPropertyCell(); 2881 cell->set_value(Smi::FromInt(Isolate::kArrayProtectorValid)); 2882 set_array_protector(*cell); 2883 2884 cell = factory->NewPropertyCell(); 2885 cell->set_value(the_hole_value()); 2886 set_empty_property_cell(*cell); 2887 2888 cell = factory->NewPropertyCell(); 2889 cell->set_value(Smi::FromInt(Isolate::kArrayProtectorValid)); 2890 set_has_instance_protector(*cell); 2891 2892 Handle<Cell> is_concat_spreadable_cell = factory->NewCell( 2893 handle(Smi::FromInt(Isolate::kArrayProtectorValid), isolate())); 2894 set_is_concat_spreadable_protector(*is_concat_spreadable_cell); 2895 2896 Handle<Cell> species_cell = factory->NewCell( 2897 handle(Smi::FromInt(Isolate::kArrayProtectorValid), isolate())); 2898 set_species_protector(*species_cell); 2899 2900 set_serialized_templates(empty_fixed_array()); 2901 2902 set_weak_stack_trace_list(Smi::FromInt(0)); 2903 2904 set_noscript_shared_function_infos(Smi::FromInt(0)); 2905 2906 // Initialize keyed lookup cache. 2907 isolate_->keyed_lookup_cache()->Clear(); 2908 2909 // Initialize context slot cache. 2910 isolate_->context_slot_cache()->Clear(); 2911 2912 // Initialize descriptor cache. 2913 isolate_->descriptor_lookup_cache()->Clear(); 2914 2915 // Initialize compilation cache. 2916 isolate_->compilation_cache()->Clear(); 2917 2918 CreateFixedStubs(); 2919 } 2920 2921 2922 bool Heap::RootCanBeWrittenAfterInitialization(Heap::RootListIndex root_index) { 2923 switch (root_index) { 2924 case kNumberStringCacheRootIndex: 2925 case kInstanceofCacheFunctionRootIndex: 2926 case kInstanceofCacheMapRootIndex: 2927 case kInstanceofCacheAnswerRootIndex: 2928 case kCodeStubsRootIndex: 2929 case kEmptyScriptRootIndex: 2930 case kSymbolRegistryRootIndex: 2931 case kScriptListRootIndex: 2932 case kMaterializedObjectsRootIndex: 2933 case kMicrotaskQueueRootIndex: 2934 case kDetachedContextsRootIndex: 2935 case kWeakObjectToCodeTableRootIndex: 2936 case kRetainedMapsRootIndex: 2937 case kNoScriptSharedFunctionInfosRootIndex: 2938 case kWeakStackTraceListRootIndex: 2939 case kSerializedTemplatesRootIndex: 2940 // Smi values 2941 #define SMI_ENTRY(type, name, Name) case k##Name##RootIndex: 2942 SMI_ROOT_LIST(SMI_ENTRY) 2943 #undef SMI_ENTRY 2944 // String table 2945 case kStringTableRootIndex: 2946 return true; 2947 2948 default: 2949 return false; 2950 } 2951 } 2952 2953 2954 bool Heap::RootCanBeTreatedAsConstant(RootListIndex root_index) { 2955 return !RootCanBeWrittenAfterInitialization(root_index) && 2956 !InNewSpace(root(root_index)); 2957 } 2958 2959 2960 int Heap::FullSizeNumberStringCacheLength() { 2961 // Compute the size of the number string cache based on the max newspace size. 2962 // The number string cache has a minimum size based on twice the initial cache 2963 // size to ensure that it is bigger after being made 'full size'. 2964 int number_string_cache_size = max_semi_space_size_ / 512; 2965 number_string_cache_size = Max(kInitialNumberStringCacheSize * 2, 2966 Min(0x4000, number_string_cache_size)); 2967 // There is a string and a number per entry so the length is twice the number 2968 // of entries. 2969 return number_string_cache_size * 2; 2970 } 2971 2972 2973 void Heap::FlushNumberStringCache() { 2974 // Flush the number to string cache. 2975 int len = number_string_cache()->length(); 2976 for (int i = 0; i < len; i++) { 2977 number_string_cache()->set_undefined(i); 2978 } 2979 } 2980 2981 2982 Map* Heap::MapForFixedTypedArray(ExternalArrayType array_type) { 2983 return Map::cast(roots_[RootIndexForFixedTypedArray(array_type)]); 2984 } 2985 2986 2987 Heap::RootListIndex Heap::RootIndexForFixedTypedArray( 2988 ExternalArrayType array_type) { 2989 switch (array_type) { 2990 #define ARRAY_TYPE_TO_ROOT_INDEX(Type, type, TYPE, ctype, size) \ 2991 case kExternal##Type##Array: \ 2992 return kFixed##Type##ArrayMapRootIndex; 2993 2994 TYPED_ARRAYS(ARRAY_TYPE_TO_ROOT_INDEX) 2995 #undef ARRAY_TYPE_TO_ROOT_INDEX 2996 2997 default: 2998 UNREACHABLE(); 2999 return kUndefinedValueRootIndex; 3000 } 3001 } 3002 3003 3004 Heap::RootListIndex Heap::RootIndexForEmptyFixedTypedArray( 3005 ElementsKind elementsKind) { 3006 switch (elementsKind) { 3007 #define ELEMENT_KIND_TO_ROOT_INDEX(Type, type, TYPE, ctype, size) \ 3008 case TYPE##_ELEMENTS: \ 3009 return kEmptyFixed##Type##ArrayRootIndex; 3010 3011 TYPED_ARRAYS(ELEMENT_KIND_TO_ROOT_INDEX) 3012 #undef ELEMENT_KIND_TO_ROOT_INDEX 3013 default: 3014 UNREACHABLE(); 3015 return kUndefinedValueRootIndex; 3016 } 3017 } 3018 3019 3020 FixedTypedArrayBase* Heap::EmptyFixedTypedArrayForMap(Map* map) { 3021 return FixedTypedArrayBase::cast( 3022 roots_[RootIndexForEmptyFixedTypedArray(map->elements_kind())]); 3023 } 3024 3025 3026 AllocationResult Heap::AllocateForeign(Address address, 3027 PretenureFlag pretenure) { 3028 // Statically ensure that it is safe to allocate foreigns in paged spaces. 3029 STATIC_ASSERT(Foreign::kSize <= Page::kMaxRegularHeapObjectSize); 3030 AllocationSpace space = (pretenure == TENURED) ? OLD_SPACE : NEW_SPACE; 3031 Foreign* result = nullptr; 3032 AllocationResult allocation = Allocate(foreign_map(), space); 3033 if (!allocation.To(&result)) return allocation; 3034 result->set_foreign_address(address); 3035 return result; 3036 } 3037 3038 3039 AllocationResult Heap::AllocateByteArray(int length, PretenureFlag pretenure) { 3040 if (length < 0 || length > ByteArray::kMaxLength) { 3041 v8::internal::Heap::FatalProcessOutOfMemory("invalid array length", true); 3042 } 3043 int size = ByteArray::SizeFor(length); 3044 AllocationSpace space = SelectSpace(pretenure); 3045 HeapObject* result = nullptr; 3046 { 3047 AllocationResult allocation = AllocateRaw(size, space); 3048 if (!allocation.To(&result)) return allocation; 3049 } 3050 3051 result->set_map_no_write_barrier(byte_array_map()); 3052 ByteArray::cast(result)->set_length(length); 3053 return result; 3054 } 3055 3056 3057 AllocationResult Heap::AllocateBytecodeArray(int length, 3058 const byte* const raw_bytecodes, 3059 int frame_size, 3060 int parameter_count, 3061 FixedArray* constant_pool) { 3062 if (length < 0 || length > BytecodeArray::kMaxLength) { 3063 v8::internal::Heap::FatalProcessOutOfMemory("invalid array length", true); 3064 } 3065 // Bytecode array is pretenured, so constant pool array should be to. 3066 DCHECK(!InNewSpace(constant_pool)); 3067 3068 int size = BytecodeArray::SizeFor(length); 3069 HeapObject* result = nullptr; 3070 { 3071 AllocationResult allocation = AllocateRaw(size, OLD_SPACE); 3072 if (!allocation.To(&result)) return allocation; 3073 } 3074 3075 result->set_map_no_write_barrier(bytecode_array_map()); 3076 BytecodeArray* instance = BytecodeArray::cast(result); 3077 instance->set_length(length); 3078 instance->set_frame_size(frame_size); 3079 instance->set_parameter_count(parameter_count); 3080 instance->set_interrupt_budget(interpreter::Interpreter::InterruptBudget()); 3081 instance->set_constant_pool(constant_pool); 3082 instance->set_handler_table(empty_fixed_array()); 3083 instance->set_source_position_table(empty_byte_array()); 3084 CopyBytes(instance->GetFirstBytecodeAddress(), raw_bytecodes, length); 3085 3086 return result; 3087 } 3088 3089 void Heap::CreateFillerObjectAt(Address addr, int size, 3090 ClearRecordedSlots mode) { 3091 if (size == 0) return; 3092 HeapObject* filler = HeapObject::FromAddress(addr); 3093 if (size == kPointerSize) { 3094 filler->set_map_no_write_barrier( 3095 reinterpret_cast<Map*>(root(kOnePointerFillerMapRootIndex))); 3096 } else if (size == 2 * kPointerSize) { 3097 filler->set_map_no_write_barrier( 3098 reinterpret_cast<Map*>(root(kTwoPointerFillerMapRootIndex))); 3099 } else { 3100 DCHECK_GT(size, 2 * kPointerSize); 3101 filler->set_map_no_write_barrier( 3102 reinterpret_cast<Map*>(root(kFreeSpaceMapRootIndex))); 3103 FreeSpace::cast(filler)->nobarrier_set_size(size); 3104 } 3105 if (mode == ClearRecordedSlots::kYes) { 3106 ClearRecordedSlotRange(addr, addr + size); 3107 } 3108 // At this point, we may be deserializing the heap from a snapshot, and 3109 // none of the maps have been created yet and are NULL. 3110 DCHECK((filler->map() == NULL && !deserialization_complete_) || 3111 filler->map()->IsMap()); 3112 } 3113 3114 3115 bool Heap::CanMoveObjectStart(HeapObject* object) { 3116 if (!FLAG_move_object_start) return false; 3117 3118 // Sampling heap profiler may have a reference to the object. 3119 if (isolate()->heap_profiler()->is_sampling_allocations()) return false; 3120 3121 Address address = object->address(); 3122 3123 if (lo_space()->Contains(object)) return false; 3124 3125 // We can move the object start if the page was already swept. 3126 return Page::FromAddress(address)->SweepingDone(); 3127 } 3128 3129 3130 void Heap::AdjustLiveBytes(HeapObject* object, int by, InvocationMode mode) { 3131 // As long as the inspected object is black and we are currently not iterating 3132 // the heap using HeapIterator, we can update the live byte count. We cannot 3133 // update while using HeapIterator because the iterator is temporarily 3134 // marking the whole object graph, without updating live bytes. 3135 if (lo_space()->Contains(object)) { 3136 lo_space()->AdjustLiveBytes(by); 3137 } else if (!in_heap_iterator() && 3138 !mark_compact_collector()->sweeping_in_progress() && 3139 Marking::IsBlack(Marking::MarkBitFrom(object->address()))) { 3140 if (mode == SEQUENTIAL_TO_SWEEPER) { 3141 MemoryChunk::IncrementLiveBytesFromGC(object, by); 3142 } else { 3143 MemoryChunk::IncrementLiveBytesFromMutator(object, by); 3144 } 3145 } 3146 } 3147 3148 3149 FixedArrayBase* Heap::LeftTrimFixedArray(FixedArrayBase* object, 3150 int elements_to_trim) { 3151 CHECK_NOT_NULL(object); 3152 DCHECK(!object->IsFixedTypedArrayBase()); 3153 DCHECK(!object->IsByteArray()); 3154 const int element_size = object->IsFixedArray() ? kPointerSize : kDoubleSize; 3155 const int bytes_to_trim = elements_to_trim * element_size; 3156 Map* map = object->map(); 3157 3158 // For now this trick is only applied to objects in new and paged space. 3159 // In large object space the object's start must coincide with chunk 3160 // and thus the trick is just not applicable. 3161 DCHECK(!lo_space()->Contains(object)); 3162 DCHECK(object->map() != fixed_cow_array_map()); 3163 3164 STATIC_ASSERT(FixedArrayBase::kMapOffset == 0); 3165 STATIC_ASSERT(FixedArrayBase::kLengthOffset == kPointerSize); 3166 STATIC_ASSERT(FixedArrayBase::kHeaderSize == 2 * kPointerSize); 3167 3168 const int len = object->length(); 3169 DCHECK(elements_to_trim <= len); 3170 3171 // Calculate location of new array start. 3172 Address new_start = object->address() + bytes_to_trim; 3173 3174 // Technically in new space this write might be omitted (except for 3175 // debug mode which iterates through the heap), but to play safer 3176 // we still do it. 3177 CreateFillerObjectAt(object->address(), bytes_to_trim, 3178 ClearRecordedSlots::kYes); 3179 // Initialize header of the trimmed array. Since left trimming is only 3180 // performed on pages which are not concurrently swept creating a filler 3181 // object does not require synchronization. 3182 DCHECK(CanMoveObjectStart(object)); 3183 Object** former_start = HeapObject::RawField(object, 0); 3184 int new_start_index = elements_to_trim * (element_size / kPointerSize); 3185 former_start[new_start_index] = map; 3186 former_start[new_start_index + 1] = Smi::FromInt(len - elements_to_trim); 3187 FixedArrayBase* new_object = 3188 FixedArrayBase::cast(HeapObject::FromAddress(new_start)); 3189 3190 // Remove recorded slots for the new map and length offset. 3191 ClearRecordedSlot(new_object, HeapObject::RawField(new_object, 0)); 3192 ClearRecordedSlot(new_object, HeapObject::RawField( 3193 new_object, FixedArrayBase::kLengthOffset)); 3194 3195 // Maintain consistency of live bytes during incremental marking 3196 Marking::TransferMark(this, object->address(), new_start); 3197 AdjustLiveBytes(new_object, -bytes_to_trim, Heap::CONCURRENT_TO_SWEEPER); 3198 3199 // Notify the heap profiler of change in object layout. 3200 OnMoveEvent(new_object, object, new_object->Size()); 3201 return new_object; 3202 } 3203 3204 3205 // Force instantiation of templatized method. 3206 template void Heap::RightTrimFixedArray<Heap::SEQUENTIAL_TO_SWEEPER>( 3207 FixedArrayBase*, int); 3208 template void Heap::RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>( 3209 FixedArrayBase*, int); 3210 3211 3212 template<Heap::InvocationMode mode> 3213 void Heap::RightTrimFixedArray(FixedArrayBase* object, int elements_to_trim) { 3214 const int len = object->length(); 3215 DCHECK_LE(elements_to_trim, len); 3216 DCHECK_GE(elements_to_trim, 0); 3217 3218 int bytes_to_trim; 3219 if (object->IsFixedTypedArrayBase()) { 3220 InstanceType type = object->map()->instance_type(); 3221 bytes_to_trim = 3222 FixedTypedArrayBase::TypedArraySize(type, len) - 3223 FixedTypedArrayBase::TypedArraySize(type, len - elements_to_trim); 3224 } else if (object->IsByteArray()) { 3225 int new_size = ByteArray::SizeFor(len - elements_to_trim); 3226 bytes_to_trim = ByteArray::SizeFor(len) - new_size; 3227 DCHECK_GE(bytes_to_trim, 0); 3228 } else { 3229 const int element_size = 3230 object->IsFixedArray() ? kPointerSize : kDoubleSize; 3231 bytes_to_trim = elements_to_trim * element_size; 3232 } 3233 3234 3235 // For now this trick is only applied to objects in new and paged space. 3236 DCHECK(object->map() != fixed_cow_array_map()); 3237 3238 if (bytes_to_trim == 0) { 3239 // No need to create filler and update live bytes counters, just initialize 3240 // header of the trimmed array. 3241 object->synchronized_set_length(len - elements_to_trim); 3242 return; 3243 } 3244 3245 // Calculate location of new array end. 3246 Address old_end = object->address() + object->Size(); 3247 Address new_end = old_end - bytes_to_trim; 3248 3249 // Technically in new space this write might be omitted (except for 3250 // debug mode which iterates through the heap), but to play safer 3251 // we still do it. 3252 // We do not create a filler for objects in large object space. 3253 // TODO(hpayer): We should shrink the large object page if the size 3254 // of the object changed significantly. 3255 if (!lo_space()->Contains(object)) { 3256 CreateFillerObjectAt(new_end, bytes_to_trim, ClearRecordedSlots::kYes); 3257 } 3258 3259 // Initialize header of the trimmed array. We are storing the new length 3260 // using release store after creating a filler for the left-over space to 3261 // avoid races with the sweeper thread. 3262 object->synchronized_set_length(len - elements_to_trim); 3263 3264 // Maintain consistency of live bytes during incremental marking 3265 AdjustLiveBytes(object, -bytes_to_trim, mode); 3266 3267 // Notify the heap profiler of change in object layout. The array may not be 3268 // moved during GC, and size has to be adjusted nevertheless. 3269 HeapProfiler* profiler = isolate()->heap_profiler(); 3270 if (profiler->is_tracking_allocations()) { 3271 profiler->UpdateObjectSizeEvent(object->address(), object->Size()); 3272 } 3273 } 3274 3275 3276 AllocationResult Heap::AllocateFixedTypedArrayWithExternalPointer( 3277 int length, ExternalArrayType array_type, void* external_pointer, 3278 PretenureFlag pretenure) { 3279 int size = FixedTypedArrayBase::kHeaderSize; 3280 AllocationSpace space = SelectSpace(pretenure); 3281 HeapObject* result = nullptr; 3282 { 3283 AllocationResult allocation = AllocateRaw(size, space); 3284 if (!allocation.To(&result)) return allocation; 3285 } 3286 3287 result->set_map_no_write_barrier(MapForFixedTypedArray(array_type)); 3288 FixedTypedArrayBase* elements = FixedTypedArrayBase::cast(result); 3289 elements->set_base_pointer(Smi::FromInt(0), SKIP_WRITE_BARRIER); 3290 elements->set_external_pointer(external_pointer, SKIP_WRITE_BARRIER); 3291 elements->set_length(length); 3292 return elements; 3293 } 3294 3295 static void ForFixedTypedArray(ExternalArrayType array_type, int* element_size, 3296 ElementsKind* element_kind) { 3297 switch (array_type) { 3298 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ 3299 case kExternal##Type##Array: \ 3300 *element_size = size; \ 3301 *element_kind = TYPE##_ELEMENTS; \ 3302 return; 3303 3304 TYPED_ARRAYS(TYPED_ARRAY_CASE) 3305 #undef TYPED_ARRAY_CASE 3306 3307 default: 3308 *element_size = 0; // Bogus 3309 *element_kind = UINT8_ELEMENTS; // Bogus 3310 UNREACHABLE(); 3311 } 3312 } 3313 3314 3315 AllocationResult Heap::AllocateFixedTypedArray(int length, 3316 ExternalArrayType array_type, 3317 bool initialize, 3318 PretenureFlag pretenure) { 3319 int element_size; 3320 ElementsKind elements_kind; 3321 ForFixedTypedArray(array_type, &element_size, &elements_kind); 3322 int size = OBJECT_POINTER_ALIGN(length * element_size + 3323 FixedTypedArrayBase::kDataOffset); 3324 AllocationSpace space = SelectSpace(pretenure); 3325 3326 HeapObject* object = nullptr; 3327 AllocationResult allocation = AllocateRaw( 3328 size, space, 3329 array_type == kExternalFloat64Array ? kDoubleAligned : kWordAligned); 3330 if (!allocation.To(&object)) return allocation; 3331 3332 object->set_map_no_write_barrier(MapForFixedTypedArray(array_type)); 3333 FixedTypedArrayBase* elements = FixedTypedArrayBase::cast(object); 3334 elements->set_base_pointer(elements, SKIP_WRITE_BARRIER); 3335 elements->set_external_pointer( 3336 ExternalReference::fixed_typed_array_base_data_offset().address(), 3337 SKIP_WRITE_BARRIER); 3338 elements->set_length(length); 3339 if (initialize) memset(elements->DataPtr(), 0, elements->DataSize()); 3340 return elements; 3341 } 3342 3343 3344 AllocationResult Heap::AllocateCode(int object_size, bool immovable) { 3345 DCHECK(IsAligned(static_cast<intptr_t>(object_size), kCodeAlignment)); 3346 AllocationResult allocation = AllocateRaw(object_size, CODE_SPACE); 3347 3348 HeapObject* result = nullptr; 3349 if (!allocation.To(&result)) return allocation; 3350 if (immovable) { 3351 Address address = result->address(); 3352 // Code objects which should stay at a fixed address are allocated either 3353 // in the first page of code space (objects on the first page of each space 3354 // are never moved) or in large object space. 3355 if (!code_space_->FirstPage()->Contains(address) && 3356 MemoryChunk::FromAddress(address)->owner()->identity() != LO_SPACE) { 3357 // Discard the first code allocation, which was on a page where it could 3358 // be moved. 3359 CreateFillerObjectAt(result->address(), object_size, 3360 ClearRecordedSlots::kNo); 3361 allocation = lo_space_->AllocateRaw(object_size, EXECUTABLE); 3362 if (!allocation.To(&result)) return allocation; 3363 OnAllocationEvent(result, object_size); 3364 } 3365 } 3366 3367 result->set_map_no_write_barrier(code_map()); 3368 Code* code = Code::cast(result); 3369 DCHECK(IsAligned(bit_cast<intptr_t>(code->address()), kCodeAlignment)); 3370 DCHECK(!memory_allocator()->code_range()->valid() || 3371 memory_allocator()->code_range()->contains(code->address()) || 3372 object_size <= code_space()->AreaSize()); 3373 code->set_gc_metadata(Smi::FromInt(0)); 3374 code->set_ic_age(global_ic_age_); 3375 return code; 3376 } 3377 3378 3379 AllocationResult Heap::CopyCode(Code* code) { 3380 AllocationResult allocation; 3381 3382 HeapObject* result = nullptr; 3383 // Allocate an object the same size as the code object. 3384 int obj_size = code->Size(); 3385 allocation = AllocateRaw(obj_size, CODE_SPACE); 3386 if (!allocation.To(&result)) return allocation; 3387 3388 // Copy code object. 3389 Address old_addr = code->address(); 3390 Address new_addr = result->address(); 3391 CopyBlock(new_addr, old_addr, obj_size); 3392 Code* new_code = Code::cast(result); 3393 3394 // Relocate the copy. 3395 DCHECK(IsAligned(bit_cast<intptr_t>(new_code->address()), kCodeAlignment)); 3396 DCHECK(!memory_allocator()->code_range()->valid() || 3397 memory_allocator()->code_range()->contains(code->address()) || 3398 obj_size <= code_space()->AreaSize()); 3399 new_code->Relocate(new_addr - old_addr); 3400 // We have to iterate over the object and process its pointers when black 3401 // allocation is on. 3402 incremental_marking()->IterateBlackObject(new_code); 3403 return new_code; 3404 } 3405 3406 AllocationResult Heap::CopyBytecodeArray(BytecodeArray* bytecode_array) { 3407 int size = BytecodeArray::SizeFor(bytecode_array->length()); 3408 HeapObject* result = nullptr; 3409 { 3410 AllocationResult allocation = AllocateRaw(size, OLD_SPACE); 3411 if (!allocation.To(&result)) return allocation; 3412 } 3413 3414 result->set_map_no_write_barrier(bytecode_array_map()); 3415 BytecodeArray* copy = BytecodeArray::cast(result); 3416 copy->set_length(bytecode_array->length()); 3417 copy->set_frame_size(bytecode_array->frame_size()); 3418 copy->set_parameter_count(bytecode_array->parameter_count()); 3419 copy->set_constant_pool(bytecode_array->constant_pool()); 3420 copy->set_handler_table(bytecode_array->handler_table()); 3421 copy->set_source_position_table(bytecode_array->source_position_table()); 3422 copy->set_interrupt_budget(bytecode_array->interrupt_budget()); 3423 bytecode_array->CopyBytecodesTo(copy); 3424 return copy; 3425 } 3426 3427 void Heap::InitializeAllocationMemento(AllocationMemento* memento, 3428 AllocationSite* allocation_site) { 3429 memento->set_map_no_write_barrier(allocation_memento_map()); 3430 DCHECK(allocation_site->map() == allocation_site_map()); 3431 memento->set_allocation_site(allocation_site, SKIP_WRITE_BARRIER); 3432 if (FLAG_allocation_site_pretenuring) { 3433 allocation_site->IncrementMementoCreateCount(); 3434 } 3435 } 3436 3437 3438 AllocationResult Heap::Allocate(Map* map, AllocationSpace space, 3439 AllocationSite* allocation_site) { 3440 DCHECK(gc_state_ == NOT_IN_GC); 3441 DCHECK(map->instance_type() != MAP_TYPE); 3442 int size = map->instance_size(); 3443 if (allocation_site != NULL) { 3444 size += AllocationMemento::kSize; 3445 } 3446 HeapObject* result = nullptr; 3447 AllocationResult allocation = AllocateRaw(size, space); 3448 if (!allocation.To(&result)) return allocation; 3449 // No need for write barrier since object is white and map is in old space. 3450 result->set_map_no_write_barrier(map); 3451 if (allocation_site != NULL) { 3452 AllocationMemento* alloc_memento = reinterpret_cast<AllocationMemento*>( 3453 reinterpret_cast<Address>(result) + map->instance_size()); 3454 InitializeAllocationMemento(alloc_memento, allocation_site); 3455 } 3456 return result; 3457 } 3458 3459 3460 void Heap::InitializeJSObjectFromMap(JSObject* obj, FixedArray* properties, 3461 Map* map) { 3462 obj->set_properties(properties); 3463 obj->initialize_elements(); 3464 // TODO(1240798): Initialize the object's body using valid initial values 3465 // according to the object's initial map. For example, if the map's 3466 // instance type is JS_ARRAY_TYPE, the length field should be initialized 3467 // to a number (e.g. Smi::FromInt(0)) and the elements initialized to a 3468 // fixed array (e.g. Heap::empty_fixed_array()). Currently, the object 3469 // verification code has to cope with (temporarily) invalid objects. See 3470 // for example, JSArray::JSArrayVerify). 3471 InitializeJSObjectBody(obj, map, JSObject::kHeaderSize); 3472 } 3473 3474 3475 void Heap::InitializeJSObjectBody(JSObject* obj, Map* map, int start_offset) { 3476 if (start_offset == map->instance_size()) return; 3477 DCHECK_LT(start_offset, map->instance_size()); 3478 3479 // We cannot always fill with one_pointer_filler_map because objects 3480 // created from API functions expect their internal fields to be initialized 3481 // with undefined_value. 3482 // Pre-allocated fields need to be initialized with undefined_value as well 3483 // so that object accesses before the constructor completes (e.g. in the 3484 // debugger) will not cause a crash. 3485 3486 // In case of Array subclassing the |map| could already be transitioned 3487 // to different elements kind from the initial map on which we track slack. 3488 bool in_progress = map->IsInobjectSlackTrackingInProgress(); 3489 Object* filler; 3490 if (in_progress) { 3491 filler = one_pointer_filler_map(); 3492 } else { 3493 filler = undefined_value(); 3494 } 3495 obj->InitializeBody(map, start_offset, Heap::undefined_value(), filler); 3496 if (in_progress) { 3497 map->FindRootMap()->InobjectSlackTrackingStep(); 3498 } 3499 } 3500 3501 3502 AllocationResult Heap::AllocateJSObjectFromMap( 3503 Map* map, PretenureFlag pretenure, AllocationSite* allocation_site) { 3504 // JSFunctions should be allocated using AllocateFunction to be 3505 // properly initialized. 3506 DCHECK(map->instance_type() != JS_FUNCTION_TYPE); 3507 3508 // Both types of global objects should be allocated using 3509 // AllocateGlobalObject to be properly initialized. 3510 DCHECK(map->instance_type() != JS_GLOBAL_OBJECT_TYPE); 3511 3512 // Allocate the backing storage for the properties. 3513 FixedArray* properties = empty_fixed_array(); 3514 3515 // Allocate the JSObject. 3516 AllocationSpace space = SelectSpace(pretenure); 3517 JSObject* js_obj = nullptr; 3518 AllocationResult allocation = Allocate(map, space, allocation_site); 3519 if (!allocation.To(&js_obj)) return allocation; 3520 3521 // Initialize the JSObject. 3522 InitializeJSObjectFromMap(js_obj, properties, map); 3523 DCHECK(js_obj->HasFastElements() || js_obj->HasFixedTypedArrayElements() || 3524 js_obj->HasFastStringWrapperElements() || 3525 js_obj->HasFastArgumentsElements()); 3526 return js_obj; 3527 } 3528 3529 3530 AllocationResult Heap::AllocateJSObject(JSFunction* constructor, 3531 PretenureFlag pretenure, 3532 AllocationSite* allocation_site) { 3533 DCHECK(constructor->has_initial_map()); 3534 3535 // Allocate the object based on the constructors initial map. 3536 AllocationResult allocation = AllocateJSObjectFromMap( 3537 constructor->initial_map(), pretenure, allocation_site); 3538 #ifdef DEBUG 3539 // Make sure result is NOT a global object if valid. 3540 HeapObject* obj = nullptr; 3541 DCHECK(!allocation.To(&obj) || !obj->IsJSGlobalObject()); 3542 #endif 3543 return allocation; 3544 } 3545 3546 3547 AllocationResult Heap::CopyJSObject(JSObject* source, AllocationSite* site) { 3548 // Make the clone. 3549 Map* map = source->map(); 3550 3551 // We can only clone regexps, normal objects, api objects, errors or arrays. 3552 // Copying anything else will break invariants. 3553 CHECK(map->instance_type() == JS_REGEXP_TYPE || 3554 map->instance_type() == JS_OBJECT_TYPE || 3555 map->instance_type() == JS_ERROR_TYPE || 3556 map->instance_type() == JS_ARRAY_TYPE || 3557 map->instance_type() == JS_API_OBJECT_TYPE || 3558 map->instance_type() == JS_SPECIAL_API_OBJECT_TYPE); 3559 3560 int object_size = map->instance_size(); 3561 HeapObject* clone = nullptr; 3562 3563 DCHECK(site == NULL || AllocationSite::CanTrack(map->instance_type())); 3564 3565 int adjusted_object_size = 3566 site != NULL ? object_size + AllocationMemento::kSize : object_size; 3567 AllocationResult allocation = AllocateRaw(adjusted_object_size, NEW_SPACE); 3568 if (!allocation.To(&clone)) return allocation; 3569 3570 SLOW_DCHECK(InNewSpace(clone)); 3571 // Since we know the clone is allocated in new space, we can copy 3572 // the contents without worrying about updating the write barrier. 3573 CopyBlock(clone->address(), source->address(), object_size); 3574 3575 if (site != NULL) { 3576 AllocationMemento* alloc_memento = reinterpret_cast<AllocationMemento*>( 3577 reinterpret_cast<Address>(clone) + object_size); 3578 InitializeAllocationMemento(alloc_memento, site); 3579 } 3580 3581 SLOW_DCHECK(JSObject::cast(clone)->GetElementsKind() == 3582 source->GetElementsKind()); 3583 FixedArrayBase* elements = FixedArrayBase::cast(source->elements()); 3584 FixedArray* properties = FixedArray::cast(source->properties()); 3585 // Update elements if necessary. 3586 if (elements->length() > 0) { 3587 FixedArrayBase* elem = nullptr; 3588 { 3589 AllocationResult allocation; 3590 if (elements->map() == fixed_cow_array_map()) { 3591 allocation = FixedArray::cast(elements); 3592 } else if (source->HasFastDoubleElements()) { 3593 allocation = CopyFixedDoubleArray(FixedDoubleArray::cast(elements)); 3594 } else { 3595 allocation = CopyFixedArray(FixedArray::cast(elements)); 3596 } 3597 if (!allocation.To(&elem)) return allocation; 3598 } 3599 JSObject::cast(clone)->set_elements(elem, SKIP_WRITE_BARRIER); 3600 } 3601 // Update properties if necessary. 3602 if (properties->length() > 0) { 3603 FixedArray* prop = nullptr; 3604 { 3605 AllocationResult allocation = CopyFixedArray(properties); 3606 if (!allocation.To(&prop)) return allocation; 3607 } 3608 JSObject::cast(clone)->set_properties(prop, SKIP_WRITE_BARRIER); 3609 } 3610 // Return the new clone. 3611 return clone; 3612 } 3613 3614 3615 static inline void WriteOneByteData(Vector<const char> vector, uint8_t* chars, 3616 int len) { 3617 // Only works for one byte strings. 3618 DCHECK(vector.length() == len); 3619 MemCopy(chars, vector.start(), len); 3620 } 3621 3622 static inline void WriteTwoByteData(Vector<const char> vector, uint16_t* chars, 3623 int len) { 3624 const uint8_t* stream = reinterpret_cast<const uint8_t*>(vector.start()); 3625 size_t stream_length = vector.length(); 3626 while (stream_length != 0) { 3627 size_t consumed = 0; 3628 uint32_t c = unibrow::Utf8::ValueOf(stream, stream_length, &consumed); 3629 DCHECK(c != unibrow::Utf8::kBadChar); 3630 DCHECK(consumed <= stream_length); 3631 stream_length -= consumed; 3632 stream += consumed; 3633 if (c > unibrow::Utf16::kMaxNonSurrogateCharCode) { 3634 len -= 2; 3635 if (len < 0) break; 3636 *chars++ = unibrow::Utf16::LeadSurrogate(c); 3637 *chars++ = unibrow::Utf16::TrailSurrogate(c); 3638 } else { 3639 len -= 1; 3640 if (len < 0) break; 3641 *chars++ = c; 3642 } 3643 } 3644 DCHECK(stream_length == 0); 3645 DCHECK(len == 0); 3646 } 3647 3648 3649 static inline void WriteOneByteData(String* s, uint8_t* chars, int len) { 3650 DCHECK(s->length() == len); 3651 String::WriteToFlat(s, chars, 0, len); 3652 } 3653 3654 3655 static inline void WriteTwoByteData(String* s, uint16_t* chars, int len) { 3656 DCHECK(s->length() == len); 3657 String::WriteToFlat(s, chars, 0, len); 3658 } 3659 3660 3661 template <bool is_one_byte, typename T> 3662 AllocationResult Heap::AllocateInternalizedStringImpl(T t, int chars, 3663 uint32_t hash_field) { 3664 DCHECK(chars >= 0); 3665 // Compute map and object size. 3666 int size; 3667 Map* map; 3668 3669 DCHECK_LE(0, chars); 3670 DCHECK_GE(String::kMaxLength, chars); 3671 if (is_one_byte) { 3672 map = one_byte_internalized_string_map(); 3673 size = SeqOneByteString::SizeFor(chars); 3674 } else { 3675 map = internalized_string_map(); 3676 size = SeqTwoByteString::SizeFor(chars); 3677 } 3678 3679 // Allocate string. 3680 HeapObject* result = nullptr; 3681 { 3682 AllocationResult allocation = AllocateRaw(size, OLD_SPACE); 3683 if (!allocation.To(&result)) return allocation; 3684 } 3685 3686 result->set_map_no_write_barrier(map); 3687 // Set length and hash fields of the allocated string. 3688 String* answer = String::cast(result); 3689 answer->set_length(chars); 3690 answer->set_hash_field(hash_field); 3691 3692 DCHECK_EQ(size, answer->Size()); 3693 3694 if (is_one_byte) { 3695 WriteOneByteData(t, SeqOneByteString::cast(answer)->GetChars(), chars); 3696 } else { 3697 WriteTwoByteData(t, SeqTwoByteString::cast(answer)->GetChars(), chars); 3698 } 3699 return answer; 3700 } 3701 3702 3703 // Need explicit instantiations. 3704 template AllocationResult Heap::AllocateInternalizedStringImpl<true>(String*, 3705 int, 3706 uint32_t); 3707 template AllocationResult Heap::AllocateInternalizedStringImpl<false>(String*, 3708 int, 3709 uint32_t); 3710 template AllocationResult Heap::AllocateInternalizedStringImpl<false>( 3711 Vector<const char>, int, uint32_t); 3712 3713 3714 AllocationResult Heap::AllocateRawOneByteString(int length, 3715 PretenureFlag pretenure) { 3716 DCHECK_LE(0, length); 3717 DCHECK_GE(String::kMaxLength, length); 3718 int size = SeqOneByteString::SizeFor(length); 3719 DCHECK(size <= SeqOneByteString::kMaxSize); 3720 AllocationSpace space = SelectSpace(pretenure); 3721 3722 HeapObject* result = nullptr; 3723 { 3724 AllocationResult allocation = AllocateRaw(size, space); 3725 if (!allocation.To(&result)) return allocation; 3726 } 3727 3728 // Partially initialize the object. 3729 result->set_map_no_write_barrier(one_byte_string_map()); 3730 String::cast(result)->set_length(length); 3731 String::cast(result)->set_hash_field(String::kEmptyHashField); 3732 DCHECK_EQ(size, HeapObject::cast(result)->Size()); 3733 3734 return result; 3735 } 3736 3737 3738 AllocationResult Heap::AllocateRawTwoByteString(int length, 3739 PretenureFlag pretenure) { 3740 DCHECK_LE(0, length); 3741 DCHECK_GE(String::kMaxLength, length); 3742 int size = SeqTwoByteString::SizeFor(length); 3743 DCHECK(size <= SeqTwoByteString::kMaxSize); 3744 AllocationSpace space = SelectSpace(pretenure); 3745 3746 HeapObject* result = nullptr; 3747 { 3748 AllocationResult allocation = AllocateRaw(size, space); 3749 if (!allocation.To(&result)) return allocation; 3750 } 3751 3752 // Partially initialize the object. 3753 result->set_map_no_write_barrier(string_map()); 3754 String::cast(result)->set_length(length); 3755 String::cast(result)->set_hash_field(String::kEmptyHashField); 3756 DCHECK_EQ(size, HeapObject::cast(result)->Size()); 3757 return result; 3758 } 3759 3760 3761 AllocationResult Heap::AllocateEmptyFixedArray() { 3762 int size = FixedArray::SizeFor(0); 3763 HeapObject* result = nullptr; 3764 { 3765 AllocationResult allocation = AllocateRaw(size, OLD_SPACE); 3766 if (!allocation.To(&result)) return allocation; 3767 } 3768 // Initialize the object. 3769 result->set_map_no_write_barrier(fixed_array_map()); 3770 FixedArray::cast(result)->set_length(0); 3771 return result; 3772 } 3773 3774 3775 AllocationResult Heap::CopyAndTenureFixedCOWArray(FixedArray* src) { 3776 if (!InNewSpace(src)) { 3777 return src; 3778 } 3779 3780 int len = src->length(); 3781 HeapObject* obj = nullptr; 3782 { 3783 AllocationResult allocation = AllocateRawFixedArray(len, TENURED); 3784 if (!allocation.To(&obj)) return allocation; 3785 } 3786 obj->set_map_no_write_barrier(fixed_array_map()); 3787 FixedArray* result = FixedArray::cast(obj); 3788 result->set_length(len); 3789 3790 // Copy the content. 3791 DisallowHeapAllocation no_gc; 3792 WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); 3793 for (int i = 0; i < len; i++) result->set(i, src->get(i), mode); 3794 3795 // TODO(mvstanton): The map is set twice because of protection against calling 3796 // set() on a COW FixedArray. Issue v8:3221 created to track this, and 3797 // we might then be able to remove this whole method. 3798 HeapObject::cast(obj)->set_map_no_write_barrier(fixed_cow_array_map()); 3799 return result; 3800 } 3801 3802 3803 AllocationResult Heap::AllocateEmptyFixedTypedArray( 3804 ExternalArrayType array_type) { 3805 return AllocateFixedTypedArray(0, array_type, false, TENURED); 3806 } 3807 3808 3809 AllocationResult Heap::CopyFixedArrayAndGrow(FixedArray* src, int grow_by, 3810 PretenureFlag pretenure) { 3811 int old_len = src->length(); 3812 int new_len = old_len + grow_by; 3813 DCHECK(new_len >= old_len); 3814 HeapObject* obj = nullptr; 3815 { 3816 AllocationResult allocation = AllocateRawFixedArray(new_len, pretenure); 3817 if (!allocation.To(&obj)) return allocation; 3818 } 3819 3820 obj->set_map_no_write_barrier(fixed_array_map()); 3821 FixedArray* result = FixedArray::cast(obj); 3822 result->set_length(new_len); 3823 3824 // Copy the content. 3825 DisallowHeapAllocation no_gc; 3826 WriteBarrierMode mode = obj->GetWriteBarrierMode(no_gc); 3827 for (int i = 0; i < old_len; i++) result->set(i, src->get(i), mode); 3828 MemsetPointer(result->data_start() + old_len, undefined_value(), grow_by); 3829 return result; 3830 } 3831 3832 AllocationResult Heap::CopyFixedArrayUpTo(FixedArray* src, int new_len, 3833 PretenureFlag pretenure) { 3834 if (new_len == 0) return empty_fixed_array(); 3835 3836 DCHECK_LE(new_len, src->length()); 3837 3838 HeapObject* obj = nullptr; 3839 { 3840 AllocationResult allocation = AllocateRawFixedArray(new_len, pretenure); 3841 if (!allocation.To(&obj)) return allocation; 3842 } 3843 obj->set_map_no_write_barrier(fixed_array_map()); 3844 3845 FixedArray* result = FixedArray::cast(obj); 3846 result->set_length(new_len); 3847 3848 // Copy the content. 3849 DisallowHeapAllocation no_gc; 3850 WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); 3851 for (int i = 0; i < new_len; i++) result->set(i, src->get(i), mode); 3852 return result; 3853 } 3854 3855 AllocationResult Heap::CopyFixedArrayWithMap(FixedArray* src, Map* map) { 3856 int len = src->length(); 3857 HeapObject* obj = nullptr; 3858 { 3859 AllocationResult allocation = AllocateRawFixedArray(len, NOT_TENURED); 3860 if (!allocation.To(&obj)) return allocation; 3861 } 3862 obj->set_map_no_write_barrier(map); 3863 if (InNewSpace(obj)) { 3864 CopyBlock(obj->address() + kPointerSize, src->address() + kPointerSize, 3865 FixedArray::SizeFor(len) - kPointerSize); 3866 return obj; 3867 } 3868 FixedArray* result = FixedArray::cast(obj); 3869 result->set_length(len); 3870 3871 // Copy the content. 3872 DisallowHeapAllocation no_gc; 3873 WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); 3874 for (int i = 0; i < len; i++) result->set(i, src->get(i), mode); 3875 return result; 3876 } 3877 3878 3879 AllocationResult Heap::CopyFixedDoubleArrayWithMap(FixedDoubleArray* src, 3880 Map* map) { 3881 int len = src->length(); 3882 HeapObject* obj = nullptr; 3883 { 3884 AllocationResult allocation = AllocateRawFixedDoubleArray(len, NOT_TENURED); 3885 if (!allocation.To(&obj)) return allocation; 3886 } 3887 obj->set_map_no_write_barrier(map); 3888 CopyBlock(obj->address() + FixedDoubleArray::kLengthOffset, 3889 src->address() + FixedDoubleArray::kLengthOffset, 3890 FixedDoubleArray::SizeFor(len) - FixedDoubleArray::kLengthOffset); 3891 return obj; 3892 } 3893 3894 3895 AllocationResult Heap::AllocateRawFixedArray(int length, 3896 PretenureFlag pretenure) { 3897 if (length < 0 || length > FixedArray::kMaxLength) { 3898 v8::internal::Heap::FatalProcessOutOfMemory("invalid array length", true); 3899 } 3900 int size = FixedArray::SizeFor(length); 3901 AllocationSpace space = SelectSpace(pretenure); 3902 3903 return AllocateRaw(size, space); 3904 } 3905 3906 3907 AllocationResult Heap::AllocateFixedArrayWithFiller(int length, 3908 PretenureFlag pretenure, 3909 Object* filler) { 3910 DCHECK(length >= 0); 3911 DCHECK(empty_fixed_array()->IsFixedArray()); 3912 if (length == 0) return empty_fixed_array(); 3913 3914 DCHECK(!InNewSpace(filler)); 3915 HeapObject* result = nullptr; 3916 { 3917 AllocationResult allocation = AllocateRawFixedArray(length, pretenure); 3918 if (!allocation.To(&result)) return allocation; 3919 } 3920 3921 result->set_map_no_write_barrier(fixed_array_map()); 3922 FixedArray* array = FixedArray::cast(result); 3923 array->set_length(length); 3924 MemsetPointer(array->data_start(), filler, length); 3925 return array; 3926 } 3927 3928 3929 AllocationResult Heap::AllocateFixedArray(int length, PretenureFlag pretenure) { 3930 return AllocateFixedArrayWithFiller(length, pretenure, undefined_value()); 3931 } 3932 3933 3934 AllocationResult Heap::AllocateUninitializedFixedArray(int length) { 3935 if (length == 0) return empty_fixed_array(); 3936 3937 HeapObject* obj = nullptr; 3938 { 3939 AllocationResult allocation = AllocateRawFixedArray(length, NOT_TENURED); 3940 if (!allocation.To(&obj)) return allocation; 3941 } 3942 3943 obj->set_map_no_write_barrier(fixed_array_map()); 3944 FixedArray::cast(obj)->set_length(length); 3945 return obj; 3946 } 3947 3948 3949 AllocationResult Heap::AllocateUninitializedFixedDoubleArray( 3950 int length, PretenureFlag pretenure) { 3951 if (length == 0) return empty_fixed_array(); 3952 3953 HeapObject* elements = nullptr; 3954 AllocationResult allocation = AllocateRawFixedDoubleArray(length, pretenure); 3955 if (!allocation.To(&elements)) return allocation; 3956 3957 elements->set_map_no_write_barrier(fixed_double_array_map()); 3958 FixedDoubleArray::cast(elements)->set_length(length); 3959 return elements; 3960 } 3961 3962 3963 AllocationResult Heap::AllocateRawFixedDoubleArray(int length, 3964 PretenureFlag pretenure) { 3965 if (length < 0 || length > FixedDoubleArray::kMaxLength) { 3966 v8::internal::Heap::FatalProcessOutOfMemory("invalid array length", true); 3967 } 3968 int size = FixedDoubleArray::SizeFor(length); 3969 AllocationSpace space = SelectSpace(pretenure); 3970 3971 HeapObject* object = nullptr; 3972 { 3973 AllocationResult allocation = AllocateRaw(size, space, kDoubleAligned); 3974 if (!allocation.To(&object)) return allocation; 3975 } 3976 3977 return object; 3978 } 3979 3980 3981 AllocationResult Heap::AllocateSymbol() { 3982 // Statically ensure that it is safe to allocate symbols in paged spaces. 3983 STATIC_ASSERT(Symbol::kSize <= Page::kMaxRegularHeapObjectSize); 3984 3985 HeapObject* result = nullptr; 3986 AllocationResult allocation = AllocateRaw(Symbol::kSize, OLD_SPACE); 3987 if (!allocation.To(&result)) return allocation; 3988 3989 result->set_map_no_write_barrier(symbol_map()); 3990 3991 // Generate a random hash value. 3992 int hash; 3993 int attempts = 0; 3994 do { 3995 hash = isolate()->random_number_generator()->NextInt() & Name::kHashBitMask; 3996 attempts++; 3997 } while (hash == 0 && attempts < 30); 3998 if (hash == 0) hash = 1; // never return 0 3999 4000 Symbol::cast(result) 4001 ->set_hash_field(Name::kIsNotArrayIndexMask | (hash << Name::kHashShift)); 4002 Symbol::cast(result)->set_name(undefined_value()); 4003 Symbol::cast(result)->set_flags(0); 4004 4005 DCHECK(!Symbol::cast(result)->is_private()); 4006 return result; 4007 } 4008 4009 4010 AllocationResult Heap::AllocateStruct(InstanceType type) { 4011 Map* map; 4012 switch (type) { 4013 #define MAKE_CASE(NAME, Name, name) \ 4014 case NAME##_TYPE: \ 4015 map = name##_map(); \ 4016 break; 4017 STRUCT_LIST(MAKE_CASE) 4018 #undef MAKE_CASE 4019 default: 4020 UNREACHABLE(); 4021 return exception(); 4022 } 4023 int size = map->instance_size(); 4024 Struct* result = nullptr; 4025 { 4026 AllocationResult allocation = Allocate(map, OLD_SPACE); 4027 if (!allocation.To(&result)) return allocation; 4028 } 4029 result->InitializeBody(size); 4030 return result; 4031 } 4032 4033 4034 bool Heap::IsHeapIterable() { 4035 // TODO(hpayer): This function is not correct. Allocation folding in old 4036 // space breaks the iterability. 4037 return new_space_top_after_last_gc_ == new_space()->top(); 4038 } 4039 4040 4041 void Heap::MakeHeapIterable() { 4042 DCHECK(AllowHeapAllocation::IsAllowed()); 4043 if (!IsHeapIterable()) { 4044 CollectAllGarbage(kMakeHeapIterableMask, "Heap::MakeHeapIterable"); 4045 } 4046 if (mark_compact_collector()->sweeping_in_progress()) { 4047 mark_compact_collector()->EnsureSweepingCompleted(); 4048 } 4049 DCHECK(IsHeapIterable()); 4050 } 4051 4052 4053 static double ComputeMutatorUtilization(double mutator_speed, double gc_speed) { 4054 const double kMinMutatorUtilization = 0.0; 4055 const double kConservativeGcSpeedInBytesPerMillisecond = 200000; 4056 if (mutator_speed == 0) return kMinMutatorUtilization; 4057 if (gc_speed == 0) gc_speed = kConservativeGcSpeedInBytesPerMillisecond; 4058 // Derivation: 4059 // mutator_utilization = mutator_time / (mutator_time + gc_time) 4060 // mutator_time = 1 / mutator_speed 4061 // gc_time = 1 / gc_speed 4062 // mutator_utilization = (1 / mutator_speed) / 4063 // (1 / mutator_speed + 1 / gc_speed) 4064 // mutator_utilization = gc_speed / (mutator_speed + gc_speed) 4065 return gc_speed / (mutator_speed + gc_speed); 4066 } 4067 4068 4069 double Heap::YoungGenerationMutatorUtilization() { 4070 double mutator_speed = static_cast<double>( 4071 tracer()->NewSpaceAllocationThroughputInBytesPerMillisecond()); 4072 double gc_speed = 4073 tracer()->ScavengeSpeedInBytesPerMillisecond(kForSurvivedObjects); 4074 double result = ComputeMutatorUtilization(mutator_speed, gc_speed); 4075 if (FLAG_trace_mutator_utilization) { 4076 PrintIsolate(isolate(), 4077 "Young generation mutator utilization = %.3f (" 4078 "mutator_speed=%.f, gc_speed=%.f)\n", 4079 result, mutator_speed, gc_speed); 4080 } 4081 return result; 4082 } 4083 4084 4085 double Heap::OldGenerationMutatorUtilization() { 4086 double mutator_speed = static_cast<double>( 4087 tracer()->OldGenerationAllocationThroughputInBytesPerMillisecond()); 4088 double gc_speed = static_cast<double>( 4089 tracer()->CombinedMarkCompactSpeedInBytesPerMillisecond()); 4090 double result = ComputeMutatorUtilization(mutator_speed, gc_speed); 4091 if (FLAG_trace_mutator_utilization) { 4092 PrintIsolate(isolate(), 4093 "Old generation mutator utilization = %.3f (" 4094 "mutator_speed=%.f, gc_speed=%.f)\n", 4095 result, mutator_speed, gc_speed); 4096 } 4097 return result; 4098 } 4099 4100 4101 bool Heap::HasLowYoungGenerationAllocationRate() { 4102 const double high_mutator_utilization = 0.993; 4103 return YoungGenerationMutatorUtilization() > high_mutator_utilization; 4104 } 4105 4106 4107 bool Heap::HasLowOldGenerationAllocationRate() { 4108 const double high_mutator_utilization = 0.993; 4109 return OldGenerationMutatorUtilization() > high_mutator_utilization; 4110 } 4111 4112 4113 bool Heap::HasLowAllocationRate() { 4114 return HasLowYoungGenerationAllocationRate() && 4115 HasLowOldGenerationAllocationRate(); 4116 } 4117 4118 4119 bool Heap::HasHighFragmentation() { 4120 intptr_t used = PromotedSpaceSizeOfObjects(); 4121 intptr_t committed = CommittedOldGenerationMemory(); 4122 return HasHighFragmentation(used, committed); 4123 } 4124 4125 4126 bool Heap::HasHighFragmentation(intptr_t used, intptr_t committed) { 4127 const intptr_t kSlack = 16 * MB; 4128 // Fragmentation is high if committed > 2 * used + kSlack. 4129 // Rewrite the exression to avoid overflow. 4130 return committed - used > used + kSlack; 4131 } 4132 4133 void Heap::SetOptimizeForMemoryUsage() { 4134 // Activate memory reducer when switching to background if 4135 // - there was no mark compact since the start. 4136 // - the committed memory can be potentially reduced. 4137 // 2 pages for the old, code, and map space + 1 page for new space. 4138 const int kMinCommittedMemory = 7 * Page::kPageSize; 4139 if (ms_count_ == 0 && CommittedMemory() > kMinCommittedMemory) { 4140 MemoryReducer::Event event; 4141 event.type = MemoryReducer::kPossibleGarbage; 4142 event.time_ms = MonotonicallyIncreasingTimeInMs(); 4143 memory_reducer_->NotifyPossibleGarbage(event); 4144 } 4145 optimize_for_memory_usage_ = true; 4146 } 4147 4148 void Heap::ReduceNewSpaceSize() { 4149 // TODO(ulan): Unify this constant with the similar constant in 4150 // GCIdleTimeHandler once the change is merged to 4.5. 4151 static const size_t kLowAllocationThroughput = 1000; 4152 const double allocation_throughput = 4153 tracer()->CurrentAllocationThroughputInBytesPerMillisecond(); 4154 4155 if (FLAG_predictable) return; 4156 4157 if (ShouldReduceMemory() || 4158 ((allocation_throughput != 0) && 4159 (allocation_throughput < kLowAllocationThroughput))) { 4160 new_space_.Shrink(); 4161 UncommitFromSpace(); 4162 } 4163 } 4164 4165 4166 void Heap::FinalizeIncrementalMarkingIfComplete(const char* comment) { 4167 if (incremental_marking()->IsMarking() && 4168 (incremental_marking()->IsReadyToOverApproximateWeakClosure() || 4169 (!incremental_marking()->finalize_marking_completed() && 4170 mark_compact_collector()->marking_deque()->IsEmpty()))) { 4171 FinalizeIncrementalMarking(comment); 4172 } else if (incremental_marking()->IsComplete() || 4173 (mark_compact_collector()->marking_deque()->IsEmpty())) { 4174 CollectAllGarbage(current_gc_flags_, comment); 4175 } 4176 } 4177 4178 4179 bool Heap::TryFinalizeIdleIncrementalMarking(double idle_time_in_ms) { 4180 size_t size_of_objects = static_cast<size_t>(SizeOfObjects()); 4181 double final_incremental_mark_compact_speed_in_bytes_per_ms = 4182 tracer()->FinalIncrementalMarkCompactSpeedInBytesPerMillisecond(); 4183 if (incremental_marking()->IsReadyToOverApproximateWeakClosure() || 4184 (!incremental_marking()->finalize_marking_completed() && 4185 mark_compact_collector()->marking_deque()->IsEmpty() && 4186 gc_idle_time_handler_->ShouldDoOverApproximateWeakClosure( 4187 idle_time_in_ms))) { 4188 FinalizeIncrementalMarking( 4189 "Idle notification: finalize incremental marking"); 4190 return true; 4191 } else if (incremental_marking()->IsComplete() || 4192 (mark_compact_collector()->marking_deque()->IsEmpty() && 4193 gc_idle_time_handler_->ShouldDoFinalIncrementalMarkCompact( 4194 idle_time_in_ms, size_of_objects, 4195 final_incremental_mark_compact_speed_in_bytes_per_ms))) { 4196 CollectAllGarbage(current_gc_flags_, 4197 "idle notification: finalize incremental marking"); 4198 return true; 4199 } 4200 return false; 4201 } 4202 4203 void Heap::RegisterReservationsForBlackAllocation(Reservation* reservations) { 4204 // TODO(hpayer): We do not have to iterate reservations on black objects 4205 // for marking. We just have to execute the special visiting side effect 4206 // code that adds objects to global data structures, e.g. for array buffers. 4207 4208 // Code space, map space, and large object space do not use black pages. 4209 // Hence we have to color all objects of the reservation first black to avoid 4210 // unnecessary marking deque load. 4211 if (incremental_marking()->black_allocation()) { 4212 for (int i = CODE_SPACE; i < Serializer::kNumberOfSpaces; i++) { 4213 const Heap::Reservation& res = reservations[i]; 4214 for (auto& chunk : res) { 4215 Address addr = chunk.start; 4216 while (addr < chunk.end) { 4217 HeapObject* obj = HeapObject::FromAddress(addr); 4218 Marking::MarkBlack(Marking::MarkBitFrom(obj)); 4219 MemoryChunk::IncrementLiveBytesFromGC(obj, obj->Size()); 4220 addr += obj->Size(); 4221 } 4222 } 4223 } 4224 for (int i = OLD_SPACE; i < Serializer::kNumberOfSpaces; i++) { 4225 const Heap::Reservation& res = reservations[i]; 4226 for (auto& chunk : res) { 4227 Address addr = chunk.start; 4228 while (addr < chunk.end) { 4229 HeapObject* obj = HeapObject::FromAddress(addr); 4230 incremental_marking()->IterateBlackObject(obj); 4231 addr += obj->Size(); 4232 } 4233 } 4234 } 4235 } 4236 } 4237 4238 GCIdleTimeHeapState Heap::ComputeHeapState() { 4239 GCIdleTimeHeapState heap_state; 4240 heap_state.contexts_disposed = contexts_disposed_; 4241 heap_state.contexts_disposal_rate = 4242 tracer()->ContextDisposalRateInMilliseconds(); 4243 heap_state.size_of_objects = static_cast<size_t>(SizeOfObjects()); 4244 heap_state.incremental_marking_stopped = incremental_marking()->IsStopped(); 4245 return heap_state; 4246 } 4247 4248 4249 bool Heap::PerformIdleTimeAction(GCIdleTimeAction action, 4250 GCIdleTimeHeapState heap_state, 4251 double deadline_in_ms) { 4252 bool result = false; 4253 switch (action.type) { 4254 case DONE: 4255 result = true; 4256 break; 4257 case DO_INCREMENTAL_STEP: { 4258 if (incremental_marking()->incremental_marking_job()->IdleTaskPending()) { 4259 result = true; 4260 } else { 4261 incremental_marking() 4262 ->incremental_marking_job() 4263 ->NotifyIdleTaskProgress(); 4264 result = IncrementalMarkingJob::IdleTask::Step(this, deadline_in_ms) == 4265 IncrementalMarkingJob::IdleTask::kDone; 4266 } 4267 break; 4268 } 4269 case DO_FULL_GC: { 4270 DCHECK(contexts_disposed_ > 0); 4271 HistogramTimerScope scope(isolate_->counters()->gc_context()); 4272 TRACE_EVENT0("v8", "V8.GCContext"); 4273 CollectAllGarbage(kNoGCFlags, "idle notification: contexts disposed"); 4274 break; 4275 } 4276 case DO_NOTHING: 4277 break; 4278 } 4279 4280 return result; 4281 } 4282 4283 4284 void Heap::IdleNotificationEpilogue(GCIdleTimeAction action, 4285 GCIdleTimeHeapState heap_state, 4286 double start_ms, double deadline_in_ms) { 4287 double idle_time_in_ms = deadline_in_ms - start_ms; 4288 double current_time = MonotonicallyIncreasingTimeInMs(); 4289 last_idle_notification_time_ = current_time; 4290 double deadline_difference = deadline_in_ms - current_time; 4291 4292 contexts_disposed_ = 0; 4293 4294 isolate()->counters()->gc_idle_time_allotted_in_ms()->AddSample( 4295 static_cast<int>(idle_time_in_ms)); 4296 4297 if (deadline_in_ms - start_ms > 4298 GCIdleTimeHandler::kMaxFrameRenderingIdleTime) { 4299 int committed_memory = static_cast<int>(CommittedMemory() / KB); 4300 int used_memory = static_cast<int>(heap_state.size_of_objects / KB); 4301 isolate()->counters()->aggregated_memory_heap_committed()->AddSample( 4302 start_ms, committed_memory); 4303 isolate()->counters()->aggregated_memory_heap_used()->AddSample( 4304 start_ms, used_memory); 4305 } 4306 4307 if (deadline_difference >= 0) { 4308 if (action.type != DONE && action.type != DO_NOTHING) { 4309 isolate()->counters()->gc_idle_time_limit_undershot()->AddSample( 4310 static_cast<int>(deadline_difference)); 4311 } 4312 } else { 4313 isolate()->counters()->gc_idle_time_limit_overshot()->AddSample( 4314 static_cast<int>(-deadline_difference)); 4315 } 4316 4317 if ((FLAG_trace_idle_notification && action.type > DO_NOTHING) || 4318 FLAG_trace_idle_notification_verbose) { 4319 PrintIsolate(isolate_, "%8.0f ms: ", isolate()->time_millis_since_init()); 4320 PrintF( 4321 "Idle notification: requested idle time %.2f ms, used idle time %.2f " 4322 "ms, deadline usage %.2f ms [", 4323 idle_time_in_ms, idle_time_in_ms - deadline_difference, 4324 deadline_difference); 4325 action.Print(); 4326 PrintF("]"); 4327 if (FLAG_trace_idle_notification_verbose) { 4328 PrintF("["); 4329 heap_state.Print(); 4330 PrintF("]"); 4331 } 4332 PrintF("\n"); 4333 } 4334 } 4335 4336 4337 double Heap::MonotonicallyIncreasingTimeInMs() { 4338 return V8::GetCurrentPlatform()->MonotonicallyIncreasingTime() * 4339 static_cast<double>(base::Time::kMillisecondsPerSecond); 4340 } 4341 4342 4343 bool Heap::IdleNotification(int idle_time_in_ms) { 4344 return IdleNotification( 4345 V8::GetCurrentPlatform()->MonotonicallyIncreasingTime() + 4346 (static_cast<double>(idle_time_in_ms) / 4347 static_cast<double>(base::Time::kMillisecondsPerSecond))); 4348 } 4349 4350 4351 bool Heap::IdleNotification(double deadline_in_seconds) { 4352 CHECK(HasBeenSetUp()); 4353 double deadline_in_ms = 4354 deadline_in_seconds * 4355 static_cast<double>(base::Time::kMillisecondsPerSecond); 4356 HistogramTimerScope idle_notification_scope( 4357 isolate_->counters()->gc_idle_notification()); 4358 TRACE_EVENT0("v8", "V8.GCIdleNotification"); 4359 double start_ms = MonotonicallyIncreasingTimeInMs(); 4360 double idle_time_in_ms = deadline_in_ms - start_ms; 4361 4362 tracer()->SampleAllocation(start_ms, NewSpaceAllocationCounter(), 4363 OldGenerationAllocationCounter()); 4364 4365 GCIdleTimeHeapState heap_state = ComputeHeapState(); 4366 4367 GCIdleTimeAction action = 4368 gc_idle_time_handler_->Compute(idle_time_in_ms, heap_state); 4369 4370 bool result = PerformIdleTimeAction(action, heap_state, deadline_in_ms); 4371 4372 IdleNotificationEpilogue(action, heap_state, start_ms, deadline_in_ms); 4373 return result; 4374 } 4375 4376 4377 bool Heap::RecentIdleNotificationHappened() { 4378 return (last_idle_notification_time_ + 4379 GCIdleTimeHandler::kMaxScheduledIdleTime) > 4380 MonotonicallyIncreasingTimeInMs(); 4381 } 4382 4383 class MemoryPressureInterruptTask : public CancelableTask { 4384 public: 4385 explicit MemoryPressureInterruptTask(Heap* heap) 4386 : CancelableTask(heap->isolate()), heap_(heap) {} 4387 4388 virtual ~MemoryPressureInterruptTask() {} 4389 4390 private: 4391 // v8::internal::CancelableTask overrides. 4392 void RunInternal() override { heap_->CheckMemoryPressure(); } 4393 4394 Heap* heap_; 4395 DISALLOW_COPY_AND_ASSIGN(MemoryPressureInterruptTask); 4396 }; 4397 4398 void Heap: