mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-27 12:48:22 +00:00
kernel: initial alloc and free implementation
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include <arch/mmu.h>
|
||||
#include <kernel/vmm.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/alloc.h>
|
||||
|
||||
static cpu_t bsp_cpu;
|
||||
|
||||
@@ -22,5 +23,6 @@ void kernel_entry() {
|
||||
logging_sethook(term_putchar);
|
||||
arch_mmu_init();
|
||||
vmm_init();
|
||||
alloc_init();
|
||||
__assert(!"kernel entry end");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#ifndef _ALLOC_H
|
||||
#define _ALLOC_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void alloc_init();
|
||||
void *alloc(size_t s);
|
||||
void free(void *addr);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
#include <kernel/alloc.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <logging.h>
|
||||
#include <string.h>
|
||||
|
||||
#define CACHE_COUNT 12
|
||||
|
||||
static size_t allocsizes[CACHE_COUNT] = {32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536};
|
||||
static scache_t *caches[CACHE_COUNT];
|
||||
|
||||
static void initarea(scache_t *cache, void *obj) {
|
||||
printf("slab ctor called\n");
|
||||
size_t *ptr = obj;
|
||||
*ptr = cache->size - sizeof(size_t);
|
||||
memset(ptr + 1, 0, *ptr);
|
||||
}
|
||||
|
||||
static scache_t *getcachefromsize(size_t size) {
|
||||
// XXX find a faster way to check this
|
||||
scache_t *cache = NULL;
|
||||
for (int i = 0; i < CACHE_COUNT; ++i) {
|
||||
if (size <= allocsizes[i]) {
|
||||
cache = caches[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
__assert(cache);
|
||||
return cache;
|
||||
}
|
||||
|
||||
void *alloc(size_t size) {
|
||||
scache_t *cache = getcachefromsize(size);
|
||||
size_t *ret = slab_allocate(cache);
|
||||
if (ret == NULL)
|
||||
return NULL;
|
||||
return ret + 1;
|
||||
}
|
||||
|
||||
void free(void *ptr) {
|
||||
size_t *start = ptr;
|
||||
size_t size = *start;
|
||||
start += 1;
|
||||
scache_t *cache = getcachefromsize(size);
|
||||
slab_free(cache, ptr);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void alloc_init() {
|
||||
for (int i = 0; i < CACHE_COUNT; ++i) {
|
||||
caches[i] = slab_newcache(allocsizes[i] + sizeof(size_t), 0, initarea, initarea);
|
||||
__assert(caches[i]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user