blob: b71f342843dd0b534d6c129a0b4d650f821a9916 [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>
swissChilif68671f2021-07-05 14:14:44 -07005#include <unistd.h>
swissChili8cfb7c42021-04-18 21:17:58 -07006
swissChili53472e82021-05-08 16:06:32 -07007void *malloc_aligned(size_t size)
swissChili8cfb7c42021-04-18 21:17:58 -07008{
swissChili708d4c42021-07-04 17:40:07 -07009 // https://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html
10 // On glibc malloc() and realloc() return 8-byte aligned addresses.
11 return malloc(size);
swissChili8cfb7c42021-04-18 21:17:58 -070012}
13
swissChili53472e82021-05-08 16:06:32 -070014void *realloc_aligned(void *addr, size_t size)
swissChili8cfb7c42021-04-18 21:17:58 -070015{
swissChili708d4c42021-07-04 17:40:07 -070016 return realloc(addr, size);
swissChili8cfb7c42021-04-18 21:17:58 -070017}
18
swissChili53472e82021-05-08 16:06:32 -070019void free_aligned(void *addr)
swissChili8cfb7c42021-04-18 21:17:58 -070020{
swissChili708d4c42021-07-04 17:40:07 -070021 free(addr);
swissChili8cfb7c42021-04-18 21:17:58 -070022}
swissChilif3e7f182021-04-20 13:57:22 -070023
swissChilif68671f2021-07-05 14:14:44 -070024void *link_program(dasm_State **Dst)
swissChilif3e7f182021-04-20 13:57:22 -070025{
26 size_t size;
27 void *buf;
28
swissChili53472e82021-05-08 16:06:32 -070029 dasm_link(Dst, &size);
30 buf = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1,
31 0);
32 dasm_encode(Dst, buf);
swissChilif3e7f182021-04-20 13:57:22 -070033
swissChili53472e82021-05-08 16:06:32 -070034 mprotect(buf, size, PROT_READ | PROT_EXEC);
swissChilif3e7f182021-04-20 13:57:22 -070035 return buf;
36}
swissChilif68671f2021-07-05 14:14:44 -070037
38bool file_exists(const char *path)
39{
40 return access(path, F_OK) == 0;
41}