Add ContainerDeletePolicy for ownership containers

This commit is contained in:
Vinnie Falco
2013-08-17 19:39:53 -07:00
parent 503b8047b1
commit 21b78adfee
12 changed files with 144 additions and 68 deletions

View File

@@ -70,11 +70,8 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\modules\beast_asio\basics\beast_BufferType.h" />
<ClInclude Include="..\..\modules\beast_asio\basics\beast_CompletionCall.h" />
<ClInclude Include="..\..\modules\beast_asio\basics\beast_ErrorCall.h" />
<ClInclude Include="..\..\modules\beast_asio\basics\beast_FixedInputBuffer.h" />
<ClInclude Include="..\..\modules\beast_asio\basics\beast_PeerRole.h" />
<ClInclude Include="..\..\modules\beast_asio\basics\beast_TransferCall.h" />
<ClInclude Include="..\..\modules\beast_asio\beast_asio.h" />
<ClInclude Include="..\..\modules\beast_asio\handshake\beast_HandshakeDetectLogic.h" />
<ClInclude Include="..\..\modules\beast_asio\handshake\beast_HandshakeDetectLogicPROXY.h" />
@@ -183,6 +180,7 @@
<ClInclude Include="..\..\modules\beast_core\memory\beast_ByteOrder.h" />
<ClInclude Include="..\..\modules\beast_core\memory\beast_ByteSwap.h" />
<ClInclude Include="..\..\modules\beast_core\memory\beast_CacheLine.h" />
<ClInclude Include="..\..\modules\beast_core\memory\beast_ContainerDeletePolicy.h" />
<ClInclude Include="..\..\modules\beast_core\memory\beast_HeapBlock.h" />
<ClInclude Include="..\..\modules\beast_core\memory\beast_Memory.h" />
<ClInclude Include="..\..\modules\beast_core\memory\beast_MemoryAlignment.h" />

View File

@@ -815,15 +815,6 @@
<ClInclude Include="..\..\modules\beast_asio\basics\beast_BufferType.h">
<Filter>beast_asio\basics</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\basics\beast_CompletionCall.h">
<Filter>beast_asio\basics</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\basics\beast_ErrorCall.h">
<Filter>beast_asio\basics</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\basics\beast_TransferCall.h">
<Filter>beast_asio\basics</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\basics\beast_PeerRole.h">
<Filter>beast_asio\basics</Filter>
</ClInclude>
@@ -857,6 +848,12 @@
<ClInclude Include="..\..\modules\beast_asio\handshake\beast_InputParser.h">
<Filter>beast_asio\handshake</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\basics\beast_HandlerCall.h">
<Filter>beast_asio\basics</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\memory\beast_ContainerDeletePolicy.h">
<Filter>beast_core\memory</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_core\beast_core.cpp">

View File

@@ -21,7 +21,7 @@ class OncePerSecond::TimerSingleton
: public SharedSingleton <OncePerSecond::TimerSingleton>
, private InterruptibleThread::EntryPoint
{
private:
public:
TimerSingleton ()
: SharedSingleton <OncePerSecond::TimerSingleton> (
SingletonLifetime::persistAfterCreation)

View File

@@ -45,7 +45,7 @@ public:
return new GlobalFifoFreeStore;
}
private:
public:
GlobalFifoFreeStore ()
: SharedSingleton <GlobalFifoFreeStore <Tag> >
(SingletonLifetime::persistAfterCreation)

View File

