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

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