CeDOS - Commit ed84a84f

Added malloc function
Celina Sophie Kalus
Tue, 25 Apr 2023 22:04:53 +0200
3 files changed, 47 insertions(+), 1 deletions(-)
M include/cedos/mm/memory.hinclude/cedos/mm/memory.h

@@ -8,6 +8,10 @@ #include <stdint.h>

typedef uint32_t size_t; +#define NULL (void*)(0) + +int malloc_init(); + /*! * Allocates a block of \p size bytes of memory. (KERNEL MODE) * \param size Size in bytes of the requested block of memory.
M src/kernel/main.csrc/kernel/main.c

@@ -7,6 +7,7 @@ #include "cedos/sched/sched.h"

#include "cedos/sched/process.h" #include "cedos/mm/paging.h" +#include "cedos/mm/memory.h" #include "cedos/interrupts.h" #include "cedos/syscall.h"

@@ -48,6 +49,10 @@ printk("done.\n");

printk("Setting up paging..."); paging_init(); + printk("done.\n"); + + printk("Initiallizing malloc..."); + malloc_init(); printk("done.\n"); printk("Activating interrupts...");
M src/kernel/mm/memory.csrc/kernel/mm/memory.c

@@ -1,12 +1,49 @@

#include "cedos/mm/memory.h" +struct memblock { + struct memblock *next; + int size; +}; + +struct memblock *malloc_first, *malloc_last, *malloc_next_free; + +int malloc_init() { + uint32_t mem_start = 0xC0400000u; + uint32_t mem_end = 0xC0800000u; + + malloc_first = (struct memblock*)(mem_start); + malloc_last = (struct memblock*)(mem_end - sizeof(struct memblock)); + malloc_next_free = malloc_first; + + malloc_first->size = 0; + malloc_first->next = malloc_last; + + malloc_last->size = 0; + malloc_last->next = NULL; +} + /*! * Allocates a block of \p size bytes of memory. (KERNEL MODE) * \param size Size in bytes of the requested block of memory. * \return Memory address to the new memory block */ void* os_kernel_malloc(size_t size) { - return (void*)0; + uint32_t addr = (uint32_t)(malloc_next_free); + + // TODO: test if memory block is large enough + addr += sizeof(struct memblock); + addr += size; + + struct memblock *new_block = (struct memblock*)(addr); + new_block->next = malloc_next_free->next; + + // TODO: maybe not big enough? + new_block->size = 0; + + malloc_next_free->next = new_block; + malloc_next_free->size = size; + + return (void*)(&malloc_next_free[1]); } /*!