Skip to content

Commit 9deade4

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 bada87b commit 9deade4

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
@@ -537,6 +537,56 @@ RetainedObjectInfo* WrapperInfo(uint16_t class_id, Handle<Value> wrapper) {
537537
}
538538

539539

540+
// User facing API.
541+
542+
void Alloc(Isolate* isolate,
543+
Handle<Object> obj,
544+
size_t length,
545+
enum ExternalArrayType type) {
546+
Alloc(Environment::GetCurrent(isolate), obj, length, type);
547+
}
548+
549+
550+
void Alloc(Isolate* isolate,
551+
Handle<Object> obj,
552+
char* data,
553+
size_t length,
554+
enum ExternalArrayType type) {
555+
Alloc(Environment::GetCurrent(isolate), obj, data, length, type);
556+
}
557+
558+
559+
void Alloc(Isolate* isolate,
560+
Handle<Object> obj,
561+
size_t length,
562+
FreeCallback fn,
563+
void* hint,
564+
enum ExternalArrayType type) {
565+
Alloc(Environment::GetCurrent(isolate), obj, length, fn, hint, type);
566+
}
567+
568+
569+
void Alloc(Isolate* isolate,
570+
Handle<Object> obj,
571+
char* data,
572+
size_t length,
573+
FreeCallback fn,
574+
void* hint,
575+
enum ExternalArrayType type) {
576+
Alloc(Environment::GetCurrent(isolate), obj, data, length, fn, hint, type);
577+
}
578+
579+
580+
void AllocDispose(Isolate* isolate, Handle<Object> obj) {
581+
AllocDispose(Environment::GetCurrent(isolate), obj);
582+
}
583+
584+
585+
bool HasExternalData(Isolate* isolate, Local<Object> obj) {
586+
return HasExternalData(Environment::GetCurrent(isolate), obj);
587+
}
588+
589+
540590
void Initialize(Handle<Object> exports,
541591
Handle<Value> unused,
542592
Handle<Context> context) {

src/smalloc.h

+40-7
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,31 @@ NODE_EXTERN size_t ExternalArraySize(enum v8::ExternalArrayType type);
7070
* v8::kExternalFloatArray);
7171
* v8::Local<v8::Object> obj = v8::Object::New();
7272
* char* data = static_cast<char*>(malloc(byte_length * array_length));
73-
* node::smalloc::Alloc(obj, data, byte_length, v8::kExternalFloatArray);
73+
* node::smalloc::Alloc(isolate, obj, data, byte_length,
74+
* v8::kExternalFloatArray);
7475
* obj->Set(v8::String::NewFromUtf8("length"),
7576
* v8::Integer::NewFromUnsigned(array_length));
7677
* \code
7778
*/
78-
NODE_EXTERN void Alloc(Environment* env,
79+
NODE_EXTERN void Alloc(v8::Isolate* isolate,
7980
v8::Handle<v8::Object> obj,
8081
size_t length,
8182
enum v8::ExternalArrayType type =
8283
v8::kExternalUnsignedByteArray);
83-
NODE_EXTERN void Alloc(Environment* env,
84+
NODE_EXTERN void Alloc(v8::Isolate* isolate,
8485
v8::Handle<v8::Object> obj,
8586
char* data,
8687
size_t length,
8788
enum v8::ExternalArrayType type =
8889
v8::kExternalUnsignedByteArray);
89-
NODE_EXTERN void Alloc(Environment* env,
90+
NODE_EXTERN void Alloc(v8::Isolate* isolate,
9091
v8::Handle<v8::Object> obj,
9192
size_t length,
9293
FreeCallback fn,
9394
void* hint,
9495
enum v8::ExternalArrayType type =
9596
v8::kExternalUnsignedByteArray);
96-
NODE_EXTERN void Alloc(Environment* env,
97+
NODE_EXTERN void Alloc(v8::Isolate* isolate,
9798
v8::Handle<v8::Object> obj,
9899
char* data,
99100
size_t length,
@@ -106,13 +107,45 @@ NODE_EXTERN void Alloc(Environment* env,
106107
* Free memory associated with an externally allocated object. If no external
107108
* memory is allocated to the object then nothing will happen.
108109
*/
109-
NODE_EXTERN void AllocDispose(Environment* env, v8::Handle<v8::Object> obj);
110+
NODE_EXTERN void AllocDispose(v8::Isolate* isolate, v8::Handle<v8::Object> obj);
110111

111112

112113
/**
113114
* Check if the Object has externally allocated memory.
114115
*/
115-
NODE_EXTERN bool HasExternalData(Environment* env, v8::Local<v8::Object> obj);
116+
NODE_EXTERN bool HasExternalData(v8::Isolate* isolate,
117+
v8::Local<v8::Object> obj);
118+
119+
120+
// Internal use
121+
void Alloc(Environment* env,
122+
v8::Handle<v8::Object> obj,
123+
size_t length,
124+
enum v8::ExternalArrayType type =
125+
v8::kExternalUnsignedByteArray);
126+
void Alloc(Environment* env,
127+
v8::Handle<v8::Object> obj,
128+
char* data,
129+
size_t length,
130+
enum v8::ExternalArrayType type =
131+
v8::kExternalUnsignedByteArray);
132+
void Alloc(Environment* env,
133+
v8::Handle<v8::Object> obj,
134+
size_t length,
135+
FreeCallback fn,
136+
void* hint,
137+
enum v8::ExternalArrayType type =
138+
v8::kExternalUnsignedByteArray);
139+
void Alloc(Environment* env,
140+
v8::Handle<v8::Object> obj,
141+
char* data,
142+
size_t length,
143+
FreeCallback fn,
144+
void* hint,
145+
enum v8::ExternalArrayType type =
146+
v8::kExternalUnsignedByteArray);
147+
void AllocDispose(Environment* env, v8::Handle<v8::Object> obj);
148+
bool HasExternalData(Environment* env, v8::Local<v8::Object> obj);
116149

117150
} // namespace smalloc
118151
} // 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)