Tidy up LeakChecked and configuration macro

This commit is contained in:
Vinnie Falco
2013-06-30 09:04:09 -07:00
parent e84b524fc4
commit 75736f547a
10 changed files with 150 additions and 133 deletions

View File

@@ -120,8 +120,4 @@
//#define BEAST_BIND_USES_TR1 1 //#define BEAST_BIND_USES_TR1 1
//#define BEAST_BIND_USES_BOOST 1 //#define BEAST_BIND_USES_BOOST 1
#ifndef BEAST_USE_LEAKCHECKED
#define BEAST_USE_LEAKCHECKED 1
#endif
#endif #endif

View File

@@ -208,6 +208,7 @@
<ClInclude Include="..\..\modules\beast_core\threads\beast_TimeSliceThread.h" /> <ClInclude Include="..\..\modules\beast_core\threads\beast_TimeSliceThread.h" />
<ClInclude Include="..\..\modules\beast_core\threads\beast_WaitableEvent.h" /> <ClInclude Include="..\..\modules\beast_core\threads\beast_WaitableEvent.h" />
<ClInclude Include="..\..\modules\beast_core\time\beast_PerformanceCounter.h" /> <ClInclude Include="..\..\modules\beast_core\time\beast_PerformanceCounter.h" />
<ClInclude Include="..\..\modules\beast_core\time\beast_PerformedAtExit.h" />
<ClInclude Include="..\..\modules\beast_core\time\beast_RelativeTime.h" /> <ClInclude Include="..\..\modules\beast_core\time\beast_RelativeTime.h" />
<ClInclude Include="..\..\modules\beast_core\time\beast_Time.h" /> <ClInclude Include="..\..\modules\beast_core\time\beast_Time.h" />
<ClInclude Include="..\..\modules\beast_core\unit_tests\beast_UnitTest.h" /> <ClInclude Include="..\..\modules\beast_core\unit_tests\beast_UnitTest.h" />
@@ -778,6 +779,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\modules\beast_core\time\beast_PerformedAtExit.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\time\beast_RelativeTime.cpp"> <ClCompile Include="..\..\modules\beast_core\time\beast_RelativeTime.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>

View File

@@ -608,6 +608,9 @@
<ClInclude Include="..\..\modules\beast_core\maths\beast_Interval.h"> <ClInclude Include="..\..\modules\beast_core\maths\beast_Interval.h">
<Filter>beast_core\maths</Filter> <Filter>beast_core\maths</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\modules\beast_core\time\beast_PerformedAtExit.h">
<Filter>beast_core\time</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\modules\beast_core\beast_core.cpp"> <ClCompile Include="..\..\modules\beast_core\beast_core.cpp">
@@ -946,6 +949,9 @@
<ClCompile Include="..\..\modules\beast_core\native\beast_win32_FPUFlags.cpp"> <ClCompile Include="..\..\modules\beast_core\native\beast_win32_FPUFlags.cpp">
<Filter>beast_core\native</Filter> <Filter>beast_core\native</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\modules\beast_core\time\beast_PerformedAtExit.cpp">
<Filter>beast_core\time</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Text Include="..\..\TODO.txt" /> <Text Include="..\..\TODO.txt" />

View File

@@ -2,6 +2,8 @@
BEAST TODO BEAST TODO
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
- Make OwnedArray add routines return a pointer instead of reference
- Tidy up CacheLine, MemoryAlignment - Tidy up CacheLine, MemoryAlignment
- Remove anything having to do with DLL builds like - Remove anything having to do with DLL builds like

View File

