Skip to content

Commit 988519d

Browse files
committed
src: track cppgc wrappers with CppgcWrapperQueue
1 parent 7c3aa9f commit 988519d

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/cppgc_helpers.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ namespace node {
3838
* }
3939
* }
4040
*/
41-
class CppgcMixin : public cppgc::GarbageCollectedMixin {
41+
class CppgcMixin : public cppgc::GarbageCollectedMixin,
42+
public CppgcWrapperCleanup {
4243
public:
4344
// To help various callbacks access wrapper objects with different memory
4445
// management, cppgc-managed objects share the same layout as BaseObjects.
@@ -58,6 +59,7 @@ class CppgcMixin : public cppgc::GarbageCollectedMixin {
5859
obj->SetAlignedPointerInInternalField(
5960
kEmbedderType, env->isolate_data()->embedder_id_for_cppgc());
6061
obj->SetAlignedPointerInInternalField(kSlot, ptr);
62+
env->cppgc_wrapper_queue()->PushFront(ptr);
6163
}
6264

6365
v8::Local<v8::Object> object() const {
@@ -88,8 +90,21 @@ class CppgcMixin : public cppgc::GarbageCollectedMixin {
8890
visitor->Trace(traced_reference_);
8991
}
9092

93+
virtual void CleanEnvResource(Environment* env) {}
94+
95+
// Run during Environment shutdown.
96+
void Clean() override {
97+
if (env_ == nullptr) {
98+
return;
99+
}
100+
101+
this->CleanEnvResource(env_);
102+
103+
env_ = nullptr;
104+
}
105+
91106
private:
92-
Environment* env_;
107+
Environment* env_ = nullptr;
93108
v8::TracedReference<v8::Object> traced_reference_;
94109
};
95110

src/env.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,8 @@ void Environment::RunCleanup() {
12901290
CleanupHandles();
12911291
}
12921292

1293+
for (CppgcWrapperCleanup* handle : cppgc_wrapper_queue_) handle->Clean();
1294+
12931295
for (const int fd : unmanaged_fds_) {
12941296
uv_fs_t close_req;
12951297
uv_fs_close(nullptr, &close_req, fd, nullptr);

src/env.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,12 @@ class Cleanable {
615615
friend class Environment;
616616
};
617617

618+
class CppgcWrapperCleanup {
619+
public:
620+
virtual void Clean() = 0;
621+
ListNode<CppgcWrapperCleanup> wrapper_queue;
622+
};
623+
618624
/**
619625
* Environment is a per-isolate data structure that represents an execution
620626
* environment. Each environment has a principal realm. An environment can
@@ -703,6 +709,7 @@ class Environment final : public MemoryRetainer {
703709
Realm* realm,
704710
const ContextInfo& info);
705711
void UnassignFromContext(v8::Local<v8::Context> context);
712+
void UntrackContext(v8::Local<v8::Context> context);
706713
void TrackShadowRealm(shadow_realm::ShadowRealm* realm);
707714
void UntrackShadowRealm(shadow_realm::ShadowRealm* realm);
708715

@@ -910,12 +917,17 @@ class Environment final : public MemoryRetainer {
910917
typedef ListHead<HandleWrap, &HandleWrap::handle_wrap_queue_> HandleWrapQueue;
911918
typedef ListHead<ReqWrapBase, &ReqWrapBase::req_wrap_queue_> ReqWrapQueue;
912919
typedef ListHead<Cleanable, &Cleanable::cleanable_queue_> CleanableQueue;
920+
typedef ListHead<CppgcWrapperCleanup, &CppgcWrapperCleanup::wrapper_queue>
921+
CppgcWrapperQueue;
913922

914923
inline HandleWrapQueue* handle_wrap_queue() { return &handle_wrap_queue_; }
915924
inline CleanableQueue* cleanable_queue() {
916925
return &cleanable_queue_;
917926
}
918927
inline ReqWrapQueue* req_wrap_queue() { return &req_wrap_queue_; }
928+
inline CppgcWrapperQueue* cppgc_wrapper_queue() {
929+
return &cppgc_wrapper_queue_;
930+
}
919931

920932
// https://w3c.github.io/hr-time/#dfn-time-origin
921933
inline uint64_t time_origin() {
@@ -1084,7 +1096,6 @@ class Environment final : public MemoryRetainer {
10841096
v8::Local<v8::Value>),
10851097
const char* errmsg);
10861098
void TrackContext(v8::Local<v8::Context> context);
1087-
void UntrackContext(v8::Local<v8::Context> context);
10881099

10891100
std::list<binding::DLib> loaded_addons_;
10901101
v8::Isolate* const isolate_;
@@ -1203,6 +1214,8 @@ class Environment final : public MemoryRetainer {
12031214
CleanableQueue cleanable_queue_;
12041215
HandleWrapQueue handle_wrap_queue_;
12051216
ReqWrapQueue req_wrap_queue_;
1217+
CppgcWrapperQueue cppgc_wrapper_queue_;
1218+
12061219
int handle_cleanup_waiting_ = 0;
12071220
int request_waiting_ = 0;
12081221

0 commit comments

Comments
 (0)