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
4041
4142namespace node {
4243
44+ using v8::ExternalMemoryAccounter;
4345using v8::HandleScope;
4446using v8::Isolate;
4547using 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