@@ -30,7 +30,7 @@ class BEAST_API GlobalPagedFreeStore
: public SharedSingleton <GlobalPagedFreeStore>
, LeakChecked <GlobalPagedFreeStore>
{
private:
public:
GlobalPagedFreeStore ();
~GlobalPagedFreeStore ();

View File

@@ -227,6 +227,7 @@ namespace beast
#include "text/beast_CharPointer_UTF8.h"
#include "text/beast_LexicalCast.h"
#include "memory/beast_ContainerDeletePolicy.h"
#include "time/beast_PerformedAtExit.h"
#include "diagnostic/beast_LeakChecked.h"
#include "memory/beast_ByteOrder.h"

View File

@@ -409,9 +409,10 @@ public:
}
}
delete toDelete; // don't want to use a ScopedPointer here because if the
// object has a private destructor, both OwnedArray and
// ScopedPointer would need to be friend classes..
// don't want to use a ScopedPointer here because if the
// object has a private destructor, both OwnedArray and
// ScopedPointer would need to be friend classes..
ContainerDeletePolicy <ObjectClass>::destroy (toDelete);
}
else
{
@@ -591,9 +592,10 @@ public:
}
}
delete toDelete; // don't want to use a ScopedPointer here because if the
// object has a private destructor, both OwnedArray and
// ScopedPointer would need to be friend classes..
// don't want to use a ScopedPointer here because if the
// object has a private destructor, both OwnedArray and
// ScopedPointer would need to be friend classes..
ContainerDeletePolicy <ObjectClass>::destroy (toDelete);
if ((numUsed << 1) < data.numAllocated)
minimiseStorageOverheads();
@@ -682,7 +684,7 @@ public:
{
for (int i = startIndex; i < endIndex; ++i)
{
delete data.elements [i];
ContainerDeletePolicy <ObjectClass>::destroy (data.elements [i]);
data.elements [i] = nullptr; // (in case one of the destructors accesses this array and hits a dangling pointer)
}
}
@@ -879,7 +881,7 @@ private:
void deleteAllObjects()
{
while (numUsed > 0)
delete data.elements [--numUsed];
ContainerDeletePolicy <ObjectClass>::destroy (data.elements [--numUsed]);
}
};

View File

@@ -0,0 +1,48 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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.
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.
*/
//==============================================================================
#ifndef BEAST_CONTAINERDELETEPOLICY_H_INCLUDED
#define BEAST_CONTAINERDELETEPOLICY_H_INCLUDED
/** The DeletePolicy provides a way to destroy objects stored in containers.
This is a specialized class. The default implementation
calls operator delete. You can customize it for your classes
by providing a suitable specialization.
A DeletePolicy must support these concepts:
DefaultConstructible
MoveConstructible (C++11)
CopyConstructible
MoveAssignable (C++11)
Destructible
*/
/** The DefaultDeletePolicy simply calls operator delete.
*/
template <typename T>
struct ContainerDeletePolicy
{
static inline void destroy (T* t)
{
delete t;
}
};
#endif

View File

