From 105ee9f4b11921c6dfd8295ec1868a30f398545c Mon Sep 17 00:00:00 2001 From: doylet Date: Thu, 1 Feb 2024 00:03:06 +1100 Subject: [PATCH] Fix compilation for emscripten backend --- dqn_base.h | 9 +++++-- dqn_docs.cpp | 6 +++-- dqn_os.cpp | 2 +- dqn_os_posix.cpp | 11 +++++--- dqn_os_win32.cpp | 10 ++++---- dqn_unit_tests.cpp | 62 ++++++++++++++++++++++++---------------------- 6 files changed, 58 insertions(+), 42 deletions(-) diff --git a/dqn_base.h b/dqn_base.h index 8aadf63..5aec175 100644 --- a/dqn_base.h +++ b/dqn_base.h @@ -415,8 +415,13 @@ struct Dqn_CallSite #else #define Dqn_CPU_TSC() __builtin_readcyclecounter() #endif - #define Dqn_CompilerReadBarrierAndCPUReadFence asm volatile("lfence" ::: "memory") - #define Dqn_CompilerWriteBarrierAndCPUWriteFence asm volatile("sfence" ::: "memory") + #if defined(DQN_PLATFORM_EMSCRIPTEN) + #define Dqn_CompilerReadBarrierAndCPUReadFence + #define Dqn_CompilerWriteBarrierAndCPUWriteFence + #else + #define Dqn_CompilerReadBarrierAndCPUReadFence asm volatile("lfence" ::: "memory") + #define Dqn_CompilerWriteBarrierAndCPUWriteFence asm volatile("sfence" ::: "memory") + #endif #else #error "Compiler not supported" #endif diff --git a/dqn_docs.cpp b/dqn_docs.cpp index 94e9a9f..6d50ff4 100644 --- a/dqn_docs.cpp +++ b/dqn_docs.cpp @@ -496,7 +496,7 @@ void Dqn_Docs_Demo() // Print the result like so if (0) { - printf("%.*s[%u] %zu cycles (%.1fms)\n", + printf("%.*s[%u] %llu cycles (%.1fms)\n", DQN_STR_FMT(anchor->name), anchor->hit_count, anchor->tsc_inclusive, @@ -610,7 +610,7 @@ void Dqn_Docs_Demo() // You may then print out the frame like so if (0) - printf("%.*s(%I64u): %.*s\n", DQN_STR_FMT(frame.file_name), frame.line_number, DQN_STR_FMT(frame.function_name)); + printf("%.*s(%llu): %.*s\n", DQN_STR_FMT(frame.file_name), frame.line_number, DQN_STR_FMT(frame.function_name)); } // If you load new shared-libraries into the address space it maybe @@ -1044,6 +1044,7 @@ void Dqn_Docs_Demo() // NOTE: Dqn_Win_LastError ///////////////////////////////////////////////////////////// // NOTE: Dqn_Win_ErrorCodeToMsg ///////////////////////////////////////////////////////////// + #if defined(DQN_PLATFORM_WIN32) if (0) { // Generate the error string for the last Win32 API called that return // an error value. @@ -1087,5 +1088,6 @@ void Dqn_Docs_Demo() printf("%.*s\n", DQN_STR_FMT(it.file_name)); } } + #endif } DQN_MSVC_WARNING_POP diff --git a/dqn_os.cpp b/dqn_os.cpp index ac1a173..7b28260 100644 --- a/dqn_os.cpp +++ b/dqn_os.cpp @@ -425,7 +425,7 @@ DQN_API void Dqn_OS_ExecOrAbort(Dqn_Slice cmd_line, Dqn_Str8 working_d // NOTE: [$HTTP] Dqn_OSHttp //////////////////////////////////////////////////////////////////////// DQN_API void Dqn_OS_HttpRequestWait(Dqn_OSHttpResponse *response) { - if (response && Dqn_OS_SemaphoreHasData(&response->on_complete_semaphore)) + if (response && Dqn_OS_SemaphoreIsValid(&response->on_complete_semaphore)) Dqn_OS_SemaphoreWait(&response->on_complete_semaphore, DQN_OS_SEMAPHORE_INFINITE_TIMEOUT); } diff --git a/dqn_os_posix.cpp b/dqn_os_posix.cpp index d41b4da..d48cbe9 100644 --- a/dqn_os_posix.cpp +++ b/dqn_os_posix.cpp @@ -119,6 +119,10 @@ DQN_API uint64_t Dqn_OS_DateUnixTime() DQN_API bool Dqn_OS_SecureRNGBytes(void *buffer, uint32_t size) { +#if defined(DQN_PLATFORM_EMSCRIPTEN) + (void)buffer; (void)size; + return false; +#else if (!buffer || size < 0) return false; @@ -135,6 +139,7 @@ DQN_API bool Dqn_OS_SecureRNGBytes(void *buffer, uint32_t size) read_bytes = getrandom(buffer, size, 0); // NOTE: EINTR can not be triggered if size <= 32 bytes } while (read_bytes != size || errno == EAGAIN); return true; +#endif } DQN_API Dqn_Str8 Dqn_OS_EXEPath(Dqn_Arena *arena) @@ -367,7 +372,7 @@ DQN_API Dqn_OSFile Dqn_OS_OpenFile(Dqn_Str8 path, Dqn_OSFileOpen open_mode, uint } if (access & Dqn_OSFileAccess_Execute) { - result.error_size = DQN_CAST(uint16_t) Dqn_SNPrintFDotTruncate( + result.error_size = DQN_CAST(uint16_t) Dqn_FmtBuffer3DotTruncate( result.error, DQN_ARRAY_UCOUNT(result.error), "Open file failed: execute access not supported for \"%.*s\"", @@ -389,7 +394,7 @@ DQN_API Dqn_OSFile Dqn_OS_OpenFile(Dqn_Str8 path, Dqn_OSFileOpen open_mode, uint default: DQN_INVALID_CODE_PATH; break; } if (!handle) { - result.error_size = DQN_CAST(uint16_t)Dqn_SNPrintFDotTruncate( + result.error_size = DQN_CAST(uint16_t)Dqn_FmtBuffer3DotTruncate( result.error, DQN_ARRAY_UCOUNT(result.error), "Open file failed: Could not open file in requested mode %d for \"%.*s\"", @@ -411,7 +416,7 @@ DQN_API Dqn_OSFile Dqn_OS_OpenFile(Dqn_Str8 path, Dqn_OSFileOpen open_mode, uint FILE *handle = fopen(path.data, fopen_mode); if (!handle) { - result.error_size = DQN_CAST(uint16_t) Dqn_SNPrintFDotTruncate( + result.error_size = DQN_CAST(uint16_t) Dqn_FmtBuffer3DotTruncate( result.error, DQN_ARRAY_UCOUNT(result.error), "Open file failed: Could not open file in fopen mode \"%s\" for \"%.*s\"", diff --git a/dqn_os_win32.cpp b/dqn_os_win32.cpp index 2b5c914..40d0d59 100644 --- a/dqn_os_win32.cpp +++ b/dqn_os_win32.cpp @@ -627,7 +627,7 @@ DQN_API Dqn_OSSemaphore Dqn_OS_SemaphoreInit(uint32_t initial_count) return result; } -DQN_API bool Dqn_OS_SemaphoreHasData(Dqn_OSSemaphore *semaphore) +DQN_API bool Dqn_OS_SemaphoreIsValid(Dqn_OSSemaphore *semaphore) { bool result = false; if (semaphore) { @@ -638,7 +638,7 @@ DQN_API bool Dqn_OS_SemaphoreHasData(Dqn_OSSemaphore *semaphore) DQN_API void Dqn_OS_SemaphoreDeinit(Dqn_OSSemaphore *semaphore) { - if (!Dqn_OS_SemaphoreHasData(semaphore)) + if (!Dqn_OS_SemaphoreIsValid(semaphore)) return; CloseHandle(semaphore->win32_handle); *semaphore = {}; @@ -646,7 +646,7 @@ DQN_API void Dqn_OS_SemaphoreDeinit(Dqn_OSSemaphore *semaphore) DQN_API void Dqn_OS_SemaphoreIncrement(Dqn_OSSemaphore *semaphore, uint32_t amount) { - if (!Dqn_OS_SemaphoreHasData(semaphore)) + if (!Dqn_OS_SemaphoreIsValid(semaphore)) return; LONG prev_count = 0; ReleaseSemaphore(DQN_CAST(HANDLE *)semaphore->win32_handle, amount, &prev_count); @@ -655,7 +655,7 @@ DQN_API void Dqn_OS_SemaphoreIncrement(Dqn_OSSemaphore *semaphore, uint32_t amou DQN_API Dqn_OSSemaphoreWaitResult Dqn_OS_SemaphoreWait(Dqn_OSSemaphore *semaphore, uint32_t timeout_ms) { Dqn_OSSemaphoreWaitResult result = {}; - if (!Dqn_OS_SemaphoreHasData(semaphore)) + if (!Dqn_OS_SemaphoreIsValid(semaphore)) return result; if (!semaphore->win32_handle) @@ -965,7 +965,7 @@ DQN_API void Dqn_OS_HttpRequestFree(Dqn_OSHttpResponse *response) response->win32_request_connection = nullptr; response->win32_request_handle = nullptr; Dqn_Arena_Deinit(&response->tmp_arena); - if (Dqn_OS_SemaphoreHasData(&response->on_complete_semaphore)) + if (Dqn_OS_SemaphoreIsValid(&response->on_complete_semaphore)) Dqn_OS_SemaphoreDeinit(&response->on_complete_semaphore); *response = {}; diff --git a/dqn_unit_tests.cpp b/dqn_unit_tests.cpp index 80da22e..430b957 100644 --- a/dqn_unit_tests.cpp +++ b/dqn_unit_tests.cpp @@ -85,7 +85,7 @@ static Dqn_UTest Dqn_Test_Arena() } Dqn_Arena_TempMemEnd(temp_memory); DQN_UTEST_ASSERT (&test, arena.curr->prev == nullptr); - DQN_UTEST_ASSERTF(&test, arena.curr->reserve >= DQN_MEGABYTES(1), "size=%zuMiB (%zuB), expect=%zuB", (arena.curr->reserve / 1024 / 1024), arena.curr->reserve, DQN_MEGABYTES(1)); + DQN_UTEST_ASSERTF(&test, arena.curr->reserve >= DQN_MEGABYTES(1), "size=%lluMiB (%lluB), expect=%lluB", (arena.curr->reserve / 1024 / 1024), arena.curr->reserve, DQN_MEGABYTES(1)); } } return test; @@ -98,57 +98,57 @@ static Dqn_UTest Dqn_Test_Bin() DQN_UTEST_GROUP(test, "Dqn_Bin") { DQN_UTEST_TEST("Convert 0x123") { uint64_t result = Dqn_Bin_HexToU64(DQN_STR8("0x123")); - DQN_UTEST_ASSERTF(&test, result == 0x123, "result: %zu", result); + DQN_UTEST_ASSERTF(&test, result == 0x123, "result: %llu", result); } DQN_UTEST_TEST("Convert 0xFFFF") { uint64_t result = Dqn_Bin_HexToU64(DQN_STR8("0xFFFF")); - DQN_UTEST_ASSERTF(&test, result == 0xFFFF, "result: %zu", result); + DQN_UTEST_ASSERTF(&test, result == 0xFFFF, "result: %llu", result); } DQN_UTEST_TEST("Convert FFFF") { uint64_t result = Dqn_Bin_HexToU64(DQN_STR8("FFFF")); - DQN_UTEST_ASSERTF(&test, result == 0xFFFF, "result: %zu", result); + DQN_UTEST_ASSERTF(&test, result == 0xFFFF, "result: %llu", result); } DQN_UTEST_TEST("Convert abCD") { uint64_t result = Dqn_Bin_HexToU64(DQN_STR8("abCD")); - DQN_UTEST_ASSERTF(&test, result == 0xabCD, "result: %zu", result); + DQN_UTEST_ASSERTF(&test, result == 0xabCD, "result: %llu", result); } DQN_UTEST_TEST("Convert 0xabCD") { uint64_t result = Dqn_Bin_HexToU64(DQN_STR8("0xabCD")); - DQN_UTEST_ASSERTF(&test, result == 0xabCD, "result: %zu", result); + DQN_UTEST_ASSERTF(&test, result == 0xabCD, "result: %llu", result); } DQN_UTEST_TEST("Convert 0x") { uint64_t result = Dqn_Bin_HexToU64(DQN_STR8("0x")); - DQN_UTEST_ASSERTF(&test, result == 0x0, "result: %zu", result); + DQN_UTEST_ASSERTF(&test, result == 0x0, "result: %llu", result); } DQN_UTEST_TEST("Convert 0X") { uint64_t result = Dqn_Bin_HexToU64(DQN_STR8("0X")); - DQN_UTEST_ASSERTF(&test, result == 0x0, "result: %zu", result); + DQN_UTEST_ASSERTF(&test, result == 0x0, "result: %llu", result); } DQN_UTEST_TEST("Convert 3") { uint64_t result = Dqn_Bin_HexToU64(DQN_STR8("3")); - DQN_UTEST_ASSERTF(&test, result == 3, "result: %zu", result); + DQN_UTEST_ASSERTF(&test, result == 3, "result: %llu", result); } DQN_UTEST_TEST("Convert f") { uint64_t result = Dqn_Bin_HexToU64(DQN_STR8("f")); - DQN_UTEST_ASSERTF(&test, result == 0xf, "result: %zu", result); + DQN_UTEST_ASSERTF(&test, result == 0xf, "result: %llu", result); } DQN_UTEST_TEST("Convert g") { uint64_t result = Dqn_Bin_HexToU64(DQN_STR8("g")); - DQN_UTEST_ASSERTF(&test, result == 0, "result: %zu", result); + DQN_UTEST_ASSERTF(&test, result == 0, "result: %llu", result); } DQN_UTEST_TEST("Convert -0x3") { uint64_t result = Dqn_Bin_HexToU64(DQN_STR8("-0x3")); - DQN_UTEST_ASSERTF(&test, result == 0, "result: %zu", result); + DQN_UTEST_ASSERTF(&test, result == 0, "result: %llu", result); } uint32_t number = 0xd095f6; @@ -829,7 +829,7 @@ static Dqn_UTest Dqn_Test_Intrinsics() DQN_UTEST_TEST("Dqn_Atomic_AddU64") { uint64_t val = 0; Dqn_Atomic_AddU64(&val, 1); - DQN_UTEST_ASSERTF(&test, val == 1, "val: %zu", val); + DQN_UTEST_ASSERTF(&test, val == 1, "val: %llu", val); } DQN_UTEST_TEST("Dqn_Atomic_SubU32") { @@ -841,7 +841,7 @@ static Dqn_UTest Dqn_Test_Intrinsics() DQN_UTEST_TEST("Dqn_Atomic_SubU64") { uint64_t val = 1; Dqn_Atomic_SubU64(&val, 1); - DQN_UTEST_ASSERTF(&test, val == 0, "val: %zu", val); + DQN_UTEST_ASSERTF(&test, val == 0, "val: %llu", val); } DQN_UTEST_TEST("Dqn_Atomic_SetValue32") { @@ -855,7 +855,7 @@ static Dqn_UTest Dqn_Test_Intrinsics() int64_t a = 0; int64_t b = 111; Dqn_Atomic_SetValue64(DQN_CAST(uint64_t *)&a, b); - DQN_UTEST_ASSERTF(&test, a == b, "a: %I64i, b: %I64i", a, b); + DQN_UTEST_ASSERTF(&test, a == b, "a: %lld, b: %lld", a, b); } Dqn_UTest_Begin(&test, "Dqn_CPU_TSC"); @@ -1154,7 +1154,7 @@ static Dqn_UTest Dqn_Test_OS() DQN_UTEST_TEST("Consecutive ticks are ordered") { uint64_t a = Dqn_OS_PerfCounterNow(); uint64_t b = Dqn_OS_PerfCounterNow(); - DQN_UTEST_ASSERTF(&test, b >= a, "a: %zu, b: %zu", a, b); + DQN_UTEST_ASSERTF(&test, b >= a, "a: %llu, b: %llu", a, b); } DQN_UTEST_TEST("Ticks to time are a correct order of magnitude") { @@ -1307,7 +1307,7 @@ static Dqn_UTest Dqn_Test_Str8() DQN_UTEST_GROUP(test, "Dqn_Str8") { DQN_UTEST_TEST("Initialise with string literal w/ macro") { Dqn_Str8 string = DQN_STR8("AB"); - DQN_UTEST_ASSERTF(&test, string.size == 2, "size: %I64u", string.size); + DQN_UTEST_ASSERTF(&test, string.size == 2, "size: %zu", string.size); DQN_UTEST_ASSERTF(&test, string.data[0] == 'A', "string[0]: %c", string.data[0]); DQN_UTEST_ASSERTF(&test, string.data[1] == 'B', "string[1]: %c", string.data[1]); } @@ -1315,7 +1315,7 @@ static Dqn_UTest Dqn_Test_Str8() DQN_UTEST_TEST("Initialise with format string") { Dqn_Scratch scratch = Dqn_Scratch_Get(nullptr); Dqn_Str8 string = Dqn_Str8_InitF(scratch.arena, "%s", "AB"); - DQN_UTEST_ASSERTF(&test, string.size == 2, "size: %I64u", string.size); + DQN_UTEST_ASSERTF(&test, string.size == 2, "size: %zu", string.size); DQN_UTEST_ASSERTF(&test, string.data[0] == 'A', "string[0]: %c", string.data[0]); DQN_UTEST_ASSERTF(&test, string.data[1] == 'B', "string[1]: %c", string.data[1]); DQN_UTEST_ASSERTF(&test, string.data[2] == 0, "string[2]: %c", string.data[2]); @@ -1325,7 +1325,7 @@ static Dqn_UTest Dqn_Test_Str8() Dqn_Scratch scratch = Dqn_Scratch_Get(nullptr); Dqn_Str8 string = DQN_STR8("AB"); Dqn_Str8 copy = Dqn_Str8_Copy(scratch.arena, string); - DQN_UTEST_ASSERTF(&test, copy.size == 2, "size: %I64u", copy.size); + DQN_UTEST_ASSERTF(&test, copy.size == 2, "size: %zu", copy.size); DQN_UTEST_ASSERTF(&test, copy.data[0] == 'A', "copy[0]: %c", copy.data[0]); DQN_UTEST_ASSERTF(&test, copy.data[1] == 'B', "copy[1]: %c", copy.data[1]); DQN_UTEST_ASSERTF(&test, copy.data[2] == 0, "copy[2]: %c", copy.data[2]); @@ -1339,7 +1339,7 @@ static Dqn_UTest Dqn_Test_Str8() DQN_UTEST_TEST("Allocate string from arena") { Dqn_Scratch scratch = Dqn_Scratch_Get(nullptr); Dqn_Str8 string = Dqn_Str8_Alloc(scratch.arena, 2, Dqn_ZeroMem_No); - DQN_UTEST_ASSERTF(&test, string.size == 2, "size: %I64u", string.size); + DQN_UTEST_ASSERTF(&test, string.size == 2, "size: %zu", string.size); } // NOTE: Dqn_CStr8_Trim[Prefix/Suffix] @@ -1499,55 +1499,55 @@ static Dqn_UTest Dqn_Test_Str8() DQN_UTEST_TEST("To U64: Convert nullptr") { Dqn_Str8ToU64Result result = Dqn_Str8_ToU64(Dqn_Str8_Init(nullptr, 5), 0); DQN_UTEST_ASSERT(&test, result.success); - DQN_UTEST_ASSERTF(&test, result.value == 0, "result: %I64u", result.value); + DQN_UTEST_ASSERTF(&test, result.value == 0, "result: %llu", result.value); } DQN_UTEST_TEST("To U64: Convert empty string") { Dqn_Str8ToU64Result result = Dqn_Str8_ToU64(DQN_STR8(""), 0); DQN_UTEST_ASSERT(&test, result.success); - DQN_UTEST_ASSERTF(&test, result.value == 0, "result: %I64u", result.value); + DQN_UTEST_ASSERTF(&test, result.value == 0, "result: %llu", result.value); } DQN_UTEST_TEST("To U64: Convert \"1\"") { Dqn_Str8ToU64Result result = Dqn_Str8_ToU64(DQN_STR8("1"), 0); DQN_UTEST_ASSERT(&test, result.success); - DQN_UTEST_ASSERTF(&test, result.value == 1, "result: %I64u", result.value); + DQN_UTEST_ASSERTF(&test, result.value == 1, "result: %llu", result.value); } DQN_UTEST_TEST("To U64: Convert \"-0\"") { Dqn_Str8ToU64Result result = Dqn_Str8_ToU64(DQN_STR8("-0"), 0); DQN_UTEST_ASSERT(&test, !result.success); - DQN_UTEST_ASSERTF(&test, result.value == 0, "result: %I64u", result.value); + DQN_UTEST_ASSERTF(&test, result.value == 0, "result: %llu", result.value); } DQN_UTEST_TEST("To U64: Convert \"-1\"") { Dqn_Str8ToU64Result result = Dqn_Str8_ToU64(DQN_STR8("-1"), 0); DQN_UTEST_ASSERT(&test, !result.success); - DQN_UTEST_ASSERTF(&test, result.value == 0, "result: %I64u", result.value); + DQN_UTEST_ASSERTF(&test, result.value == 0, "result: %llu", result.value); } DQN_UTEST_TEST("To U64: Convert \"1.2\"") { Dqn_Str8ToU64Result result = Dqn_Str8_ToU64(DQN_STR8("1.2"), 0); DQN_UTEST_ASSERT(&test, !result.success); - DQN_UTEST_ASSERTF(&test, result.value == 1, "result: %I64u", result.value); + DQN_UTEST_ASSERTF(&test, result.value == 1, "result: %llu", result.value); } DQN_UTEST_TEST("To U64: Convert \"1,234\"") { Dqn_Str8ToU64Result result = Dqn_Str8_ToU64(DQN_STR8("1,234"), ','); DQN_UTEST_ASSERT(&test, result.success); - DQN_UTEST_ASSERTF(&test, result.value == 1234, "result: %I64u", result.value); + DQN_UTEST_ASSERTF(&test, result.value == 1234, "result: %llu", result.value); } DQN_UTEST_TEST("To U64: Convert \"1,2\"") { Dqn_Str8ToU64Result result = Dqn_Str8_ToU64(DQN_STR8("1,2"), ','); DQN_UTEST_ASSERT(&test, result.success); - DQN_UTEST_ASSERTF(&test, result.value == 12, "result: %I64u", result.value); + DQN_UTEST_ASSERTF(&test, result.value == 12, "result: %llu", result.value); } DQN_UTEST_TEST("To U64: Convert \"12a3\"") { Dqn_Str8ToU64Result result = Dqn_Str8_ToU64(DQN_STR8("12a3"), 0); DQN_UTEST_ASSERT(&test, !result.success); - DQN_UTEST_ASSERTF(&test, result.value == 12, "result: %I64u", result.value); + DQN_UTEST_ASSERTF(&test, result.value == 12, "result: %llu", result.value); } // NOTE: Dqn_Str8_Find @@ -1776,6 +1776,7 @@ static Dqn_UTest Dqn_Test_VArray() return test; } +#if defined(DQN_PLATFORM_WIN32) static Dqn_UTest Dqn_Test_Win() { Dqn_UTest test = {}; @@ -1821,6 +1822,7 @@ static Dqn_UTest Dqn_Test_Win() } return test; } +#endif // DQN_PLATFORM_WIN#@ void Dqn_Test_RunSuite() { @@ -1843,7 +1845,9 @@ void Dqn_Test_RunSuite() Dqn_Test_Str8(), Dqn_Test_TicketMutex(), Dqn_Test_VArray(), + #if defined(DQN_PLATFORM_WIN32) Dqn_Test_Win(), + #endif }; int total_tests = 0;