@@ -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
50825088void Heap::ConfigureHeapDefault () {
50835089 v8::ResourceConstraints constraints;
5084- ConfigureHeap (constraints);
5090+ ConfigureHeap (constraints, nullptr );
50855091}
50865092
50875093void Heap::RecordStats (HeapStats* stats, bool take_snapshot) {
@@ -5818,12 +5824,24 @@ EmbedderRootsHandler* Heap::GetEmbedderRootsHandler() const {
58185824}
58195825
58205826void 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
58265838void 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
58425860void 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 ();
0 commit comments