swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 1 | #include "plat.h" |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 4 | #include <sys/mman.h> |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 5 | |
| 6 | void *malloc_aligned (size_t size) |
| 7 | { |
| 8 | void *mem = malloc (size + 8 + sizeof (void *) * 2); |
| 9 | void **aligned_ptr = (void **) ((uintptr_t) (mem + 8 + sizeof (void *)) & ~7); |
| 10 | aligned_ptr[ -1 ] = mem; |
| 11 | aligned_ptr[ -2 ] = (void *) size; |
| 12 | return aligned_ptr; |
| 13 | } |
| 14 | |
| 15 | void *realloc_aligned (void *addr, size_t size) |
| 16 | { |
| 17 | void *mem = malloc (size + 8 + sizeof (void *) * 2); |
| 18 | void **aligned_ptr = (void **) ((uintptr_t) (mem + 8 + sizeof (void *)) & ~7); |
| 19 | aligned_ptr[ -1 ] = mem; |
| 20 | |
| 21 | memcpy (aligned_ptr, addr, (uintptr_t) aligned_ptr[ -2 ]); |
| 22 | |
| 23 | return aligned_ptr; |
| 24 | } |
| 25 | |
| 26 | void free_aligned (void *addr) |
| 27 | { |
| 28 | void **ptr = (void **)addr; |
| 29 | free (ptr[ -1 ]); |
| 30 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 31 | |
| 32 | void *link (dasm_State **Dst) |
| 33 | { |
| 34 | size_t size; |
| 35 | void *buf; |
| 36 | |
| 37 | dasm_link (Dst, &size); |
| 38 | buf = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 39 | dasm_encode (Dst, buf); |
| 40 | |
| 41 | mprotect (buf, size, PROT_READ | PROT_EXEC); |
| 42 | return buf; |
| 43 | } |