Skip to content

Commit 084e62a

Browse files
committed
src: use v8::ExternalMemoryAccounter
`Isolate::AdjustAmountOfExternalAllocatedMemory` is deprecated. Refs: v8/v8@7dc4c18
1 parent 32bc1b9 commit 084e62a

10 files changed

+44
-26
lines changed

src/crypto/crypto_bio.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,15 @@ class NodeBIO : public MemoryRetainer {
156156
len_(len),
157157
next_(nullptr) {
158158
data_ = new char[len];
159-
if (env_ != nullptr)
160-
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(len);
159+
if (env_ != nullptr) {
160+
env_->external_memory_accounter()->Increase(env_->isolate(), len);
161+
}
161162
}
162163

163164
~Buffer() {
164165
delete[] data_;
165166
if (env_ != nullptr) {
166-
const int64_t len = static_cast<int64_t>(len_);
167-
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(-len);
167+
env_->external_memory_accounter()->Decrease(env_->isolate(), len_);
168168
}
169169
}
170170

src/crypto/crypto_context.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -1024,12 +1024,13 @@ SecureContext* SecureContext::Create(Environment* env) {
10241024
SecureContext::SecureContext(Environment* env, Local<Object> wrap)
10251025
: BaseObject(env, wrap) {
10261026
MakeWeak();
1027-
env->isolate()->AdjustAmountOfExternalAllocatedMemory(kExternalSize);
1027+
env->external_memory_accounter()->Increase(env->isolate(), kExternalSize);
10281028
}
10291029

10301030
inline void SecureContext::Reset() {
10311031
if (ctx_ != nullptr) {
1032-
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
1032+
env()->external_memory_accounter()->Decrease(env()->isolate(),
1033+
kExternalSize);
10331034
}
10341035
ctx_.reset();
10351036
cert_.reset();

src/crypto/crypto_tls.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ TLSWrap::TLSWrap(Environment* env,
431431
StreamBase::AttachToObject(GetObject());
432432
stream->PushStreamListener(this);
433433

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

436436
InitSSL();
437437
Debug(this, "Created new TLSWrap");
@@ -1317,7 +1317,7 @@ void TLSWrap::Destroy() {
13171317
// And destroy
13181318
InvokeQueued(UV_ECANCELED, "Canceled because of SSL destruction");
13191319

1320-
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
1320+
env()->external_memory_accounter()->Decrease(env()->isolate(), kExternalSize);
13211321
ssl_.reset();
13221322

13231323
enc_in_ = nullptr;

src/env-inl.h

+5
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ inline v8::Isolate* Environment::isolate() const {
205205
return isolate_;
206206
}
207207

208+
inline v8::ExternalMemoryAccounter* Environment::external_memory_accounter()
209+
const {
210+
return external_memory_accounter_;
211+
}
212+
208213
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
209214
return ContainerOf(&Environment::timer_handle_, handle);
210215
}

src/env.cc

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ using v8::CppHeap;
4848
using v8::CppHeapCreateParams;
4949
using v8::EmbedderGraph;
5050
using v8::EscapableHandleScope;
51+
using v8::ExternalMemoryAccounter;
5152
using v8::Function;
5253
using v8::HandleScope;
5354
using v8::HeapProfiler;
@@ -801,6 +802,7 @@ Environment::Environment(IsolateData* isolate_data,
801802
EnvironmentFlags::Flags flags,
802803
ThreadId thread_id)
803804
: isolate_(isolate),
805+
external_memory_accounter_(new ExternalMemoryAccounter()),
804806
isolate_data_(isolate_data),
805807
async_hooks_(isolate, MAYBE_FIELD_PTR(env_info, async_hooks)),
806808
immediate_info_(isolate, MAYBE_FIELD_PTR(env_info, immediate_info)),
@@ -1054,6 +1056,8 @@ Environment::~Environment() {
10541056
addon.Close();
10551057
}
10561058
}
1059+
1060+
delete external_memory_accounter_;
10571061
}
10581062

10591063
void Environment::InitializeLibuv() {

src/env.h

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "req_wrap.h"
4949
#include "util.h"
5050
#include "uv.h"
51+
#include "v8-external-memory-accounter.h"
5152
#include "v8.h"
5253

5354
#if HAVE_OPENSSL
@@ -696,6 +697,7 @@ class Environment final : public MemoryRetainer {
696697
void StartProfilerIdleNotifier();
697698

698699
inline v8::Isolate* isolate() const;
700+
inline v8::ExternalMemoryAccounter* external_memory_accounter() const;
699701
inline uv_loop_t* event_loop() const;
700702
void TryLoadAddon(const char* filename,
701703
int flags,
@@ -1076,6 +1078,7 @@ class Environment final : public MemoryRetainer {
10761078

10771079
std::list<binding::DLib> loaded_addons_;
10781080
v8::Isolate* const isolate_;
1081+
v8::ExternalMemoryAccounter* const external_memory_accounter_;
10791082
IsolateData* const isolate_data_;
10801083

10811084
bool env_handle_initialized_ = false;

src/node_buffer.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ CallbackInfo::CallbackInfo(Environment* env,
154154
hint_(hint),
155155
env_(env) {
156156
env->cleanable_queue()->PushFront(this);
157-
env->isolate()->AdjustAmountOfExternalAllocatedMemory(sizeof(*this));
157+
env->external_memory_accounter()->Increase(env->isolate(), sizeof(*this));
158158
}
159159

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

188187
callback(data_, hint_);
189188
}

src/node_mem-inl.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ void* NgLibMemoryManager<Class, T>::ReallocImpl(void* ptr,
5454
if (mem != nullptr) {
5555
// Adjust the memory info counter.
5656
// TODO(addaleax): Avoid the double bookkeeping we do with
57-
// current_nghttp2_memory_ + AdjustAmountOfExternalAllocatedMemory
57+
// current_nghttp2_memory_ + ExternalMemoryAccounter
5858
// and provide versions of our memory allocation utilities that take an
5959
// Environment*/Isolate* parameter and call the V8 method transparently.
6060
const int64_t new_size = size - previous_size;
6161
manager->IncreaseAllocatedSize(new_size);
62-
manager->env()->isolate()->AdjustAmountOfExternalAllocatedMemory(
63-
new_size);
62+
manager->env()->external_memory_accounter()->Increase(
63+
manager->env()->isolate(), new_size);
6464
*reinterpret_cast<size_t*>(mem) = size;
6565
mem += sizeof(size_t);
6666
} else if (size == 0) {
6767
manager->DecreaseAllocatedSize(previous_size);
68-
manager->env()->isolate()->AdjustAmountOfExternalAllocatedMemory(
69-
-static_cast<int64_t>(previous_size));
68+
manager->env()->external_memory_accounter()->Decrease(
69+
manager->env()->isolate(), previous_size);
7070
}
7171
return mem;
7272
}
@@ -99,8 +99,8 @@ void NgLibMemoryManager<Class, T>::StopTrackingMemory(void* ptr) {
9999
static_cast<char*>(ptr) - sizeof(size_t));
100100
Class* manager = static_cast<Class*>(this);
101101
manager->DecreaseAllocatedSize(*original_ptr);
102-
manager->env()->isolate()->AdjustAmountOfExternalAllocatedMemory(
103-
-static_cast<int64_t>(*original_ptr));
102+
manager->env()->external_memory_accounter()->Decrease(
103+
manager->env()->isolate(), *original_ptr);
104104
*original_ptr = 0;
105105
}
106106

src/node_zlib.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,8 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
640640
if (report == 0) return;
641641
CHECK_IMPLIES(report < 0, zlib_memory_ >= static_cast<size_t>(-report));
642642
zlib_memory_ += report;
643-
AsyncWrap::env()->isolate()->AdjustAmountOfExternalAllocatedMemory(report);
643+
AsyncWrap::env()->external_memory_accounter()->Increase(
644+
AsyncWrap::env()->isolate(), report);
644645
}
645646

646647
struct AllocScope {

src/string_bytes.cc

+12-7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "node_errors.h"
2828
#include "simdutf.h"
2929
#include "util.h"
30+
#include "v8-external-memory-accounter.h"
3031

3132
#include <climits>
3233
#include <cstring> // memcpy
@@ -40,6 +41,7 @@
4041

4142
namespace node {
4243

44+
using v8::ExternalMemoryAccounter;
4345
using v8::HandleScope;
4446
using v8::Isolate;
4547
using v8::Just;
@@ -57,7 +59,8 @@ class ExternString: public ResourceType {
5759
public:
5860
~ExternString() override {
5961
free(const_cast<TypeName*>(data_));
60-
isolate()->AdjustAmountOfExternalAllocatedMemory(-byte_length());
62+
external_memory_accounter_->Decrease(isolate(), byte_length());
63+
delete external_memory_accounter_;
6164
}
6265

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

71-
int64_t byte_length() const {
72-
return length() * sizeof(*data());
73-
}
74+
size_t byte_length() const { return length() * sizeof(*data()); }
7475

7576
static MaybeLocal<Value> NewFromCopy(Isolate* isolate,
7677
const TypeName* data,
@@ -120,16 +121,19 @@ class ExternString: public ResourceType {
120121
return MaybeLocal<Value>();
121122
}
122123

123-
isolate->AdjustAmountOfExternalAllocatedMemory(h_str->byte_length());
124-
125124
return str;
126125
}
127126

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

130129
private:
131130
ExternString(Isolate* isolate, const TypeName* data, size_t length)
132-
: isolate_(isolate), data_(data), length_(length) { }
131+
: isolate_(isolate),
132+
external_memory_accounter_(new ExternalMemoryAccounter()),
133+
data_(data),
134+
length_(length) {
135+
external_memory_accounter_->Increase(isolate, byte_length());
136+
}
133137
static MaybeLocal<Value> NewExternal(Isolate* isolate,
134138
ExternString* h_str);
135139

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

142146
Isolate* isolate_;
147+
ExternalMemoryAccounter* external_memory_accounter_;
143148
const TypeName* data_;
144149
size_t length_;
145150
};

0 commit comments

Comments
 (0)