11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "include/cppgc/testing.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "src/base/logging.h"
81cb0ef41Sopenharmony_ci#include "src/heap/cppgc/heap-base.h"
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cinamespace cppgc {
111cb0ef41Sopenharmony_cinamespace testing {
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciOverrideEmbedderStackStateScope::OverrideEmbedderStackStateScope(
141cb0ef41Sopenharmony_ci    HeapHandle& heap_handle, EmbedderStackState state)
151cb0ef41Sopenharmony_ci    : heap_handle_(heap_handle) {
161cb0ef41Sopenharmony_ci  auto& heap = internal::HeapBase::From(heap_handle_);
171cb0ef41Sopenharmony_ci  CHECK_NULL(heap.override_stack_state_.get());
181cb0ef41Sopenharmony_ci  heap.override_stack_state_ = std::make_unique<EmbedderStackState>(state);
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciOverrideEmbedderStackStateScope::~OverrideEmbedderStackStateScope() {
221cb0ef41Sopenharmony_ci  internal::HeapBase::From(heap_handle_).override_stack_state_.reset();
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciStandaloneTestingHeap::StandaloneTestingHeap(HeapHandle& heap_handle)
261cb0ef41Sopenharmony_ci    : heap_handle_(heap_handle) {}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_civoid StandaloneTestingHeap::StartGarbageCollection() {
291cb0ef41Sopenharmony_ci  internal::HeapBase::From(heap_handle_)
301cb0ef41Sopenharmony_ci      .StartIncrementalGarbageCollectionForTesting();
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_cibool StandaloneTestingHeap::PerformMarkingStep(EmbedderStackState stack_state) {
341cb0ef41Sopenharmony_ci  return internal::HeapBase::From(heap_handle_)
351cb0ef41Sopenharmony_ci      .marker()
361cb0ef41Sopenharmony_ci      ->IncrementalMarkingStepForTesting(stack_state);
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_civoid StandaloneTestingHeap::FinalizeGarbageCollection(
401cb0ef41Sopenharmony_ci    EmbedderStackState stack_state) {
411cb0ef41Sopenharmony_ci  internal::HeapBase::From(heap_handle_)
421cb0ef41Sopenharmony_ci      .FinalizeIncrementalGarbageCollectionForTesting(stack_state);
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_civoid StandaloneTestingHeap::ToggleMainThreadMarking(bool should_mark) {
461cb0ef41Sopenharmony_ci  internal::HeapBase::From(heap_handle_)
471cb0ef41Sopenharmony_ci      .marker()
481cb0ef41Sopenharmony_ci      ->SetMainThreadMarkingDisabledForTesting(!should_mark);
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_civoid StandaloneTestingHeap::ForceCompactionForNextGarbageCollection() {
521cb0ef41Sopenharmony_ci  internal::HeapBase::From(heap_handle_)
531cb0ef41Sopenharmony_ci      .compactor()
541cb0ef41Sopenharmony_ci      .EnableForNextGCForTesting();
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_cibool IsHeapObjectOld(void* object) {
581cb0ef41Sopenharmony_ci#if defined(CPPGC_YOUNG_GENERATION)
591cb0ef41Sopenharmony_ci  return internal::HeapObjectHeader::FromObject(object).IsMarked();
601cb0ef41Sopenharmony_ci#else
611cb0ef41Sopenharmony_ci  return true;
621cb0ef41Sopenharmony_ci#endif
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci}  // namespace testing
661cb0ef41Sopenharmony_ci}  // namespace cppgc
67