réplica de
https://github.com/Arnau478/quark.git
synced 2024-11-23 21:08:07 +01:00
Implemented basic yet usable memory allocation
This commit is contained in:
pare
2273635a04
commit
77661a8f6f
S'han modificat 3 arxius amb 37 adicions i 0 eliminacions
21
src/kernel/lib/memory.c
Normal file
21
src/kernel/lib/memory.c
Normal file
|
@ -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
9
src/kernel/lib/memory.h
Normal file
|
@ -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);
|
|
@ -1,6 +1,7 @@
|
||||||
#include "shell.h"
|
#include "shell.h"
|
||||||
#include "lib/stdio.h"
|
#include "lib/stdio.h"
|
||||||
#include "lib/string.h"
|
#include "lib/string.h"
|
||||||
|
#include "lib/memory.h"
|
||||||
|
|
||||||
int shell_run(char *cmd){
|
int shell_run(char *cmd){
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
@ -12,6 +13,12 @@ int shell_run(char *cmd){
|
||||||
else if(!strcmp(cmd, "VERSION")){
|
else if(!strcmp(cmd, "VERSION")){
|
||||||
printf("%s\n", OS_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{
|
else{
|
||||||
printf("SHELL: Unknown command \"%s\"\n", cmd);
|
printf("SHELL: Unknown command \"%s\"\n", cmd);
|
||||||
ret = 127;
|
ret = 127;
|
||||||
|
|
Loading…
Referencia en una nova incidència