Skip to content

Commit 4e34634

Browse files
nornagontargos
authored andcommitted
src: delegate NodeArrayBufferAllocator to v8's allocator
PR-URL: #43594 Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Shelley Vohr <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 9edea5f commit 4e34634

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
@@ -83,32 +83,32 @@ MaybeLocal<Value> PrepareStackTraceCallback(Local<Context> context,
8383
void* NodeArrayBufferAllocator::Allocate(size_t size) {
8484
void* ret;
8585
if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers)
86-
ret = UncheckedCalloc(size);
86+
ret = allocator_->Allocate(size);
8787
else
88-
ret = UncheckedMalloc(size);
88+
ret = allocator_->AllocateUninitialized(size);
8989
if (LIKELY(ret != nullptr))
9090
total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
9191
return ret;
9292
}
9393

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

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

109109
void NodeArrayBufferAllocator::Free(void* data, size_t size) {
110110
total_mem_usage_.fetch_sub(size, std::memory_order_relaxed);
111-
free(data);
111+
allocator_->Free(data, size);
112112
}
113113

114114
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)