📄 simplememory.cpp
字号:
/*
* ============================================================================
* Name : CGlobalData from GlobalData.cpp
* Part of : Game
* Created : 2003-06-16 by Qiu Wei Min
* Modified : 2003-09-03 by Zheng Jie Sheng
* Implementation notes:
*
* Version :
* Copyright: Gameloft S.A.
* ============================================================================
*/
// INCLUDE FILES
//#include <AEEStdLib.h>
#include "simplememory.h"
#include "Sysdef.h"
//--------------------------------------------------------------------------
// operator new
//--------------------------------------------------------------------------
void* operator new(unsigned int size)
{
return MALLOC(size);
}
//--------------------------------------------------------------------------
// operator delete
//--------------------------------------------------------------------------
void operator delete(void * ptr)
{
FREE(ptr);
}
// ================= MEMBER FUNCTIONS =======================
CSimpleMemory::CSimpleMemory()
{
m_pHeapStart = m_pHeapCurrent = NULL;
m_pStackStart = m_pStackCurrent = NULL;
m_pBuffer = NULL;
m_pGlobalCurrent = NULL;
m_pGlobal = NULL;
m_nLocal = m_nGlobal = 0;
}
CSimpleMemory::~CSimpleMemory()
{
if(m_pBuffer)
FREE(m_pBuffer);
if(m_pGlobal)
FREE(m_pGlobal);
}
bool CSimpleMemory::Init(int nGlobal, int nLocal)
{
m_nLocal = nLocal;
m_nGlobal = nGlobal;
m_pGlobalCurrent = m_pGlobal = (char*)MALLOC(m_nGlobal);
if(m_pGlobal == NULL)
{
return false;
}
m_pBuffer = (char*)MALLOC(m_nLocal);
if(m_pBuffer == NULL)
{
FREE(m_pGlobal);
m_pGlobalCurrent = m_pGlobal = NULL;
return false;
}
ResetHeap();
ResetStack();
return true;
}
bool CSimpleMemory::IsInitSuccess()
{
return (NULL != m_pBuffer && NULL != m_pGlobal);
}
void CSimpleMemory::ResetHeap()
{
m_pHeapCurrent = m_pHeapStart = m_pBuffer;
MEMSET(m_pBuffer, 0, m_nLocal);
}
void CSimpleMemory::ResetStack()
{
m_pStackStart = m_pStackCurrent = m_pBuffer + m_nLocal;
MEMSET(m_pBuffer, 0, m_nLocal);
}
void* CSimpleMemory::HeapMalloc(int nSize)
{
char *p;
nSize = (nSize + 3 ) & ~3;
p = m_pHeapCurrent;
SYS_ASSERT(m_pHeapCurrent + nSize <= m_pStackCurrent);
m_pHeapCurrent += nSize;
return p;
}
void* CSimpleMemory::StackMalloc(int nSize)
{
char* p;
nSize = (nSize + 3 ) & ~3;
p = m_pStackCurrent - nSize - 4;
SYS_ASSERT(p >= m_pHeapCurrent);
//store
*(char**)p = m_pStackCurrent;
m_pStackCurrent = p;
return p + 4;
}
void CSimpleMemory::StackFree(void *p)
{
m_pStackCurrent = *(char**)m_pStackCurrent;
}
void* CSimpleMemory::GlobalMalloc(int nSize)
{
char *p;
nSize = (nSize + 3 ) & ~3;
p = m_pGlobalCurrent;
SYS_ASSERT(m_pGlobalCurrent + nSize <= m_pGlobal + m_nGlobal);
m_pGlobalCurrent += nSize;
return p;
}
void* CSimpleMemory::Fill(void *dest, int c, unsigned int count)
{
MEMSET(dest, c, count);
return dest;
}
void* CSimpleMemory::Copy(void *dest, const void *src, unsigned int count)
{
MEMCPY(dest, src, count);
return dest;
}
char* CSimpleMemory::strcpy(char *strDestination, const char *strSource)
{
char *p = strDestination;
while(*strSource)
{
*p++ = *strSource++;
}
*p = 0;
return strDestination;
}
int CSimpleMemory::GetMemType(void *p)
{
if(p >= m_pHeapStart && p < m_pHeapCurrent)
return MEM_HEAP;
else if(p >= m_pStackCurrent && p < m_pStackStart)
return MEM_STACK;
else if(p >= m_pGlobal && p < m_pGlobal + m_nGlobal)
return MEM_GLOBAL;
else return MEM_UNKNOWN;
}
char *CSimpleMemory::strcat(char *strDestination, const char *strSource)
{
return STRCAT(strDestination, strSource);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -