Skip to content

Commit 5c313f8

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 5c313f8

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,17 @@ 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+
600+
public:
601+
virtual void Clean() = 0;
602+
603+
private:
604+
Cleanable* next_ = nullptr;
605+
Cleanable* prev_ = nullptr;
606+
};
607+
597608
/**
598609
* Environment is a per-isolate data structure that represents an execution
599610
* environment. Each environment has a principal realm. An environment can
@@ -611,6 +622,9 @@ class Environment final : public MemoryRetainer {
611622
static std::string GetExecPath(const std::vector<std::string>& argv);
612623
static std::string GetCwd(const std::string& exec_path);
613624

625+
void AddCleanable(Cleanable* new_cleanable);
626+
void RemoveCleanable(Cleanable* old_cleanable);
627+
614628
inline size_t SelfSize() const override;
615629
bool IsRootNode() const override { return true; }
616630
void MemoryInfo(MemoryTracker* tracker) const override;
@@ -1080,6 +1094,7 @@ class Environment final : public MemoryRetainer {
10801094
void TrackContext(v8::Local<v8::Context> context);
10811095
void UntrackContext(v8::Local<v8::Context> context);
10821096

1097+
Cleanable* cleanables_;
10831098
std::list<binding::DLib> loaded_addons_;
10841099
v8::Isolate* const isolate_;
10851100
IsolateData* const isolate_data_;

src/node_buffer.cc

Lines changed: 9 additions & 12 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,
@@ -112,7 +112,6 @@ class CallbackInfo {
112112
Environment* const env_;
113113
};
114114

115-
116115
Local<ArrayBuffer> CallbackInfo::CreateTrackedArrayBuffer(
117116
Environment* env,
118117
char* data,
@@ -152,25 +151,23 @@ CallbackInfo::CallbackInfo(Environment* env,
152151
data_(data),
153152
hint_(hint),
154153
env_(env) {
155-
env->AddCleanupHook(CleanupHook, this);
154+
env->AddCleanable(this);
156155
env->isolate()->AdjustAmountOfExternalAllocatedMemory(sizeof(*this));
157156
}
158157

159-
void CallbackInfo::CleanupHook(void* data) {
160-
CallbackInfo* self = static_cast<CallbackInfo*>(data);
161-
158+
void CallbackInfo::Clean() {
162159
{
163-
HandleScope handle_scope(self->env_->isolate());
164-
Local<ArrayBuffer> ab = self->persistent_.Get(self->env_->isolate());
160+
HandleScope handle_scope(env_->isolate());
161+
Local<ArrayBuffer> ab = persistent_.Get(env_->isolate());
165162
if (!ab.IsEmpty() && ab->IsDetachable()) {
166163
ab->Detach(Local<Value>()).Check();
167-
self->persistent_.Reset();
164+
persistent_.Reset();
168165
}
169166
}
170167

171168
// Call the callback in this case, but don't delete `this` yet because the
172169
// BackingStore deleter callback will do so later.
173-
self->CallAndResetCallback();
170+
CallAndResetCallback();
174171
}
175172

176173
void CallbackInfo::CallAndResetCallback() {
@@ -182,7 +179,7 @@ void CallbackInfo::CallAndResetCallback() {
182179
}
183180
if (callback != nullptr) {
184181
// Clean up all Environment-related state and run the callback.
185-
env_->RemoveCleanupHook(CleanupHook, this);
182+
env_->RemoveCleanable(this);
186183
int64_t change_in_bytes = -static_cast<int64_t>(sizeof(*this));
187184
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
188185

0 commit comments

Comments
 (0)