Files
boost_beast/beast/utility/LeakChecked.h
T

181 lines
4.1 KiB
C++
Raw Normal View History

2013-06-17 09:28:43 -07:00
//------------------------------------------------------------------------------
2013-06-17 06:42:49 -07:00
/*
2013-06-17 09:28:43 -07:00
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
2013-06-17 06:42:49 -07:00
2013-06-17 09:28:43 -07:00
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
2013-06-17 06:42:49 -07:00
2013-06-17 09:28:43 -07:00
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2013-06-17 06:42:49 -07:00
*/
2013-06-17 09:28:43 -07:00
//==============================================================================
2013-06-17 06:42:49 -07:00
2013-10-04 13:49:24 -07:00
#ifndef BEAST_UTILITY_LEAKCHECKED_H_INCLUDED
#define BEAST_UTILITY_LEAKCHECKED_H_INCLUDED
2013-06-17 06:42:49 -07:00
#include <beast/Config.h>
#include <beast/Atomic.h>
#include <beast/intrusive/LockFreeStack.h>
#include <beast/utility/StaticObject.h>
2013-10-04 13:49:24 -07:00
namespace beast {
namespace detail {
2013-06-17 06:42:49 -07:00
class LeakCheckedBase
2013-06-17 06:42:49 -07:00
{
public:
static void checkForLeaks ();
2013-06-17 06:42:49 -07:00
protected:
2013-09-20 16:44:04 -07:00
class LeakCounterBase : public LockFreeStack <LeakCounterBase>::Node
2013-06-17 06:42:49 -07:00
{
public:
2013-09-20 16:44:04 -07:00
LeakCounterBase ();
2013-06-17 06:42:49 -07:00
2013-09-20 16:44:04 -07:00
virtual ~LeakCounterBase ()
{
}
2013-06-17 06:42:49 -07:00
inline int increment ()
{
return ++m_count;
}
inline int decrement ()
{
return --m_count;
}
virtual char const* getClassName () const = 0;
private:
2013-06-30 09:04:09 -07:00
void checkForLeaks ();
2013-06-17 06:42:49 -07:00
virtual void checkPureVirtual () const = 0;
class Singleton;
friend class LeakCheckedBase;
2013-06-17 06:42:49 -07:00
Atomic <int> m_count;
};
2013-06-30 09:04:09 -07:00
2013-07-05 07:56:51 -07:00
static void reportDanglingPointer (char const* objectName);
2013-06-17 06:42:49 -07:00
};
//------------------------------------------------------------------------------
/** Detects leaks at program exit.
To use this, derive your class from this template using CRTP (curiously
recurring template pattern).
*/
template <class Object>
class LeakChecked : private LeakCheckedBase
{
protected:
LeakChecked () noexcept
{
2013-06-29 22:59:07 -07:00
getCounter ().increment ();
2013-06-17 06:42:49 -07:00
}
2013-06-30 09:04:09 -07:00
LeakChecked (LeakChecked const&) noexcept
2013-06-17 06:42:49 -07:00
{
2013-06-29 22:59:07 -07:00
getCounter ().increment ();
2013-06-17 06:42:49 -07:00
}
~LeakChecked ()
{
2013-06-29 22:59:07 -07:00
if (getCounter ().decrement () < 0)
2013-06-17 06:42:49 -07:00
{
2013-07-05 07:56:51 -07:00
reportDanglingPointer (getLeakCheckedName ());
2013-06-17 06:42:49 -07:00
}
}
private:
2013-06-30 09:04:09 -07:00
// Singleton that maintains the count of this object
//
2013-09-20 16:44:04 -07:00
class LeakCounter : public LeakCounterBase
2013-06-17 06:42:49 -07:00
{
public:
2013-09-20 16:44:04 -07:00
LeakCounter () noexcept
2013-06-17 06:42:49 -07:00
{
}
char const* getClassName () const
{
return getLeakCheckedName ();
}
void checkPureVirtual () const { }
};
private:
/* Due to a bug in Visual Studio 10 and earlier, the string returned by
typeid().name() will appear to leak on exit. Therefore, we should
only call this function when there's an actual leak, or else there
will be spurious leak notices at exit.
*/
static const char* getLeakCheckedName ()
{
return typeid (Object).name ();
}
2013-06-30 09:04:09 -07:00
// Retrieve the singleton for this object
//
2013-09-20 16:44:04 -07:00
static LeakCounter& getCounter () noexcept
2013-06-17 06:42:49 -07:00
{
2013-09-20 16:44:04 -07:00
return StaticObject <LeakCounter>::get();
2013-06-17 06:42:49 -07:00
}
};
2013-06-30 09:04:09 -07:00
}
//------------------------------------------------------------------------------
namespace detail
2013-06-30 09:04:09 -07:00
{
2013-06-17 06:42:49 -07:00
namespace disabled
2013-06-17 06:42:49 -07:00
{
class LeakCheckedBase
{
2013-06-30 09:04:09 -07:00
public:
static void checkForLeaks ()
{
}
2013-06-17 06:42:49 -07:00
};
template <class Object>
class LeakChecked : public LeakCheckedBase
2013-06-17 06:42:49 -07:00
{
public:
2013-06-17 06:42:49 -07:00
};
2013-06-30 09:04:09 -07:00
}
}
2013-06-30 09:04:09 -07:00
//------------------------------------------------------------------------------
// Lift the appropriate implementation into our namespace
2013-06-30 09:04:09 -07:00
//
#if BEAST_CHECK_MEMORY_LEAKS
using detail::LeakChecked;
using detail::LeakCheckedBase;
2013-06-30 09:04:09 -07:00
#else
using detail::disabled::LeakChecked;
using detail::disabled::LeakCheckedBase;
2013-06-17 06:42:49 -07:00
#endif
2013-10-04 13:49:24 -07:00
}
2013-06-17 06:42:49 -07:00
#endif