v8/include/cppgc/testing.h
Anton Bikineev 4c2b5f4d30 cppgc: Provide API to test if object is marked or freed
This is needed to support minor gc tests on the Blink side.

Bug: chromium:1029379
Change-Id: Ic7ce1ae8e626b87738d76663ceecf7198e425d18
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3437050
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Commit-Queue: Anton Bikineev <bikineev@chromium.org>
Auto-Submit: Anton Bikineev <bikineev@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78943}
2022-02-04 12:30:05 +00:00

102 lines
2.8 KiB
C++

// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_TESTING_H_
#define INCLUDE_CPPGC_TESTING_H_
#include "cppgc/common.h"
#include "cppgc/macros.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
class HeapHandle;
/**
* Namespace contains testing helpers.
*/
namespace testing {
/**
* Overrides the state of the stack with the provided value. Takes precedence
* over other parameters that set the stack state. Must no be nested.
*/
class V8_EXPORT V8_NODISCARD OverrideEmbedderStackStateScope final {
CPPGC_STACK_ALLOCATED();
public:
/**
* Constructs a scoped object that automatically enters and leaves the scope.
*
* \param heap_handle The corresponding heap.
*/
explicit OverrideEmbedderStackStateScope(HeapHandle& heap_handle,
EmbedderStackState state);
~OverrideEmbedderStackStateScope();
OverrideEmbedderStackStateScope(const OverrideEmbedderStackStateScope&) =
delete;
OverrideEmbedderStackStateScope& operator=(
const OverrideEmbedderStackStateScope&) = delete;
private:
HeapHandle& heap_handle_;
};
/**
* Testing interface for managed heaps that allows for controlling garbage
* collection timings. Embedders should use this class when testing the
* interaction of their code with incremental/concurrent garbage collection.
*/
class V8_EXPORT StandaloneTestingHeap final {
public:
explicit StandaloneTestingHeap(HeapHandle&);
/**
* Start an incremental garbage collection.
*/
void StartGarbageCollection();
/**
* Perform an incremental step. This will also schedule concurrent steps if
* needed.
*
* \param stack_state The state of the stack during the step.
*/
bool PerformMarkingStep(EmbedderStackState stack_state);
/**
* Finalize the current garbage collection cycle atomically.
* Assumes that garbage collection is in progress.
*
* \param stack_state The state of the stack for finalizing the garbage
* collection cycle.
*/
void FinalizeGarbageCollection(EmbedderStackState stack_state);
/**
* Toggle main thread marking on/off. Allows to stress concurrent marking
* (e.g. to better detect data races).
*
* \param should_mark Denotes whether the main thread should contribute to
* marking. Defaults to true.
*/
void ToggleMainThreadMarking(bool should_mark);
/**
* Force enable compaction for the next garbage collection cycle.
*/
void ForceCompactionForNextGarbageCollection();
private:
HeapHandle& heap_handle_;
};
V8_EXPORT bool IsHeapObjectOld(void*);
} // namespace testing
} // namespace cppgc
#endif // INCLUDE_CPPGC_TESTING_H_