-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryManager.h
More file actions
256 lines (204 loc) · 7.5 KB
/
MemoryManager.h
File metadata and controls
256 lines (204 loc) · 7.5 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
#pragma once
#ifndef MEMORYMANAGER_H
#define MEMORYMANAGER_H
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#define MEMORY_RESERVED_ADDRESS 0x100000
#define MEMORY_BEGIN_ADDRESS 0x101000
class MemoryPool {
public:
size_t itemCount;
size_t itemSize;
bool* freeFlags;
void* items;
void* allocate() const {
size_t i = 0;
for (; this->freeFlags[i] == false; i++) {
if (i > this->itemCount) {
return nullptr;
}
}
this->freeFlags[i] = false;
return reinterpret_cast<void*>(reinterpret_cast<size_t>(this->items) + this->itemSize * i);
}
void free(void* p) const {
const size_t n = reinterpret_cast<size_t>(p) - reinterpret_cast<size_t>(this->items);
if (n > this->itemCount) {
// massive fail, trying to free item from another pool
return;
}
this->freeFlags[n] = true;
}
void* getLocation() const {
return this->freeFlags;
}
};
class MemoryManager {
size_t remainingMemory = 0;
void* freeAddress = nullptr;
MemoryPool* pool1 = nullptr;
MemoryPool* pool2 = nullptr;
MemoryPool* pool4 = nullptr;
MemoryPool* pool8 = nullptr;
MemoryPool* pool16 = nullptr;
MemoryPool* pool32 = nullptr;
MemoryPool* pool64 = nullptr;
MemoryPool* pool128 = nullptr;
MemoryPool* pool256 = nullptr;
MemoryPool* pool1024 = nullptr;
MemoryPool* pool4096 = nullptr;
MemoryPool* pool65536 = nullptr;
void* poolEndIndicator = nullptr;
MemoryPool* createPool(const size_t itemCount, const size_t itemSize) {
const size_t poolMetadataSize = itemCount + itemSize + sizeof(void*) * 2;
const auto pool = static_cast<MemoryPool*>(this->getMemorySlice((sizeof(bool) + itemSize) * itemCount));
pool->itemCount = itemCount;
pool->itemSize = itemSize;
pool->freeFlags = reinterpret_cast<bool*>(reinterpret_cast<size_t>(pool) + poolMetadataSize);
pool->items = static_cast<void*>(pool->freeFlags + sizeof(bool) * itemCount);
// Initialize everything as free
for (size_t i = 0; i < itemCount; i++) {
pool->freeFlags[i] = true;
}
return pool;
}
public:
static size_t getMemoryLimit() {
return 1000 * 1000 + *reinterpret_cast<uint16_t *>(0x7c00 + 506) * 1000 + *reinterpret_cast<uint16_t *>(0x7c00 + 508) * 64 * 1000;
}
static MemoryManager* getInstance() {
return reinterpret_cast<MemoryManager *>(MEMORY_RESERVED_ADDRESS);
}
/**
* You NEED to call this once before doing anything.
*
* I intentionally didn't just magically set this up since getInstance() can be based on random memory -> invalid pointers.
*/
void setUp() {
this->remainingMemory = getMemoryLimit() - MEMORY_BEGIN_ADDRESS;
this->freeAddress = reinterpret_cast<void*>(MEMORY_BEGIN_ADDRESS);
const size_t poolItemLimit = this->remainingMemory / 2 / (1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 + 1024 + 4096 + 65536);
this->pool1 = this->createPool(poolItemLimit, 1);
this->pool2 = this->createPool(poolItemLimit, 2);
this->pool4 = this->createPool(poolItemLimit, 4);
this->pool8 = this->createPool(poolItemLimit, 8);
this->pool16 = this->createPool(poolItemLimit, 16);
this->pool32 = this->createPool(poolItemLimit, 32);
this->pool64 = this->createPool(poolItemLimit, 64);
this->pool128 = this->createPool(poolItemLimit, 128);
this->pool256 = this->createPool(poolItemLimit, 256);
this->pool1024 = this->createPool(poolItemLimit, 1024);
this->pool4096 = this->createPool(poolItemLimit, 4096);
this->pool65536 = this->createPool(poolItemLimit, 65536);
this->poolEndIndicator = this->getMemorySlice(1);
}
/**
* This gets a raw slice of memory, should not be used directly
*/
void* getMemorySlice(const size_t size) {
// TODO: handle out of memory, not relevant yet
const auto slice = this->freeAddress;
this->freeAddress = reinterpret_cast<void*>(reinterpret_cast<size_t>(this->freeAddress) + size);
return slice;
}
void* allocate(const size_t size) {
if (size == 1) {
void* allocated = this->pool1->allocate();
if (allocated != nullptr) return allocated;
}
if (size == 2) {
void* allocated = this->pool2->allocate();
if (allocated != nullptr) return allocated;
}
if (size <= 4) {
void* allocated = this->pool4->allocate();
if (allocated != nullptr) return allocated;
}
if (size <= 8) {
void* allocated = this->pool8->allocate();
if (allocated != nullptr) return allocated;
}
if (size <= 16) {
void* allocated = this->pool16->allocate();
if (allocated != nullptr) return allocated;
}
if (size <= 32) {
void* allocated = this->pool32->allocate();
if (allocated != nullptr) return allocated;
}
if (size <= 64) {
void* allocated = this->pool64->allocate();
if (allocated != nullptr) return allocated;
}
if (size <= 128) {
void* allocated = this->pool128->allocate();
if (allocated != nullptr) return allocated;
}
if (size <= 256) {
void* allocated = this->pool256->allocate();
if (allocated != nullptr) return allocated;
}
if (size <= 1024) {
void* allocated = this->pool1024->allocate();
if (allocated != nullptr) return allocated;
}
if (size <= 4096) {
void* allocated = this->pool4096->allocate();
if (allocated != nullptr) return allocated;
}
if (size <= 65536) {
void* allocated = this->pool65536->allocate();
if (allocated != nullptr) return allocated;
}
// cant be freed (yet)
return this->getMemorySlice(size);
}
void free(void* pointer) const {
if (pointer > this->poolEndIndicator) {
return;
}
if (pointer > this->pool65536->getLocation()) {
pool65536->free(pointer);
}
if (pointer > this->pool4096->getLocation()) {
pool4096->free(pointer);
}
if (pointer > this->pool1024->getLocation()) {
pool1024->free(pointer);
}
if (pointer > this->pool256->getLocation()) {
pool256->free(pointer);
}
if (pointer > this->pool128->getLocation()) {
pool128->free(pointer);
}
if (pointer > this->pool64->getLocation()) {
pool64->free(pointer);
}
if (pointer > this->pool32->getLocation()) {
pool32->free(pointer);
}
if (pointer > this->pool16->getLocation()) {
pool16->free(pointer);
}
if (pointer > this->pool8->getLocation()) {
pool8->free(pointer);
}
if (pointer > this->pool4->getLocation()) {
pool4->free(pointer);
}
if (pointer > this->pool2->getLocation()) {
pool2->free(pointer);
}
if (pointer > this->pool1->getLocation()) {
pool1->free(pointer);
}
}
void* reallocate(void* ptr, size_t size) {
void* reallocated = this->allocate(size);
this->free(ptr);
return reallocated;
}
};
#endif //MEMORYMANAGER_H