From 7def058cf41a78c00562f8b26a9a0842329bbcb0 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Thu, 11 Apr 2019 22:34:20 +0200 Subject: [PATCH] Add slab allocator --- core/alloc.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++ core/alloc.h | 15 ++++++++ core/main.c | 2 ++ core/math.c | 11 ++++++ core/math.h | 4 +++ core/mem.h | 4 +-- core/paging.h | 4 +-- core/stdarg.h | 2 ++ 8 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 core/alloc.c create mode 100644 core/alloc.h create mode 100644 core/math.c create mode 100644 core/math.h diff --git a/core/alloc.c b/core/alloc.c new file mode 100644 index 0000000..5a96d28 --- /dev/null +++ b/core/alloc.c @@ -0,0 +1,98 @@ +#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" + +// Slab will contains object from sizeof(void *) to PAGE_SIZE/2 by pow2 +#define SLUB_SIZE (PAGE_SHIFT - 1) +struct slabDesc *slubArray[SLUB_SIZE]; + +int initSlabDesc(struct slabDesc **desc, size_t size); +int formatPage(struct slabDesc *desc, size_t size); + +int initSlab(void) +{ + uint start = log2(sizeof(void *)); + for (uint i = start; i < SLUB_SIZE; i++) { + if (initSlabDesc(&slubArray[i - start], 1U << i)) + return ENOMEM; + } + return 0; +} + +int initSlabDesc(struct slabDesc **desc, size_t size) +{ + if (size > PAGE_SIZE) + return ENOENT; + paddr_t alloc = allocPhyPage(); + if (alloc == (paddr_t)NULL) + return ENOMEM; + if (pageMap((vaddr_t)alloc, alloc, PAGING_MEM_WRITE)) { + return ENOMEM; + } + *desc = (struct slabDesc *)alloc; + list_singleton(*desc, *desc); + (*desc)->page = (vaddr_t)alloc; + (*desc)->full = 0; + (*desc)->freeEl = (char *)(*desc) + sizeof(struct slabDesc); + (*desc)->size = size; + pr_devel("got page %d for size %d first %d", alloc, size, (*desc)->freeEl); + return formatPage((*desc), size); +} + +int formatPage(struct slabDesc *desc, size_t size) +{ + char *cur = (char *)desc + sizeof(struct slabDesc); + ulong i; + for (i = 0; i < ((PAGE_SIZE - sizeof(struct slabDesc)) / size - 1); i++) { + *((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) { + pr_devel("Slab %d full\n", slab); + 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; + } + uint slubIdx = 0; + while (size > slubArray[slubIdx]->size) { + slubIdx++; + } + struct slabDesc *slab; + int slabIdx; + list_foreach(slubArray[slubIdx], slab, slabIdx) + { + if (!slab->full) { + pr_devel("found place in slub %d at idx %d \n", slubIdx, slabIdx); + return allocFromSlab(slab); + } + } + if (!list_foreach_early_break(slubArray[slubIdx], slab, slabIdx)) { + struct slabDesc *newSlab; + initSlabDesc(&newSlab, slab->size); + list_add_head(slubArray[slubIdx], newSlab); + return allocFromSlab(newSlab); + } + return NULL; +} diff --git a/core/alloc.h b/core/alloc.h new file mode 100644 index 0000000..cbe1019 --- /dev/null +++ b/core/alloc.h @@ -0,0 +1,15 @@ +#pragma once +#include "paging.h" +#include "stdarg.h" + +struct slabDesc { + vaddr_t page; + size_t size; + void *freeEl; + char full; + struct slabDesc *next; + struct slabDesc *prev; +}; +int initSlab(void); + +void *malloc(size_t size); diff --git a/core/main.c b/core/main.c index 2ade256..4d24459 100644 --- a/core/main.c +++ b/core/main.c @@ -1,3 +1,4 @@ +#include "alloc.h" #include "exception.h" #include "gdt.h" #include "idt.h" @@ -83,6 +84,7 @@ void kmain(unsigned long magic, unsigned long addr) #ifdef RUN_TEST run_test(); #endif + initSlab(); int count = 0; while (1) { diff --git a/core/math.c b/core/math.c new file mode 100644 index 0000000..a6b13e5 --- /dev/null +++ b/core/math.c @@ -0,0 +1,11 @@ +#include "math.h" + +inline uint32_t log2(const uint32_t x) { + uint32_t y; + // Get the highest set bit + asm ( "\tbsr %1, %0\n" + : "=r"(y) + : "r" (x) + ); + return y; +} diff --git a/core/math.h b/core/math.h new file mode 100644 index 0000000..92bfdbb --- /dev/null +++ b/core/math.h @@ -0,0 +1,4 @@ +#pragma once +#include "stdint.h" + +uint32_t log2(uint32_t x); diff --git a/core/mem.h b/core/mem.h index fb8844b..1652b78 100644 --- a/core/mem.h +++ b/core/mem.h @@ -3,8 +3,8 @@ #include "stdarg.h" -#define PAGE_SHIFT 12 -#define PAGE_SIZE (1 << PAGE_SHIFT) +#define PAGE_SHIFT 12U +#define PAGE_SIZE (1U << PAGE_SHIFT) // Defined in linker.ld script extern uint32_t __ld_kernel_begin; diff --git a/core/paging.h b/core/paging.h index 4bdcecc..9b143d1 100644 --- a/core/paging.h +++ b/core/paging.h @@ -2,8 +2,8 @@ #include "types.h" #define PAGING_MEM_USER 1 -#define PAGING_MEM_READ (1 << 1) -#define PAGING_MEM_WRITE (1 << 2) +#define PAGING_MEM_READ (1U << 1) +#define PAGING_MEM_WRITE (1U << 2) int pagingSetup(paddr_t upperKernelAddr); diff --git a/core/stdarg.h b/core/stdarg.h index 6866c81..36589ef 100644 --- a/core/stdarg.h +++ b/core/stdarg.h @@ -41,9 +41,11 @@ typedef enum { FALSE=0, TRUE } bool_t; #if __x86_64__ typedef unsigned long size_t; typedef long ssize_t; +typedef unsigned long int uintptr_t; #else typedef unsigned int size_t; typedef int ssize_t; +typedef unsigned int uintptr_t; #endif typedef void *va_list;