| OLD | NEW |
| 1 #include <stdlib.h> | 1 #include <stdlib.h> |
| 2 #include <stdint.h> | 2 #include <stdint.h> |
| 3 #include <errno.h> | 3 #include <errno.h> |
| 4 #include "libc.h" | 4 #include "libc.h" |
| 5 | 5 |
| 6 /* This function should work with most dlmalloc-like chunk bookkeeping | 6 /* This function should work with most dlmalloc-like chunk bookkeeping |
| 7 * systems, but it's only guaranteed to work with the native implementation | 7 * systems, but it's only guaranteed to work with the native implementation |
| 8 * used in this library. */ | 8 * used in this library. */ |
| 9 | 9 |
| 10 void *__memalign(size_t align, size_t len) | 10 void* __memalign(size_t align, size_t len) { |
| 11 { | 11 unsigned char *mem, *new, *end; |
| 12 » unsigned char *mem, *new, *end; | 12 size_t header, footer; |
| 13 » size_t header, footer; | |
| 14 | 13 |
| 15 » if ((align & -align) != align) { | 14 if ((align & -align) != align) { |
| 16 » » errno = EINVAL; | 15 errno = EINVAL; |
| 17 » » return NULL; | 16 return NULL; |
| 18 » } | 17 } |
| 19 | 18 |
| 20 » if (len > SIZE_MAX - align) { | 19 if (len > SIZE_MAX - align) { |
| 21 » » errno = ENOMEM; | 20 errno = ENOMEM; |
| 22 » » return NULL; | 21 return NULL; |
| 23 » } | 22 } |
| 24 | 23 |
| 25 » if (align <= 4*sizeof(size_t)) { | 24 if (align <= 4 * sizeof(size_t)) { |
| 26 » » if (!(mem = malloc(len))) | 25 if (!(mem = malloc(len))) |
| 27 » » » return NULL; | 26 return NULL; |
| 28 » » return mem; | 27 return mem; |
| 29 » } | 28 } |
| 30 | 29 |
| 31 » if (!(mem = malloc(len + align-1))) | 30 if (!(mem = malloc(len + align - 1))) |
| 32 » » return NULL; | 31 return NULL; |
| 33 | 32 |
| 34 » new = (void *)((uintptr_t)mem + align-1 & -align); | 33 new = (void*)((uintptr_t)mem + align - 1 & -align); |
| 35 » if (new == mem) return mem; | 34 if (new == mem) |
| 35 return mem; |
| 36 | 36 |
| 37 » header = ((size_t *)mem)[-1]; | 37 header = ((size_t*)mem)[-1]; |
| 38 | 38 |
| 39 » if (!(header & 7)) { | 39 if (!(header & 7)) { |
| 40 » » ((size_t *)new)[-2] = ((size_t *)mem)[-2] + (new-mem); | 40 ((size_t*)new)[-2] = ((size_t*)mem)[-2] + (new - mem); |
| 41 » » ((size_t *)new)[-1] = ((size_t *)mem)[-1] - (new-mem); | 41 ((size_t*)new)[-1] = ((size_t*)mem)[-1] - (new - mem); |
| 42 » » return new; | 42 return new; |
| 43 » } | 43 } |
| 44 | 44 |
| 45 » end = mem + (header & -8); | 45 end = mem + (header & -8); |
| 46 » footer = ((size_t *)end)[-2]; | 46 footer = ((size_t*)end)[-2]; |
| 47 | 47 |
| 48 » ((size_t *)mem)[-1] = (header&7) | new-mem; | 48 ((size_t*)mem)[-1] = (header & 7) | new - mem; |
| 49 » ((size_t *)new)[-2] = (footer&7) | new-mem; | 49 ((size_t*)new)[-2] = (footer & 7) | new - mem; |
| 50 » ((size_t *)new)[-1] = (header&7) | end-new; | 50 ((size_t*)new)[-1] = (header & 7) | end - new; |
| 51 » ((size_t *)end)[-2] = (footer&7) | end-new; | 51 ((size_t*)end)[-2] = (footer & 7) | end - new; |
| 52 | 52 |
| 53 » free(mem); | 53 free(mem); |
| 54 » return new; | 54 return new; |
| 55 } | 55 } |
| 56 | 56 |
| 57 weak_alias(__memalign, memalign); | 57 weak_alias(__memalign, memalign); |
| OLD | NEW |