4c682be500ffd266357d628cb0210e21cb0320e5
[oota-llvm.git] / include / Support / LeakDetector.h
1 //===-- Support/LeakDetector.h - Provide simple leak detection --*- C++ -*-===//
2 //
3 // This file defines a class that can be used to provide very simple memory leak
4 // checks for an API.  Basically LLVM uses this to make sure that Instructions,
5 // for example, are deleted when they are supposed to be, and not leaked away.
6 //
7 // When compiling with NDEBUG (Release build), this class does nothing, thus
8 // adding no checking overhead to release builds.  Note that this class is
9 // implemented in a very simple way, requiring completely manual manipulation
10 // and checking for garbage, but this is intentional: users should not be using
11 // this API, only other APIs should.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef SUPPORT_LEAKDETECTOR_H
16 #define SUPPORT_LEAKDETECTOR_H
17
18 #include <string>
19 class Value;
20
21 struct LeakDetector {
22   /// addGarbageObject - Add a pointer to the internal set of "garbage" object
23   /// pointers.  This should be called when objects are created, or if they are
24   /// taken out of an owning collection.
25   ///
26   static void addGarbageObject(void *Object) {
27 #ifndef NDEBUG
28     addGarbageObjectImpl(Object);
29 #endif
30   }
31
32   /// removeGarbageObject - Remove a pointer from our internal representation of
33   /// our "garbage" objects.  This should be called when an object is added to
34   /// an "owning" collection.
35   ///
36   static void removeGarbageObject(void *Object) {
37 #ifndef NDEBUG
38     removeGarbageObjectImpl(Object);
39 #endif
40   }
41   
42   /// checkForGarbage - Traverse the internal representation of garbage
43   /// pointers.  If there are any pointers that have been add'ed, but not
44   /// remove'd, big obnoxious warnings about memory leaks are issued.
45   ///
46   /// The specified message will be printed indicating when the check was
47   /// performed.
48   ///
49   static void checkForGarbage(const std::string &Message) {
50 #ifndef NDEBUG
51     checkForGarbageImpl(Message);
52 #endif
53   }
54
55   /// Overload the normal methods to work better with Value*'s because they are
56   /// by far the most common in LLVM.  This does not affect the actual
57   /// functioning of this class, it just makes the warning messages nicer.
58   ///
59   static void addGarbageObject(const Value *Object) {
60 #ifndef NDEBUG
61     addGarbageObjectImpl(Object);
62 #endif
63   }
64   static void removeGarbageObject(const Value *Object) {
65 #ifndef NDEBUG
66     removeGarbageObjectImpl(Object);
67 #endif
68   }
69
70 private:
71   // If we are debugging, the actual implementations will be called...
72   static void addGarbageObjectImpl(const Value *Object);
73   static void removeGarbageObjectImpl(const Value *Object);
74   static void addGarbageObjectImpl(void *Object);
75   static void removeGarbageObjectImpl(void *Object);
76   static void checkForGarbageImpl(const std::string &Message);
77 };
78
79 #endif