@@ -48,8 +48,14 @@ using v8::kExternalUint8Array;
48
48
49
49
class CallbackInfo {
50
50
public:
51
+ enum Ownership {
52
+ kInternal ,
53
+ kExternal
54
+ };
55
+
51
56
static inline void Free (char * data, void * hint);
52
57
static inline CallbackInfo* New (Isolate* isolate,
58
+ Ownership ownership,
53
59
Handle <Object> object,
54
60
FreeCallback callback,
55
61
void * hint = 0 );
@@ -59,10 +65,12 @@ class CallbackInfo {
59
65
static void WeakCallback (const WeakCallbackData<Object, CallbackInfo>&);
60
66
inline void WeakCallback (Isolate* isolate, Local<Object> object);
61
67
inline CallbackInfo (Isolate* isolate,
68
+ Ownership ownership,
62
69
Handle <Object> object,
63
70
FreeCallback callback,
64
71
void * hint);
65
72
~CallbackInfo ();
73
+ const Ownership ownership_;
66
74
Persistent<Object> persistent_;
67
75
FreeCallback const callback_;
68
76
void * const hint_;
@@ -76,10 +84,11 @@ void CallbackInfo::Free(char* data, void*) {
76
84
77
85
78
86
CallbackInfo* CallbackInfo::New (Isolate* isolate,
87
+ CallbackInfo::Ownership ownership,
79
88
Handle <Object> object,
80
89
FreeCallback callback,
81
90
void * hint) {
82
- return new CallbackInfo (isolate, object, callback, hint);
91
+ return new CallbackInfo (isolate, ownership, object, callback, hint);
83
92
}
84
93
85
94
@@ -94,15 +103,18 @@ Persistent<Object>* CallbackInfo::persistent() {
94
103
95
104
96
105
CallbackInfo::CallbackInfo (Isolate* isolate,
106
+ CallbackInfo::Ownership ownership,
97
107
Handle <Object> object,
98
108
FreeCallback callback,
99
109
void * hint)
100
- : persistent_(isolate, object),
110
+ : ownership_(ownership),
111
+ persistent_ (isolate, object),
101
112
callback_(callback),
102
113
hint_(hint) {
103
114
persistent_.SetWeak (this , WeakCallback);
104
115
persistent_.SetWrapperClassId (ALLOC_ID);
105
116
persistent_.MarkIndependent ();
117
+ isolate->AdjustAmountOfExternalAllocatedMemory (sizeof (*this ));
106
118
}
107
119
108
120
@@ -129,9 +141,11 @@ void CallbackInfo::WeakCallback(Isolate* isolate, Local<Object> object) {
129
141
array_length *= array_size;
130
142
}
131
143
object->SetIndexedPropertiesToExternalArrayData (nullptr , array_type, 0 );
132
- int64_t change_in_bytes = -static_cast <int64_t >(array_length + sizeof (*this ));
133
- isolate->AdjustAmountOfExternalAllocatedMemory (change_in_bytes);
134
144
callback_ (static_cast <char *>(array_data), hint_);
145
+ int64_t change_in_bytes = -static_cast <int64_t >(sizeof (*this ));
146
+ if (ownership_ == kInternal )
147
+ change_in_bytes -= static_cast <int64_t >(array_length);
148
+ isolate->AdjustAmountOfExternalAllocatedMemory (change_in_bytes);
135
149
delete this ;
136
150
}
137
151
@@ -333,7 +347,10 @@ void Alloc(Environment* env,
333
347
env->isolate ()->AdjustAmountOfExternalAllocatedMemory (length);
334
348
size_t size = length / ExternalArraySize (type);
335
349
obj->SetIndexedPropertiesToExternalArrayData (data, type, size);
336
- CallbackInfo::New (env->isolate (), obj, CallbackInfo::Free);
350
+ CallbackInfo::New (env->isolate (),
351
+ CallbackInfo::kInternal ,
352
+ obj,
353
+ CallbackInfo::Free);
337
354
}
338
355
339
356
@@ -381,6 +398,25 @@ void AllocDispose(Environment* env, Handle<Object> obj) {
381
398
}
382
399
383
400
401
+ static void Alloc (Environment* env,
402
+ CallbackInfo::Ownership ownership,
403
+ Handle <Object> obj,
404
+ char * data,
405
+ size_t length,
406
+ FreeCallback fn,
407
+ void * hint,
408
+ enum ExternalArrayType type) {
409
+ CHECK_EQ (false , obj->HasIndexedPropertiesInExternalArrayData ());
410
+ Isolate* isolate = env->isolate ();
411
+ HandleScope handle_scope (isolate);
412
+ env->set_using_smalloc_alloc_cb (true );
413
+ CallbackInfo* info = CallbackInfo::New (isolate, ownership, obj, fn, hint);
414
+ obj->SetHiddenValue (env->smalloc_p_string (), External::New (isolate, info));
415
+ size_t size = length / ExternalArraySize (type);
416
+ obj->SetIndexedPropertiesToExternalArrayData (data, type, size);
417
+ }
418
+
419
+
384
420
void Alloc (Environment* env,
385
421
Handle <Object> obj,
386
422
size_t length,
@@ -404,7 +440,8 @@ void Alloc(Environment* env,
404
440
UNREACHABLE ();
405
441
}
406
442
407
- Alloc (env, obj, data, length, fn, hint, type);
443
+ env->isolate ()->AdjustAmountOfExternalAllocatedMemory (length);
444
+ Alloc (env, CallbackInfo::kInternal , obj, data, length, fn, hint, type);
408
445
}
409
446
410
447
@@ -415,15 +452,7 @@ void Alloc(Environment* env,
415
452
FreeCallback fn,
416
453
void * hint,
417
454
enum ExternalArrayType type) {
418
- CHECK_EQ (false , obj->HasIndexedPropertiesInExternalArrayData ());
419
- Isolate* isolate = env->isolate ();
420
- HandleScope handle_scope (isolate);
421
- env->set_using_smalloc_alloc_cb (true );
422
- CallbackInfo* info = CallbackInfo::New (isolate, obj, fn, hint);
423
- obj->SetHiddenValue (env->smalloc_p_string (), External::New (isolate, info));
424
- isolate->AdjustAmountOfExternalAllocatedMemory (length + sizeof (*info));
425
- size_t size = length / ExternalArraySize (type);
426
- obj->SetIndexedPropertiesToExternalArrayData (data, type, size);
455
+ Alloc (env, CallbackInfo::kExternal , obj, data, length, fn, hint, type);
427
456
}
428
457
429
458
0 commit comments