From 70114f6326d85907facc7d7a7c1c86c73c51ad64 Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Wed, 23 Sep 2026 12:48:54 +0200 Subject: [PATCH] basic heap scafolding --- TODO | 2 +- docs/memory-management.txt | 16 ++++++++++++++++ kernel/include/kmalloc.h | 17 +++++++++++++++++ kernel/src/kmalloc.c | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 kernel/include/kmalloc.h create mode 100644 kernel/src/kmalloc.c diff --git a/TODO b/TODO index 64bc727..ed93c31 100644 --- a/TODO +++ b/TODO @@ -22,7 +22,7 @@ Low-Level Hardware & Interrupts Memory Management [x] Physical memory manager (Bitmap or Buddy) [x] Paging (Recursive or Offset mapping) -[ ] Kernel heap (kmalloc/kfree - Linked list or Slab) +[-] Kernel heap (kmalloc/kfree - Linked list or Slab) [ ] Memory mapped I/O (MMIO) API for drivers [ ] VMM: Shared memory regions (For IPC/Framebuffers) [ ] VMM: Copy-on-Write (COW) support (Optional but good) diff --git a/docs/memory-management.txt b/docs/memory-management.txt index 2a9605f..bf376e9 100644 --- a/docs/memory-management.txt +++ b/docs/memory-management.txt @@ -48,3 +48,19 @@ A pointer to allocated memory or NULL. **Parameters** * `size_t count` - +**Description** +This function allocates memory for `count` elements each of `size` bytes, zeroed-out memory. + +**Returns** +A pointer to allocated memory or NULL. + +`void* kfree(void* p)` +****************************************** +**Parameters** +* `void* p` - + +**Description** +This function frees memory previously allocated using `k[m,z,c]alloc()` + +**Returns** +Nothing \ No newline at end of file diff --git a/kernel/include/kmalloc.h b/kernel/include/kmalloc.h new file mode 100644 index 0000000..85a257a --- /dev/null +++ b/kernel/include/kmalloc.h @@ -0,0 +1,17 @@ +#ifndef _KMALLOC_H +#define _KMALLOC_H + +#include "types.h" + +// This function allocates size bytes of contiguous memory. +// Returns a pointer to allocated memory or NULL. +void* kmalloc(size_t size); + +// This function allocates size bytes of contiguous, zeroed-out memory. +// 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. +void* kcalloc(size_t count, size_t size); +#endif \ No newline at end of file diff --git a/kernel/src/kmalloc.c b/kernel/src/kmalloc.c new file mode 100644 index 0000000..bea9787 --- /dev/null +++ b/kernel/src/kmalloc.c @@ -0,0 +1,35 @@ +#include "types.h" +#include +#include +// this file contains SLUB inspired HEAP allocator +void* freelist = NULL; + +void* kcalloc(size_t count, size_t size) { + size_t size_to_alloc; + __asm__ volatile goto(".intel_syntax noprefix\n\t" + "mov rax, %1\n\t" + "mov rdx, %2\n\t" + "mul rdx\n\t" + "jo %l[overflow]\n\t" + "mov %0, rax\n\t" + ".att_syntax prefix\n\t" + : "=r"(size_to_alloc) + : "r"(count), "r"(size) + : "rax", "rdx", "cc" + : overflow); + return kzalloc(size_to_alloc); + +overflow: + return NULL; +} + +void* kzalloc(size_t size) { + void* allocated = kmalloc(size); + if (!allocated) { + return NULL; + } + memset(allocated, 0, size); + return allocated; +} + +void* kmalloc(size_t size) {} \ No newline at end of file