|
@@ -16,30 +16,34 @@
|
|
|
#include "heap.h"
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
struct heap_free_area *
|
|
|
__heap_free (struct heap *heap, void *mem, size_t size)
|
|
|
{
|
|
|
- struct heap_free_area *prev_fa, *fa;
|
|
|
+ struct heap_free_area *fa, *prev_fa;
|
|
|
void *end = (char *)mem + size;
|
|
|
|
|
|
HEAP_DEBUG (heap, "before __heap_free");
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+ This is the most speed critical loop in this malloc implementation:
|
|
|
+ since we use a simple linked-list for the free-list, and we keep it in
|
|
|
+ address-sorted order, it can become very expensive to insert something
|
|
|
+ in the free-list when it becomes fragmented and long. [A better
|
|
|
+ implemention would use a balanced tree or something for the free-list,
|
|
|
+ though that bloats the code-size and complexity quite a bit.] */
|
|
|
for (prev_fa = 0, fa = heap->free_areas; fa; prev_fa = fa, fa = fa->next)
|
|
|
+ if (unlikely (HEAP_FREE_AREA_END (fa) >= mem))
|
|
|
+ break;
|
|
|
+
|
|
|
+ if (fa && HEAP_FREE_AREA_START (fa) <= end)
|
|
|
+
|
|
|
{
|
|
|
- size_t fa_size = fa->size;
|
|
|
- void *fa_mem = HEAP_FREE_AREA_START (fa);
|
|
|
+ size_t fa_size = fa->size + size;
|
|
|
|
|
|
- if (fa_mem > end)
|
|
|
-
|
|
|
- adjacent free-area, so continue below to add a new free area. */
|
|
|
- break;
|
|
|
- else if (fa_mem == end)
|
|
|
-
|
|
|
+ if (HEAP_FREE_AREA_START (fa) == end)
|
|
|
+
|
|
|
{
|
|
|
- fa_size += size;
|
|
|
-
|
|
|
|
|
|
if (prev_fa && mem == HEAP_FREE_AREA_END (prev_fa))
|
|
|
|
|
@@ -47,18 +51,12 @@ __heap_free (struct heap *heap, void *mem, size_t size)
|
|
|
fa_size += prev_fa->size;
|
|
|
__heap_link_free_area_after (heap, fa, prev_fa->prev);
|
|
|
}
|
|
|
-
|
|
|
- fa->size = fa_size;
|
|
|
-
|
|
|
- goto done;
|
|
|
}
|
|
|
- else if (HEAP_FREE_AREA_END (fa) == mem)
|
|
|
-
|
|
|
+ else
|
|
|
+
|
|
|
{
|
|
|
struct heap_free_area *next_fa = fa->next;
|
|
|
|
|
|
- fa_size += size;
|
|
|
-
|
|
|
|
|
|
if (next_fa && end == HEAP_FREE_AREA_START (next_fa))
|
|
|
|
|
@@ -77,17 +75,14 @@ __heap_free (struct heap *heap, void *mem, size_t size)
|
|
|
|
|
|
__heap_link_free_area (heap, fa, prev_fa, next_fa);
|
|
|
}
|
|
|
-
|
|
|
- fa->size = fa_size;
|
|
|
-
|
|
|
- goto done;
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
-
|
|
|
- fa = __heap_add_free_area (heap, mem, size, prev_fa, fa);
|
|
|
+ fa->size = fa_size;
|
|
|
+ }
|
|
|
+ else
|
|
|
+
|
|
|
+ fa = __heap_add_free_area (heap, mem, size, prev_fa, fa);
|
|
|
|
|
|
- done:
|
|
|
HEAP_DEBUG (heap, "after __heap_free");
|
|
|
|
|
|
return fa;
|