Alloc: could be initialized in random way

fix ENO usage
This commit is contained in:
Mathieu Maret 2019-04-15 21:35:38 +02:00
parent 6c91272fc2
commit f1b3895473
4 changed files with 47 additions and 18 deletions

View File

@ -11,32 +11,56 @@
#define SLUB_SIZE (PAGE_SHIFT - 1) #define SLUB_SIZE (PAGE_SHIFT - 1)
struct slabDesc *slub; struct slabDesc *slub;
int initSlabDesc(struct slabDesc **desc, size_t size); int addSlab(struct slabDesc **desc, size_t size);
static int formatPage(struct slabDesc *desc, size_t size); static int formatPage(struct slabDesc *desc, size_t size);
int initSlab(void) int allocInit(void)
{ {
uint start = log2(sizeof(void *)); uint start = log2(sizeof(void *));
list_init(slub); list_init(slub);
for (uint i = start; i < SLUB_SIZE; i++) { for (uint i = start; i < SLUB_SIZE; i++) {
struct slabDesc *slab; int ret;
if (initSlabDesc(&slab, 1U << i)) if ((ret = allocBookSlab(1U << i))) {
return ENOMEM; pr_devel("Fail to allocBookSlab %d\n", ret);
list_add_tail(slub, slab); return -ENOMEM;
}
} }
return 0; return 0;
} }
int initSlabDesc(struct slabDesc **desc, size_t size) int allocBookSlab(size_t size)
{
struct slabDesc *slabEntry;
struct slabDesc *slab;
int slabIdx;
int ret;
if ((ret = addSlab(&slabEntry, size)))
return ret;
list_foreach(slub, slab, slabIdx)
{
if (slab->size == size) {
pr_devel("Slab already exist");
return -EEXIST;
}
if (slab->size > size) {
list_insert_before(slub, slab, slabEntry);
return 0;
}
}
list_add_tail(slub, slabEntry);
return 0;
}
int addSlab(struct slabDesc **desc, size_t size)
{ {
if (size > PAGE_SIZE) if (size > PAGE_SIZE)
return ENOENT; return -ENOENT;
paddr_t alloc = allocPhyPage(); paddr_t alloc = allocPhyPage();
if (alloc == (paddr_t)NULL) if (alloc == (paddr_t)NULL)
return ENOMEM; return -ENOMEM;
if (pageMap((vaddr_t)alloc, alloc, PAGING_MEM_WRITE)) { if (pageMap((vaddr_t)alloc, alloc, PAGING_MEM_WRITE))
return ENOMEM; return -ENOMEM;
}
*desc = (struct slabDesc *)alloc; *desc = (struct slabDesc *)alloc;
list_singleton(*desc, *desc); list_singleton(*desc, *desc);
(*desc)->page = (vaddr_t)alloc; (*desc)->page = (vaddr_t)alloc;
@ -64,7 +88,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 full\n", slab); pr_devel("Slab @%d for %d full\n", slab, slab->size);
slab->full = 1; slab->full = 1;
} else { } else {
slab->freeEl = (void *)(*next); slab->freeEl = (void *)(*next);
@ -82,7 +106,7 @@ void *malloc(size_t size)
uint slubIdx = 0; uint slubIdx = 0;
list_foreach(slub, slubEntry, slubIdx) list_foreach(slub, slubEntry, slubIdx)
{ {
if(size <= slubEntry->size) if (size <= slubEntry->size)
break; break;
} }
struct slabDesc *slab; struct slabDesc *slab;
@ -96,7 +120,11 @@ void *malloc(size_t size)
} }
if (!list_foreach_early_break(slubEntry, slab, slabIdx)) { if (!list_foreach_early_break(slubEntry, slab, slabIdx)) {
struct slabDesc *newSlab; struct slabDesc *newSlab;
initSlabDesc(&newSlab, slab->size); int ret;
if ((ret = addSlab(&newSlab, slab->size))) {
pr_devel("Fail to addSlab %d\n", ret);
return NULL;
}
list_add_head(slubEntry, newSlab); list_add_head(slubEntry, newSlab);
return allocFromSlab(newSlab); return allocFromSlab(newSlab);
} }

View File

@ -10,6 +10,7 @@ struct slabDesc {
struct slabDesc *next; struct slabDesc *next;
struct slabDesc *prev; struct slabDesc *prev;
}; };
int initSlab(void); int allocInit(void);
int allocBookSlab(size_t size);
void *malloc(size_t size); void *malloc(size_t size);

View File

@ -81,7 +81,7 @@ void kmain(unsigned long magic, unsigned long addr)
printf("Setting up Serial link (115200)\n"); printf("Setting up Serial link (115200)\n");
serialSetup(115200); serialSetup(115200);
initSlab(); allocInit();
#ifdef RUN_TEST #ifdef RUN_TEST
run_test(); run_test();
#endif #endif

View File

@ -141,7 +141,7 @@ int pageMap(vaddr_t vaddr, paddr_t paddr, int flags)
if (!pd[pdEntry].present) { if (!pd[pdEntry].present) {
paddr_t ptPhy = allocPhyPage(); paddr_t ptPhy = allocPhyPage();
if (ptPhy == (vaddr_t)NULL) if (ptPhy == (vaddr_t)NULL)
return ENOMEM; return -ENOMEM;
pd[pdEntry].user = (flags & PAGING_MEM_USER) ? 1 : 0; pd[pdEntry].user = (flags & PAGING_MEM_USER) ? 1 : 0;
pd[pdEntry].present = 1; pd[pdEntry].present = 1;