-
Notifications
You must be signed in to change notification settings - Fork 0
/
Allocator.h
186 lines (141 loc) · 3.76 KB
/
Allocator.h
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
//
// Created by Marrony Neris on 11/10/15.
//
#ifndef ALLOCATOR_H
#define ALLOCATOR_H
#include <stdio.h>
#include <assert.h>
#include <memory.h>
class LinearAllocator {
public:
LinearAllocator(void* begin, size_t size) : begin((int8_t*)begin), current((int8_t*)begin), end((int8_t*)begin + size) { }
void* allocate(size_t size) {
if (current + size > end)
return nullptr;
void* data = current;
current += size;
return data;
}
size_t memoryUsed() {
return current - begin;
}
void reset() {
current = begin;
}
private:
int8_t* begin;
int8_t* end;
int8_t* current;
};
class HeapAllocator {
public:
HeapAllocator() : freeList(nullptr), bytesAllocated(0), numberAllocations(0) { }
~HeapAllocator() {
assert(numberAllocations == 0);
assert(bytesAllocated == 0);
clearMemory();
}
void clearMemory() {
while(freeList) {
void* ptr = freeList;
freeList = freeList->next;
free(ptr);
}
}
void* allocate(size_t size) {
FreeList* current = freeList;
FreeList* previous = nullptr;
size = roundSize(size);
while(current) {
size_t percent = size * 100 / current->size;
if(percent >= 75 && percent <= 100) break;
previous = current;
current = current->next;
}
if(!current) {
bytesAllocated += size;
numberAllocations++;
FreeList* header = (FreeList*) malloc(size + sizeof(FreeList));
header->size = size;
return header->data;
}
if(current == freeList)
freeList = freeList->next;
else
previous->next = current->next;
bytesAllocated += current->size;
numberAllocations++;
return current->data;
}
void* reallocate(void* ptr, size_t newSize) {
if(ptr != nullptr) {
FreeList* header = (FreeList*) ptr - 1;
size_t oldSize = header->size;
if (oldSize >= newSize)
return ptr;
deallocate(ptr);
void* newPtr = allocate(newSize);
memcpy(newPtr, ptr, oldSize);
return newPtr;
}
return allocate(newSize);
}
void deallocate(void* ptr) {
assert(ptr != nullptr);
FreeList* node = (FreeList*) ptr - 1;
assert(alreadyDeallocated(node) == false);
numberAllocations--;
bytesAllocated -= node->size;
if (node->size > 128*1024) {
free(node);
} else {
node->next = freeList;
freeList = node;
}
}
size_t memoryUsed() {
return bytesAllocated;
}
void dumpFreeList() {
printf("numberAllocations: %ld\n", numberAllocations);
printf("bytesAllocated: %ld\n", bytesAllocated);
FreeList* f = freeList;
while(f) {
printf("free: %ld\n", f->size);
f = f->next;
}
}
private:
struct FreeList {
size_t size;
FreeList* next;
char data[];
};
size_t roundSize(size_t size) {
const size_t blockSize = 128;
size_t blocks = size / blockSize;
if((size % blockSize) != 0)
blocks++;
return blocks * blockSize;
}
bool alreadyDeallocated(FreeList* node) {
FreeList* f = freeList;
while(f != nullptr) {
if(f == node)
return true;
f = f->next;
}
return false;
}
FreeList* freeList;
size_t numberAllocations;
size_t bytesAllocated;
};
template<int SIZE>
class PoolAllocator {
public:
private:
};
class BuddyAllocator {
};
#endif //ALLOCATOR_H