Add slab allocator

This commit is contained in:
Mathieu Maret 2019-04-11 22:34:20 +02:00
parent 28309db7b9
commit 7def058cf4
8 changed files with 136 additions and 4 deletions

98
core/alloc.c Normal file
View File

@ -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;
}

15
core/alloc.h Normal file
View File

@ -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);

View File

@ -1,3 +1,4 @@
#include "alloc.h"
#include "exception.h" #include "exception.h"
#include "gdt.h" #include "gdt.h"
#include "idt.h" #include "idt.h"
@ -83,6 +84,7 @@ void kmain(unsigned long magic, unsigned long addr)
#ifdef RUN_TEST #ifdef RUN_TEST
run_test(); run_test();
#endif #endif
initSlab();
int count = 0; int count = 0;
while (1) { while (1) {

11
core/math.c Normal file
View File

@ -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;
}

4
core/math.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#include "stdint.h"
uint32_t log2(uint32_t x);

View File

@ -3,8 +3,8 @@
#include "stdarg.h" #include "stdarg.h"
#define PAGE_SHIFT 12 #define PAGE_SHIFT 12U
#define PAGE_SIZE (1 << PAGE_SHIFT) #define PAGE_SIZE (1U << PAGE_SHIFT)
// Defined in linker.ld script // Defined in linker.ld script
extern uint32_t __ld_kernel_begin; extern uint32_t __ld_kernel_begin;

View File

@ -2,8 +2,8 @@
#include "types.h" #include "types.h"
#define PAGING_MEM_USER 1 #define PAGING_MEM_USER 1
#define PAGING_MEM_READ (1 << 1) #define PAGING_MEM_READ (1U << 1)
#define PAGING_MEM_WRITE (1 << 2) #define PAGING_MEM_WRITE (1U << 2)
int pagingSetup(paddr_t upperKernelAddr); int pagingSetup(paddr_t upperKernelAddr);

View File

@ -41,9 +41,11 @@ typedef enum { FALSE=0, TRUE } bool_t;
#if __x86_64__ #if __x86_64__
typedef unsigned long size_t; typedef unsigned long size_t;
typedef long ssize_t; typedef long ssize_t;
typedef unsigned long int uintptr_t;
#else #else
typedef unsigned int size_t; typedef unsigned int size_t;
typedef int ssize_t; typedef int ssize_t;
typedef unsigned int uintptr_t;
#endif #endif
typedef void *va_list; typedef void *va_list;