summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libc/stdlib/malloc/free.c11
-rw-r--r--libc/stdlib/malloc/heap_free.c14
-rw-r--r--libc/stdlib/malloc/malloc.c7
3 files changed, 17 insertions, 15 deletions
diff --git a/libc/stdlib/malloc/free.c b/libc/stdlib/malloc/free.c
index 3074ff5e6..6de5237dc 100644
--- a/libc/stdlib/malloc/free.c
+++ b/libc/stdlib/malloc/free.c
@@ -26,6 +26,7 @@ free (void *mem)
{
size_t size;
struct heap_free_area *fa;
+ struct heap *heap = &__malloc_heap;
mem -= MALLOC_ALIGNMENT;
size = *(size_t *)mem;
@@ -36,7 +37,7 @@ free (void *mem)
__malloc_lock ();
/* Put MEM back in the heap, and get the free-area it was placed in. */
- fa = __heap_free (&__malloc_heap, mem, size);
+ fa = __heap_free (heap, mem, size);
/* See if the free-area FA has grown big enough that it should be
unmapped. */
@@ -79,7 +80,7 @@ free (void *mem)
start, end, end - start);
/* Remove FA from the heap. */
- __heap_unlink_free_area (&__malloc_heap, fa);
+ __heap_unlink_free_area (heap, fa);
if (!fa->next && !fa->prev)
/* We want to avoid the heap from losing all memory, so reserve
@@ -90,7 +91,7 @@ free (void *mem)
/* Put the reserved memory back in the heap; we asssume that
MALLOC_UNMAP_THRESHOLD is greater than MALLOC_MIN_SIZE, so
we use the latter unconditionally here. */
- __heap_free (&__malloc_heap, (void *)start, MALLOC_MIN_SIZE);
+ __heap_free (heap, (void *)start, MALLOC_MIN_SIZE);
start += MALLOC_MIN_SIZE;
}
@@ -119,13 +120,13 @@ free (void *mem)
{
if (unmap_start - start < HEAP_MIN_FREE_AREA_SIZE)
unmap_start += MALLOC_PAGE_SIZE;
- __heap_free (&__malloc_heap, (void *)start, unmap_start - start);
+ __heap_free (heap, (void *)start, unmap_start - start);
}
if (end > unmap_end)
{
if (end - unmap_end < HEAP_MIN_FREE_AREA_SIZE)
unmap_end -= MALLOC_PAGE_SIZE;
- __heap_free (&__malloc_heap, (void *)unmap_end, end - unmap_end);
+ __heap_free (heap, (void *)unmap_end, end - unmap_end);
}
/* Release the malloc lock before we do the system call. */
diff --git a/libc/stdlib/malloc/heap_free.c b/libc/stdlib/malloc/heap_free.c
index 6084b43c3..272327260 100644
--- a/libc/stdlib/malloc/heap_free.c
+++ b/libc/stdlib/malloc/heap_free.c
@@ -31,13 +31,17 @@ __heap_free (struct heap *heap, void *mem, size_t size)
size_t fa_size = fa->size;
void *fa_mem = HEAP_FREE_AREA_START (fa);
- if (end == fa_mem)
+ if (fa_mem > end)
+ /* We've reached the right spot in the free-list without finding an
+ adjacent free-area, so continue below to add a new free area. */
+ break;
+ else if (fa_mem == end)
/* FA is just after MEM, grow down to encompass it. */
{
fa_size += size;
/* See if FA can now be merged with its predecessor. */
- if (prev_fa && fa_mem - size == HEAP_FREE_AREA_END (prev_fa))
+ if (prev_fa && mem == HEAP_FREE_AREA_END (prev_fa))
/* Yup; merge PREV_FA's info into FA. */
{
fa_size += prev_fa->size;
@@ -56,7 +60,7 @@ __heap_free (struct heap *heap, void *mem, size_t size)
fa_size += size;
/* See if FA can now be merged with its successor. */
- if (next_fa && mem + size == HEAP_FREE_AREA_START (next_fa))
+ if (next_fa && end == HEAP_FREE_AREA_START (next_fa))
/* Yup; merge FA's info into NEXT_FA. */
{
fa_size += next_fa->size;
@@ -78,10 +82,6 @@ __heap_free (struct heap *heap, void *mem, size_t size)
goto done;
}
- else if (fa_mem > mem)
- /* We've reached the right spot in the free-list without finding an
- adjacent free-area, so continue below to add a new free area. */
- break;
}
/* Make MEM into a new free-list entry. */
diff --git a/libc/stdlib/malloc/malloc.c b/libc/stdlib/malloc/malloc.c
index 2ec12218c..bb1e07dfb 100644
--- a/libc/stdlib/malloc/malloc.c
+++ b/libc/stdlib/malloc/malloc.c
@@ -36,6 +36,7 @@ void *
malloc (size_t size)
{
void *mem;
+ struct heap *heap = &__malloc_heap;
MALLOC_DEBUG ("malloc: %d bytes\n", size);
@@ -44,7 +45,7 @@ malloc (size_t size)
__malloc_lock ();
- mem = __heap_alloc (&__malloc_heap, &size);
+ mem = __heap_alloc (heap, &size);
if (! mem)
/* We couldn't allocate from the heap, so get some more memory
from the system, add it to the heap, and try again. */
@@ -108,10 +109,10 @@ malloc (size_t size)
(long)block, (long)block + block_size, block_size);
/* Put BLOCK into the heap. */
- __heap_free (&__malloc_heap, block, block_size);
+ __heap_free (heap, block, block_size);
/* Try again to allocate. */
- mem = __heap_alloc (&__malloc_heap, &size);
+ mem = __heap_alloc (heap, &size);
}
}