DDraceNetwork Docs
memheap.h
Go to the documentation of this file.
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;
16 char *m_pEnd;
18 };
19
20 enum
21 {
22 // how large each chunk should be
23 CHUNK_SIZE = 1025 * 64,
24 };
25
27
28 void Clear();
29 void NewChunk();
30 void *AllocateFromChunk(unsigned int Size, unsigned Alignment);
31
32public:
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(sizeof(T), alignof(T))) T(std::forward<TArgs>(Args)...);
43 }
44};
45
46#endif
Definition: memheap.h:11
void Clear()
Definition: memheap.cpp:61
CHeap()
Definition: memheap.cpp:43
void Reset()
Definition: memheap.cpp:54
@ CHUNK_SIZE
Definition: memheap.h:23
T * Allocate(TArgs &&... Args)
Definition: memheap.h:40
void * AllocateFromChunk(unsigned int Size, unsigned Alignment)
Definition: memheap.cpp:26
void * Allocate(unsigned Size, unsigned Alignment=alignof(std::max_align_t))
Definition: memheap.cpp:72
~CHeap()
Definition: memheap.cpp:49
CChunk * m_pCurrent
Definition: memheap.h:26
void NewChunk()
Definition: memheap.cpp:9
const char * StoreString(const char *pSrc)
Definition: memheap.cpp:88
Definition: memheap.h:13
char * m_pCurrent
Definition: memheap.h:15
char * m_pMemory
Definition: memheap.h:14
char * m_pEnd
Definition: memheap.h:16
CChunk * m_pNext
Definition: memheap.h:17