Compare commits

..

No commits in common. "3cda78532bdb8164bde86277494f7048af401e71" and "06c8562a8008e5357f68110b3adbe3b106bc18f5" have entirely different histories.

2 changed files with 19 additions and 46 deletions

View File

@ -9,11 +9,10 @@
#define IS_SELF_CONTAINED(desc) ((vaddr_t)((desc)->page) == (vaddr_t)(desc)) #define IS_SELF_CONTAINED(desc) ((vaddr_t)((desc)->page) == (vaddr_t)(desc))
// 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) #define SLUB_SIZE (PAGE_SHIFT - 1)
struct slabDesc *slub; struct slabDesc *slub;
int allocSlab(struct slabDesc **desc, size_t size, int self_containing); int addSlab(struct slabDesc **desc, size_t size, int self_containing);
int allocSlabEntry(struct slabEntry **desc, size_t size, int selfContained);
static int formatPage(struct slabEntry *desc, size_t size, int selfContained); static int formatPage(struct slabEntry *desc, size_t size, int selfContained);
int allocInit(void) int allocInit(void)
@ -56,7 +55,7 @@ int allocBookSlab(size_t size, int selfContained)
} }
} }
struct slabDesc *newSlab; struct slabDesc *newSlab;
if ((ret = allocSlab(&newSlab, size, selfContained))) if ((ret = addSlab(&newSlab, size, selfContained)))
return ret; return ret;
if (list_foreach_early_break(slub, slab, slabIdx)) { if (list_foreach_early_break(slub, slab, slabIdx)) {
list_insert_before(slub, slab, newSlab); list_insert_before(slub, slab, newSlab);
@ -66,7 +65,7 @@ int allocBookSlab(size_t size, int selfContained)
return 0; return 0;
} }
int allocSlab(struct slabDesc **desc, size_t size, int selfContained) int addSlab(struct slabDesc **desc, size_t size, int selfContained)
{ {
// pr_devel("%s for size %d is self %d\n", __func__, size, selfContained); // pr_devel("%s for size %d is self %d\n", __func__, size, selfContained);
if (size > PAGE_SIZE) if (size > PAGE_SIZE)
@ -93,7 +92,7 @@ int allocSlab(struct slabDesc **desc, size_t size, int selfContained)
return formatPage(&(*desc)->slab, size, selfContained); return formatPage(&(*desc)->slab, size, selfContained);
} }
int allocSlabEntry(struct slabEntry **desc, size_t size, int selfContained) int addSlabEntry(struct slabEntry **desc, size_t size, int selfContained)
{ {
pr_devel("%s for size %d is self %d\n", __func__, size, selfContained); pr_devel("%s for size %d is self %d\n", __func__, size, selfContained);
if (size > PAGE_SIZE) if (size > PAGE_SIZE)
@ -174,8 +173,8 @@ void *malloc(size_t size)
struct slabEntry *newSlabEntry; struct slabEntry *newSlabEntry;
struct slabEntry *slabList = &slab->slab; struct slabEntry *slabList = &slab->slab;
int ret; int ret;
if ((ret = allocSlabEntry(&newSlabEntry, slab->size, IS_SELF_CONTAINED(&slab->slab)))) { if ((ret = addSlabEntry(&newSlabEntry, slab->size, IS_SELF_CONTAINED(&slab->slab)))) {
pr_devel("Fail to allocSlabEntry %d\n", ret); pr_devel("Fail to addSlabEntry %d\n", ret);
return NULL; return NULL;
} }
pr_devel("Allocate new slab for object of size %d\n", slab->size); pr_devel("Allocate new slab for object of size %d\n", slab->size);
@ -183,7 +182,7 @@ void *malloc(size_t size)
return allocFromSlab(newSlabEntry); return allocFromSlab(newSlabEntry);
} }
int freeFromSlab(void *ptr, struct slabEntry *slab) int slabFree(void *ptr, struct slabEntry *slab)
{ {
struct slabEntry *slabEntry; struct slabEntry *slabEntry;
int slabIdx; int slabIdx;
@ -213,7 +212,7 @@ void free(void *ptr)
int entryIdx; int entryIdx;
list_foreach(&slab->slab, slabEntry, entryIdx) list_foreach(&slab->slab, slabEntry, entryIdx)
{ {
if (freeFromSlab(ptr, slabEntry)) if (slabFree(ptr, slabEntry))
return; return;
} }
} }

View File

@ -38,20 +38,12 @@ void testPhymem(void)
unrefPhyPage((ulong)page); unrefPhyPage((ulong)page);
} }
static void *testAllocNSet(size_t size)
{
void *allocated = malloc(size);
assert(allocated);
memset(allocated, size, size);
return allocated;
}
static void testAlloc(void) static void testAlloc(void)
{ {
for (uint i = 0; i < PAGE_SIZE / (sizeof(struct slabEntry)); i++) { for( uint i = 0; i < PAGE_SIZE/(sizeof(struct slabEntry)); i++){
malloc(sizeof(struct slabEntry)); malloc(sizeof(struct slabEntry));
} }
for (uint i = 0; i < PAGE_SIZE / (sizeof(struct slabDesc)); i++) { for( uint i = 0; i < PAGE_SIZE/(sizeof(struct slabDesc)); i++){
malloc(sizeof(struct slabDesc)); malloc(sizeof(struct slabDesc));
} }
assert(malloc(1)); assert(malloc(1));
@ -64,32 +56,14 @@ static void testAlloc(void)
free(malloc2); free(malloc2);
void *malloc3 = malloc(sizeof(void *)); void *malloc3 = malloc(sizeof(void *));
assertmsg((char *)malloc2 == (char *)malloc3, " %d %d\n", malloc2, malloc3); assertmsg((char *)malloc2 == (char *)malloc3, " %d %d\n", malloc2, malloc3);
void *alloc1 = testAllocNSet(1024); assert(malloc(1024));
void *alloc2 = testAllocNSet(1024); assert(malloc(1024));
void *alloc3 = testAllocNSet(1024); assert(malloc(1024));
void *alloc4 = testAllocNSet(1024); assert(malloc(1024));
void *alloc5 = testAllocNSet(1024); assert(malloc(1024));
void *alloc6 = testAllocNSet(1024); assert(malloc(1024));
void *alloc7 = testAllocNSet(4096); assert(malloc(2048));
free(alloc1); //assert(malloc(4096));
free(alloc2);
free(alloc3);
free(alloc4);
free(alloc5);
free(alloc6);
free(alloc7);
void *alloc11 = testAllocNSet(1024);
void *alloc12 = testAllocNSet(1024);
void *alloc13 = testAllocNSet(1024);
void *alloc14 = testAllocNSet(1024);
void *alloc15 = testAllocNSet(1024);
void *alloc16 = testAllocNSet(1024);
free(alloc11);
free(alloc12);
free(alloc13);
free(alloc14);
free(alloc15);
free(alloc16);
} }
static void testPaging(void) static void testPaging(void)