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 "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallVector.h"
23 /// \brief Gathers pairs of pointer-to-object/pointer-to-cleanup-function
24 /// allowing the cleanup functions to get called (with the pointer as parameter)
25 /// at specific points.
27 /// The use case is to simplify clean-up of certain resources that, while their
28 /// lifetime is well-known and restricted, cleaning them up manually is easy to
29 /// miss and cause a leak.
31 /// The same pointer can be added multiple times; its clean-up function will
32 /// only be called once.
33 class DelayedCleanupPool {
35 typedef void (*CleanupFn)(void *ptr);
37 /// \brief Adds a pointer and its associated cleanup function to be called
40 /// \returns false if the pointer is already added, true otherwise.
41 bool delayCleanup(void *ptr, CleanupFn fn) {
42 assert(ptr && "Expected valid pointer to object");
43 assert(fn && "Expected valid pointer to function");
45 CleanupFn &mapFn = Ptrs[ptr];
46 assert((!mapFn || mapFn == fn) &&
47 "Adding a pointer with different cleanup function!");
51 Cleanups.push_back(std::make_pair(ptr, fn));
59 bool delayDelete(T *ptr) {
60 return delayCleanup(ptr, cleanupWithDelete<T>);
63 template <typename T, void (T::*Fn)()>
64 bool delayMemberFunc(T *ptr) {
65 return delayCleanup(ptr, cleanupWithMemberFunc<T, Fn>);
69 for (llvm::SmallVector<std::pair<void *, CleanupFn>, 8>::reverse_iterator
70 I = Cleanups.rbegin(), E = Cleanups.rend(); I != E; ++I)
76 ~DelayedCleanupPool() {
81 llvm::DenseMap<void *, CleanupFn> Ptrs;
82 llvm::SmallVector<std::pair<void *, CleanupFn>, 8> Cleanups;
85 static void cleanupWithDelete(void *ptr) {
86 delete static_cast<T *>(ptr);
89 template <typename T, void (T::*Fn)()>
90 static void cleanupWithMemberFunc(void *ptr) {
91 (static_cast<T *>(ptr)->*Fn)();
95 /// \brief RAII object for triggering a cleanup of a DelayedCleanupPool.
96 class DelayedCleanupPoint {
97 DelayedCleanupPool &Pool;
100 DelayedCleanupPoint(DelayedCleanupPool &pool) : Pool(pool) { }
102 ~DelayedCleanupPoint() {
107 } // end namespace clang