• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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/cppgc/heap-base.h"
6 
7 #include "src/base/bounded-page-allocator.h"
8 #include "src/base/platform/platform.h"
9 #include "src/heap/base/stack.h"
10 #include "src/heap/cppgc/globals.h"
11 #include "src/heap/cppgc/heap-object-header.h"
12 #include "src/heap/cppgc/heap-page.h"
13 #include "src/heap/cppgc/heap-visitor.h"
14 #include "src/heap/cppgc/marker.h"
15 #include "src/heap/cppgc/marking-verifier.h"
16 #include "src/heap/cppgc/page-memory.h"
17 #include "src/heap/cppgc/prefinalizer-handler.h"
18 #include "src/heap/cppgc/stats-collector.h"
19 
20 namespace cppgc {
21 namespace internal {
22 
23 namespace {
24 
25 class ObjectSizeCounter : private HeapVisitor<ObjectSizeCounter> {
26   friend class HeapVisitor<ObjectSizeCounter>;
27 
28  public:
GetSize(RawHeap * heap)29   size_t GetSize(RawHeap* heap) {
30     Traverse(heap);
31     return accumulated_size_;
32   }
33 
34  private:
ObjectSize(const HeapObjectHeader * header)35   static size_t ObjectSize(const HeapObjectHeader* header) {
36     const size_t size =
37         header->IsLargeObject()
38             ? static_cast<const LargePage*>(BasePage::FromPayload(header))
39                   ->PayloadSize()
40             : header->GetSize();
41     DCHECK_GE(size, sizeof(HeapObjectHeader));
42     return size - sizeof(HeapObjectHeader);
43   }
44 
VisitHeapObjectHeader(HeapObjectHeader * header)45   bool VisitHeapObjectHeader(HeapObjectHeader* header) {
46     if (header->IsFree()) return true;
47     accumulated_size_ += ObjectSize(header);
48     return true;
49   }
50 
51   size_t accumulated_size_ = 0;
52 };
53 
54 }  // namespace
55 
HeapBase(std::shared_ptr<cppgc::Platform> platform,const std::vector<std::unique_ptr<CustomSpaceBase>> & custom_spaces,StackSupport stack_support)56 HeapBase::HeapBase(
57     std::shared_ptr<cppgc::Platform> platform,
58     const std::vector<std::unique_ptr<CustomSpaceBase>>& custom_spaces,
59     StackSupport stack_support)
60     : raw_heap_(this, custom_spaces),
61       platform_(std::move(platform)),
62 #if defined(CPPGC_CAGED_HEAP)
63       caged_heap_(this, platform_->GetPageAllocator()),
64       page_backend_(std::make_unique<PageBackend>(&caged_heap_.allocator())),
65 #else
66       page_backend_(
67           std::make_unique<PageBackend>(platform_->GetPageAllocator())),
68 #endif
69       stats_collector_(std::make_unique<StatsCollector>()),
70       stack_(std::make_unique<heap::base::Stack>(
71           v8::base::Stack::GetStackStart())),
72       prefinalizer_handler_(std::make_unique<PreFinalizerHandler>()),
73       compactor_(raw_heap_),
74       object_allocator_(&raw_heap_, page_backend_.get(),
75                         stats_collector_.get()),
76       sweeper_(&raw_heap_, platform_.get(), stats_collector_.get()),
77       stack_support_(stack_support) {
78 }
79 
80 HeapBase::~HeapBase() = default;
81 
ObjectPayloadSize() const82 size_t HeapBase::ObjectPayloadSize() const {
83   return ObjectSizeCounter().GetSize(const_cast<RawHeap*>(&raw_heap()));
84 }
85 
NoGCScope(HeapBase & heap)86 HeapBase::NoGCScope::NoGCScope(HeapBase& heap) : heap_(heap) {
87   heap_.no_gc_scope_++;
88 }
89 
~NoGCScope()90 HeapBase::NoGCScope::~NoGCScope() { heap_.no_gc_scope_--; }
91 
AdvanceIncrementalGarbageCollectionOnAllocationIfNeeded()92 void HeapBase::AdvanceIncrementalGarbageCollectionOnAllocationIfNeeded() {
93   if (marker_) marker_->AdvanceMarkingOnAllocation();
94 }
95 
96 }  // namespace internal
97 }  // namespace cppgc
98