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

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