1 //=== DelayedCleanupPool.h - Delayed Clean-up Pool Implementation *- C++ -*===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a facility to delay calling cleanup methods until specific
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_BASIC_DELAYEDCLEANUPPOOL_H
16 #define LLVM_CLANG_BASIC_DELAYEDCLEANUPPOOL_H
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/SmallVector.h"
24 /// \brief Gathers pairs of pointer-to-object/pointer-to-cleanup-function
25 /// allowing the cleanup functions to get called (with the pointer as parameter)
26 /// at specific points.
28 /// The use case is to simplify clean-up of certain resources that, while their
29 /// lifetime is well-known and restricted, cleaning them up manually is easy to
30 /// miss and cause a leak.
32 /// The same pointer can be added multiple times; its clean-up function will
33 /// only be called once.
34 class DelayedCleanupPool {
36 typedef void (*CleanupFn)(void *ptr);
38 /// \brief Adds a pointer and its associated cleanup function to be called
41 /// \returns false if the pointer is already added, true otherwise.
42 bool delayCleanup(void *ptr, CleanupFn fn) {
43 assert(ptr && "Expected valid pointer to object");
44 assert(fn && "Expected valid pointer to function");
46 CleanupFn &mapFn = Ptrs[ptr];
47 assert((!mapFn || mapFn == fn) &&
48 "Adding a pointer with different cleanup function!");
52 Cleanups.push_back(std::make_pair(ptr, fn));
60 bool delayDelete(T *ptr) {
61 return delayCleanup(ptr, cleanupWithDelete<T>);
64 template <typename T, void (T::*Fn)()>
65 bool delayMemberFunc(T *ptr) {
66 return delayCleanup(ptr, cleanupWithMemberFunc<T, Fn>);
70 for (SmallVector<std::pair<void *, CleanupFn>, 8>::reverse_iterator
71 I = Cleanups.rbegin(), E = Cleanups.rend(); I != E; ++I)
77 ~DelayedCleanupPool() {
82 llvm::DenseMap<void *, CleanupFn> Ptrs;
83 SmallVector<std::pair<void *, CleanupFn>, 8> Cleanups;
86 static void cleanupWithDelete(void *ptr) {
87 delete static_cast<T *>(ptr);
90 template <typename T, void (T::*Fn)()>
91 static void cleanupWithMemberFunc(void *ptr) {
92 (static_cast<T *>(ptr)->*Fn)();
96 /// \brief RAII object for triggering a cleanup of a DelayedCleanupPool.
97 class DelayedCleanupPoint {
98 DelayedCleanupPool &Pool;
101 DelayedCleanupPoint(DelayedCleanupPool &pool) : Pool(pool) { }
103 ~DelayedCleanupPoint() {
108 } // end namespace clang