-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregion.hpp
155 lines (113 loc) · 2.75 KB
/
region.hpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef TL_REGION_HPP
#define TL_REGION_HPP 1
#include "platform.h"
#include "vec.hpp"
namespace tl {
#ifndef TL_REGION_MAX_ALIGN
# define TL_REGION_MAX_ALIGN (8)
#endif
// TODO: Change these to functions
#define TL_ALIGN_SIZE(size, alignment) (((size) + ((alignment)-1)) & ~((usize)(alignment) - 1))
#define TL_ALIGN_PTR(p, alignment) ((u8 *)TL_ALIGN_SIZE((usize)(p), alignment))
#define TL_IS_ALIGNED(p, alignment) (((usize)(p) & ((usize)(alignment) - 1)) == 0)
struct RegionBlock {
RegionBlock* prev;
u8 mem[];
};
struct Allocator {
u8 *cur, *end;
void* alloc(usize size) {
usize rounded_size = TL_ALIGN_SIZE(size, TL_REGION_MAX_ALIGN);
if (usize(this->end - this->cur) >= rounded_size) {
void* ret = this->cur;
this->cur += rounded_size;
return ret;
}
return this->alloc_fallback(rounded_size);
}
virtual void* alloc_fallback(usize rounded_size) = 0;
};
/*
template<usize Size>
struct FixedSizeAllocator {
u8 *cur, *end;
static usize const RoundedSize = TL_ALIGN_SIZE(Size, TL_REGION_MAX_ALIGN);
void* alloc() {
if (usize(this->end - this->cur) >= RoundedSize) {
void* ret = this->cur;
this->cur += RoundedSize;
return ret;
}
return this->alloc_fallback(rounded_size);
}
virtual void* alloc_fallback() = 0;
};
*/
struct Region : Allocator {
static usize const PageSize = 4096;
Region()
: last_block(0) {
this->cur = TL_ALIGN_PTR(this->mem, TL_REGION_MAX_ALIGN);
this->end = this->mem + PageSize;
}
void* alloc_fallback(usize rounded_size);
~Region();
RegionBlock* last_block;
u8 mem[PageSize];
};
struct BasicFreelist {
BasicFreelist()
: top(NULL) {
}
void* top;
};
template<usize Size>
struct Freelist : BasicFreelist {
static_assert(Size >= sizeof(void*), "Freelist size must be at least as large as a pointer");
static_assert(TL_IS_ALIGNED(Size, TL_REGION_MAX_ALIGN), "Freelist size must be aligned");
Freelist() {
}
void* try_alloc() {
void* p = this->top;
if (p) {
this->top = *(void **)p;
}
return p;
}
void free(void* p) {
assert(TL_IS_ALIGNED(p, TL_REGION_MAX_ALIGN));
*(void **)p = this->top;
this->top = p;
}
};
struct FreelistDelete {
FreelistDelete()
: ptrToTop(0) {
}
FreelistDelete(BasicFreelist& freelist)
: ptrToTop(&freelist.top) {
}
void operator()(void* p) {
*(void **)p = *ptrToTop;
*ptrToTop = p;
}
void** ptrToTop;
};
template<typename T>
struct PooledRegion : Region {
static usize const RoundedSize = TL_ALIGN_SIZE(sizeof(T), TL_REGION_MAX_ALIGN);
Freelist<RoundedSize> freelist;
void* alloc() {
return this->alloc(RoundedSize);
}
T* alloc_from_pool() {
void* p = this->freelist.try_alloc();
if (p) return (T *)p;
return this->alloc();
}
void free_to_pool(T* p) {
this->freelist.free(p);
}
};
}
#endif // TL_REGION_HPP