-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathheap.c
142 lines (107 loc) · 3.46 KB
/
heap.c
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
#include "include/heap.h"
#include "include/llist.h"
uint offset = 8;
void init_heap(heap_t *heap, long start) {
node_t *init_region = (node_t *) start;
init_region->hole = 1;
init_region->size = (HEAP_INIT_SIZE) - sizeof(node_t) - sizeof(footer_t);
create_foot(init_region);
add_node(heap->bins[get_bin_index(init_region->size)], init_region);
heap->start = (void *) start;
heap->end = (void *) (start + HEAP_INIT_SIZE);
}
void *heap_alloc(heap_t *heap, size_t size) {
uint index = get_bin_index(size);
bin_t *temp = (bin_t *) heap->bins[index];
node_t *found = get_best_fit(temp, size);
while (found == NULL) {
if (index + 1 >= BIN_COUNT)
return NULL;
temp = heap->bins[++index];
found = get_best_fit(temp, size);
}
if ((found->size - size) > (overhead + MIN_ALLOC_SZ)) {
node_t *split = (node_t *) (((char *) found + sizeof(node_t) + sizeof(footer_t)) + size);
split->size = found->size - size - sizeof(node_t) - sizeof(footer_t);
split->hole = 1;
create_foot(split);
uint new_idx = get_bin_index(split->size);
add_node(heap->bins[new_idx], split);
found->size = size;
create_foot(found);
}
found->hole = 0;
remove_node(heap->bins[index], found);
node_t *wild = get_wilderness(heap);
if (wild->size < MIN_WILDERNESS) {
uint success = expand(heap, 0x1000);
if (success == 0) {
return NULL;
}
}
else if (wild->size > MAX_WILDERNESS) {
contract(heap, 0x1000);
}
found->prev = NULL;
found->next = NULL;
return &found->next;
}
void heap_free(heap_t *heap, void *p) {
bin_t *list;
footer_t *new_foot, *old_foot;
node_t *head = (node_t *) ((char *) p - offset);
if (head == (node_t *) (uintptr_t) heap->start) {
head->hole = 1;
add_node(heap->bins[get_bin_index(head->size)], head);
return;
}
node_t *next = (node_t *) ((char *) get_foot(head) + sizeof(footer_t));
footer_t *f = (footer_t *) ((char *) head - sizeof(footer_t));
node_t *prev = f->header;
if (prev->hole) {
list = heap->bins[get_bin_index(prev->size)];
remove_node(list, prev);
prev->size += overhead + head->size;
new_foot = get_foot(head);
new_foot->header = prev;
head = prev;
}
if (next->hole) {
list = heap->bins[get_bin_index(next->size)];
remove_node(list, next);
head->size += overhead + next->size;
old_foot = get_foot(next);
old_foot->header = 0;
next->size = 0;
next->hole = 0;
new_foot = get_foot(head);
new_foot->header = head;
}
head->hole = 1;
add_node(heap->bins[get_bin_index(head->size)], head);
}
uint expand(heap_t *heap, size_t sz) {
return 0;
}
void contract(heap_t *heap, size_t sz) {
return;
}
uint get_bin_index(size_t sz) {
uint index = 0;
sz = sz < 4 ? 4 : sz;
while (sz >>= 1) index++;
index -= 2;
if (index > BIN_MAX_IDX) index = BIN_MAX_IDX;
return index;
}
void create_foot(node_t *head) {
footer_t *foot = get_foot(head);
foot->header = head;
}
footer_t *get_foot(node_t *node) {
return (footer_t *) ((char *) node + sizeof(node_t) + node->size);
}
node_t *get_wilderness(heap_t *heap) {
footer_t *wild_foot = (footer_t *) ((char *) heap->end - sizeof(footer_t));
return wild_foot->header;
}