-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
142 lines (109 loc) · 3.28 KB
/
main.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 "time.h"
#include "heap.h"
#define break_if_null(x) if(!x) break
void test_heap_alloc() {
heap_clear();
int count = 1;
while (1) {
void* alloc1 = heap_alloc(4);
break_if_null(alloc1);
count++;
void* alloc2 = heap_alloc(4);
break_if_null(alloc2);
void* alloc3 = heap_alloc(4);
break_if_null(alloc3);
count++;
void* alloc4 = heap_alloc(4);
break_if_null(alloc4);
void* alloc6 = heap_alloc(4);
break_if_null(alloc6);
count++;
heap_free(alloc2);
heap_free(alloc4);
void* alloc7 = heap_alloc(8);
break_if_null(alloc7);
count++;
// heap_free(alloc1);
// heap_free(alloc3);
// heap_free(alloc6);
// heap_free(alloc7);
// print_all_nodes();
// heap_merge_fragment();
// print_all_nodes();
}
printf("test_heap_alloc: %u\n", count);
}
void test_heap_alloc_with_fragment_merge() {
heap_clear();
int count = 1;
while (1) {
void* alloc1 = heap_alloc(4);
break_if_null(alloc1);
count++;
void* alloc2 = heap_alloc(4);
break_if_null(alloc2);
void* alloc3 = heap_alloc(4);
break_if_null(alloc3);
count++;
void* alloc4 = heap_alloc(4);
break_if_null(alloc4);
void* alloc6 = heap_alloc(4);
break_if_null(alloc6);
count++;
heap_free(alloc2);
heap_free(alloc4);
void* alloc7 = heap_alloc(8);
break_if_null(alloc7);
count++;
// heap_free(alloc1);
// heap_free(alloc3);
// heap_free(alloc6);
// heap_free(alloc7);
// print_all_nodes();
heap_merge_fragment();
// print_all_nodes();
}
printf("test_heap_alloc_with_fragment_merge: %u\n", count);
}
void test_malloc() {
int count = 1;
size_t s = 0;
while (1) {
void* alloc1 = malloc(4);
s += 4 + heap_node_overhead;
if (!alloc1 || s >= HEAP_SIZE) break;
count++;
void* alloc2 = malloc(4);
s += 4 + heap_node_overhead;
if (!alloc2 || s >= HEAP_SIZE) break;
void* alloc3 = malloc(4);
s += 4 + heap_node_overhead;
if (!alloc3 || s >= HEAP_SIZE) break;
count++;
void* alloc4 = malloc(4);
s += 4 + heap_node_overhead;
if (!alloc4 || s >= HEAP_SIZE) break;
void* alloc6 = malloc(4);
s += 4 + heap_node_overhead;
if (!alloc6 || s >= HEAP_SIZE) break;
count++;
heap_free(alloc2);
heap_free(alloc4);
void* alloc7 = malloc(8);
s += 8 + heap_node_overhead;
if (!alloc7 || s >= HEAP_SIZE) break;
count++;
// heap_free(alloc1);
// heap_free(alloc3);
// heap_free(alloc6);
// heap_free(alloc7);
}
printf("test_malloc: %u\n", count);
}
int main(int args, char* argv[]) {
heap_init();
printf("%d, %d, %d, %d\n", sizeof(size_t), heap_node_overhead, sizeof(node_t), sizeof(footer_t));
test_heap_alloc();
test_heap_alloc_with_fragment_merge();
test_malloc();
}