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 {
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<v8::Function> init;
@ -46,7 +50,11 @@ void WrappableBase::FirstWeakCallback(
const v8::WeakCallbackInfo<WrappableBase>& data) {
WrappableBase* wrappable = data.GetParameter();
wrappable->wrapper_.Reset();
data.SetSecondPassCallback(SecondWeakCallback);
if (wrappable->high_memory_) {
delete wrappable;
} else {
data.SetSecondPassCallback(SecondWeakCallback);
}
}
// static

View file

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