-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkallocator.c
81 lines (58 loc) · 1.81 KB
/
kallocator.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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "kallocator.h"
struct KAllocator {
enum allocation_algorithm aalgorithm;
int size;
void* memory;
// Some other data members you want,
// such as lists to record allocated/free memory
};
struct KAllocator kallocator;
void initialize_allocator(int _size, enum allocation_algorithm _aalgorithm) {
assert(_size > 0);
kallocator.aalgorithm = _aalgorithm;
kallocator.size = _size;
kallocator.memory = malloc((size_t)kallocator.size);
// Add some other initialization
}
void destroy_allocator() {
free(kallocator.memory);
// free other dynamic allocated memory to avoid memory leak
}
void* kalloc(int _size) {
void* ptr = NULL;
// Allocate memory from kallocator.memory
// ptr = address of allocated memory
return ptr;
}
void kfree(void* _ptr) {
assert(_ptr != NULL);
}
int compact_allocation(void** _before, void** _after) {
int compacted_size = 0;
// compact allocated memory
// update _before, _after and compacted_size
return compacted_size;
}
int available_memory() {
int available_memory_size = 0;
// Calculate available memory size
return available_memory_size;
}
void print_statistics() {
int allocated_size = 0;
int allocated_chunks = 0;
int free_size = 0;
int free_chunks = 0;
int smallest_free_chunk_size = kallocator.size;
int largest_free_chunk_size = 0;
// Calculate the statistics
printf("Allocated size = %d\n", allocated_size);
printf("Allocated chunks = %d\n", allocated_chunks);
printf("Free size = %d\n", free_size);
printf("Free chunks = %d\n", free_chunks);
printf("Largest free chunk size = %d\n", largest_free_chunk_size);
printf("Smallest free chunk size = %d\n", smallest_free_chunk_size);
}