Skip to content

Commit 4e6a844

Browse files
nornagontargos
authored andcommitted
src: delegate NodeArrayBufferAllocator to v8's allocator
PR-URL: #43594 Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Shelley Vohr <shelley.vohr@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent baa22a7 commit 4e6a844

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/api/environment.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,32 @@ MaybeLocal<Value> PrepareStackTraceCallback(Local<Context> context,
8585
void* NodeArrayBufferAllocator::Allocate(size_t size) {
8686
void* ret;
8787
if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers)
88-
ret = UncheckedCalloc(size);
88+
ret = allocator_->Allocate(size);
8989
else
90-
ret = UncheckedMalloc(size);
90+
ret = allocator_->AllocateUninitialized(size);
9191
if (LIKELY(ret != nullptr))
9292
total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
9393
return ret;
9494
}
9595

9696
void* NodeArrayBufferAllocator::AllocateUninitialized(size_t size) {
97-
void* ret = node::UncheckedMalloc(size);
97+
void* ret = allocator_->AllocateUninitialized(size);
9898
if (LIKELY(ret != nullptr))
9999
total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
100100
return ret;
101101
}
102102

103103
void* NodeArrayBufferAllocator::Reallocate(
104104
void* data, size_t old_size, size_t size) {
105-
void* ret = UncheckedRealloc<char>(static_cast<char*>(data), size);
105+
void* ret = allocator_->Reallocate(data, old_size, size);
106106
if (LIKELY(ret != nullptr) || UNLIKELY(size == 0))
107107
total_mem_usage_.fetch_add(size - old_size, std::memory_order_relaxed);
108108
return ret;
109109
}
110110

111111
void NodeArrayBufferAllocator::Free(void* data, size_t size) {
112112
total_mem_usage_.fetch_sub(size, std::memory_order_relaxed);
113-
free(data);
113+
allocator_->Free(data, size);
114114
}
115115

116116
DebuggingArrayBufferAllocator::~DebuggingArrayBufferAllocator() {

src/node_internals.h

+4
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ class NodeArrayBufferAllocator : public ArrayBufferAllocator {
118118
private:
119119
uint32_t zero_fill_field_ = 1; // Boolean but exposed as uint32 to JS land.
120120
std::atomic<size_t> total_mem_usage_ {0};
121+
122+
// Delegate to V8's allocator for compatibility with the V8 memory cage.
123+
std::unique_ptr<v8::ArrayBuffer::Allocator> allocator_{
124+
v8::ArrayBuffer::Allocator::NewDefaultAllocator()};
121125
};
122126

123127
class DebuggingArrayBufferAllocator final : public NodeArrayBufferAllocator {

0 commit comments

Comments
 (0)