LLVM API Documentation
00001 //===- LeakDetector.h - Provide leak detection ------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // 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_IR_LEAKDETECTOR_H 00023 #define LLVM_IR_LEAKDETECTOR_H 00024 00025 #include <string> 00026 00027 namespace llvm { 00028 00029 class LLVMContext; 00030 class Value; 00031 00032 struct LeakDetector { 00033 /// addGarbageObject - Add a pointer to the internal set of "garbage" object 00034 /// pointers. This should be called when objects are created, or if they are 00035 /// taken out of an owning collection. 00036 /// 00037 static void addGarbageObject(void *Object) { 00038 #ifndef NDEBUG 00039 addGarbageObjectImpl(Object); 00040 #endif 00041 } 00042 00043 /// removeGarbageObject - Remove a pointer from our internal representation of 00044 /// our "garbage" objects. This should be called when an object is added to 00045 /// an "owning" collection. 00046 /// 00047 static void removeGarbageObject(void *Object) { 00048 #ifndef NDEBUG 00049 removeGarbageObjectImpl(Object); 00050 #endif 00051 } 00052 00053 /// checkForGarbage - Traverse the internal representation of garbage 00054 /// pointers. If there are any pointers that have been add'ed, but not 00055 /// remove'd, big obnoxious warnings about memory leaks are issued. 00056 /// 00057 /// The specified message will be printed indicating when the check was 00058 /// performed. 00059 /// 00060 static void checkForGarbage(LLVMContext &C, const std::string &Message) { 00061 #ifndef NDEBUG 00062 checkForGarbageImpl(C, Message); 00063 #endif 00064 } 00065 00066 /// Overload the normal methods to work better with Value*'s because they are 00067 /// by far the most common in LLVM. This does not affect the actual 00068 /// functioning of this class, it just makes the warning messages nicer. 00069 /// 00070 static void addGarbageObject(const Value *Object) { 00071 #ifndef NDEBUG 00072 addGarbageObjectImpl(Object); 00073 #endif 00074 } 00075 static void removeGarbageObject(const Value *Object) { 00076 #ifndef NDEBUG 00077 removeGarbageObjectImpl(Object); 00078 #endif 00079 } 00080 00081 private: 00082 // If we are debugging, the actual implementations will be called... 00083 static void addGarbageObjectImpl(const Value *Object); 00084 static void removeGarbageObjectImpl(const Value *Object); 00085 static void addGarbageObjectImpl(void *Object); 00086 static void removeGarbageObjectImpl(void *Object); 00087 static void checkForGarbageImpl(LLVMContext &C, const std::string &Message); 00088 }; 00089 00090 } // End llvm namespace 00091 00092 #endif