2020// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121
2222#include " node.h"
23+ #include " base_object-inl.h"
2324#include " env-inl.h"
25+ #include " memory_tracker-inl.h"
2426#include " util-inl.h"
2527#include " v8.h"
2628
@@ -59,7 +61,7 @@ using v8::Value;
5961 V (10 , number_of_detached_contexts, kNumberOfDetachedContextsIndex )
6062
6163#define V (a, b, c ) +1
62- static const size_t kHeapStatisticsPropertiesCount =
64+ static constexpr size_t kHeapStatisticsPropertiesCount =
6365 HEAP_STATISTICS_PROPERTIES (V);
6466#undef V
6567
@@ -71,10 +73,28 @@ static const size_t kHeapStatisticsPropertiesCount =
7173 V (3 , physical_space_size, kPhysicalSpaceSizeIndex )
7274
7375#define V (a, b, c ) +1
74- static const size_t kHeapSpaceStatisticsPropertiesCount =
76+ static constexpr size_t kHeapSpaceStatisticsPropertiesCount =
7577 HEAP_SPACE_STATISTICS_PROPERTIES (V);
7678#undef V
7779
80+ class BindingData : public BaseObject {
81+ public:
82+ BindingData (Environment* env, Local<Object> obj) : BaseObject(env, obj) {}
83+
84+ std::shared_ptr<BackingStore> heap_statistics_buffer;
85+ std::shared_ptr<BackingStore> heap_space_statistics_buffer;
86+ std::shared_ptr<BackingStore> heap_code_statistics_buffer;
87+
88+ void MemoryInfo (MemoryTracker* tracker) const override {
89+ tracker->TrackField (" heap_statistics_buffer" , heap_statistics_buffer);
90+ tracker->TrackField (" heap_space_statistics_buffer" ,
91+ heap_space_statistics_buffer);
92+ tracker->TrackField (" heap_code_statistics_buffer" ,
93+ heap_code_statistics_buffer);
94+ }
95+ SET_SELF_SIZE (BindingData)
96+ SET_MEMORY_INFO_NAME (BindingData)
97+ };
7898
7999#define HEAP_CODE_STATISTICS_PROPERTIES (V ) \
80100 V (0 , code_and_metadata_size, kCodeAndMetadataSizeIndex ) \
@@ -96,40 +116,44 @@ void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
96116
97117
98118void UpdateHeapStatisticsArrayBuffer (const FunctionCallbackInfo<Value>& args) {
99- Environment* env = Environment::GetCurrent (args);
119+ BindingData* data = Unwrap<BindingData> (args. Data () );
100120 HeapStatistics s;
101- env->isolate ()->GetHeapStatistics (&s);
102- double * const buffer = env->heap_statistics_buffer ();
121+ args.GetIsolate ()->GetHeapStatistics (&s);
122+ double * const buffer =
123+ static_cast <double *>(data->heap_statistics_buffer ->Data ());
103124#define V (index, name, _ ) buffer[index] = static_cast <double >(s.name());
104125 HEAP_STATISTICS_PROPERTIES (V)
105126#undef V
106127}
107128
108129
109130void UpdateHeapSpaceStatisticsBuffer (const FunctionCallbackInfo<Value>& args) {
110- Environment* env = Environment::GetCurrent (args);
131+ BindingData* data = Unwrap<BindingData> (args. Data () );
111132 HeapSpaceStatistics s;
112- Isolate* const isolate = env->isolate ();
113- double * buffer = env->heap_space_statistics_buffer ();
114- size_t number_of_heap_spaces = env->isolate ()->NumberOfHeapSpaces ();
133+ Isolate* const isolate = args.GetIsolate ();
134+ size_t number_of_heap_spaces = isolate->NumberOfHeapSpaces ();
135+
136+ double * const buffer =
137+ static_cast <double *>(data->heap_space_statistics_buffer ->Data ());
115138
116139 for (size_t i = 0 ; i < number_of_heap_spaces; i++) {
117140 isolate->GetHeapSpaceStatistics (&s, i);
118141 size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount ;
119- #define V (index, name, _ ) buffer[property_offset + index] = \
120- static_cast <double >(s.name ());
121- HEAP_SPACE_STATISTICS_PROPERTIES (V)
142+ #define V (index, name, _ ) \
143+ buffer[property_offset + index] = static_cast <double >(s.name ());
144+ HEAP_SPACE_STATISTICS_PROPERTIES (V)
122145#undef V
123146 }
124147}
125148
126149
127150void UpdateHeapCodeStatisticsArrayBuffer (
128151 const FunctionCallbackInfo<Value>& args) {
129- Environment* env = Environment::GetCurrent (args);
152+ BindingData* data = Unwrap<BindingData> (args. Data () );
130153 HeapCodeStatistics s;
131- env->isolate ()->GetHeapCodeAndMetadataStatistics (&s);
132- double * const buffer = env->heap_code_statistics_buffer ();
154+ args.GetIsolate ()->GetHeapCodeAndMetadataStatistics (&s);
155+ double * const buffer =
156+ static_cast <double *>(data->heap_code_statistics_buffer ->Data ());
133157#define V (index, name, _ ) buffer[index] = static_cast <double >(s.name());
134158 HEAP_CODE_STATISTICS_PROPERTIES (V)
135159#undef V
@@ -148,6 +172,9 @@ void Initialize(Local<Object> target,
148172 Local<Context> context,
149173 void * priv) {
150174 Environment* env = Environment::GetCurrent (context);
175+ Environment::BindingScope<BindingData> binding_scope (env);
176+ BindingData* binding_data = binding_scope.data ;
177+ if (binding_data == nullptr ) return ;
151178
152179 env->SetMethodNoSideEffect (target, " cachedDataVersionTag" ,
153180 CachedDataVersionTag);
@@ -158,11 +185,11 @@ void Initialize(Local<Object> target,
158185 UpdateHeapStatisticsArrayBuffer);
159186
160187 const size_t heap_statistics_buffer_byte_length =
161- sizeof (*env-> heap_statistics_buffer () ) * kHeapStatisticsPropertiesCount ;
188+ sizeof (double ) * kHeapStatisticsPropertiesCount ;
162189
163190 Local<ArrayBuffer> heap_statistics_ab =
164191 ArrayBuffer::New (env->isolate (), heap_statistics_buffer_byte_length);
165- env-> set_heap_statistics_buffer ( heap_statistics_ab->GetBackingStore () );
192+ binding_data-> heap_statistics_buffer = heap_statistics_ab->GetBackingStore ();
166193 target->Set (env->context (),
167194 FIXED_ONE_BYTE_STRING (env->isolate (),
168195 " heapStatisticsArrayBuffer" ),
@@ -182,19 +209,17 @@ void Initialize(Local<Object> target,
182209 UpdateHeapCodeStatisticsArrayBuffer);
183210
184211 const size_t heap_code_statistics_buffer_byte_length =
185- sizeof (*env->heap_code_statistics_buffer ())
186- * kHeapCodeStatisticsPropertiesCount ;
212+ sizeof (double ) * kHeapCodeStatisticsPropertiesCount ;
187213
188214 Local<ArrayBuffer> heap_code_statistics_ab =
189215 ArrayBuffer::New (env->isolate (),
190216 heap_code_statistics_buffer_byte_length);
191- env-> set_heap_code_statistics_buffer (
192- heap_code_statistics_ab->GetBackingStore ()) ;
217+ binding_data-> heap_code_statistics_buffer =
218+ heap_code_statistics_ab->GetBackingStore ();
193219 target->Set (env->context (),
194220 FIXED_ONE_BYTE_STRING (env->isolate (),
195221 " heapCodeStatisticsArrayBuffer" ),
196- heap_code_statistics_ab)
197- .Check ();
222+ heap_code_statistics_ab).Check ();
198223
199224#define V (i, _, name ) \
200225 target->Set (env->context (), \
@@ -236,20 +261,20 @@ void Initialize(Local<Object> target,
236261 UpdateHeapSpaceStatisticsBuffer);
237262
238263 const size_t heap_space_statistics_buffer_byte_length =
239- sizeof (*env-> heap_space_statistics_buffer () ) *
264+ sizeof (double ) *
240265 kHeapSpaceStatisticsPropertiesCount *
241266 number_of_heap_spaces;
242267
243268 Local<ArrayBuffer> heap_space_statistics_ab =
244269 ArrayBuffer::New (env->isolate (),
245270 heap_space_statistics_buffer_byte_length);
246- env->set_heap_space_statistics_buffer (
247- heap_space_statistics_ab->GetBackingStore ());
271+ binding_data->heap_space_statistics_buffer =
272+ heap_space_statistics_ab->GetBackingStore ();
273+
248274 target->Set (env->context (),
249275 FIXED_ONE_BYTE_STRING (env->isolate (),
250276 " heapSpaceStatisticsArrayBuffer" ),
251- heap_space_statistics_ab)
252- .Check ();
277+ heap_space_statistics_ab).Check ();
253278
254279#define V (i, _, name ) \
255280 target->Set (env->context (), \
0 commit comments