From b9649a495976e67d4ef9efad1df88f7095bf46de Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Sat, 9 Jul 2022 04:59:59 -0700 Subject: [PATCH] src: delegate NodeArrayBufferAllocator to v8's allocator PR-URL: https://github.com/nodejs/node/pull/43594 Reviewed-By: Darshan Sen Reviewed-By: Shelley Vohr Reviewed-By: Joyee Cheung Reviewed-By: Anna Henningsen --- src/api/environment.cc | 10 +++++----- src/node_internals.h | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/api/environment.cc b/src/api/environment.cc index cc3edb87feb6ba..6adf7122426ac1 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -83,16 +83,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; @@ -100,7 +100,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; @@ -108,7 +108,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 feeef6d1be34f9..ed208cca7046fa 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -118,6 +118,10 @@ 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}; + + // Delegate to V8's allocator for compatibility with the V8 memory cage. + std::unique_ptr allocator_{ + v8::ArrayBuffer::Allocator::NewDefaultAllocator()}; }; class DebuggingArrayBufferAllocator final : public NodeArrayBufferAllocator {