blob: 82ecb104ff041850396eff1ab028e8ecd4991166 [file] [log] [blame]
swissChili8cfb7c42021-04-18 21:17:58 -07001#include "plat.h"
swissChilib51552c2021-08-03 10:23:37 -07002#include <stdio.h>
swissChili8cfb7c42021-04-18 21:17:58 -07003#include <stdlib.h>
4#include <string.h>
swissChilif3e7f182021-04-20 13:57:22 -07005#include <sys/mman.h>
swissChilif68671f2021-07-05 14:14:44 -07006#include <unistd.h>
swissChili7e1393c2021-07-07 12:59:12 -07007#include <readline/readline.h>
8#include <readline/history.h>
swissChili8cfb7c42021-04-18 21:17:58 -07009
swissChili53472e82021-05-08 16:06:32 -070010void *malloc_aligned(size_t size)
swissChili8cfb7c42021-04-18 21:17:58 -070011{
swissChili708d4c42021-07-04 17:40:07 -070012 // 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);
swissChili8cfb7c42021-04-18 21:17:58 -070015}
16
swissChili53472e82021-05-08 16:06:32 -070017void *realloc_aligned(void *addr, size_t size)
swissChili8cfb7c42021-04-18 21:17:58 -070018{
swissChili708d4c42021-07-04 17:40:07 -070019 return realloc(addr, size);
swissChili8cfb7c42021-04-18 21:17:58 -070020}
21
swissChili53472e82021-05-08 16:06:32 -070022void free_aligned(void *addr)
swissChili8cfb7c42021-04-18 21:17:58 -070023{
swissChili708d4c42021-07-04 17:40:07 -070024 free(addr);
swissChili8cfb7c42021-04-18 21:17:58 -070025}
swissChilif3e7f182021-04-20 13:57:22 -070026
swissChilif68671f2021-07-05 14:14:44 -070027void *link_program(dasm_State **Dst)
swissChilif3e7f182021-04-20 13:57:22 -070028{
29 size_t size;
30 void *buf;
31
swissChili53472e82021-05-08 16:06:32 -070032 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);
swissChilif3e7f182021-04-20 13:57:22 -070036
swissChili53472e82021-05-08 16:06:32 -070037 mprotect(buf, size, PROT_READ | PROT_EXEC);
swissChilif3e7f182021-04-20 13:57:22 -070038 return buf;
39}
swissChilif68671f2021-07-05 14:14:44 -070040
41bool file_exists(const char *path)
42{
43 return access(path, F_OK) == 0;
44}
swissChili7e1393c2021-07-07 12:59:12 -070045
46char *read_input_line(char *prompt)
47{
48 return readline(prompt);
49}
50
51void add_line_to_history(char *line)
52{
53 add_history(line);
54}