-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_allocator.h
50 lines (43 loc) · 1.09 KB
/
stack_allocator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef RDESTL_STACK_ALLOCATOR_H
#define RDESTL_STACK_ALLOCATOR_H
#include "rdestl_common.h"
namespace rde
{
// Stack based allocator.
// Traits:
// - operates on buffer of TBytes bytes of stack memory
// - never frees memory
// - cannot be copied
template<int TBytes>
class stack_allocator
{
public:
explicit stack_allocator(const char* name = "STACK")
: m_name(name),
m_bufferTop(0)
{
/**/
}
void* allocate(size_t bytes, int /*flags*/ = 0)
{
RDE_ASSERT(m_bufferTop + bytes <= TBytes);
char* ret = &m_buffer[0] + m_bufferTop;
m_bufferTop += bytes;
return ret;
}
void deallocate(void* ptr, size_t /*bytes*/)
{
RDE_ASSERT(ptr == 0 || (ptr >= &m_buffer[0] && ptr < &m_buffer[TBytes]));
sizeof(ptr);
}
const char* get_name() const { return m_name; }
private:
stack_allocator(const stack_allocator&);
stack_allocator& operator=(const stack_allocator&);
const char* m_name;
char m_buffer[TBytes];
size_t m_bufferTop;
};
} // namespace rde
//-----------------------------------------------------------------------------
#endif