EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SafeMem.h File Reference
#include <cstddef>
#include <type_traits>
#include <memory>
#include <utility>
#include <memory.h>
#include <stdlib.h>
Include dependency graph for SafeMem.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  myPtr< T >
 

Functions

template<typename T >
T * SafeAlloc ()
 Allocates and zero-initializes memory. More...
 
template<typename T >
T * SafeAllocArray (size_t size)
 Allocates and zero-initializes an array. More...
 
template<typename T >
void SafeDelete (T *&p)
 Deletes and nullifies a pointer. More...
 
template<typename T >
void SafeDeleteArray (T *&p)
 Deletes and nullifies an array pointer. More...
 
template<typename T >
void SafeFree (T *&p)
 Frees and nullifies an array pointer. More...
 
template<typename T , typename F >
void SafeRelease (T *&p, F f)
 Releases pointer and nullifies it. More...
 
template<class T , class... Args>
std::unique_ptr< T > make_unique (Args &&...args)
 
template<class T >
std::unique_ptr< T > make_uniquea (size_t n)
 

Function Documentation

template<class T , class... Args>
std::unique_ptr<T> make_unique ( Args &&...  args)

Definition at line 137 of file SafeMem.h.

References args.

137  {
138  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
139 }
* args
template<class T >
std::unique_ptr<T> make_uniquea ( size_t  n)

Definition at line 143 of file SafeMem.h.

143  {
144  //typedef typename remove_extent<T>::type U;
145  return std::unique_ptr<T>(new T[n]());
146 }
template<typename T >
T* SafeAlloc ( )
inline

Allocates and zero-initializes memory.

Useful for allocating constructor-less C structs.

Returns
A pointer to the allocated memory.

Definition at line 45 of file SafeMem.h.

46 {
47  T* p = new T;
48  ::memset( p, 0, sizeof( T ) );
49 
50  return p;
51 }
template<typename T >
T* SafeAllocArray ( size_t  size)
inline

Allocates and zero-initializes an array.

Useful for allocating constructor-less C struct arrays.

Parameters
[in]sizeDesired size of the array.
Returns
A pointer to the array.

Definition at line 63 of file SafeMem.h.

64 {
65  T* p = new T[ size ];
66  ::memset( p, 0, size * sizeof( T ) );
67 
68  return p;
69 }
template<typename T >
void SafeDelete ( T *&  p)
inline

Deletes and nullifies a pointer.

Basic programming tips URL: http://nedprod.com/programs/index.html Note: always nullify pointers after deletion, why? because its safer on a MT application

(Allan) by setting a pointer to the null pointer, you have successfully eliminated the only chance to detect double deletion

Parameters
[in]pThe pointer.

Definition at line 83 of file SafeMem.h.

