Compare commits
2 Commits
06c8562a80
...
3cda78532b
Author | SHA1 | Date |
---|---|---|
Mathieu Maret | 3cda78532b | |
Mathieu Maret | 340ae6a4d0 |
19
core/alloc.c
19
core/alloc.c
|
@ -9,10 +9,11 @@
|
|||
|
||||
#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
|
||||
#define SLUB_SIZE (PAGE_SHIFT - 1)
|
||||
#define SLUB_SIZE (PAGE_SHIFT)
|
||||
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);
|
||||
|
||||
int allocInit(void)
|
||||
|
@ -55,7 +56,7 @@ int allocBookSlab(size_t size, int selfContained)
|
|||
}
|
||||
}
|
||||
struct slabDesc *newSlab;
|
||||
if ((ret = addSlab(&newSlab, size, selfContained)))
|
||||
if ((ret = allocSlab(&newSlab, size, selfContained)))
|
||||
return ret;
|
||||
if (list_foreach_early_break(slub, slab, slabIdx)) {
|
||||
list_insert_before(slub, slab, newSlab);
|
||||
|
@ -65,7 +66,7 @@ int allocBookSlab(size_t size, int selfContained)
|
|||
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);
|
||||
if (size > PAGE_SIZE)
|
||||
|
@ -92,7 +93,7 @@ int addSlab(struct slabDesc **desc, size_t size, int 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);
|
||||
if (size > PAGE_SIZE)
|
||||
|
@ -173,8 +174,8 @@ void *malloc(size_t size)
|
|||
struct slabEntry *newSlabEntry;
|
||||
struct slabEntry *slabList = &slab->slab;
|
||||
int ret;
|
||||
if ((ret = addSlabEntry(&newSlabEntry, slab->size, IS_SELF_CONTAINED(&slab->slab)))) {
|
||||
pr_devel("Fail to addSlabEntry %d\n", ret);
|
||||
if ((ret = allocSlabEntry(&newSlabEntry, slab->size, IS_SELF_CONTAINED(&slab->slab)))) {
|
||||
pr_devel("Fail to allocSlabEntry %d\n", ret);
|
||||
return NULL;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
int slabFree(void *ptr, struct slabEntry *slab)
|
||||
int freeFromSlab(void *ptr, struct slabEntry *slab)
|
||||
{
|
||||
struct slabEntry *slabEntry;
|
||||
int slabIdx;
|
||||
|
@ -212,7 +213,7 @@ void free(void *ptr)
|
|||
int entryIdx;
|
||||
list_foreach(&slab->slab, slabEntry, entryIdx)
|
||||
{
|
||||
if (slabFree(ptr, slabEntry))
|
||||
if (freeFromSlab(ptr, slabEntry))
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
46
tests/test.c
46
tests/test.c
|
@ -38,12 +38,20 @@ void testPhymem(void)
|
|||
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)
|
||||
{
|
||||
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));
|
||||
}
|
||||
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));
|
||||
}
|
||||
assert(malloc(1));
|
||||
|
@ -56,14 +64,32 @@ static void testAlloc(void)
|
|||
free(malloc2);
|
||||
void *malloc3 = malloc(sizeof(void *));
|
||||
assertmsg((char *)malloc2 == (char *)malloc3, " %d %d\n", malloc2, malloc3);
|
||||
assert(malloc(1024));
|
||||
assert(malloc(1024));
|
||||
assert(malloc(1024));
|
||||
assert(malloc(1024));
|
||||
assert(malloc(1024));
|
||||
assert(malloc(1024));
|
||||
assert(malloc(2048));
|
||||
//assert(malloc(4096));
|
||||
void *alloc1 = testAllocNSet(1024);
|
||||
void *alloc2 = testAllocNSet(1024);
|
||||
void *alloc3 = testAllocNSet(1024);
|
||||
void *alloc4 = testAllocNSet(1024);
|
||||
void *alloc5 = testAllocNSet(1024);
|
||||
void *alloc6 = testAllocNSet(1024);
|
||||
void *alloc7 = testAllocNSet(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)
|
||||
|
|
Loading…
Reference in New Issue