2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2010-05-29 07:25:38 +00:00
|
|
|
#include "memheap.h"
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <base/system.h>
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
// allocates a new chunk to be used
|
|
|
|
void CHeap::NewChunk()
|
|
|
|
{
|
|
|
|
CChunk *pChunk;
|
|
|
|
char *pMem;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// allocate memory
|
2018-04-09 09:56:39 +00:00
|
|
|
pMem = (char *)malloc(sizeof(CChunk) + CHUNK_SIZE);
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!pMem)
|
|
|
|
return;
|
|
|
|
|
2018-02-04 15:00:47 +00:00
|
|
|
// the chunk structure is located in the beginning of the chunk
|
2010-05-29 07:25:38 +00:00
|
|
|
// init it and return the chunk
|
2020-09-26 19:41:58 +00:00
|
|
|
pChunk = (CChunk *)pMem;
|
|
|
|
pChunk->m_pMemory = (char *)(pChunk + 1);
|
2010-05-29 07:25:38 +00:00
|
|
|
pChunk->m_pCurrent = pChunk->m_pMemory;
|
|
|
|
pChunk->m_pEnd = pChunk->m_pMemory + CHUNK_SIZE;
|
|
|
|
pChunk->m_pNext = (CChunk *)0x0;
|
|
|
|
|
|
|
|
pChunk->m_pNext = m_pCurrent;
|
2011-04-13 18:37:12 +00:00
|
|
|
m_pCurrent = pChunk;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//****************
|
|
|
|
void *CHeap::AllocateFromChunk(unsigned int Size)
|
|
|
|
{
|
|
|
|
char *pMem;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// check if we need can fit the allocation
|
|
|
|
if(m_pCurrent->m_pCurrent + Size > m_pCurrent->m_pEnd)
|
2020-09-26 19:41:58 +00:00
|
|
|
return (void *)0x0;
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
// get memory and move the pointer forward
|
|
|
|
pMem = m_pCurrent->m_pCurrent;
|
|
|
|
m_pCurrent->m_pCurrent += Size;
|
|
|
|
return pMem;
|
|
|
|
}
|
|
|
|
|
|
|
|
// creates a heap
|
|
|
|
CHeap::CHeap()
|
|
|
|
{
|
|
|
|
m_pCurrent = 0x0;
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
CHeap::~CHeap()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CHeap::Reset()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
NewChunk();
|
|
|
|
}
|
|
|
|
|
|
|
|
// destroys the heap
|
|
|
|
void CHeap::Clear()
|
|
|
|
{
|
|
|
|
CChunk *pChunk = m_pCurrent;
|
|
|
|
CChunk *pNext;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
while(pChunk)
|
|
|
|
{
|
|
|
|
pNext = pChunk->m_pNext;
|
2018-04-09 09:56:39 +00:00
|
|
|
free(pChunk);
|
2010-05-29 07:25:38 +00:00
|
|
|
pChunk = pNext;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pCurrent = 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
void *CHeap::Allocate(unsigned Size)
|
|
|
|
{
|
|
|
|
char *pMem;
|
|
|
|
|
|
|
|
// try to allocate from current chunk
|
|
|
|
pMem = (char *)AllocateFromChunk(Size);
|
|
|
|
if(!pMem)
|
|
|
|
{
|
|
|
|
// allocate new chunk and add it to the heap
|
|
|
|
NewChunk();
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// try to allocate again
|
|
|
|
pMem = (char *)AllocateFromChunk(Size);
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
return pMem;
|
|
|
|
}
|