summaryrefslogtreecommitdiff
path: root/linux.c
diff options
context:
space:
mode:
Diffstat (limited to 'linux.c')
-rw-r--r--linux.c70
1 files changed, 70 insertions, 0 deletions
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 <sys/mman.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+
+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;
+}