Skip to content

Commit

Permalink
src: use v8::ExternalMemoryAccounter
Browse files Browse the repository at this point in the history
`Isolate::AdjustAmountOfExternalAllocatedMemory` is deprecated.

Refs: v8/v8@7dc4c18
  • Loading branch information
targos authored and nodejs-github-bot committed Feb 20, 2025
1 parent 6a5275e commit 976e6c8
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/crypto/crypto_bio.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ class NodeBIO : public MemoryRetainer {
len_(len),
next_(nullptr) {
data_ = new char[len];
if (env_ != nullptr)
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(len);
if (env_ != nullptr) {
env_->external_memory_accounter()->Increase(env_->isolate(), len);
}
}

~Buffer() {
delete[] data_;
if (env_ != nullptr) {
const int64_t len = static_cast<int64_t>(len_);
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(-len);
env_->external_memory_accounter()->Decrease(env_->isolate(), len_);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/crypto/crypto_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1024,12 +1024,13 @@ SecureContext* SecureContext::Create(Environment* env) {
SecureContext::SecureContext(Environment* env, Local<Object> wrap)
: BaseObject(env, wrap) {
MakeWeak();
env->isolate()->AdjustAmountOfExternalAllocatedMemory(kExternalSize);
env->external_memory_accounter()->Increase(env->isolate(), kExternalSize);
}

inline void SecureContext::Reset() {
if (ctx_ != nullptr) {
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
env()->external_memory_accounter()->Decrease(env()->isolate(),
kExternalSize);
}
ctx_.reset();
cert_.reset();
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/crypto_tls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ TLSWrap::TLSWrap(Environment* env,
StreamBase::AttachToObject(GetObject());
stream->PushStreamListener(this);

env_->isolate()->AdjustAmountOfExternalAllocatedMemory(kExternalSize);
env_->external_memory_accounter()->Increase(env_->isolate(), kExternalSize);

InitSSL();
Debug(this, "Created new TLSWrap");
Expand Down Expand Up @@ -1317,7 +1317,7 @@ void TLSWrap::Destroy() {
// And destroy
InvokeQueued(UV_ECANCELED, "Canceled because of SSL destruction");

env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
env()->external_memory_accounter()->Decrease(env()->isolate(), kExternalSize);
ssl_.reset();

enc_in_ = nullptr;
Expand Down
5 changes: 5 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ inline v8::Isolate* Environment::isolate() const {
return isolate_;
}

inline v8::ExternalMemoryAccounter* Environment::external_memory_accounter()
const {
return external_memory_accounter_;
}

inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
return ContainerOf(&Environment::timer_handle_, handle);
}
Expand Down
4 changes: 4 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ using v8::CppHeap;
using v8::CppHeapCreateParams;
using v8::EmbedderGraph;
using v8::EscapableHandleScope;
using v8::ExternalMemoryAccounter;
using v8::Function;
using v8::HandleScope;
using v8::HeapProfiler;
Expand Down Expand Up @@ -801,6 +802,7 @@ Environment::Environment(IsolateData* isolate_data,
EnvironmentFlags::Flags flags,
ThreadId thread_id)
: isolate_(isolate),
external_memory_accounter_(new ExternalMemoryAccounter()),
isolate_data_(isolate_data),
async_hooks_(isolate, MAYBE_FIELD_PTR(env_info, async_hooks)),
immediate_info_(isolate, MAYBE_FIELD_PTR(env_info, immediate_info)),
Expand Down Expand Up @@ -1054,6 +1056,8 @@ Environment::~Environment() {
addon.Close();
}
}

delete external_memory_accounter_;
}

void Environment::InitializeLibuv() {
Expand Down
3 changes: 3 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "req_wrap.h"
#include "util.h"
#include "uv.h"
#include "v8-external-memory-accounter.h"
#include "v8.h"

#if HAVE_OPENSSL
Expand Down Expand Up @@ -696,6 +697,7 @@ class Environment final : public MemoryRetainer {
void StartProfilerIdleNotifier();

inline v8::Isolate* isolate() const;
inline v8::ExternalMemoryAccounter* external_memory_accounter() const;
inline uv_loop_t* event_loop() const;
void TryLoadAddon(const char* filename,
int flags,
Expand Down Expand Up @@ -1076,6 +1078,7 @@ class Environment final : public MemoryRetainer {

std::list<binding::DLib> loaded_addons_;
v8::Isolate* const isolate_;
v8::ExternalMemoryAccounter* const external_memory_accounter_;
IsolateData* const isolate_data_;

bool env_handle_initialized_ = false;
Expand Down
5 changes: 2 additions & 3 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ CallbackInfo::CallbackInfo(Environment* env,
hint_(hint),
env_(env) {
env->cleanable_queue()->PushFront(this);
env->isolate()->AdjustAmountOfExternalAllocatedMemory(sizeof(*this));
env->external_memory_accounter()->Increase(env->isolate(), sizeof(*this));
}

void CallbackInfo::Clean() {
Expand Down Expand Up @@ -182,8 +182,7 @@ void CallbackInfo::CallAndResetCallback() {
if (callback != nullptr) {
// Clean up all Environment-related state and run the callback.
cleanable_queue_.Remove();
int64_t change_in_bytes = -static_cast<int64_t>(sizeof(*this));
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
env_->external_memory_accounter()->Decrease(env_->isolate(), sizeof(*this));

callback(data_, hint_);
}
Expand Down
14 changes: 7 additions & 7 deletions src/node_mem-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ void* NgLibMemoryManager<Class, T>::ReallocImpl(void* ptr,
if (mem != nullptr) {
// Adjust the memory info counter.
// TODO(addaleax): Avoid the double bookkeeping we do with
// current_nghttp2_memory_ + AdjustAmountOfExternalAllocatedMemory
// current_nghttp2_memory_ + ExternalMemoryAccounter
// and provide versions of our memory allocation utilities that take an
// Environment*/Isolate* parameter and call the V8 method transparently.
const int64_t new_size = size - previous_size;
manager->IncreaseAllocatedSize(new_size);
manager->env()->isolate()->AdjustAmountOfExternalAllocatedMemory(
new_size);
manager->env()->external_memory_accounter()->Increase(
manager->env()->isolate(), new_size);
*reinterpret_cast<size_t*>(mem) = size;
mem += sizeof(size_t);
} else if (size == 0) {
manager->DecreaseAllocatedSize(previous_size);
manager->env()->isolate()->AdjustAmountOfExternalAllocatedMemory(
-static_cast<int64_t>(previous_size));
manager->env()->external_memory_accounter()->Decrease(
manager->env()->isolate(), previous_size);
}
return mem;
}
Expand Down Expand Up @@ -99,8 +99,8 @@ void NgLibMemoryManager<Class, T>::StopTrackingMemory(void* ptr) {
static_cast<char*>(ptr) - sizeof(size_t));
Class* manager = static_cast<Class*>(this);
manager->DecreaseAllocatedSize(*original_ptr);
manager->env()->isolate()->AdjustAmountOfExternalAllocatedMemory(
-static_cast<int64_t>(*original_ptr));
manager->env()->external_memory_accounter()->Decrease(
manager->env()->isolate(), *original_ptr);
*original_ptr = 0;
}

Expand Down
3 changes: 2 additions & 1 deletion src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,8 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
if (report == 0) return;
CHECK_IMPLIES(report < 0, zlib_memory_ >= static_cast<size_t>(-report));
zlib_memory_ += report;
AsyncWrap::env()->isolate()->AdjustAmountOfExternalAllocatedMemory(report);
AsyncWrap::env()->external_memory_accounter()->Increase(
AsyncWrap::env()->isolate(), report);
}

struct AllocScope {
Expand Down
19 changes: 12 additions & 7 deletions src/string_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "node_errors.h"
#include "simdutf.h"
#include "util.h"
#include "v8-external-memory-accounter.h"

#include <climits>
#include <cstring> // memcpy
Expand All @@ -40,6 +41,7 @@

namespace node {

using v8::ExternalMemoryAccounter;
using v8::HandleScope;
using v8::Isolate;
using v8::Just;
Expand All @@ -57,7 +59,8 @@ class ExternString: public ResourceType {
public:
~ExternString() override {
free(const_cast<TypeName*>(data_));
isolate()->AdjustAmountOfExternalAllocatedMemory(-byte_length());
external_memory_accounter_->Decrease(isolate(), byte_length());
delete external_memory_accounter_;
}

const TypeName* data() const override {
Expand All @@ -68,9 +71,7 @@ class ExternString: public ResourceType {
return length_;
}

int64_t byte_length() const {
return length() * sizeof(*data());
}
size_t byte_length() const { return length() * sizeof(*data()); }

static MaybeLocal<Value> NewFromCopy(Isolate* isolate,
const TypeName* data,
Expand Down Expand Up @@ -120,16 +121,19 @@ class ExternString: public ResourceType {
return MaybeLocal<Value>();
}

isolate->AdjustAmountOfExternalAllocatedMemory(h_str->byte_length());

return str;
}

inline Isolate* isolate() const { return isolate_; }

private:
ExternString(Isolate* isolate, const TypeName* data, size_t length)
: isolate_(isolate), data_(data), length_(length) { }
: isolate_(isolate),
external_memory_accounter_(new ExternalMemoryAccounter()),
data_(data),
length_(length) {
external_memory_accounter_->Increase(isolate, byte_length());
}
static MaybeLocal<Value> NewExternal(Isolate* isolate,
ExternString* h_str);

Expand All @@ -140,6 +144,7 @@ class ExternString: public ResourceType {
Local<Value>* error);

Isolate* isolate_;
ExternalMemoryAccounter* external_memory_accounter_;
const TypeName* data_;
size_t length_;
};
Expand Down

0 comments on commit 976e6c8

Please sign in to comment.