From 52de4cb107a6fc1a06f7c98f4fd36c7f7fd539d5 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 12 Jun 2020 00:23:16 +0200 Subject: [PATCH] src: minor updates to FastHrtime MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Don’t use 12 as a magic number for the buffer size - Mark the object as weak (which is conceptually the right thing to do, even if there is little practical impact) - Keep a reference to the `ArrayBuffer` in question for memory tracking PR-URL: https://github.com/nodejs/node/pull/33851 Reviewed-By: Gus Caplan Reviewed-By: Colin Ihrig Reviewed-By: David Carlier Reviewed-By: James M Snell --- src/node_process_methods.cc | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 800ce0313647d4..88d5f843f3072f 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -1,6 +1,7 @@ #include "base_object-inl.h" #include "debug_utils-inl.h" #include "env-inl.h" +#include "memory_tracker-inl.h" #include "node.h" #include "node_errors.h" #include "node_internals.h" @@ -451,8 +452,10 @@ class FastHrtime : public BaseObject { Local obj = otmpl->NewInstance(env->context()).ToLocalChecked(); - Local ab = ArrayBuffer::New(env->isolate(), 12); - new FastHrtime(env, obj, ab->GetBackingStore()); + Local ab = + ArrayBuffer::New(env->isolate(), + std::max(sizeof(uint64_t), sizeof(uint32_t) * 3)); + new FastHrtime(env, obj, ab); obj->Set( env->context(), FIXED_ONE_BYTE_STRING(env->isolate(), "buffer"), ab) .ToChecked(); @@ -463,11 +466,16 @@ class FastHrtime : public BaseObject { private: FastHrtime(Environment* env, Local object, - std::shared_ptr backing_store) - : BaseObject(env, object), backing_store_(backing_store) {} - - void MemoryInfo(MemoryTracker* tracker) const override {} + Local ab) + : BaseObject(env, object), + array_buffer_(env->isolate(), ab), + backing_store_(ab->GetBackingStore()) { + MakeWeak(); + } + void MemoryInfo(MemoryTracker* tracker) const override { + tracker->TrackField("array_buffer", array_buffer_); + } SET_MEMORY_INFO_NAME(FastHrtime) SET_SELF_SIZE(FastHrtime) @@ -502,6 +510,7 @@ class FastHrtime : public BaseObject { FastBigInt(FromJSObject(args.Holder())); } + v8::Global array_buffer_; std::shared_ptr backing_store_; };