alloc: slub implemented with list

This commit is contained in:
Mathieu Maret 2019-04-14 15:56:48 +02:00
parent e03f0c0d78
commit 6c91272fc2
1 changed files with 13 additions and 7 deletions

View File

@ -9,7 +9,7 @@
// Slab will contains object from sizeof(void *) to PAGE_SIZE/2 by pow2 // Slab will contains object from sizeof(void *) to PAGE_SIZE/2 by pow2
#define SLUB_SIZE (PAGE_SHIFT - 1) #define SLUB_SIZE (PAGE_SHIFT - 1)
struct slabDesc *slubArray[SLUB_SIZE]; struct slabDesc *slub;
int initSlabDesc(struct slabDesc **desc, size_t size); int initSlabDesc(struct slabDesc **desc, size_t size);
static int formatPage(struct slabDesc *desc, size_t size); static int formatPage(struct slabDesc *desc, size_t size);
@ -17,9 +17,12 @@ static int formatPage(struct slabDesc *desc, size_t size);
int initSlab(void) int initSlab(void)
{ {
uint start = log2(sizeof(void *)); uint start = log2(sizeof(void *));
list_init(slub);
for (uint i = start; i < SLUB_SIZE; i++) { for (uint i = start; i < SLUB_SIZE; i++) {
if (initSlabDesc(&slubArray[i - start], 1U << i)) struct slabDesc *slab;
if (initSlabDesc(&slab, 1U << i))
return ENOMEM; return ENOMEM;
list_add_tail(slub, slab);
} }
return 0; return 0;
} }
@ -75,23 +78,26 @@ void *malloc(size_t size)
printf("implement malloc for big size\n"); printf("implement malloc for big size\n");
return NULL; return NULL;
} }
struct slabDesc *slubEntry;
uint slubIdx = 0; uint slubIdx = 0;
while (size > slubArray[slubIdx]->size) { list_foreach(slub, slubEntry, slubIdx)
slubIdx++; {
if(size <= slubEntry->size)
break;
} }
struct slabDesc *slab; struct slabDesc *slab;
int slabIdx; int slabIdx;
list_foreach(slubArray[slubIdx], slab, slabIdx) list_foreach(slubEntry, slab, slabIdx)
{ {
if (!slab->full) { if (!slab->full) {
pr_devel("found place in slub %d at idx %d \n", slubIdx, slabIdx); pr_devel("found place in slub %d at idx %d \n", slubIdx, slabIdx);
return allocFromSlab(slab); return allocFromSlab(slab);
} }
} }
if (!list_foreach_early_break(slubArray[slubIdx], slab, slabIdx)) { if (!list_foreach_early_break(slubEntry, slab, slabIdx)) {
struct slabDesc *newSlab; struct slabDesc *newSlab;
initSlabDesc(&newSlab, slab->size); initSlabDesc(&newSlab, slab->size);
list_add_head(slubArray[slubIdx], newSlab); list_add_head(slubEntry, newSlab);
return allocFromSlab(newSlab); return allocFromSlab(newSlab);
} }
return NULL; return NULL;