-
Notifications
You must be signed in to change notification settings - Fork 0
/
memoryManager.cpp
executable file
·222 lines (164 loc) · 5.27 KB
/
memoryManager.cpp
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
#include "memoryManager.h"
#include <boost/assert.hpp>
#include <boost/foreach.hpp>
#include <cstring>
#include <algorithm>
#include <sstream>
#include "windows.h"
using namespace std;
using namespace exceptions;
/*
* === memoryManager::_freeChunk ===
*/
struct memoryManager::_freeChunk
{
_freeChunk * next;
};
/*
* === memoryManager::_allocedChunk ===
*/
struct memoryManager::_allocedChunk
{
/*
* An index in the _allChunks where this chunk will be returned to when it
* is freed.
* Big chunks allocated individualy via allocHelper(...) have _bigChunkIndex
* as a value of this field.
*/
_allChunks_type::size_type index;
};
/*
* === memoryManager ===
*/
memoryManager::memoryManager()
{
/*
* Global checks. Idealy this would be at the namespace level in a "class
* static constructor".
*/
checkAllocationSize();
static_assert(sizeof(_freeChunk) <= _minChunkSize,
"Smallest chunks should be big enough to contian an empty "
"chunk info.");
static_assert(sizeof(_allocedChunk) < _minChunkSize,
"Smallest chunks should be big enough to contian a header "
"and something else.");
/*
* End of global checks.
*/
size_t size = 1;
while (size < _minChunkSize)
size <<= 1;
while (size <= _maxChunkSize)
{
_allChunkSizes.push_back(size);
size <<= 1;
}
_allChunks.resize(_allChunkSizes.size());
}
memoryManager::~memoryManager()
{
BOOST_FOREACH (void * p, _blocks)
releaseHelper(p);
_blocks.clear();
_allChunks.clear();
_allChunkSizes.clear();
}
void * memoryManager::alloc(size_t size, bool zero)
{
/* Large allocations are forwarded to the default memory allocator. */
if (size + sizeof(_allocedChunk) > _allChunkSizes.back())
{
void * block = allocHelper(size + sizeof(_allocedChunk), zero);
_allocedChunk * header = reinterpret_cast<_allocedChunk *>(block);
header->index = _bigChunkIndex;
return reinterpret_cast<char *>(block) + sizeof(header);
}
/*
* Round size up to the next power of 2 and include _allocedChunk size as we
* will add this header to the allocated block.
*/
_allChunkSizes_type::iterator i =
lower_bound(_allChunkSizes.begin(), _allChunkSizes.end(),
size + sizeof(_allocedChunk));
size_t chunkSize = *i;
size_t j = i - _allChunkSizes.begin();
_freeChunk * freeChunk = _allChunks[j];
if (!freeChunk)
freeChunk = prepareNewBlock(chunkSize);
_allChunks[j] = freeChunk->next;
_allocedChunk * header =
reinterpret_cast<_allocedChunk *>(freeChunk);
header->index = j;
char * body = reinterpret_cast<char *>(header) + sizeof(header);
if (zero)
memset(body, 0, size);
return body;
}
void memoryManager::release(void * p)
{
_allocedChunk * allocedChunk = reinterpret_cast<_allocedChunk *>
(reinterpret_cast<char *>(p) - sizeof(_allocedChunk));
if (allocedChunk->index == _bigChunkIndex)
{
releaseHelper(p);
return;
}
size_t index = allocedChunk->index;
BOOST_ASSERT(index < _allChunks.size());
_freeChunk * freeChunk = reinterpret_cast<_freeChunk *>(allocedChunk);
freeChunk->next = _allChunks[index];
_allChunks[index] = freeChunk;
}
memoryManager::_freeChunk *
memoryManager::prepareNewBlock(size_t chunkSize)
{
void * block = allocHelper(_allocationSize, false);
_blocks.push_back(block);
_freeChunk * firstChunk = reinterpret_cast<_freeChunk *>(block);
size_t count = _allocationSize / chunkSize;
BOOST_ASSERT(count > 0);
/* Link all the chunks in the new block. */
{
char * p = reinterpret_cast<char *>(firstChunk);
for ( ; count > 0; p += chunkSize, --count)
{
reinterpret_cast<_freeChunk *>(p)->next =
reinterpret_cast<_freeChunk *>(p + chunkSize);
}
/* Last chunk has no next. */
reinterpret_cast<_freeChunk *>(p - chunkSize)->next = 0;
}
return firstChunk;
}
void memoryManager::checkAllocationSize() throw(logic_error)
{
SYSTEM_INFO si;
GetSystemInfo(&si);
if (_allocationSize % si.dwPageSize != 0)
{
ostringstream ss;
ss << "_allocationSize is not a multiple of the system page size: "
<< _allocationSize << " % " << si.dwPageSize << " != 0";
throw logic_error(ss.str());
}
}
void * memoryManager::allocHelper(size_t size, bool /* zero */)
throw(systemError)
{
void * p = VirtualAlloc
(0 /* lpAddress */,
size /* dwSize */,
MEM_RESERVE | MEM_COMMIT /* flAllocationType */,
PAGE_EXECUTE_READWRITE /* flProtect */
);
if (!p)
throw systemError(systemError::getLast);
return p;
}
void memoryManager::releaseHelper(void * p) throw(systemError)
{
BOOL res = VirtualFree(p, 0, MEM_RELEASE);
if (!res)
throw systemError(systemError::getLast);
}