swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 1 | #include "plat.h" |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | void *malloc_aligned (size_t size) |
| 6 | { |
| 7 | void *mem = malloc (size + 8 + sizeof (void *) * 2); |
| 8 | void **aligned_ptr = (void **) ((uintptr_t) (mem + 8 + sizeof (void *)) & ~7); |
| 9 | aligned_ptr[ -1 ] = mem; |
| 10 | aligned_ptr[ -2 ] = (void *) size; |
| 11 | return aligned_ptr; |
| 12 | } |
| 13 | |
| 14 | void *realloc_aligned (void *addr, size_t size) |
| 15 | { |
| 16 | void *mem = malloc (size + 8 + sizeof (void *) * 2); |
| 17 | void **aligned_ptr = (void **) ((uintptr_t) (mem + 8 + sizeof (void *)) & ~7); |
| 18 | aligned_ptr[ -1 ] = mem; |
| 19 | |
| 20 | memcpy (aligned_ptr, addr, (uintptr_t) aligned_ptr[ -2 ]); |
| 21 | |
| 22 | return aligned_ptr; |
| 23 | } |
| 24 | |
| 25 | void free_aligned (void *addr) |
| 26 | { |
| 27 | void **ptr = (void **)addr; |
| 28 | free (ptr[ -1 ]); |
| 29 | } |