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