@@ -81,10 +81,6 @@
#define BEAST_BOOST_IS_AVAILABLE 0 #define BEAST_BOOST_IS_AVAILABLE 0
#endif #endif
#ifndef BEAST_USE_LEAKCHECKED
#define BEAST_USE_LEAKCHECKED BEAST_CHECK_MEMORY_LEAKS
#endif
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// //
// This is a hack to fix boost's goofy placeholders // This is a hack to fix boost's goofy placeholders
@@ -208,6 +204,7 @@ namespace beast
#include "containers/beast_LockFreeStack.h" #include "containers/beast_LockFreeStack.h"
#include "threads/beast_SpinDelay.h" #include "threads/beast_SpinDelay.h"
#include "memory/beast_StaticObject.h" #include "memory/beast_StaticObject.h"
#include "time/beast_PerformedAtExit.h"
#include "diagnostic/beast_LeakChecked.h" #include "diagnostic/beast_LeakChecked.h"
#include "memory/beast_Memory.h" #include "memory/beast_Memory.h"
#include "memory/beast_ByteOrder.h" #include "memory/beast_ByteOrder.h"
@@ -261,7 +258,6 @@ namespace beast
#include "memory/beast_ReferenceCountedObject.h" #include "memory/beast_ReferenceCountedObject.h"
#include "memory/beast_ScopedPointer.h" #include "memory/beast_ScopedPointer.h"
#include "threads/beast_SpinLock.h" #include "threads/beast_SpinLock.h"
#include "time/beast_PerformedAtExit.h"
#include "memory/beast_SharedSingleton.h" #include "memory/beast_SharedSingleton.h"
#include "memory/beast_WeakReference.h" #include "memory/beast_WeakReference.h"
#include "memory/beast_MemoryAlignment.h" #include "memory/beast_MemoryAlignment.h"

View File

@@ -17,7 +17,8 @@
*/ */
//============================================================================== //==============================================================================
#if BEAST_USE_LEAKCHECKED namespace Implemented
{
class LeakCheckedBase::CounterBase::Singleton class LeakCheckedBase::CounterBase::Singleton
{ {
@@ -27,16 +28,16 @@ public:
m_list.push_front (counter); m_list.push_front (counter);
} }
void detectAllLeaks () void checkForLeaks ()
{ {
for (;;) for (;;)
{ {
CounterBase* counter = m_list.pop_front (); CounterBase* const counter = m_list.pop_front ();
if (!counter) if (!counter)
break; break;
counter->detectLeaks (); counter->checkForLeaks ();
} }
} }
@@ -48,6 +49,8 @@ public:
} }
private: private:
friend class LeakCheckedBase;
LockFreeStack <CounterBase> m_list; LockFreeStack <CounterBase> m_list;
}; };
@@ -58,12 +61,7 @@ LeakCheckedBase::CounterBase::CounterBase ()
Singleton::getInstance ().push_back (this); Singleton::getInstance ().push_back (this);
} }
void LeakCheckedBase::CounterBase::detectAllLeaks () void LeakCheckedBase::CounterBase::checkForLeaks ()
{
Singleton::getInstance ().detectAllLeaks ();
}
void LeakCheckedBase::CounterBase::detectLeaks ()
{ {
// If there's a runtime error from this line, it means there's // If there's a runtime error from this line, it means there's
// an order of destruction problem between different translation units! // an order of destruction problem between different translation units!
@@ -91,9 +89,9 @@ void LeakCheckedBase::CounterBase::detectLeaks ()
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void LeakCheckedBase::detectAllLeaks () void LeakCheckedBase::checkForLeaks ()
{ {
CounterBase::detectAllLeaks (); CounterBase::Singleton::getInstance ().checkForLeaks ();
} }
#endif }

View File

@@ -17,20 +17,14 @@
*/ */
//============================================================================== //==============================================================================
#ifndef BEAST_LEAKCHECKED_BEASTHEADER #ifndef BEAST_LEAKCHECKED_H_INCLUDED
#define BEAST_LEAKCHECKED_BEASTHEADER #define BEAST_LEAKCHECKED_H_INCLUDED
// namespace Implemented
// Derived classes are automatically leak-checked on exit {
//
#if BEAST_USE_LEAKCHECKED
class BEAST_API LeakCheckedBase class BEAST_API LeakCheckedBase
{ {
public:
static void detectAllLeaks ();
protected: protected:
class CounterBase : public LockFreeStack <CounterBase>::Node class CounterBase : public LockFreeStack <CounterBase>::Node
{ {
@@ -51,18 +45,23 @@ protected:
virtual char const* getClassName () const = 0; virtual char const* getClassName () const = 0;
static void detectAllLeaks ();
private: private:
void detectLeaks (); void checkForLeaks ();
virtual void checkPureVirtual () const = 0; virtual void checkPureVirtual () const = 0;
protected: private:
friend class LeakCheckedBase;
class Singleton; class Singleton;
Atomic <int> m_count; Atomic <int> m_count;
}; };
private:
friend class PerformedAtExit::ExitHook;
static void checkForLeaks ();
}; };
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -81,7 +80,7 @@ protected:
getCounter ().increment (); getCounter ().increment ();
} }
LeakChecked (const LeakChecked&) noexcept LeakChecked (LeakChecked const&) noexcept
{ {
getCounter ().increment (); getCounter ().increment ();
} }
@@ -113,6 +112,8 @@ protected:
} }
private: private:
// Singleton that maintains the count of this object
//
class Counter : public CounterBase class Counter : public CounterBase
{ {
public: public:
@@ -139,6 +140,8 @@ private:
return typeid (Object).name (); return typeid (Object).name ();
} }
// Retrieve the singleton for this object
//
static Counter& getCounter () noexcept static Counter& getCounter () noexcept
{ {
static Counter* volatile s_instance; static Counter* volatile s_instance;
@@ -156,14 +159,20 @@ private:
} }
}; };
#else }
//------------------------------------------------------------------------------
namespace Dummy
{
class BEAST_API LeakCheckedBase class BEAST_API LeakCheckedBase
{ {
private: private:
friend class PerformedAtExit; friend class PerformedAtExit;
static void detectAllLeaks () { } public:
static void checkForLeaks () { }
}; };
template <class Object> template <class Object>
@@ -171,6 +180,18 @@ struct LeakChecked : LeakCheckedBase
{ {
}; };
}
//------------------------------------------------------------------------------
// Lift the corresponding implementation
//
#if BEAST_CHECK_MEMORY_LEAKS
using Implemented::LeakChecked;
using Implemented::LeakCheckedBase;
#else
using Dummy::LeakChecked;
using Dummy::LeakCheckedBase;
#endif #endif
#endif #endif

