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