• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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 "include/cppgc/testing.h"
6 
7 #include "src/base/logging.h"
8 #include "src/heap/cppgc/heap-base.h"
9 
10 namespace cppgc {
11 namespace testing {
12 
OverrideEmbedderStackStateScope(HeapHandle & heap_handle,EmbedderStackState state)13 OverrideEmbedderStackStateScope::OverrideEmbedderStackStateScope(
14     HeapHandle& heap_handle, EmbedderStackState state)
15     : heap_handle_(heap_handle) {
16   auto& heap = internal::HeapBase::From(heap_handle_);
17   CHECK_NULL(heap.override_stack_state_.get());
18   heap.override_stack_state_ = std::make_unique<EmbedderStackState>(state);
19 }
20 
~OverrideEmbedderStackStateScope()21 OverrideEmbedderStackStateScope::~OverrideEmbedderStackStateScope() {
22   internal::HeapBase::From(heap_handle_).override_stack_state_.reset();
23 }
24 
StandaloneTestingHeap(HeapHandle & heap_handle)25 StandaloneTestingHeap::StandaloneTestingHeap(HeapHandle& heap_handle)
26     : heap_handle_(heap_handle) {}
27 
StartGarbageCollection()28 void StandaloneTestingHeap::StartGarbageCollection() {
29   internal::HeapBase::From(heap_handle_)
30       .StartIncrementalGarbageCollectionForTesting();
31 }
32 
PerformMarkingStep(EmbedderStackState stack_state)33 bool StandaloneTestingHeap::PerformMarkingStep(EmbedderStackState stack_state) {
34   return internal::HeapBase::From(heap_handle_)
35       .marker()
36       ->IncrementalMarkingStepForTesting(stack_state);
37 }
38 
FinalizeGarbageCollection(EmbedderStackState stack_state)39 void StandaloneTestingHeap::FinalizeGarbageCollection(
40     EmbedderStackState stack_state) {
41   internal::HeapBase::From(heap_handle_)
42       .FinalizeIncrementalGarbageCollectionForTesting(stack_state);
43 }
44 
ToggleMainThreadMarking(bool should_mark)45 void StandaloneTestingHeap::ToggleMainThreadMarking(bool should_mark) {
46   internal::HeapBase::From(heap_handle_)
47       .marker()
48       ->SetMainThreadMarkingDisabledForTesting(!should_mark);
49 }
50 
ForceCompactionForNextGarbageCollection()51 void StandaloneTestingHeap::ForceCompactionForNextGarbageCollection() {
52   internal::HeapBase::From(heap_handle_)
53       .compactor()
54       .EnableForNextGCForTesting();
55 }
56 
IsHeapObjectOld(void * object)57 bool IsHeapObjectOld(void* object) {
58 #if defined(CPPGC_YOUNG_GENERATION)
59   return internal::HeapObjectHeader::FromObject(object).IsMarked();
60 #else
61   return true;
62 #endif
63 }
64 
65 }  // namespace testing
66 }  // namespace cppgc
67