-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockAllocator.hpp
More file actions
166 lines (144 loc) · 3.58 KB
/
Copy pathBlockAllocator.hpp
File metadata and controls
166 lines (144 loc) · 3.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
/**
@file BlockAllocator.hpp
@author Adam Wallberg
@date 23/09/2017
*/
#pragma once
#include <cstdlib>
#include <cstring>
#include <assert.h>
template<class T, unsigned int BLOCK_SIZE = 32, unsigned int MAX_NUM_BLOCKS = 4>
class BlockAllocator
{
private:
static_assert(BLOCK_SIZE > 0, "Block size should not be less than 1!");
static_assert(MAX_NUM_BLOCKS > 0, "Max number of blocks should not be less than 1!");
struct Block
{
bool filledFlags[BLOCK_SIZE];
T* first;
T* last;
T* current;
};
public:
BlockAllocator()
: currentBlock(0)
, numBlocks(0)
{
blocks[0] = allocBlock();
}
~BlockAllocator()
{
for (unsigned int i = 0; i < numBlocks; i++)
{
free(blocks[i].first);
}
}
// Constructs the object in place, from specified parameters
template<class... Params>
T* alloc(Params&&... args)
{
// Store result in address.
T* result = getFreeAddress();
*result = T(args...);
incrementBlock();
return result;
}
// Copies the value of the object into the address
T* insert(const T& object)
{
T* result = getFreeAddress();
*result = object;
incrementBlock();
return result;
}
void dealloc(T* object)
{
for (unsigned int i = 0; i < numBlocks; i++)
{
Block* block = &blocks[i];
// Check if address is in range of this block
if (object >= block->first && object <= block->last)
{
assert(
block->filledFlags[object - block->first]
&& "This item is already deallocated!"
);
memset(object, 0, sizeof(T));
block->filledFlags[object - block->first] = false;
// Since we now have a free address, we set the "current"
// pointer here, so we don't get a fragmented block.
if (i <= currentBlock)
{
currentBlock = i;
if (object <= block->current)
{
block->current = object;
}
}
return;
}
}
}
unsigned int getNumAllocatedBlocks() const { return numBlocks; }
private:
// Retrieves the next free address.
T* getFreeAddress()
{
Block* block = &blocks[currentBlock];
int position = block->current - block->first;
while (true)
{
// Check if we are out of space in current block.
if (block->current > block->last)
{
// Check if we have to allocate a new block,
// or if we already have an empty one.
currentBlock++;
if (currentBlock >= numBlocks)
blocks[currentBlock] = allocBlock();
// Update pointer
block = &blocks[currentBlock];
position = block->current - block->first;
}
// Check if the current address is filled.
else if (block->filledFlags[position])
{
block->current++;
position = block->current - block->first;
}
else
{
// We have found a free address.
break;
}
}
return block->current;
}
void incrementBlock()
{
Block* block = &blocks[currentBlock];
unsigned int position = block->current - block->first;
block->filledFlags[position] = true;
block->current++;
}
Block allocBlock()
{
assert(
numBlocks < MAX_NUM_BLOCKS
&& "Out of memory blocks! Please increase block capacity!"
);
Block block;
// Allocate block memory.
block.current = block.first = (T*)malloc(sizeof(T) * BLOCK_SIZE);
block.last = block.first + BLOCK_SIZE - 1;
memset(block.first, 0, sizeof(T) * BLOCK_SIZE);
// Set flags to false.
memset(&block.filledFlags[0], false, sizeof(bool) * BLOCK_SIZE);
numBlocks++;
return block;
}
Block blocks[MAX_NUM_BLOCKS];
unsigned int currentBlock;
unsigned int numBlocks;
};