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