Alloc: selfContaining is optionnal
This commit is contained in:
parent
f1b3895473
commit
698490800f
53
core/alloc.c
53
core/alloc.c
@ -7,34 +7,41 @@
|
|||||||
#include "mem.h"
|
#include "mem.h"
|
||||||
#include "vga.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
|
// 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)
|
||||||
struct slabDesc *slub;
|
struct slabDesc *slub;
|
||||||
|
|
||||||
int addSlab(struct slabDesc **desc, size_t size);
|
int addSlab(struct slabDesc **desc, size_t size, int self_containing);
|
||||||
static int formatPage(struct slabDesc *desc, size_t size);
|
static int formatPage(struct slabDesc *desc, size_t size, int selfContained);
|
||||||
|
|
||||||
int allocInit(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++) {
|
|
||||||
int ret;
|
int ret;
|
||||||
if ((ret = allocBookSlab(1U << i))) {
|
if ((ret = allocBookSlab(sizeof(struct slabDesc), 1))) {
|
||||||
pr_devel("Fail to allocBookSlab %d\n", ret);
|
pr_devel("Fail to allocBookSlab %d for slabDesc :( \n");
|
||||||
return -ENOMEM;
|
return ret;
|
||||||
|
}
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int allocBookSlab(size_t size)
|
int allocBookSlab(size_t size, int 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;
|
||||||
int ret;
|
int ret;
|
||||||
if ((ret = addSlab(&slabEntry, size)))
|
if ((ret = addSlab(&slabEntry, size, selfContained)))
|
||||||
return ret;
|
return ret;
|
||||||
list_foreach(slub, slab, slabIdx)
|
list_foreach(slub, slab, slabIdx)
|
||||||
{
|
{
|
||||||
@ -51,8 +58,9 @@ int allocBookSlab(size_t size)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int addSlab(struct slabDesc **desc, size_t size)
|
int addSlab(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)
|
if (size > PAGE_SIZE)
|
||||||
return -ENOENT;
|
return -ENOENT;
|
||||||
paddr_t alloc = allocPhyPage();
|
paddr_t alloc = allocPhyPage();
|
||||||
@ -61,21 +69,29 @@ int addSlab(struct slabDesc **desc, size_t size)
|
|||||||
if (pageMap((vaddr_t)alloc, alloc, PAGING_MEM_WRITE))
|
if (pageMap((vaddr_t)alloc, alloc, PAGING_MEM_WRITE))
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
|
if (selfContained) {
|
||||||
*desc = (struct slabDesc *)alloc;
|
*desc = (struct slabDesc *)alloc;
|
||||||
|
(*desc)->freeEl = (char *)(*desc) + sizeof(struct slabDesc);
|
||||||
|
} else {
|
||||||
|
*desc = malloc(sizeof(struct slabDesc));
|
||||||
|
(*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)->freeEl = (char *)(*desc) + sizeof(struct slabDesc);
|
|
||||||
(*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);
|
return formatPage((*desc), size, selfContained);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int formatPage(struct slabDesc *desc, size_t size)
|
static int formatPage(struct slabDesc *desc, size_t size, int selfContained)
|
||||||
{
|
{
|
||||||
char *cur = (char *)desc + sizeof(struct slabDesc);
|
char *cur = desc->freeEl;
|
||||||
|
ulong nbEl = PAGE_SIZE / size - 1;
|
||||||
|
if (selfContained)
|
||||||
|
nbEl = (PAGE_SIZE - sizeof(struct slabDesc)) / size - 1;
|
||||||
ulong i;
|
ulong i;
|
||||||
for (i = 0; i < ((PAGE_SIZE - sizeof(struct slabDesc)) / size - 1); i++) {
|
for (i = 0; i < nbEl; i++) {
|
||||||
*((vaddr_t *)cur) = (vaddr_t)cur + size;
|
*((vaddr_t *)cur) = (vaddr_t)cur + size;
|
||||||
cur += size;
|
cur += size;
|
||||||
}
|
}
|
||||||
@ -114,19 +130,18 @@ void *malloc(size_t size)
|
|||||||
list_foreach(slubEntry, slab, slabIdx)
|
list_foreach(slubEntry, slab, slabIdx)
|
||||||
{
|
{
|
||||||
if (!slab->full) {
|
if (!slab->full) {
|
||||||
pr_devel("found place in slub %d at idx %d \n", slubIdx, slabIdx);
|
pr_devel("found place in slub %d at idx %d for size %d\n", slubIdx, slabIdx, size);
|
||||||
return allocFromSlab(slab);
|
return allocFromSlab(slab);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!list_foreach_early_break(slubEntry, slab, slabIdx)) {
|
|
||||||
|
// No room found
|
||||||
struct slabDesc *newSlab;
|
struct slabDesc *newSlab;
|
||||||
int ret;
|
int ret;
|
||||||
if ((ret = addSlab(&newSlab, slab->size))) {
|
if ((ret = addSlab(&newSlab, slab->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;
|
||||||
}
|
}
|
||||||
list_add_head(slubEntry, newSlab);
|
list_add_head(slubEntry, newSlab);
|
||||||
return allocFromSlab(newSlab);
|
return allocFromSlab(newSlab);
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,6 @@ struct slabDesc {
|
|||||||
struct slabDesc *prev;
|
struct slabDesc *prev;
|
||||||
};
|
};
|
||||||
int allocInit(void);
|
int allocInit(void);
|
||||||
int allocBookSlab(size_t size);
|
int allocBookSlab(size_t size, int selfContained);
|
||||||
|
|
||||||
void *malloc(size_t size);
|
void *malloc(size_t size);
|
||||||
|
Loading…
Reference in New Issue
Block a user