LLVM API Documentation
00001 //===-- llvm/Support/LeakDetector.h - Provide leak detection ----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines a class that can be used to provide very simple memory leak 00011 // checks for an API. Basically LLVM uses this to make sure that Instructions, 00012 // for example, are deleted when they are supposed to be, and not leaked away. 00013 // 00014 // When compiling with NDEBUG (Release build), this class does nothing, thus 00015 // adding no checking overhead to release builds. Note that this class is 00016 // implemented in a very simple way, requiring completely manual manipulation 00017 // and checking for garbage, but this is intentional: users should not be using 00018 // this API, only other APIs should. 00019 // 00020 //===----------------------------------------------------------------------===// 00021 00022 #ifndef LLVM_SUPPORT_LEAKDETECTOR_H 00023 #define LLVM_SUPPORT_LEAKDETECTOR_H 00024 00025 #include <string> 00026 00027 namespace llvm { 00028 00029 class Value; 00030 00031 struct LeakDetector { 00032 /// addGarbageObject - Add a pointer to the internal set of "garbage" object 00033 /// pointers. This should be called when objects are created, or if they are 00034 /// taken out of an owning collection. 00035 /// 00036 static void addGarbageObject(void *Object) { 00037 #ifndef NDEBUG 00038 addGarbageObjectImpl(Object); 00039 #endif 00040 } 00041 00042 /// removeGarbageObject - Remove a pointer from our internal representation of 00043 /// our "garbage" objects. This should be called when an object is added to 00044 /// an "owning" collection. 00045 /// 00046 static void removeGarbageObject(void *Object) { 00047 #ifndef NDEBUG 00048 removeGarbageObjectImpl(Object); 00049 #endif 00050 } 00051 00052 /// checkForGarbage - Traverse the internal representation of garbage 00053 /// pointers. If there are any pointers that have been add'ed, but not 00054 /// remove'd, big obnoxious warnings about memory leaks are issued. 00055 /// 00056 /// The specified message will be printed indicating when the check was 00057 /// performed. 00058 /// 00059 static void checkForGarbage(const std::string &Message) { 00060 #ifndef NDEBUG 00061 checkForGarbageImpl(Message); 00062 #endif 00063 } 00064 00065 /// Overload the normal methods to work better with Value*'s because they are 00066 /// by far the most common in LLVM. This does not affect the actual 00067 /// functioning of this class, it just makes the warning messages nicer. 00068 /// 00069 static void addGarbageObject(const Value *Object) { 00070 #ifndef NDEBUG 00071 addGarbageObjectImpl(Object); 00072 #endif 00073 } 00074 static void removeGarbageObject(const Value *Object) { 00075 #ifndef NDEBUG 00076 removeGarbageObjectImpl(Object); 00077 #endif 00078 } 00079 00080 private: 00081 // If we are debugging, the actual implementations will be called... 00082 static void addGarbageObjectImpl(const Value *Object); 00083 static void removeGarbageObjectImpl(const Value *Object); 00084 static void addGarbageObjectImpl(void *Object); 00085 static void removeGarbageObjectImpl(void *Object); 00086 static void checkForGarbageImpl(const std::string &Message); 00087 }; 00088 00089 } // End llvm namespace 00090 00091 #endif