Browse Source

smalloc: don't take address of stack var

v0.11.4-release
Ben Noordhuis 12 years ago
parent
commit
3220bc4254
  1. 42
      src/smalloc.cc

42
src/smalloc.cc

@ -53,18 +53,18 @@ using v8::kExternalUnsignedByteArray;
struct CallbackInfo { struct CallbackInfo {
void* hint; void* hint;
FreeCallback cb; FreeCallback cb;
Persistent<Object>* p_obj; Persistent<Object> p_obj;
}; };
typedef v8::WeakReferenceCallbacks<Object, char>::Revivable Callback;
typedef v8::WeakReferenceCallbacks<Object, void>::Revivable CallbackFree;
void TargetCallback(Isolate* isolate, void TargetCallback(Isolate* isolate,
Persistent<Object>* target, Persistent<Object>* target,
char* arg); char* arg);
void TargetFreeCallback(Isolate* isolate, void TargetFreeCallback(Isolate* isolate,
Persistent<Object>* target, Persistent<Object>* target,
void* arg); CallbackInfo* arg);
void TargetFreeCallback(Isolate* isolate,
Local<Object> target,
CallbackInfo* cb_info);
Cached<String> smalloc_sym; Cached<String> smalloc_sym;
static bool using_alloc_cb; static bool using_alloc_cb;
@ -165,7 +165,7 @@ void Alloc(Handle<Object> obj, char* data, size_t length) {
void TargetCallback(Isolate* isolate, void TargetCallback(Isolate* isolate,
Persistent<Object>* target, Persistent<Object>* target,
char* data) { char* data) {
HandleScope handle_scope(node_isolate); HandleScope handle_scope(isolate);
Local<Object> obj = Local<Object>::New(isolate, *target); Local<Object> obj = Local<Object>::New(isolate, *target);
int len = obj->GetIndexedPropertiesExternalArrayDataLength(); int len = obj->GetIndexedPropertiesExternalArrayDataLength();
if (data != NULL && len > 0) { if (data != NULL && len > 0) {
@ -187,10 +187,8 @@ void AllocDispose(Handle<Object> obj) {
if (using_alloc_cb && obj->Has(smalloc_sym)) { if (using_alloc_cb && obj->Has(smalloc_sym)) {
Local<External> ext = obj->Get(smalloc_sym).As<External>(); Local<External> ext = obj->Get(smalloc_sym).As<External>();
CallbackInfo* cb_info = static_cast<CallbackInfo*>(ext->Value()); CallbackInfo* cb_info = static_cast<CallbackInfo*>(ext->Value());
Local<Object> obj = Local<Object>::New(node_isolate, *cb_info->p_obj); Local<Object> obj = Local<Object>::New(node_isolate, cb_info->p_obj);
char* data = static_cast<char*>( TargetFreeCallback(node_isolate, obj, cb_info);
obj->GetIndexedPropertiesExternalArrayData());
TargetFreeCallback(node_isolate, cb_info->p_obj, data);
return; return;
} }
@ -229,17 +227,16 @@ void Alloc(Handle<Object> obj,
using_alloc_cb = true; using_alloc_cb = true;
} }
Persistent<Object> p_obj(node_isolate, obj);
CallbackInfo* cb_info = new CallbackInfo; CallbackInfo* cb_info = new CallbackInfo;
cb_info->cb = fn; cb_info->cb = fn;
cb_info->hint = hint; cb_info->hint = hint;
cb_info->p_obj = &p_obj; cb_info->p_obj.Reset(node_isolate, obj);
node_isolate->AdjustAmountOfExternalAllocatedMemory(length + node_isolate->AdjustAmountOfExternalAllocatedMemory(length +
sizeof(*cb_info)); sizeof(*cb_info));
p_obj.MakeWeak(static_cast<void*>(cb_info), TargetFreeCallback); cb_info->p_obj.MakeWeak(cb_info, TargetFreeCallback);
p_obj.MarkIndependent(); cb_info->p_obj.MarkIndependent();
p_obj.SetWrapperClassId(ALLOC_ID); cb_info->p_obj.SetWrapperClassId(ALLOC_ID);
obj->SetIndexedPropertiesToExternalArrayData(data, obj->SetIndexedPropertiesToExternalArrayData(data,
kExternalUnsignedByteArray, kExternalUnsignedByteArray,
length); length);
@ -248,14 +245,21 @@ void Alloc(Handle<Object> obj,
void TargetFreeCallback(Isolate* isolate, void TargetFreeCallback(Isolate* isolate,
Persistent<Object>* target, Persistent<Object>* target,
void* arg) { CallbackInfo* cb_info) {
HandleScope handle_scope(node_isolate); HandleScope handle_scope(isolate);
Local<Object> obj = Local<Object>::New(isolate, *target); Local<Object> obj = Local<Object>::New(isolate, *target);
TargetFreeCallback(isolate, obj, cb_info);
}
void TargetFreeCallback(Isolate* isolate,
Local<Object> obj,
CallbackInfo* cb_info) {
HandleScope handle_scope(isolate);
char* data = static_cast<char*>(obj->GetIndexedPropertiesExternalArrayData()); char* data = static_cast<char*>(obj->GetIndexedPropertiesExternalArrayData());
int len = obj->GetIndexedPropertiesExternalArrayDataLength(); int len = obj->GetIndexedPropertiesExternalArrayDataLength();
CallbackInfo* cb_info = static_cast<CallbackInfo*>(arg);
isolate->AdjustAmountOfExternalAllocatedMemory(-(len + sizeof(*cb_info))); isolate->AdjustAmountOfExternalAllocatedMemory(-(len + sizeof(*cb_info)));
(*target).Dispose(); cb_info->p_obj.Dispose();
cb_info->cb(data, cb_info->hint); cb_info->cb(data, cb_info->hint);
delete cb_info; delete cb_info;
} }

Loading…
Cancel
Save