diff options
Diffstat (limited to 'libexec/rtld-elf/xmalloc.c')
-rw-r--r-- | libexec/rtld-elf/xmalloc.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/libexec/rtld-elf/xmalloc.c b/libexec/rtld-elf/xmalloc.c index 178f49b..3b3408c 100644 --- a/libexec/rtld-elf/xmalloc.c +++ b/libexec/rtld-elf/xmalloc.c @@ -67,3 +67,33 @@ xstrdup(const char *str) memcpy(copy, str, len); return (copy); } + +void * +malloc_aligned(size_t size, size_t align) +{ + void *mem, *res; + uintptr_t x; + size_t asize, r; + + r = round(sizeof(void *), align); + asize = round(size, align) + r; + mem = xmalloc(asize); + x = (uintptr_t)mem; + res = (void *)round(x, align); + *(void **)((uintptr_t)res - sizeof(void *)) = mem; + return (res); +} + +void +free_aligned(void *ptr) +{ + void *mem; + uintptr_t x; + + if (ptr == NULL) + return; + x = (uintptr_t)ptr; + x -= sizeof(void *); + mem = *(void **)x; + free(mem); +} |