123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <sys/mman.h>
- #include "malloc.h"
- #include "heap.h"
- HEAP_DECLARE_STATIC_FREE_AREA (initial_fa, 256);
- struct heap_free_area *__malloc_heap = HEAP_INIT_WITH_FA (initial_fa);
- #ifdef HEAP_USE_LOCKING
- __UCLIBC_MUTEX_INIT(__malloc_heap_lock,PTHREAD_MUTEX_INITIALIZER);
- #endif
- #if defined(MALLOC_USE_LOCKING) && defined(MALLOC_USE_SBRK)
- __UCLIBC_MUTEX(__malloc_sbrk_lock);
- #endif
- #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
- struct malloc_mmb *__malloc_mmapped_blocks = 0;
- HEAP_DECLARE_STATIC_FREE_AREA (initial_mmb_fa, 48);
- struct heap_free_area *__malloc_mmb_heap = HEAP_INIT_WITH_FA (initial_mmb_fa);
- #ifdef HEAP_USE_LOCKING
- __UCLIBC_MUTEX_INIT(__malloc_mmb_heap_lock,PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
- #endif
- #endif
- #ifdef HEAP_USE_LOCKING
- #define malloc_from_heap(size, heap, lck) __malloc_from_heap(size, heap, lck)
- #else
- #define malloc_from_heap(size, heap, lck) __malloc_from_heap(size, heap)
- #endif
- static void *
- __malloc_from_heap (size_t size, struct heap_free_area **heap
- #ifdef HEAP_USE_LOCKING
- , __UCLIBC_MUTEX_TYPE *heap_lock
- #endif
- )
- {
- void *mem;
- MALLOC_DEBUG (1, "malloc: %d bytes", size);
-
- size += MALLOC_HEADER_SIZE;
- __heap_lock (heap_lock);
-
- mem = __heap_alloc (heap, &size);
- __heap_unlock (heap_lock);
- if (unlikely (! mem))
-
- {
-
- void *block;
- size_t block_size
- = (size < MALLOC_HEAP_EXTEND_SIZE
- ? MALLOC_HEAP_EXTEND_SIZE
- : MALLOC_ROUND_UP_TO_PAGE_SIZE (size));
-
- #ifdef MALLOC_USE_SBRK
- __malloc_lock_sbrk ();
-
- block = sbrk (block_size);
- if (likely (block != (void *)-1))
- {
-
- long aligned_block = MALLOC_ROUND_UP ((long)block, MALLOC_ALIGNMENT);
- if (block != (void *)aligned_block)
-
- {
-
- sbrk (aligned_block - (long)block);
- block = (void *)aligned_block;
- }
- }
- __malloc_unlock_sbrk ();
- #else
-
- #ifdef __ARCH_USE_MMU__
- block = mmap ((void *)0, block_size, PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
- #else
- block = mmap ((void *)0, block_size, PROT_READ | PROT_WRITE,
- MAP_SHARED | MAP_ANONYMOUS | MAP_UNINITIALIZED, 0, 0);
- #endif
- #endif
- if (likely (block != (void *)-1))
- {
- #if !defined(MALLOC_USE_SBRK) && defined(__UCLIBC_UCLINUX_BROKEN_MUNMAP__)
- struct malloc_mmb *mmb, *prev_mmb, *new_mmb;
- #endif
- MALLOC_DEBUG (1, "adding system memory to heap: 0x%lx - 0x%lx (%d bytes)",
- (long)block, (long)block + block_size, block_size);
-
- __heap_lock (heap_lock);
-
- __heap_free (heap, block, block_size);
- MALLOC_DEBUG_INDENT (-1);
-
- mem = __heap_alloc (heap, &size);
- #if !defined(MALLOC_USE_SBRK) && defined(__UCLIBC_UCLINUX_BROKEN_MUNMAP__)
-
- new_mmb = malloc_from_heap (sizeof *new_mmb, &__malloc_mmb_heap, &__malloc_mmb_heap_lock);
- for (prev_mmb = 0, mmb = __malloc_mmapped_blocks;
- mmb;
- prev_mmb = mmb, mmb = mmb->next)
- if (block < mmb->mem)
- break;
- new_mmb->next = mmb;
- new_mmb->mem = block;
- new_mmb->size = block_size;
- if (prev_mmb)
- prev_mmb->next = new_mmb;
- else
- __malloc_mmapped_blocks = new_mmb;
- MALLOC_MMB_DEBUG (0, "new mmb at 0x%x: 0x%x[%d]",
- (unsigned)new_mmb,
- (unsigned)new_mmb->mem, block_size);
- #endif
- __heap_unlock (heap_lock);
- }
- }
- if (likely (mem))
-
- {
- mem = MALLOC_SETUP (mem, size);
- MALLOC_DEBUG (-1, "malloc: returning 0x%lx (base:0x%lx, total_size:%ld)",
- (long)mem, (long)MALLOC_BASE(mem), (long)MALLOC_SIZE(mem));
- }
- else
- MALLOC_DEBUG (-1, "malloc: returning 0");
- return mem;
- }
- void *
- malloc (size_t size)
- {
- void *mem;
- #ifdef MALLOC_DEBUGGING
- static smallint debugging_initialized;
- if (! debugging_initialized)
- {
- debugging_initialized = 1;
- __malloc_debug_init ();
- }
- if (__malloc_check)
- __heap_check (__malloc_heap, "malloc");
- #endif
- if (unlikely (size == 0))
- size++;
-
- if (unlikely(((unsigned long)size > (unsigned long)(MALLOC_HEADER_SIZE*-2))))
- goto oom;
- mem = malloc_from_heap (size, &__malloc_heap, &__malloc_heap_lock);
- if (unlikely (!mem))
- {
- oom:
- __set_errno (ENOMEM);
- return 0;
- }
- return mem;
- }
|