6 /* This function should work with most dlmalloc-like chunk bookkeeping
7 * systems, but it's only guaranteed to work with the native implementation
8 * used in this library. */
10 void *__memalign(size_t align, size_t len)
12 unsigned char *mem, *new, *end;
13 size_t header, footer;
15 if ((align & -align) != align) {
20 if (len > SIZE_MAX - align) {
25 if (align <= 4*sizeof(size_t)) {
26 if (!(mem = malloc(len)))
31 if (!(mem = malloc(len + align-1)))
34 new = (void *)((uintptr_t)mem + align-1 & -align);
35 if (new == mem) return mem;
37 header = ((size_t *)mem)[-1];
40 ((size_t *)new)[-2] = ((size_t *)mem)[-2] + (new-mem);
41 ((size_t *)new)[-1] = ((size_t *)mem)[-1] - (new-mem);
45 end = mem + (header & -8);
46 footer = ((size_t *)end)[-2];
48 ((size_t *)mem)[-1] = header&7 | new-mem;
49 ((size_t *)new)[-2] = footer&7 | new-mem;
50 ((size_t *)new)[-1] = header&7 | end-new;
51 ((size_t *)end)[-2] = footer&7 | end-new;
57 weak_alias(__memalign, memalign);