Skip to content

Commit 1d5d7b6

Browse files
targosRafaelGSS
authored andcommitted
src: use v8::ExternalMemoryAccounter
`Isolate::AdjustAmountOfExternalAllocatedMemory` is deprecated. Refs: v8/v8@7dc4c18 PR-URL: #58070 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
1 parent 3779e43 commit 1d5d7b6

File tree

10 files changed

+44
-26
lines changed

10 files changed

+44
-26
lines changed

src/crypto/crypto_bio.h

Lines changed: 4 additions & 4 deletions
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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,12 +1104,13 @@ SecureContext* SecureContext::Create(Environment* env) {
11041104
SecureContext::SecureContext(Environment* env, Local<Object> wrap)
11051105
: BaseObject(env, wrap) {
11061106
MakeWeak();
1107-
env->isolate()->AdjustAmountOfExternalAllocatedMemory(kExternalSize);
1107+
env->external_memory_accounter()->Increase(env->isolate(), kExternalSize);
11081108
}
11091109

11101110
inline void SecureContext::Reset() {
11111111
if (ctx_ != nullptr) {
1112-
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
1112+
env()->external_memory_accounter()->Decrease(env()->isolate(),
1113+
kExternalSize);
11131114
}
11141115
ctx_.reset();
11151116
cert_.reset();

src/crypto/crypto_tls.cc

Lines changed: 2 additions & 2 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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;
@@ -803,6 +804,7 @@ Environment::Environment(IsolateData* isolate_data,
803804
EnvironmentFlags::Flags flags,
804805
ThreadId thread_id)
805806
: isolate_(isolate),
807+
external_memory_accounter_(new ExternalMemoryAccounter()),
806808
isolate_data_(isolate_data),
807809
async_hooks_(isolate, MAYBE_FIELD_PTR(env_info, async_hooks)),
808810
immediate_info_(isolate, MAYBE_FIELD_PTR(env_info, immediate_info)),
@@ -1056,6 +1058,8 @@ Environment::~Environment() {
10561058
addon.Close();
10571059
}
10581060
}
1061+
1062+
delete external_memory_accounter_;
10591063
}
10601064

10611065
void Environment::InitializeLibuv() {

src/env.h

Lines changed: 3 additions & 0 deletions
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,
@@ -1078,6 +1080,7 @@ class Environment final : public MemoryRetainer {
10781080

10791081
std::list<binding::DLib> loaded_addons_;
10801082
v8::Isolate* const isolate_;
1083+
v8::ExternalMemoryAccounter* const external_memory_accounter_;
10811084
IsolateData* const isolate_data_;
10821085

10831086
bool env_handle_initialized_ = false;

src/node_buffer.cc

Lines changed: 2 additions & 3 deletions
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

Lines changed: 7 additions & 7 deletions
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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,8 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
642642
if (report == 0) return;
643643
CHECK_IMPLIES(report < 0, zlib_memory_ >= static_cast<size_t>(-report));
644644
zlib_memory_ += report;
645-
AsyncWrap::env()->isolate()->AdjustAmountOfExternalAllocatedMemory(report);
645+
AsyncWrap::env()->external_memory_accounter()->Increase(
646+
AsyncWrap::env()->isolate(), report);
646647
}
647648

648649
struct AllocScope {

src/string_bytes.cc

Lines changed: 12 additions & 7 deletions
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,
@@ -117,16 +118,19 @@ class ExternString: public ResourceType {
117118
return MaybeLocal<Value>();
118119
}
119120

120-
isolate->AdjustAmountOfExternalAllocatedMemory(h_str->byte_length());
121-
122121
return str;
123122
}
124123

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

127126
private:
128127
ExternString(Isolate* isolate, const TypeName* data, size_t length)
129-
: isolate_(isolate), data_(data), length_(length) { }
128+
: isolate_(isolate),
129+
external_memory_accounter_(new ExternalMemoryAccounter()),
130+
data_(data),
131+
length_(length) {
132+
external_memory_accounter_->Increase(isolate, byte_length());
133+
}
130134
static MaybeLocal<Value> NewExternal(Isolate* isolate,
131135
ExternString* h_str);
132136

@@ -136,6 +140,7 @@ class ExternString: public ResourceType {
136140
size_t length);
137141

138142
Isolate* isolate_;
143+
ExternalMemoryAccounter* external_memory_accounter_;
139144
const TypeName* data_;
140145
size_t length_;
141146
};

0 commit comments

Comments
 (0)