diff options
Diffstat (limited to 'src/smalloc.cc')
-rw-r--r-- | src/smalloc.cc | 95 |
1 files changed, 52 insertions, 43 deletions
diff --git a/src/smalloc.cc b/src/smalloc.cc index 7b8e43336..347d7035b 100644 --- a/src/smalloc.cc +++ b/src/smalloc.cc @@ -95,20 +95,21 @@ size_t ExternalArraySize(enum ExternalArrayType type) { // copyOnto(source, source_start, dest, dest_start, copy_length) void CopyOnto(const FunctionCallbackInfo<Value>& args) { - HandleScope handle_scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); if (!args[0]->IsObject()) - return ThrowTypeError("source must be an object"); + return env->ThrowTypeError("source must be an object"); if (!args[2]->IsObject()) - return ThrowTypeError("dest must be an object"); + return env->ThrowTypeError("dest must be an object"); Local<Object> source = args[0].As<Object>(); Local<Object> dest = args[2].As<Object>(); if (!source->HasIndexedPropertiesInExternalArrayData()) - return ThrowError("source has no external array data"); + return env->ThrowError("source has no external array data"); if (!dest->HasIndexedPropertiesInExternalArrayData()) - return ThrowError("dest has no external array data"); + return env->ThrowError("dest has no external array data"); size_t source_start = args[1]->Uint32Value(); size_t dest_start = args[3]->Uint32Value(); @@ -131,16 +132,16 @@ void CopyOnto(const FunctionCallbackInfo<Value>& args) { // optimization for Uint8 arrays (i.e. Buffers) if (source_size != 1 && dest_size != 1) { if (source_size == 0) - return ThrowTypeError("unknown source external array type"); + return env->ThrowTypeError("unknown source external array type"); if (dest_size == 0) - return ThrowTypeError("unknown dest external array type"); + return env->ThrowTypeError("unknown dest external array type"); if (source_length * source_size < source_length) - return ThrowRangeError("source_length * source_size overflow"); + return env->ThrowRangeError("source_length * source_size overflow"); if (copy_length * source_size < copy_length) - return ThrowRangeError("copy_length * source_size overflow"); + return env->ThrowRangeError("copy_length * source_size overflow"); if (dest_length * dest_size < dest_length) - return ThrowRangeError("dest_length * dest_size overflow"); + return env->ThrowRangeError("dest_length * dest_size overflow"); source_length *= source_size; copy_length *= source_size; @@ -149,19 +150,19 @@ void CopyOnto(const FunctionCallbackInfo<Value>& args) { // necessary to check in case (source|dest)_start _and_ copy_length overflow if (copy_length > source_length) - return ThrowRangeError("copy_length > source_length"); + return env->ThrowRangeError("copy_length > source_length"); if (copy_length > dest_length) - return ThrowRangeError("copy_length > dest_length"); + return env->ThrowRangeError("copy_length > dest_length"); if (source_start > source_length) - return ThrowRangeError("source_start > source_length"); + return env->ThrowRangeError("source_start > source_length"); if (dest_start > dest_length) - return ThrowRangeError("dest_start > dest_length"); + return env->ThrowRangeError("dest_start > dest_length"); // now we can guarantee these will catch oob access and *_start overflow if (source_start + copy_length > source_length) - return ThrowRangeError("source_start + copy_length > source_length"); + return env->ThrowRangeError("source_start + copy_length > source_length"); if (dest_start + copy_length > dest_length) - return ThrowRangeError("dest_start + copy_length > dest_length"); + return env->ThrowRangeError("dest_start + copy_length > dest_length"); memmove(dest_data + dest_start, source_data + source_start, copy_length); } @@ -171,7 +172,8 @@ void CopyOnto(const FunctionCallbackInfo<Value>& args) { // for internal use: // dest._data = sliceOnto(source, dest, start, end); void SliceOnto(const FunctionCallbackInfo<Value>& args) { - HandleScope handle_scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); Local<Object> source = args[0].As<Object>(); Local<Object> dest = args[1].As<Object>(); @@ -211,13 +213,14 @@ void SliceOnto(const FunctionCallbackInfo<Value>& args) { // for internal use: // alloc(obj, n[, type]); void Alloc(const FunctionCallbackInfo<Value>& args) { - HandleScope handle_scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); Local<Object> obj = args[0].As<Object>(); // can't perform this check in JS if (obj->HasIndexedPropertiesInExternalArrayData()) - return ThrowTypeError("object already has external array data"); + return env->ThrowTypeError("object already has external array data"); size_t length = args[1]->Uint32Value(); enum ExternalArrayType array_type; @@ -232,19 +235,22 @@ void Alloc(const FunctionCallbackInfo<Value>& args) { length *= type_length; } - Alloc(obj, length, array_type); + Alloc(env, obj, length, array_type); args.GetReturnValue().Set(obj); } -void Alloc(Handle<Object> obj, size_t length, enum ExternalArrayType type) { +void Alloc(Environment* env, + Handle<Object> obj, + size_t length, + enum ExternalArrayType type) { size_t type_size = ExternalArraySize(type); assert(length <= kMaxLength); assert(type_size > 0); if (length == 0) - return Alloc(obj, NULL, length, type); + return Alloc(env, obj, NULL, length, type); char* data = static_cast<char*>(malloc(length)); if (data == NULL) { @@ -252,17 +258,18 @@ void Alloc(Handle<Object> obj, size_t length, enum ExternalArrayType type) { " v8::ExternalArrayType)", "Out Of Memory"); } - Alloc(obj, data, length, type); + Alloc(env, obj, data, length, type); } -void Alloc(Handle<Object> obj, +void Alloc(Environment* env, + Handle<Object> obj, char* data, size_t length, enum ExternalArrayType type) { assert(!obj->HasIndexedPropertiesInExternalArrayData()); - Persistent<Object> p_obj(node_isolate, obj); - node_isolate->AdjustAmountOfExternalAllocatedMemory(length); + Persistent<Object> p_obj(env->isolate(), obj); + env->isolate()->AdjustAmountOfExternalAllocatedMemory(length); p_obj.MakeWeak(data, TargetCallback); p_obj.MarkIndependent(); p_obj.SetWrapperClassId(ALLOC_ID); @@ -293,20 +300,20 @@ void TargetCallback(Isolate* isolate, // for internal use: dispose(obj); void AllocDispose(const FunctionCallbackInfo<Value>& args) { - AllocDispose(args[0].As<Object>()); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + AllocDispose(env, args[0].As<Object>()); } -void AllocDispose(Handle<Object> obj) { - HandleScope handle_scope(node_isolate); - Environment* env = Environment::GetCurrent(node_isolate); +void AllocDispose(Environment* env, Handle<Object> obj) { + HandleScope handle_scope(env->isolate()); if (env->using_smalloc_alloc_cb()) { Local<Value> ext_v = obj->GetHiddenValue(env->smalloc_p_string()); if (ext_v->IsExternal()) { Local<External> ext = ext_v.As<External>(); CallbackInfo* cb_info = static_cast<CallbackInfo*>(ext->Value()); - TargetFreeCallback(node_isolate, &cb_info->p_obj, cb_info); + TargetFreeCallback(env->isolate(), &cb_info->p_obj, cb_info); return; } } @@ -329,12 +336,13 @@ void AllocDispose(Handle<Object> obj) { free(data); } if (length != 0) { - node_isolate->AdjustAmountOfExternalAllocatedMemory(-length); + env->isolate()->AdjustAmountOfExternalAllocatedMemory(-length); } } -void Alloc(Handle<Object> obj, +void Alloc(Environment* env, + Handle<Object> obj, size_t length, FreeCallback fn, void* hint, @@ -349,11 +357,12 @@ void Alloc(Handle<Object> obj, length *= type_size; char* data = new char[length]; - Alloc(obj, data, length, fn, hint, type); + Alloc(env, obj, data, length, fn, hint, type); } -void Alloc(Handle<Object> obj, +void Alloc(Environment* env, + Handle<Object> obj, char* data, size_t length, FreeCallback fn, @@ -361,18 +370,17 @@ void Alloc(Handle<Object> obj, enum ExternalArrayType type) { assert(!obj->HasIndexedPropertiesInExternalArrayData()); - HandleScope handle_scope(node_isolate); - Environment* env = Environment::GetCurrent(node_isolate); + HandleScope handle_scope(env->isolate()); env->set_using_smalloc_alloc_cb(true); CallbackInfo* cb_info = new CallbackInfo; cb_info->cb = fn; cb_info->hint = hint; - cb_info->p_obj.Reset(node_isolate, obj); + cb_info->p_obj.Reset(env->isolate(), obj); obj->SetHiddenValue(env->smalloc_p_string(), External::New(cb_info)); - node_isolate->AdjustAmountOfExternalAllocatedMemory(length + - sizeof(*cb_info)); + env->isolate()->AdjustAmountOfExternalAllocatedMemory(length + + sizeof(*cb_info)); cb_info->p_obj.MakeWeak(cb_info, TargetFreeCallback); cb_info->p_obj.MarkIndependent(); cb_info->p_obj.SetWrapperClassId(ALLOC_ID); @@ -404,12 +412,13 @@ void TargetFreeCallback(Isolate* isolate, void HasExternalData(const FunctionCallbackInfo<Value>& args) { + Environment* env = Environment::GetCurrent(args.GetIsolate()); args.GetReturnValue().Set(args[0]->IsObject() && - HasExternalData(args[0].As<Object>())); + HasExternalData(env, args[0].As<Object>())); } -bool HasExternalData(Local<Object> obj) { +bool HasExternalData(Environment* env, Local<Object> obj) { return obj->HasIndexedPropertiesInExternalArrayData(); } @@ -485,7 +494,7 @@ void Initialize(Handle<Object> exports, NODE_SET_METHOD(exports, "hasExternalData", HasExternalData); - exports->Set(FIXED_ONE_BYTE_STRING(node_isolate, "kMaxLength"), + exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kMaxLength"), Uint32::NewFromUnsigned(kMaxLength, env->isolate())); HeapProfiler* heap_profiler = env->isolate()->GetHeapProfiler(); |