update os layer
This commit is contained in:
12
core.cpp
12
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;
|
||||
|
||||
9
core.h
9
core.h
@@ -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
12
os.cpp
Normal 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
12
os.h
Normal 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
21
os_linux.cpp
Normal 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
20
os_win32.cpp
Normal 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
|
||||
Reference in New Issue
Block a user