summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLizzy Fleckenstein <lizzy@vlhl.dev>2023-12-17 17:45:13 +0100
committerLizzy Fleckenstein <lizzy@vlhl.dev>2023-12-17 17:45:13 +0100
commit5881b4d5c1040c762599f90e091e4cc4c3abe6b1 (patch)
tree7d10f25dddcd46cd266a5f8c2a64085139651f02
parent2298d17186cb0e58a96d285384de431902da9b1e (diff)
downloadcuddles-5881b4d5c1040c762599f90e091e4cc4c3abe6b1.tar.xz
add realloc
-rw-r--r--stage3/heap.c19
-rw-r--r--stage3/heap.h1
2 files changed, 20 insertions, 0 deletions
diff --git a/stage3/heap.c b/stage3/heap.c
index dd1b23a..0a82213 100644
--- a/stage3/heap.c
+++ b/stage3/heap.c
@@ -1,5 +1,6 @@
#include "halt.h"
#include "heap.h"
+#include "memory.h"
#define PAGESIZE 0x1000
#define MAGIC ((void *) 0x69)
@@ -74,6 +75,24 @@ void *malloc(usize size)
return nil;
}
+void *realloc(void *ptr, usize size)
+{
+ if (ptr == nil)
+ return malloc(size);
+
+ Header *h = ((Header *) ptr) - 1;
+
+ if (h->next != MAGIC)
+ panic("realloc: invalid pointer");
+
+ void *new = malloc(size);
+
+ memcpy(new, ptr, h->size);
+ free(ptr);
+
+ return new;
+}
+
void heap_init()
{
free_ptr = &init_free_ptr;
diff --git a/stage3/heap.h b/stage3/heap.h
index bf02e43..39bd70d 100644
--- a/stage3/heap.h
+++ b/stage3/heap.h
@@ -12,5 +12,6 @@ void heap_add_region(MemRegion *region);
void *try_malloc(usize size);
void *malloc(usize siz);
void free(void *ptr);
+void *realloc(void *ptr, usize size);
#endif