-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
47 lines (36 loc) · 1.1 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
#include <stdio.h>
#include "kallocator.h"
int main(int argc, char* argv[]) {
initialize_allocator(100, FIRST_FIT);
// initialize_allocator(100, BEST_FIT);
// initialize_allocator(100, WORST_FIT);
printf("Using first fit algorithm on memory size 100\n");
int* p[50] = {NULL};
for(int i=0; i<10; ++i) {
p[i] = kalloc(sizeof(int));
if(p[i] == NULL) {
printf("Allocation failed\n");
continue;
}
*(p[i]) = i;
printf("p[%d] = %p ; *p[%d] = %d\n", i, p[i], i, *(p[i]));
}
print_statistics();
for(int i=0; i<10; ++i) {
if(i%2 == 0)
continue;
printf("Freeing p[%d]\n", i);
kfree(p[i]);
p[i] = NULL;
}
printf("available_memory %d", available_memory());
void* before[100] = {NULL};
void* after[100] = {NULL};
compact_allocation(before, after);
print_statistics();
// You can assume that the destroy_allocator will always be the
// last funciton call of main function to avoid memory leak
// before exit
destroy_allocator();
return 0;
}