Implemented basic yet usable memory allocation

This commit is contained in:
Arnau Camprubí 2022-06-20 16:52:25 +02:00
pare 2273635a04
commit 77661a8f6f
S'han modificat 3 arxius amb 37 adicions i 0 eliminacions

21
src/kernel/lib/memory.c Normal file
Veure arxiu

@ -0,0 +1,21 @@
#include <stdbool.h>
#include <stdint.h>
#include "memory.h"
static void *g_free_memory = ALLOC_START;
void *kmalloc(size_t size){
void *ptr = g_free_memory;
g_free_memory += size;
return ptr;
}
void *kcalloc(size_t count, size_t size){
void *ptr = g_free_memory;
size *= count;
g_free_memory += size;
for(int i = 0; i < size; i++){
((uint8_t *)ptr)[i] = 0;
}
return ptr;
}

9
src/kernel/lib/memory.h Normal file
Veure arxiu

@ -0,0 +1,9 @@
#pragma once
#include <stddef.h>
#define ALLOC_START (void *)0x1000
void *kmalloc(size_t size);
// TODO: void kfree(void *ptr);
void *kcalloc(size_t count, size_t size);

Veure arxiu

@ -1,6 +1,7 @@
#include "shell.h"
#include "lib/stdio.h"
#include "lib/string.h"
#include "lib/memory.h"
int shell_run(char *cmd){
int ret = 0;
@ -12,6 +13,12 @@ int shell_run(char *cmd){
else if(!strcmp(cmd, "VERSION")){
printf("%s\n", OS_VERSION);
}
else if(!strcmp(cmd, "MALLOC")){
printf("Allocated 10 bytes:\n%x\n", kmalloc(10));
}
else if(!strcmp(cmd, "CALLOC")){
printf("Allocated and zeroed 10 chars:\n%x\n", kcalloc(10, sizeof(char)));
}
else{
printf("SHELL: Unknown command \"%s\"\n", cmd);
ret = 127;