blob: b24b3f766d2f76a468a6d0740992e8c62f525a21 [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>
swissChili7e1393c2021-07-07 12:59:12 -07006#include <readline/readline.h>
7#include <readline/history.h>
swissChili8cfb7c42021-04-18 21:17:58 -07008
swissChili53472e82021-05-08 16:06:32 -07009void *malloc_aligned(size_t size)
swissChili8cfb7c42021-04-18 21:17:58 -070010{
swissChili708d4c42021-07-04 17:40:07 -070011 // 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);
swissChili8cfb7c42021-04-18 21:17:58 -070014}
15
swissChili53472e82021-05-08 16:06:32 -070016void *realloc_aligned(void *addr, size_t size)
swissChili8cfb7c42021-04-18 21:17:58 -070017{
swissChili708d4c42021-07-04 17:40:07 -070018 return realloc(addr, size);
swissChili8cfb7c42021-04-18 21:17:58 -070019}
20
swissChili53472e82021-05-08 16:06:32 -070021void free_aligned(void *addr)
swissChili8cfb7c42021-04-18 21:17:58 -070022{
swissChili708d4c42021-07-04 17:40:07 -070023 free(addr);
swissChili8cfb7c42021-04-18 21:17:58 -070024}
swissChilif3e7f182021-04-20 13:57:22 -070025
swissChilif68671f2021-07-05 14:14:44 -070026void *link_program(dasm_State **Dst)
swissChilif3e7f182021-04-20 13:57:22 -070027{
28 size_t size;
29 void *buf;
30
swissChili53472e82021-05-08 16:06:32 -070031 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);
swissChilif3e7f182021-04-20 13:57:22 -070035
swissChili53472e82021-05-08 16:06:32 -070036 mprotect(buf, size, PROT_READ | PROT_EXEC);
swissChilif3e7f182021-04-20 13:57:22 -070037 return buf;
38}
swissChilif68671f2021-07-05 14:14:44 -070039
40bool file_exists(const char *path)
41{
42 return access(path, F_OK) == 0;
43}
swissChili7e1393c2021-07-07 12:59:12 -070044
45char *read_input_line(char *prompt)
46{
47 return readline(prompt);
48}
49
50void add_line_to_history(char *line)
51{
52 add_history(line);
53}