Wed, 29 Nov 2023 14:47:51 +0100
11 files changed,
93 insertions(+),
59 deletions(-)
A
common/memory.h
@@ -0,0 +1,34 @@
+/*! \file + * Provides functions for memory allocation. + */ +#ifndef MEMORY_H +#define MEMORY_H + +#include <stdint.h> + +typedef uint32_t size_t; + +#define NULL (void*)(0) + +/*! + * Initialize memory allocator with predefined memory region. + * \param start Start of malloc memory region. + * \param end End of malloc memory region. + * \return Currently always 0. + */ +int malloc_init(void *start, void *end); + +/*! + * Allocates a block of \p size bytes of memory. + * \param size Size in bytes of the requested block of memory. + * \return Memory address to the new memory block + */ +void* malloc(size_t size); + +/*! + * Frees a previously allocated block of memory. + * \param ptr Pointer to the memory block to be freed. + */ +void free(void* ptr); + +#endif
M
kernel/drivers/ps2_keyboard.c
→
kernel/drivers/ps2_keyboard.c
@@ -3,7 +3,7 @@ #include "interrupts.h"
#include "pic.h" #include "core.h" #include "sched/sched.h" -#include "mm/memory.h" +#include "memory.h" #include "assembly.h" #define PS2_DATA 0x60@@ -83,7 +83,7 @@ int ps2_kb_init(void) {
buffer_head = 0; buffer_tail = 0; - buffer = os_kernel_malloc(BUFFER_LENGTH); + buffer = malloc(BUFFER_LENGTH); // clear incoming data inb(PS2_DATA);
M
kernel/elf.c
→
kernel/elf.c
@@ -5,7 +5,7 @@
#include "file.h" #include "sched/process.h" -#include "mm/memory.h" +#include "memory.h" #include "assert.h"@@ -175,7 +175,7 @@ int section_size = header.sh_entry_size;
SECT_HEADER *sect_names_sh = §_headers[header.sh_strndx]; - char *sect_names = os_kernel_malloc(sect_names_sh->size); + char *sect_names = malloc(sect_names_sh->size); if (sect_names == NULL) { printk("Error while starting executable: Memory allocation failed.\n");
M
kernel/fat.c
→
kernel/fat.c
@@ -4,7 +4,7 @@ #include "string.h"
#include "assert.h" #include "core.h" -#include "mm/memory.h" +#include "memory.h" #include <stdint.h>@@ -85,7 +85,7 @@ long root_dir_size = boot_sect->max_root_dir_entries * sizeof(DIR_ENTRY);
data_lba = root_lba + (root_dir_size / boot_sect->bytes_per_sect); cluster_size = boot_sect->bytes_per_sect * boot_sect->sect_per_cluster; - cluster_buffer = os_kernel_malloc(cluster_size); + cluster_buffer = malloc(cluster_size); } void *FAT_read_sector_offset(uint32_t lba, uint32_t *offset) {
M
kernel/file.c
→
kernel/file.c
@@ -5,7 +5,7 @@
#include "drivers/tty.h" #include "core.h" -#include "mm/memory.h" +#include "memory.h" #include "sched/sched.h" #include "sched/process.h"@@ -24,7 +24,7 @@
int stdin, stdout, fat_root, pipe; int file_init() { - file_table = os_kernel_malloc(sizeof(file_t) * 512); + file_table = malloc(sizeof(file_t) * 512); file_table[next_free].fops = &tty_fops; file_table[next_free].stdio_id = 0;
M
kernel/main.c
→
kernel/main.c
@@ -7,7 +7,7 @@ #include "sched/sched.h"
#include "sched/process.h" #include "mm/paging.h" -#include "mm/memory.h" +#include "memory.h" #include "interrupts.h" #include "syscall.h"@@ -56,7 +56,7 @@ sti();
printk("done.\n"); printk("Initiallizing malloc..."); - malloc_init(); + malloc_init(0xC0400000u, 0xC0800000u); printk("done.\n"); printk("Installing syscalls...");
M
kernel/mm/memory.c
→
common/memory.c
@@ -1,4 +1,4 @@
-#include "mm/memory.h" +#include "memory.h" #include "assert.h"@@ -9,9 +9,9 @@ };
volatile struct memblock volatile *malloc_first, *malloc_last, *malloc_next_free; -int malloc_init() { - uint32_t mem_start = 0xC0400000u; - uint32_t mem_end = 0xC0800000u; +int malloc_init(void *start, void *end) { + uint32_t mem_start = (uint32_t)(start); + uint32_t mem_end = (uint32_t)(end); malloc_first = (volatile struct memblock*)(mem_start); malloc_last = (volatile struct memblock*)(mem_end - sizeof(struct memblock));@@ -22,6 +22,8 @@ malloc_first->next = malloc_last;
malloc_last->size = 0; malloc_last->next = NULL; + + return 0; } /*!@@ -29,7 +31,7 @@ * 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) { +void* malloc(size_t size) { // size == 0 means the block is free assert(malloc_next_free->size == 0);@@ -61,7 +63,7 @@ /*!
* Frees a previously allocated block of memory. (KERNEL MODE) * \param ptr Pointer to the memory block to be freed. */ -void os_kernel_free(void* ptr) { +void free(void* ptr) { }
D
kernel/mm/memory.h
@@ -1,41 +0,0 @@
-/*! \file - * Provides functions for memory allocation. - */ -#ifndef MEMORY_H -#define MEMORY_H - -#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. - * \return Memory address to the new memory block - */ -void* os_kernel_malloc(size_t size); - -/*! - * Frees a previously allocated block of memory. (KERNEL MODE) - * \param ptr Pointer to the memory block to be freed. - */ -void os_kernel_free(void* ptr); - -/*! - * Allocates a block of \p size bytes of memory. (USER MODE) - * \param size Size in bytes of the requested block of memory. - * \return Memory address to the new memory block - */ -void* os_user_malloc(size_t size); - -/*! - * Frees a previously allocated block of memory. (USER MODE) - * \param ptr Pointer to the memory block to be freed. - */ -void os_user_free(void* ptr); - -#endif
M
kernel/sched/sched.c
→
kernel/sched/sched.c
@@ -3,7 +3,7 @@ #include "sched/process.h"
#include "sched/sched_strats.h" #include "mm/paging.h" -#include "mm/memory.h" +#include "memory.h" #include "drivers/console.h" #include "drivers/speaker.h"@@ -30,7 +30,7 @@ #define PRINT_DBG(...) {}
#endif PROCESS* get_slot(void) { - PROCESS *new_process = (PROCESS*)os_kernel_malloc(sizeof(PROCESS)); + PROCESS *new_process = (PROCESS*)malloc(sizeof(PROCESS)); return new_process; }
M
libcedos/start.c
→
libcedos/start.c
@@ -2,8 +2,12 @@ #include "assembly.h"
#include <stdint.h> +#include "memory.h" + extern void main(char *args); int _start(char *args) { + malloc_init(0x20000000, 0x30000000); + main(args); }
A
libcedos/stdlib.h
@@ -0,0 +1,35 @@
+/*! \file + * Provides functions for memory allocation. + */ +#ifndef MEMORY_H +#define MEMORY_H + +#include <stdint.h> + +typedef uint32_t size_t; + +#define NULL (void*)(0) + +/*! + * Initialize memory allocator with predefined memory region. + * \param start Start of malloc memory region. + * \param end End of malloc memory region. + * \return Currently always 0. + */ +int malloc_init(void *start, void *end); + +/*! + * Allocates a block of \p size bytes of memory. + * \param size Size in bytes of the requested block of memory. + * \return Memory address to the new memory block + */ +void* malloc(size_t size); + +/*! + * Frees a previously allocated block of memory. + * \param ptr Pointer to the memory block to be freed. + */ +void free(void* ptr); + + +#endif