From 577779f8ae1eaa144d225ce82b7f9430398cf09f Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Thu, 20 Apr 2023 17:03:10 -0400 Subject: [PATCH] mem: add kmalloc and kfree --- include/kernel/common.h | 5 +++ include/kernel/slab.h | 11 ++++++ init/main.c | 2 + mem/slab.c | 87 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 104 insertions(+), 1 deletion(-) diff --git a/include/kernel/common.h b/include/kernel/common.h index 00cd6c4..ec613e1 100644 --- a/include/kernel/common.h +++ b/include/kernel/common.h @@ -14,6 +14,11 @@ #include #define PACKED __attribute__((packed)) +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +#define KB 1024 +#define MB (1024 * KB) +#define GB (1024 * MB) typedef uintptr_t paddr_t; diff --git a/include/kernel/slab.h b/include/kernel/slab.h index e5418c5..80c25c1 100644 --- a/include/kernel/slab.h +++ b/include/kernel/slab.h @@ -13,8 +13,19 @@ struct slab { struct slab *next; }; +struct kmap_entry { + uintptr_t *ptr; + size_t size; + struct slab *slab; + struct kmap_entry *next; +}; + struct slab *slab_create(size_t size); void *slab_alloc(struct slab *slab); void slab_free(struct slab *slab, void *ptr); +void kmalloc_init(); +void *kmalloc(size_t size); +void kfree(void *ptr); + #endif /* _SLAB_H_ */ diff --git a/init/main.c b/init/main.c index c87ed52..a9a4c9d 100644 --- a/init/main.c +++ b/init/main.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -43,6 +44,7 @@ void kmain(void) const uintptr_t one_gb = 0x40000000; char kmem[one_gb] ALIGN(0x1000); mem_early_init(kmem, one_gb); + kmalloc_init(); cli(); pic_mask(4, 0); diff --git a/mem/slab.c b/mem/slab.c index 840055f..8307dee 100644 --- a/mem/slab.c +++ b/mem/slab.c @@ -5,6 +5,12 @@ #define SLAB_MIN_COUNT 16 +size_t slab_sizes[] = { 16, 32, 64, 128, 256, 512, 1024, 2048 }; +struct slab *slab_cache[ARRAY_SIZE(slab_sizes)]; + +struct slab *kmap_slab = NULL; +struct kmap_entry *kmem_map = NULL; + void slab_range(struct slab *slab, uintptr_t *o_start, uintptr_t *o_end) { uintptr_t sslab = (uintptr_t)slab; @@ -49,6 +55,9 @@ struct slab *slab_create(size_t size) void *slab_alloc(struct slab *slab) { + if (slab == NULL) + return NULL; + if (slab->nextfree == NULL) { if (slab->next == NULL) { slab->next = slab_create(slab->size); @@ -76,9 +85,85 @@ void slab_free(struct slab *slab, void *ptr) uintptr_t high; slab_range(slab, &low, &high); - if ((uintptr_t)ptr < low || (uintptr_t)ptr > high) + if ((uintptr_t)ptr < low || (uintptr_t)ptr > high) { slab_free(slab->next, ptr); + return; + } *(uintptr_t *)ptr = (uintptr_t)slab->nextfree; slab->nextfree = ptr; } + +void kmalloc_init() +{ + for (size_t i = 0; i < ARRAY_SIZE(slab_sizes); i++) + slab_cache[i] = slab_create(slab_sizes[i]); + kmap_slab = slab_create(sizeof(struct kmap_entry)); +} + +void *kmalloc(size_t size) +{ + void *ret = NULL; + struct slab *slab = NULL; + + if (size > slab_sizes[ARRAY_SIZE(slab_sizes) - 1]) { + ret = buddy_alloc(npow2(size)); + } else { + for (size_t i = 0; i < ARRAY_SIZE(slab_sizes); i++) { + if (size <= slab_sizes[i]) { + size = slab_sizes[i]; + slab = slab_cache[i]; + ret = slab_alloc(slab); + + break; + } + } + } + + if (ret == NULL) + return NULL; + + struct kmap_entry *entry = slab_alloc(kmap_slab); + if (entry == NULL) { + slab_free(slab, ret); + return NULL; + } + + entry->ptr = ret; + entry->slab = slab; + entry->size = size; + entry->next = kmem_map; + + kmem_map = entry; + + return ret; +} + +void kfree(void *ptr) +{ + if (ptr == NULL) + return; + + struct kmap_entry *entry = kmem_map; + struct kmap_entry *prev = NULL; + + while (entry != NULL) { + if (entry->ptr == ptr) { + if (prev == NULL) + kmem_map = entry->next; + else + prev->next = entry->next; + + if (entry->slab == NULL) + buddy_free(entry->ptr); + else + slab_free(entry->slab, entry->ptr); + + slab_free(kmap_slab, entry); + return; + } + + prev = entry; + entry = entry->next; + } +}