Skip to content

Commit 81203f6

Browse files
committed
fixup! src: implement MemoryRetainer in Environment
1 parent 76c6bdf commit 81203f6

File tree

5 files changed

+60
-46
lines changed

5 files changed

+60
-46
lines changed

src/base_object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class BaseObject : public MemoryRetainer {
9494
// position of members in memory are predictable. For more information please
9595
// refer to `doc/guides/node-postmortem-support.md`
9696
friend int GenDebugSymbols();
97-
friend class Environment;
97+
friend class CleanupHookCallback;
9898

9999
Persistent<v8::Object> persistent_handle_;
100100
Environment* env_;

src/env-inl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -976,17 +976,17 @@ void Environment::RemoveCleanupHook(void (*fn)(void*), void* arg) {
976976
cleanup_hooks_.erase(search);
977977
}
978978

979-
size_t Environment::CleanupHookCallback::Hash::operator()(
979+
size_t CleanupHookCallback::Hash::operator()(
980980
const CleanupHookCallback& cb) const {
981981
return std::hash<void*>()(cb.arg_);
982982
}
983983

984-
bool Environment::CleanupHookCallback::Equal::operator()(
984+
bool CleanupHookCallback::Equal::operator()(
985985
const CleanupHookCallback& a, const CleanupHookCallback& b) const {
986986
return a.fn_ == b.fn_ && a.arg_ == b.arg_;
987987
}
988988

989-
BaseObject* Environment::CleanupHookCallback::GetBaseObject() const {
989+
BaseObject* CleanupHookCallback::GetBaseObject() const {
990990
if (fn_ == BaseObject::DeleteMe)
991991
return static_cast<BaseObject*>(arg_);
992992
else

src/env.cc

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,12 @@ void IsolateData::MemoryInfo(MemoryTracker* tracker) const {
123123
PER_ISOLATE_STRING_PROPERTIES(V)
124124
#undef V
125125

126-
tracker->TrackFieldWithSize(
127-
"allocator", sizeof(*allocator_), "v8::ArrayBuffer::Allocator");
128126
if (node_allocator_ != nullptr) {
129127
tracker->TrackFieldWithSize(
130128
"node_allocator", sizeof(*node_allocator_), "NodeArrayBufferAllocator");
129+
} else {
130+
tracker->TrackFieldWithSize(
131+
"allocator", sizeof(*allocator_), "v8::ArrayBuffer::Allocator");
131132
}
132133
tracker->TrackFieldWithSize(
133134
"platform", sizeof(*platform_), "MultiIsolatePlatform");
@@ -852,15 +853,25 @@ void Environment::stop_sub_worker_contexts() {
852853
}
853854
}
854855

855-
void Environment::CleanupHookCallback::MemoryInfo(
856-
MemoryTracker* tracker) const {
856+
void MemoryTracker::TrackField(const char* edge_name,
857+
const CleanupHookCallback& value,
858+
const char* node_name) {
859+
v8::HandleScope handle_scope(isolate_);
860+
// Here, we utilize the fact that CleanupHookCallback instances
861+
// are all unique and won't be tracked twice in one BuildEmbedderGraph
862+
// callback.
863+
MemoryRetainerNode* n =
864+
PushNode("CleanupHookCallback", sizeof(value), edge_name);
857865
// TODO(joyeecheung): at the moment only arguments of type BaseObject will be
858866
// identified and tracked here (based on their deleters),
859867
// but we may convert and track other known types here.
860-
BaseObject* obj = GetBaseObject();
868+
BaseObject* obj = value.GetBaseObject();
861869
if (obj != nullptr) {
862-
tracker->TrackField("arg", obj);
870+
this->TrackField("arg", obj);
863871
}
872+
CHECK_EQ(CurrentNode(), n);
873+
CHECK_NE(n->size_, 0);
874+
PopNode();
864875
}
865876

866877
void Environment::BuildEmbedderGraph(Isolate* isolate,

src/env.h

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,36 @@ class ShouldNotAbortOnUncaughtScope {
739739
Environment* env_;
740740
};
741741

742+
class CleanupHookCallback {
743+
public:
744+
CleanupHookCallback(void (*fn)(void*),
745+
void* arg,
746+
uint64_t insertion_order_counter)
747+
: fn_(fn), arg_(arg), insertion_order_counter_(insertion_order_counter) {}
748+
749+
// Only hashes `arg_`, since that is usually enough to identify the hook.
750+
struct Hash {
751+
inline size_t operator()(const CleanupHookCallback& cb) const;
752+
};
753+
754+
// Compares by `fn_` and `arg_` being equal.
755+
struct Equal {
756+
inline bool operator()(const CleanupHookCallback& a,
757+
const CleanupHookCallback& b) const;
758+
};
759+
760+
inline BaseObject* GetBaseObject() const;
761+
762+
private:
763+
friend class Environment;
764+
void (*fn_)(void*);
765+
void* arg_;
766+
767+
// We keep track of the insertion order for these objects, so that we can
768+
// call the callbacks in reverse order when we are cleaning up.
769+
uint64_t insertion_order_counter_;
770+
};
771+
742772
class Environment : public MemoryRetainer {
743773
public:
744774
Environment(const Environment&) = delete;
@@ -1224,42 +1254,6 @@ class Environment : public MemoryRetainer {
12241254
void RunAndClearNativeImmediates();
12251255
static void CheckImmediate(uv_check_t* handle);
12261256

1227-
class CleanupHookCallback : public MemoryRetainer {
1228-
public:
1229-
CleanupHookCallback(void (*fn)(void*),
1230-
void* arg,
1231-
uint64_t insertion_order_counter)
1232-
: fn_(fn),
1233-
arg_(arg),
1234-
insertion_order_counter_(insertion_order_counter) {}
1235-
1236-
// Only hashes `arg_`, since that is usually enough to identify the hook.
1237-
struct Hash {
1238-
inline size_t operator()(const CleanupHookCallback& cb) const;
1239-
};
1240-
1241-
// Compares by `fn_` and `arg_` being equal.
1242-
struct Equal {
1243-
inline bool operator()(const CleanupHookCallback& a,
1244-
const CleanupHookCallback& b) const;
1245-
};
1246-
1247-
SET_MEMORY_INFO_NAME(CleanupHookCallback);
1248-
SET_SELF_SIZE(CleanupHookCallback);
1249-
void MemoryInfo(MemoryTracker* tracker) const override;
1250-
1251-
inline BaseObject* GetBaseObject() const;
1252-
1253-
private:
1254-
friend class Environment;
1255-
void (*fn_)(void*);
1256-
void* arg_;
1257-
1258-
// We keep track of the insertion order for these objects, so that we can
1259-
// call the callbacks in reverse order when we are cleaning up.
1260-
uint64_t insertion_order_counter_;
1261-
};
1262-
12631257
// Use an unordered_set, so that we have efficient insertion and removal.
12641258
std::unordered_set<CleanupHookCallback,
12651259
CleanupHookCallback::Hash,

src/memory_tracker.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ namespace crypto {
3535
class NodeBIO;
3636
}
3737

38+
class CleanupHookCallback;
39+
3840
/* Example:
3941
*
4042
* class ExampleRetainer : public MemoryRetainer {
@@ -195,6 +197,13 @@ class MemoryTracker {
195197
inline void TrackField(const char* edge_name,
196198
const MallocedBuffer<T>& value,
197199
const char* node_name = nullptr);
200+
// We do not implement CleanupHookCallback as MemoryRetainer
201+
// but instead specialize the method here to avoid the cost of
202+
// virtual pointers.
203+
// TODO(joyeecheung): do this for BaseObject and remove WrappedObject()
204+
void TrackField(const char* edge_name,
205+
const CleanupHookCallback& value,
206+
const char* node_name = nullptr);
198207
inline void TrackField(const char* edge_name,
199208
const uv_buf_t& value,
200209
const char* node_name = nullptr);

0 commit comments

Comments
 (0)