-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArenaAllocator.h
More file actions
132 lines (111 loc) · 3.89 KB
/
ArenaAllocator.h
File metadata and controls
132 lines (111 loc) · 3.89 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#pragma once
#ifndef ARENA_ALLOCATOR_H
#define ARENA_ALLOCATOR_H
#include <cstddef>
//#include <cstddef>
#include <stdexcept>
//#include <stdexcept>
#include <new> // For placement new
//#include <new> // For placement new
#include <utility> // For std::forward
//#include <utility> // For std::forward
class ArenaAllocator
{
public:
ArenaAllocator(size_t size) : size_(size), offset_(0)
// ArenaAllocator(size_t size) : size_(size), offset_(0)
{
buffer_ = new char[size];
// buffer_ = new char[size];
}
// Delete copy constructor and assignment to prevent shallow copies
// Delete copy constructor and assignment to prevent shallow copies
ArenaAllocator (const ArenaAllocator&) = delete;
ArenaAllocator& operator=(const ArenaAllocator&) = delete;
// Optional: Move constructor could be implemented if needed
// Optional: Move constructor could be implemented if needed
ArenaAllocator (ArenaAllocator&&) = default;
ArenaAllocator& operator=(ArenaAllocator&&) = default;
~ArenaAllocator()
// ~ArenaAllocator()
{
delete[] buffer_;
// delete[] buffer_;
}
void* Allocate(size_t bytes)
// void* Allocate(size_t bytes)
{
// Align the offset
// Align the offset
size_t alignment = alignof(std::max_align_t);
// size_t alignment = alignof(std::max_align_t);
size_t aligned_offset = (offset_ + alignment - 1) & ~(alignment - 1);
// size_t aligned_offset = (offset_ + alignment - 1) & ~(alignment - 1);
if (aligned_offset + bytes > size_)
// if (aligned_offset + bytes > size_)
{
throw std::bad_alloc();
// throw std::bad_alloc();
}
void* ptr = buffer_ + aligned_offset;
// void* ptr = buffer_ + aligned_offset;
offset_ = aligned_offset + bytes;
// offset_ = aligned_offset + bytes;
return ptr;
// return ptr;
}
void* Allocate(size_t bytes, size_t alignment)
// void* Allocate(size_t bytes, size_t alignment)
{
size_t aligned_offset = (offset_ + alignment - 1) & ~(alignment - 1);
// size_t aligned_offset = (offset_ + alignment - 1) & ~(alignment - 1);
if (aligned_offset + bytes > size_) throw std::bad_alloc();
// if (aligned_offset + bytes > size_) throw std::bad_alloc();
void* ptr = buffer_ + aligned_offset;
// void* ptr = buffer_ + aligned_offset;
offset_ = aligned_offset + bytes;
// offset_ = aligned_offset + bytes;
return ptr;
// return ptr;
}
void Deallocate(void* /*ptr*/)
// void Deallocate(void* /*ptr*/)
{
// No-op; arena allocator doesn't support individual deallocation
// // No-op; arena allocator doesn't support individual deallocation
}
void Reset()
// void Reset()
{
offset_ = 0;
// offset_ = 0;
}
template<typename T, typename... Args>
// template<typename T, typename... Args>
T* ConstructGenerals(Args&&... args)
// T* ConstructGenerals(Args&&... args)
{
void* ptr = Allocate(sizeof(T));
// void* ptr = Allocate(sizeof(T));
return new(ptr) T(std::forward<Args>(args)...); // Placement new to construct the object
// return new(ptr) T(std::forward<Args>(args)...); // Placement new to construct the object
}
template<typename T, typename... Args>
// template<typename T, typename... Args>
T* ConstructSpecific(Args&&... args)
// T* ConstructSpecific(Args&&... args)
{
void* ptr = Allocate(sizeof(T), alignof(T));
// void* ptr = Allocate(sizeof(T), alignof(T));
return new(ptr) T(std::forward<Args>(args)...); // Placement new to construct the object
// return new(ptr) T(std::forward<Args>(args)...); // Placement new to construct the object
}
private:
char* buffer_;
// char* buffer_;
size_t size_;
// size_t size_;
size_t offset_;
// size_t offset_;
};
#endif