@@ -67,7 +67,8 @@ class ScopedPointer : public Uncopyable
public:
//==============================================================================
/** Creates a ScopedPointer containing a null pointer. */
inline ScopedPointer() noexcept : object (nullptr)
inline ScopedPointer() noexcept
: object (nullptr)
{
}
@@ -92,7 +93,10 @@ public:
/** Destructor.
This will delete the object that this ScopedPointer currently refers to.
*/
inline ~ScopedPointer() { delete object; }
inline ~ScopedPointer()
{
ContainerDeletePolicy <ObjectType>::destroy (object);
}
/** Changes this ScopedPointer to point to a new object.
@@ -114,7 +118,7 @@ public:
ObjectType* const oldObject = object;
object = objectToTransferFrom.object;
objectToTransferFrom.object = nullptr;
delete oldObject;
ContainerDeletePolicy <ObjectType>::destroy (oldObject);
}
return *this;
@@ -133,7 +137,7 @@ public:
{
ObjectType* const oldObject = object;
object = newObjectToTakePossessionOf;
delete oldObject;
ContainerDeletePolicy <ObjectType>::destroy (oldObject);
}
return *this;

View File

@@ -21,12 +21,11 @@
*/
//==============================================================================
#ifndef BEAST_REFERENCECOUNTEDOBJECT_BEASTHEADER
#define BEAST_REFERENCECOUNTEDOBJECT_BEASTHEADER
#ifndef BEAST_SHAREDOBJECT_H_INCLUDED
#define BEAST_SHAREDOBJECT_H_INCLUDED
#include "beast_Atomic.h"
//==============================================================================
/**
Adds reference-counting to an object.
@@ -75,14 +74,23 @@ public:
/** Decreases the object's reference count.
If the count gets to zero, the object will be deleted.
If doDelete is true the object will be deleted when the reference
count drops to zero. The delete is performed using the regular
operator and does NOT go through the ContainerDeletePolicy.
The return value indicates if the reference count dropped to zero,
so callers who know the derived type can use the ContainerDeletePolicy.
*/
inline void decReferenceCount() noexcept
inline bool decReferenceCount (bool doDelete = true) noexcept
{
bassert (getReferenceCount() > 0);
if (--refCount == 0)
delete this;
{
if (doDelete)
delete this;
return true;
}
return false;
}
/** Returns the object's current reference count. */
@@ -115,7 +123,6 @@ private:
Atomic <int> refCount;
};
//==============================================================================
/**
Adds reference-counting to an object.
@@ -143,16 +150,26 @@ public:
/** Decreases the object's reference count.
If the count gets to zero, the object will be deleted.
If doDelete is true the object will be deleted when the reference
count drops to zero. The delete is performed using the regular
operator and does NOT go through the ContainerDeletePolicy.
The return value indicates if the reference count dropped to zero,
so callers who know the derived type can use the ContainerDeletePolicy.
*/
inline void decReferenceCount() noexcept
inline bool decReferenceCount (bool doDelete = true) noexcept
{
bassert (getReferenceCount() > 0);
if (--refCount == 0)
delete this;
{
if (doDelete)
delete this;
return true;
}
return false;
}
/** Returns the object's current reference count. */
inline int getReferenceCount() const noexcept { return refCount; }
@@ -190,12 +207,12 @@ private:
@see SharedObject, SharedObjectArray
*/
template <class SharedObjectClass>
template <class Object>
class SharedObjectPtr
{
public:
/** The class being referenced by this pointer. */
typedef SharedObjectClass ReferencedType;
typedef Object ReferencedType;
//==============================================================================
/** Creates a pointer to a null object. */
@@ -208,7 +225,7 @@ public:
This will increment the object's reference-count if it is non-null.
*/
inline SharedObjectPtr (SharedObjectClass* const refCountedObject) noexcept
inline SharedObjectPtr (Object* const refCountedObject) noexcept
: referencedObject (refCountedObject)
{
if (refCountedObject != nullptr)
@@ -239,7 +256,7 @@ public:
*/
template <class DerivedClass>
inline SharedObjectPtr (const SharedObjectPtr<DerivedClass>& other) noexcept
: referencedObject (static_cast <SharedObjectClass*> (other.get()))
: referencedObject (static_cast <Object*> (other.get()))
{
if (referencedObject != nullptr)
referencedObject->incReferenceCount();
@@ -263,7 +280,7 @@ public:
template <class DerivedClass>
SharedObjectPtr& operator= (const SharedObjectPtr<DerivedClass>& other)
{
return operator= (static_cast <SharedObjectClass*> (other.get()));
return operator= (static_cast <Object*> (other.get()));
}
#if BEAST_COMPILER_SUPPORTS_MOVE_SEMANTICS
@@ -280,18 +297,21 @@ public:
The reference count of the old object is decremented, and it might be
deleted if it hits zero. The new object's count is incremented.
*/
SharedObjectPtr& operator= (SharedObjectClass* const newObject)
SharedObjectPtr& operator= (Object* const newObject)
{
if (referencedObject != newObject)
{
if (newObject != nullptr)
newObject->incReferenceCount();
SharedObjectClass* const oldObject = referencedObject;
Object* const oldObject = referencedObject;
referencedObject = newObject;
if (oldObject != nullptr)
oldObject->decReferenceCount();
{
if (oldObject->decReferenceCount (false))
ContainerDeletePolicy <Object>::destroy (oldObject);
}
}
return *this;
@@ -305,19 +325,22 @@ public:
inline ~SharedObjectPtr()
{
if (referencedObject != nullptr)
referencedObject->decReferenceCount();
{
if (referencedObject->decReferenceCount (false))
ContainerDeletePolicy <Object>::destroy (referencedObject);
}
}
/** Returns the object that this pointer references.
The pointer returned may be zero, of course.
*/
inline operator SharedObjectClass*() const noexcept
inline operator Object*() const noexcept
{
return referencedObject;
}
// the -> operator is called on the referenced object
inline SharedObjectClass* operator->() const noexcept
inline Object* operator->() const noexcept
{
return referencedObject;
}
@@ -325,7 +348,7 @@ public:
/** Returns the object that this pointer references.
The pointer returned may be zero, of course.
*/
inline SharedObjectClass* get() const noexcept
inline Object* get() const noexcept
{
return referencedObject;
}
@@ -333,55 +356,55 @@ public:
/** Returns the object that this pointer references.
The pointer returned may be zero, of course.
*/
inline SharedObjectClass* getObject() const noexcept
inline Object* getObject() const noexcept
{
return referencedObject;
}
private:
//==============================================================================
SharedObjectClass* referencedObject;
Object* referencedObject;
};
/** Compares two SharedObjectPointers. */
template <class SharedObjectClass>
bool operator== (const SharedObjectPtr<SharedObjectClass>& object1, SharedObjectClass* const object2) noexcept
template <class Object>
bool operator== (const SharedObjectPtr<Object>& object1, Object* const object2) noexcept
{
return object1.get() == object2;
}
/** Compares two SharedObjectPointers. */
template <class SharedObjectClass>
bool operator== (const SharedObjectPtr<SharedObjectClass>& object1, const SharedObjectPtr<SharedObjectClass>& object2) noexcept
template <class Object>
bool operator== (const SharedObjectPtr<Object>& object1, const SharedObjectPtr<Object>& object2) noexcept
{
return object1.get() == object2.get();
}
/** Compares two SharedObjectPointers. */
template <class SharedObjectClass>
bool operator== (SharedObjectClass* object1, SharedObjectPtr<SharedObjectClass>& object2) noexcept
template <class Object>
bool operator== (Object* object1, SharedObjectPtr<Object>& object2) noexcept
{
return object1 == object2.get();
}
/** Compares two SharedObjectPointers. */
template <class SharedObjectClass>
bool operator!= (const SharedObjectPtr<SharedObjectClass>& object1, const SharedObjectClass* object2) noexcept
template <class Object>
bool operator!= (const SharedObjectPtr<Object>& object1, const Object* object2) noexcept
{
return object1.get() != object2;
}
/** Compares two SharedObjectPointers. */
template <class SharedObjectClass>
bool operator!= (const SharedObjectPtr<SharedObjectClass>& object1, SharedObjectPtr<SharedObjectClass>& object2) noexcept
template <class Object>
bool operator!= (const SharedObjectPtr<Object>& object1, SharedObjectPtr<Object>& object2) noexcept
{
return object1.get() != object2.get();
}
/** Compares two SharedObjectPointers. */
template <class SharedObjectClass>
bool operator!= (SharedObjectClass* object1, SharedObjectPtr<SharedObjectClass>& object2) noexcept
template <class Object>
bool operator!= (Object* object1, SharedObjectPtr<Object>& object2) noexcept
{
return object1 != object2.get();
}

View File

@@ -139,10 +139,13 @@ public:
m_refs.addref ();
}
inline void decReferenceCount () noexcept
inline bool decReferenceCount (bool = true) noexcept
{
if (m_refs.release ())
destroySingleton ();
// We let destroySingleton enforce the ContainerDeletePolicy
return false;
}
// Caller must synchronize.
@@ -183,7 +186,7 @@ private:
{
bassert (m_lifetime != neverDestroyed);
delete this;
ContainerDeletePolicy <Object>::destroy (static_cast <Object*>(this));
}
}

View File

@@ -62,7 +62,7 @@ namespace sqdb
class session::Sqlite3 : public SharedSingleton <Sqlite3>
{
private:
public:
friend class SharedSingleton <Sqlite3>;
Sqlite3() : SharedSingleton <Sqlite3> (SingletonLifetime::persistAfterCreation)