1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_SHARED_MEMHEAP_H
4#define ENGINE_SHARED_MEMHEAP_H
5
6#include <cstddef>
7#include <new>
8#include <utility>
9
10class CHeap
11{
12 struct CChunk
13 {
14 char *m_pMemory;
15 char *m_pCurrent;
16 char *m_pEnd;
17 CChunk *m_pNext;
18 };
19
20 /**
21 * How large each chunk should be.
22 */
23 static constexpr size_t CHUNK_SIZE = 1025 * 64;
24
25 CChunk *m_pCurrent;
26
27 void Clear();
28 void NewChunk(size_t ChunkSize);
29 void *AllocateFromChunk(size_t Size, size_t Alignment);
30
31public:
32 CHeap();
33 ~CHeap();
34 void Reset();
35 void *Allocate(size_t Size, size_t Alignment = alignof(std::max_align_t));
36 const char *StoreString(const char *pSrc);
37
38 template<typename T, typename... TArgs>
39 T *Allocate(TArgs &&...Args)
40 {
41 return new(Allocate(Size: sizeof(T), Alignment: alignof(T))) T(std::forward<TArgs>(Args)...);
42 }
43};
44
45#endif
46