Files
eepp/src/base/memorymanager.hpp
spartanj 613ad329cf Added cUIWindow, still in development, basic window is working ( no resizing from borders ).
Some UI bug fixes.
Some minor changes on cEngine where made.
2011-01-07 04:46:59 -03:00

106 lines
2.7 KiB
C++

#ifndef EE_MEMORY_MANAGER_HPP
#define EE_MEMORY_MANAGER_HPP
#include <string>
#include <map>
#include <cstdlib>
#include <cstdio>
namespace EE {
class cAllocatedPointer {
public:
cAllocatedPointer( void * Data, const std::string& File, int Line, size_t Memory );
std::string mFile;
int mLine;
size_t mMemory;
void * mData;
};
typedef std::map<void*, cAllocatedPointer> tAllocatedPointerMap;
typedef tAllocatedPointerMap::iterator tAllocatedPointerMapIt;
class MemoryManager {
public:
static void * AddPointer( const cAllocatedPointer& aAllocatedPointer );
static bool RemovePointer( void * Data );
static void LogResults();
template<class T>
static T* Delete( T * Data ) {
delete Data;
return Data;
}
template<class T>
static T* DeleteArray( T * Data ) {
delete [] Data;
return Data;
}
template<class T>
static T * Free( T * Data ) {
free( Data );
return Data;
}
inline static void * Allocate( size_t size ) {
return malloc( size );
}
static size_t GetPeakMemoryUsage() { return mPeakMemoryUsage; }
static size_t GetTotalMemoryUsage() { return mTotalMemoryUsage; }
static tAllocatedPointerMap mMapPointers;
static size_t mTotalMemoryUsage;
static size_t mPeakMemoryUsage;
};
#ifdef EE_MEMORY_MANAGER
#define eeNew( classType, constructor ) \
( classType *)EE::MemoryManager::AddPointer( EE::cAllocatedPointer( new classType constructor ,__FILE__,__LINE__, sizeof(classType) ) )
#define eeNewArray( classType, amount ) \
( classType *) EE::MemoryManager::AddPointer( EE::cAllocatedPointer( new classType [ amount ], __FILE__, __LINE__, amount * sizeof( classType ) ) )
#define eeMalloc(amount) \
EE::MemoryManager::AddPointer( EE::cAllocatedPointer( EE::MemoryManager::Allocate( amount ), __FILE__, __LINE__, amount ) )
#define eeDelete( data ){ \
if( EE::MemoryManager::RemovePointer( EE::MemoryManager::Delete( data ) ) == false ) printf( "Deleting at '%s' %d\n", __FILE__, __LINE__ ); \
}
#define eeDeleteArray( data ){ \
if ( EE::MemoryManager::RemovePointer( EE::MemoryManager::DeleteArray( data ) ) == false ) printf( "Deleting at '%s' %d\n", __FILE__, __LINE__ ); \
}
#define eeFree( data ){ \
if( EE::MemoryManager::RemovePointer( EE::MemoryManager::Free( data ) ) == false ) printf( "Deleting at '%s' %d\n", __FILE__, __LINE__ ); \
}
#else
#define eeNew( classType, constructor ) \
new classType constructor
#define eeNewArray( classType, amount ) \
new classType [ amount ]
#define eeMalloc( amount ) \
malloc( amount )
#define eeDelete( data ) \
delete data;
#define eeDeleteArray( data ) \
delete [] data;
#define eeFree( data ) \
free(data);
#endif
}
#endif