diff options
-rw-r--r-- | apps/wm.c | 1 | ||||
-rw-r--r-- | kernel/features/syscall.c | 2 | ||||
-rw-r--r-- | kernel/main.c | 3 | ||||
-rw-r--r-- | libc/inc/mem.h | 14 | ||||
-rw-r--r-- | libc/mem.c | 53 |
5 files changed, 61 insertions, 12 deletions
@@ -141,7 +141,6 @@ int main(int argc, char **argv) default: printf("Unknown WM request %d from pid %d\n", msg->type, msg->src); } - yield(); }; return 0; diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c index fbbb68b..5915d9d 100644 --- a/kernel/features/syscall.c +++ b/kernel/features/syscall.c @@ -33,7 +33,7 @@ void syscall_handler(struct regs *r) } case SYS_FREE: { printf("free\n"); - free(r->ebx); + free((void *)r->ebx); break; } case SYS_READ: { diff --git a/kernel/main.c b/kernel/main.c index e97dad1..7d87be5 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -13,8 +13,7 @@ void kernel_main(struct vid_info *vid_info) { - HEAP = 0x00f00000; - HEAP_START = HEAP; // For malloc function + heap_init(0x00f00000); boot_passed = vid_info; diff --git a/libc/inc/mem.h b/libc/inc/mem.h index cb64ce8..9aeeee9 100644 --- a/libc/inc/mem.h +++ b/libc/inc/mem.h @@ -7,8 +7,13 @@ // Huh #ifdef kernel -#define malloc(n) (void *)((HEAP += n) - n) // TODO: Implement real/better malloc/free -#define free(ptr) +#define HEAP_SIZE 0x10000 +#define HEAP_MAGIC 0x42 +void heap_init(u32 start); +void *malloc(u32 size); +void free(void *ptr); +/* #define malloc(n) (void *)((HEAP += n) - n) // TODO: Implement real/better malloc/free */ +/* #define free(ptr) */ #elif defined(userspace) #include <sys.h> #define malloc(n) (void *)sys1(SYS_MALLOC, n) @@ -17,11 +22,8 @@ #error "No lib target specified. Please use -Dkernel or -Duserspace" #endif -u32 HEAP; -u32 HEAP_START; - void *memcpy(void *dest, const void *src, u32 n); -void *memset(void *dest, int c, u32 n); +void *memset(void *dest, int val, u32 n); int memcmp(const void *s1, const void *s2, u32 n); #endif @@ -1,7 +1,7 @@ // MIT License, Copyright (c) 2020 Marvin Borner #include <def.h> -#include <print.h> +#include <mem.h> #include <sys.h> // Taken from jgraef at osdev @@ -24,7 +24,7 @@ void *memcpy(void *dest, const void *src, u32 n) return dest; } -void *memset(void *dest, char val, u32 n) +void *memset(void *dest, int val, u32 n) { u32 num_dwords = n / 4; u32 num_bytes = n % 4; @@ -55,3 +55,52 @@ int memcmp(const void *s1, const void *s2, u32 n) } return 0; } + +#ifdef kernel + +static u32 *heap; +static u32 index; + +void heap_init(u32 start) +{ + heap = (u32 *)start; + for (int i = 0; i < HEAP_SIZE; i++) + heap[i] = 0; + heap[0] = HEAP_MAGIC; + index = 1; +} + +int count() +{ + int i = 0; + u32 *iterator = heap + 1; + do { + iterator += iterator[0] + 1; + i++; + } while (iterator[0] != 0); + return i; +} + +void *malloc(u32 size) +{ + if (size < 1) + return NULL; + + heap[index] = size; + index += size + 1; + return (void *)(heap + index - size); +} + +// TODO: Implement free, realloc and find_smallest_hole +void free(void *ptr) +{ + (void)ptr; +} + +void *realloc(void *ptr, u32 size) +{ + (void)size; + return ptr; +} + +#endif |