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#ifndef INCLUDE_CPPGC_TESTING_H_
61cb0ef41Sopenharmony_ci#define INCLUDE_CPPGC_TESTING_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "cppgc/common.h"
91cb0ef41Sopenharmony_ci#include "cppgc/macros.h"
101cb0ef41Sopenharmony_ci#include "v8config.h"  // NOLINT(build/include_directory)
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cinamespace cppgc {
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciclass HeapHandle;
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci/**
171cb0ef41Sopenharmony_ci * Namespace contains testing helpers.
181cb0ef41Sopenharmony_ci */
191cb0ef41Sopenharmony_cinamespace testing {
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci/**
221cb0ef41Sopenharmony_ci * Overrides the state of the stack with the provided value. Parameters passed
231cb0ef41Sopenharmony_ci * to explicit garbage collection calls still take precedence. Must not be
241cb0ef41Sopenharmony_ci * nested.
251cb0ef41Sopenharmony_ci *
261cb0ef41Sopenharmony_ci * This scope is useful to make the garbage collector consider the stack when
271cb0ef41Sopenharmony_ci * tasks that invoke garbage collection (through the provided platform) contain
281cb0ef41Sopenharmony_ci * interesting pointers on its stack.
291cb0ef41Sopenharmony_ci */
301cb0ef41Sopenharmony_ciclass V8_EXPORT V8_NODISCARD OverrideEmbedderStackStateScope final {
311cb0ef41Sopenharmony_ci  CPPGC_STACK_ALLOCATED();
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci public:
341cb0ef41Sopenharmony_ci  /**
351cb0ef41Sopenharmony_ci   * Constructs a scoped object that automatically enters and leaves the scope.
361cb0ef41Sopenharmony_ci   *
371cb0ef41Sopenharmony_ci   * \param heap_handle The corresponding heap.
381cb0ef41Sopenharmony_ci   */
391cb0ef41Sopenharmony_ci  explicit OverrideEmbedderStackStateScope(HeapHandle& heap_handle,
401cb0ef41Sopenharmony_ci                                           EmbedderStackState state);
411cb0ef41Sopenharmony_ci  ~OverrideEmbedderStackStateScope();
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  OverrideEmbedderStackStateScope(const OverrideEmbedderStackStateScope&) =
441cb0ef41Sopenharmony_ci      delete;
451cb0ef41Sopenharmony_ci  OverrideEmbedderStackStateScope& operator=(
461cb0ef41Sopenharmony_ci      const OverrideEmbedderStackStateScope&) = delete;
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci private:
491cb0ef41Sopenharmony_ci  HeapHandle& heap_handle_;
501cb0ef41Sopenharmony_ci};
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci/**
531cb0ef41Sopenharmony_ci * Testing interface for managed heaps that allows for controlling garbage
541cb0ef41Sopenharmony_ci * collection timings. Embedders should use this class when testing the
551cb0ef41Sopenharmony_ci * interaction of their code with incremental/concurrent garbage collection.
561cb0ef41Sopenharmony_ci */
571cb0ef41Sopenharmony_ciclass V8_EXPORT StandaloneTestingHeap final {
581cb0ef41Sopenharmony_ci public:
591cb0ef41Sopenharmony_ci  explicit StandaloneTestingHeap(HeapHandle&);
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  /**
621cb0ef41Sopenharmony_ci   * Start an incremental garbage collection.
631cb0ef41Sopenharmony_ci   */
641cb0ef41Sopenharmony_ci  void StartGarbageCollection();
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  /**
671cb0ef41Sopenharmony_ci   * Perform an incremental step. This will also schedule concurrent steps if
681cb0ef41Sopenharmony_ci   * needed.
691cb0ef41Sopenharmony_ci   *
701cb0ef41Sopenharmony_ci   * \param stack_state The state of the stack during the step.
711cb0ef41Sopenharmony_ci   */
721cb0ef41Sopenharmony_ci  bool PerformMarkingStep(EmbedderStackState stack_state);
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  /**
751cb0ef41Sopenharmony_ci   * Finalize the current garbage collection cycle atomically.
761cb0ef41Sopenharmony_ci   * Assumes that garbage collection is in progress.
771cb0ef41Sopenharmony_ci   *
781cb0ef41Sopenharmony_ci   * \param stack_state The state of the stack for finalizing the garbage
791cb0ef41Sopenharmony_ci   * collection cycle.
801cb0ef41Sopenharmony_ci   */
811cb0ef41Sopenharmony_ci  void FinalizeGarbageCollection(EmbedderStackState stack_state);
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  /**
841cb0ef41Sopenharmony_ci   * Toggle main thread marking on/off. Allows to stress concurrent marking
851cb0ef41Sopenharmony_ci   * (e.g. to better detect data races).
861cb0ef41Sopenharmony_ci   *
871cb0ef41Sopenharmony_ci   * \param should_mark Denotes whether the main thread should contribute to
881cb0ef41Sopenharmony_ci   * marking. Defaults to true.
891cb0ef41Sopenharmony_ci   */
901cb0ef41Sopenharmony_ci  void ToggleMainThreadMarking(bool should_mark);
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  /**
931cb0ef41Sopenharmony_ci   * Force enable compaction for the next garbage collection cycle.
941cb0ef41Sopenharmony_ci   */
951cb0ef41Sopenharmony_ci  void ForceCompactionForNextGarbageCollection();
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci private:
981cb0ef41Sopenharmony_ci  HeapHandle& heap_handle_;
991cb0ef41Sopenharmony_ci};
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ciV8_EXPORT bool IsHeapObjectOld(void*);
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci}  // namespace testing
1041cb0ef41Sopenharmony_ci}  // namespace cppgc
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci#endif  // INCLUDE_CPPGC_TESTING_H_
107