12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/mman.h>
- #include "malloc.h"
- #include "heap.h"
- #define MAX(x,y) ((x) > (y) ? (x) : (y))
- void *
- memalign (size_t alignment, size_t size)
- {
- void *mem, *base;
- unsigned long tot_addr, tot_end_addr, addr, end_addr;
- struct heap *heap = &__malloc_heap;
-
- 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_free (heap, base, init_size);
-
- base += init_size;
- }
-
- end_addr = addr + size;
- if (end_addr + MALLOC_REALLOC_MIN_FREE_SIZE < tot_end_addr)
- __heap_free (heap, (void *)end_addr, tot_end_addr - end_addr);
- else
-
- end_addr = tot_end_addr;
- return MALLOC_SETUP (base, end_addr - (unsigned long)base);
- }
|