1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- #include <stdlib.h>
- #include "heap.h"
- struct heap_free_area *
- __heap_free (struct heap_free_area **heap, void *mem, size_t size)
- {
- struct heap_free_area *fa, *prev_fa;
- void *end = (char *)mem + size;
- HEAP_DEBUG (*heap, "before __heap_free");
-
- for (prev_fa = 0, fa = *heap; 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 + size;
- if (HEAP_FREE_AREA_START (fa) == end)
-
- {
-
- if (prev_fa && mem == HEAP_FREE_AREA_END (prev_fa))
-
- {
- fa_size += prev_fa->size;
- __heap_link_free_area_after (heap, fa, prev_fa->prev);
- }
- }
- else
-
- {
- struct heap_free_area *next_fa = fa->next;
-
- if (next_fa && end == HEAP_FREE_AREA_START (next_fa))
-
- {
- fa_size += next_fa->size;
- __heap_link_free_area_after (heap, next_fa, prev_fa);
- fa = next_fa;
- }
- else
-
- {
-
- fa = (struct heap_free_area *)((char *)fa + size);
-
- __heap_link_free_area (heap, fa, prev_fa, next_fa);
- }
- }
- fa->size = fa_size;
- }
- else
-
- fa = __heap_add_free_area (heap, mem, size, prev_fa, fa);
- HEAP_DEBUG (*heap, "after __heap_free");
- return fa;
- }
|