update os layer

This commit is contained in:
Daniel Ledda
2025-01-02 11:02:27 +01:00
parent 5f0eaedfc6
commit 39ff16b6a6
7 changed files with 71 additions and 17 deletions

View File

@@ -1,3 +1,3 @@
CompileFlags:
Add:
- -DOS_WINDOWS
- -DOS_LINUX

View File

@@ -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;

9
core.h
View File

@@ -7,15 +7,6 @@
#include <stdio.h>
#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 ###
#if ENABLE_ASSERT
#define Assert(expression) if (!(expression)) {*(volatile int *)0 = 0;}

12
os.cpp Normal file
View File

@@ -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

12
os.h Normal file
View File

@@ -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

21
os_linux.cpp Normal file
View File

@@ -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

20
os_win32.cpp Normal file
View File

@@ -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