diff options
author | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2023-12-17 17:45:13 +0100 |
---|---|---|
committer | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2023-12-17 17:45:13 +0100 |
commit | 5881b4d5c1040c762599f90e091e4cc4c3abe6b1 (patch) | |
tree | 7d10f25dddcd46cd266a5f8c2a64085139651f02 /stage3/heap.c | |
parent | 2298d17186cb0e58a96d285384de431902da9b1e (diff) | |
download | cuddles-5881b4d5c1040c762599f90e091e4cc4c3abe6b1.tar.xz |
add realloc
Diffstat (limited to 'stage3/heap.c')
-rw-r--r-- | stage3/heap.c | 19 |
1 files changed, 19 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; |