1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
//=== DelayedCleanupPool.h - Delayed Clean-up Pool Implementation *- C++ -*===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines a facility to delay calling cleanup methods until specific
// points.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_BASIC_DELAYEDCLEANUPPOOL_H
#define LLVM_CLANG_BASIC_DELAYEDCLEANUPPOOL_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
namespace clang {
/// \brief Gathers pairs of pointer-to-object/pointer-to-cleanup-function
/// allowing the cleanup functions to get called (with the pointer as parameter)
/// at specific points.
///
/// The use case is to simplify clean-up of certain resources that, while their
/// lifetime is well-known and restricted, cleaning them up manually is easy to
/// miss and cause a leak.
///
/// The same pointer can be added multiple times; its clean-up function will
/// only be called once.
class DelayedCleanupPool {
public:
typedef void (*CleanupFn)(void *ptr);
/// \brief Adds a pointer and its associated cleanup function to be called
/// at a later point.
///
/// \returns false if the pointer is already added, true otherwise.
bool delayCleanup(void *ptr, CleanupFn fn) {
assert(ptr && "Expected valid pointer to object");
assert(fn && "Expected valid pointer to function");
CleanupFn &mapFn = Ptrs[ptr];
assert((!mapFn || mapFn == fn) &&
"Adding a pointer with different cleanup function!");
if (!mapFn) {
mapFn = fn;
Cleanups.push_back(std::make_pair(ptr, fn));
return true;
}
return false;
}
template <typename T>
bool delayDelete(T *ptr) {
return delayCleanup(ptr, cleanupWithDelete<T>);
}
template <typename T, void (T::*Fn)()>
bool delayMemberFunc(T *ptr) {
return delayCleanup(ptr, cleanupWithMemberFunc<T, Fn>);
}
void doCleanup() {
for (llvm::SmallVector<std::pair<void *, CleanupFn>, 8>::reverse_iterator
I = Cleanups.rbegin(), E = Cleanups.rend(); I != E; ++I)
I->second(I->first);
Cleanups.clear();
Ptrs.clear();
}
~DelayedCleanupPool() {
doCleanup();
}
private:
llvm::DenseMap<void *, CleanupFn> Ptrs;
llvm::SmallVector<std::pair<void *, CleanupFn>, 8> Cleanups;
template <typename T>
static void cleanupWithDelete(void *ptr) {
delete static_cast<T *>(ptr);
}
template <typename T, void (T::*Fn)()>
static void cleanupWithMemberFunc(void *ptr) {
(static_cast<T *>(ptr)->*Fn)();
}
};
/// \brief RAII object for triggering a cleanup of a DelayedCleanupPool.
class DelayedCleanupPoint {
DelayedCleanupPool &Pool;
public:
DelayedCleanupPoint(DelayedCleanupPool &pool) : Pool(pool) { }
~DelayedCleanupPoint() {
Pool.doCleanup();
}
};
} // end namespace clang
#endif
|