Skip to content

Commit d5631f6

Browse files
mlippautzV8 LUCI CQ
authored andcommitted
[api] Allow passing CppHeap on Isolate creation
This is a first step towards always having a CppHeap when constructing V8 Isolates. AttachCppHeap() and DetachedCppHeap() are deprecated. Once they are removed, V8 will either be constructed with an API provided CppHeap or will initialize a Heap internally, so that code can rely on its existence. Bug: v8:13754 Change-Id: Ica37dca5da7d30e7f0f3fcbac96c2afe7624f84d Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4989254 Commit-Queue: Michael Lippautz <[email protected]> Reviewed-by: Anton Bikineev <[email protected]> Reviewed-by: Dominik Inführ <[email protected]> Cr-Commit-Position: refs/heads/main@{#90675}
1 parent 4341fa2 commit d5631f6

5 files changed

Lines changed: 55 additions & 6 deletions

File tree

include/v8-isolate.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ class V8_EXPORT Isolate {
294294
*/
295295
FatalErrorCallback fatal_error_callback = nullptr;
296296
OOMErrorCallback oom_error_callback = nullptr;
297+
298+
/**
299+
* A CppHeap used to construct the Isolate. V8 takes ownership of the
300+
* CppHeap passed this way.
301+
*/
302+
CppHeap* cpp_heap = nullptr;
297303
};
298304

299305
/**
@@ -1013,12 +1019,20 @@ class V8_EXPORT Isolate {
10131019
*
10141020
* Multi-threaded use requires the use of v8::Locker/v8::Unlocker, see
10151021
* CppHeap.
1022+
*
1023+
* If a CppHeap is set via CreateParams, then this call is a noop.
10161024
*/
1025+
V8_DEPRECATE_SOON(
1026+
"Set the heap on Isolate creation using CreateParams instead.")
10171027
void AttachCppHeap(CppHeap*);
10181028

10191029
/**
10201030
* Detaches a managed C++ heap if one was attached using `AttachCppHeap()`.
1031+
*
1032+
* If a CppHeap is set via CreateParams, then this call is a noop.
10211033
*/
1034+
V8_DEPRECATE_SOON(
1035+
"Set the heap on Isolate creation using CreateParams instead.")
10221036
void DetachCppHeap();
10231037

10241038
/**

src/api/api.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9594,7 +9594,7 @@ void Isolate::Initialize(Isolate* v8_isolate,
95949594
i_isolate->set_api_external_references(params.external_references);
95959595
i_isolate->set_allow_atomics_wait(params.allow_atomics_wait);
95969596

9597-
i_isolate->heap()->ConfigureHeap(params.constraints);
9597+
i_isolate->heap()->ConfigureHeap(params.constraints, params.cpp_heap);
95989598
if (params.constraints.stack_limit() != nullptr) {
95999599
uintptr_t limit =
96009600
reinterpret_cast<uintptr_t>(params.constraints.stack_limit());

src/heap/heap.cc

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4893,7 +4893,8 @@ size_t Heap::OldGenerationToSemiSpaceRatioLowMemory() {
48934893
return kOldGenerationToSemiSpaceRatioLowMemory / (v8_flags.minor_ms ? 2 : 1);
48944894
}
48954895

4896-
void Heap::ConfigureHeap(const v8::ResourceConstraints& constraints) {
4896+
void Heap::ConfigureHeap(const v8::ResourceConstraints& constraints,
4897+
v8::CppHeap* cpp_heap) {
48974898
// Initialize max_semi_space_size_.
48984899
{
48994900
max_semi_space_size_ = DefaultMaxSemiSpaceSize();
@@ -5054,6 +5055,11 @@ void Heap::ConfigureHeap(const v8::ResourceConstraints& constraints) {
50545055

50555056
code_range_size_ = constraints.code_range_size_in_bytes();
50565057

5058+
if (cpp_heap) {
5059+
AttachCppHeap(cpp_heap);
5060+
owning_cpp_heap_.reset(CppHeap::From(cpp_heap));
5061+
}
5062+
50575063
configured_ = true;
50585064
}
50595065

@@ -5081,7 +5087,7 @@ void Heap::GetFromRingBuffer(char* buffer) {
50815087

50825088
void Heap::ConfigureHeapDefault() {
50835089
v8::ResourceConstraints constraints;
5084-
ConfigureHeap(constraints);
5090+
ConfigureHeap(constraints, nullptr);
50855091
}
50865092

50875093
void Heap::RecordStats(HeapStats* stats, bool take_snapshot) {
@@ -5818,12 +5824,24 @@ EmbedderRootsHandler* Heap::GetEmbedderRootsHandler() const {
58185824
}
58195825

58205826
void Heap::AttachCppHeap(v8::CppHeap* cpp_heap) {
5827+
// The API function should be a noop in case a CppHeap was passed on Isolate
5828+
// creation.
5829+
if (owning_cpp_heap_) {
5830+
return;
5831+
}
5832+
58215833
CHECK(!incremental_marking()->IsMarking());
58225834
CppHeap::From(cpp_heap)->AttachIsolate(isolate());
58235835
cpp_heap_ = cpp_heap;
58245836
}
58255837

58265838
void Heap::DetachCppHeap() {
5839+
// The API function should be a noop in case a CppHeap was passed on Isolate
5840+
// creation.
5841+
if (owning_cpp_heap_) {
5842+
return;
5843+
}
5844+
58275845
CppHeap::From(cpp_heap_)->DetachIsolate();
58285846
cpp_heap_ = nullptr;
58295847
}
@@ -5840,6 +5858,15 @@ void Heap::SetStackStart(void* stack_start) {
58405858
::heap::base::Stack& Heap::stack() { return isolate_->stack(); }
58415859

58425860
void Heap::StartTearDown() {
5861+
if (owning_cpp_heap_) {
5862+
// Release the pointer. The non-owning pointer is still set which allows
5863+
// DetachCppHeap() to work properly.
5864+
auto* cpp_heap = owning_cpp_heap_.release();
5865+
DetachCppHeap();
5866+
// Termination will free up all managed C++ memory and invoke destructors.
5867+
cpp_heap->Terminate();
5868+
}
5869+
58435870
// Finish any ongoing sweeping to avoid stray background tasks still accessing
58445871
// the heap during teardown.
58455872
CompleteSweepingFull();

src/heap/heap.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,8 @@ class Heap final {
712712
// Initialization. ===========================================================
713713
// ===========================================================================
714714

715-
void ConfigureHeap(const v8::ResourceConstraints& constraints);
715+
void ConfigureHeap(const v8::ResourceConstraints& constraints,
716+
v8::CppHeap* cpp_heap);
716717
void ConfigureHeapDefault();
717718

718719
// Prepares the heap, setting up for deserialization.
@@ -2269,7 +2270,14 @@ class Heap final {
22692270
TrustedRange* trusted_range_ = nullptr;
22702271
#endif
22712272

2272-
v8::CppHeap* cpp_heap_ = nullptr; // Owned by the embedder.
2273+
// V8 configuration where V8 owns the heap which is either created or passed
2274+
// in during Isolate initialization.
2275+
std::unique_ptr<CppHeap> owning_cpp_heap_;
2276+
// Deprecated API where the heap is owned by the embedder. This field is
2277+
// always set, independent of which CppHeap configuration (owned, unowned) is
2278+
// used. As soon as Isolate::AttachCppHeap() is removed, this field should
2279+
// also be removed and we should exclusively rely on the owning version.
2280+
v8::CppHeap* cpp_heap_ = nullptr;
22732281
EmbedderRootsHandler* embedder_roots_handler_ =
22742282
nullptr; // Owned by the embedder.
22752283

src/snapshot/mksnapshot.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ int main(int argc, char** argv) {
279279
i::kMaxPCRelativeCodeRangeInMB);
280280
v8::ResourceConstraints constraints;
281281
constraints.set_code_range_size_in_bytes(code_range_size_mb * i::MB);
282-
i_isolate->heap()->ConfigureHeap(constraints);
282+
i_isolate->heap()->ConfigureHeap(constraints, nullptr);
283283
// The isolate contains data from builtin compilation that needs
284284
// to be written out if builtins are embedded.
285285
i_isolate->RegisterEmbeddedFileWriter(&embedded_writer);

0 commit comments

Comments
 (0)