-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathslab.c
327 lines (267 loc) · 8.27 KB
/
slab.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "slab.h"
/* Creates a cache of objects.
@name used for reference
@size size of the objects
@align align boundary
@constructor object constructor
@destructor object destructor
Returns a cache pointer or NULL if no memory is available.
*/
kmem_cache_t
kmem_cache_create(char *name, size_t size, int align,
void (*constructor)(void *, size_t),
void (*destructor)(void *, size_t)) {
kmem_cache_t cp = malloc(sizeof(struct kmem_cache));
if (cp != NULL) {
if (align == 0) align = SLAB_DEFAULT_ALIGN;
cp->name = name;
cp->size = size;
cp->effsize = align * ((size-1)/align + 1);
cp->constructor = constructor;
cp->destructor = destructor;
cp->slabs = NULL;
cp->slabs_back = NULL;
// if this is for small object
if (cp->size <= SLAB_SMALL_OBJ_SZ) {
cp->slab_maxbuf = (PAGE_SZ - sizeof(struct kmem_slab)) / cp->effsize;
}
else {
// TODO: compute number of objects programmatically
cp->slab_maxbuf = 8;
// create hash table...
// hcreate(cp->slab_maxbuf * 100);
}
}
return cp;
}
/* Grow a specified cache. Specifically adds one slab to it.
@cp cache pointer
*/
void
kmem_cache_grow(kmem_cache_t cp) {
void *mem;
kmem_slab_t slab;
void *p, *lastbuf;
int i;
kmem_bufctl_t bufctl;
// if this is a small object
if (cp->size <= SLAB_SMALL_OBJ_SZ) {
// allocating one page
if (0 != posix_memalign(&mem, PAGE_SZ, PAGE_SZ))
return;
// positioning slab at the end of the page
slab = mem + PAGE_SZ - sizeof(struct kmem_slab);
slab->next = slab->prev = slab;
slab->bufcount = 0;
slab->free_list = mem;
// creating linkage
lastbuf = mem + (cp->effsize * (cp->slab_maxbuf-1));
for (p=mem; p < lastbuf; p+=cp->effsize)
*((void **)p) = p + cp->effsize;
// complete slab at the front...
__slab_move_to_front(cp, slab);
assert(cp->slabs == slab);
// printf("\n%p\n%p\n%#x\n%#x\n", mem, slab, sizeof(struct kmem_slab), sizeof(struct kmem_cache));
}
// if this is a large object
else {
// allocating pages
if (0 != posix_memalign(&mem, PAGE_SZ, (cp->slab_maxbuf * cp->effsize)/PAGE_SZ))
return;
// allocating slab
slab = (kmem_slab_t)malloc(sizeof(struct kmem_slab));
// initializing slab
slab->next = slab->prev = slab;
slab->bufcount = 0;
bufctl = (kmem_bufctl_t)malloc(sizeof(struct kmem_bufctl) * cp->slab_maxbuf);
bufctl[0].next = NULL;
bufctl[0].buf = mem;
bufctl[0].slab = slab;
slab->start = &bufctl[0];
slab->free_list = &bufctl[0];
// creating addtl bufctls
for (i=1; i < cp->slab_maxbuf; i++) {
bufctl[i].next = slab->free_list;
bufctl[i].buf = mem + (i*cp->effsize + (PAGE_SZ%cp->effsize * (((i+1)*cp->effsize)/PAGE_SZ)));
bufctl[i].slab = slab;
slab->free_list = &bufctl[i];
}
// complete slab at the front...
__slab_move_to_front(cp, slab);
// printf("\n%p\n%p\n%#x\n%#x\n", mem, slab, sizeof(struct kmem_slab), sizeof(struct kmem_cache));
}
}
/* Requests an allocated object from the cache.
@cp cache pointer
@flags flags KM_SLEEP or KM_NOSLEEP
*/
void *
kmem_cache_alloc(kmem_cache_t cp, int flags) {
void *buf;
// grow the cache if necessary...
if (cp->slabs == NULL)
kmem_cache_grow(cp);
if (cp->slabs->bufcount == cp->slab_maxbuf)
kmem_cache_grow(cp);
// if this is a small object
if (cp->size <= SLAB_SMALL_OBJ_SZ) {
buf = cp->slabs->free_list;
cp->slabs->free_list = *((void**)buf);
cp->slabs->bufcount++;
}
else {
kmem_bufctl_t bufctl = cp->slabs->free_list;
cp->slabs->free_list = bufctl->next;
buf = bufctl->buf;
cp->slabs->bufcount++;
}
// if slab is empty
if (cp->slabs->bufcount == cp->slab_maxbuf)
__slab_move_to_back(cp, cp->slabs);
return buf;
}
/* Frees an allocated object from the cache.
@cp cache pointer
@buf object pointer
*/
void
kmem_cache_free(kmem_cache_t cp, void *buf) {
void * mem;
kmem_slab_t slab;
// kmem_bufctl_t bufctl;
// if this is a small object
if (cp->size <= SLAB_SMALL_OBJ_SZ) {
// compute slab position
// TODO: DO IT GENERIC (PAGE_SZ != 0x1000)
mem = (void*)((long)buf >> 12 << 12);
slab = mem + PAGE_SZ - sizeof(struct kmem_slab);
// put buffer back in the slab free list
*((void **)buf) = slab->free_list;
slab->free_list = buf;
slab->bufcount--;
// if slab is now complete, discard whole page
if (slab->bufcount == 0) {
__slab_remove(cp, slab);
free(mem);
}
// if slab WAS empty, re-add to non-empty slabs
if (slab->bufcount == cp->slab_maxbuf-1)
__slab_move_to_front(cp, slab);
}
// if this is a large object
else {
// use hash table to get to bufctl
// ...
// bufctl = (kmem_cache_t)0x4000;
// slab = bufctl->slab;
// put bufctl back in the slab free list
// bufctl->next = slab->free_list;
// slab->free_list = bufctl;
// if slab is now complete, discard whole page
// if (slab->bufcount == 0) {
// __slab_remove(cp, slab);
// free(slab->start->buf); // free objects
// free(slab->start); // free bufctls
// free(slab); // free slab
// }
}
}
/* Destroys a specified cache.
@cp cache pointer
*/
void
kmem_cache_destroy(kmem_cache_t cp) {
kmem_slab_t slab;
void * mem;
if (cp->size <= SLAB_SMALL_OBJ_SZ) {
// freeing all allocated memory
while (cp->slabs) {
slab = cp->slabs;
__slab_remove(cp, slab);
mem = (void*)slab - PAGE_SZ + sizeof(struct kmem_slab);
free(mem);
}
}
else {
while (cp->slabs) {
slab = cp->slabs;
__slab_remove(cp, slab);
free(slab->start->buf); // free objects
free(slab->start); // free bufctls
free(slab); // free slab
}
}
free(cp);
}
/* Internal auxiliary to remove slab of freelist
@cp cache pointer
@slab slab pointer
*/
inline void
__slab_remove(kmem_cache_t cp, kmem_slab_t slab) {
slab->next->prev = slab->prev;
slab->prev->next = slab->next;
// if front slab...
if (cp->slabs == slab) {
// if last slab
if (slab->prev == slab)
cp->slabs = NULL;
else
cp->slabs = slab->prev;
}
// if back slab
if (cp->slabs_back == slab) {
// if last slab
if (slab->next == slab)
cp->slabs_back = NULL;
else
cp->slabs_back = slab->next;
}
}
/* Internal auxiliary to move slab to the front of freelist
@cp cache pointer
@slab slab pointer
*/
inline void
__slab_move_to_front(kmem_cache_t cp, kmem_slab_t slab) {
if (cp->slabs == slab) return;
__slab_remove(cp, slab);
// check if there is any slab in the cache
if (cp->slabs == NULL) {
slab->prev = slab;
slab->next = slab;
cp->slabs_back = slab;
}
else {
slab->prev = cp->slabs;
cp->slabs->next = slab;
slab->next = cp->slabs_back;
cp->slabs_back->prev = slab;
}
cp->slabs = slab;
}
/* Internal auxiliary to move slab to the front of freelist
@cp cache pointer
@slab slab pointer
*/
inline void
__slab_move_to_back(kmem_cache_t cp, kmem_slab_t slab) {
if (cp->slabs_back == slab) return;
__slab_remove(cp, slab);
// check if there is any slab in the cache
if (cp->slabs == NULL) {
slab->prev = slab;
slab->next = slab;
cp->slabs = slab;
}
else {
slab->prev = cp->slabs;
cp->slabs->next = slab;
slab->next = cp->slabs_back;
cp->slabs_back->prev = slab;
}
cp->slabs_back = slab;
}