diff options
-rw-r--r-- | stage3/heap.c | 19 | ||||
-rw-r--r-- | stage3/heap.h | 1 |
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 |