-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-memory-allocator.cpp
More file actions
425 lines (299 loc) · 10 KB
/
Copy pathcustom-memory-allocator.cpp
File metadata and controls
425 lines (299 loc) · 10 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include <iostream>
#include <cstring>
#include <stdexcept>
using namespace std;
// CONSTANTS
const size_t HEAP_SIZE = 1024 * 1024;
unsigned char HEAP[HEAP_SIZE];
const int MAX_VECTORS = 128;
enum VecType { VEC_INT = 1, VEC_DOUBLE = 2 };
// FUNCTION DECLARATIONS
// control area
size_t& heap_used();
size_t& id_count();
size_t* id_table();
size_t control_area_size();
// header access
size_t header_size();
size_t* h_size(size_t h);
size_t* h_cap(size_t h);
size_t* h_type(size_t h);
size_t* h_block(size_t h);
size_t data_start(size_t h);
// utilities
size_t type_size(size_t t);
void heap_check(size_t bytes);
void validate_id(int id);
// vector core
int vector_create(int type, size_t capacity);
void vector_destroy(int id);
void vector_reserve(int id, size_t new_cap);
void vector_push_back(int id, const void* value);
void* vector_at(int id, size_t index);
// extra operations
void print_vector(int id);
void vector_push_at(int id, size_t index, const void* value);
void vector_pop_back(int id);
void vector_erase_range(int id, size_t start, size_t end);
void vector_clear(int id);
int vector_copy(int id);
void print_memory_status();
// MAIN
int main() {
// initialize heap control area
heap_used() = control_area_size();
id_count() = 0;
memset(id_table(), 0, MAX_VECTORS * sizeof(size_t));
try {
cout <<"CUSTOM VECTOR FULL TEST : \n\n";
int myVector = vector_create(VEC_INT, 5);
cout << "Created int vector (myVector)\n";
int initial_vals[] = {2, 5, 7, 13, 24};
for (int i = 0; i < 5; ++i)
vector_push_back(myVector, &initial_vals[i]);
cout << "After pushing 2,5,7,13,24: ";
print_vector(myVector);
int val19 = 19;
vector_push_at(myVector, 2, &val19);
cout << "After inserting 19 at index 2: ";
print_vector(myVector);
vector_pop_back(myVector);
cout << "After popping last element: ";
print_vector(myVector);
vector_erase_range(myVector, 1, 3);
cout << "After erasing range index 1 to 3: ";
print_vector(myVector);
vector_clear(myVector);
cout << "After clearing vector: ";
print_vector(myVector);
int new_vals[] = {8, 21, 17, 36, 4};
for (int i = 0; i < 5; ++i)
vector_push_back(myVector, &new_vals[i]);
cout << "After adding 8,21,17,36,4: ";
print_vector(myVector);
int* third_elem = (int*)vector_at(myVector, 2);
cout << "3rd element using vector_at: " << *third_elem << "\n";
int myVector2 = vector_copy(myVector);
cout << "Copied myVector into myVector2:\n";
print_vector(myVector2);
cout << "\nMemory status after copy:\n";
print_memory_status();
size_t remaining_bytes = HEAP_SIZE - heap_used();
size_t double_capacity =
(remaining_bytes - header_size()) / sizeof(double);
int myVector3 = vector_create(VEC_DOUBLE, double_capacity);
cout << "\nCreated myVector3 (double vector) using remaining heap capacity.\n";
double doubleValue = 1.1;
for (size_t i = 0; i < double_capacity; ++i)
vector_push_back(myVector3, &doubleValue);
cout << "Filled myVector3 completely.\nWhole Heap is full!!!!";
cout << "\nFinal Heap Status:\n";
print_memory_status();
cout << "\nAttempting to push beyond heap capacity...\n";
try {
vector_push_back(myVector3, &doubleValue);
}
catch (const exception& e) {
cout << "Exception caught successfully: " << e.what() << "\n";
}
cout << "\nHeap status after failed push attempt:\n";
print_memory_status();
cout<< "\nDestroying all three vector.....\n";
vector_destroy(myVector);
vector_destroy(myVector2);
vector_destroy(myVector3);
cout << "\nAll vectors destroyed successfully.\n";
}
catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
}
return 0;
}
// FUNCTION DEFINITIONS :
// CONTROL AREA
size_t& heap_used() { return *(size_t*)HEAP; }
size_t& id_count() { return *(size_t*)(HEAP + sizeof(size_t)); }
size_t* id_table() { return (size_t*)(HEAP + 2 * sizeof(size_t)); }
size_t control_area_size() {
return 2 * sizeof(size_t) + MAX_VECTORS * sizeof(size_t);
}
//HEADER ACCESS
size_t header_size() { return 4 * sizeof(size_t); }
size_t* h_size(size_t h) { return (size_t*)(HEAP + h); }
size_t* h_cap(size_t h) { return (size_t*)(HEAP + h + sizeof(size_t)); }
size_t* h_type(size_t h) { return (size_t*)(HEAP + h + 2 * sizeof(size_t)); }
size_t* h_block(size_t h) { return (size_t*)(HEAP + h + 3 * sizeof(size_t)); }
size_t data_start(size_t h) { return h + header_size(); }
// UTILITIES
size_t type_size(size_t t) {
if (t == VEC_INT) return sizeof(int);
if (t == VEC_DOUBLE) return sizeof(double);
throw runtime_error("Invalid type");
}
void heap_check(size_t bytes) {
if (heap_used() + bytes > HEAP_SIZE)
throw runtime_error("Heap overflow");
}
void validate_id(int id) {
if (id < 0 || id >= (int)id_count())
throw runtime_error("Invalid ID");
if (id_table()[id] == 0)
throw runtime_error("Destroyed vector");
}
// VECTOR CORE
int vector_create(int type, size_t capacity) {
if (id_count() >= MAX_VECTORS)
throw runtime_error("Max vectors reached");
size_t elem = type_size(type);
size_t total = header_size() + capacity * elem;
heap_check(total);
size_t h = heap_used();
*h_size(h) = 0;
*h_cap(h) = capacity;
*h_type(h) = type;
*h_block(h) = total;
heap_used() += total;
id_table()[id_count()] = h;
return id_count()++;
}
void vector_destroy(int id) {
validate_id(id);
size_t h = id_table()[id];
size_t block = *h_block(h);
size_t end = h + block;
memmove(HEAP + h, HEAP + end, heap_used() - end);
heap_used() -= block;
for (size_t i = 0; i < id_count(); i++)
if (id_table()[i] > h)
id_table()[i] -= block;
id_table()[id] = 0;
}
void vector_reserve(int id, size_t new_cap) {
validate_id(id);
size_t old_h = id_table()[id];
size_t old_size = *h_size(old_h);
size_t old_cap = *h_cap(old_h);
size_t type = *h_type(old_h);
if (new_cap <= old_cap)
return;
size_t elem = type_size(type);
size_t new_total = header_size() + new_cap * elem;
heap_check(new_total);
size_t new_h = heap_used();
*h_size(new_h) = old_size;
*h_cap(new_h) = new_cap;
*h_type(new_h) = type;
*h_block(new_h) = new_total;
memcpy(HEAP + data_start(new_h),
HEAP + data_start(old_h),
old_size * elem);
heap_used() += new_total;
size_t old_block = *h_block(old_h);
size_t old_end = old_h + old_block;
memmove(HEAP + old_h, HEAP + old_end, heap_used() - old_end);
heap_used() -= old_block;
for (size_t i = 0; i < id_count(); i++)
if (id_table()[i] > old_h)
id_table()[i] -= old_block;
id_table()[id] = new_h - old_block;
}
void vector_push_back(int id, const void* value) {
validate_id(id);
size_t h = id_table()[id];
size_t size = *h_size(h);
size_t cap = *h_cap(h);
size_t type = *h_type(h);
if (size == cap) {
vector_reserve(id, cap * 2 + 1);
h = id_table()[id];
}
size = *h_size(h);
size_t elem = type_size(type);
memcpy(HEAP + data_start(h) + size * elem, value, elem);
(*h_size(h))++;
}
void* vector_at(int id, size_t index) {
validate_id(id);
size_t h = id_table()[id];
if (index >= *h_size(h))
throw out_of_range("Index out of range");
return HEAP + data_start(h)
+ index * type_size(*h_type(h));
}
// EXTRA OPERATIONS
void print_vector(int id) {
validate_id(id);
size_t h = id_table()[id];
size_t size = *h_size(h);
size_t type = *h_type(h);
cout << "[ ";
if (type == VEC_INT) {
for (size_t i = 0; i < size; ++i)
cout << *(int*)vector_at(id, i) << " ";
}
else if (type == VEC_DOUBLE) {
for (size_t i = 0; i < size; ++i)
cout << *(double*)vector_at(id, i) << " ";
}
cout << "]\n";
}
void vector_push_at(int id, size_t index, const void* value) {
validate_id(id);
size_t h = id_table()[id];
size_t size = *h_size(h);
size_t elem = type_size(*h_type(h));
if (index > size)
throw out_of_range("Push index out of range");
if (size == *h_cap(h)) {
vector_reserve(id, *h_cap(h) * 2 + 1);
h = id_table()[id];
}
size = *h_size(h);
memmove(HEAP + data_start(h) + (index + 1) * elem,
HEAP + data_start(h) + index * elem,
(size - index) * elem);
memcpy(HEAP + data_start(h) + index * elem, value, elem);
(*h_size(h))++;
}
void vector_pop_back(int id) {
validate_id(id);
if (*h_size(id_table()[id]) == 0)
throw runtime_error("Pop from empty vector");
(*h_size(id_table()[id]))--;
}
void vector_erase_range(int id, size_t start, size_t end) {
validate_id(id);
size_t h = id_table()[id];
size_t size = *h_size(h);
size_t elem = type_size(*h_type(h));
if (start > end || end >= size)
throw out_of_range("Invalid erase range");
size_t count = end - start + 1;
memmove(HEAP + data_start(h) + start * elem,
HEAP + data_start(h) + (end + 1) * elem,
(size - end - 1) * elem);
*h_size(h) -= count;
}
void vector_clear(int id) {
validate_id(id);
*h_size(id_table()[id]) = 0;
}
int vector_copy(int id) {
validate_id(id);
size_t h = id_table()[id];
size_t size = *h_size(h);
size_t cap = *h_cap(h);
size_t type = *h_type(h);
int new_id = vector_create(type, cap);
memcpy(HEAP + data_start(id_table()[new_id]),
HEAP + data_start(h),
size * type_size(type));
*h_size(id_table()[new_id]) = size;
return new_id;
}
void print_memory_status() {
cout << "Heap used: " << heap_used()
<< " bytes\nRemaining: "
<< HEAP_SIZE - heap_used()
<< " bytes\n";
}