Skip to content

Commit 565a52b

Browse files
committed
src: delegate NodeArrayBufferAllocator to v8's allocator
1 parent 986307b commit 565a52b

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/api/environment.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,32 +86,32 @@ MaybeLocal<Value> PrepareStackTraceCallback(Local<Context> context,
8686
void* NodeArrayBufferAllocator::Allocate(size_t size) {
8787
void* ret;
8888
if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers)
89-
ret = UncheckedCalloc(size);
89+
ret = allocator_->Allocate(size);
9090
else
91-
ret = UncheckedMalloc(size);
91+
ret = allocator_->AllocateUninitialized(size);
9292
if (LIKELY(ret != nullptr))
9393
total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
9494
return ret;
9595
}
9696

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

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

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

117117
DebuggingArrayBufferAllocator::~DebuggingArrayBufferAllocator() {

src/node_internals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ 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+
std::unique_ptr<v8::ArrayBuffer::Allocator> allocator_{v8::ArrayBuffer::Allocator::NewDefaultAllocator()};
121123
};
122124

123125
class DebuggingArrayBufferAllocator final : public NodeArrayBufferAllocator {

0 commit comments

Comments
 (0)