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,
@@ -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