diff options
author | Chris Lattner <sabre@nondot.org> | 2002-09-08 18:51:12 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2002-09-08 18:51:12 +0000 |
commit | 96c2ce8614bab233451aac237c4bb1d19963d33f (patch) | |
tree | d633fee12ef0b7bd1dc21d0456500d5efbffac4d | |
parent | a6299345ee306f219170d58acf8b4c7b7f54518b (diff) |
Checkin initial support for automatic memory leak detection routines
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@3617 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/Support/LeakDetector.h | 79 | ||||
-rw-r--r-- | include/llvm/Support/LeakDetector.h | 79 |
2 files changed, 158 insertions, 0 deletions
diff --git a/include/Support/LeakDetector.h b/include/Support/LeakDetector.h new file mode 100644 index 0000000000..dadbac1efa --- /dev/null +++ b/include/Support/LeakDetector.h @@ -0,0 +1,79 @@ +//===-- Support/LeakDetector.h - Provide simple leak detection --*- C++ -*-===// +// +// This file defines a class that can be used to provide very simple memory leak +// checks for an API. Basically LLVM uses this to make sure that Instructions, +// for example, are deleted when they are supposed to be, and not leaked away. +// +// When compiling with NDEBUG (Release build), this class does nothing, thus +// adding no checking overhead to release builds. Note that this class is +// implemented in a very simple way, requiring completely manual manipulation +// and checking for garbage, but this is intentional: users should not be using +// this API, only other APIs should. +// +//===----------------------------------------------------------------------===// + +#ifndef SUPPORT_LEAK_DETECTOR_H +#define SUPPORT_LEAK_DETECTOR_H + +#include <string> +class Value; + +struct LeakDetector { + /// addGarbageObject - Add a pointer to the internal set of "garbage" object + /// pointers. This should be called when objects are created, or if they are + /// taken out of an owning collection. + /// + static void addGarbageObject(void *Object) { +#ifndef NDEBUG + addGarbageObjectImpl(Object); +#endif + } + + /// removeGarbageObject - Remove a pointer from our internal representation of + /// our "garbage" objects. This should be called when an object is added to + /// an "owning" collection. + /// + static void removeGarbageObject(void *Object) { +#ifndef NDEBUG + removeGarbageObjectImpl(Object); +#endif + } + + /// checkForGarbage - Traverse the internal representation of garbage + /// pointers. If there are any pointers that have been add'ed, but not + /// remove'd, big obnoxious warnings about memory leaks are issued. + /// + /// The specified message will be printed indicating when the check was + /// performed. + /// + static void checkForGarbage(const std::string &Message) { +#ifndef NDEBUG + checkForGarbageImpl(Message); +#endif + } + + /// Overload the normal methods to work better with Value*'s because they are + /// by far the most common in LLVM. This does not affect the actual + /// functioning of this class, it just makes the warning messages nicer. + /// + static void addGarbageObject(const Value *Object) { +#ifndef NDEBUG + addGarbageObjectImpl(Object); +#endif + } + static void removeGarbageObject(const Value *Object) { +#ifndef NDEBUG + removeGarbageObjectImpl(Object); +#endif + } + +private: + // If we are debugging, the actual implementations will be called... + static void addGarbageObjectImpl(const Value *Object); + static void removeGarbageObjectImpl(const Value *Object); + static void addGarbageObjectImpl(void *Object); + static void removeGarbageObjectImpl(void *Object); + static void checkForGarbageImpl(const std::string &Message); +}; + +#endif diff --git a/include/llvm/Support/LeakDetector.h b/include/llvm/Support/LeakDetector.h new file mode 100644 index 0000000000..dadbac1efa --- /dev/null +++ b/include/llvm/Support/LeakDetector.h @@ -0,0 +1,79 @@ +//===-- Support/LeakDetector.h - Provide simple leak detection --*- C++ -*-===// +// +// This file defines a class that can be used to provide very simple memory leak +// checks for an API. Basically LLVM uses this to make sure that Instructions, +// for example, are deleted when they are supposed to be, and not leaked away. +// +// When compiling with NDEBUG (Release build), this class does nothing, thus +// adding no checking overhead to release builds. Note that this class is +// implemented in a very simple way, requiring completely manual manipulation +// and checking for garbage, but this is intentional: users should not be using +// this API, only other APIs should. +// +//===----------------------------------------------------------------------===// + +#ifndef SUPPORT_LEAK_DETECTOR_H +#define SUPPORT_LEAK_DETECTOR_H + +#include <string> +class Value; + +struct LeakDetector { + /// addGarbageObject - Add a pointer to the internal set of "garbage" object + /// pointers. This should be called when objects are created, or if they are + /// taken out of an owning collection. + /// + static void addGarbageObject(void *Object) { +#ifndef NDEBUG + addGarbageObjectImpl(Object); +#endif + } + + /// removeGarbageObject - Remove a pointer from our internal representation of + /// our "garbage" objects. This should be called when an object is added to + /// an "owning" collection. + /// + static void removeGarbageObject(void *Object) { +#ifndef NDEBUG + removeGarbageObjectImpl(Object); +#endif + } + + /// checkForGarbage - Traverse the internal representation of garbage + /// pointers. If there are any pointers that have been add'ed, but not + /// remove'd, big obnoxious warnings about memory leaks are issued. + /// + /// The specified message will be printed indicating when the check was + /// performed. + /// + static void checkForGarbage(const std::string &Message) { +#ifndef NDEBUG + checkForGarbageImpl(Message); +#endif + } + + /// Overload the normal methods to work better with Value*'s because they are + /// by far the most common in LLVM. This does not affect the actual + /// functioning of this class, it just makes the warning messages nicer. + /// + static void addGarbageObject(const Value *Object) { +#ifndef NDEBUG + addGarbageObjectImpl(Object); +#endif + } + static void removeGarbageObject(const Value *Object) { +#ifndef NDEBUG + removeGarbageObjectImpl(Object); +#endif + } + +private: + // If we are debugging, the actual implementations will be called... + static void addGarbageObjectImpl(const Value *Object); + static void removeGarbageObjectImpl(const Value *Object); + static void addGarbageObjectImpl(void *Object); + static void removeGarbageObjectImpl(void *Object); + static void checkForGarbageImpl(const std::string &Message); +}; + +#endif |