"L. Spiro Engine"
|
00001 00016 #ifndef __LSA_MEMLIB_H__ 00017 #define __LSA_MEMLIB_H__ 00018 00019 #include "OSHeap/LSAOsHeap.h" 00020 #include "Allocators/LSAStdAllocator.h" 00021 00022 00023 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00024 // MACROS 00025 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00026 // It is better to call the full version of new, as: 00027 // new( __FILE__, __LINE__ ). 00028 // Use the LSENEW macro to make this easier. 00029 #define LSENEW new( __FILE__, __LINE__ ) 00030 #define LSEDELETE delete 00031 00032 #if !defined( __CRTDECL ) 00033 #define __CRTDECL 00034 #endif // #if !defined( __CRTDECL ) 00035 00036 namespace lsa { 00037 00043 class CMemLib { 00044 public : 00045 // == Functions. 00053 static LSBOOL LSE_CALL Init( LSA_SIZE _sDefaultSize, LSBOOL _bGrowable ); 00054 00058 static LSVOID LSE_CALL Destroy(); 00059 00068 static CStdAllocator * LSE_CALL GetAllocatorD( LSA_SIZE _sDefaultSize, LSBOOL _bGrowable LSA_DEBUGPARMSDECL ); 00069 00076 static LSBOOL LSE_CALL DestroyAllocator( CStdAllocator * _psaAllocator ); 00077 00085 static LSVOID * LSE_CALL AllocD( LSA_SIZE _sSize, LSUINT32 _ulAlign = 0 LSA_DEBUGPARMSDECL ); 00086 00094 static LSVOID * LSE_CALL CAllocD( LSA_SIZE _sSize, LSUINT32 _ulAlign = 0 LSA_DEBUGPARMSDECL ); 00095 00102 static LSBOOL LSE_CALL Free( LSVOID * _pvAddr ); 00103 00112 static LSVOID * LSE_CALL ReAllocD( LSVOID * _pvAddr, LSA_SIZE _sSize LSA_DEBUGPARMSDECL ); 00113 00119 static LSBOOL LSE_CALL Ready(); 00120 00124 static LSVOID LSE_CALL ReleaseEmptyPools(); 00125 00133 static LSUINT32 LSE_CALL GetAllocationCounter(); 00134 00144 static LSA_SIZE LSE_CALL PrintAllocations( LSUINT32 _ui32Count = 0UL, LSUINT32 _ui32End = 0xFFFFFFFFUL ); 00145 00153 static LSA_SIZE LSE_CALL GetTotalAllocatedSize(); 00154 00162 template <class _cType> 00163 static _cType * LSE_CALL New( CStdAllocator * _psaAllocator LSA_DEBUGPARMSDECL ); 00164 00171 template <class _cType> 00172 static LSVOID LSE_CALL Delete( CStdAllocator * _psaAllocator, _cType * _pcObject ); 00173 00174 #ifdef LSA_DEBUG 00175 static CStdAllocator * LSE_CALL GetThisAllocator() { return &m_saAllocator; } 00176 #endif // #ifdef LSA_DEBUG 00177 00178 #ifdef LSA_DEBUG 00179 #define MAllocAligned( _sSize, _ulAlign ) AllocD( _sSize, _ulAlign, __FILE__, __LINE__ ) 00180 #define MAlloc( _sSize ) AllocD( _sSize, 0, __FILE__, __LINE__ ) 00181 #define CMAllocAligned( _sSize, _ulAlign ) CAllocD( _sSize, _ulAlign, __FILE__, __LINE__ ) 00182 #define CMAlloc( _sSize ) CAllocD( _sSize, 0, __FILE__, __LINE__ ) 00183 #define MReAlloc( _pvAddr, _sSize ) ReAllocD( _pvAddr, _sSize, __FILE__, __LINE__ ) 00184 #define MGetAllocator( _sDefaultSize, _bGrowable ) GetAllocatorD( _sDefaultSize, _bGrowable, __FILE__, __LINE__ ) 00185 #define MNew( _psaAllocator, _cType ) New<_cType>( _psaAllocator, __FILE__, __LINE__ ) 00186 #define MDelete( _psaAllocator, _cType, _pcObject ) Delete<_cType>( _psaAllocator, _pcObject ) 00187 #else // #ifdef LSA_DEBUG 00188 #define MAllocAligned( _sSize, _ulAlign ) AllocD( _sSize, _ulAlign ) 00189 #define MAlloc( _sSize ) AllocD( _sSize, 0 ) 00190 #define CMAllocAligned( _sSize, _ulAlign ) CAllocD( _sSize, _ulAlign ) 00191 #define CMAlloc( _sSize ) CAllocD( _sSize, 0 ) 00192 #define MReAlloc( _pvAddr, _sSize ) ReAllocD( _pvAddr, _sSize ) 00193 #define MGetAllocator( _sDefaultSize, _bGrowable ) GetAllocatorD( _sDefaultSize, _bGrowable ) 00194 #define MNew( _psaAllocator, _cType ) New<_cType>( _psaAllocator ) 00195 #define MDelete( _psaAllocator, _cType, _pcObject ) Delete<_cType>( _psaAllocator, _pcObject ) 00196 #endif // #ifdef LSA_DEBUG 00197 00198 private : 00199 // == Members. 00203 static CStdAllocator m_saAllocator; 00204 00208 static LSBOOL m_bAllocatorReady; 00209 }; 00210 00211 00212 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00213 // DEFINITIONS 00214 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00215 // == Functions. 00221 LSE_INLINE LSBOOL LSE_CALL CMemLib::Ready() { 00222 return m_bAllocatorReady; 00223 } 00224 00232 template <class _cType> _cType * LSE_CALL CMemLib::New( CStdAllocator * _psaAllocator LSA_DEBUGPARMSDEF ) { 00233 if ( !_psaAllocator ) { return LSENEW _cType(); } 00234 LSVOID * pvNew = _psaAllocator->Alloc( sizeof( _cType ), 16UL LSA_DEBUGPARMSPASS ); 00235 if ( !pvNew ) { return NULL; } 00236 return new( pvNew ) _cType(); 00237 } 00238 00245 template <class _cType> LSVOID LSE_CALL CMemLib::Delete( CStdAllocator * _psaAllocator, _cType * _pcObject ) { 00246 if ( !_pcObject ) { return; } 00247 if ( !_psaAllocator ) { 00248 LSEDELETE _pcObject; 00249 return; 00250 } 00251 _pcObject->~_cType(); 00252 _psaAllocator->Free( _pcObject ); 00253 } 00254 00255 } // namespace lsa 00256 00257 00258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00259 // OVERRIDES 00260 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00261 #ifdef __GNUC__ 00262 #define LSA_THROW throw() 00263 #define LSA_THROW_BAD throw( std::bad_alloc ) 00264 #else 00265 #define LSA_THROW 00266 #define LSA_THROW_BAD 00267 #endif // #ifdef __GNUC__ 00268 // Standard new. 00269 void * __CRTDECL operator new( size_t _stSize, const LSCHAR * _pcFile, lsstd::LSUINT32 _ui32Line ); 00270 void * __CRTDECL operator new( size_t _stSize ) LSA_THROW_BAD; 00271 // Standard delete. 00272 void __CRTDECL operator delete( void * _pvAddr, const LSCHAR * _pcFile, lsstd::LSUINT32 _ui32Line ); 00273 void __CRTDECL operator delete( void * _pvAddr ) LSA_THROW; 00274 00275 // Array new. 00276 void * __CRTDECL operator new [] ( size_t _stSize, const LSCHAR * _pcFile, lsstd::LSUINT32 _ui32Line ); 00277 void * __CRTDECL operator new [] ( size_t _stSize ) LSA_THROW_BAD; 00278 // Array delete. 00279 void __CRTDECL operator delete [] ( void * _pvAddr, const LSCHAR * _pcFile, lsstd::LSUINT32 _ui32Line ); 00280 void __CRTDECL operator delete [] ( void * _pvAddr ) LSA_THROW; 00281 00282 // No-throw new. 00283 void * __CRTDECL operator new( size_t _stSize, const std::nothrow_t &_tNoThrow, const LSCHAR * _pcFile, lsstd::LSUINT32 _ui32Line ); 00284 void * __CRTDECL operator new( size_t _stSize, const std::nothrow_t &_tNoThrow ) LSA_THROW; 00285 // No-throw delete. 00286 void __CRTDECL operator delete( void * _pvAddr, const std::nothrow_t &_tNoThrow, const LSCHAR * _pcFile, lsstd::LSUINT32 _ui32Line ); 00287 void __CRTDECL operator delete( void * _pvAddr, const std::nothrow_t &_tNoThrow ) LSA_THROW; 00288 00289 // Array no-throw new. 00290 void * __CRTDECL operator new [] ( size_t _stSize, const std::nothrow_t &_tNoThrow, const LSCHAR * _pcFile, lsstd::LSUINT32 _ui32Line ); 00291 void * __CRTDECL operator new [] ( size_t _stSize, const std::nothrow_t &_tNoThrow ) LSA_THROW; 00292 // Array no-throw delete. 00293 void __CRTDECL operator delete [] ( void * _pvAddr, const std::nothrow_t &_tNoThrow, const LSCHAR * _pcFile, lsstd::LSUINT32 _ui32Line ); 00294 void __CRTDECL operator delete [] ( void * _pvAddr, const std::nothrow_t &_tNoThrow ) LSA_THROW; 00295 00296 #endif // __LSA_MEMLIB_H__