blob: f3ad0320b05f2ce2321a3d6170cfa1827a026599 [file] [log] [blame]
swissChili8cfb7c42021-04-18 21:17:58 -07001#include "plat.h"
swissChilia7568dc2021-08-08 16:52:52 -07002#include "../error.h"
swissChilib51552c2021-08-03 10:23:37 -07003#include <stdio.h>
swissChili8cfb7c42021-04-18 21:17:58 -07004#include <stdlib.h>
5#include <string.h>
swissChilif3e7f182021-04-20 13:57:22 -07006#include <sys/mman.h>
swissChilif68671f2021-07-05 14:14:44 -07007#include <unistd.h>
swissChili4691cf62021-08-05 21:34:57 -07008#ifndef NO_READLINE
swissChili7e1393c2021-07-07 12:59:12 -07009#include <readline/readline.h>
10#include <readline/history.h>
swissChili4691cf62021-08-05 21:34:57 -070011#endif
swissChili8cfb7c42021-04-18 21:17:58 -070012
swissChili53472e82021-05-08 16:06:32 -070013void *malloc_aligned(size_t size)
swissChili8cfb7c42021-04-18 21:17:58 -070014{
swissChili708d4c42021-07-04 17:40:07 -070015 // https://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html
16 // On glibc malloc() and realloc() return 8-byte aligned addresses.
17 return malloc(size);
swissChili8cfb7c42021-04-18 21:17:58 -070018}
19
swissChili53472e82021-05-08 16:06:32 -070020void *realloc_aligned(void *addr, size_t size)
swissChili8cfb7c42021-04-18 21:17:58 -070021{
swissChili708d4c42021-07-04 17:40:07 -070022 return realloc(addr, size);
swissChili8cfb7c42021-04-18 21:17:58 -070023}
24
swissChili53472e82021-05-08 16:06:32 -070025void free_aligned(void *addr)
swissChili8cfb7c42021-04-18 21:17:58 -070026{
swissChili708d4c42021-07-04 17:40:07 -070027 free(addr);
swissChili8cfb7c42021-04-18 21:17:58 -070028}
swissChilif3e7f182021-04-20 13:57:22 -070029
swissChilif68671f2021-07-05 14:14:44 -070030void *link_program(dasm_State **Dst)
swissChilif3e7f182021-04-20 13:57:22 -070031{
32 size_t size;
33 void *buf;
34
swissChili53472e82021-05-08 16:06:32 -070035 dasm_link(Dst, &size);
36 buf = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1,
37 0);
38 dasm_encode(Dst, buf);
swissChilif3e7f182021-04-20 13:57:22 -070039
swissChili53472e82021-05-08 16:06:32 -070040 mprotect(buf, size, PROT_READ | PROT_EXEC);
swissChilif3e7f182021-04-20 13:57:22 -070041 return buf;
42}
swissChilif68671f2021-07-05 14:14:44 -070043
44bool file_exists(const char *path)
45{
46 return access(path, F_OK) == 0;
47}
swissChili7e1393c2021-07-07 12:59:12 -070048
49char *read_input_line(char *prompt)
50{
swissChili4691cf62021-08-05 21:34:57 -070051#ifndef NO_READLINE
swissChili7e1393c2021-07-07 12:59:12 -070052 return readline(prompt);
swissChili4691cf62021-08-05 21:34:57 -070053#else
swissChilia890aed2022-07-30 17:13:07 -070054 fprintf(stdout, "%s", prompt);
55 fflush(stdout);
56 char *buffer = malloc(512);
57 fgets(buffer, 512, stdin);
58
59 return buffer;
swissChili4691cf62021-08-05 21:34:57 -070060#endif
swissChili7e1393c2021-07-07 12:59:12 -070061}
62
63void add_line_to_history(char *line)
64{
swissChili4691cf62021-08-05 21:34:57 -070065#ifndef NO_READLINE
swissChili7e1393c2021-07-07 12:59:12 -070066 add_history(line);
swissChilia7568dc2021-08-08 16:52:52 -070067#else
68 UNUSED(line);
swissChili4691cf62021-08-05 21:34:57 -070069#endif
swissChili7e1393c2021-07-07 12:59:12 -070070}