-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocator.hpp
executable file
·169 lines (140 loc) · 4.9 KB
/
allocator.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#pragma once
#include "allocated_ref.hpp"
#include "array.hpp"
#include "tagged_union.hpp"
#include "utils.hpp"
#include <cassert>
#include <ostream>
#include <utility>
namespace compile_time {
template <std::size_t size, typename T, typename Allocator>
struct SingleAllocator {
const std::size_t allocator_index;
Allocator &parent;
constexpr SingleAllocator(SingleAllocator &&, Allocator &a)
: allocator_index(a.template get_allocator_index<SingleAllocator>()),
parent(a) {}
constexpr SingleAllocator(Allocator &a)
: allocator_index(a.template get_allocator_index<SingleAllocator>()),
parent(a) {}
private:
constexpr std::size_t _allocate() {
for (auto i = 0u; i < size; ++i) {
if (parent.free_slots[i]) {
parent.free_slots[i] = false;
return i;
}
}
// this will always assert false;
// gcc just gets upset if I do that directly,
// and clang gets upset if I don't.
if (!parent.free_slots[3]) {
assert(false && "out of memory");
return 0;
} else
return 0;
}
public:
constexpr auto allocate() { return allocated_ref<T>{*this}; }
constexpr void free(std::size_t i) { parent.free_slots[i] = true; }
constexpr void free(allocated_ref<T> &o) { return o.free(*this); }
friend struct allocated_ref<T>;
};
template <typename T>
template <std::size_t s, typename Allocator>
constexpr allocated_ref<T>::allocated_ref(SingleAllocator<s, T, Allocator> &sa)
: indx(sa._allocate() + 1) {
construct(sa);
}
template <typename T>
template <std::size_t s, typename Allocator>
constexpr void allocated_ref<T>::free(SingleAllocator<s, T, Allocator> &sa) {
sa.free(indx);
}
template <typename T, std::size_t s, typename Allocator>
constexpr erased_ref::erased_ref(SingleAllocator<s, T, Allocator> &sa)
: indx(allocated_ref<T>{sa}.indx), allocator_index{sa.allocator_index} {}
template <typename T, std::size_t s, typename Allocator>
constexpr void erased_ref::free(SingleAllocator<s, T, Allocator> &sa) {
sa.free(indx);
}
template <std::size_t s, typename Top, typename... Subs>
struct Allocator
: public SingleAllocator<s, Subs, Allocator<s, Top, Subs...>>... {
Top top{};
static constexpr const std::size_t size = s;
tagged_union<Subs...> slab[s] = {tagged_union<Subs...>{}};
bool free_slots[s] = {true};
constexpr Allocator() : SingleAllocator<s, Subs, Allocator>(*this)... {
for (auto i = 0u; i < size; ++i)
free_slots[i] = true;
}
constexpr Allocator(Allocator &&o)
: SingleAllocator<s, Subs, Allocator>(std::move(o), *this)...,
top(std::move(o.top)) {
for (auto i = 0u; i < size; ++i) {
free_slots[i] = o.free_slots[i];
slab[i] = std::move(o.slab[i]);
}
}
template <typename SA> static constexpr std::size_t get_allocator_index() {
return index_of<SA, SingleAllocator<s, Subs, Allocator>...>;
}
template <typename T> constexpr SingleAllocator<s, T, Allocator> &get() {
return *this;
}
template <typename sub>
constexpr SingleAllocator<s, sub, Allocator> &as_single_allocator() {
return *this;
}
template <typename sub>
constexpr const SingleAllocator<s, sub, Allocator> &
as_single_allocator() const {
return *this;
}
template <typename T>
constexpr const SingleAllocator<s, T, Allocator> &get() const {
return *this;
}
template <typename T> constexpr allocated_ref<T> allocate() {
return get<T>().allocate();
}
template <typename T> constexpr void free(std::size_t index) {
get<T>().free(index);
}
template <typename T> constexpr void free(allocated_ref<T> ptr) {
get<T>().free(ptr);
}
};
#define $(a) a.get(allocator)
template <typename T, std::size_t s, typename Top, typename... Subs>
std::ostream &print(std::ostream &o, const allocated_ref<T> &ptr,
const Allocator<s, Top, Subs...> &_allocator) {
const SingleAllocator<s, T, Allocator<s, Top, Subs...>> &allocator =
_allocator;
o << "&(";
print(o, ptr.get(allocator), allocator);
return o << ")";
}
template <typename T, std::size_t s, typename Allocator>
std::ostream &print(std::ostream &o, const allocated_ref<T> &ptr,
const SingleAllocator<s, T, Allocator> &allocator) {
o << "&(";
print(o, ptr.get(allocator), allocator);
return o << ")";
}
template <typename T, std::size_t s, typename Top, typename... Subs>
std::ostream &pretty_print(std::ostream &o, const allocated_ref<T> &ptr,
const Allocator<s, Top, Subs...> &_allocator) {
const SingleAllocator<s, T, Allocator<s, Top, Subs...>> &allocator =
_allocator;
pretty_print(o, ptr.get(allocator), allocator);
return o;
}
template <typename T, std::size_t s, typename Allocator>
std::ostream &pretty_print(std::ostream &o, const allocated_ref<T> &ptr,
const SingleAllocator<s, T, Allocator> &allocator) {
pretty_print(o, ptr.get(allocator), allocator);
return o;
}
} // namespace compile_time