GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: cleanup_queue.cc Lines: 15 15 100.0 %
Date: 2022-09-22 04:22:24 Branches: 7 8 87.5 %

Line Branch Exec Source
1
#include "cleanup_queue.h"  // NOLINT(build/include_inline)
2
#include <vector>
3
#include "cleanup_queue-inl.h"
4
5
namespace node {
6
7
5723
void CleanupQueue::Drain() {
8
  // Copy into a vector, since we can't sort an unordered_set in-place.
9
  std::vector<CleanupHookCallback> callbacks(cleanup_hooks_.begin(),
10
11446
                                             cleanup_hooks_.end());
11
  // We can't erase the copied elements from `cleanup_hooks_` yet, because we
12
  // need to be able to check whether they were un-scheduled by another hook.
13
14
5723
  std::sort(callbacks.begin(),
15
            callbacks.end(),
16
1290176
            [](const CleanupHookCallback& a, const CleanupHookCallback& b) {
17
              // Sort in descending order so that the most recently inserted
18
              // callbacks are run first.
19
1290176
              return a.insertion_order_counter_ > b.insertion_order_counter_;
20
            });
21
22
193604
  for (const CleanupHookCallback& cb : callbacks) {
23
187881
    if (cleanup_hooks_.count(cb) == 0) {
24
      // This hook was removed from the `cleanup_hooks_` set during another
25
      // hook that was run earlier. Nothing to do here.
26
1030
      continue;
27
    }
28
29
186851
    cb.fn_(cb.arg_);
30
186851
    cleanup_hooks_.erase(cb);
31
  }
32
5723
}
33
34
3368409
size_t CleanupQueue::CleanupHookCallback::Hash::operator()(
35
    const CleanupHookCallback& cb) const {
36
3368409
  return std::hash<void*>()(cb.arg_);
37
}
38
39
1685263
bool CleanupQueue::CleanupHookCallback::Equal::operator()(
40
    const CleanupHookCallback& a, const CleanupHookCallback& b) const {
41

1685263
  return a.fn_ == b.fn_ && a.arg_ == b.arg_;
42
}
43
44
}  // namespace node