#ifndef __GAMA_MEMORY_MANAGER_H__ #define __GAMA_MEMORY_MANAGER_H__ #pragma once #include #include void TempLogFunction( const char* szFormat, ... ) ; #if defined( GM_USE_MEMORY ) void* operator new( size_t size ) ; void* operator new[]( size_t size ) ; void operator delete( void* pMem ) ; void operator delete[]( void* pMem ) ; // GM_USE_MEMORY »ç¿ë½Ã new ¸¦ ÆÄÀϸí, ÇÔ¼ö¸í, ¶óÀιøÈ£¸¦ ¹Þ´Â new ·Î È£ÃâÇÑ´Ù. // delete ´Â ÆÄÀϸí, ÇÔ¼ö¸í, ¶óÀιøÈ£ ÇÔ¼ö°¡ È£ÃâµÇÁö ¾Ê´Â´Ù. void* operator new( size_t size, const char* szFile, const char* szFunc, int iLine ) ; void* operator new[]( size_t size, const char* szFile, const char* szFunc, int iLine ) ; void operator delete( void* pMem, const char* szFile, const char* szFunc, int iLine ) ; void operator delete[]( void* pMem, const char* szFile, const char* szFunc, int iLine ) ; //void* GMMalloc( size_t size, const char* szFile, const char* szFunc, int iLine ) ; //void* GMRealloc( void* pMem, size_t size, const char* szFile, const char* szFunc, int iLine ) ; //void GMFree( void* pMem, const char* szFile, const char* szFunc, int iLine ) ; #define GM_NEW new( __FILE__, __FUNCTION__, __LINE__ ) //#define GM_MALLOC( a ) GMMalloc( a, __FILE__, __FUNCTION__, __LINE__ ) //#define GM_FREE( a ) GMFree( a, __FILE__, __FUNCTION__, __LINE__ ) //#define GM_REALLOC( a, b ) GMRealloc( a, b, __FILE__, __FUNCTION__, __LINE__ ) // delete ´Â ½±°Ô ÀçÁ¤ÀÇ ÇÒ¼ö ¾ø´Ù. #define new GM_NEW //#define malloc( a ) GM_MALLOC( a ) //#define realloc( a, b ) GM_REALLOC( a, b ) //#define free( a ) GM_FREE( a ) // ---------------------------------------------------------------------------------- // // // // new / delete / malloc / realloc / free ÀÇ ¸Þ¸ð¸® »ç¿ëÁ¤º¸¸¦ °ü¸®ÇÏ´Â STATIC Ŭ·¡½º // // // // ---------------------------------------------------------------------------------- // class GMMemoryManager { public : ~GMMemoryManager() ; static GMMemoryManager* Instance() ; private : GMMemoryManager() ; // Non exist function GMMemoryManager( const GMMemoryManager& mgr ) ; GMMemoryManager& operator = ( const GMMemoryManager& mgr ) ; bool Initialize() ; // ÃʱâÈ­ void Shutdown() ; // Á¾·á float GetCurrentTimeInSec() ; // ½Ã°£À» ¾ò¾î¿À´Â ÇÔ¼ö // --------------------------------------------------------------- // For memory tracker public : enum ETrackConst { PATH_LEN = 64, FUNCTION_LEN = 64, TRACKER_ALLOCATE_NUM = 512 // ±âº» ¸Þ¸ð¸® Æ®·¡Ä¿ ÇÒ´ç ¼ö } ; struct SMemoryTrack { unsigned long m_dwID ; // ¸Þ¸ð¸® Æ®·¡Ä¿ ID void* m_pMem ; // ¸Þ¸ð¸® ÁÖ¼Ò unsigned long m_dwSize ; // ÇÒ´çµÈ Å©±â char m_szFile[ PATH_LEN ] ; // ¸Þ¸ð¸® ÇÒ´çÀ» ÇÑ ÆÄÀϸí char m_szFunc[ FUNCTION_LEN ] ; // ¸Þ¸ð¸® ÇÒ´çÀ» ÇÑ ÇÔ¼ö¸í int m_iLine ; // ¸Þ¸ð¸® ÇÒ´çÀ» ÇÑ ¶óÀÎ SMemoryTrack* m_pPrev ; // Prev link SMemoryTrack* m_pNext ; // Next link float m_fTimeNew ; // ÇÒ´çµÈ ½Ã°£ float m_fTimeDelete ; // ÇØÁ¦µÈ ½Ã°£ unsigned long m_dwBytesAllocated ; // ÇØ´ç ¸Þ¸ð¸®°¡ ÇÒ´çµÈ ½ÃÁ¡¿¡¼­ÀÇ ÇÒ´çµÈ ¸Þ¸ð¸® ÃÑ·® } ; void DeleteTrackerBlock() ; // ¸Þ¸ð¸® Æ®·¡Ä¿µéÀ» ÇØÁ¦ private : SMemoryTrack* m_pAllocatedTrackerList ; // ÇÒ´çµÈ ¸Þ¸ð¸®ÀÇ ¸Þ¸ð¸® Æ®·¡Ä¿ ¸®½ºÆ® // --------------------------------------------------------------- // --------------------------------------------------------------- // For memory manage public : enum EConst { MEMORY_MULT_KB = (1024), MEMORY_MULT_MB = (1024 * 1024) } ; void DumpMemoryStats() ; // ¸Þ¸ð¸® »ç¿ë Åë°è Á¤º¸ Ãâ·Â void DumpMemory() ; // ¸Þ¸ð¸® ÇÒ´ç / ÇØÁ¦ Á¤º¸ Ãâ·Â void CallNew() ; // New È£Ãâ void CallDelete() ; // Delete È£Ãâ bool UseTracker() ; // ¸Þ¸ð¸® Æ®·¡Ä¿ »ç¿ë ¿©ºÎ È®ÀÎ void SetUseTracker( bool bUse ) ; // ¸Þ¸ð¸® Æ®·¡Ä¿ »ç¿ë ¿©ºÎ ¼³Á¤ void SetDumpAllocFrees( bool bSet ) ; // ¸Þ¸ð¸® ÇÒ´ç / ÇØÁ¦½Ã ·Î±ë ¿©ºÎ ¼³Á¤ void AllocateBytes( unsigned long dwSize ) ; void AllocateBytes( SMemoryTrack* pNewTrack, void* pMem, unsigned long dwSize, const char* szFile = 0, const char* szFunc = 0, int iLine = 0 ) ; void DeleteBytes( SMemoryTrack* pDeleteTrack, const char* szFile = 0, const char* szFunc = 0, int iLine = 0 ) ; void Timestamp( const char* szString ) ; // Timestamp() ´ë½Å GM_TIMESTAMP ¸ÅÅ©·Î »ç¿ë!! private : int m_iCallsNew ; // ¸Þ¸ð¸® ÇÒ´ç Ƚ¼ö int m_iCallsDelete ; // ¸Þ¸ð¸® ÇØÁ¦ Ƚ¼ö bool m_bUseTracker ; // ¸Þ¸ð¸® Æ®·¡Ä¿ »ç¿ë ¿©ºÎ bool m_bDumpAllocFrees ; // ¸Þ¸ð¸® ÇÒ´ç / ÇØÁ¦½Ã Á¤º¸ Ç¥½Ã ¿©ºÎ unsigned long m_dwNextID ; // ¸Þ¸ð¸® ÇÒ´ç½Ã ºÎ¿©µÇ´Â ID unsigned long m_dwBytesAllocated ; // ÇÒ´çµÈ ¸Þ¸ð¸® ¹ÙÀÌÆ® ¼ö unsigned long m_dwBytesMaxAllocated ; // ÃÖ´ë·Î ÇÒ´çµÈ ¸Þ¸ð¸® ¹ÙÀÌÆ® ¼ö unsigned long m_dwBytesMaxChunkAllocated ; // Çѹø¿¡ ÇÒ´çµÈ ¸Þ¸ð¸® ûũÀÇ ÃÖ´ë ¹ÙÀÌÆ® ¼ö unsigned long m_dwBytesRunningTotalAllocated ; // ½ÇÇàÁß¿¡ ÇÒ´çµÈ ¸Þ¸ð¸® ¹ÙÀÌÆ® ÃѼö unsigned long m_dwBytesFreed ; // ÇØÁ¦µÈ ¸Þ¸ð¸® ¹ÙÀÌÆ® ¼ö float m_fTimeInit ; // Ãʱ⠽𣠰ª float m_fTimeMaxAllocated ; // ÃÖ´ë·Î ÇÒ´çµÈ ¶§ÀÇ ½Ã°£ // --------------------------------------------------------------- // --------------------------------------------------------------- // For timestamp public : enum ETimestampConst { MAX_STRING_LEN = 256, // Timestamp ¹®ÀÚ¿­ ÃÖ´ë ±æÀÌ TIMESTAMP_ALLOCATE_NUM = 32 // ±âº» Timestamp ³ëµå ÇÒ´ç ¼ö } ; struct STimestamp { char m_szString[ MAX_STRING_LEN ] ; // Timestamp ¹®ÀÚ¿­ float m_fTime ; // ½Ã°£ STimestamp* m_pPrev ; // Prev link STimestamp* m_pNext ; // Next link } ; void AllocateTimestampBlock() ; // Timestamp node¸¦ ÇÒ´ç void DeleteTimestampBlock() ; // Timestamp node¸¦ ÇØÁ¦ STimestamp* GetFreeTimestamp() ; // »ç¿ë °¡´ÉÇÑ Timestamp node¸¦ ¹ÝȯÇÏ´Â ÇÔ¼ö private : int m_iTimestampAllocated ; // Timestamp °¡ ÇÒ´çµÈ ¼ö STimestamp* m_pAvailableTimestampList ; // »ç¿ë °¡´ÉÇÑ Timestamp ¸®½ºÆ® STimestamp* m_pTimestampList ; // ÇÒ´çµÈ Timestamp ¸®½ºÆ® STimestamp* m_pTimestampEnd ; // ÇÒ´çµÈ Timestamp ¸®½ºÆ® ¸¶Áö¸· ³ëµå // --------------------------------------------------------------- // --------------------------------------------------------------- // For log public : enum ELogConst { MAX_LOG_PATH = 255, MAX_MEMORY_LOG_OUTPUT_LENGTH = 512, MAX_LOG_FILE_SIZE = 10 * 1024 * 1024 } ; void Log( const char* szFormat, ... ) ; private : bool OpenLogFile( const char* szName="RylMemory.log", bool bFlushOnWrite=false, bool bCommitToDisk=false ) ; bool OpenNextLogFile() ; void CloseLogFile() ; FILE* m_pLogFile ; // ¸Þ¸ð¸® ·Î±× ÆÄÀÏ bool m_bLogFlushOnWrite ; // ¸Þ¸ð¸® ·Î±× ÆÄÀÏ¿¡ ¾µ¶§¸¶´Ù Ç÷¯½¬ ¿©ºÎ bool m_bCommitToDisk ; char m_szLogName[ MAX_LOG_PATH ] ; // ¸Þ¸ð¸® ·Î±× ÆÄÀϸí char m_szLogBuffer[ MAX_MEMORY_LOG_OUTPUT_LENGTH ] ; // ¸Þ¸ð¸® ·Î±× ¹öÆÛ size_t m_WriteSize ; // ÇöÀç ·Î±× ÆÄÀÏ¿¡ ¾´ Å©±â int m_iLogFileCounter ; // ·Î±× ÆÄÀÏ À妽º Ä«¿îÅÍ // --------------------------------------------------------------- } ; #else #define GM_NEW new //#define GM_MALLOC malloc //#define GM_FREE free //#define GM_REALLOC realloc #endif // GM_USE_MEMORY // Macro define #if defined( GM_USE_MEMORY ) #define GM_MEMORY_LOG GMMemoryManager::Instance()->Log #define GM_TIMESTAMP GMMemoryManager::Instance()->Timestamp #else #define GM_MEMORY_LOG TempLogFunction #define GM_TIMESTAMP( str ) #endif // GM_USE_MEMORY #include "GMMemory.inl" #endif // __GAMA_MEMORY_MANAGER_H__