From 37568c093a742d0c92f6caa3764e6b801f3b5912 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Thu, 6 Apr 2017 08:51:18 +0200 Subject: [PATCH] src: use std::list for at_exit_functions This change was suggested by bnoordhuis in the following comment: https://github.com/nodejs/node/pull/9163#discussion_r84264628 Not including any tests as this is covered by test/addons/at-exit. PR-URL: https://github.com/nodejs/node/pull/12255 Reviewed-By: Ben Noordhuis Reviewed-By: Richard Lau Reviewed-By: Colin Ihrig Reviewed-By: Anna Henningsen --- src/node.cc | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/node.cc b/src/node.cc index a7b4a789cc3874..7a7f2451d8b45c 100644 --- a/src/node.cc +++ b/src/node.cc @@ -63,6 +63,7 @@ #include #include +#include #if defined(NODE_HAVE_I18N_SUPPORT) #include @@ -4290,34 +4291,24 @@ void Init(int* argc, struct AtExitCallback { - AtExitCallback* next_; void (*cb_)(void* arg); void* arg_; }; -static AtExitCallback* at_exit_functions_; +static std::list at_exit_functions; // TODO(bnoordhuis) Turn into per-context event. void RunAtExit(Environment* env) { - AtExitCallback* p = at_exit_functions_; - at_exit_functions_ = nullptr; - - while (p) { - AtExitCallback* q = p->next_; - p->cb_(p->arg_); - delete p; - p = q; + for (AtExitCallback at_exit : at_exit_functions) { + at_exit.cb_(at_exit.arg_); } + at_exit_functions.clear(); } void AtExit(void (*cb)(void* arg), void* arg) { - AtExitCallback* p = new AtExitCallback; - p->cb_ = cb; - p->arg_ = arg; - p->next_ = at_exit_functions_; - at_exit_functions_ = p; + at_exit_functions.push_back(AtExitCallback{cb, arg}); }