Skip to content

Commit f3d6207

Browse files
committed
doc: fix the example implementation of MemoryRetainer
We need to be careful not to include the size of non-pointer fields in the parent's self size if we want to track them separately as a different node. Refs: https://github.com/nodejs/node/pull/26161/files#r259170771 PR-URL: #26262 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 617f055 commit f3d6207

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/memory_tracker.h

+15-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ class NodeBIO;
4444
* // Node name and size comes from the MemoryInfoName and SelfSize of
4545
* // AnotherRetainerClass
4646
* tracker->TrackField("another_retainer", another_retainer_);
47+
*
48+
* // Add non_pointer_retainer as a separate node into the graph
49+
* // and track its memory information recursively.
50+
* // Note that we need to make sure its size is not accounted in
51+
* // ExampleRetainer::SelfSize().
52+
* tracker->TrackField("non_pointer_retainer", &non_pointer_retainer_);
53+
*
4754
* // Specify node name and size explicitly
4855
* tracker->TrackFieldWithSize("internal_member",
4956
* internal_member_.size(),
@@ -60,9 +67,12 @@ class NodeBIO;
6067
* return "ExampleRetainer";
6168
* }
6269
*
63-
* // Or use SET_SELF_SIZE(ExampleRetainer)
70+
* // Classes that only want to return its sizeof() value can use the
71+
* // SET_SELF_SIZE(Class) macro instead.
6472
* size_t SelfSize() const override {
65-
* return sizeof(ExampleRetainer);
73+
* // We need to exclude the size of non_pointer_retainer so that
74+
* // we can track it separately in ExampleRetainer::MemoryInfo().
75+
* return sizeof(ExampleRetainer) - sizeof(NonPointerRetainerClass);
6676
* }
6777
*
6878
* // Note: no need to implement these two methods when implementing
@@ -71,8 +81,10 @@ class NodeBIO;
7181
* v8::Local<v8::Object> WrappedObject() const override {
7282
* return node::PersistentToLocal::Default(wrapped_);
7383
* }
84+
*
7485
* private:
75-
* AnotherRetainerClass another_retainer_;
86+
* AnotherRetainerClass* another_retainer_;
87+
* NonPointerRetainerClass non_pointer_retainer;
7688
* InternalClass internal_member_;
7789
* std::vector<uv_async_t> vector_;
7890
* node::Persistent<Object> target_;

0 commit comments

Comments
 (0)