GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: cleanup_queue.cc Lines: 15 15 100.0 %
Date: 2022-09-21 04:23:13 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
5637
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
11274
                                             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
5637
  std::sort(callbacks.begin(),
15
            callbacks.end(),
16
1284702
            [](const CleanupHookCallback& a, const CleanupHookCallback& b) {
17
              // Sort in descending order so that the most recently inserted
18
              // callbacks are run first.
19
1284702
              return a.insertion_order_counter_ > b.insertion_order_counter_;
20
            });
21
22
191945
  for (const CleanupHookCallback& cb : callbacks) {
23
186308
    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
1032
      continue;
27
    }
28
29
185276
    cb.fn_(cb.arg_);
30
185276
    cleanup_hooks_.erase(cb);
31
  }
32
5637
}
33
34
3362138
size_t CleanupQueue::CleanupHookCallback::Hash::operator()(
35
    const CleanupHookCallback& cb) const {
36
3362138
  return std::hash<void*>()(cb.arg_);
37
}
38
39
1681160
bool CleanupQueue::CleanupHookCallback::Equal::operator()(
40
    const CleanupHookCallback& a, const CleanupHookCallback& b) const {
41

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