Added LLVM notice.
[oota-llvm.git] / include / llvm / Support / LeakDetector.h
1 //===-- Support/LeakDetector.h - Provide simple 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 SUPPORT_LEAKDETECTOR_H
23 #define SUPPORT_LEAKDETECTOR_H
24
25 #include <string>
26 class Value;
27
28 struct LeakDetector {
29   /// addGarbageObject - Add a pointer to the internal set of "garbage" object
30   /// pointers.  This should be called when objects are created, or if they are
31   /// taken out of an owning collection.
32   ///
33   static void addGarbageObject(void *Object) {
34 #ifndef NDEBUG
35     addGarbageObjectImpl(Object);
36 #endif
37   }
38
39   /// removeGarbageObject - Remove a pointer from our internal representation of
40   /// our "garbage" objects.  This should be called when an object is added to
41   /// an "owning" collection.
42   ///
43   static void removeGarbageObject(void *Object) {
44 #ifndef NDEBUG
45     removeGarbageObjectImpl(Object);
46 #endif
47   }
48   
49   /// checkForGarbage - Traverse the internal representation of garbage
50   /// pointers.  If there are any pointers that have been add'ed, but not
51   /// remove'd, big obnoxious warnings about memory leaks are issued.
52   ///
53   /// The specified message will be printed indicating when the check was
54   /// performed.
55   ///
56   static void checkForGarbage(const std::string &Message) {
57 #ifndef NDEBUG
58     checkForGarbageImpl(Message);
59 #endif
60   }
61
62   /// Overload the normal methods to work better with Value*'s because they are
63   /// by far the most common in LLVM.  This does not affect the actual
64   /// functioning of this class, it just makes the warning messages nicer.
65   ///
66   static void addGarbageObject(const Value *Object) {
67 #ifndef NDEBUG
68     addGarbageObjectImpl(Object);
69 #endif
70   }
71   static void removeGarbageObject(const Value *Object) {
72 #ifndef NDEBUG
73     removeGarbageObjectImpl(Object);
74 #endif
75   }
76
77 private:
78   // If we are debugging, the actual implementations will be called...
79   static void addGarbageObjectImpl(const Value *Object);
80   static void removeGarbageObjectImpl(const Value *Object);
81   static void addGarbageObjectImpl(void *Object);
82   static void removeGarbageObjectImpl(void *Object);
83   static void checkForGarbageImpl(const std::string &Message);
84 };
85
86 #endif