From fdefa2601c46d6d3c8e6e966fe0994359f878e38 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Sat, 5 Feb 2011 06:15:32 +0100 Subject: Fixing free. --- libc/src/malloc.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'libc') diff --git a/libc/src/malloc.c b/libc/src/malloc.c index 95181ac..2b8a806 100644 --- a/libc/src/malloc.c +++ b/libc/src/malloc.c @@ -163,6 +163,7 @@ void free(void * ptr) { if (!ptr || !head) return; + // First block; bumping head ahead. if (ptr == head->ptr) { size = head->size + (size_t) (head->ptr - (void *) head); head = head->next; @@ -177,17 +178,20 @@ void free(void * ptr) { return; } + // Finding the proper block cur = head; for (cur = head; ptr != cur->ptr; cur = cur->next) if (!cur->next) return; if (cur->next) { + // In the middle, just unlink it cur->next->prev = cur->prev; } else { + // At the end, shrink heap tail = cur->prev; top = sbrk(0); - size = (uintptr_t) top - (uintptr_t) cur->prev->ptr + cur->prev->size; + size = (top - cur->prev->ptr) - cur->prev->size; sbrk(-size); } -- cgit v1.2.3