-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsuper_heap.c
More file actions
57 lines (46 loc) · 911 Bytes
/
super_heap.c
File metadata and controls
57 lines (46 loc) · 911 Bytes
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
#include <sys/mman.h>
#include <cdefs.h>
#include "super.h"
/* Heap is only used for large buffers in load_dir_ents()
and ctrl code. Custom routines because the ones from util.h
rely on fail() atm and pid 1 should be careful with that. */
static void* brk;
static void* ptr;
static void* end;
void setup_heap(void)
{
brk = sys_brk(NULL);
ptr = end = brk;
}
static long alignto(long x, long size)
{
return x + (size - x % size) % size;
}
void* heap_alloc(int len)
{
void* old = ptr;
void* new = old + len;
void* req = old + alignto(len, PAGE);
if(new > end)
end = sys_brk(req);
if(new > end)
return NULL;
ptr = new;
return old;
}
void trim_heap(void* old)
{
if(old < brk || old > end)
return;
ptr = old;
request(F_FLUSH_HEAP);
}
void flush_heap(void)
{
if(ptr > brk)
report("retaining non-empty heap", NULL, 0);
else if(end == brk)
return;
else
end = sys_brk(brk);
}