Merge pull request #12 from brenca/add-high-memory-option

Add a flag that skips the two round GC and instead uses only one
This commit is contained in:
Cheng Zhao 2017-05-01 16:06:50 +09:00 committed by GitHub
commit 7d9c1a80f0
2 changed files with 17 additions and 4 deletions

View file

@ -11,12 +11,15 @@
namespace mate { namespace mate {
WrappableBase::WrappableBase() WrappableBase::WrappableBase()
: isolate_(nullptr) { : isolate_(nullptr), high_memory_(false) {
} }
WrappableBase::~WrappableBase() { WrappableBase::~WrappableBase() {
if (!wrapper_.IsEmpty()) if (wrapper_.IsEmpty())
GetWrapper()->SetAlignedPointerInInternalField(0, nullptr); return;
GetWrapper()->SetAlignedPointerInInternalField(0, nullptr);
wrapper_.ClearWeak();
wrapper_.Reset(); wrapper_.Reset();
} }
@ -32,6 +35,7 @@ void WrappableBase::InitWith(v8::Isolate* isolate,
wrapper->SetAlignedPointerInInternalField(0, this); wrapper->SetAlignedPointerInInternalField(0, this);
wrapper_.Reset(isolate, wrapper); wrapper_.Reset(isolate, wrapper);
wrapper_.SetWeak(this, FirstWeakCallback, v8::WeakCallbackType::kParameter); wrapper_.SetWeak(this, FirstWeakCallback, v8::WeakCallbackType::kParameter);
wrapper_.MarkIndependent();
// Call object._init if we have one. // Call object._init if we have one.
v8::Local<v8::Function> init; v8::Local<v8::Function> init;
@ -46,7 +50,11 @@ void WrappableBase::FirstWeakCallback(
const v8::WeakCallbackInfo<WrappableBase>& data) { const v8::WeakCallbackInfo<WrappableBase>& data) {
WrappableBase* wrappable = data.GetParameter(); WrappableBase* wrappable = data.GetParameter();
wrappable->wrapper_.Reset(); wrappable->wrapper_.Reset();
data.SetSecondPassCallback(SecondWeakCallback); if (wrappable->high_memory_) {
delete wrappable;
} else {
data.SetSecondPassCallback(SecondWeakCallback);
}
} }
// static // static

View file

@ -44,6 +44,10 @@ class WrappableBase {
// This method should only be called by classes using Constructor. // This method should only be called by classes using Constructor.
virtual void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper); virtual void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> 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: private:
friend struct internal::Destroyable; friend struct internal::Destroyable;
@ -54,6 +58,7 @@ class WrappableBase {
v8::Isolate* isolate_; v8::Isolate* isolate_;
v8::Global<v8::Object> wrapper_; // Weak v8::Global<v8::Object> wrapper_; // Weak
bool high_memory_;
DISALLOW_COPY_AND_ASSIGN(WrappableBase); DISALLOW_COPY_AND_ASSIGN(WrappableBase);
}; };