Repository navigation
Expand file tree
/
Copy pathfixed_align_allocator.h
More file actions
285 lines (255 loc) · 8.58 KB
/
Copy pathfixed_align_allocator.h
File metadata and controls
285 lines (255 loc) · 8.58 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright © 2016 zendo (734181759@qq.com). All rights reserved.
#pragma once
#include <atomic>
#include <string>
#include <cstddef>
#include <cstdint>
#include <assert.h>
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace zendo
{
template<bool ThreadSafe = false>
class fixed_align_allocator
{
protected:
struct fake_lock
{
using guard_type = std::lock_guard<fake_lock>;
fake_lock() = default;
constexpr void lock()noexcept{}
constexpr void unlock()noexcept{}
};
struct spin_lock
{
uint8_t impl_{0};
std::atomic<uint8_t>* payload() { return reinterpret_cast<std::atomic<uint8_t>*>(&impl_); }
bool cas(uint8_t compare, uint8_t new_val)
{ return std::atomic_compare_exchange_strong_explicit(payload(), &compare, new_val, std::memory_order_acquire, std::memory_order_relaxed); }
void lock()
{
uint32_t spin_count = 0;
do
{
while (payload()->load())
{
if(spin_count < 4000)
{
++spin_count;
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
::_mm_pause();
#elif defined(__i386__) || defined(__x86_64__)
asm volatile("pause");
#elif defined(__aarch64__) || defined(__arm__)
asm volatile("yield");
#elif defined(__powerpc64__)
asm volatile("or 27,27,27");
#endif
}
else
{
struct timespec ts = {0, 50000};
nanosleep(&ts, nullptr);
}
}
}while(!cas(0,1));
}
void unlock()
{
payload()->store(0, std::memory_order_release);
}
};
using lock_type = typename std::conditional<ThreadSafe, spin_lock, fake_lock>::type;
struct block;
#pragma pack(push, 1)
struct section { union { struct block* block_; struct section* link_; }; char data_[1]; };
#pragma pack(pop)
struct block
{
lock_type lock_;
struct section* free_;
const size_t size_;
block(size_t size) : lock_(), free_(nullptr), size_(size) {}
~block()
{
while (free_)
{
section* next = free_->link_;
free(free_);
free_ = next;
}
}
void gabage_collect()
{
lock_.lock();
section* targets = free_;
free_ = nullptr;
lock_.unlock();
while (targets)
{
struct section* next_stn = targets->link_;
free(targets);
targets = next_stn;
}
}
section* allocate()
{
section* stn;
lock_.lock();
if (free_)
{
stn = free_;
free_ = free_->link_;
lock_.unlock();
}
else
{
lock_.unlock();
stn = static_cast<section*>(malloc(sizeof(void*) + size_));
}
stn->block_ = this;
return stn;
}
void deallocate(section* stn)
{
lock_.lock();
stn->link_ = free_;
free_ = stn;
lock_.unlock();
}
};
static constexpr size_t offset_data(){ return (size_t)((unsigned char*)(&(((section*)0x1)->data_)) - (unsigned char*)(section*)0x1); }
static section* extract_section(void* data) { return reinterpret_cast<section*>(reinterpret_cast<char* const>(data) - offset_data()); }
static block* extract_block(void* data)
{
section* stn = extract_section(data);
if(stn)
{
return stn->block_;
}
return static_cast<block*>(nullptr);
}
struct chunk
{
const size_t align_, max_align_, variety_;
struct block* blocks_;
size_t find_index(size_t size) const { return (size + (align_ - 1)) / align_ - 1; }
chunk(size_t align, size_t variety) : align_(align), max_align_(align * variety), variety_(variety), blocks_(variety ? static_cast<block*>(malloc(sizeof(block)*variety)) : nullptr)
{
if(blocks_)
{
for (size_t i = 0; i < variety_; ++i)
{
new (&blocks_[i]) block(align_*(i + 1));
}
}
}
~chunk()
{
if (blocks_)
{
for (size_t i = 0; i < variety_; ++i)
{
blocks_[i].~block();
}
free(blocks_);
}
}
size_t get_max_align() const { return max_align_; }
void gabage_collect(size_t begin_index = 0)
{
for (size_t i = begin_index; i < variety_; ++i)
{
blocks_[i].gabage_collect();
}
}
void* allocate(size_t size)
{
return blocks_[find_index(size)].allocate()->data_;
}
static bool deallocate(void* data)
{
section* stn = extract_section(data);
if (stn)
{
stn->block_->deallocate(stn);
return true;
}
return false;
}
}* free_lists_;
const size_t align_, count_,variety_;
public:
fixed_align_allocator() = delete;
fixed_align_allocator(size_t align, size_t count, size_t variety)
: align_(align), count_(count), variety_(variety)
, free_lists_(static_cast<chunk*>(malloc(sizeof(chunk)* count)))
{
if (free_lists_)
{
new (&free_lists_[0]) chunk(align_, variety_);
for (size_t i = 1; i < count_; ++i)
{
new (&free_lists_[i]) chunk(free_lists_[i - 1].get_max_align() * 2, variety_);
}
}
}
~fixed_align_allocator()
{
if (free_lists_)
{
for (size_t i = 0; i < count_; ++i)
{
free_lists_[i].~chunk();
}
free(free_lists_);
}
}
size_t get_max_align(size_t index) const { return free_lists_[index].get_max_align(); }
size_t get_max_align() const { return free_lists_[count_ - 1].get_max_align(); }
static size_t extract_size(void* data)
{
block* blk = extract_block(data);
if(blk)
{
return blk->size_;
}
return 0;
}
void gabage_collect(uint32_t begin_index = 0)
{
for (uint32_t i = begin_index; i < count_; ++i)
{
free_lists_[begin_index].gabage_collect();
}
}
void* allocate(size_t size)
{
for (size_t i = 0; i < count_; ++i)
{
if (size <= free_lists_[i].get_max_align())
{
return free_lists_[i].allocate(size);
}
}
section* stn = static_cast<struct section*>(malloc(sizeof(void*) + size));
stn->block_ = nullptr;
return stn->data_;
}
static void deallocate(void* data)
{
section* stn = extract_section(data);
if (stn)
{
block* blk = stn->block_;
if(blk)
{
blk->deallocate(stn);
return;
}
free(stn);
}
}
};
}