@@ -1,3 +1,3 @@ | |||||
CompileFlags: | CompileFlags: | ||||
Add: | Add: | ||||
- -DOS_WINDOWS | |||||
- -DOS_LINUX |
@@ -1,5 +1,6 @@ | |||||
#define STB_SPRINTF_IMPLEMENTATION | #define STB_SPRINTF_IMPLEMENTATION | ||||
#include "core.h" | #include "core.h" | ||||
#include "os.h" | |||||
void *pushSize(Arena *arena, size_t bytes) { | void *pushSize(Arena *arena, size_t bytes) { | ||||
if (arena->capacity - arena->head >= bytes) { | if (arena->capacity - arena->head >= bytes) { | ||||
@@ -11,11 +12,7 @@ void *pushSize(Arena *arena, size_t bytes) { | |||||
} | } | ||||
Arena *arenaAlloc(size_t capacity) { | Arena *arenaAlloc(size_t capacity) { | ||||
#if OS_WINDOWS | |||||
Arena *result = (Arena *)VirtualAlloc(NULL, sizeof(Arena) + capacity, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); | |||||
#elif OS_LINUX | |||||
Arena *result = (Arena *)mmap(0, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |||||
#endif | |||||
Arena *result = (Arena *)os_alloc(sizeof(Arena) + capacity); | |||||
result->memory = result + sizeof(Arena); | result->memory = result + sizeof(Arena); | ||||
result->capacity = capacity; | result->capacity = capacity; | ||||
result->head = 0; | result->head = 0; | ||||
@@ -23,6 +20,7 @@ Arena *arenaAlloc(size_t capacity) { | |||||
} | } | ||||
void arenaFree(Arena *arena) { | void arenaFree(Arena *arena) { | ||||
os_free(arena); | |||||
#if OS_WINDOWS | #if OS_WINDOWS | ||||
VirtualFree(arena, NULL, MEM_RELEASE); | VirtualFree(arena, NULL, MEM_RELEASE); | ||||
#elif OS_LINUX | #elif OS_LINUX | ||||
@@ -315,9 +313,9 @@ string readEntireFile(Arena *arena, string filename) { | |||||
} | } | ||||
return result; | return result; | ||||
#elif OS_LINUX | #elif OS_LINUX | ||||
FILE *input = fopen((char *)file.str, "r"); | |||||
FILE *input = fopen((char *)filename.str, "r"); | |||||
struct stat st; | struct stat st; | ||||
stat((char *)file.str, &st); | |||||
stat((char *)filename.str, &st); | |||||
size_t fsize = st.st_size; | size_t fsize = st.st_size; | ||||
string readBuffer = PushString(arena, filesize); | string readBuffer = PushString(arena, filesize); | ||||
readBuffer.length = filesize; | readBuffer.length = filesize; | ||||
@@ -7,15 +7,6 @@ | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <time.h> // TODO(dledda): try not to depend on this one | #include <time.h> // TODO(dledda): try not to depend on this one | ||||
#if OS_WINDOWS | |||||
#include "Windows.h" | |||||
#elif OS_LINUX | |||||
#include <sys/mman.h> | |||||
#include <sys/stat.h> | |||||
#else | |||||
#error Development environment not supported. | |||||
#endif | |||||
// ### Misc macros ### | // ### Misc macros ### | ||||
#if ENABLE_ASSERT | #if ENABLE_ASSERT | ||||
#define Assert(expression) if (!(expression)) {*(volatile int *)0 = 0;} | #define Assert(expression) if (!(expression)) {*(volatile int *)0 = 0;} | ||||
@@ -0,0 +1,12 @@ | |||||
#ifndef OS_CPP | |||||
#define OS_CPP | |||||
#if OS_WINDOWS | |||||
#include "os_win32.cpp" | |||||
#elif OS_LINUX | |||||
#include "os_linux.cpp" | |||||
#else | |||||
#error Development environment not supported. | |||||
#endif | |||||
#endif |
@@ -0,0 +1,12 @@ | |||||
#ifndef OS_H | |||||
#define OS_H | |||||
#include "core.h" | |||||
// ### Memory ### | |||||
void *os_alloc(size_t capacity); | |||||
void os_reserve(void *ptr); | |||||
void os_decommit(void *ptr); | |||||
void os_free(void *ptr); | |||||
#endif |
@@ -0,0 +1,21 @@ | |||||
#ifndef OS_IMPL_LINUX_CPP | |||||
#define OS_IMPL_LINUX_CPP | |||||
#include <sys/mman.h> | |||||
#include <sys/stat.h> | |||||
#include "os.h" | |||||
void *os_alloc(size_t capacity) { | |||||
return mmap(0, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |||||
} | |||||
void os_reserve(void *ptr) { | |||||
} | |||||
void os_decommit(void *ptr) { | |||||
} | |||||
void os_free(void *ptr) { | |||||
} | |||||
#endif |
@@ -0,0 +1,20 @@ | |||||
#ifndef OS_IMPL_WIN32_CPP | |||||
#define OS_IMPL_WIN32_CPP | |||||
#include "os.h" | |||||
#include "Windows.h" | |||||
void *osAlloc(size_t capacity) { | |||||
return VirtualAlloc(NULL, sizeof(Arena) + capacity, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); | |||||
} | |||||
void osReserve(void *ptr) { | |||||
} | |||||
void osDecommit(void *ptr) { | |||||
} | |||||
void osFree(void *ptr) { | |||||
} | |||||
#endif |