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

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