Compare commits

...

3 Commits

Author SHA1 Message Date
Mathieu Maret f2b0edc5a3 alloc: fix new slab allocation 2019-04-17 12:48:24 +02:00
Mathieu Maret f90b9bd3fd alloc: fix indent 2019-04-17 12:06:54 +02:00
Mathieu Maret f3a03f3965 test: memset the whole page 2019-04-17 12:06:08 +02:00
2 changed files with 34 additions and 23 deletions

View File

@ -7,7 +7,6 @@
#include "mem.h" #include "mem.h"
#include "vga.h" #include "vga.h"
#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 - 1)
@ -25,7 +24,7 @@ int allocInit(void)
pr_devel("Fail to allocBookSlab %d for slabDesc :( \n"); pr_devel("Fail to allocBookSlab %d for slabDesc :( \n");
return ret; return ret;
} }
for (uint i = start; i < SLUB_SIZE; i++) { for (uint i = start; i <= SLUB_SIZE; i++) {
if ((ret = allocBookSlab(1U << i, 0))) { if ((ret = allocBookSlab(1U << i, 0))) {
pr_devel("Fail to allocBookSlab %d for %d \n", ret, (1U << i)); pr_devel("Fail to allocBookSlab %d for %d \n", ret, (1U << i));
return ret; return ret;
@ -36,7 +35,7 @@ int allocInit(void)
int allocBookSlab(size_t size, int selfContained) int allocBookSlab(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 );
struct slabDesc *slabEntry; struct slabDesc *slabEntry;
struct slabDesc *slab; struct slabDesc *slab;
int slabIdx; int slabIdx;
@ -73,13 +72,13 @@ int addSlab(struct slabDesc **desc, size_t size, int selfContained)
*desc = (struct slabDesc *)alloc; *desc = (struct slabDesc *)alloc;
(*desc)->freeEl = (char *)(*desc) + sizeof(struct slabDesc); (*desc)->freeEl = (char *)(*desc) + sizeof(struct slabDesc);
} else { } else {
*desc = malloc(sizeof(struct slabDesc)); *desc = malloc(sizeof(struct slabDesc));
(*desc)->freeEl = (void *)alloc; (*desc)->freeEl = (void *)alloc;
} }
list_singleton(*desc, *desc); list_singleton(*desc, *desc);
(*desc)->page = (vaddr_t)alloc; (*desc)->page = (vaddr_t)alloc;
(*desc)->full = 0; (*desc)->full = 0;
(*desc)->size = size; (*desc)->size = size;
pr_devel("got page %d for size %d first %d", alloc, size, (*desc)->freeEl); pr_devel("got page %d for size %d first %d", alloc, size, (*desc)->freeEl);
return formatPage((*desc), size, selfContained); return formatPage((*desc), size, selfContained);
} }
@ -104,7 +103,7 @@ static void *allocFromSlab(struct slabDesc *slab)
{ {
vaddr_t *next = slab->freeEl; vaddr_t *next = slab->freeEl;
if (*next == (vaddr_t)NULL) { if (*next == (vaddr_t)NULL) {
pr_devel("Slab @%d for %d full\n", slab, slab->size); pr_devel("Slab @%d for %d is now full\n", slab, slab->size);
slab->full = 1; slab->full = 1;
} else { } else {
slab->freeEl = (void *)(*next); slab->freeEl = (void *)(*next);
@ -119,7 +118,7 @@ void *malloc(size_t size)
return NULL; return NULL;
} }
struct slabDesc *slubEntry; struct slabDesc *slubEntry;
uint slubIdx = 0; uint slubIdx;
list_foreach(slub, slubEntry, slubIdx) list_foreach(slub, slubEntry, slubIdx)
{ {
if (size <= slubEntry->size) if (size <= slubEntry->size)
@ -129,8 +128,13 @@ void *malloc(size_t size)
int slabIdx; int slabIdx;
list_foreach(slubEntry, slab, slabIdx) list_foreach(slubEntry, slab, slabIdx)
{ {
if (slab->size > slubEntry->size){
pr_devel("No more room in slub %d\n", slubIdx);
break;
}
if (!slab->full) { if (!slab->full) {
pr_devel("found place in slub %d at idx %d for size %d\n", slubIdx, slabIdx, size); pr_devel("found place in slub %d at idx %d for size %d\n", slubIdx,
slabIdx, size);
return allocFromSlab(slab); return allocFromSlab(slab);
} }
} }
@ -138,38 +142,42 @@ void *malloc(size_t size)
// No room found // No room found
struct slabDesc *newSlab; struct slabDesc *newSlab;
int ret; int ret;
if ((ret = addSlab(&newSlab, slab->size, IS_SELF_CONTAINED(slubEntry)))) { if ((ret = addSlab(&newSlab, slubEntry->size, IS_SELF_CONTAINED(slubEntry)))) {
pr_devel("Fail to addSlab %d\n", ret); pr_devel("Fail to addSlab %d\n", ret);
return NULL; return NULL;
} }
pr_devel("Allocate new slab for object of size %d\n", slubEntry->size);
list_add_head(slubEntry, newSlab); list_add_head(slubEntry, newSlab);
return allocFromSlab(newSlab); return allocFromSlab(newSlab);
} }
int slabFree(void *ptr, struct slabDesc *slab){ int slabFree(void *ptr, struct slabDesc *slab)
{
struct slabDesc *slabEntry; struct slabDesc *slabEntry;
int slabIdx; int slabIdx;
list_foreach(slab, slabEntry, slabIdx) list_foreach(slab, slabEntry, slabIdx)
{ {
if ((slabEntry->page <= (vaddr_t)ptr) && if ((slabEntry->page <= (vaddr_t)ptr) &&
((vaddr_t)ptr < (slabEntry->page + PAGE_SIZE))){ ((vaddr_t)ptr < (slabEntry->page + PAGE_SIZE))) {
pr_devel("free place! was %d is now %d\n", slabEntry->freeEl, ptr); pr_devel("free place! was %d is now %d\n", slabEntry->freeEl, ptr);
*((vaddr_t *)ptr) = (vaddr_t)slabEntry->freeEl; *((vaddr_t *)ptr) = (vaddr_t)slabEntry->freeEl;
slabEntry->freeEl = ptr; slabEntry->freeEl = ptr;
slabEntry->full = 0; slabEntry->full = 0;
return 1; return 1;
} }
} }
return 0; return 0;
} }
void free(void *ptr){ void free(void *ptr)
if(!ptr) {
if (!ptr)
return; return;
struct slabDesc *slab; struct slabDesc *slab;
int slabIdx; int slabIdx;
list_foreach(slub, slab, slabIdx){ list_foreach(slub, slab, slabIdx)
if(slabFree(ptr, slab)) {
if (slabFree(ptr, slab))
return; return;
} }
pr_devel("free: slab not found\n"); pr_devel("free: slab not found\n");

View File

@ -33,7 +33,8 @@ void testPhymem(void)
} }
printf("%d pages freed\n", freeCount); printf("%d pages freed\n", freeCount);
assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL, "Cannot allocate memory\n"); assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL,
"Cannot allocate memory\n");
unrefPhyPage((ulong)page); unrefPhyPage((ulong)page);
} }
@ -68,15 +69,16 @@ static void testPaging(void)
while ((page = (struct mem_desc *)allocPhyPage()) != NULL) { while ((page = (struct mem_desc *)allocPhyPage()) != NULL) {
assertmsg(pageMap((vaddr_t)page, (paddr_t)page, PAGING_MEM_WRITE) == 0, assertmsg(pageMap((vaddr_t)page, (paddr_t)page, PAGING_MEM_WRITE) == 0,
"Fail to map page %d\n", allocCount); "Fail to map page %d\n", allocCount);
page->phy_addr = allocCount; memset(page, allocCount, PAGE_SIZE);
allocCount++; allocCount++;
list_add_tail(allocated_page_list, page); list_add_tail(allocated_page_list, page);
} }
printf("%d pages allocated\n", allocCount); printf("%d pages allocated\n", allocCount);
while ((page = list_pop_head(allocated_page_list)) != NULL) { while ((page = list_pop_head(allocated_page_list)) != NULL) {
assertmsg(page->phy_addr == (ulong)freeCount, "page modified"); assertmsg((char)page->phy_addr == (char)freeCount,
"page modified %d but is %d\n", freeCount, page->phy_addr);
assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n", assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n",
(ulong)page); (ulong)page);
pageUnmap((vaddr_t)page); pageUnmap((vaddr_t)page);
@ -84,7 +86,8 @@ static void testPaging(void)
} }
printf("%d pages freed\n", freeCount); printf("%d pages freed\n", freeCount);
assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL, "Cannot allocate memory\n"); assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL,
"Cannot allocate memory\n");
unrefPhyPage((ulong)page); unrefPhyPage((ulong)page);
} }