mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-20 06:58:20 +00:00
Merge pull request #7549 from Robyt3/Memheap-Refactoring
Refactor `CHeap`
This commit is contained in:
commit
c90899cc29
|
@ -657,7 +657,7 @@ void CServerBrowser::SetLatency(NETADDR Addr, int Latency)
|
||||||
CServerBrowser::CServerEntry *CServerBrowser::Add(const NETADDR *pAddrs, int NumAddrs)
|
CServerBrowser::CServerEntry *CServerBrowser::Add(const NETADDR *pAddrs, int NumAddrs)
|
||||||
{
|
{
|
||||||
// create new pEntry
|
// create new pEntry
|
||||||
CServerEntry *pEntry = (CServerEntry *)m_ServerlistHeap.Allocate(sizeof(CServerEntry));
|
CServerEntry *pEntry = m_ServerlistHeap.Allocate<CServerEntry>();
|
||||||
mem_zero(pEntry, sizeof(CServerEntry));
|
mem_zero(pEntry, sizeof(CServerEntry));
|
||||||
|
|
||||||
// set the info
|
// set the info
|
||||||
|
|
|
@ -172,7 +172,7 @@ class CConsole : public IConsole
|
||||||
|
|
||||||
void AddEntry()
|
void AddEntry()
|
||||||
{
|
{
|
||||||
CQueueEntry *pEntry = static_cast<CQueueEntry *>(m_Queue.Allocate(sizeof(CQueueEntry)));
|
CQueueEntry *pEntry = m_Queue.Allocate<CQueueEntry>();
|
||||||
pEntry->m_pNext = 0;
|
pEntry->m_pNext = 0;
|
||||||
if(!m_pFirst)
|
if(!m_pFirst)
|
||||||
m_pFirst = pEntry;
|
m_pFirst = pEntry;
|
||||||
|
|
|
@ -8,21 +8,15 @@
|
||||||
// allocates a new chunk to be used
|
// allocates a new chunk to be used
|
||||||
void CHeap::NewChunk()
|
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
|
// the chunk structure is located in the beginning of the chunk
|
||||||
// init it and return the chunk
|
// init it and return the chunk
|
||||||
pChunk = (CChunk *)pMem;
|
CChunk *pChunk = static_cast<CChunk *>(malloc(sizeof(CChunk) + CHUNK_SIZE));
|
||||||
pChunk->m_pMemory = (char *)(pChunk + 1);
|
if(!pChunk)
|
||||||
|
return;
|
||||||
|
pChunk->m_pMemory = static_cast<char *>(static_cast<void *>(pChunk + 1));
|
||||||
pChunk->m_pCurrent = pChunk->m_pMemory;
|
pChunk->m_pCurrent = pChunk->m_pMemory;
|
||||||
pChunk->m_pEnd = pChunk->m_pMemory + CHUNK_SIZE;
|
pChunk->m_pEnd = pChunk->m_pMemory + CHUNK_SIZE;
|
||||||
pChunk->m_pNext = (CChunk *)0x0;
|
pChunk->m_pNext = nullptr;
|
||||||
|
|
||||||
pChunk->m_pNext = m_pCurrent;
|
pChunk->m_pNext = m_pCurrent;
|
||||||
m_pCurrent = pChunk;
|
m_pCurrent = pChunk;
|
||||||
|
@ -31,17 +25,16 @@ void CHeap::NewChunk()
|
||||||
//****************
|
//****************
|
||||||
void *CHeap::AllocateFromChunk(unsigned int Size, unsigned Alignment)
|
void *CHeap::AllocateFromChunk(unsigned int Size, unsigned Alignment)
|
||||||
{
|
{
|
||||||
char *pMem;
|
|
||||||
size_t Offset = reinterpret_cast<uintptr_t>(m_pCurrent->m_pCurrent) % Alignment;
|
size_t Offset = reinterpret_cast<uintptr_t>(m_pCurrent->m_pCurrent) % Alignment;
|
||||||
if(Offset)
|
if(Offset)
|
||||||
Offset = Alignment - Offset;
|
Offset = Alignment - Offset;
|
||||||
|
|
||||||
// check if we need can fit the allocation
|
// check if we need can fit the allocation
|
||||||
if(m_pCurrent->m_pCurrent + Offset + Size > m_pCurrent->m_pEnd)
|
if(m_pCurrent->m_pCurrent + Offset + Size > m_pCurrent->m_pEnd)
|
||||||
return (void *)0x0;
|
return nullptr;
|
||||||
|
|
||||||
// get memory and move the pointer forward
|
// get memory and move the pointer forward
|
||||||
pMem = m_pCurrent->m_pCurrent + Offset;
|
char *pMem = m_pCurrent->m_pCurrent + Offset;
|
||||||
m_pCurrent->m_pCurrent += Offset + Size;
|
m_pCurrent->m_pCurrent += Offset + Size;
|
||||||
return pMem;
|
return pMem;
|
||||||
}
|
}
|
||||||
|
@ -49,7 +42,7 @@ void *CHeap::AllocateFromChunk(unsigned int Size, unsigned Alignment)
|
||||||
// creates a heap
|
// creates a heap
|
||||||
CHeap::CHeap()
|
CHeap::CHeap()
|
||||||
{
|
{
|
||||||
m_pCurrent = 0x0;
|
m_pCurrent = nullptr;
|
||||||
Reset();
|
Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,33 +60,26 @@ void CHeap::Reset()
|
||||||
// destroys the heap
|
// destroys the heap
|
||||||
void CHeap::Clear()
|
void CHeap::Clear()
|
||||||
{
|
{
|
||||||
CChunk *pChunk = m_pCurrent;
|
while(m_pCurrent)
|
||||||
CChunk *pNext;
|
|
||||||
|
|
||||||
while(pChunk)
|
|
||||||
{
|
{
|
||||||
pNext = pChunk->m_pNext;
|
CChunk *pNext = m_pCurrent->m_pNext;
|
||||||
free(pChunk);
|
free(m_pCurrent);
|
||||||
pChunk = pNext;
|
m_pCurrent = pNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_pCurrent = 0x0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
void *CHeap::Allocate(unsigned Size, unsigned Alignment)
|
void *CHeap::Allocate(unsigned Size, unsigned Alignment)
|
||||||
{
|
{
|
||||||
char *pMem;
|
|
||||||
|
|
||||||
// try to allocate from current chunk
|
// try to allocate from current chunk
|
||||||
pMem = (char *)AllocateFromChunk(Size, Alignment);
|
void *pMem = AllocateFromChunk(Size, Alignment);
|
||||||
if(!pMem)
|
if(!pMem)
|
||||||
{
|
{
|
||||||
// allocate new chunk and add it to the heap
|
// allocate new chunk and add it to the heap
|
||||||
NewChunk();
|
NewChunk();
|
||||||
|
|
||||||
// try to allocate again
|
// try to allocate again
|
||||||
pMem = (char *)AllocateFromChunk(Size, Alignment);
|
pMem = AllocateFromChunk(Size, Alignment);
|
||||||
}
|
}
|
||||||
|
|
||||||
return pMem;
|
return pMem;
|
||||||
|
|
|
@ -4,6 +4,9 @@
|
||||||
#define ENGINE_SHARED_MEMHEAP_H
|
#define ENGINE_SHARED_MEMHEAP_H
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <new>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
class CHeap
|
class CHeap
|
||||||
{
|
{
|
||||||
struct CChunk
|
struct CChunk
|
||||||
|
@ -32,5 +35,12 @@ public:
|
||||||
void Reset();
|
void Reset();
|
||||||
void *Allocate(unsigned Size, unsigned Alignment = alignof(std::max_align_t));
|
void *Allocate(unsigned Size, unsigned Alignment = alignof(std::max_align_t));
|
||||||
const char *StoreString(const char *pSrc);
|
const char *StoreString(const char *pSrc);
|
||||||
|
|
||||||
|
template<typename T, typename... TArgs>
|
||||||
|
T *Allocate(TArgs &&... Args)
|
||||||
|
{
|
||||||
|
return new(Allocate(sizeof(T), alignof(T))) T(std::forward<TArgs>(Args)...);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -163,7 +163,7 @@ void CVoting::AddOption(const char *pDescription)
|
||||||
m_pRecycleLast = 0;
|
m_pRecycleLast = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
pOption = (CVoteOptionClient *)m_Heap.Allocate(sizeof(CVoteOptionClient));
|
pOption = m_Heap.Allocate<CVoteOptionClient>();
|
||||||
|
|
||||||
pOption->m_pNext = 0;
|
pOption->m_pNext = 0;
|
||||||
pOption->m_pPrev = m_pLast;
|
pOption->m_pPrev = m_pLast;
|
||||||
|
|
Loading…
Reference in a new issue