123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/mman.h>
- #include "malloc.h"
- #include "heap.h"
- #ifdef HEAP_USE_LOCKING
- #define free_to_heap(mem, heap, lck) __free_to_heap(mem, heap, lck)
- #else
- #define free_to_heap(mem, heap, lck) __free_to_heap(mem, heap)
- #endif
- static void
- __free_to_heap (void *mem, struct heap_free_area **heap
- #ifdef HEAP_USE_LOCKING
- , __UCLIBC_MUTEX_TYPE *heap_lock
- #endif
- )
- {
- size_t size;
- struct heap_free_area *fa;
-
- if (unlikely (! mem))
- return;
-
- MALLOC_DEBUG (1, "free: 0x%lx (base = 0x%lx, total_size = %d)",
- (long)mem, (long)MALLOC_BASE (mem), MALLOC_SIZE (mem));
- size = MALLOC_SIZE (mem);
- mem = MALLOC_BASE (mem);
- __heap_lock (heap_lock);
-
- fa = __heap_free (heap, mem, size);
-
- if (HEAP_FREE_AREA_SIZE (fa) < MALLOC_UNMAP_THRESHOLD)
-
- __heap_unlock (heap_lock);
- else
-
- {
- unsigned long start = (unsigned long)HEAP_FREE_AREA_START (fa);
- unsigned long end = (unsigned long)HEAP_FREE_AREA_END (fa);
- #ifndef MALLOC_USE_SBRK
- # ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
- struct malloc_mmb *mmb, *prev_mmb;
- unsigned long mmb_start, mmb_end;
- # else
- unsigned long unmap_start, unmap_end;
- # endif
- #endif
- #ifdef MALLOC_USE_SBRK
-
- __malloc_lock_sbrk ();
-
- if ((void *)end != sbrk (0))
- {
- MALLOC_DEBUG (-1, "not unmapping: 0x%lx - 0x%lx (%ld bytes)",
- start, end, end - start);
- __malloc_unlock_sbrk ();
- __heap_unlock (heap_lock);
- return;
- }
- #endif
- MALLOC_DEBUG (0, "unmapping: 0x%lx - 0x%lx (%ld bytes)",
- start, end, end - start);
-
- __heap_delete (heap, fa);
- if (__heap_is_empty (heap))
-
- {
-
- __heap_free (heap, (void *)start, MALLOC_MIN_SIZE);
- start += MALLOC_MIN_SIZE;
- }
- #ifdef MALLOC_USE_SBRK
-
- __heap_unlock (heap_lock);
-
- sbrk (start - end);
-
- __malloc_unlock_sbrk ();
- #else
- # ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
-
- MALLOC_MMB_DEBUG (1, "walking mmb list for region 0x%x[%d]...",
- start, end - start);
- prev_mmb = 0;
- mmb = __malloc_mmapped_blocks;
- while (mmb
- && ((mmb_end = (mmb_start = (unsigned long)mmb->mem) + mmb->size)
- <= end))
- {
- MALLOC_MMB_DEBUG (1, "considering mmb at 0x%x: 0x%x[%d]",
- (unsigned)mmb, mmb_start, mmb_end - mmb_start);
- if (mmb_start >= start
-
- && (start == mmb_start
- || mmb_start - start > HEAP_MIN_FREE_AREA_SIZE))
- {
- struct malloc_mmb *next_mmb = mmb->next;
- if (mmb_end != end && mmb_end + HEAP_MIN_FREE_AREA_SIZE > end)
-
- break;
- MALLOC_MMB_DEBUG (1, "unmapping mmb at 0x%x: 0x%x[%d]",
- (unsigned)mmb, mmb_start, mmb_end - mmb_start);
- if (mmb_start != start)
-
- {
- MALLOC_MMB_DEBUG (0, "putting intervening region back into heap: 0x%x[%d]",
- start, mmb_start - start);
- __heap_free (heap, (void *)start, mmb_start - start);
- }
- MALLOC_MMB_DEBUG_INDENT (-1);
-
- if (prev_mmb)
- prev_mmb->next = next_mmb;
- else
- __malloc_mmapped_blocks = next_mmb;
-
- start = mmb_end;
-
- free_to_heap (mmb, &__malloc_mmb_heap, &__malloc_mmb_heap_lock);
-
- __heap_unlock (heap_lock);
-
- munmap ((void *)mmb_start, mmb_end - mmb_start);
- __heap_lock (heap_lock);
- # ifdef __UCLIBC_HAS_THREADS__
-
- prev_mmb = 0;
- mmb = __malloc_mmapped_blocks;
- # else
- mmb = next_mmb;
- # endif
- }
- else
- {
- prev_mmb = mmb;
- mmb = mmb->next;
- }
- MALLOC_MMB_DEBUG_INDENT (-1);
- }
- if (start != end)
-
- {
- MALLOC_MMB_DEBUG (0, "putting tail region back into heap: 0x%x[%d]",
- start, end - start);
- __heap_free (heap, (void *)start, end - start);
- }
-
- __heap_unlock (heap_lock);
- MALLOC_MMB_DEBUG_INDENT (-1);
- # else
-
- unmap_start = MALLOC_ROUND_UP_TO_PAGE_SIZE (start);
- unmap_end = MALLOC_ROUND_DOWN_TO_PAGE_SIZE (end);
-
- if (unmap_start > start)
- {
- if (unmap_start - start < HEAP_MIN_FREE_AREA_SIZE)
- unmap_start += MALLOC_PAGE_SIZE;
- __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 (heap, (void *)unmap_end, end - unmap_end);
- }
-
- __heap_unlock (heap_lock);
- if (unmap_end > unmap_start)
-
- munmap ((void *)unmap_start, unmap_end - unmap_start);
- # endif
- #endif
- }
- MALLOC_DEBUG_INDENT (-1);
- }
- void
- free (void *mem)
- {
- free_to_heap (mem, &__malloc_heap, &__malloc_heap_lock);
- }
|