From 719e37dd5a09b9df6172c51fa733257eb86b4a12 Mon Sep 17 00:00:00 2001 From: spikey Date: Sat, 17 Feb 2024 19:20:00 -0500 Subject: [PATCH] added mem --- sys/include/memory.h | 26 ++++++++++++++++++++++++++ sys/include/types.h | 14 ++++++++------ sys/memory.c | 24 ++++++++++++++++++++---- 3 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 sys/include/memory.h diff --git a/sys/include/memory.h b/sys/include/memory.h new file mode 100644 index 0000000..88a31a7 --- /dev/null +++ b/sys/include/memory.h @@ -0,0 +1,26 @@ +#ifndef MEMORY_H +#define MEMORY_H + +#include + + +/* + * Each allocated memory is stored internally like this: + * nnnnssss(u)+ + * n represents the address to the next string + * s represents the size of the allocated memory + * c + * u represents the actual string + * + * A struct cannot be plainly used because we would have to store a reference + * so this complicated structure is utilized instead. + */ + +#define START 0xb80f1 +#define USABLE(m) m + sizeof(s32) + sizeof(void *) +#define NEXT(m) (void *) *m +#define SIZE(m) (s32) m[sizeof(void *)] + +void *start = 0x0 ; + +#endif diff --git a/sys/include/types.h b/sys/include/types.h index 0c16911..529220f 100644 --- a/sys/include/types.h +++ b/sys/include/types.h @@ -1,11 +1,13 @@ #ifndef TYPES_H #define TYPES_H -typedef unsigned int u32; -typedef unsigned short u16; -typedef unsigned char u8; -typedef int s32; -typedef short s16; -typedef char s8; +#define NULL 0x0 + +typedef unsigned int u32; +typedef unsigned short u16; +typedef unsigned char u8; +typedef int s32; +typedef short s16; +typedef char s8; #endif diff --git a/sys/memory.c b/sys/memory.c index a769dea..be51074 100644 --- a/sys/memory.c +++ b/sys/memory.c @@ -1,6 +1,22 @@ #include -struct linkedlist { - u32 node; - linkedlist *next; -}; +void * +malloc(s32 size) +{ + s32 actual_size = size + sizeof(s32) + sizeof(void *); + + if (start == NULL) { + start = (void *) START; + + start[0] = '\0'; + start[1] = '\0'; + start[2] = '\0'; + start[3] = '\0'; + + SIZE(m) = size; + + return USABLE(m); + } + + /* TODO */ +}