Referenced by EVEClientSession::_HandlePacket(), PlanetDataMgr::_Populate(), PIDataMgr::_Populate(), CachedObjectMgr::_UpdateCache(), Colony::AbandonColony(), CommandDispatcher::AddCommand(), SystemManager::AddMarker(), Client::Board(), TradeService::CancelTrade(), BubbleManager::clear(), SystemBubble::clear(), BeltMgr::ClearAll(), StreamPacketizer::ClearBuffers(), TCPConnection::ClearBuffers(), TargetManager::ClearFromTargets(), XMLParser::ClearParsers(), TargetManager::ClearTargets(), CommandDispatcher::Close(), ItemFactory::Close(), PyServiceMgr::Close(), BaseTCPServer::Close(), EntityList::Close(), StatisticMgr::CompileData(), TCPConnection::Connect(), CynoModule::DeactivateCycle(), ActiveModule::DeactivateCycle(), Client::DestroyShipSE(), TCPConnection::DoDisconnect(), PySubStream::EncodeData(), EntityList::FindOrBootSystem(), DestinyManager::Halt(), DestinyManager::InitWarp(), CachedObjectMgr::InvalidateCache(), SystemEntity::Killed(), CachedObjectMgr::LoadCachedCall(), CachedObjectMgr::LoadCachedFile(), CachedObjectMgr::LoadCachedFromFile(), CachedObjectMgr::LoadCachedObject(), ShipItem::LoadWeaponGroups(), Marshal(), MarshalDeflate(), ObjectToSQL(), BaseTCPServer::Open(), EVETCPConnection::PopRep(), FxDataMgr::Populate(), MapData::Populate(), MissionDataMgr::Populate(), SovereigntyDataMgr::Populate(), StationDataMgr::Populate(), DungeonDataMgr::Populate(), StaticDataMgr::Populate(), EntityList::Process(), MiningLaser::ProcessCycle(), Client::ProcessNet(), EVETCPConnection::QueueRep(), SystemBubble::RemoveMarkers(), XMLParser::RemoveParser(), TargetManager::RemoveTarget(), SpawnMgr::ReSpawn(), TCPConnection::Send(), SystemBubble::SendAddBalls(), SystemBubble::SendAddBalls2(), TCPConnection::SendData(), SystemManager::SendStaticBall(), TowerSE::SetOffline(), EntityList::Shutdown(), LSCService::SystemUnload(), TargetManager::TargetedByLost(), TargetManager::TargetLost(), ModuleManager::UnfitModule(), TargetManager::Unload(), SystemManager::UnloadSystem(), Unmarshal(), CachedObjectMgr::UpdateCache(), SovereigntyDataMgr::UpdateClaim(), DestinyManager::WarpStop(), DestinyManager::WarpTo(), AlertService::~AlertService(), CachedObjectMgr::~CachedObjectMgr(), CargoContainer::~CargoContainer(), Character::~Character(), Client::~Client(), Colony::~Colony(), CommandDispatcher::~CommandDispatcher(), Concord::~Concord(), ContainerSE::~ContainerSE(), DestinyManager::~DestinyManager(), DroneSE::~DroneSE(), DynamicSystemEntity::~DynamicSystemEntity(), InventoryItem::~InventoryItem(), ItemFactory::~ItemFactory(), LSCService::~LSCService(), ModuleManager::~ModuleManager(), NPC::~NPC(), ObjectSystemEntity::~ObjectSystemEntity(), PlanetMgrBound::~PlanetMgrBound(), PlanetSE::~PlanetSE(), ReactorSE::~ReactorSE(), Sentry::~Sentry(), ShipItem::~ShipItem(), SolarSystem::~SolarSystem(), StationItem::~StationItem(), StationOffice::~StationOffice(), StructureItem::~StructureItem(), SystemBubble::~SystemBubble(), SystemManager::~SystemManager(), TCPServer< EVETCPConnection >::~TCPServer(), and WreckContainer::~WreckContainer().

84 {
85  delete p;
86  p = nullptr;
87 }
template<typename T >
void SafeDeleteArray ( T *&  p)
inline

Deletes and nullifies an array pointer.

Parameters
[in]pThe array pointer.
See also
safeDelete< T >( T*& p )

Definition at line 97 of file SafeMem.h.

Referenced by DBQueryResult::Reset(), and DBQueryResult::~DBQueryResult().

98 {
99  delete[] p;
100  p = nullptr;
101 }

Here is the caller graph for this function:

template<typename T >
void SafeFree ( T *&  p)
inline

Frees and nullifies an array pointer.

Parameters
[in]pThe pointer.
See also
safeDelete< T >( T*& p )

Definition at line 111 of file SafeMem.h.

Referenced by PyPfxVisitor::_pfxExtend(), CustomError::CustomError(), Client::SelfChatMessage(), Client::SelfEveMail(), Client::SendErrorMsg(), Client::SendInfoModalMsg(), Client::SendNotifyMsg(), and Buffer::~Buffer().

112 {
113  ::free( p );
114  p = nullptr;
115 }

Here is the caller graph for this function:

template<typename T , typename F >
void SafeRelease ( T *&  p,
F  f 
)
inline

Releases pointer and nullifies it.

Parameters
[in]pThe pointer.
[in]fThe release function.

Definition at line 124 of file SafeMem.h.

125 {
126  /* We don't know if the function accepts
127  NULL pointers, like delete or free. */
128  if( NULL != p )
129  f( p );
130 
131  p = nullptr;
132 }