diff --git a/.clangd b/.clangd index 4ec85cd..5467e39 100644 --- a/.clangd +++ b/.clangd @@ -1,3 +1,3 @@ CompileFlags: Add: - - -DOS_WINDOWS + - -DOS_LINUX diff --git a/core.cpp b/core.cpp index efa4328..7320438 100644 --- a/core.cpp +++ b/core.cpp @@ -1,5 +1,6 @@ #define STB_SPRINTF_IMPLEMENTATION #include "core.h" +#include "os.h" void *pushSize(Arena *arena, size_t bytes) { if (arena->capacity - arena->head >= bytes) { @@ -11,11 +12,7 @@ void *pushSize(Arena *arena, size_t bytes) { } 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->capacity = capacity; result->head = 0; @@ -23,6 +20,7 @@ Arena *arenaAlloc(size_t capacity) { } void arenaFree(Arena *arena) { + os_free(arena); #if OS_WINDOWS VirtualFree(arena, NULL, MEM_RELEASE); #elif OS_LINUX @@ -315,9 +313,9 @@ string readEntireFile(Arena *arena, string filename) { } return result; #elif OS_LINUX - FILE *input = fopen((char *)file.str, "r"); + FILE *input = fopen((char *)filename.str, "r"); struct stat st; - stat((char *)file.str, &st); + stat((char *)filename.str, &st); size_t fsize = st.st_size; string readBuffer = PushString(arena, filesize); readBuffer.length = filesize; diff --git a/core.h b/core.h index 55fe41a..7ff357b 100644 --- a/core.h +++ b/core.h @@ -7,15 +7,6 @@ #include #include // TODO(dledda): try not to depend on this one -#if OS_WINDOWS - #include "Windows.h" -#elif OS_LINUX - #include - #include -#else - #error Development environment not supported. -#endif - // ### Misc macros ### #if ENABLE_ASSERT #define Assert(expression) if (!(expression)) {*(volatile int *)0 = 0;} diff --git a/os.cpp b/os.cpp new file mode 100644 index 0000000..64a2575 --- /dev/null +++ b/os.cpp @@ -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 diff --git a/os.h b/os.h new file mode 100644 index 0000000..e23818f --- /dev/null +++ b/os.h @@ -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 diff --git a/os_linux.cpp b/os_linux.cpp new file mode 100644 index 0000000..0ca6a11 --- /dev/null +++ b/os_linux.cpp @@ -0,0 +1,21 @@ +#ifndef OS_IMPL_LINUX_CPP +#define OS_IMPL_LINUX_CPP + +#include +#include +#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 diff --git a/os_win32.cpp b/os_win32.cpp new file mode 100644 index 0000000..46fc5ef --- /dev/null +++ b/os_win32.cpp @@ -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