GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: cleanup_queue.cc Lines: 15 15 100.0 %
Date: 2022-12-07 04:23:16 Branches: 7 8 87.5 %

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

1695819
  return a.fn_ == b.fn_ && a.arg_ == b.arg_;
43
}
44
45
}  // namespace node