2019-04-11 22:34:20 +02:00
|
|
|
#define pr_fmt(fmt) "[alloc]: " fmt
|
|
|
|
//#define DEBUG
|
|
|
|
#include "alloc.h"
|
|
|
|
#include "errno.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "math.h"
|
|
|
|
#include "mem.h"
|
|
|
|
#include "vga.h"
|
|
|
|
|
2019-04-15 22:48:37 +02:00
|
|
|
#define IS_SELF_CONTAINED(desc) ((vaddr_t)((desc)->page) == (vaddr_t)(desc))
|
2019-04-11 22:34:20 +02:00
|
|
|
// Slab will contains object from sizeof(void *) to PAGE_SIZE/2 by pow2
|
|
|
|
#define SLUB_SIZE (PAGE_SHIFT - 1)
|
2019-04-14 15:56:48 +02:00
|
|
|
struct slabDesc *slub;
|
2019-04-11 22:34:20 +02:00
|
|
|
|
2019-04-15 22:48:37 +02:00
|
|
|
int addSlab(struct slabDesc **desc, size_t size, int self_containing);
|
|
|
|
static int formatPage(struct slabDesc *desc, size_t size, int selfContained);
|
2019-04-11 22:34:20 +02:00
|
|
|
|
2019-04-15 21:35:38 +02:00
|
|
|
int allocInit(void)
|
2019-04-11 22:34:20 +02:00
|
|
|
{
|
|
|
|
uint start = log2(sizeof(void *));
|
2019-04-14 15:56:48 +02:00
|
|
|
list_init(slub);
|
2019-04-15 22:48:37 +02:00
|
|
|
int ret;
|
|
|
|
if ((ret = allocBookSlab(sizeof(struct slabDesc), 1))) {
|
|
|
|
pr_devel("Fail to allocBookSlab %d for slabDesc :( \n");
|
|
|
|
return ret;
|
|
|
|
}
|
2019-04-11 22:34:20 +02:00
|
|
|
for (uint i = start; i < SLUB_SIZE; i++) {
|
2019-04-15 22:48:37 +02:00
|
|
|
if ((ret = allocBookSlab(1U << i, 0))) {
|
|
|
|
pr_devel("Fail to allocBookSlab %d for %d \n", ret, (1U << i));
|
|
|
|
return ret;
|
2019-04-15 21:35:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:48:37 +02:00
|
|
|
int allocBookSlab(size_t size, int selfContained)
|
2019-04-15 21:35:38 +02:00
|
|
|
{
|
2019-04-16 20:11:24 +02:00
|
|
|
// pr_devel("%s for size %d is self %d\n", __func__, size, selfContained );
|
2019-04-15 21:35:38 +02:00
|
|
|
struct slabDesc *slabEntry;
|
|
|
|
struct slabDesc *slab;
|
|
|
|
int slabIdx;
|
|
|
|
int ret;
|
2019-04-15 22:48:37 +02:00
|
|
|
if ((ret = addSlab(&slabEntry, size, selfContained)))
|
2019-04-15 21:35:38 +02:00
|
|
|
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;
|
|
|
|
}
|
2019-04-11 22:34:20 +02:00
|
|
|
}
|
2019-04-15 21:35:38 +02:00
|
|
|
list_add_tail(slub, slabEntry);
|
2019-04-11 22:34:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:48:37 +02:00
|
|
|
int addSlab(struct slabDesc **desc, size_t size, int selfContained)
|
2019-04-11 22:34:20 +02:00
|
|
|
{
|
2019-04-15 22:48:37 +02:00
|
|
|
pr_devel("%s for size %d is self %d\n", __func__, size, selfContained);
|
2019-04-11 22:34:20 +02:00
|
|
|
if (size > PAGE_SIZE)
|
2019-04-15 21:35:38 +02:00
|
|
|
return -ENOENT;
|
2019-04-11 22:34:20 +02:00
|
|
|
paddr_t alloc = allocPhyPage();
|
|
|
|
if (alloc == (paddr_t)NULL)
|
2019-04-15 21:35:38 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
if (pageMap((vaddr_t)alloc, alloc, PAGING_MEM_WRITE))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-04-15 22:48:37 +02:00
|
|
|
if (selfContained) {
|
|
|
|
*desc = (struct slabDesc *)alloc;
|
|
|
|
(*desc)->freeEl = (char *)(*desc) + sizeof(struct slabDesc);
|
|
|
|
} else {
|
2019-04-16 20:11:24 +02:00
|
|
|
*desc = malloc(sizeof(struct slabDesc));
|
2019-04-15 22:48:37 +02:00
|
|
|
(*desc)->freeEl = (void *)alloc;
|
|
|
|
}
|
2019-04-11 22:34:20 +02:00
|
|
|
list_singleton(*desc, *desc);
|
2019-04-16 20:11:24 +02:00
|
|
|
(*desc)->page = (vaddr_t)alloc;
|
|
|
|
(*desc)->full = 0;
|
|
|
|
(*desc)->size = size;
|
2019-04-11 22:34:20 +02:00
|
|
|
pr_devel("got page %d for size %d first %d", alloc, size, (*desc)->freeEl);
|
2019-04-15 22:48:37 +02:00
|
|
|
return formatPage((*desc), size, selfContained);
|
2019-04-11 22:34:20 +02:00
|
|
|
}
|
|
|
|
|
2019-04-15 22:48:37 +02:00
|
|
|
static int formatPage(struct slabDesc *desc, size_t size, int selfContained)
|
2019-04-11 22:34:20 +02:00
|
|
|
{
|
2019-04-15 22:48:37 +02:00
|
|
|
char *cur = desc->freeEl;
|
|
|
|
ulong nbEl = PAGE_SIZE / size - 1;
|
|
|
|
if (selfContained)
|
|
|
|
nbEl = (PAGE_SIZE - sizeof(struct slabDesc)) / size - 1;
|
2019-04-11 22:34:20 +02:00
|
|
|
ulong i;
|
2019-04-15 22:48:37 +02:00
|
|
|
for (i = 0; i < nbEl; i++) {
|
2019-04-11 22:34:20 +02:00
|
|
|
*((vaddr_t *)cur) = (vaddr_t)cur + size;
|
|
|
|
cur += size;
|
|
|
|
}
|
|
|
|
*((vaddr_t *)cur) = (vaddr_t)NULL;
|
|
|
|
pr_devel("last at %d allocated %d\n", cur, i + 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *allocFromSlab(struct slabDesc *slab)
|
|
|
|
{
|
|
|
|
vaddr_t *next = slab->freeEl;
|
|
|
|
if (*next == (vaddr_t)NULL) {
|
2019-04-15 21:35:38 +02:00
|
|
|
pr_devel("Slab @%d for %d full\n", slab, slab->size);
|
2019-04-11 22:34:20 +02:00
|
|
|
slab->full = 1;
|
|
|
|
} else {
|
|
|
|
slab->freeEl = (void *)(*next);
|
|
|
|
}
|
|
|
|
return (void *)next;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *malloc(size_t size)
|
|
|
|
{
|
|
|
|
if (size > (1U << SLUB_SIZE)) {
|
|
|
|
printf("implement malloc for big size\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-14 15:56:48 +02:00
|
|
|
struct slabDesc *slubEntry;
|
2019-04-16 20:11:24 +02:00
|
|
|
uint slubIdx;
|
2019-04-14 15:56:48 +02:00
|
|
|
list_foreach(slub, slubEntry, slubIdx)
|
|
|
|
{
|
2019-04-15 21:35:38 +02:00
|
|
|
if (size <= slubEntry->size)
|
2019-04-14 15:56:48 +02:00
|
|
|
break;
|
2019-04-11 22:34:20 +02:00
|
|
|
}
|
|
|
|
struct slabDesc *slab;
|
|
|
|
int slabIdx;
|
2019-04-14 15:56:48 +02:00
|
|
|
list_foreach(slubEntry, slab, slabIdx)
|
2019-04-11 22:34:20 +02:00
|
|
|
{
|
|
|
|
if (!slab->full) {
|
2019-04-16 20:11:24 +02:00
|
|
|
pr_devel("found place in slub %d at idx %d for size %d\n", slubIdx,
|
|
|
|
slabIdx, size);
|
2019-04-11 22:34:20 +02:00
|
|
|
return allocFromSlab(slab);
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 22:48:37 +02:00
|
|
|
|
|
|
|
// No room found
|
|
|
|
struct slabDesc *newSlab;
|
|
|
|
int ret;
|
|
|
|
if ((ret = addSlab(&newSlab, slab->size, IS_SELF_CONTAINED(slubEntry)))) {
|
|
|
|
pr_devel("Fail to addSlab %d\n", ret);
|
|
|
|
return NULL;
|
2019-04-11 22:34:20 +02:00
|
|
|
}
|
2019-04-15 22:48:37 +02:00
|
|
|
list_add_head(slubEntry, newSlab);
|
|
|
|
return allocFromSlab(newSlab);
|
2019-04-11 22:34:20 +02:00
|
|
|
}
|
2019-04-15 23:09:09 +02:00
|
|
|
|
2019-04-16 20:11:24 +02:00
|
|
|
int slabFree(void *ptr, struct slabDesc *slab)
|
|
|
|
{
|
2019-04-15 23:09:09 +02:00
|
|
|
struct slabDesc *slabEntry;
|
|
|
|
int slabIdx;
|
|
|
|
list_foreach(slab, slabEntry, slabIdx)
|
|
|
|
{
|
|
|
|
if ((slabEntry->page <= (vaddr_t)ptr) &&
|
2019-04-16 20:11:24 +02:00
|
|
|
((vaddr_t)ptr < (slabEntry->page + PAGE_SIZE))) {
|
2019-04-15 23:09:09 +02:00
|
|
|
pr_devel("free place! was %d is now %d\n", slabEntry->freeEl, ptr);
|
|
|
|
*((vaddr_t *)ptr) = (vaddr_t)slabEntry->freeEl;
|
|
|
|
slabEntry->freeEl = ptr;
|
2019-04-16 20:11:24 +02:00
|
|
|
slabEntry->full = 0;
|
2019-04-15 23:09:09 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2019-04-16 20:11:24 +02:00
|
|
|
void free(void *ptr)
|
|
|
|
{
|
|
|
|
if (!ptr)
|
2019-04-15 23:09:09 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
struct slabDesc *slab;
|
|
|
|
int slabIdx;
|
2019-04-16 20:11:24 +02:00
|
|
|
list_foreach(slub, slab, slabIdx)
|
|
|
|
{
|
|
|
|
if (slabFree(ptr, slab))
|
2019-04-15 23:09:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
pr_devel("free: slab not found\n");
|
|
|
|
}
|