Merge pull request #7549 from Robyt3/Memheap-Refactoring

Refactor `CHeap`
This commit is contained in:
Dennis Felsing 2023-11-26 13:36:30 +00:00 committed by GitHub
commit c90899cc29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 31 deletions

View file

@ -657,7 +657,7 @@ void CServerBrowser::SetLatency(NETADDR Addr, int Latency)
CServerBrowser::CServerEntry *CServerBrowser::Add(const NETADDR *pAddrs, int NumAddrs)
{
// create new pEntry
CServerEntry *pEntry = (CServerEntry *)m_ServerlistHeap.Allocate(sizeof(CServerEntry));
CServerEntry *pEntry = m_ServerlistHeap.Allocate<CServerEntry>();
mem_zero(pEntry, sizeof(CServerEntry));
// set the info

View file

@ -172,7 +172,7 @@ class CConsole : public IConsole
void AddEntry()
{
CQueueEntry *pEntry = static_cast<CQueueEntry *>(m_Queue.Allocate(sizeof(CQueueEntry)));
CQueueEntry *pEntry = m_Queue.Allocate<CQueueEntry>();
pEntry->m_pNext = 0;
if(!m_pFirst)
m_pFirst = pEntry;

View file

@ -8,21 +8,15 @@
// 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);
CChunk *pChunk = static_cast<CChunk *>(malloc(sizeof(CChunk) + CHUNK_SIZE));
if(!pChunk)
return;
pChunk->m_pMemory = static_cast<char *>(static_cast<void *>(pChunk + 1));
pChunk->m_pCurrent = pChunk->m_pMemory;
pChunk->m_pEnd = pChunk->m_pMemory + CHUNK_SIZE;
pChunk->m_pNext = (CChunk *)0x0;
pChunk->m_pNext = nullptr;
pChunk->m_pNext = m_pCurrent;
m_pCurrent = pChunk;
@ -31,17 +25,16 @@ void CHeap::NewChunk()
//****************
void *CHeap::AllocateFromChunk(unsigned int Size, unsigned Alignment)
{
char *pMem;
size_t Offset = reinterpret_cast<uintptr_t>(m_pCurrent->m_pCurrent) % Alignment;
if(Offset)
Offset = Alignment - Offset;
// check if we need can fit the allocation
if(m_pCurrent->m_pCurrent + Offset + Size > m_pCurrent->m_pEnd)
return (void *)0x0;
return nullptr;
// 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;
return pMem;
}
@ -49,7 +42,7 @@ void *CHeap::AllocateFromChunk(unsigned int Size, unsigned Alignment)
// creates a heap
CHeap::CHeap()
{
m_pCurrent = 0x0;
m_pCurrent = nullptr;
Reset();
}
@ -67,33 +60,26 @@ void CHeap::Reset()
// destroys the heap
void CHeap::Clear()
{
CChunk *pChunk = m_pCurrent;
CChunk *pNext;
while(pChunk)
while(m_pCurrent)
{
pNext = pChunk->m_pNext;
free(pChunk);
pChunk = pNext;
CChunk *pNext = m_pCurrent->m_pNext;
free(m_pCurrent);
m_pCurrent = pNext;
}
m_pCurrent = 0x0;
}
//
void *CHeap::Allocate(unsigned Size, unsigned Alignment)
{
char *pMem;
// try to allocate from current chunk
pMem = (char *)AllocateFromChunk(Size, Alignment);
void *pMem = AllocateFromChunk(Size, Alignment);
if(!pMem)
{
// allocate new chunk and add it to the heap
NewChunk();
// try to allocate again
pMem = (char *)AllocateFromChunk(Size, Alignment);
pMem = AllocateFromChunk(Size, Alignment);
}
return pMem;

View file

@ -4,6 +4,9 @@
#define ENGINE_SHARED_MEMHEAP_H
#include <cstddef>
#include <new>
#include <utility>
class CHeap
{
struct CChunk
@ -32,5 +35,12 @@ public:
void Reset();
void *Allocate(unsigned Size, unsigned Alignment = alignof(std::max_align_t));
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

View file

@ -163,7 +163,7 @@ void CVoting::AddOption(const char *pDescription)
m_pRecycleLast = 0;
}
else
pOption = (CVoteOptionClient *)m_Heap.Allocate(sizeof(CVoteOptionClient));
pOption = m_Heap.Allocate<CVoteOptionClient>();
pOption->m_pNext = 0;
pOption->m_pPrev = m_pLast;