diff --git a/src/smalloc.cc b/src/smalloc.cc index eda13c7e457fa4..89461959dd8912 100644 --- a/src/smalloc.cc +++ b/src/smalloc.cc @@ -509,6 +509,56 @@ RetainedObjectInfo* WrapperInfo(uint16_t class_id, Handle wrapper) { } +// User facing API. + +void Alloc(Isolate* isolate, + Handle obj, + size_t length, + enum ExternalArrayType type) { + Alloc(Environment::GetCurrent(isolate), obj, length, type); +} + + +void Alloc(Isolate* isolate, + Handle obj, + char* data, + size_t length, + enum ExternalArrayType type) { + Alloc(Environment::GetCurrent(isolate), obj, data, length, type); +} + + +void Alloc(Isolate* isolate, + Handle obj, + size_t length, + FreeCallback fn, + void* hint, + enum ExternalArrayType type) { + Alloc(Environment::GetCurrent(isolate), obj, length, fn, hint, type); +} + + +void Alloc(Isolate* isolate, + Handle obj, + char* data, + size_t length, + FreeCallback fn, + void* hint, + enum ExternalArrayType type) { + Alloc(Environment::GetCurrent(isolate), obj, data, length, fn, hint, type); +} + + +void AllocDispose(Isolate* isolate, Handle obj) { + AllocDispose(Environment::GetCurrent(isolate), obj); +} + + +bool HasExternalData(Isolate* isolate, Local obj) { + return HasExternalData(Environment::GetCurrent(isolate), obj); +} + + void Initialize(Handle exports, Handle unused, Handle context) { diff --git a/src/smalloc.h b/src/smalloc.h index 27c7ec6b6122d9..a2d0f978a23973 100644 --- a/src/smalloc.h +++ b/src/smalloc.h @@ -49,30 +49,31 @@ NODE_EXTERN size_t ExternalArraySize(enum v8::ExternalArrayType type); * v8::kExternalFloatArray); * v8::Local obj = v8::Object::New(); * char* data = static_cast(malloc(byte_length * array_length)); - * node::smalloc::Alloc(env, obj, data, byte_length, v8::kExternalFloatArray); + * node::smalloc::Alloc(isolate, obj, data, byte_length, + * v8::kExternalFloatArray); * obj->Set(v8::String::NewFromUtf8("length"), * v8::Integer::NewFromUnsigned(array_length)); * \code */ -NODE_EXTERN void Alloc(Environment* env, +NODE_EXTERN void Alloc(v8::Isolate* isolate, v8::Handle obj, size_t length, enum v8::ExternalArrayType type = v8::kExternalUnsignedByteArray); -NODE_EXTERN void Alloc(Environment* env, +NODE_EXTERN void Alloc(v8::Isolate* isolate, v8::Handle obj, char* data, size_t length, enum v8::ExternalArrayType type = v8::kExternalUnsignedByteArray); -NODE_EXTERN void Alloc(Environment* env, +NODE_EXTERN void Alloc(v8::Isolate* isolate, v8::Handle obj, size_t length, FreeCallback fn, void* hint, enum v8::ExternalArrayType type = v8::kExternalUnsignedByteArray); -NODE_EXTERN void Alloc(Environment* env, +NODE_EXTERN void Alloc(v8::Isolate* isolate, v8::Handle obj, char* data, size_t length, @@ -85,13 +86,45 @@ NODE_EXTERN void Alloc(Environment* env, * Free memory associated with an externally allocated object. If no external * memory is allocated to the object then nothing will happen. */ -NODE_EXTERN void AllocDispose(Environment* env, v8::Handle obj); +NODE_EXTERN void AllocDispose(v8::Isolate* isolate, v8::Handle obj); /** * Check if the Object has externally allocated memory. */ -NODE_EXTERN bool HasExternalData(Environment* env, v8::Local obj); +NODE_EXTERN bool HasExternalData(v8::Isolate* isolate, + v8::Local obj); + + +// Internal use +void Alloc(Environment* env, + v8::Handle obj, + size_t length, + enum v8::ExternalArrayType type = + v8::kExternalUnsignedByteArray); +void Alloc(Environment* env, + v8::Handle obj, + char* data, + size_t length, + enum v8::ExternalArrayType type = + v8::kExternalUnsignedByteArray); +void Alloc(Environment* env, + v8::Handle obj, + size_t length, + FreeCallback fn, + void* hint, + enum v8::ExternalArrayType type = + v8::kExternalUnsignedByteArray); +void Alloc(Environment* env, + v8::Handle obj, + char* data, + size_t length, + FreeCallback fn, + void* hint, + enum v8::ExternalArrayType type = + v8::kExternalUnsignedByteArray); +void AllocDispose(Environment* env, v8::Handle obj); +bool HasExternalData(Environment* env, v8::Local obj); } // namespace smalloc } // namespace node diff --git a/test/addons/smalloc-alloc/binding.cc b/test/addons/smalloc-alloc/binding.cc new file mode 100644 index 00000000000000..6efde5d6d36e70 --- /dev/null +++ b/test/addons/smalloc-alloc/binding.cc @@ -0,0 +1,30 @@ +#include +#include +#include + +using namespace v8; + +void Alloc(const FunctionCallbackInfo& args) { + Isolate* isolate = args.GetIsolate(); + Local obj = Object::New(isolate); + size_t len = args[0]->Uint32Value(); + node::smalloc::Alloc(isolate, obj, len); + args.GetReturnValue().Set(obj); +} + +void Dispose(const FunctionCallbackInfo& args) { + node::smalloc::AllocDispose(args.GetIsolate(), args[0].As()); +} + +void HasExternalData(const FunctionCallbackInfo& args) { + args.GetReturnValue().Set( + node::smalloc::HasExternalData(args.GetIsolate(), args[0].As())); +} + +void init(Handle target) { + NODE_SET_METHOD(target, "alloc", Alloc); + NODE_SET_METHOD(target, "dispose", Dispose); + NODE_SET_METHOD(target, "hasExternalData", HasExternalData); +} + +NODE_MODULE(binding, init); diff --git a/test/addons/smalloc-alloc/binding.gyp b/test/addons/smalloc-alloc/binding.gyp new file mode 100644 index 00000000000000..3bfb84493f3e87 --- /dev/null +++ b/test/addons/smalloc-alloc/binding.gyp @@ -0,0 +1,8 @@ +{ + 'targets': [ + { + 'target_name': 'binding', + 'sources': [ 'binding.cc' ] + } + ] +} diff --git a/test/addons/smalloc-alloc/test.js b/test/addons/smalloc-alloc/test.js new file mode 100644 index 00000000000000..47197be631ed17 --- /dev/null +++ b/test/addons/smalloc-alloc/test.js @@ -0,0 +1,9 @@ +var assert = require('assert'); +var binding = require('./build/Release/binding'); +var obj = binding.alloc(16); +for (var i = 0; i < 16; i++) { + assert.ok(typeof obj[i] == 'number'); +} +assert.ok(binding.hasExternalData(obj)); +binding.dispose(obj); +assert.ok(typeof obj[0] !== 'number');