Skip to content

Commit

Permalink
- added mail removal prevention mechanic to the mailbox template to p…
Browse files Browse the repository at this point in the history
…revent issue with mail being removed and deleted while a Peek() has an outstanding pointer to data

- some generic code updates, like adding more move constructors to more classes
  • Loading branch information
LiberatorUSA committed Sep 2, 2023
1 parent fd56cec commit 2f24399
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 122 deletions.
2 changes: 2 additions & 0 deletions common/include/gucef_basichelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#define GUCEF_PLATFORM_H
#endif /* GUCEF_PLATFORM_H ? */

#include <assert.h>

/*-------------------------------------------------------------------------//
// //
// GENERAL MACROS //
Expand Down
8 changes: 8 additions & 0 deletions common/include/gucef_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@

/*-------------------------------------------------------------------------*/

#ifdef GUCEF_RVALUE_REFERENCES_SUPPORTED
#define GUCEF_MOVE( m ) std::move( m )
#else
#define GUCEF_MOVE( m ) m
#endif

/*-------------------------------------------------------------------------*/

#if defined(__cplusplus)
#undef GUCEF_NOEXCEPT_IS_SUPPORTED
#if __cplusplus > 201103L /* >= C++ 11 : This is not fullproof since not every compiler truly supports the spec */
Expand Down
6 changes: 5 additions & 1 deletion platform/gucefCORE/include/CDataNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,14 @@ class GUCEF_CORE_PUBLIC_CPP CDataNode : public CIEnumerable
*/
CDataNode( const CDataNode& src );

#ifdef GUCEF_RVALUE_REFERENCES_SUPPORTED
CDataNode( CDataNode&& src ) GUCEF_NOEXCEPT;
#endif

/**
* Cleans up all attributes and children
*/
virtual ~CDataNode();
virtual ~CDataNode() GUCEF_VIRTUAL_OVERRIDE;

/**
* Turns the given node and it's sub-tree into an replica
Expand Down
14 changes: 9 additions & 5 deletions platform/gucefCORE/include/CEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ class GUCEF_CORE_PUBLIC_CPP CEvent
{
public:

CEvent( void );
CEvent( void ) GUCEF_NOEXCEPT;

CEvent( const CString& eventName );
CEvent( const CString& eventName ) GUCEF_NOEXCEPT;

CEvent( const char* eventName );
CEvent( const char* eventName ) GUCEF_NOEXCEPT;

CEvent( const CEvent& src );
CEvent( const CEvent& src ) GUCEF_NOEXCEPT;

#ifdef GUCEF_RVALUE_REFERENCES_SUPPORTED
CEvent( CEvent&& src ) GUCEF_NOEXCEPT;
#endif

~CEvent();

Expand Down Expand Up @@ -113,7 +117,7 @@ class GUCEF_CORE_PUBLIC_CPP CEvent
friend class CNotificationIDRegistry;

CEvent( const UInt32 eventID ,
const CString& eventName );
const CString& eventName ) GUCEF_NOEXCEPT;

private:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class GUCEF_CORE_PUBLIC_CPP CGenericValueToDataNodeSerializer : public CIValueTo

CGenericValueToDataNodeSerializer( const CGenericValueToDataNodeSerializer& src );

virtual ~CGenericValueToDataNodeSerializer();
virtual ~CGenericValueToDataNodeSerializer() GUCEF_VIRTUAL_OVERRIDE;

CGenericValueToDataNodeSerializer& operator=( const CGenericValueToDataNodeSerializer& src );

Expand Down
27 changes: 27 additions & 0 deletions platform/gucefCORE/src/CDataNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ CDataNode::CDataNode( const CDataNode& src )
}
}

/*-------------------------------------------------------------------------*/

#ifdef GUCEF_RVALUE_REFERENCES_SUPPORTED

CDataNode::CDataNode( CDataNode&& src ) GUCEF_NOEXCEPT
: CIEnumerable()
, m_nodeType( src.m_nodeType )
, _name( GUCEF_MOVE( src._name ) )
, m_value( GUCEF_MOVE( src.m_value ) )
, _atts( GUCEF_MOVE( src._atts ) )
, _pparent( src._pparent )
, _pnext( src._pnext )
, _pprev( src._pprev )
, m_children( GUCEF_MOVE( src.m_children ) )
, m_associatedData( src.m_associatedData )
{GUCEF_TRACE;

// reset source to default constructor values
src.m_nodeType = GUCEF_DATATYPE_OBJECT;
src._pparent = GUCEF_NULL;
src._pnext = GUCEF_NULL;
src._pprev = GUCEF_NULL;
src.m_associatedData = GUCEF_NULL;
}

#endif /* GUCEF_RVALUE_REFERENCES_SUPPORTED ? */

/*-------------------------------------------------------------------------*/

CDataNode::~CDataNode()
Expand Down
25 changes: 21 additions & 4 deletions platform/gucefCORE/src/CDynamicBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ CDynamicBuffer::CDynamicBuffer( UInt32 initialsize ,
if ( initialsize > 0 )
{
_buffer = (Int8*) malloc( initialsize );
_bsize = initialsize;
if ( GUCEF_NULL != _buffer )
{
_bsize = initialsize;
}
}
}

