summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas "Pixel" Noble <pixel@nobis-crew.org>2011-02-05 06:15:32 +0100
committerNicolas "Pixel" Noble <pixel@nobis-crew.org>2011-02-05 06:15:32 +0100
commitfdefa2601c46d6d3c8e6e966fe0994359f878e38 (patch)
tree238cb9fa271a0fe38b1c240ab3238b1714fbccb4
parentcf2f2a833d743aeff5281d1296ac2b3d852d61a1 (diff)
Fixing free.
-rw-r--r--libc/src/malloc.c6
1 files changed, 5 insertions, 1 deletions
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);
}