mirror of
https://github.com/ddnet/ddnet.git
synced 2024-10-20 07:48:18 +00:00
41d2c80f21
Add a templated `CHeap::Allocate` function to simplify memory allocation of objects using `CHeap`. The function uses perfect forwarding to construct objects with the specified arguments.
47 lines
956 B
C++
47 lines
956 B
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. */
|
|
#ifndef ENGINE_SHARED_MEMHEAP_H
|
|
#define ENGINE_SHARED_MEMHEAP_H
|
|
|
|
#include <cstddef>
|
|
#include <new>
|
|
#include <utility>
|
|
|
|
class CHeap
|
|
{
|
|
struct CChunk
|
|
{
|
|
char *m_pMemory;
|
|
char *m_pCurrent;
|
|
char *m_pEnd;
|
|
CChunk *m_pNext;
|
|
};
|
|
|
|
enum
|
|
{
|
|
// how large each chunk should be
|
|
CHUNK_SIZE = 1025 * 64,
|
|
};
|
|
|
|
CChunk *m_pCurrent;
|
|
|
|
void Clear();
|
|
void NewChunk();
|
|
void *AllocateFromChunk(unsigned int Size, unsigned Alignment);
|
|
|
|
public:
|
|
CHeap();
|
|
~CHeap();
|
|
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
|