Expand All @@ -150,8 +153,15 @@ CDynamicBuffer::CDynamicBuffer( const CDynamicBuffer &src ,
_bsize = src.m_dataSize;
if ( _bsize > 0 )
{
_buffer = (Int8*) malloc( src.m_dataSize );
memcpy( _buffer, src._buffer, m_dataSize );
_buffer = (Int8*) malloc( src.m_dataSize );
if ( GUCEF_NULL != _buffer )
{
memcpy( _buffer, src._buffer, m_dataSize );
}
else
{
_bsize = m_dataSize = 0;
}
}
}
else
Expand All @@ -160,7 +170,14 @@ CDynamicBuffer::CDynamicBuffer( const CDynamicBuffer &src ,
if ( _bsize > 0 )
{
_buffer = (Int8*) malloc( src._bsize );
memcpy( _buffer, src._buffer, m_dataSize );
if ( GUCEF_NULL != _buffer )
{
memcpy( _buffer, src._buffer, m_dataSize );
}
else
{
_bsize = m_dataSize = 0;
}
}
}
}
Expand Down
27 changes: 21 additions & 6 deletions platform/gucefCORE/src/CEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ namespace CORE {
// //
//-------------------------------------------------------------------------*/

CEvent::CEvent( void )
CEvent::CEvent( void ) GUCEF_NOEXCEPT
: m_eventID( 0 ) ,
m_eventName()
{GUCEF_TRACE;
}

/*-------------------------------------------------------------------------*/

CEvent::CEvent( const char* eventName )
CEvent::CEvent( const char* eventName ) GUCEF_NOEXCEPT
: m_eventID( 0 ) ,
m_eventName( eventName )
{GUCEF_TRACE;
Expand All @@ -75,7 +75,7 @@ CEvent::CEvent( const char* eventName )

/*-------------------------------------------------------------------------*/

CEvent::CEvent( const CORE::CString& eventName )
CEvent::CEvent( const CORE::CString& eventName ) GUCEF_NOEXCEPT
: m_eventID( 0 ) ,
m_eventName( eventName )
{GUCEF_TRACE;
Expand All @@ -85,8 +85,23 @@ CEvent::CEvent( const CORE::CString& eventName )

/*-------------------------------------------------------------------------*/

#ifdef GUCEF_RVALUE_REFERENCES_SUPPORTED

CEvent::CEvent( CEvent&& src ) GUCEF_NOEXCEPT
: m_eventID( src.m_eventID ) ,
m_eventName( GUCEF_MOVE( src.m_eventName ) )
{GUCEF_TRACE;

// reset source to default constructor values
src.m_eventID = 0;
}

#endif /* GUCEF_RVALUE_REFERENCES_SUPPORTED ? */

/*-------------------------------------------------------------------------*/

CEvent::CEvent( const UInt32 eventID ,
const CString& eventName )
const CString& eventName ) GUCEF_NOEXCEPT
: m_eventID( eventID ) ,
m_eventName( eventName )
{GUCEF_TRACE;
Expand All @@ -95,8 +110,8 @@ CEvent::CEvent( const UInt32 eventID ,

/*-------------------------------------------------------------------------*/

CEvent::CEvent( const CEvent& src )
: m_eventID( src.m_eventID ) ,
CEvent::CEvent( const CEvent& src ) GUCEF_NOEXCEPT
: m_eventID( src.m_eventID ) ,
m_eventName( src.m_eventName )
{GUCEF_TRACE;

Expand Down
55 changes: 33 additions & 22 deletions platform/gucefCORE/src/CPumpedObserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,12 @@ CPumpedObserver::ClearMailbox( bool acceptNewMail )
&dataptr ) )
{
maildata = static_cast< CMailElement* >( dataptr );
GUCEF_DELETE maildata->GetCallback();
GUCEF_DELETE maildata->GetData();
GUCEF_DELETE maildata;
if ( GUCEF_NULL != maildata )
{
GUCEF_DELETE maildata->GetCallback();
GUCEF_DELETE maildata->GetData();
GUCEF_DELETE maildata;
}
}

if ( acceptNewMail )
Expand Down Expand Up @@ -338,33 +341,41 @@ CPumpedObserver::OnPulse( CNotifier* notifier ,

if ( m_notificationsHolds <= 0 )
{
MT::CTMailBox< CEvent >::TMailBoxMailRemovalBlock mailRemovalBlock( m_mailbox );

CEvent mailEventID;
CICloneable* dataptr( GUCEF_NULL );
CMailElement* maildata( GUCEF_NULL );
while ( m_mailbox.PeekMail( mailEventID ,
&dataptr ) )
while ( m_mailbox.PeekMail( mailRemovalBlock ,
mailEventID ,
&dataptr ) )
{
try
{
maildata = static_cast< CMailElement* >( dataptr );
CIEventHandlerFunctorBase* callback = maildata->GetCallback();
if ( GUCEF_NULL == callback )
{
OnPumpedNotify( maildata->GetNotifier() ,
mailEventID ,
maildata->GetData() );
}
else
if ( GUCEF_NULL != maildata )
{
OnPumpedFunctorNotify( maildata->GetNotifier() ,
mailEventID ,
maildata->GetData() ,
callback );

GUCEF_DELETE callback;
CICloneable* eventData = maildata->GetData();
CIEventHandlerFunctorBase* callback = maildata->GetCallback();
CNotifier* eventNotifier = maildata->GetNotifier();
if ( GUCEF_NULL == callback )
{
OnPumpedNotify( eventNotifier ,
mailEventID ,
eventData );
}
else
{
OnPumpedFunctorNotify( eventNotifier ,
mailEventID ,
eventData ,
callback );

GUCEF_DELETE callback;
}

GUCEF_DELETE eventData;
}

GUCEF_DELETE maildata->GetData();
}
catch ( const timeout_exception& )
{
Expand All @@ -373,7 +384,7 @@ CPumpedObserver::OnPulse( CNotifier* notifier ,
return;
}

m_mailbox.PopMail();
m_mailbox.PopMail( mailRemovalBlock );
}
}

Expand Down
Loading

0 comments on commit 2f24399

Please sign in to comment.