summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2019-03-08 16:19:33 +0100
committerAnna Henningsen <anna@addaleax.net>2019-03-13 09:14:14 +0000
commit0e3adddd0d6e5d90b5224c62ef26b561320d1cff (patch)
tree919a9a4555026d9d04395f8695c2aebd2c380424 /src/api
parent17ab2ed3c885d75ee13286cd31c14ea136d00918 (diff)
downloadandroid-node-v8-0e3adddd0d6e5d90b5224c62ef26b561320d1cff.tar.gz
android-node-v8-0e3adddd0d6e5d90b5224c62ef26b561320d1cff.tar.bz2
android-node-v8-0e3adddd0d6e5d90b5224c62ef26b561320d1cff.zip
embedding: refactor public `ArrayBufferAllocator` API
Use a RAII approach by default, and make it possible for embedders to use the `ArrayBufferAllocator` directly as a V8 `ArrayBuffer::Allocator`, e.g. when passing to `Isolate::CreateParams` manually. PR-URL: https://github.com/nodejs/node/pull/26525 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'src/api')
-rw-r--r--src/api/environment.cc22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/api/environment.cc b/src/api/environment.cc
index 35d93f1cc8..421735a82a 100644
--- a/src/api/environment.cc
+++ b/src/api/environment.cc
@@ -68,7 +68,7 @@ static void OnMessage(Local<Message> message, Local<Value> error) {
}
}
-void* ArrayBufferAllocator::Allocate(size_t size) {
+void* NodeArrayBufferAllocator::Allocate(size_t size) {
if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers)
return UncheckedCalloc(size);
else
@@ -81,14 +81,14 @@ DebuggingArrayBufferAllocator::~DebuggingArrayBufferAllocator() {
void* DebuggingArrayBufferAllocator::Allocate(size_t size) {
Mutex::ScopedLock lock(mutex_);
- void* data = ArrayBufferAllocator::Allocate(size);
+ void* data = NodeArrayBufferAllocator::Allocate(size);
RegisterPointerInternal(data, size);
return data;
}
void* DebuggingArrayBufferAllocator::AllocateUninitialized(size_t size) {
Mutex::ScopedLock lock(mutex_);
- void* data = ArrayBufferAllocator::AllocateUninitialized(size);
+ void* data = NodeArrayBufferAllocator::AllocateUninitialized(size);
RegisterPointerInternal(data, size);
return data;
}
@@ -96,14 +96,14 @@ void* DebuggingArrayBufferAllocator::AllocateUninitialized(size_t size) {
void DebuggingArrayBufferAllocator::Free(void* data, size_t size) {
Mutex::ScopedLock lock(mutex_);
UnregisterPointerInternal(data, size);
- ArrayBufferAllocator::Free(data, size);
+ NodeArrayBufferAllocator::Free(data, size);
}
void* DebuggingArrayBufferAllocator::Reallocate(void* data,
size_t old_size,
size_t size) {
Mutex::ScopedLock lock(mutex_);
- void* ret = ArrayBufferAllocator::Reallocate(data, old_size, size);
+ void* ret = NodeArrayBufferAllocator::Reallocate(data, old_size, size);
if (ret == nullptr) {
if (size == 0) // i.e. equivalent to free().
UnregisterPointerInternal(data, old_size);
@@ -146,11 +146,15 @@ void DebuggingArrayBufferAllocator::RegisterPointerInternal(void* data,
allocations_[data] = size;
}
-ArrayBufferAllocator* CreateArrayBufferAllocator() {
- if (per_process::cli_options->debug_arraybuffer_allocations)
- return new DebuggingArrayBufferAllocator();
+std::unique_ptr<ArrayBufferAllocator> ArrayBufferAllocator::Create(bool debug) {
+ if (debug || per_process::cli_options->debug_arraybuffer_allocations)
+ return std::make_unique<DebuggingArrayBufferAllocator>();
else
- return new ArrayBufferAllocator();
+ return std::make_unique<NodeArrayBufferAllocator>();
+}
+
+ArrayBufferAllocator* CreateArrayBufferAllocator() {
+ return ArrayBufferAllocator::Create().release();
}
void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator) {