View File

@@ -23,7 +23,7 @@
// Allows turning off of all padding, // Allows turning off of all padding,
// e.g. for memory-constrained systems or testing. // e.g. for memory-constrained systems or testing.
// //
#define GLOBAL_PADDING_ENABLED 0 #define GLOBAL_PADDING_ENABLED 1
namespace CacheLine namespace CacheLine
{ {
@@ -32,77 +32,76 @@ namespace CacheLine
// Pads an object so that it starts on a cache line boundary. // Pads an object so that it starts on a cache line boundary.
// //
/** Pad an object to start on a cache line boundary.
Up to 8 constructor parameters are passed through.
*/
template <typename T> template <typename T>
class Aligned class Aligned
{ {
public: public:
~Aligned ()
{
ptr ()->~T ();
}
Aligned () Aligned ()
{ {
new (ptr ()) T; new (ptr ()) T;
} }
template <class T1> template <class T1>
explicit Aligned (const T1& t1) Aligned (T1 t1)
{ {
new (ptr ()) T (t1); new (ptr ()) T (t1);
} }
template <class T1, class T2> template <class T1, class T2>
Aligned (const T1& t1, const T2& t2) Aligned (T1 t1, T2 t2)
{ {
new (ptr ()) T (t1, t2); new (ptr ()) T (t1, t2);
} }
template <class T1, class T2, class T3> template <class T1, class T2, class T3>
Aligned (const T1& t1, const T2& t2, const T3& t3) Aligned (T1 t1, T2 t2, T3 t3)
{ {
new (ptr ()) T (t1, t2, t3); new (ptr ()) T (t1, t2, t3);
} }
template <class T1, class T2, class T3, class T4> template <class T1, class T2, class T3, class T4>
Aligned (const T1& t1, const T2& t2, const T3& t3, const T4& t4) Aligned (T1 t1, T2 t2, T3 t3, T4 t4)
{ {
new (ptr ()) T (t1, t2, t3, t4); new (ptr ()) T (t1, t2, t3, t4);
} }
template <class T1, class T2, class T3, class T4, class T5> template <class T1, class T2, class T3, class T4, class T5>
Aligned (const T1& t1, const T2& t2, const T3& t3, Aligned (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
const T4& t4, const T5& t5)
{ {
new (ptr ()) T (t1, t2, t3, t4, t5); new (ptr ()) T (t1, t2, t3, t4, t5);
} }
template <class T1, class T2, class T3, class T4, class T5, class T6> template <class T1, class T2, class T3, class T4, class T5, class T6>
Aligned (const T1& t1, const T2& t2, const T3& t3, Aligned (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6)
const T4& t4, const T5& t5, const T6& t6)
{ {
new (ptr ()) T (t1, t2, t3, t4, t5, t6); new (ptr ()) T (t1, t2, t3, t4, t5, t6);
} }
template < class T1, class T2, class T3, class T4, template <class T1, class T2, class T3, class T4, class T5, class T6, class T7>
class T5, class T6, class T7 > Aligned (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7)
Aligned (const T1& t1, const T2& t2, const T3& t3, const T4& t4,
const T5& t5, const T6& t6, const T7& t7)
{ {
new (ptr ()) T (t1, t2, t3, t4, t5, t6, t7); new (ptr ()) T (t1, t2, t3, t4, t5, t6, t7);
} }
template < class T1, class T2, class T3, class T4, template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
class T5, class T6, class T7, class T8 > Aligned (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8)
Aligned (const T1& t1, const T2& t2, const T3& t3, const T4& t4,
const T5& t5, const T6& t6, const T7& t7, const T8& t8)
{ {
new (ptr ()) T (t1, t2, t3, t4, t5, t6, t7, t8); new (ptr ()) T (t1, t2, t3, t4, t5, t6, t7, t8);
} }
void operator= (T const& other) ~Aligned ()
{
ptr ()->~T ();
}
T& operator= (T const& other)
{ {
*ptr () = other; *ptr () = other;
return *ptr ();
} }
inline T& operator* () noexcept { return *ptr (); } inline T& operator* () noexcept { return *ptr (); }
@@ -110,22 +109,10 @@ public:
inline operator T& () noexcept { return *ptr (); } inline operator T& () noexcept { return *ptr (); }
inline operator T* () noexcept { return ptr (); } inline operator T* () noexcept { return ptr (); }
inline const T& operator* () const noexcept inline const T& operator* () const noexcept { return *ptr (); }
{ inline const T* operator-> () const noexcept { return ptr (); }
return *ptr (); inline operator T const& () const noexcept { return *ptr (); }
} inline operator T const* () const noexcept { return ptr (); }
inline const T* operator-> () const noexcept
{
return ptr ();
}
inline operator const T& () const noexcept
{
return *ptr ();
}
inline operator const T* () const noexcept
{
return ptr ();
}
private: private:
inline T* ptr () noexcept inline T* ptr () noexcept
@@ -138,85 +125,85 @@ private:
*/ */
} }
char m_storage [ (sizeof (T) + Memory::cacheLineAlignMask) char m_storage [ (sizeof (T) + Memory::cacheLineAlignMask) & ~Memory::cacheLineAlignMask];
& ~Memory::cacheLineAlignMask];
}; };
// Holds an object padded it to completely fill a CPU cache line. /** End-pads an object to completely fill straddling CPU cache lines.
// The caller must ensure that this object starts at the beginning
// of a cache line. The caller must ensure that this object starts at the beginning
// of a cache line.
*/
template <typename T> template <typename T>
class Padded class Padded
{ {
public: public:
Padded () Padded ()
{ } {
}
template <class T1> template <class T1>
explicit Padded (const T1& t1) explicit Padded (T1 t1)
: m_t (t1) { } : m_t (t1)
{
}
template <class T1, class T2> template <class T1, class T2>
Padded (const T1& t1, const T2& t2) Padded (T1 t1, T2 t2)
: m_t (t1, t2) { } : m_t (t1, t2)
{
}
template <class T1, class T2, class T3> template <class T1, class T2, class T3>
Padded (const T1& t1, const T2& t2, const T3& t3) Padded (T1 t1, T2 t2, T3 t3)
: m_t (t1, t2, t3) { } : m_t (t1, t2, t3)
{
}
template <class T1, class T2, class T3, class T4> template <class T1, class T2, class T3, class T4>
Padded (const T1& t1, const T2& t2, const T3& t3, const T4& t4) Padded (T1 t1, T2 t2, T3 t3, T4 t4)
: m_t (t1, t2, t3, t4) { } : m_t (t1, t2, t3, t4)
{
}
template <class T1, class T2, class T3, class T4, class T5> template <class T1, class T2, class T3, class T4, class T5>
Padded (const T1& t1, const T2& t2, const T3& t3, Padded (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
const T4& t4, const T5& t5) : m_t (t1, t2, t3, t4, t5)
: m_t (t1, t2, t3, t4, t5) { } {
}
template <class T1, class T2, class T3, class T4, class T5, class T6> template <class T1, class T2, class T3, class T4, class T5, class T6>
Padded (const T1& t1, const T2& t2, const T3& t3, Padded (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6)
const T4& t4, const T5& t5, const T6& t6) : m_t (t1, t2, t3, t4, t5, t6)
: m_t (t1, t2, t3, t4, t5, t6) { } {
}
template < class T1, class T2, class T3, class T4, template <class T1, class T2, class T3, class T4, class T5, class T6, class T7>
class T5, class T6, class T7 > Padded (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7)
Padded (const T1& t1, const T2& t2, const T3& t3, const T4& t4, : m_t (t1, t2, t3, t4, t5, t6, t7)
const T5& t5, const T6& t6, const T7& t7) {
: m_t (t1, t2, t3, t4, t5, t6, t7) { } }
template < class T1, class T2, class T3, class T4, template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
class T5, class T6, class T7, class T8 > Padded (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8)
Padded (const T1& t1, const T2& t2, const T3& t3, const T4& t4, : m_t (t1, t2, t3, t4, t5, t6, t7, t8)
const T5& t5, const T6& t6, const T7& t7, const T8& t8) {
: m_t (t1, t2, t3, t4, t5, t6, t7, t8) { } }
void operator= (const T& other) T& operator= (T const& other)
{ {
m_t = other; m_t = other;
return m_t;
} }
T& operator* () noexcept { return m_t; } T& operator* () noexcept { return m_t; }
T* operator-> () noexcept { return &m_t; } T* operator-> () noexcept { return &m_t; }
operator T& () noexcept { return m_t; } operator T& () noexcept { return m_t; }
operator T* () noexcept { return &m_t; } operator T* () noexcept { return &m_t; }
const T& operator* () const noexcept const T& operator* () const noexcept { return m_t; }
{ const T* operator-> () const noexcept { return &m_t; }
return m_t; operator T const& () const noexcept { return m_t; }
} operator T const* () const noexcept { return &m_t; }
const T* operator-> () const noexcept
{
return &m_t;
}
operator const T& () const noexcept
{
return m_t;
}
operator const T* () const noexcept
{
return &m_t;
}
private: private:
T m_t; T m_t;

View File

@@ -25,6 +25,8 @@ public:
private: private:
~ExitHook () ~ExitHook ()
{ {
// Call all PerformedAtExit objects
//
PerformedAtExit* object = s_list->pop_front (); PerformedAtExit* object = s_list->pop_front ();
while (object != nullptr) while (object != nullptr)
@@ -34,7 +36,9 @@ private:
object = s_list->pop_front (); object = s_list->pop_front ();
} }
LeakCheckedBase::detectAllLeaks (); // Now do the leak checking
//
LeakCheckedBase::checkForLeaks ();
} }
public: public:

View File

@@ -36,6 +36,9 @@
// //
class BEAST_API PerformedAtExit : public LockFreeStack <PerformedAtExit>::Node class BEAST_API PerformedAtExit : public LockFreeStack <PerformedAtExit>::Node
{ {
public:
class ExitHook;
protected: protected:
PerformedAtExit (); PerformedAtExit ();
virtual ~PerformedAtExit () { } virtual ~PerformedAtExit () { }
@@ -44,9 +47,6 @@ protected:
/** Called at program exit. /** Called at program exit.
*/ */
virtual void performAtExit () = 0; virtual void performAtExit () = 0;
private:
class ExitHook;
}; };
#endif #endif