-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment_allocator.h
308 lines (263 loc) · 7.29 KB
/
segment_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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#pragma once
#include <cstdint>
#include <cstring>
#include <iostream>
#define ENABLE_BIT(v, b) (v) |= ((uint64_t)1) << (b)
#define GET_BIT(v, b) (((v) >> (b)) & 1)
#define DISABLE_BIT(v, b) (v) &= ~(((uint64_t)1) << (b))
template<uint32_t IndexBegin, uint32_t IndexEnd, uint32_t BlockSize, uint32_t MaxBufferSize, uint32_t ErrorResult>
class SegmentAllocator
{
public:
static_assert((BlockSize % 64) == 0);
static_assert((BlockSize <= 64 * 64) && (BlockSize >= 64));
static_assert((MaxBufferSize <= 64) && (MaxBufferSize >= 1));
// Last block can't be less than MaxBufferSize
static_assert(((IndexEnd - IndexBegin) % BlockSize == 0) || ((IndexEnd - IndexBegin) % BlockSize >= MaxBufferSize));
struct OneSizeBlockInfo
{
uint32_t head_block = 0;
uint32_t used_blocks = 0;
uint32_t busy_blocks = 0;
uint32_t used_segments = 0;
};
SegmentAllocator()
{
free_cells_ = IndexEnd - IndexBegin;
// Initialize blocks in main list of free blocks and set size of each block
for (uint32_t index = 0; index < total_blocks_; ++index)
{
all_blocks_[index].Initialize(index - 1, index + 1);
}
all_blocks_[0].previous = null_block_;
all_blocks_[total_blocks_ - 1].next = null_block_;
// the size of the last block may vary
all_blocks_[total_blocks_ - 1].block_size = IndexEnd - IndexBegin - (total_blocks_ - 1) * BlockSize;
// Initialize data for each size of segments
for (uint32_t index = 1; index <= MaxBufferSize; ++index)
{
sizes_info_[index].head_block = null_block_;
}
// size = 0 - free blocks, at start head_block = 0
sizes_info_[0].used_blocks = total_blocks_;
}
uint32_t Allocate(uint16_t size)
{
if ((size == 0) || (size > MaxBufferSize))
{
// bad size
errors_external_++;
return ErrorResult;
}
// try find block
OneSizeBlockInfo& size_info = sizes_info_[size];
if (size_info.head_block == null_block_)
{
uint32_t block_index = sizes_info_[0].head_block;
// no free blocks this size
if (block_index == null_block_)
{
// no free blocks
return ErrorResult;
}
else
{
// move block from list of free to blocks of this size
RemoveFromList(block_index, 0);
all_blocks_[block_index].SetSize(size);
InsertToList(block_index, size);
}
}
OneBlock& block = all_blocks_[size_info.head_block];
uint16_t index_in_block = block.Allocate();
if (index_in_block == error_in_block_)
{
errors_internal_++;
return ErrorResult;
}
uint32_t result = size_info.head_block * BlockSize + index_in_block * size + IndexBegin;
if (block.free_segments == 0)
{
// block is busy
RemoveFromList(size_info.head_block, size);
size_info.busy_blocks++;
}
size_info.used_segments++;
free_cells_ -= size;
return result;
}
uint32_t Free(uint32_t start, uint16_t size)
{
if ((size == 0) || (size > MaxBufferSize) || (start >= IndexEnd) || (start < IndexBegin))
{
errors_external_++;
return false;
}
uint32_t block_index = (start - IndexBegin) / BlockSize;
OneBlock& block = all_blocks_[block_index];
if (block.one_segment_size != size)
{
errors_external_++;
return false;
}
else if ((start - block_index * BlockSize - IndexBegin) % size != 0)
{
errors_external_++;
return false;
}
bool block_was_busy = (block.free_segments == 0);
uint16_t index = (start - block_index * BlockSize - IndexBegin) / size;
if (block.Free(index) == error_in_block_)
{
errors_external_++;
return false;
}
OneSizeBlockInfo& size_info = sizes_info_[size];
if (block_was_busy)
{
if ((block.previous != null_block_) || (block.next != null_block_))
{
errors_internal_++;
}
InsertToList(block_index, size);
size_info.busy_blocks--;
}
if (block.free_segments == block.total_segments)
{
RemoveFromList(block_index, size);
InsertToList(block_index, 0);
}
size_info.used_segments--;
free_cells_ += size;
return true;
}
uint32_t Size() const
{
return free_cells_;
}
const OneSizeBlockInfo* GetBlocksStat() const
{
return sizes_info_;
}
std::pair<uint64_t, uint64_t> GetErrors() const
{
return {errors_external_, errors_internal_};
}
private:
struct OneBlock
{
// indexes of previous and next blocks in list
uint32_t previous;
uint32_t next;
// block structure
uint16_t block_size;
uint16_t one_segment_size;
uint16_t total_segments;
uint16_t free_segments;
// bit masks of segments usage
uint64_t group_mask;
uint64_t masks[BlockSize / 64];
void Initialize(uint32_t previous, uint32_t next)
{
this->previous = previous;
this->next = next;
block_size = BlockSize;
group_mask = 0;
memset(masks, 0, sizeof(masks));
}
void SetSize(uint16_t segment_size)
{
one_segment_size = segment_size;
total_segments = block_size / segment_size;
free_segments = total_segments;
}
uint16_t Allocate()
{
// calling the function implies that we are sure that there are free segments
if ((~group_mask) == 0)
{
return error_in_block_;
}
uint16_t group = __builtin_ctzll(~group_mask);
if ((group >= sizeof(masks) / sizeof(masks[0])) || ((~masks[group]) == 0))
{
return error_in_block_;
}
uint16_t index_in_group = __builtin_ctzll(~masks[group]);
uint16_t index = group * 64 + index_in_group;
if (index >= total_segments)
{
return error_in_block_;
}
// set bits usage
ENABLE_BIT(masks[group], index_in_group);
if ((~masks[group]) == 0)
{
ENABLE_BIT(group_mask, group);
}
// change number of free segments
free_segments--;
return index;
}
uint16_t Free(uint16_t index)
{
if (index >= total_segments)
{
return error_in_block_;
}
uint16_t group = index >> 6; // divide by 64
index &= 0x003f; // % 64
if (GET_BIT(masks[group], index) == 0)
{
return error_in_block_;
}
// set bits usage
DISABLE_BIT(masks[group], index);
if (masks[group] == 0)
{
DISABLE_BIT(group_mask, group);
}
// change number of free segments
free_segments++;
return 0;
}
};
static constexpr uint32_t total_blocks_ = (IndexEnd - IndexBegin + BlockSize - 1) / BlockSize;
static constexpr uint32_t null_block_ = static_cast<uint32_t>(-1);
static constexpr uint16_t error_in_block_ = static_cast<uint16_t>(-1);
OneBlock all_blocks_[total_blocks_];
OneSizeBlockInfo sizes_info_[MaxBufferSize + 1];
uint32_t free_cells_;
uint64_t errors_external_ = 0;
uint64_t errors_internal_ = 0;
void RemoveFromList(uint32_t block_index, uint16_t size)
{
OneBlock& block = all_blocks_[block_index];
sizes_info_[size].used_blocks--;
// change links in previous and next block
if (block.previous != null_block_)
{
all_blocks_[block.previous].next = block.next;
}
if (block.next != null_block_)
{
all_blocks_[block.next].previous = block.previous;
}
// if it was head
if (sizes_info_[size].head_block == block_index)
{
sizes_info_[size].head_block = block.next;
}
// we don't have to delete links to the previous and next one, but we do it only
// for internal verification when we add it to the list
block.previous = null_block_;
block.next = null_block_;
}
void InsertToList(uint32_t block_index, uint16_t size)
{
all_blocks_[block_index].next = sizes_info_[size].head_block;
all_blocks_[block_index].previous = null_block_;
sizes_info_[size].head_block = block_index;
sizes_info_[size].used_blocks++;
}
};