Compare commits

..

2 Commits

Author SHA1 Message Date
Mathieu Maret 3cda78532b test: add more malloc/free test 2019-04-17 23:47:25 +02:00
Mathieu Maret 340ae6a4d0 alloc: fix some naming. alloc up to page_size 2019-04-17 23:46:56 +02:00
2 changed files with 46 additions and 19 deletions

View File

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

View File

@ -38,12 +38,20 @@ 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));
@ -56,14 +64,32 @@ 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);
assert(malloc(1024)); 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(2048)); void *alloc7 = testAllocNSet(4096);
//assert(malloc(4096)); free(alloc1);
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)