1 //===-- Support/LeakDetector.h - Provide simple leak detection --*- C++ -*-===//
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.
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef SUPPORT_LEAK_DETECTOR_H
16 #define SUPPORT_LEAK_DETECTOR_H
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.
26 static void addGarbageObject(void *Object) {
28 addGarbageObjectImpl(Object);
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.
36 static void removeGarbageObject(void *Object) {
38 removeGarbageObjectImpl(Object);
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.
46 /// The specified message will be printed indicating when the check was
49 static void checkForGarbage(const std::string &Message) {
51 checkForGarbageImpl(Message);
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.
59 static void addGarbageObject(const Value *Object) {
61 addGarbageObjectImpl(Object);
64 static void removeGarbageObject(const Value *Object) {
66 removeGarbageObjectImpl(Object);
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);