...
 
Commits (3)
......@@ -7,7 +7,6 @@
#include "mem.h"
#include "vga.h"
#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)
......@@ -25,7 +24,7 @@ int allocInit(void)
pr_devel("Fail to allocBookSlab %d for slabDesc :( \n");
return ret;
}
for (uint i = start; i < SLUB_SIZE; i++) {
for (uint i = start; i <= SLUB_SIZE; i++) {
if ((ret = allocBookSlab(1U << i, 0))) {
pr_devel("Fail to allocBookSlab %d for %d \n", ret, (1U << i));
return ret;
......@@ -36,7 +35,7 @@ int allocInit(void)
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 *slab;
int slabIdx;
......@@ -73,13 +72,13 @@ int addSlab(struct slabDesc **desc, size_t size, int selfContained)
*desc = (struct slabDesc *)alloc;
(*desc)->freeEl = (char *)(*desc) + sizeof(struct slabDesc);
} else {
*desc = malloc(sizeof(struct slabDesc));
*desc = malloc(sizeof(struct slabDesc));
(*desc)->freeEl = (void *)alloc;
}
list_singleton(*desc, *desc);
(*desc)->page = (vaddr_t)alloc;
(*desc)->full = 0;
(*desc)->size = size;
(*desc)->page = (vaddr_t)alloc;
(*desc)->full = 0;
(*desc)->size = size;
pr_devel("got page %d for size %d first %d", alloc, size, (*desc)->freeEl);
return formatPage((*desc), size, selfContained);
}
......@@ -104,7 +103,7 @@ static void *allocFromSlab(struct slabDesc *slab)
{
vaddr_t *next = slab->freeEl;
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;
} else {
slab->freeEl = (void *)(*next);
......@@ -119,7 +118,7 @@ void *malloc(size_t size)
return NULL;
}
struct slabDesc *slubEntry;
uint slubIdx = 0;
uint slubIdx;
list_foreach(slub, slubEntry, slubIdx)
{
if (size <= slubEntry->size)
......@@ -129,8 +128,13 @@ void *malloc(size_t size)
int 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) {
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);
}
}
......@@ -138,38 +142,42 @@ void *malloc(size_t size)
// No room found
struct slabDesc *newSlab;
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);
return NULL;
}
pr_devel("Allocate new slab for object of size %d\n", slubEntry->size);
list_add_head(slubEntry, newSlab);
return allocFromSlab(newSlab);
}
int slabFree(void *ptr, struct slabDesc *slab){
int slabFree(void *ptr, struct slabDesc *slab)
{
struct slabDesc *slabEntry;
int slabIdx;
list_foreach(slab, slabEntry, slabIdx)
{
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);
*((vaddr_t *)ptr) = (vaddr_t)slabEntry->freeEl;
slabEntry->freeEl = ptr;
slabEntry->full = 0;
slabEntry->full = 0;
return 1;
}
}
return 0;
}
void free(void *ptr){
if(!ptr)
void free(void *ptr)
{
if (!ptr)
return;
struct slabDesc *slab;
int slabIdx;
list_foreach(slub, slab, slabIdx){
if(slabFree(ptr, slab))
list_foreach(slub, slab, slabIdx)
{
if (slabFree(ptr, slab))
return;
}
pr_devel("free: slab not found\n");
......
......@@ -33,7 +33,8 @@ void testPhymem(void)
}
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);
}
......@@ -68,15 +69,16 @@ static void testPaging(void)
while ((page = (struct mem_desc *)allocPhyPage()) != NULL) {
assertmsg(pageMap((vaddr_t)page, (paddr_t)page, PAGING_MEM_WRITE) == 0,
"Fail to map page %d\n", allocCount);
page->phy_addr = allocCount;
"Fail to map page %d\n", allocCount);
memset(page, allocCount, PAGE_SIZE);
allocCount++;
list_add_tail(allocated_page_list, page);
}
printf("%d pages allocated\n", allocCount);
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",
(ulong)page);
pageUnmap((vaddr_t)page);
......@@ -84,7 +86,8 @@ static void testPaging(void)
}
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);
}
......