aboutsummaryrefslogtreecommitdiff
path: root/libc/mem.c
diff options
context:
space:
mode:
Diffstat (limited to 'libc/mem.c')
-rw-r--r--libc/mem.c53
1 files changed, 51 insertions, 2 deletions
diff --git a/libc/mem.c b/libc/mem.c
index 4b7981f..4744a8b 100644
--- a/libc/mem.c
+++ b/libc/mem.c
@@ -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