Skip to content

Commit 26ebe98

Browse files
committed
smalloc: extend user API
node::Environment isn't accessible to user APIs, so extend smalloc to also accept v8::Isolate. Fixes: 75adde0 "src: remove `node_isolate` from source" PR-URL: #905 Reviewed-by: Fedor Indutny <[email protected]>
1 parent 329f364 commit 26ebe98

File tree

5 files changed

+137
-7
lines changed

5 files changed

+137
-7
lines changed

src/smalloc.cc

+50
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,56 @@ RetainedObjectInfo* WrapperInfo(uint16_t class_id, Handle<Value> wrapper) {
509509
}
510510

511511

512+
// User facing API.
513+
514+
void Alloc(Isolate* isolate,
515+
Handle<Object> obj,
516+
size_t length,
517+
enum ExternalArrayType type) {
518+
Alloc(Environment::GetCurrent(isolate), obj, length, type);
519+
}
520+
521+
522+
void Alloc(Isolate* isolate,
523+
Handle<Object> obj,
524+
char* data,
525+
size_t length,
526+
enum ExternalArrayType type) {
527+
Alloc(Environment::GetCurrent(isolate), obj, data, length, type);
528+
}
529+
530+
531+
void Alloc(Isolate* isolate,
532+
Handle<Object> obj,
533+
size_t length,
534+
FreeCallback fn,
535+
void* hint,
536+
enum ExternalArrayType type) {
537+
Alloc(Environment::GetCurrent(isolate), obj, length, fn, hint, type);
538+
}
539+
540+
541+
void Alloc(Isolate* isolate,
542+
Handle<Object> obj,
543+
char* data,
544+
size_t length,
545+
FreeCallback fn,
546+
void* hint,
547+
enum ExternalArrayType type) {
548+
Alloc(Environment::GetCurrent(isolate), obj, data, length, fn, hint, type);
549+
}
550+
551+
552+
void AllocDispose(Isolate* isolate, Handle<Object> obj) {
553+
AllocDispose(Environment::GetCurrent(isolate), obj);
554+
}
555+
556+
557+
bool HasExternalData(Isolate* isolate, Local<Object> obj) {
558+
return HasExternalData(Environment::GetCurrent(isolate), obj);
559+
}
560+
561+
512562
void Initialize(Handle<Object> exports,
513563
Handle<Value> unused,
514564
Handle<Context> context) {

src/smalloc.h

+40-7
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,31 @@ NODE_EXTERN size_t ExternalArraySize(enum v8::ExternalArrayType type);
4949
* v8::kExternalFloatArray);
5050
* v8::Local<v8::Object> obj = v8::Object::New();
5151
* char* data = static_cast<char*>(malloc(byte_length * array_length));
52-
* node::smalloc::Alloc(env, obj, data, byte_length, v8::kExternalFloatArray);
52+
* node::smalloc::Alloc(isolate, obj, data, byte_length,
53+
* v8::kExternalFloatArray);
5354
* obj->Set(v8::String::NewFromUtf8("length"),
5455
* v8::Integer::NewFromUnsigned(array_length));
5556
* \code
5657
*/
57-
NODE_EXTERN void Alloc(Environment* env,
58+
NODE_EXTERN void Alloc(v8::Isolate* isolate,
5859
v8::Handle<v8::Object> obj,
5960
size_t length,
6061
enum v8::ExternalArrayType type =
6162
v8::kExternalUnsignedByteArray);
62-
NODE_EXTERN void Alloc(Environment* env,
63+
NODE_EXTERN void Alloc(v8::Isolate* isolate,
6364
v8::Handle<v8::Object> obj,
6465
char* data,
6566
size_t length,
6667
enum v8::ExternalArrayType type =
6768
v8::kExternalUnsignedByteArray);
68-
NODE_EXTERN void Alloc(Environment* env,
69+
NODE_EXTERN void Alloc(v8::Isolate* isolate,
6970
v8::Handle<v8::Object> obj,
7071
size_t length,
7172
FreeCallback fn,
7273
void* hint,
7374
enum v8::ExternalArrayType type =
7475
v8::kExternalUnsignedByteArray);
75-
NODE_EXTERN void Alloc(Environment* env,
76+
NODE_EXTERN void Alloc(v8::Isolate* isolate,
7677
v8::Handle<v8::Object> obj,
7778
char* data,
7879
size_t length,
@@ -85,13 +86,45 @@ NODE_EXTERN void Alloc(Environment* env,
8586
* Free memory associated with an externally allocated object. If no external
8687
* memory is allocated to the object then nothing will happen.
8788
*/
88-
NODE_EXTERN void AllocDispose(Environment* env, v8::Handle<v8::Object> obj);
89+
NODE_EXTERN void AllocDispose(v8::Isolate* isolate, v8::Handle<v8::Object> obj);
8990

9091

9192
/**
9293
* Check if the Object has externally allocated memory.
9394
*/
94-
NODE_EXTERN bool HasExternalData(Environment* env, v8::Local<v8::Object> obj);
95+
NODE_EXTERN bool HasExternalData(v8::Isolate* isolate,
96+
v8::Local<v8::Object> obj);
97+
98+
99+
// Internal use
100+
void Alloc(Environment* env,
101+
v8::Handle<v8::Object> obj,
102+
size_t length,
103+
enum v8::ExternalArrayType type =
104+
v8::kExternalUnsignedByteArray);
105+
void Alloc(Environment* env,
106+
v8::Handle<v8::Object> obj,
107+
char* data,
108+
size_t length,
109+
enum v8::ExternalArrayType type =
110+
v8::kExternalUnsignedByteArray);
111+
void Alloc(Environment* env,
112+
v8::Handle<v8::Object> obj,
113+
size_t length,
114+
FreeCallback fn,
115+
void* hint,
116+
enum v8::ExternalArrayType type =
117+
v8::kExternalUnsignedByteArray);
118+
void Alloc(Environment* env,
119+
v8::Handle<v8::Object> obj,
120+
char* data,
121+
size_t length,
122+
FreeCallback fn,
123+
void* hint,
124+
enum v8::ExternalArrayType type =
125+
v8::kExternalUnsignedByteArray);
126+
void AllocDispose(Environment* env, v8::Handle<v8::Object> obj);
127+
bool HasExternalData(Environment* env, v8::Local<v8::Object> obj);
95128

96129
} // namespace smalloc
97130
} // namespace node

test/addons/smalloc-alloc/binding.cc

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <node.h>
2+
#include <smalloc.h>
3+
#include <v8.h>
4+
5+
using namespace v8;
6+
7+
void Alloc(const FunctionCallbackInfo<Value>& args) {
8+
Isolate* isolate = args.GetIsolate();
9+
Local<Object> obj = Object::New(isolate);
10+
size_t len = args[0]->Uint32Value();
11+
node::smalloc::Alloc(isolate, obj, len);
12+
args.GetReturnValue().Set(obj);
13+
}
14+
15+
void Dispose(const FunctionCallbackInfo<Value>& args) {
16+
node::smalloc::AllocDispose(args.GetIsolate(), args[0].As<Object>());
17+
}
18+
19+
void HasExternalData(const FunctionCallbackInfo<Value>& args) {
20+
args.GetReturnValue().Set(
21+
node::smalloc::HasExternalData(args.GetIsolate(), args[0].As<Object>()));
22+
}
23+
24+
void init(Handle<Object> target) {
25+
NODE_SET_METHOD(target, "alloc", Alloc);
26+
NODE_SET_METHOD(target, "dispose", Dispose);
27+
NODE_SET_METHOD(target, "hasExternalData", HasExternalData);
28+
}
29+
30+
NODE_MODULE(binding, init);

test/addons/smalloc-alloc/binding.gyp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'binding',
5+
'sources': [ 'binding.cc' ]
6+
}
7+
]
8+
}

test/addons/smalloc-alloc/test.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var assert = require('assert');
2+
var binding = require('./build/Release/binding');
3+
var obj = binding.alloc(16);
4+
for (var i = 0; i < 16; i++) {
5+
assert.ok(typeof obj[i] == 'number');
6+
}
7+
assert.ok(binding.hasExternalData(obj));
8+
binding.dispose(obj);
9+
assert.ok(typeof obj[0] !== 'number');

0 commit comments

Comments
 (0)