From 565a52b5feedadafa48fa4a4a05f923563f2c759 Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Mon, 27 Jun 2022 13:56:03 -0700 Subject: [PATCH] src: delegate NodeArrayBufferAllocator to v8's allocator --- src/api/environment.cc | 10 +++++----- src/node_internals.h | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/api/environment.cc b/src/api/environment.cc index ccdcdefb20fe84..f7455fc5f1e98c 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -86,16 +86,16 @@ MaybeLocal PrepareStackTraceCallback(Local context, void* NodeArrayBufferAllocator::Allocate(size_t size) { void* ret; if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers) - ret = UncheckedCalloc(size); + ret = allocator_->Allocate(size); else - ret = UncheckedMalloc(size); + ret = allocator_->AllocateUninitialized(size); if (LIKELY(ret != nullptr)) total_mem_usage_.fetch_add(size, std::memory_order_relaxed); return ret; } void* NodeArrayBufferAllocator::AllocateUninitialized(size_t size) { - void* ret = node::UncheckedMalloc(size); + void* ret = allocator_->AllocateUninitialized(size); if (LIKELY(ret != nullptr)) total_mem_usage_.fetch_add(size, std::memory_order_relaxed); return ret; @@ -103,7 +103,7 @@ void* NodeArrayBufferAllocator::AllocateUninitialized(size_t size) { void* NodeArrayBufferAllocator::Reallocate( void* data, size_t old_size, size_t size) { - void* ret = UncheckedRealloc(static_cast(data), size); + void* ret = allocator_->Reallocate(data, old_size, size); if (LIKELY(ret != nullptr) || UNLIKELY(size == 0)) total_mem_usage_.fetch_add(size - old_size, std::memory_order_relaxed); return ret; @@ -111,7 +111,7 @@ void* NodeArrayBufferAllocator::Reallocate( void NodeArrayBufferAllocator::Free(void* data, size_t size) { total_mem_usage_.fetch_sub(size, std::memory_order_relaxed); - free(data); + allocator_->Free(data, size); } DebuggingArrayBufferAllocator::~DebuggingArrayBufferAllocator() { diff --git a/src/node_internals.h b/src/node_internals.h index f8c31b386d7b62..3413a2ac09de57 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -118,6 +118,8 @@ class NodeArrayBufferAllocator : public ArrayBufferAllocator { private: uint32_t zero_fill_field_ = 1; // Boolean but exposed as uint32 to JS land. std::atomic total_mem_usage_ {0}; + + std::unique_ptr allocator_{v8::ArrayBuffer::Allocator::NewDefaultAllocator()}; }; class DebuggingArrayBufferAllocator final : public NodeArrayBufferAllocator {