swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 1 | #include "plat.h" |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 2 | #include <stdio.h> |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 5 | #include <sys/mman.h> |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 6 | #include <unistd.h> |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 7 | #include <readline/readline.h> |
| 8 | #include <readline/history.h> |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 9 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 10 | void *malloc_aligned(size_t size) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 11 | { |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 12 | // https://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html |
| 13 | // On glibc malloc() and realloc() return 8-byte aligned addresses. |
| 14 | return malloc(size); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 15 | } |
| 16 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 17 | void *realloc_aligned(void *addr, size_t size) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 18 | { |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 19 | return realloc(addr, size); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 20 | } |
| 21 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 22 | void free_aligned(void *addr) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 23 | { |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 24 | free(addr); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 25 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 26 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 27 | void *link_program(dasm_State **Dst) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 28 | { |
| 29 | size_t size; |
| 30 | void *buf; |
| 31 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 32 | dasm_link(Dst, &size); |
| 33 | buf = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, |
| 34 | 0); |
| 35 | dasm_encode(Dst, buf); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 36 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 37 | mprotect(buf, size, PROT_READ | PROT_EXEC); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 38 | return buf; |
| 39 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 40 | |
| 41 | bool file_exists(const char *path) |
| 42 | { |
| 43 | return access(path, F_OK) == 0; |
| 44 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 45 | |
| 46 | char *read_input_line(char *prompt) |
| 47 | { |
| 48 | return readline(prompt); |
| 49 | } |
| 50 | |
| 51 | void add_line_to_history(char *line) |
| 52 | { |
| 53 | add_history(line); |
| 54 | } |