blob: bd9af102c33c4c6f3ec310cb1269c3b211b538d7 [file] [log] [blame]
swissChili8cfb7c42021-04-18 21:17:58 -07001#include "plat.h"
2#include <stdlib.h>
3#include <string.h>
swissChilif3e7f182021-04-20 13:57:22 -07004#include <sys/mman.h>
swissChili8cfb7c42021-04-18 21:17:58 -07005
6void *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
15void *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
26void free_aligned (void *addr)
27{
28 void **ptr = (void **)addr;
29 free (ptr[ -1 ]);
30}
swissChilif3e7f182021-04-20 13:57:22 -070031
32void *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}