summaryrefslogtreecommitdiff
path: root/src/aliased_buffer.h
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2019-02-18 22:28:17 +0100
committerAnna Henningsen <anna@addaleax.net>2019-02-21 22:22:04 +0100
commitd1011f6bbfadfa254925014896aa710f02403770 (patch)
treea122c55affa499ae4c86f92bafdd145386486043 /src/aliased_buffer.h
parent6fb7baf935442a1ceddcd0a585892456438f95aa (diff)
downloadandroid-node-v8-d1011f6bbfadfa254925014896aa710f02403770.tar.gz
android-node-v8-d1011f6bbfadfa254925014896aa710f02403770.tar.bz2
android-node-v8-d1011f6bbfadfa254925014896aa710f02403770.zip
src: simplify AliasedBuffer lifetime management
Rely on the V8 garbage collector to take care of managing the lifetime of the underlying memory of an `AliasedBuffer`. PR-URL: https://github.com/nodejs/node/pull/26196 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/aliased_buffer.h')
-rw-r--r--src/aliased_buffer.h43
1 files changed, 14 insertions, 29 deletions
diff --git a/src/aliased_buffer.h b/src/aliased_buffer.h
index eae60f4d93..498bab4d23 100644
--- a/src/aliased_buffer.h
+++ b/src/aliased_buffer.h
@@ -30,19 +30,17 @@ class AliasedBuffer {
AliasedBuffer(v8::Isolate* isolate, const size_t count)
: isolate_(isolate),
count_(count),
- byte_offset_(0),
- free_buffer_(true) {
+ byte_offset_(0) {
CHECK_GT(count, 0);
const v8::HandleScope handle_scope(isolate_);
- const size_t size_in_bytes = sizeof(NativeT) * count;
-
- // allocate native buffer
- buffer_ = Calloc<NativeT>(count);
+ const size_t size_in_bytes =
+ MultiplyWithOverflowCheck(sizeof(NativeT), count);
// allocate v8 ArrayBuffer
v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(
- isolate_, buffer_, size_in_bytes);
+ isolate_, size_in_bytes);
+ buffer_ = static_cast<NativeT*>(ab->GetContents().Data());
// allocate v8 TypedArray
v8::Local<V8T> js_array = V8T::New(ab, byte_offset_, count);
@@ -65,8 +63,7 @@ class AliasedBuffer {
v8::Uint8Array>& backing_buffer)
: isolate_(isolate),
count_(count),
- byte_offset_(byte_offset),
- free_buffer_(false) {
+ byte_offset_(byte_offset) {
const v8::HandleScope handle_scope(isolate_);
v8::Local<v8::ArrayBuffer> ab = backing_buffer.GetArrayBuffer();
@@ -74,7 +71,8 @@ class AliasedBuffer {
// validate that the byte_offset is aligned with sizeof(NativeT)
CHECK_EQ(byte_offset & (sizeof(NativeT) - 1), 0);
// validate this fits inside the backing buffer
- CHECK_LE(sizeof(NativeT) * count, ab->ByteLength() - byte_offset);
+ CHECK_LE(MultiplyWithOverflowCheck(sizeof(NativeT), count),
+ ab->ByteLength() - byte_offset);
buffer_ = reinterpret_cast<NativeT*>(
const_cast<uint8_t*>(backing_buffer.GetNativeBuffer() + byte_offset));
@@ -87,25 +85,16 @@ class AliasedBuffer {
: isolate_(that.isolate_),
count_(that.count_),
byte_offset_(that.byte_offset_),
- buffer_(that.buffer_),
- free_buffer_(false) {
+ buffer_(that.buffer_) {
js_array_ = v8::Global<V8T>(that.isolate_, that.GetJSArray());
}
- ~AliasedBuffer() {
- if (free_buffer_ && buffer_ != nullptr) {
- free(buffer_);
- }
- js_array_.Reset();
- }
-
AliasedBuffer& operator=(AliasedBuffer&& that) noexcept {
this->~AliasedBuffer();
isolate_ = that.isolate_;
count_ = that.count_;
byte_offset_ = that.byte_offset_;
buffer_ = that.buffer_;
- free_buffer_ = that.free_buffer_;
js_array_.Reset(isolate_, that.js_array_.Get(isolate_));
@@ -231,29 +220,26 @@ class AliasedBuffer {
void reserve(size_t new_capacity) {
DCHECK_GE(new_capacity, count_);
DCHECK_EQ(byte_offset_, 0);
- DCHECK(free_buffer_);
const v8::HandleScope handle_scope(isolate_);
const size_t old_size_in_bytes = sizeof(NativeT) * count_;
const size_t new_size_in_bytes = sizeof(NativeT) * new_capacity;
+ // allocate v8 new ArrayBuffer
+ v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(
+ isolate_, new_size_in_bytes);
+
// allocate new native buffer
- NativeT* new_buffer = Calloc<NativeT>(new_capacity);
+ NativeT* new_buffer = static_cast<NativeT*>(ab->GetContents().Data());
// copy old content
memcpy(new_buffer, buffer_, old_size_in_bytes);
- // allocate v8 new ArrayBuffer
- v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(
- isolate_, new_buffer, new_size_in_bytes);
-
// allocate v8 TypedArray
v8::Local<V8T> js_array = V8T::New(ab, byte_offset_, new_capacity);
// move over old v8 TypedArray
js_array_ = std::move(v8::Global<V8T>(isolate_, js_array));
- // Free old buffer and set new values
- free(buffer_);
buffer_ = new_buffer;
count_ = new_capacity;
}
@@ -264,7 +250,6 @@ class AliasedBuffer {
size_t byte_offset_;
NativeT* buffer_;
v8::Global<V8T> js_array_;
- bool free_buffer_;
};
} // namespace node