diff --git a/src/api/environment.cc b/src/api/environment.cc index f3a8f49812d5ce..b337e026a953af 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -85,16 +85,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; @@ -102,7 +102,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; @@ -110,7 +110,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..4be91ca699c04c 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 {