From d7aaba8c85cc71f3184cddcf20b740c5157c864d Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 7 Dec 2014 11:49:52 +0100 Subject: Refactor kmalloc & tests in kmain. --- kernel/l0/kmalloc.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 kernel/l0/kmalloc.c (limited to 'kernel/l0/kmalloc.c') diff --git a/kernel/l0/kmalloc.c b/kernel/l0/kmalloc.c new file mode 100644 index 0000000..a4ef8fe --- /dev/null +++ b/kernel/l0/kmalloc.c @@ -0,0 +1,52 @@ +#include + +#include +#include + +#include +#include +#include + +static void* page_alloc_fun_for_kmalloc(size_t bytes) { + void* addr = region_alloc(bytes, REGION_T_CORE_HEAP, default_allocator_pf_handler); + return addr; +} + +static slab_type_t slab_sizes[] = { + { "8B kmalloc objects", 8, 2 }, + { "16B kmalloc objects", 16, 2 }, + { "32B kmalloc objects", 32, 2 }, + { "64B kmalloc objects", 64, 4 }, + { "128B kmalloc objects", 128, 4 }, + { "256B kmalloc objects", 256, 4 }, + { "512B kmalloc objects", 512, 8 }, + { "1KB kmalloc objects", 1024, 8 }, + { "2KB kmalloc objects", 2048, 16 }, + { "4KB kmalloc objects", 4096, 16 }, + { 0, 0, 0 } +}; + +static mem_allocator_t *kernel_allocator = 0; +STATIC_MUTEX(kmalloc_mutex); + +void kmalloc_setup() { + kernel_allocator = + create_slab_allocator(slab_sizes, page_alloc_fun_for_kmalloc, + region_free_unmap_free); +} + +void* kmalloc(size_t sz) { + void* res = 0; + + mutex_lock(&kmalloc_mutex); + res = slab_alloc(kernel_allocator, sz); + mutex_unlock(&kmalloc_mutex); + + return res; +} + +void kfree(void* ptr) { + mutex_lock(&kmalloc_mutex); + slab_free(kernel_allocator, ptr); + mutex_unlock(&kmalloc_mutex); +} -- cgit v1.2.3