2014-03-04 12:46:06 +00:00
|
|
|
//===- LeakDetector.h - Provide leak detection ------------------*- C++ -*-===//
|
2005-04-21 20:48:15 +00:00
|
|
|
//
|
2003-10-20 19:46:57 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:48:15 +00:00
|
|
|
//
|
2003-10-20 19:46:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-08 18:51:12 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-03-04 12:46:06 +00:00
|
|
|
#ifndef LLVM_IR_LEAKDETECTOR_H
|
|
|
|
#define LLVM_IR_LEAKDETECTOR_H
|
2002-09-08 18:51:12 +00:00
|
|
|
|
|
|
|
#include <string>
|
2003-11-11 22:41:34 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2009-11-05 13:30:28 +00:00
|
|
|
class LLVMContext;
|
2002-09-08 18:51:12 +00:00
|
|
|
class Value;
|
2014-12-11 21:39:39 +00:00
|
|
|
class MDNode;
|
2002-09-08 18:51:12 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
///
|
2014-12-11 21:23:43 +00:00
|
|
|
template <class T> static void addGarbageObject(T *Object) {
|
2002-09-08 18:51:12 +00:00
|
|
|
#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.
|
|
|
|
///
|
2014-12-11 21:23:43 +00:00
|
|
|
template <class T> static void removeGarbageObject(T *Object) {
|
2002-09-08 18:51:12 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
removeGarbageObjectImpl(Object);
|
|
|
|
#endif
|
|
|
|
}
|
2005-04-21 20:48:15 +00:00
|
|
|
|
2002-09-08 18:51:12 +00:00
|
|
|
/// 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.
|
|
|
|
///
|
2009-08-19 17:07:46 +00:00
|
|
|
static void checkForGarbage(LLVMContext &C, const std::string &Message) {
|
2002-09-08 18:51:12 +00:00
|
|
|
#ifndef NDEBUG
|
2009-08-19 17:07:46 +00:00
|
|
|
checkForGarbageImpl(C, Message);
|
2002-09-08 18:51:12 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-12-11 21:23:43 +00:00
|
|
|
/// Overload the normal methods to work better with Value* because they are
|
|
|
|
/// by far the most common in LLVM.
|
|
|
|
///
|
|
|
|
/// Besides making the warning messages nicer, this hides errors by storing
|
|
|
|
/// Value* in a different leak-detection container than other classes.
|
2002-09-08 18:51:12 +00:00
|
|
|
static void addGarbageObjectImpl(const Value *Object);
|
|
|
|
static void removeGarbageObjectImpl(const Value *Object);
|
2014-12-11 21:23:43 +00:00
|
|
|
|
2014-12-11 21:39:39 +00:00
|
|
|
/// Overload the normal methods to work better with MDNode* to improve error
|
|
|
|
/// messages.
|
|
|
|
///
|
|
|
|
/// For better or worse, this hides errors when other types are added as
|
|
|
|
/// garbage, deleted without being removed, and an MDNode is allocated in the
|
|
|
|
/// same spot.
|
|
|
|
///
|
|
|
|
/// \note Only handle \a MDNode for now, since we can't always get access to
|
|
|
|
/// an \a LLVMContext for other \a Metadata types.
|
|
|
|
static void addGarbageObjectImpl(const MDNode *Object);
|
|
|
|
static void removeGarbageObjectImpl(const MDNode *Object);
|
|
|
|
|
2002-09-08 18:51:12 +00:00
|
|
|
static void addGarbageObjectImpl(void *Object);
|
|
|
|
static void removeGarbageObjectImpl(void *Object);
|
2009-08-19 17:07:46 +00:00
|
|
|
static void checkForGarbageImpl(LLVMContext &C, const std::string &Message);
|
2002-09-08 18:51:12 +00:00
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2002-09-08 18:51:12 +00:00
|
|
|
#endif
|