@@ -236,26 +236,24 @@ class HeapSnapshotStream : public AsyncWrap,
236
236
public:
237
237
HeapSnapshotStream (
238
238
Environment* env,
239
- const HeapSnapshot* snapshot,
239
+ HeapSnapshotPointer&& snapshot,
240
240
v8::Local<v8::Object> obj) :
241
241
AsyncWrap (env, obj, AsyncWrap::PROVIDER_HEAPSNAPSHOT),
242
242
StreamBase (env),
243
- snapshot_ (snapshot) {
243
+ snapshot_ (std::move( snapshot) ) {
244
244
MakeWeak ();
245
245
StreamBase::AttachToObject (GetObject ());
246
246
}
247
247
248
- ~HeapSnapshotStream () override {
249
- Cleanup ();
250
- }
248
+ ~HeapSnapshotStream () override {}
251
249
252
250
int GetChunkSize () override {
253
251
return 65536 ; // big chunks == faster
254
252
}
255
253
256
254
void EndOfStream () override {
257
255
EmitRead (UV_EOF);
258
- Cleanup ();
256
+ snapshot_. reset ();
259
257
}
260
258
261
259
WriteResult WriteAsciiChunk (char * data, int size) override {
@@ -309,22 +307,13 @@ class HeapSnapshotStream : public AsyncWrap,
309
307
SET_SELF_SIZE (HeapSnapshotStream)
310
308
311
309
private:
312
- void Cleanup () {
313
- if (snapshot_ != nullptr ) {
314
- const_cast <HeapSnapshot*>(snapshot_)->Delete ();
315
- snapshot_ = nullptr ;
316
- }
317
- }
318
-
319
-
320
- const HeapSnapshot* snapshot_;
310
+ HeapSnapshotPointer snapshot_;
321
311
};
322
312
323
313
inline void TakeSnapshot (Isolate* isolate, v8::OutputStream* out) {
324
- const HeapSnapshot* const snapshot =
325
- isolate->GetHeapProfiler ()->TakeHeapSnapshot ();
314
+ HeapSnapshotPointer snapshot {
315
+ isolate->GetHeapProfiler ()->TakeHeapSnapshot () } ;
326
316
snapshot->Serialize (out, HeapSnapshot::kJSON );
327
- const_cast <HeapSnapshot*>(snapshot)->Delete ();
328
317
}
329
318
330
319
inline bool WriteSnapshot (Isolate* isolate, const char * filename) {
@@ -339,20 +328,44 @@ inline bool WriteSnapshot(Isolate* isolate, const char* filename) {
339
328
340
329
} // namespace
341
330
342
- void CreateHeapSnapshotStream (const FunctionCallbackInfo<Value>& args) {
343
- Environment* env = Environment::GetCurrent (args);
331
+ void DeleteHeapSnapshot (const v8::HeapSnapshot* snapshot) {
332
+ const_cast <HeapSnapshot*>(snapshot)->Delete ();
333
+ }
334
+
335
+ BaseObjectPtr<AsyncWrap> CreateHeapSnapshotStream (
336
+ Environment* env, HeapSnapshotPointer&& snapshot) {
344
337
HandleScope scope (env->isolate ());
345
- const HeapSnapshot* const snapshot =
346
- env->isolate ()->GetHeapProfiler ()->TakeHeapSnapshot ();
347
- CHECK_NOT_NULL (snapshot);
338
+
339
+ if (env->streambaseoutputstream_constructor_template ().IsEmpty ()) {
340
+ // Create FunctionTemplate for HeapSnapshotStream
341
+ Local<FunctionTemplate> os = FunctionTemplate::New (env->isolate ());
342
+ os->Inherit (AsyncWrap::GetConstructorTemplate (env));
343
+ Local<ObjectTemplate> ost = os->InstanceTemplate ();
344
+ ost->SetInternalFieldCount (StreamBase::kStreamBaseFieldCount );
345
+ os->SetClassName (
346
+ FIXED_ONE_BYTE_STRING (env->isolate (), " HeapSnapshotStream" ));
347
+ StreamBase::AddMethods (env, os);
348
+ env->set_streambaseoutputstream_constructor_template (ost);
349
+ }
350
+
348
351
Local<Object> obj;
349
352
if (!env->streambaseoutputstream_constructor_template ()
350
353
->NewInstance (env->context ())
351
354
.ToLocal (&obj)) {
352
- return ;
355
+ return {} ;
353
356
}
354
- HeapSnapshotStream* out = new HeapSnapshotStream (env, snapshot, obj);
355
- args.GetReturnValue ().Set (out->object ());
357
+ return MakeBaseObject<HeapSnapshotStream>(env, std::move (snapshot), obj);
358
+ }
359
+
360
+ void CreateHeapSnapshotStream (const FunctionCallbackInfo<Value>& args) {
361
+ Environment* env = Environment::GetCurrent (args);
362
+ HeapSnapshotPointer snapshot {
363
+ env->isolate ()->GetHeapProfiler ()->TakeHeapSnapshot () };
364
+ CHECK (snapshot);
365
+ BaseObjectPtr<AsyncWrap> stream =
366
+ CreateHeapSnapshotStream (env, std::move (snapshot));
367
+ if (stream)
368
+ args.GetReturnValue ().Set (stream->object ());
356
369
}
357
370
358
371
void TriggerHeapSnapshot (const FunctionCallbackInfo<Value>& args) {
@@ -388,15 +401,6 @@ void Initialize(Local<Object> target,
388
401
env->SetMethod (target, " buildEmbedderGraph" , BuildEmbedderGraph);
389
402
env->SetMethod (target, " triggerHeapSnapshot" , TriggerHeapSnapshot);
390
403
env->SetMethod (target, " createHeapSnapshotStream" , CreateHeapSnapshotStream);
391
-
392
- // Create FunctionTemplate for HeapSnapshotStream
393
- Local<FunctionTemplate> os = FunctionTemplate::New (env->isolate ());
394
- os->Inherit (AsyncWrap::GetConstructorTemplate (env));
395
- Local<ObjectTemplate> ost = os->InstanceTemplate ();
396
- ost->SetInternalFieldCount (StreamBase::kStreamBaseFieldCount );
397
- os->SetClassName (FIXED_ONE_BYTE_STRING (env->isolate (), " HeapSnapshotStream" ));
398
- StreamBase::AddMethods (env, os);
399
- env->set_streambaseoutputstream_constructor_template (ost);
400
404
}
401
405
402
406
} // namespace heap
0 commit comments