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 | |
10 | class 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 | enum |
21 | { |
22 | // how large each chunk should be |
23 | CHUNK_SIZE = 1025 * 64, |
24 | }; |
25 | |
26 | CChunk *m_pCurrent; |
27 | |
28 | void Clear(); |
29 | void NewChunk(); |
30 | void *AllocateFromChunk(unsigned int Size, unsigned Alignment); |
31 | |
32 | public: |
33 | CHeap(); |
34 | ~CHeap(); |
35 | void Reset(); |
36 | void *Allocate(unsigned Size, unsigned Alignment = alignof(std::max_align_t)); |
37 | const char *StoreString(const char *pSrc); |
38 | |
39 | template<typename T, typename... TArgs> |
40 | T *Allocate(TArgs &&... Args) |
41 | { |
42 | return new(Allocate(Size: sizeof(T), Alignment: alignof(T))) T(std::forward<TArgs>(Args)...); |
43 | } |
44 | }; |
45 | |
46 | #endif |
47 | |