From 8909e3c4ca6b1c7db887c05f50216234e0272704 Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Wed, 23 Sep 2026 18:48:44 +0200 Subject: [PATCH] more kmalloc --- kernel/include/kmalloc.h | 29 +++++++++++++++++++++++++++-- kernel/src/kmalloc.c | 18 ++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/kernel/include/kmalloc.h b/kernel/include/kmalloc.h index 85a257a..8d76285 100644 --- a/kernel/include/kmalloc.h +++ b/kernel/include/kmalloc.h @@ -11,7 +11,32 @@ void* kmalloc(size_t size); // Returns a pointer to allocated memory or NULL. void* kzalloc(size_t size); -// This function allocates memory for count elements each of size bytes, zeroed-out memory. -// Returns a pointer to allocated memory or NULL. +// This function allocates memory for count elements each of size bytes, +// zeroed-out memory. Returns a pointer to allocated memory or NULL. void* kcalloc(size_t count, size_t size); + +// powers of 2 +#define KMALLOC_MAX_SIZE 17 + +union per_cpu_freelist_cache { + void* freelists[KMALLOC_MAX_SIZE]; + struct { + void* kmalloc_8; + void* kmalloc_16; + void* kmalloc_32; + void* kmalloc_64; + void* kmalloc_128; + void* kmalloc_256; + void* kmalloc_512; + void* kmalloc_1024; + void* kmalloc_2048; + void* kmalloc_4096; + void* kmalloc_8192; + void* kmalloc_16384; + void* kmalloc_32768; + void* kmalloc_65536; + void* kmalloc_131072; + }; +}; + #endif \ No newline at end of file diff --git a/kernel/src/kmalloc.c b/kernel/src/kmalloc.c index bea9787..a5dbbbc 100644 --- a/kernel/src/kmalloc.c +++ b/kernel/src/kmalloc.c @@ -2,7 +2,9 @@ #include #include // this file contains SLUB inspired HEAP allocator -void* freelist = NULL; + +// TODO this will be an allocated array later when we have APIC MADT +union per_cpu_freelist_cache freelists_all_cpus[1]; void* kcalloc(size_t count, size_t size) { size_t size_to_alloc; @@ -32,4 +34,16 @@ void* kzalloc(size_t size) { return allocated; } -void* kmalloc(size_t size) {} \ No newline at end of file +void* kmalloc(size_t size) { + uint32_t core_id; + __asm__ volatile( + "rdtscp" + : "=c"(core_id) // Core ID from IA32_TSC_AUX is placed in ECX + : + : "eax", "edx"); + + // freelists_all_cpus[core_id]; + // TODO enable when we actualy know how many CPUs we have + if () { + } +} \ No newline at end of file