Porting to Unix
This commit is contained in:
122
os_unix.cpp
Normal file
122
os_unix.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#define POSIX_PATH_MAX_KIND_OF 4096
|
||||
#define POSIX_PAGE_SIZE 4096
|
||||
|
||||
function B32
|
||||
os_write_file(String filename, String filecontent){
|
||||
FILE *f = fopen((const char *)filename.str, "w");
|
||||
if(f){
|
||||
fwrite(filecontent.str, 1, filecontent.len, f);
|
||||
fclose(f);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function String
|
||||
os_read_file(Allocator *a, String name){
|
||||
String result = {0};
|
||||
FILE *f = fopen((char *)name.str, "rb");
|
||||
if(f){
|
||||
fseek(f, 0, SEEK_END);
|
||||
result.len = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
result.str = (U8 *)exp_alloc(a, result.len + 1);
|
||||
fread(result.str, result.len, 1, f);
|
||||
fclose(f);
|
||||
result.str[result.len] = 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function String
|
||||
os_get_exe_dir(Allocator *a){
|
||||
return "/mnt/c/AProgramming/cparse/compiler"_s;
|
||||
}
|
||||
|
||||
function String
|
||||
os_get_absolute_path(Allocator *a, String path){
|
||||
return ""_s;
|
||||
}
|
||||
|
||||
function B32
|
||||
os_does_file_exist(String path){
|
||||
return true;
|
||||
}
|
||||
|
||||
function String
|
||||
os_get_working_dir(Allocator *allocator){
|
||||
char *buffer = exp_alloc_array(allocator, char, POSIX_PATH_MAX_KIND_OF);
|
||||
char *result = getcwd(buffer, POSIX_PATH_MAX_KIND_OF);
|
||||
return string_from_cstring(result);
|
||||
}
|
||||
|
||||
function U8 *
|
||||
os_advance_commit(OS_Memory *m, size_t *commit_size, size_t page_size) {
|
||||
size_t aligned_up_commit = align_up(*commit_size, page_size);
|
||||
size_t to_be_total_commited_size = aligned_up_commit + m->commit;
|
||||
size_t to_be_total_commited_size_clamped_to_reserve = clamp_top(to_be_total_commited_size, m->reserve);
|
||||
size_t adjusted_to_boundary_commit = to_be_total_commited_size_clamped_to_reserve - m->commit;
|
||||
assert_message(adjusted_to_boundary_commit, "Debug WIN32 Error: Reached the virtual memory reserved boundary");
|
||||
U8 *result = m->data + m->commit;
|
||||
if (adjusted_to_boundary_commit == 0)
|
||||
result = 0;
|
||||
*commit_size = adjusted_to_boundary_commit;
|
||||
return result;
|
||||
}
|
||||
|
||||
function OS_Memory
|
||||
os_reserve(size_t size) {
|
||||
OS_Memory result = {};
|
||||
size_t size_aligned = align_up(size, POSIX_PAGE_SIZE);
|
||||
result.data = (U8 *)mmap(0, size_aligned, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
assert_message(result.data, POSIX_ERROR "Failed to reserve memory using mmap!!");
|
||||
if (result.data) {
|
||||
result.reserve = size_aligned;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function B32
|
||||
os_commit(OS_Memory *m, size_t commit) {
|
||||
B32 result = false;
|
||||
U8 * pointer = os_advance_commit(m, &commit, POSIX_PAGE_SIZE);
|
||||
if (pointer) {
|
||||
int mprotect_result = mprotect(pointer, commit, PROT_READ | PROT_WRITE);
|
||||
assert_message(mprotect_result == 0, "OS1 POSIX Debug Error: Failed to commit more memory using mmap");
|
||||
if (mprotect_result == 0) {
|
||||
m->commit += commit;
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function void
|
||||
os_release(OS_Memory *m) {
|
||||
int result = munmap(m->data, m->reserve);
|
||||
assert_message(result == 0, "OS1 POSIX Debug Error: Failed to release virtual memory using munmap");
|
||||
if (result == 0) {
|
||||
memory_zero(m, sizeof(*m));
|
||||
}
|
||||
}
|
||||
|
||||
function U64
|
||||
os_get_microseconds() {
|
||||
timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
U64 result = (((U64)ts.tv_sec) * 1000000ull) + ((U64)ts.tv_nsec) / 1000ull;
|
||||
return result;
|
||||
}
|
||||
|
||||
function F64
|
||||
os_time(){
|
||||
F64 time = (F64)os_get_microseconds();
|
||||
F64 result = time / 1000000.0; // Microseconds to seconds
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user