Skip to content

Commit 5df0c78

Browse files
src: add Cleanable class to Environment
We store a linked list of `Cleanable` objects on the `node::Environment` and invoke their `Clean()` method during env teardown. `Cleanable`s can be added/removed using `Environment::AddCleanable()` resp. `Environment::RemoveCleanable()`. This eliminates the need for adding many cleanup hooks.
1 parent 5de919b commit 5df0c78

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

src/env-inl.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,31 @@ inline void Environment::PopAsyncCallbackScope() {
148148
async_callback_scope_depth_--;
149149
}
150150

151+
inline void Environment::AddCleanable(Cleanable* new_cleanable) {
152+
CHECK_NOT_NULL(new_cleanable);
153+
new_cleanable->next_ = cleanables_;
154+
new_cleanable->prev_ = nullptr;
155+
if (cleanables_ != nullptr) {
156+
cleanables_->prev_ = new_cleanable;
157+
}
158+
cleanables_ = new_cleanable;
159+
}
160+
161+
inline void Environment::RemoveCleanable(Cleanable* old_cleanable) {
162+
CHECK_NOT_NULL(old_cleanable);
163+
if (old_cleanable->prev_ != nullptr) {
164+
old_cleanable->prev_->next_ = old_cleanable->next_;
165+
}
166+
if (old_cleanable->next_ != nullptr) {
167+
old_cleanable->next_->prev_ = old_cleanable->prev_;
168+
}
169+
if (cleanables_ == old_cleanable) {
170+
cleanables_ = old_cleanable->next_;
171+
}
172+
old_cleanable->next_ = nullptr;
173+
old_cleanable->prev_ = nullptr;
174+
}
175+
151176
inline AliasedUint32Array& ImmediateInfo::fields() {
152177
return fields_;
153178
}

src/env.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ Environment::Environment(IsolateData* isolate_data,
804804
timeout_info_(isolate_, 1, MAYBE_FIELD_PTR(env_info, timeout_info)),
805805
tick_info_(isolate, MAYBE_FIELD_PTR(env_info, tick_info)),
806806
timer_base_(uv_now(isolate_data->event_loop())),
807+
cleanables_(nullptr),
807808
exec_argv_(exec_args),
808809
argv_(args),
809810
exec_path_(Environment::GetExecPath(args)),
@@ -1279,6 +1280,12 @@ void Environment::RunCleanup() {
12791280
// Defer the BaseObject cleanup after handles are cleaned up.
12801281
CleanupHandles();
12811282

1283+
while (cleanables_ != nullptr) {
1284+
Cleanable* cleanable = cleanables_;
1285+
RemoveCleanable(cleanable);
1286+
cleanable->Clean();
1287+
}
1288+
12821289
while (!cleanup_queue_.empty() || principal_realm_->HasCleanupHooks() ||
12831290
native_immediates_.size() > 0 ||
12841291
native_immediates_threadsafe_.size() > 0 ||

src/env.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,15 @@ void DefaultProcessExitHandlerInternal(Environment* env, ExitCode exit_code);
594594
v8::Maybe<ExitCode> SpinEventLoopInternal(Environment* env);
595595
v8::Maybe<ExitCode> EmitProcessExitInternal(Environment* env);
596596

597+
class Cleanable {
598+
friend class Environment;
599+
public:
600+
virtual void Clean() = 0;
601+
private:
602+
Cleanable* next_ = nullptr;
603+
Cleanable* prev_ = nullptr;
604+
};
605+
597606
/**
598607
* Environment is a per-isolate data structure that represents an execution
599608
* environment. Each environment has a principal realm. An environment can
@@ -611,6 +620,9 @@ class Environment final : public MemoryRetainer {
611620
static std::string GetExecPath(const std::vector<std::string>& argv);
612621
static std::string GetCwd(const std::string& exec_path);
613622

623+
void AddCleanable(Cleanable* new_cleanable);
624+
void RemoveCleanable(Cleanable* old_cleanable);
625+
614626
inline size_t SelfSize() const override;
615627
bool IsRootNode() const override { return true; }
616628
void MemoryInfo(MemoryTracker* tracker) const override;
@@ -1080,6 +1092,7 @@ class Environment final : public MemoryRetainer {
10801092
void TrackContext(v8::Local<v8::Context> context);
10811093
void UntrackContext(v8::Local<v8::Context> context);
10821094

1095+
Cleanable* cleanables_;
10831096
std::list<binding::DLib> loaded_addons_;
10841097
v8::Isolate* const isolate_;
10851098
IsolateData* const isolate_data_;

src/node_buffer.cc

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ using v8::Value;
8484

8585
namespace {
8686

87-
class CallbackInfo {
87+
class CallbackInfo: public Cleanable {
8888
public:
8989
static inline Local<ArrayBuffer> CreateTrackedArrayBuffer(
9090
Environment* env,
@@ -95,9 +95,9 @@ class CallbackInfo {
9595

9696
CallbackInfo(const CallbackInfo&) = delete;
9797
CallbackInfo& operator=(const CallbackInfo&) = delete;
98+
void Clean();
9899

99100
private:
100-
static void CleanupHook(void* data);
101101
inline void OnBackingStoreFree();
102102
inline void CallAndResetCallback();
103103
inline CallbackInfo(Environment* env,
@@ -152,25 +152,23 @@ CallbackInfo::CallbackInfo(Environment* env,
152152
data_(data),
153153
hint_(hint),
154154
env_(env) {
155-
env->AddCleanupHook(CleanupHook, this);
155+
env->AddCleanable(this);
156156
env->isolate()->AdjustAmountOfExternalAllocatedMemory(sizeof(*this));
157157
}
158158

159-
void CallbackInfo::CleanupHook(void* data) {
160-
CallbackInfo* self = static_cast<CallbackInfo*>(data);
161-
159+
void CallbackInfo::Clean() {
162160
{
163-
HandleScope handle_scope(self->env_->isolate());
164-
Local<ArrayBuffer> ab = self->persistent_.Get(self->env_->isolate());
161+
HandleScope handle_scope(env_->isolate());
162+
Local<ArrayBuffer> ab = persistent_.Get(env_->isolate());
165163
if (!ab.IsEmpty() && ab->IsDetachable()) {
166164
ab->Detach(Local<Value>()).Check();
167-
self->persistent_.Reset();
165+
persistent_.Reset();
168166
}
169167
}
170168

171169
// Call the callback in this case, but don't delete `this` yet because the
172170
// BackingStore deleter callback will do so later.
173-
self->CallAndResetCallback();
171+
CallAndResetCallback();
174172
}
175173

176174
void CallbackInfo::CallAndResetCallback() {
@@ -182,7 +180,7 @@ void CallbackInfo::CallAndResetCallback() {
182180
}
183181
if (callback != nullptr) {
184182
// Clean up all Environment-related state and run the callback.
185-
env_->RemoveCleanupHook(CleanupHook, this);
183+
env_->RemoveCleanable(this);
186184
int64_t change_in_bytes = -static_cast<int64_t>(sizeof(*this));
187185
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
188186

0 commit comments

Comments
 (0)