diff --git a/src/engine/client/serverbrowser.cpp b/src/engine/client/serverbrowser.cpp index 2b649ef5f..236f9d6c3 100644 --- a/src/engine/client/serverbrowser.cpp +++ b/src/engine/client/serverbrowser.cpp @@ -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(); mem_zero(pEntry, sizeof(CServerEntry)); // set the info diff --git a/src/engine/shared/console.h b/src/engine/shared/console.h index 614dd0e0c..d16770c46 100644 --- a/src/engine/shared/console.h +++ b/src/engine/shared/console.h @@ -172,7 +172,7 @@ class CConsole : public IConsole void AddEntry() { - CQueueEntry *pEntry = static_cast(m_Queue.Allocate(sizeof(CQueueEntry))); + CQueueEntry *pEntry = m_Queue.Allocate(); pEntry->m_pNext = 0; if(!m_pFirst) m_pFirst = pEntry; diff --git a/src/engine/shared/memheap.cpp b/src/engine/shared/memheap.cpp index a18a87903..311a6ddb4 100644 --- a/src/engine/shared/memheap.cpp +++ b/src/engine/shared/memheap.cpp @@ -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(malloc(sizeof(CChunk) + CHUNK_SIZE)); + if(!pChunk) + return; + pChunk->m_pMemory = static_cast(static_cast(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(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; diff --git a/src/engine/shared/memheap.h b/src/engine/shared/memheap.h index 2d44dab46..acabfa623 100644 --- a/src/engine/shared/memheap.h +++ b/src/engine/shared/memheap.h @@ -4,6 +4,9 @@ #define ENGINE_SHARED_MEMHEAP_H #include +#include +#include + 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 + T *Allocate(TArgs &&... Args) + { + return new(Allocate(sizeof(T), alignof(T))) T(std::forward(Args)...); + } }; + #endif diff --git a/src/game/client/components/voting.cpp b/src/game/client/components/voting.cpp index 21242a9d5..aaab39ded 100644 --- a/src/game/client/components/voting.cpp +++ b/src/game/client/components/voting.cpp @@ -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(); pOption->m_pNext = 0; pOption->m_pPrev = m_pLast;