-
Notifications
You must be signed in to change notification settings - Fork 0
/
SLAB_allocator.c
198 lines (168 loc) · 6.13 KB
/
SLAB_allocator.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stddef.h>
#include "SLAB_allocator.h"
#include "assert.h"
#define MALLOC_SMALL_MAX_SIZE 256
static struct spinlock slab_lock;
struct bufctl {
void* buf_adr;
struct bufctl* next_ctl;
struct slab* slab_ctl;
};
void* heads;
void* get_page_adr(void* adr) {
return (void*)((uint64_t)adr&(~(PAGE_SIZE - 1)));
}
uint64_t get_buffsize(struct slabctl* slab) {
return (uint64_t)max(2, (int)align_up(slab->block_size, slab->alignment));
}
void* small_slab_get_buffer_addr(struct slabctl* slab, int id) {
uint64_t start_page_adr = align_up((uint64_t)get_page_adr(slab), slab->alignment);
uint64_t buffsize = get_buffsize(slab);
return (void*)(start_page_adr + buffsize*id);
}
void* big_slab_get_buffer_addr(struct slabctl* slab, int id, int cnt_page) {
uint64_t start_page_adr = align_up((uint64_t)get_page_adr(slab)-(cnt_page - 1)*PAGE_SIZE, slab->alignment);
uint64_t buffsize = get_buffsize(slab);
return (void*)(start_page_adr + buffsize*id);
}
size_t small_slab_cnt(struct slabctl* slab) {
uint64_t start_page_adr = align_up((uint64_t)get_page_adr(slab), slab->alignment);
uint64_t buffsize = get_buffsize(slab);
return ((uint64_t)slab - start_page_adr)/buffsize;
}
size_t big_slab_cnt(struct slabctl* slab, int cnt_page) {
uint64_t start_page_adr = align_up((uint64_t)get_page_adr(slab)-(cnt_page - 1)*PAGE_SIZE, slab->alignment);
uint64_t buffsize = get_buffsize(slab);
return ((uint64_t)slab - start_page_adr)/buffsize;
}
uint16_t get_big_id(struct slabctl* slab, void* addr, int cnt_page) {
uint64_t start_page_adr = align_up((uint64_t)get_page_adr(slab)-(cnt_page - 1)*PAGE_SIZE, slab->alignment);
uint64_t buffsize = get_buffsize(slab);
return (uint16_t)(((uint64_t)addr - start_page_adr)/buffsize);
}
uint16_t get_small_id (struct slabctl* slab, void* addr) {
uint64_t start_page_adr = align_up((uint64_t)get_page_adr(slab), slab->alignment);
uint64_t buffsize = get_buffsize(slab);
return (uint16_t)(((uint64_t)addr - start_page_adr)/buffsize);
}
void* allocate_slab_small(unsigned int size, unsigned int al) {
void* buf = get_page(0);
struct slabctl* slab_control = (struct slabctl*)((char*)buf + PAGE_SIZE - sizeof(struct slabctl));
slab_control->alignment = al;
slab_control->block_size = size;
slab_control->cnt_ref = small_slab_cnt(slab_control);
slab_control->head=0;
descriptors[get_phys_adr((virt_t)((char*)buf))/PAGE_SIZE].slab = slab_control;
size_t cnt = small_slab_cnt(slab_control);
for (uint16_t i = 0; i < cnt; ++i) {
uint16_t* curbuff = small_slab_get_buffer_addr(slab_control, i);
*curbuff = i + (uint16_t)1;
}
return slab_control;
}
void* allocate_slab_big(unsigned int size, unsigned int al) {
void* buf = get_page(2);
struct slabctl* slab_control = (struct slabctl*)((char*)buf + CNT_PAGES * PAGE_SIZE - sizeof(struct slabctl));
for (int i = 0; i < CNT_PAGES; ++i) {
descriptors[get_phys_adr((virt_t)((char*)buf + i*PAGE_SIZE))/PAGE_SIZE].slab = slab_control;
}
slab_control->alignment = al;
slab_control->block_size = size;
slab_control->cnt_ref = big_slab_cnt(slab_control, CNT_PAGES);
slab_control->head=0;
size_t cnt = big_slab_cnt(slab_control, CNT_PAGES);
for (uint16_t i = 0; i < cnt; ++i) {
uint16_t * curbuff = big_slab_get_buffer_addr(slab_control, i, CNT_PAGES);
*curbuff = i + (uint16_t)1;
}
return slab_control;
}
void* allocate_slab(unsigned int size, unsigned int al) {
if (al == 0) {
al = 1;
}
void* ret;
if (size*8 <= PAGE_SIZE) {
ret = allocate_slab_small(size, al);
} else {
ret = allocate_slab_big(size, al);
}
return ret;
}
int is_big_slab(struct slabctl* slab) {
return slab->block_size * 8 > PAGE_SIZE;
}
void* allocate_block(struct slabctl* slab) {
if (is_big_slab(slab)) {
void* res = big_slab_get_buffer_addr(slab, slab->head, CNT_PAGES);
slab->head = *((uint16_t*)res);
slab->cnt_ref--;
return res;
}
void* res = small_slab_get_buffer_addr(slab, slab->head);
assert(*((uint16_t*)res) < small_slab_cnt(slab));
slab->head = *((uint16_t*)res);
slab->cnt_ref--;
assert((uint64_t)res < (uint64_t)slab);
return res;
}
void free_block(void *addr) {
void* pg_addr = get_page_adr(addr);
struct slabctl* sl = descriptors[get_phys_adr((virt_t)pg_addr)/PAGE_SIZE].slab;
start_critical_section();
uint16_t id;
if (is_big_slab(sl)) {
id = get_big_id(sl, addr, CNT_PAGES);
} else {
id = get_small_id(sl, addr);
}
*((uint16_t*)addr) = sl->head;
sl->head = id;
assert(addr == small_slab_get_buffer_addr(sl, id));
sl->cnt_ref++;
if (sl->cnt_ref == 1) {
sl->next = *((struct slabctl**)sl->slab_list_head);
*((struct slabctl**)sl->slab_list_head) = sl;
}
end_critical_section();
}
void* allocate_block_in_slab_system (struct slabctl** slab_sys) {
struct slabctl** head = slab_sys;
lock(&(*head)->lock);
void* ret = allocate_block(*head);
if ((*head)->cnt_ref == 0) {
if ((*head)->next == (*head)) {
*head = allocate_slab((*head)->block_size, (*head)->alignment);
(*head)->slab_list_head = head;
(*head)->next = *head;
} else {
(*head) = (*head)->next;
}
}
unlock(&(*head)->lock);
return ret;
}
struct slabctl** create_slab_system (unsigned int size, unsigned int al) {
lock(&slab_lock);
if (heads == NULL) {
heads = get_page(0);
}
struct slabctl** head = heads;
heads = (void*)((uint64_t) heads + sizeof(struct slabctl**));
*head = (struct slabctl*)allocate_slab(size, al);
(*head)->slab_list_head = head;
(*head)->next = *head;
unlock(&slab_lock);
return head;
}
struct slabctl** small_slabs[MALLOC_SMALL_MAX_SIZE];
void init_malloc_small() {
for (unsigned i = 1; i < MALLOC_SMALL_MAX_SIZE; ++i) {
small_slabs[i] = create_slab_system(i, 1);
}
}
void* malloc_small (unsigned int size) {
assert(size > 0);
assert(size < MALLOC_SMALL_MAX_SIZE);
return allocate_block_in_slab_system(small_slabs[size]);
}