From 7e73843ac5873833c707687cd0ada0a3034e600c Mon Sep 17 00:00:00 2001 From: pryazha Date: Thu, 11 Sep 2025 19:25:30 +0500 Subject: add new files --- linux.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 linux.c (limited to 'linux.c') diff --git a/linux.c b/linux.c new file mode 100644 index 0000000..ff20e92 --- /dev/null +++ b/linux.c @@ -0,0 +1,70 @@ +#include "sys.h" +#include "macros.h" +#include +#include +#include +#include +#include + +void *sys_alloc(u64 length) +{ + assert(length); + i32 prot = PROT_READ | PROT_WRITE; + i32 flags = MAP_PRIVATE|MAP_ANONYMOUS; + void *result = mmap(0, length, prot, flags, -1, 0); + return result; +} + +void sys_free(void *memory, u64 length) +{ + assert(memory); + assert(length); + munmap(memory, length); +} + +void die(const char *format, ...) +{ + va_list args; + va_start(args, format); + fprintf(stderr, "error: "); + vfprintf(stderr, format, args); + fprintf(stderr, "\n"); + va_end(args); + _exit(1); +} + +void info(const char *format, ...) +{ + va_list args; + va_start(args, format); + vprintf(format, args); + putchar('\n'); + va_end(args); +} + +char *read_entire_file(struct arena *arena, u64 *len, const char *filename) +{ + FILE *file = fopen(filename, "rb"); + if (!file) + return 0; + if (fseek(file, 0, SEEK_END) == -1) + goto error; + i64 n = ftell(file); + if (n <= 0) + goto error; + if (len) + *len = n; + if (fseek(file, 0, SEEK_SET) == -1) + goto error; + char *buffer = push_arena(arena, n + 1); + if (!fread(buffer, 1, n, file)) { + pop_arena(arena, n + 1); + goto error; + } + fclose(file); + buffer[n] = 0; + return buffer; +error: + fclose(file); + return 0; +} -- cgit v1.2.3-70-g09d2