-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathheap.cpp
302 lines (233 loc) · 6.44 KB
/
heap.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
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
#include "general.h"
#include "heap.h"
#include "rtlib.h"
#include "term.h"
#define block_used(b) (1 == ((b)->head.parts.used))
#define block_size(b) ((b)->head.sz & 0x7fffffff)
#define MIN_SPLIT_DELTA 512
// The kernel heap
heap kheap;
// The applications heap
heap appheap;
static const uint32 block_size = sizeof(block);
void heap_init(heap *h, void *start, size_t size) {
h->start = start;
h->size = size;
h->alloc = 0;
h->f = h->l = h->ff = h->lf = NULL;
}
static void *heap_sbrk(heap *h, int32 size) {
size_t a = h->alloc;
if (size < 0) {
}
// Maintain word alignment
size = (size + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
if (a + size > h->size) {
return NULL;
} else if (a + size < 0) {
panic(L"Incorrect call on heap_sbrk");
}
h->alloc = a + size;
return CAST(void *, CAST(uint8 *, h->start) + a);
}
static inline block *get_block_ptr(void *ptr) {
// The ptr is the pointer to a data section of a block
// We can add the size of two void pointer (to be at the end), and substract
// the block size;
return CAST(block *, CAST(uint8 *, ptr) + (2 * sizeof(void *)) - block_size);
}
static inline void *get_data_ptr(block *blk) {
return CAST(void *, &blk->tail.notfree.data);
}
// Find the smallest block of at least the desired size
static block *find_block(heap *h, size_t sz) {
block *scout = h->ff;
while (NULL != scout) {
if (block_size(scout) >= sz) {
break;
}
scout = scout->tail.free.next;
}
return scout;
}
static block *split(heap *h, block *bk, size_t min_size) {
// Fix the min size so we can break on multiple of four
if (min_size % 4 != 0) {
min_size += 4 - (min_size % 4);
}
uint8 *new_end_of_zone =
CAST(uint8 *, bk) + (min_size + block_size - (2 * sizeof(void *)));
block *left_bound = bk->prev;
block *right_bound = bk->next;
block *other_half = CAST(block *, new_end_of_zone);
other_half->head.sz =
(bk->head.sz - min_size) - block_size + (2 * sizeof(void *));
bk->head.sz = min_size;
other_half->head.parts.used = 0;
bk->prev = left_bound;
bk->next = other_half;
other_half->prev = bk;
other_half->next = right_bound;
if (NULL != right_bound) {
right_bound->prev = other_half;
} else {
h->l = other_half;
}
if ((NULL != right_bound) && CAST(uint8 *, right_bound) !=
(CAST(uint8 *, other_half) + block_size +
other_half->head.sz - 2 * sizeof(void *))) {
debug_write(CAST(uint8 *, right_bound));
debug_write((CAST(uint8 *, other_half) + block_size + other_half->head.sz -
2 * sizeof(void *)));
panic(L"!");
}
return other_half;
}
/**
* Extend the heap enough to have a block of size sz.
*/
static block *extend_heap(heap *h, size_t sz) {
// There is no cost in extending exactly the size we need.
size_t total_sz = sz + block_size - (sizeof(void *) * 2);
block *blk = CAST(block *, heap_sbrk(h, total_sz));
if (NULL == blk) {
return NULL;
}
blk->head.parts.used = 0;
uint32 diff =
((CAST(uint8 *, h->start) + h->alloc) - CAST(uint8 *, blk)) - total_sz;
blk->head.sz = sz + diff;
blk->next = NULL;
blk->prev = h->l;
if (NULL != h->l) {
h->l->next = blk;
}
if (NULL == h->f) {
h->f = blk;
}
h->l = blk;
return blk;
}
static void detach(heap *h, block *bl) {
block *prev = bl->tail.free.prev;
block *next = bl->tail.free.next;
if (NULL != prev) {
prev->tail.free.next = next;
} else {
// bl must have been the first
h->ff = next;
}
if (NULL != next) {
next->tail.free.prev = prev;
} else {
// bl must have been the last
h->lf = prev;
}
}
static void mark_as_free(heap *h, block *bk) {
bk->tail.free.next = NULL;
bk->tail.free.prev = NULL;
bk->head.parts.used = 0;
block *scout = h->lf;
while (NULL != scout && (block_size(scout) >= block_size(bk))) {
scout = scout->tail.free.prev;
}
if (NULL == scout) { // we are the first one
block *old_first = h->ff;
old_first->tail.free.prev = bk;
bk->tail.free.next = old_first;
h->ff = bk;
} else {
// We insert after scout
block *old_next = scout->tail.free.next;
if (NULL == old_next) { // end of chain
h->lf = bk;
} else {
old_next->tail.free.prev = bk;
bk->tail.free.next = old_next;
}
scout->tail.free.next = bk;
bk->tail.free.prev = scout;
}
}
static block *get_block(heap *h, size_t sz) {
block *blk = find_block(h, sz);
if (NULL == blk) {
// need to allocate
blk = extend_heap(h, sz);
} else {
detach(h, blk);
if (block_size(blk) >= MIN_SPLIT_DELTA + sz) {
block *other_half = split(h, blk, sz);
mark_as_free(h, other_half);
}
}
if (NULL != blk) {
blk->head.parts.used = 1;
}
return blk;
}
static block *merge(heap *h, block *leftmost, uint8 until) {
block *scout = leftmost;
block *left_bound = leftmost->prev;
block *right_bound = NULL;
size_t nsize = 0;
for (uint8 i = 0; i < until; ++i) {
if (!block_used(scout)) {
detach(h, scout);
}
nsize += block_size + scout->head.sz - (2 * sizeof(block *));
scout = scout->next;
}
right_bound = scout;
leftmost->prev = left_bound;
leftmost->next = right_bound;
if (NULL != left_bound) {
left_bound->next = leftmost;
} else {
// leftmost is the start of the chain
h->f = leftmost;
}
if (NULL != right_bound) {
right_bound->prev = leftmost;
} else {
// The last block we merged was the last one
h->l = leftmost;
}
leftmost->head.sz = nsize - block_size + (2 * sizeof(void *));
leftmost->head.parts.used = 0;
return leftmost;
}
void heap_free(heap *h, void *ptr) {
block *bk = get_block_ptr(ptr);
block *left = bk->prev;
block *right = bk->next;
bool left_free = (NULL != left && !block_used(left));
bool right_free = (NULL != right && !block_used(right));
if (left_free && right_free) {
bk = merge(h, left, 2);
} else if (left_free) {
bk = merge(h, left, 1);
} else if (right_free) {
bk = merge(h, bk, 1);
}
mark_as_free(h, bk);
}
#define MIN_ALLOC_SIZE (2 * sizeof(void *))
void *heap_malloc(heap *h, size_t size) {
if (size < MIN_ALLOC_SIZE) {
size = MIN_ALLOC_SIZE;
}
block *blk = get_block(h, size);
if (NULL == blk) {
return NULL;
}
void *ptr = get_data_ptr(blk);
if ((CAST(uint32, ptr) % 4) != 0) {
panic(L"Not aligned");
}
return ptr;
}
#undef block_used
#undef block_size
#undef MIN_SPLIT_DELTA