ddnet/src/engine/shared/memheap.cpp
heinrich5991 f8277267a0 Remove mem_alloc and mem_free, use standard C functions instead
Replace `mem_free` by `free`, and `mem_alloc` by `malloc` or `calloc`
(the latter one being used to allocate a zeroed array of elements,
sometimes, this makes a call to `mem_zero` superfluous).

This results in having to remove `mem_stats` which previously recorded
the number of allocations and their size that the Teeworlds code did
directly.

Remove OOM handling in `src/game/client/components/binds.cpp`.

Remove needless copying in the CSV code in
`src/game/client/components/statboard.cpp`.
2018-04-09 11:56:39 +02:00

96 lines
1.7 KiB
C++

/* (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. */
#include <base/system.h>
#include "memheap.h"
// allocates a new chunk to be used
void CHeap::NewChunk()
{
CChunk *pChunk;
char *pMem;
// allocate memory
pMem = (char *)malloc(sizeof(CChunk) + CHUNK_SIZE);
if(!pMem)
return;
// the chunk structure is located in the beginning of the chunk
// init it and return the chunk
pChunk = (CChunk*)pMem;
pChunk->m_pMemory = (char*)(pChunk+1);
pChunk->m_pCurrent = pChunk->m_pMemory;
pChunk->m_pEnd = pChunk->m_pMemory + CHUNK_SIZE;
pChunk->m_pNext = (CChunk *)0x0;
pChunk->m_pNext = m_pCurrent;
m_pCurrent = pChunk;
}
//****************
void *CHeap::AllocateFromChunk(unsigned int Size)
{
char *pMem;
// check if we need can fit the allocation
if(m_pCurrent->m_pCurrent + Size > m_pCurrent->m_pEnd)
return (void*)0x0;
// 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;
while(pChunk)
{
pNext = pChunk->m_pNext;
free(pChunk);
pChunk = pNext;
}
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();
// try to allocate again
pMem = (char *)AllocateFromChunk(Size);
}
return pMem;
}