-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathslab_alloc.c
More file actions
153 lines (128 loc) · 3.53 KB
/
slab_alloc.c
File metadata and controls
153 lines (128 loc) · 3.53 KB
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
143
144
145
146
147
148
149
150
151
152
153
#include <slab_alloc.h>
#include <slab.h>
#include <page_alloc.h>
#include <utils.h>
#define MAX_POWER 10
#define MAX_SIZE (1 << MAX_POWER)
#define DEBUG_ALLOC 1
#if DEBUG_ALLOC
#include <print.h>
#define debug(...) printa(__VA_ARGS__)
#else
#define debug(...) do {} while(0)
#endif
static int initialized = 0;
static slab_cache caches[MAX_POWER];
typedef struct big_object {
void *start;
size_t page_power;
struct big_object *next;
} big_object;
slab_cache big_object_cache = { .size = sizeof(big_object) };
big_object first_big = { .next = NULL };
static inline void init_slab_allocator() {
if (!initialized) {
for (int i = 0; i < MAX_POWER; ++i) {
slab_cache_init(&caches[i], 1 << i);
}
}
initialized = 1;
}
static inline int get_2_power(size_t size) {
int power = 0;
while (size) {
++power;
size /= 2;
}
return power;
}
void *slab_alloc(size_t size) {
init_slab_allocator();
if (size == 0) {
return NULL;
}
if (size > MAX_SIZE) {
big_object *bo = slab_cache_alloc(&big_object_cache);
bo->next = first_big.next;
first_big.next = bo;
bo->page_power = get_2_power(size/PAGE_SIZE);
bo->start = page_alloc(bo->page_power);
return bo->start;
}
int power_of_2 = get_2_power(size-1);
return slab_cache_alloc(&caches[power_of_2]);
}
void slab_free(void *addr) {
if (!addr) {
return;
}
big_object *bo = &first_big;
while (bo->next) {
if (bo->next->start == addr) {
big_object *tmp = bo->next;
page_free(tmp->start, tmp->page_power);
bo->next = tmp->next;
slab_cache_free(&big_object_cache, tmp);
return;
}
bo = bo->next;
}
for (int i = 0; i < MAX_POWER; ++i) {
if (slab_cache_free(&caches[i], addr)) {
return;
}
}
}
void *slab_realloc(void *addr, size_t size) {
if (!addr) {
return slab_alloc(size);
}
if (!size) {
slab_free(addr);
}
big_object *bo = &first_big;
while (bo->next) {
if (bo->next->start == addr) {
size_t object_size = (1 << bo->next->page_power)*PAGE_SIZE;
if (object_size >= size) {
return addr;
}
void *new_pos = slab_alloc(size);
memcpy(new_pos, addr, object_size);
big_object *tmp = bo->next;
bo->next = tmp->next;
page_free(tmp->start, tmp->page_power);
slab_cache_free(&big_object_cache, bo);
return new_pos;
}
bo = bo->next;
}
for (int i = MAX_POWER - 1; i >= 0; --i) {
if (in_this_slab_cache(&caches[i], addr)) {
size_t slot_size = 1 << i;
if (slot_size >= size) {
return addr;
}
void *new_pos = slab_alloc(size);
memcpy(new_pos, addr, slot_size);
slab_cache_free(&caches[i], addr);
return new_pos;
}
}
return NULL;
}
void slab_mem_dump(void) {
#if DEBUG_ALLOC
debug("==========================\nBegin slab dumping\n===========================\n");
big_object *bo = first_big.next;
while (bo) {
debug("Big object at: %p (%x)\n", bo->start, bo->page_power);
bo = bo->next;
}
for (int i = 0; i < MAX_POWER; ++i) {
debug("Slab %x\n", i);
slab_cache_mem_dump(&caches[i]);
}
debug("==========================\nEnd slab dumping\n===========================\n");
#endif
}