20
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
22
22
#include " node.h"
23
+ #include " base_object-inl.h"
23
24
#include " env-inl.h"
25
+ #include " memory_tracker-inl.h"
24
26
#include " util-inl.h"
25
27
#include " v8.h"
26
28
27
29
namespace node {
28
30
29
31
using v8::Array;
30
32
using v8::ArrayBuffer;
33
+ using v8::BackingStore;
31
34
using v8::Context;
32
35
using v8::FunctionCallbackInfo;
33
36
using v8::HeapCodeStatistics;
@@ -59,7 +62,7 @@ using v8::Value;
59
62
V (10 , number_of_detached_contexts, kNumberOfDetachedContextsIndex )
60
63
61
64
#define V (a, b, c ) +1
62
- static const size_t kHeapStatisticsPropertiesCount =
65
+ static constexpr size_t kHeapStatisticsPropertiesCount =
63
66
HEAP_STATISTICS_PROPERTIES (V);
64
67
#undef V
65
68
@@ -71,10 +74,28 @@ static const size_t kHeapStatisticsPropertiesCount =
71
74
V (3 , physical_space_size, kPhysicalSpaceSizeIndex )
72
75
73
76
#define V (a, b, c ) +1
74
- static const size_t kHeapSpaceStatisticsPropertiesCount =
77
+ static constexpr size_t kHeapSpaceStatisticsPropertiesCount =
75
78
HEAP_SPACE_STATISTICS_PROPERTIES (V);
76
79
#undef V
77
80
81
+ class BindingData : public BaseObject {
82
+ public:
83
+ BindingData (Environment* env, Local<Object> obj) : BaseObject(env, obj) {}
84
+
85
+ std::shared_ptr<BackingStore> heap_statistics_buffer;
86
+ std::shared_ptr<BackingStore> heap_space_statistics_buffer;
87
+ std::shared_ptr<BackingStore> heap_code_statistics_buffer;
88
+
89
+ void MemoryInfo (MemoryTracker* tracker) const override {
90
+ tracker->TrackField (" heap_statistics_buffer" , heap_statistics_buffer);
91
+ tracker->TrackField (" heap_space_statistics_buffer" ,
92
+ heap_space_statistics_buffer);
93
+ tracker->TrackField (" heap_code_statistics_buffer" ,
94
+ heap_code_statistics_buffer);
95
+ }
96
+ SET_SELF_SIZE (BindingData)
97
+ SET_MEMORY_INFO_NAME (BindingData)
98
+ };
78
99
79
100
#define HEAP_CODE_STATISTICS_PROPERTIES (V ) \
80
101
V (0 , code_and_metadata_size, kCodeAndMetadataSizeIndex ) \
@@ -96,40 +117,44 @@ void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
96
117
97
118
98
119
void UpdateHeapStatisticsArrayBuffer (const FunctionCallbackInfo<Value>& args) {
99
- Environment* env = Environment::GetCurrent (args);
120
+ BindingData* data = Unwrap<BindingData> (args. Data () );
100
121
HeapStatistics s;
101
- env->isolate ()->GetHeapStatistics (&s);
102
- double * const buffer = env->heap_statistics_buffer ();
122
+ args.GetIsolate ()->GetHeapStatistics (&s);
123
+ double * const buffer =
124
+ static_cast <double *>(data->heap_statistics_buffer ->Data ());
103
125
#define V (index, name, _ ) buffer[index] = static_cast <double >(s.name());
104
126
HEAP_STATISTICS_PROPERTIES (V)
105
127
#undef V
106
128
}
107
129
108
130
109
131
void UpdateHeapSpaceStatisticsBuffer (const FunctionCallbackInfo<Value>& args) {
110
- Environment* env = Environment::GetCurrent (args);
132
+ BindingData* data = Unwrap<BindingData> (args. Data () );
111
133
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 ();
134
+ Isolate* const isolate = args.GetIsolate ();
135
+ size_t number_of_heap_spaces = isolate->NumberOfHeapSpaces ();
136
+
137
+ double * const buffer =
138
+ static_cast <double *>(data->heap_space_statistics_buffer ->Data ());
115
139
116
140
for (size_t i = 0 ; i < number_of_heap_spaces; i++) {
117
141
isolate->GetHeapSpaceStatistics (&s, i);
118
142
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)
143
+ #define V (index, name, _ ) \
144
+ buffer[property_offset + index ] = static_cast <double >(s.name ());
145
+ HEAP_SPACE_STATISTICS_PROPERTIES (V)
122
146
#undef V
123
147
}
124
148
}
125
149
126
150
127
151
void UpdateHeapCodeStatisticsArrayBuffer (
128
152
const FunctionCallbackInfo<Value>& args) {
129
- Environment* env = Environment::GetCurrent (args);
153
+ BindingData* data = Unwrap<BindingData> (args. Data () );
130
154
HeapCodeStatistics s;
131
- env->isolate ()->GetHeapCodeAndMetadataStatistics (&s);
132
- double * const buffer = env->heap_code_statistics_buffer ();
155
+ args.GetIsolate ()->GetHeapCodeAndMetadataStatistics (&s);
156
+ double * const buffer =
157
+ static_cast <double *>(data->heap_code_statistics_buffer ->Data ());
133
158
#define V (index, name, _ ) buffer[index] = static_cast <double >(s.name());
134
159
HEAP_CODE_STATISTICS_PROPERTIES (V)
135
160
#undef V
@@ -148,6 +173,9 @@ void Initialize(Local<Object> target,
148
173
Local<Context> context,
149
174
void * priv) {
150
175
Environment* env = Environment::GetCurrent (context);
176
+ Environment::BindingScope<BindingData> binding_scope (env);
177
+ if (!binding_scope) return ;
178
+ BindingData* binding_data = binding_scope.data ;
151
179
152
180
env->SetMethodNoSideEffect (target, " cachedDataVersionTag" ,
153
181
CachedDataVersionTag);
@@ -158,11 +186,11 @@ void Initialize(Local<Object> target,
158
186
UpdateHeapStatisticsArrayBuffer);
159
187
160
188
const size_t heap_statistics_buffer_byte_length =
161
- sizeof (*env-> heap_statistics_buffer () ) * kHeapStatisticsPropertiesCount ;
189
+ sizeof (double ) * kHeapStatisticsPropertiesCount ;
162
190
163
191
Local<ArrayBuffer> heap_statistics_ab =
164
192
ArrayBuffer::New (env->isolate (), heap_statistics_buffer_byte_length);
165
- env-> set_heap_statistics_buffer ( heap_statistics_ab->GetBackingStore () );
193
+ binding_data-> heap_statistics_buffer = heap_statistics_ab->GetBackingStore ();
166
194
target->Set (env->context (),
167
195
FIXED_ONE_BYTE_STRING (env->isolate (),
168
196
" heapStatisticsArrayBuffer" ),
@@ -182,19 +210,17 @@ void Initialize(Local<Object> target,
182
210
UpdateHeapCodeStatisticsArrayBuffer);
183
211
184
212
const size_t heap_code_statistics_buffer_byte_length =
185
- sizeof (*env->heap_code_statistics_buffer ())
186
- * kHeapCodeStatisticsPropertiesCount ;
213
+ sizeof (double ) * kHeapCodeStatisticsPropertiesCount ;
187
214
188
215
Local<ArrayBuffer> heap_code_statistics_ab =
189
216
ArrayBuffer::New (env->isolate (),
190
217
heap_code_statistics_buffer_byte_length);
191
- env-> set_heap_code_statistics_buffer (
192
- heap_code_statistics_ab->GetBackingStore ()) ;
218
+ binding_data-> heap_code_statistics_buffer =
219
+ heap_code_statistics_ab->GetBackingStore ();
193
220
target->Set (env->context (),
194
221
FIXED_ONE_BYTE_STRING (env->isolate (),
195
222
" heapCodeStatisticsArrayBuffer" ),
196
- heap_code_statistics_ab)
197
- .Check ();
223
+ heap_code_statistics_ab).Check ();
198
224
199
225
#define V (i, _, name ) \
200
226
target->Set (env->context (), \
@@ -236,20 +262,20 @@ void Initialize(Local<Object> target,
236
262
UpdateHeapSpaceStatisticsBuffer);
237
263
238
264
const size_t heap_space_statistics_buffer_byte_length =
239
- sizeof (*env-> heap_space_statistics_buffer () ) *
265
+ sizeof (double ) *
240
266
kHeapSpaceStatisticsPropertiesCount *
241
267
number_of_heap_spaces;
242
268
243
269
Local<ArrayBuffer> heap_space_statistics_ab =
244
270
ArrayBuffer::New (env->isolate (),
245
271
heap_space_statistics_buffer_byte_length);
246
- env->set_heap_space_statistics_buffer (
247
- heap_space_statistics_ab->GetBackingStore ());
272
+ binding_data->heap_space_statistics_buffer =
273
+ heap_space_statistics_ab->GetBackingStore ();
274
+
248
275
target->Set (env->context (),
249
276
FIXED_ONE_BYTE_STRING (env->isolate (),
250
277
" heapSpaceStatisticsArrayBuffer" ),
251
- heap_space_statistics_ab)
252
- .Check ();
278
+ heap_space_statistics_ab).Check ();
253
279
254
280
#define V (i, _, name ) \
255
281
target->Set (env->context (), \
0 commit comments