Skip to content

Commit f1ea367

Browse files
danbevMylesBorins
authored andcommitted
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 202f007 commit f1ea367

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

src/node.cc

+6-15
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262

6363
#include <string>
6464
#include <vector>
65+
#include <list>
6566

6667
#if defined(NODE_HAVE_I18N_SUPPORT)
6768
#include <unicode/uvernum.h>
@@ -4411,34 +4412,24 @@ void Init(int* argc,
44114412

44124413

44134414
struct AtExitCallback {
4414-
AtExitCallback* next_;
44154415
void (*cb_)(void* arg);
44164416
void* arg_;
44174417
};
44184418

4419-
static AtExitCallback* at_exit_functions_;
4419+
static std::list<AtExitCallback> at_exit_functions;
44204420

44214421

44224422
// TODO(bnoordhuis) Turn into per-context event.
44234423
void RunAtExit(Environment* env) {
4424-
AtExitCallback* p = at_exit_functions_;
4425-
at_exit_functions_ = nullptr;
4426-
4427-
while (p) {
4428-
AtExitCallback* q = p->next_;
4429-
p->cb_(p->arg_);
4430-
delete p;
4431-
p = q;
4424+
for (AtExitCallback at_exit : at_exit_functions) {
4425+
at_exit.cb_(at_exit.arg_);
44324426
}
4427+
at_exit_functions.clear();
44334428
}
44344429

44354430

44364431
void AtExit(void (*cb)(void* arg), void* arg) {
4437-
AtExitCallback* p = new AtExitCallback;
4438-
p->cb_ = cb;
4439-
p->arg_ = arg;
4440-
p->next_ = at_exit_functions_;
4441-
at_exit_functions_ = p;
4432+
at_exit_functions.push_back(AtExitCallback{cb, arg});
44424433
}
44434434

44444435

0 commit comments

Comments
 (0)