os_core: linux port for memory, thread info, filesystem apis

This commit is contained in:
Ryan Fleury
2024-07-16 13:24:54 -07:00
parent 1b74fb0760
commit 3cc51038df
42 changed files with 17931 additions and 17128 deletions
+9 -9
View File
@@ -99,16 +99,16 @@ if not "%no_meta%"=="1" (
:: --- Build Everything (@build_targets) -------------------------------------- :: --- Build Everything (@build_targets) --------------------------------------
pushd build pushd build
if "%raddbg%"=="1" set didbuild=1 && %compile% ..\src\raddbg\raddbg_main.c %compile_link% %out%raddbg.exe || exit /b 1 if "%raddbg%"=="1" set didbuild=1 && %compile% ..\src\raddbg\raddbg_main.c %compile_link% %out%raddbg.exe || exit /b 1
if "%rdi_from_pdb%"=="1" set didbuild=1 && %compile% ..\src\rdi_from_pdb\rdi_from_pdb_main.c %compile_link% %out%rdi_from_pdb.exe || exit /b 1 if "%rdi_from_pdb%"=="1" set didbuild=1 && %compile% ..\src\rdi_from_pdb\rdi_from_pdb_main.c %compile_link% %out%rdi_from_pdb.exe || exit /b 1
if "%rdi_from_dwarf%"=="1" set didbuild=1 && %compile% ..\src\rdi_from_dwarf\rdi_from_dwarf.c %compile_link% %out%rdi_from_dwarf.exe || exit /b 1 if "%rdi_from_dwarf%"=="1" set didbuild=1 && %compile% ..\src\rdi_from_dwarf\rdi_from_dwarf.c %compile_link% %out%rdi_from_dwarf.exe || exit /b 1
if "%rdi_dump%"=="1" set didbuild=1 && %compile% ..\src\rdi_dump\rdi_dump_main.c %compile_link% %out%rdi_dump.exe || exit /b 1 if "%rdi_dump%"=="1" set didbuild=1 && %compile% ..\src\rdi_dump\rdi_dump_main.c %compile_link% %out%rdi_dump.exe || exit /b 1
if "%rdi_breakpad_from_pdb%"=="1" set didbuild=1 && %compile% ..\src\rdi_breakpad_from_pdb\rdi_breakpad_from_pdb_main.c %compile_link% %out%rdi_breakpad_from_pdb.exe || exit /b 1 if "%rdi_breakpad_from_pdb%"=="1" set didbuild=1 && %compile% ..\src\rdi_breakpad_from_pdb\rdi_breakpad_from_pdb_main.c %compile_link% %out%rdi_breakpad_from_pdb.exe || exit /b 1
if "%ryan_scratch%"=="1" set didbuild=1 && %compile% ..\src\scratch\ryan_scratch.c %compile_link% %out%ryan_scratch.exe || exit /b 1 if "%ryan_scratch%"=="1" set didbuild=1 && %compile% ..\src\scratch\ryan_scratch.c %compile_link% %out%ryan_scratch.exe || exit /b 1
if "%cpp_tests%"=="1" set didbuild=1 && %compile% ..\src\scratch\i_hate_c_plus_plus.cpp %compile_link% %out%cpp_tests.exe || exit /b 1 if "%cpp_tests%"=="1" set didbuild=1 && %compile% ..\src\scratch\i_hate_c_plus_plus.cpp %compile_link% %out%cpp_tests.exe || exit /b 1
if "%look_at_raddbg%"=="1" set didbuild=1 && %compile% ..\src\scratch\look_at_raddbg.c %compile_link% %out%look_at_raddbg.exe || exit /b 1 if "%look_at_raddbg%"=="1" set didbuild=1 && %compile% ..\src\scratch\look_at_raddbg.c %compile_link% %out%look_at_raddbg.exe || exit /b 1
if "%mule_main%"=="1" set didbuild=1 && del vc*.pdb mule*.pdb && %compile_release% %only_compile% ..\src\mule\mule_inline.cpp && %compile_release% %only_compile% ..\src\mule\mule_o2.cpp && %compile_debug% %EHsc% ..\src\mule\mule_main.cpp ..\src\mule\mule_c.c mule_inline.obj mule_o2.obj %compile_link% %no_aslr% %out%mule_main.exe || exit /b 1 if "%mule_main%"=="1" set didbuild=1 && del vc*.pdb mule*.pdb && %compile_release% %only_compile% ..\src\mule\mule_inline.cpp && %compile_release% %only_compile% ..\src\mule\mule_o2.cpp && %compile_debug% %EHsc% ..\src\mule\mule_main.cpp ..\src\mule\mule_c.c mule_inline.obj mule_o2.obj %compile_link% %no_aslr% %out%mule_main.exe || exit /b 1
if "%mule_module%"=="1" set didbuild=1 && %compile% ..\src\mule\mule_module.cpp %compile_link% %link_dll% %out%mule_module.dll || exit /b 1 if "%mule_module%"=="1" set didbuild=1 && %compile% ..\src\mule\mule_module.cpp %compile_link% %link_dll% %out%mule_module.dll || exit /b 1
if "%mule_hotload%"=="1" set didbuild=1 && %compile% ..\src\mule\mule_hotload_main.c %compile_link% %out%mule_hotload.exe & %compile% ..\src\mule\mule_hotload_module_main.c %compile_link% %link_dll% %out%mule_hotload_module.dll || exit /b 1 if "%mule_hotload%"=="1" set didbuild=1 && %compile% ..\src\mule\mule_hotload_main.c %compile_link% %out%mule_hotload.exe & %compile% ..\src\mule\mule_hotload_module_main.c %compile_link% %link_dll% %out%mule_hotload_module.dll || exit /b 1
if "%mule_peb_trample%"=="1" ( if "%mule_peb_trample%"=="1" (
set didbuild=1 set didbuild=1
+3 -2
View File
@@ -16,7 +16,7 @@ auto_compile_flags=''
clang_common='-I../src/ -I../local/ -gcodeview -fdiagnostics-absolute-paths -Wall -Wno-unknown-warning-option -Wno-missing-braces -Wno-unused-function -Wno-writable-strings -Wno-unused-value -Wno-unused-variable -Wno-unused-local-typedef -Wno-deprecated-register -Wno-deprecated-declarations -Wno-unused-but-set-variable -Wno-single-bit-bitfield-constant-conversion -Wno-compare-distinct-pointer-types -Wno-initializer-overrides -Wno-incompatible-pointer-types-discards-qualifiers -Xclang -flto-visibility-public-std -D_USE_MATH_DEFINES -Dstrdup=_strdup -Dgnu_printf=printf' clang_common='-I../src/ -I../local/ -gcodeview -fdiagnostics-absolute-paths -Wall -Wno-unknown-warning-option -Wno-missing-braces -Wno-unused-function -Wno-writable-strings -Wno-unused-value -Wno-unused-variable -Wno-unused-local-typedef -Wno-deprecated-register -Wno-deprecated-declarations -Wno-unused-but-set-variable -Wno-single-bit-bitfield-constant-conversion -Wno-compare-distinct-pointer-types -Wno-initializer-overrides -Wno-incompatible-pointer-types-discards-qualifiers -Xclang -flto-visibility-public-std -D_USE_MATH_DEFINES -Dstrdup=_strdup -Dgnu_printf=printf'
clang_debug="clang -g -O0 -DBUILD_DEBUG=1 ${clang_common} ${auto_compile_flags}" clang_debug="clang -g -O0 -DBUILD_DEBUG=1 ${clang_common} ${auto_compile_flags}"
clang_release="clang -g -O2 -DBUILD_DEBUG=0 ${clang_common} ${auto_compile_flags}" clang_release="clang -g -O2 -DBUILD_DEBUG=0 ${clang_common} ${auto_compile_flags}"
clang_link="" clang_link="-lpthread"
clang_out="-o" clang_out="-o"
# --- Per-Build Settings ------------------------------------------------------ # --- Per-Build Settings ------------------------------------------------------
@@ -46,7 +46,8 @@ fi
# --- Build Everything (@build_targets) --------------------------------------- # --- Build Everything (@build_targets) ---------------------------------------
cd build cd build
if [ "$raddbg" = "1" ]; then didbuild=1 && $compile ../src/raddbg/raddbg_main.c $compile_link $out raddbg || exit 1; fi if [ "$raddbg" = "1" ]; then didbuild=1 && $compile ../src/raddbg/raddbg_main.c $compile_link $out raddbg || exit 1; fi
if [ "$ryan_scratch" = "1" ]; then didbuild=1 && $compile ../src/scratch/ryan_scratch.c $compile_link $out ryan_scratch || exit 1; fi
cd .. cd ..
# --- Warn On No Builds ------------------------------------------------------- # --- Warn On No Builds -------------------------------------------------------
-1
View File
@@ -101,7 +101,6 @@
#elif COMPILER_GCC #elif COMPILER_GCC
# define AlignOf(T) __alignof__(T) # define AlignOf(T) __alignof__(T)
#else #else
#else
# error AlignOf not defined for this compiler. # error AlignOf not defined for this compiler.
#endif #endif
+790
View File
@@ -1,2 +1,792 @@
// Copyright (c) 2024 Epic Games Tools // Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/) // Licensed under the MIT license (https://opensource.org/license/mit/)
////////////////////////////////
//~ rjf: Helpers
internal DateTime
os_lnx_date_time_from_tm(tm in, U32 msec)
{
DateTime dt = {0};
dt.sec = in.tm_sec;
dt.min = in.tm_min;
dt.hour = in.tm_hour;
dt.day = in.tm_mday-1;
dt.mon = in.tm_mon;
dt.year = in.tm_year+1900;
dt.msec = msec;
return dt;
}
internal tm
os_lnx_tm_from_date_time(DateTime dt)
{
tm result = {0};
result.tm_sec = dt.sec;
result.tm_min = dt.min;
result.tm_hour= dt.hour;
result.tm_mday= dt.day+1;
result.tm_mon = dt.mon;
result.tm_year= dt.year-1900;
return result;
}
internal timespec
os_lnx_timespec_from_date_time(DateTime dt)
{
tm tm_val = os_lnx_tm_from_date_time(dt);
time_t seconds = timegm(&tm_val);
timespec result = {0};
result.tv_sec = seconds;
return result;
}
internal DenseTime
os_lnx_dense_time_from_timespec(timespec in)
{
DenseTime result = 0;
{
struct tm tm_time = {0};
gmtime_r(&in.tv_sec, &tm_time);
DateTime date_time = os_lnx_date_time_from_tm(tm_time, in.tv_nsec/Million(1));
result = dense_time_from_date_time(date_time);
}
return result;
}
internal FileProperties
os_lnx_file_properties_from_stat(struct stat *s)
{
FileProperties props = {0};
props.size = s->st_size;
props.created = os_lnx_dense_time_from_timespec(s->st_ctim);
props.modified = os_lnx_dense_time_from_timespec(s->st_mtim);
if(s->st_mode & S_IFDIR)
{
props.flags |= FilePropertyFlag_IsFolder;
}
return props;
}
////////////////////////////////
//~ rjf: @os_hooks System/Process Info (Implemented Per-OS)
internal OS_SystemInfo *
os_get_system_info(void)
{
return &os_lnx_state.system_info;
}
internal OS_ProcessInfo *
os_get_process_info(void)
{
return &os_lnx_state.process_info;
}
internal String8
os_get_current_path(Arena *arena)
{
char *cwdir = getcwd(0, 0);
String8 string = push_str8_copy(arena, str8_cstring(cwdir));
return string;
}
////////////////////////////////
//~ rjf: @os_hooks Memory Allocation (Implemented Per-OS)
//- rjf: basic
internal void *
os_reserve(U64 size)
{
void *result = mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
return result;
}
internal B32
os_commit(void *ptr, U64 size)
{
mprotect(ptr, size, PROT_READ|PROT_WRITE);
return 1;
}
internal void
os_decommit(void *ptr, U64 size)
{
madvise(ptr, size, MADV_DONTNEED);
mprotect(ptr, size, PROT_NONE);
}
internal void
os_release(void *ptr, U64 size)
{
munmap(ptr, size);
}
//- rjf: large pages
internal void *
os_reserve_large(U64 size)
{
void *result = mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB, -1, 0);
return result;
}
internal B32
os_commit_large(void *ptr, U64 size)
{
mprotect(ptr, size, PROT_READ|PROT_WRITE);
return 1;
}
////////////////////////////////
//~ rjf: @os_hooks Thread Info (Implemented Per-OS)
internal U32
os_tid(void)
{
U32 result = 0;
#if defined(SYS_gettid)
result = syscall(SYS_gettid);
#else
result = gettid();
#endif
return result;
}
internal void
os_set_thread_name(String8 name)
{
Temp scratch = scratch_begin(0, 0);
String8 name_copy = push_str8_copy(scratch.arena, name);
pthread_t current_thread = pthread_self();
pthread_setname_np(current_thread, (char *)name_copy.str);
scratch_end(scratch);
}
////////////////////////////////
//~ rjf: @os_hooks Aborting (Implemented Per-OS)
internal void
os_abort(S32 exit_code)
{
exit(exit_code);
}
////////////////////////////////
//~ rjf: @os_hooks File System (Implemented Per-OS)
//- rjf: files
internal OS_Handle
os_file_open(OS_AccessFlags flags, String8 path)
{
Temp scratch = scratch_begin(0, 0);
String8 path_copy = push_str8_copy(scratch.arena, path);
int lnx_flags = 0;
if(flags & (OS_AccessFlag_Read|OS_AccessFlag_Write))
{
lnx_flags = O_RDWR;
}
else if(flags & OS_AccessFlag_Write)
{
lnx_flags = O_WRONLY;
}
else if(flags & OS_AccessFlag_Read)
{
lnx_flags = O_RDONLY;
}
if(flags & OS_AccessFlag_Append)
{
lnx_flags |= O_APPEND;
}
int fd = open((char *)path_copy.str, lnx_flags);
OS_Handle handle = {0};
if(fd != -1)
{
handle.u64[0] = fd;
}
scratch_end(scratch);
return handle;
}
internal void
os_file_close(OS_Handle file)
{
if(os_handle_match(file, os_handle_zero())) { return; }
int fd = (int)file.u64[0];
close(fd);
}
internal U64
os_file_read(OS_Handle file, Rng1U64 rng, void *out_data)
{
if(os_handle_match(file, os_handle_zero())) { return 0; }
int fd = (int)file.u64[0];
if(rng.min != 0)
{
lseek(fd, rng.min, SEEK_SET);
}
U64 total_num_bytes_to_read = dim_1u64(rng);
U64 total_num_bytes_read = 0;
U64 total_num_bytes_left_to_read = total_num_bytes_to_read;
for(;total_num_bytes_left_to_read > 0;)
{
int read_result = read(fd, (U8 *)out_data + total_num_bytes_read, total_num_bytes_left_to_read);
if(read_result >= 0)
{
total_num_bytes_read += read_result;
total_num_bytes_left_to_read -= read_result;
}
else if(errno != EINTR)
{
break;
}
}
return total_num_bytes_read;
}
internal U64
os_file_write(OS_Handle file, Rng1U64 rng, void *data)
{
if(os_handle_match(file, os_handle_zero())) { return 0; }
int fd = (int)file.u64[0];
if(rng.min != 0)
{
lseek(fd, rng.min, SEEK_SET);
}
U64 total_num_bytes_to_write = dim_1u64(rng);
U64 total_num_bytes_written = 0;
U64 total_num_bytes_left_to_write = total_num_bytes_to_write;
for(;total_num_bytes_left_to_write > 0;)
{
int write_result = write(fd, (U8 *)data + total_num_bytes_written, total_num_bytes_left_to_write);
if(write_result >= 0)
{
total_num_bytes_written += write_result;
total_num_bytes_left_to_write -= write_result;
}
else if(errno != EINTR)
{
break;
}
}
return total_num_bytes_written;
}
internal B32
os_file_set_times(OS_Handle file, DateTime date_time)
{
if(os_handle_match(file, os_handle_zero())) { return 0; }
int fd = (int)file.u64[0];
timespec time = os_lnx_timespec_from_date_time(date_time);
timespec times[2] = {time, time};
int futimens_result = futimens(fd, times);
B32 good = (futimens_result != -1);
return good;
}
internal FileProperties
os_properties_from_file(OS_Handle file)
{
if(os_handle_match(file, os_handle_zero())) { return (FileProperties){0}; }
int fd = (int)file.u64[0];
struct stat fd_stat = {0};
int fstat_result = fstat(fd, &fd_stat);
FileProperties props = {0};
if(fstat_result != -1)
{
props = os_lnx_file_properties_from_stat(&fd_stat);
}
return props;
}
internal OS_FileID
os_id_from_file(OS_Handle file)
{
if(os_handle_match(file, os_handle_zero())) { return (OS_FileID){0}; }
int fd = (int)file.u64[0];
struct stat fd_stat = {0};
int fstat_result = fstat(fd, &fd_stat);
OS_FileID id = {0};
if(fstat_result != -1)
{
id.v[0] = fd_stat.st_dev;
id.v[1] = fd_stat.st_ino;
}
return id;
}
internal B32
os_delete_file_at_path(String8 path)
{
Temp scratch = scratch_begin(0, 0);
B32 result = 0;
String8 path_copy = push_str8_copy(scratch.arena, path);
if(remove((char*)path_copy.str) != -1)
{
result = 1;
}
scratch_end(scratch);
return result;
}
internal B32
os_copy_file_path(String8 dst, String8 src)
{
B32 result = 0;
OS_Handle src_h = os_file_open(OS_AccessFlag_Read, src);
OS_Handle dst_h = os_file_open(OS_AccessFlag_Write, dst);
if(!os_handle_match(src_h, os_handle_zero()) &&
!os_handle_match(dst_h, os_handle_zero()))
{
FileProperties src_props = os_properties_from_file(src_h);
U64 size = src_props.size;
U64 total_bytes_copied = 0;
U64 bytes_left_to_copy = size;
for(;bytes_left_to_copy > 0;)
{
Temp scratch = scratch_begin(0, 0);
U64 buffer_size = Min(bytes_left_to_copy, MB(8));
U8 *buffer = push_array_no_zero(scratch.arena, U8, buffer_size);
U64 bytes_read = os_file_read(src_h, r1u64(total_bytes_copied, total_bytes_copied+buffer_size), buffer);
U64 bytes_written = os_file_write(dst_h, r1u64(total_bytes_copied, total_bytes_copied+bytes_read), buffer);
U64 bytes_copied = Min(bytes_read, bytes_written);
bytes_left_to_copy -= bytes_copied;
total_bytes_copied += bytes_copied;
scratch_end(scratch);
if(bytes_copied == 0)
{
break;
}
}
}
os_file_close(src_h);
os_file_close(dst_h);
return result;
}
internal String8
os_full_path_from_path(Arena *arena, String8 path)
{
Temp scratch = scratch_begin(&arena, 1);
String8 path_copy = push_str8_copy(scratch.arena, path);
char buffer[PATH_MAX] = {0};
realpath((char *)path_copy.str, buffer);
String8 result = push_str8_copy(arena, str8_cstring(buffer));
scratch_end(scratch);
return result;
}
internal B32
os_file_path_exists(String8 path)
{
Temp scratch = scratch_begin(0, 0);
String8 path_copy = push_str8_copy(scratch.arena, path);
int access_result = access((char *)path_copy.str, F_OK);
B32 result = 0;
if(access_result == 0)
{
result = 1;
}
scratch_end(scratch);
return result;
}
internal FileProperties
os_properties_from_file_path(String8 path)
{
Temp scratch = scratch_begin(0, 0);
String8 path_copy = push_str8_copy(scratch.arena, path);
struct stat f_stat = {0};
int stat_result = stat((char *)path_copy.str, &f_stat);
FileProperties props = {0};
if(stat_result != -1)
{
props = os_lnx_file_properties_from_stat(&f_stat);
}
scratch_end(scratch);
return props;
}
//- rjf: file maps
internal OS_Handle
os_file_map_open(OS_AccessFlags flags, OS_Handle file)
{
NotImplemented;
}
internal void
os_file_map_close(OS_Handle map)
{
NotImplemented;
}
internal void *
os_file_map_view_open(OS_Handle map, OS_AccessFlags flags, Rng1U64 range)
{
NotImplemented;
}
internal void
os_file_map_view_close(OS_Handle map, void *ptr)
{
NotImplemented;
}
//- rjf: directory iteration
internal OS_FileIter *
os_file_iter_begin(Arena *arena, String8 path, OS_FileIterFlags flags)
{
NotImplemented;
}
internal B32
os_file_iter_next(Arena *arena, OS_FileIter *iter, OS_FileInfo *info_out)
{
NotImplemented;
}
internal void
os_file_iter_end(OS_FileIter *iter)
{
NotImplemented;
}
//- rjf: directory creation
internal B32
os_make_directory(String8 path)
{
NotImplemented;
}
////////////////////////////////
//~ rjf: @os_hooks Shared Memory (Implemented Per-OS)
internal OS_Handle
os_shared_memory_alloc(U64 size, String8 name)
{
NotImplemented;
}
internal OS_Handle
os_shared_memory_open(String8 name)
{
NotImplemented;
}
internal void
os_shared_memory_close(OS_Handle handle)
{
NotImplemented;
}
internal void *
os_shared_memory_view_open(OS_Handle handle, Rng1U64 range)
{
NotImplemented;
}
internal void
os_shared_memory_view_close(OS_Handle handle, void *ptr)
{
NotImplemented;
}
////////////////////////////////
//~ rjf: @os_hooks Time (Implemented Per-OS)
internal U64
os_now_microseconds(void)
{
NotImplemented;
}
internal U32
os_now_unix(void)
{
NotImplemented;
}
internal DateTime
os_now_universal_time(void)
{
NotImplemented;
}
internal DateTime
os_universal_time_from_local(DateTime *date_time)
{
NotImplemented;
}
internal DateTime
os_local_time_from_universal(DateTime *date_time)
{
NotImplemented;
}
internal void
os_sleep_milliseconds(U32 msec)
{
NotImplemented;
}
////////////////////////////////
//~ rjf: @os_hooks Child Processes (Implemented Per-OS)
internal OS_Handle
os_process_launch(OS_ProcessLaunchParams *params)
{
NotImplemented;
}
internal B32
os_process_join(OS_Handle handle, U64 endt_us)
{
NotImplemented;
}
internal void
os_process_detach(OS_Handle handle)
{
NotImplemented;
}
////////////////////////////////
//~ rjf: @os_hooks Threads (Implemented Per-OS)
internal OS_Handle
os_thread_launch(OS_ThreadFunctionType *func, void *ptr, void *params)
{
NotImplemented;
}
internal B32
os_thread_join(OS_Handle handle, U64 endt_us)
{
NotImplemented;
}
internal void
os_thread_detach(OS_Handle thread)
{
NotImplemented;
}
////////////////////////////////
//~ rjf: @os_hooks Synchronization Primitives (Implemented Per-OS)
//- rjf: mutexes
internal OS_Handle
os_mutex_alloc(void)
{
NotImplemented;
}
internal void
os_mutex_release(OS_Handle mutex)
{
NotImplemented;
}
internal void
os_mutex_take(OS_Handle mutex)
{
NotImplemented;
}
internal void
os_mutex_drop(OS_Handle mutex)
{
NotImplemented;
}
//- rjf: reader/writer mutexes
internal OS_Handle
os_rw_mutex_alloc(void)
{
NotImplemented;
}
internal void
os_rw_mutex_release(OS_Handle rw_mutex)
{
NotImplemented;
}
internal void
os_rw_mutex_take_r(OS_Handle rw_mutex)
{
NotImplemented;
}
internal void
os_rw_mutex_drop_r(OS_Handle rw_mutex)
{
NotImplemented;
}
internal void
os_rw_mutex_take_w(OS_Handle rw_mutex)
{
NotImplemented;
}
internal void
os_rw_mutex_drop_w(OS_Handle rw_mutex)
{
NotImplemented;
}
//- rjf: condition variables
internal OS_Handle
os_condition_variable_alloc(void)
{
NotImplemented;
}
internal void
os_condition_variable_release(OS_Handle cv)
{
NotImplemented;
}
internal B32
os_condition_variable_wait(OS_Handle cv, OS_Handle mutex, U64 endt_us)
{
NotImplemented;
}
internal B32
os_condition_variable_wait_rw_r(OS_Handle cv, OS_Handle mutex_rw, U64 endt_us)
{
NotImplemented;
}
internal B32
os_condition_variable_wait_rw_w(OS_Handle cv, OS_Handle mutex_rw, U64 endt_us)
{
NotImplemented;
}
internal void
os_condition_variable_signal(OS_Handle cv)
{
NotImplemented;
}
internal void
os_condition_variable_broadcast(OS_Handle cv)
{
NotImplemented;
}
//- rjf: cross-process semaphores
internal OS_Handle
os_semaphore_alloc(U32 initial_count, U32 max_count, String8 name)
{
NotImplemented;
}
internal void
os_semaphore_release(OS_Handle semaphore)
{
NotImplemented;
}
internal OS_Handle
os_semaphore_open(String8 name)
{
NotImplemented;
}
internal void
os_semaphore_close(OS_Handle semaphore)
{
NotImplemented;
}
internal B32
os_semaphore_take(OS_Handle semaphore, U64 endt_us)
{
NotImplemented;
}
internal void
os_semaphore_drop(OS_Handle semaphore)
{
NotImplemented;
}
////////////////////////////////
//~ rjf: @os_hooks Dynamically-Loaded Libraries (Implemented Per-OS)
internal OS_Handle
os_library_open(String8 path)
{
Temp scratch = scratch_begin(0, 0);
char *path_cstr = (char *)push_str8_copy(scratch.arena, path).str;
void *so = dlopen(path_cstr, RTLD_LAZY);
OS_Handle lib = { (U64)so };
scratch_end(scratch);
return lib;
}
internal VoidProc*
os_library_load_proc(OS_Handle lib, String8 name)
{
Temp scratch = scratch_begin(0, 0);
void *so = (void *)lib.u64;
char *name_cstr = (char *)push_str8_copy(scratch.arena, name).str;
VoidProc *proc = (VoidProc *)dlsym(so, name_cstr);
scratch_end(scratch);
return proc;
}
internal void
os_library_close(OS_Handle lib)
{
void *so = (void *)lib.u64;
dlclose(so);
}
////////////////////////////////
//~ rjf: @os_hooks Safe Calls (Implemented Per-OS)
internal void
os_safe_call(OS_ThreadFunctionType *func, OS_ThreadFunctionType *fail_handler, void *ptr)
{
NotImplemented;
}
////////////////////////////////
//~ rjf: @os_hooks GUIDs (Implemented Per-OS)
internal OS_Guid
os_make_guid(void)
{
NotImplemented;
}
////////////////////////////////
//~ rjf: @os_hooks Entry Points (Implemented Per-OS)
int
main(int argc, char **argv)
{
//- rjf: set up OS layer
{
}
//- rjf: call into "real" entry point
main_thread_base_entry_point(entry_point, argv, (U64)argc);
}
+43
View File
@@ -4,4 +4,47 @@
#ifndef OS_CORE_LINUX_H #ifndef OS_CORE_LINUX_H
#define OS_CORE_LINUX_H #define OS_CORE_LINUX_H
////////////////////////////////
//~ rjf: Includes
#define _GNU_SOURCE
#include <features.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/limits.h>
#include <time.h>
#include <dirent.h>
#include <pthread.h>
#include <sys/syscall.h>
#include <signal.h>
#include <errno.h>
#include <dlfcn.h>
#include <sys/sysinfo.h>
int pthread_setname_np(pthread_t thread, const char *name);
int pthread_getname_np(pthread_t thread, char *name, size_t size);
typedef struct tm tm;
typedef struct timespec timespec;
////////////////////////////////
//~ rjf: State
typedef struct OS_LNX_State OS_LNX_State;
struct OS_LNX_State
{
Arena *arena;
OS_SystemInfo system_info;
OS_ProcessInfo process_info;
};
////////////////////////////////
//~ rjf: Globals
global OS_LNX_State os_lnx_state = {0};
#endif // OS_CORE_LINUX_H #endif // OS_CORE_LINUX_H
+1 -1
View File
@@ -1120,7 +1120,7 @@ os_file_read(OS_Handle file, Rng1U64 rng, void *out_data)
return 0; return 0;
} }
internal void internal U64
os_file_write(OS_Handle file, Rng1U64 rng, void *data) os_file_write(OS_Handle file, Rng1U64 rng, void *data)
{ {
NotImplemented; NotImplemented;
+1 -3
View File
@@ -187,8 +187,6 @@ internal void os_decommit(void *ptr, U64 size);
internal void os_release(void *ptr, U64 size); internal void os_release(void *ptr, U64 size);
//- rjf: large pages //- rjf: large pages
internal B32 os_set_large_pages_enabled(B32 flag);
internal B32 os_large_pages_enabled(void);
internal void *os_reserve_large(U64 size); internal void *os_reserve_large(U64 size);
internal B32 os_commit_large(void *ptr, U64 size); internal B32 os_commit_large(void *ptr, U64 size);
@@ -210,7 +208,7 @@ internal void os_abort(S32 exit_code);
internal OS_Handle os_file_open(OS_AccessFlags flags, String8 path); internal OS_Handle os_file_open(OS_AccessFlags flags, String8 path);
internal void os_file_close(OS_Handle file); internal void os_file_close(OS_Handle file);
internal U64 os_file_read(OS_Handle file, Rng1U64 rng, void *out_data); internal U64 os_file_read(OS_Handle file, Rng1U64 rng, void *out_data);
internal void os_file_write(OS_Handle file, Rng1U64 rng, void *data); internal U64 os_file_write(OS_Handle file, Rng1U64 rng, void *data);
internal B32 os_file_set_times(OS_Handle file, DateTime time); internal B32 os_file_set_times(OS_Handle file, DateTime time);
internal FileProperties os_properties_from_file(OS_Handle file); internal FileProperties os_properties_from_file(OS_Handle file);
internal OS_FileID os_id_from_file(OS_Handle file); internal OS_FileID os_id_from_file(OS_Handle file);
+15 -40
View File
@@ -201,37 +201,6 @@ os_release(void *ptr, U64 size)
//- rjf: large pages //- rjf: large pages
internal B32
os_set_large_pages_enabled(B32 flag)
{
B32 is_ok = 0;
HANDLE token;
if(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
{
LUID luid;
if(LookupPrivilegeValue(0, SE_LOCK_MEMORY_NAME, &luid))
{
TOKEN_PRIVILEGES priv;
priv.PrivilegeCount = 1;
priv.Privileges[0].Luid = luid;
priv.Privileges[0].Attributes = flag ? SE_PRIVILEGE_ENABLED: 0;
if(AdjustTokenPrivileges(token, 0, &priv, sizeof(priv), 0, 0))
{
os_w32_state.large_pages_enabled = flag;
is_ok = 1;
}
}
CloseHandle(token);
}
return is_ok;
}
internal B32
os_large_pages_enabled(void)
{
return os_w32_state.large_pages_enabled;
}
internal void * internal void *
os_reserve_large(U64 size) os_reserve_large(U64 size)
{ {
@@ -385,14 +354,15 @@ os_file_read(OS_Handle file, Rng1U64 rng, void *out_data)
return total_read_size; return total_read_size;
} }
internal void internal U64
os_file_write(OS_Handle file, Rng1U64 rng, void *data) os_file_write(OS_Handle file, Rng1U64 rng, void *data)
{ {
if(os_handle_match(file, os_handle_zero())) { return; } if(os_handle_match(file, os_handle_zero())) { return 0; }
HANDLE win_handle = (HANDLE)file.u64[0]; HANDLE win_handle = (HANDLE)file.u64[0];
U64 src_off = 0; U64 src_off = 0;
U64 dst_off = rng.min; U64 dst_off = rng.min;
U64 bytes_to_write_total = rng.max-rng.min; U64 bytes_to_write_total = rng.max-rng.min;
U64 total_bytes_written = 0;
for(;src_off < bytes_to_write_total;) for(;src_off < bytes_to_write_total;)
{ {
void *bytes_src = (void *)((U8 *)data + src_off); void *bytes_src = (void *)((U8 *)data + src_off);
@@ -409,17 +379,19 @@ os_file_write(OS_Handle file, Rng1U64 rng, void *data)
} }
src_off += bytes_written; src_off += bytes_written;
dst_off += bytes_written; dst_off += bytes_written;
total_bytes_written += bytes_written;
} }
return total_bytes_written;
} }
internal B32 internal B32
os_file_set_times(OS_Handle file, DateTime time) os_file_set_time(OS_Handle file, DateTime time)
{ {
if(os_handle_match(file, os_handle_zero())) { return 0; } if(os_handle_match(file, os_handle_zero())) { return 0; }
B32 result = 0; B32 result = 0;
HANDLE handle = (HANDLE)file.u64[0]; HANDLE handle = (HANDLE)file.u64[0];
SYSTEMTIME system_time = {0}; SYSTEMTIME system_time = {0};
w32_system_time_from_date_time(&system_time, &time); os_w32_system_time_from_date_time(&system_time, &time);
FILETIME file_time = {0}; FILETIME file_time = {0};
result = (SystemTimeToFileTime(&system_time, &file_time) && result = (SystemTimeToFileTime(&system_time, &file_time) &&
SetFileTime(handle, &file_time, &file_time, &file_time)); SetFileTime(handle, &file_time, &file_time, &file_time));
@@ -1232,27 +1204,30 @@ os_semaphore_drop(OS_Handle semaphore)
//~ rjf: @os_hooks Dynamically-Loaded Libraries (Implemented Per-OS) //~ rjf: @os_hooks Dynamically-Loaded Libraries (Implemented Per-OS)
internal OS_Handle internal OS_Handle
os_library_open(String8 path){ os_library_open(String8 path)
{
Temp scratch = scratch_begin(0, 0); Temp scratch = scratch_begin(0, 0);
String16 path16 = str16_from_8(scratch.arena, path); String16 path16 = str16_from_8(scratch.arena, path);
HMODULE mod = LoadLibraryW((LPCWSTR)path16.str); HMODULE mod = LoadLibraryW((LPCWSTR)path16.str);
OS_Handle result = { (U64)mod }; OS_Handle result = { (U64)mod };
scratch_end(scratch); scratch_end(scratch);
return(result); return result;
} }
internal VoidProc* internal VoidProc*
os_library_load_proc(OS_Handle lib, String8 name){ os_library_load_proc(OS_Handle lib, String8 name)
{
Temp scratch = scratch_begin(0, 0); Temp scratch = scratch_begin(0, 0);
HMODULE mod = (HMODULE)lib.u64[0]; HMODULE mod = (HMODULE)lib.u64[0];
name = push_str8_copy(scratch.arena, name); name = push_str8_copy(scratch.arena, name);
VoidProc *result = (VoidProc*)GetProcAddress(mod, (LPCSTR)name.str); VoidProc *result = (VoidProc*)GetProcAddress(mod, (LPCSTR)name.str);
scratch_end(scratch); scratch_end(scratch);
return(result); return result;
} }
internal void internal void
os_library_close(OS_Handle lib){ os_library_close(OS_Handle lib)
{
HMODULE mod = (HMODULE)lib.u64[0]; HMODULE mod = (HMODULE)lib.u64[0];
FreeLibrary(mod); FreeLibrary(mod);
} }
-3
View File
@@ -82,9 +82,6 @@ struct OS_W32_State
OS_SystemInfo system_info; OS_SystemInfo system_info;
OS_ProcessInfo process_info; OS_ProcessInfo process_info;
// rjf: large pages
B32 large_pages_enabled;
// rjf: entity storage // rjf: entity storage
CRITICAL_SECTION entity_mutex; CRITICAL_SECTION entity_mutex;
Arena *entity_arena; Arena *entity_arena;
+1 -9
View File
@@ -46,13 +46,5 @@
internal void internal void
entry_point(CmdLine *cmdline) entry_point(CmdLine *cmdline)
{ {
Arena *arena = arena_alloc(); printf("Hello, World!\n");
RDIM_SortKey keys_unsorted[] = {{1, (void *)2}, {2}, {3}, {1, (void *)1}, {2}, {3}, {1, (void *)3}, {2}, {3}, {1, (void *)4}, {2}, {3}, {1, (void *)5}, {2}, {3}};
U64 keys_count = ArrayCount(keys_unsorted);
RDIM_SortKey *keys_sorted = rdim_sort_key_array(arena, keys_unsorted, keys_count);
for(U64 idx = 0; idx < keys_count; idx += 1)
{
printf("%I64u (%I64u),", keys_sorted[idx].key, (U64)keys_sorted[idx].val);
}
printf("\n");
} }
+3 -3
View File
@@ -1100,7 +1100,7 @@ static SINTa rr_lzb_simple_encode_fast_sub(rr_lzb_simple_context * fh,
#endif #endif
} }
done: done:;
int cur_lrl = rrPtrDiff32(rpEnd - literals_start); int cur_lrl = rrPtrDiff32(rpEnd - literals_start);
#if LZB_END_WITH_LITERALS #if LZB_END_WITH_LITERALS
@@ -1278,7 +1278,7 @@ static SINTa rr_lzb_simple_encode_veryfast_sub(rr_lzb_simple_context * fh,
} }
//------------------------------- //-------------------------------
found_match: found_match:;
// found something // found something
@@ -1355,7 +1355,7 @@ static SINTa rr_lzb_simple_encode_veryfast_sub(rr_lzb_simple_context * fh,
goto done; goto done;
} }
done: done:;
int cur_lrl = rrPtrDiff32(rpEnd - literals_start); int cur_lrl = rrPtrDiff32(rpEnd - literals_start);
#if LZB_END_WITH_LITERALS #if LZB_END_WITH_LITERALS
+9 -1
View File
@@ -66,6 +66,14 @@ typedef S32 RAD_S32;
# error need force inline for this compiler # error need force inline for this compiler
#endif #endif
#if _MSC_VER
# define RADLZB_TRAP() __debugbreak()
#elif __clang__ || __GNUC__
# define RADLZB_TRAP() __builtin_trap()
#else
# error Unknown trap intrinsic for this compiler.
#endif
#define RR_STRING_JOIN(arg1, arg2) RR_STRING_JOIN_DELAY(arg1, arg2) #define RR_STRING_JOIN(arg1, arg2) RR_STRING_JOIN_DELAY(arg1, arg2)
#define RR_STRING_JOIN_DELAY(arg1, arg2) RR_STRING_JOIN_IMMEDIATE(arg1, arg2) #define RR_STRING_JOIN_DELAY(arg1, arg2) RR_STRING_JOIN_IMMEDIATE(arg1, arg2)
#define RR_STRING_JOIN_IMMEDIATE(arg1, arg2) arg1 ## arg2 #define RR_STRING_JOIN_IMMEDIATE(arg1, arg2) arg1 ## arg2
@@ -91,7 +99,7 @@ typedef S32 RAD_S32;
#define RAD_PTRBYTES 8 #define RAD_PTRBYTES 8
#define RR_MIN(a,b) ( (a) < (b) ? (a) : (b) ) #define RR_MIN(a,b) ( (a) < (b) ? (a) : (b) )
#define RR_MAX(a,b) ( (a) > (b) ? (a) : (b) ) #define RR_MAX(a,b) ( (a) > (b) ? (a) : (b) )
#define RR_ASSERT_ALWAYS(c) do{if(!(c)) {__debugbreak();}}while(0) #define RR_ASSERT_ALWAYS(c) do{if(!(c)) {RADLZB_TRAP();}}while(0)
#define RR_ASSERT(c) RR_ASSERT_ALWAYS(c) #define RR_ASSERT(c) RR_ASSERT_ALWAYS(c)
#define RR_PUT16_LE(ptr,val) *((U16 *)(ptr)) = (U16)(val) #define RR_PUT16_LE(ptr,val) *((U16 *)(ptr)) = (U16)(val)