-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAllocator.cpp
More file actions
71 lines (51 loc) · 1.59 KB
/
Copy pathAllocator.cpp
File metadata and controls
71 lines (51 loc) · 1.59 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
#include "Allocator.hpp"
#include "LispNode.h"
static void call_destructor(void *pointer, uint8_t tag) {
if(tag == 0) {
static_cast<LispNode *>(pointer)->~LispNode();
}
else {
static_cast<Box *>(pointer)->~Box();
}
}
void *allocate_generic(CircularQueue &queue, size_t size, uint8_t tag) {
void *recycled = queue.dequeue();
if(recycled) {
call_destructor(recycled, tag);
return recycled;
}
CounterType *pointer = (CounterType *) Allocate(size + sizeof(CounterType));
if(!pointer) {
fputs("Out of memory: halt", stdout);
exit(EXIT_FAILURE);
}
*pointer = 0;
return pointer + 1;
}
void deallocate_generic(void *pointer) noexcept {
Deallocate(((CounterType *) pointer) - 1);
}
bool process_deletions_generic(CircularQueue &queue, uint8_t tag) {
bool deleted = false;
while(!queue.is_empty_or_overflown()) {
void *pointer = queue.dequeue();
call_destructor(pointer, tag);
deallocate_generic(pointer);
deleted = true;
}
return deleted;
}
void reinit_generic(CircularQueue &queue, uint8_t tag) {
void **old_queue = queue.reinit();
for(size_t current = 0; current < CircularQueue::QUEUE_SIZE; current++) {
call_destructor(old_queue[current], tag);
deallocate_generic(old_queue[current]);
while(process_deletions_generic(queue, tag)) {}
}
Deallocate(old_queue);
}
// Definitions of the static deletion queues
template<>
CircularQueue Allocator<LispNode>::deletion_queue{};
template<>
CircularQueue Allocator<Box>::deletion_queue{};