From ff8f439afdccc5693906e994655994ef0308a212 Mon Sep 17 00:00:00 2001 From: Heilig Benedek Date: Wed, 12 Apr 2017 15:51:28 +0200 Subject: [PATCH] add ability to set high memory usage, skipping the second round of GC callbacks --- native_mate/wrappable.cc | 16 ++++++++++++---- native_mate/wrappable_base.h | 5 +++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/native_mate/wrappable.cc b/native_mate/wrappable.cc index c42bc96dab64..6743a139f54e 100644 --- a/native_mate/wrappable.cc +++ b/native_mate/wrappable.cc @@ -11,12 +11,15 @@ namespace mate { WrappableBase::WrappableBase() - : isolate_(nullptr) { + : isolate_(nullptr), high_memory_(false) { } WrappableBase::~WrappableBase() { - if (!wrapper_.IsEmpty()) - GetWrapper()->SetAlignedPointerInInternalField(0, nullptr); + if (wrapper_.IsEmpty()) + return; + + GetWrapper()->SetAlignedPointerInInternalField(0, nullptr); + wrapper_.ClearWeak(); wrapper_.Reset(); } @@ -32,6 +35,7 @@ void WrappableBase::InitWith(v8::Isolate* isolate, wrapper->SetAlignedPointerInInternalField(0, this); wrapper_.Reset(isolate, wrapper); wrapper_.SetWeak(this, FirstWeakCallback, v8::WeakCallbackType::kParameter); + wrapper_.MarkIndependent(); // Call object._init if we have one. v8::Local init; @@ -46,7 +50,11 @@ void WrappableBase::FirstWeakCallback( const v8::WeakCallbackInfo& data) { WrappableBase* wrappable = data.GetParameter(); wrappable->wrapper_.Reset(); - data.SetSecondPassCallback(SecondWeakCallback); + if (wrappable->high_memory_) { + delete wrappable; + } else { + data.SetSecondPassCallback(SecondWeakCallback); + } } // static diff --git a/native_mate/wrappable_base.h b/native_mate/wrappable_base.h index 942d699f2055..17a10fc0d8bc 100644 --- a/native_mate/wrappable_base.h +++ b/native_mate/wrappable_base.h @@ -44,6 +44,10 @@ class WrappableBase { // This method should only be called by classes using Constructor. virtual void InitWith(v8::Isolate* isolate, v8::Local wrapper); + // Marks wrapped object as high memory usage + // Deletes the wrapped object on the first round of GC callbacks + void MarkHighMemoryUsage() { high_memory_ = true; } + private: friend struct internal::Destroyable; @@ -54,6 +58,7 @@ class WrappableBase { v8::Isolate* isolate_; v8::Global wrapper_; // Weak + bool high_memory_; DISALLOW_COPY_AND_ASSIGN(WrappableBase); };