#include #include #include #include #include #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){ char *buffer_out = exp_alloc_array(a, char, POSIX_PATH_MAX_KIND_OF); assert(path.str[path.len] == 0); realpath((char *)path.str, buffer_out); // @memory @todo: If we allocated only 32 bytes here then deallocate rest return string_from_cstring(buffer_out); } 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 Array os_list_dir(Allocator *a, String dir, U32 flags = LIST_NO_FLAGS){ Scratch scratch(a); Array dirs_to_read = {scratch}; dirs_to_read.add(dir); Array result = {a}; for(auto it = dirs_to_read.begin(); it != dirs_to_read.end(); it++){ assert(it->str[it->len] == 0); dirent *dir; DIR *d = opendir((char *)it->str); if(d){ while ((dir = readdir(d)) != NULL) { if(dir->d_name[0] == '.'){ if(dir->d_name[1] == '.'){ if(dir->d_name[2] == 0) continue; } if(dir->d_name[1] == 0) continue; } OS_File_Info entry = {}; entry.relative_path = string_from_cstring(dir->d_name); entry.absolute_path = os_get_absolute_path(a, entry.relative_path); log_info("%Q\n", entry.absolute_path); } closedir(d); } } return 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; }