123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include <errno.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/mman.h>
- #include "malloc.h"
- #include "heap.h"
- void *memalign (size_t alignment, size_t size);
- libc_hidden_proto(memalign)
- void *
- memalign (size_t alignment, size_t size)
- {
- void *mem, *base;
- unsigned long tot_addr, tot_end_addr, addr, end_addr;
- struct heap_free_area **heap = &__malloc_heap;
- if (unlikely(size > PTRDIFF_MAX)) {
- __set_errno(ENOMEM);
- return NULL;
- }
-
- size = HEAP_ADJUST_SIZE (size);
-
- mem = malloc (size + 2 * alignment);
- if (! mem)
-
- return 0;
- if (alignment < MALLOC_ALIGNMENT)
- return mem;
-
- base = MALLOC_BASE (mem);
-
- tot_addr = (unsigned long)mem;
- tot_end_addr = (unsigned long)base + MALLOC_SIZE (mem);
-
- addr = MALLOC_ROUND_UP (tot_addr, alignment);
-
- if (addr != tot_addr)
- {
- size_t init_size = addr - tot_addr;
-
- if (init_size < HEAP_MIN_SIZE)
- {
- addr = MALLOC_ROUND_UP (tot_addr + HEAP_MIN_SIZE, alignment);
- init_size = addr - tot_addr;
- }
- __heap_lock (&__malloc_heap_lock);
- __heap_free (heap, base, init_size);
- __heap_unlock (&__malloc_heap_lock);
-
- base += init_size;
- }
-
- end_addr = addr + size;
- if (end_addr + MALLOC_REALLOC_MIN_FREE_SIZE < tot_end_addr) {
- __heap_lock (&__malloc_heap_lock);
- __heap_free (heap, (void *)end_addr, tot_end_addr - end_addr);
- __heap_unlock (&__malloc_heap_lock);
- } else
-
- end_addr = tot_end_addr;
- return MALLOC_SETUP (base, end_addr - (unsigned long)base);
- }
- weak_alias(memalign, aligned_alloc)
- libc_hidden_def(memalign)
|