Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/nodejs/src/cleanup_queue.h
1 #ifndef SRC_CLEANUP_QUEUE_H_ 2 #define SRC_CLEANUP_QUEUE_H_ 3 4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 5 6 #include <cstddef> 7 #include <cstdint> 8 #include <unordered_set> 9 #include <vector> 10 11 #include "memory_tracker.h" 12 13 namespace node { 14 15 class CleanupQueue : public MemoryRetainer { 16 public: 17 typedef void (*Callback)(void*); 18 19 CleanupQueue() = default; 20 21 // Not copyable. 22 CleanupQueue(const CleanupQueue&) = delete; 23 24 SET_MEMORY_INFO_NAME(CleanupQueue) 25 SET_NO_MEMORY_INFO() 26 inline size_t SelfSize() const override; 27 28 inline bool empty() const; 29 30 inline void Add(Callback cb, void* arg); 31 inline void Remove(Callback cb, void* arg); 32 void Drain(); 33 34 private: 35 class CleanupHookCallback { 36 public: 37 CleanupHookCallback(Callback fn, 38 void* arg, 39 uint64_t insertion_order_counter) 40 : fn_(fn), 41 arg_(arg), 42 insertion_order_counter_(insertion_order_counter) {} 43 44 // Only hashes `arg_`, since that is usually enough to identify the hook. 45 struct Hash { 46 size_t operator()(const CleanupHookCallback& cb) const; 47 }; 48 49 // Compares by `fn_` and `arg_` being equal. 50 struct Equal { 51 bool operator()(const CleanupHookCallback& a, 52 const CleanupHookCallback& b) const; 53 }; 54 55 private: 56 friend class CleanupQueue; 57 Callback fn_; 58 void* arg_; 59 60 // We keep track of the insertion order for these objects, so that we can 61 // call the callbacks in reverse order when we are cleaning up. 62 uint64_t insertion_order_counter_; 63 }; 64 65 std::vector<CleanupHookCallback> GetOrdered() const; 66 67 // Use an unordered_set, so that we have efficient insertion and removal. 68 std::unordered_set<CleanupHookCallback, 69 CleanupHookCallback::Hash, 70 CleanupHookCallback::Equal> 71 cleanup_hooks_; 72 uint64_t cleanup_hook_counter_ = 0; 73 }; 74 75 } // namespace node 76 77 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 78 79 #endif // SRC_CLEANUP_QUEUE_H_