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