Skip to content

Commit fe016c6

Browse files
committed
src: use std::list for at_exit_functions
This change was suggested by bnoordhuis in the following comment: #9163 (comment) Not including any tests as this is covered by test/addons/at-exit. PR-URL: #12255 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 65a6e05 commit fe016c6

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

src/node.cc

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484

8585
#include <string>
8686
#include <vector>
87+
#include <list>
8788

8889
#if defined(NODE_HAVE_I18N_SUPPORT)
8990
#include <unicode/uvernum.h>
@@ -4267,34 +4268,24 @@ void Init(int* argc,
42674268

42684269

42694270
struct AtExitCallback {
4270-
AtExitCallback* next_;
42714271
void (*cb_)(void* arg);
42724272
void* arg_;
42734273
};
42744274

4275-
static AtExitCallback* at_exit_functions_;
4275+
static std::list<AtExitCallback> at_exit_functions;
42764276

42774277

42784278
// TODO(bnoordhuis) Turn into per-context event.
42794279
void RunAtExit(Environment* env) {
4280-
AtExitCallback* p = at_exit_functions_;
4281-
at_exit_functions_ = nullptr;
4282-
4283-
while (p) {
4284-
AtExitCallback* q = p->next_;
4285-
p->cb_(p->arg_);
4286-
delete p;
4287-
p = q;
4280+
for (AtExitCallback at_exit : at_exit_functions) {
4281+
at_exit.cb_(at_exit.arg_);
42884282
}
4283+
at_exit_functions.clear();
42894284
}
42904285

42914286

42924287
void AtExit(void (*cb)(void* arg), void* arg) {
4293-
AtExitCallback* p = new AtExitCallback;
4294-
p->cb_ = cb;
4295-
p->arg_ = arg;
4296-
p->next_ = at_exit_functions_;
4297-
at_exit_functions_ = p;
4288+
at_exit_functions.push_back(AtExitCallback{cb, arg});
42984289
}
42994290

43004291

0 commit comments

Comments
 (0)