@@ -30,19 +30,17 @@ class AliasedBuffer {
30
30
AliasedBuffer (v8::Isolate* isolate, const size_t count)
31
31
: isolate_(isolate),
32
32
count_ (count),
33
- byte_offset_(0 ),
34
- free_buffer_(true ) {
33
+ byte_offset_(0 ) {
35
34
CHECK_GT (count, 0 );
36
35
const v8::HandleScope handle_scope (isolate_);
37
36
38
- const size_t size_in_bytes = sizeof (NativeT) * count;
39
-
40
- // allocate native buffer
41
- buffer_ = Calloc<NativeT>(count);
37
+ const size_t size_in_bytes =
38
+ MultiplyWithOverflowCheck (sizeof (NativeT), count);
42
39
43
40
// allocate v8 ArrayBuffer
44
41
v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New (
45
- isolate_, buffer_, size_in_bytes);
42
+ isolate_, size_in_bytes);
43
+ buffer_ = static_cast <NativeT*>(ab->GetContents ().Data ());
46
44
47
45
// allocate v8 TypedArray
48
46
v8::Local<V8T> js_array = V8T::New (ab, byte_offset_, count);
@@ -65,16 +63,16 @@ class AliasedBuffer {
65
63
v8::Uint8Array>& backing_buffer)
66
64
: isolate_(isolate),
67
65
count_(count),
68
- byte_offset_(byte_offset),
69
- free_buffer_(false ) {
66
+ byte_offset_(byte_offset) {
70
67
const v8::HandleScope handle_scope (isolate_);
71
68
72
69
v8::Local<v8::ArrayBuffer> ab = backing_buffer.GetArrayBuffer ();
73
70
74
71
// validate that the byte_offset is aligned with sizeof(NativeT)
75
72
CHECK_EQ (byte_offset & (sizeof (NativeT) - 1 ), 0 );
76
73
// validate this fits inside the backing buffer
77
- CHECK_LE (sizeof (NativeT) * count, ab->ByteLength () - byte_offset);
74
+ CHECK_LE (MultiplyWithOverflowCheck (sizeof (NativeT), count),
75
+ ab->ByteLength () - byte_offset);
78
76
79
77
buffer_ = reinterpret_cast <NativeT*>(
80
78
const_cast <uint8_t *>(backing_buffer.GetNativeBuffer () + byte_offset));
@@ -87,25 +85,16 @@ class AliasedBuffer {
87
85
: isolate_(that.isolate_),
88
86
count_(that.count_),
89
87
byte_offset_(that.byte_offset_),
90
- buffer_(that.buffer_),
91
- free_buffer_(false ) {
88
+ buffer_(that.buffer_) {
92
89
js_array_ = v8::Global<V8T>(that.isolate_ , that.GetJSArray ());
93
90
}
94
91
95
- ~AliasedBuffer () {
96
- if (free_buffer_ && buffer_ != nullptr ) {
97
- free (buffer_);
98
- }
99
- js_array_.Reset ();
100
- }
101
-
102
92
AliasedBuffer& operator =(AliasedBuffer&& that) noexcept {
103
93
this ->~AliasedBuffer ();
104
94
isolate_ = that.isolate_ ;
105
95
count_ = that.count_ ;
106
96
byte_offset_ = that.byte_offset_ ;
107
97
buffer_ = that.buffer_ ;
108
- free_buffer_ = that.free_buffer_ ;
109
98
110
99
js_array_.Reset (isolate_, that.js_array_ .Get (isolate_));
111
100
@@ -231,29 +220,26 @@ class AliasedBuffer {
231
220
void reserve (size_t new_capacity) {
232
221
DCHECK_GE (new_capacity, count_);
233
222
DCHECK_EQ (byte_offset_, 0 );
234
- DCHECK (free_buffer_);
235
223
const v8::HandleScope handle_scope (isolate_);
236
224
237
225
const size_t old_size_in_bytes = sizeof (NativeT) * count_;
238
226
const size_t new_size_in_bytes = sizeof (NativeT) * new_capacity;
239
227
228
+ // allocate v8 new ArrayBuffer
229
+ v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New (
230
+ isolate_, new_size_in_bytes);
231
+
240
232
// allocate new native buffer
241
- NativeT* new_buffer = Calloc <NativeT>(new_capacity );
233
+ NativeT* new_buffer = static_cast <NativeT*>(ab-> GetContents (). Data () );
242
234
// copy old content
243
235
memcpy (new_buffer, buffer_, old_size_in_bytes);
244
236
245
- // allocate v8 new ArrayBuffer
246
- v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New (
247
- isolate_, new_buffer, new_size_in_bytes);
248
-
249
237
// allocate v8 TypedArray
250
238
v8::Local<V8T> js_array = V8T::New (ab, byte_offset_, new_capacity);
251
239
252
240
// move over old v8 TypedArray
253
241
js_array_ = std::move (v8::Global<V8T>(isolate_, js_array));
254
242
255
- // Free old buffer and set new values
256
- free (buffer_);
257
243
buffer_ = new_buffer;
258
244
count_ = new_capacity;
259
245
}
@@ -264,7 +250,6 @@ class AliasedBuffer {
264
250
size_t byte_offset_;
265
251
NativeT* buffer_;
266
252
v8::Global<V8T> js_array_;
267
- bool free_buffer_;
268
253
};
269
254
} // namespace node
270
255
0 commit comments