Update platform for emscripten builds

This commit is contained in:
doylet 2025-07-14 23:15:14 +10:00
parent 941f61ec34
commit f6f2148888
9 changed files with 100 additions and 36 deletions

View File

@ -1,4 +1,4 @@
// Generated by the DN single header generator 2025-06-30 22:12:04
// Generated by the DN single header generator 2025-07-14 23:14:47
#define DN_BASE_INC_CPP
@ -8,17 +8,24 @@
// DN: Single header generator commented out this header => #include "../dn_clangd.h"
// NOTE: [$INTR] Intrinsics ////////////////////////////////////////////////////////////////////////
DN_CPUFeatureDecl g_dn_cpu_feature_decl[DN_CPUFeature_Count];
#if !defined(DN_PLATFORM_ARM64) && !defined(DN_PLATFORM_EMSCRIPTEN)
#define DN_SUPPORTS_CPU_ID
#endif
#if defined(DN_SUPPORTS_CPU_ID)
#if defined(DN_COMPILER_GCC) || defined(DN_COMPILER_CLANG)
#include <cpuid.h>
#endif
DN_CPUFeatureDecl g_dn_cpu_feature_decl[DN_CPUFeature_Count];
#endif // defined(DN_SUPPORTS_CPU_ID)
DN_API DN_CPUIDResult DN_CPU_ID(DN_CPUIDArgs args)
{
DN_CPUIDResult result = {};
#if defined(DN_SUPPORTS_CPU_ID)
__cpuidex(result.values, args.eax, args.ecx);
#endif
return result;
}
@ -66,6 +73,7 @@ DN_API void DN_CPU_SetFeature(DN_CPUReport *report, DN_CPUFeature feature)
DN_API DN_CPUReport DN_CPU_Report()
{
DN_CPUReport result = {};
#if defined(DN_SUPPORTS_CPU_ID)
DN_CPUIDResult fn_0000_[500] = {};
DN_CPUIDResult fn_8000_[500] = {};
int const EXTENDED_FUNC_BASE_EAX = 0x8000'0000;
@ -206,10 +214,9 @@ DN_API DN_CPUReport DN_CPU_Report()
if (available)
DN_CPU_SetFeature(&result, DN_CAST(DN_CPUFeature) ext_index);
}
#endif // DN_SUPPORTS_CPU_ID
return result;
}
#endif // !defined(DN_PLATFORM_ARM64) && !defined(DN_PLATFORM_EMSCRIPTEN)
// NOTE: DN_TicketMutex ////////////////////////////////////////////////////////////////////////////
DN_API void DN_TicketMutex_Begin(DN_TicketMutex *mutex)
@ -2958,11 +2965,11 @@ DN_API void *DN_Pool_Alloc(DN_Pool *pool, DN_USize size)
DN_USize slot_index = 0;
if (required_size > 32) {
// NOTE: Round up if not PoT as the low bits are set.
DN_USize dist_to_next_msb = DN_CountLeadingZerosU64(required_size) + 1;
DN_USize dist_to_next_msb = DN_CountLeadingZerosUSize(required_size) + 1;
dist_to_next_msb -= DN_CAST(DN_USize)(!DN_IsPowerOfTwo(required_size));
DN_USize const register_size = sizeof(DN_USize) * 8;
DN_Assert(register_size >= dist_to_next_msb + size_to_slot_offset);
DN_AssertF(register_size >= (dist_to_next_msb - size_to_slot_offset), "lhs=%zu, rhs=%zu");
slot_index = register_size - dist_to_next_msb - size_to_slot_offset;
}
@ -2970,8 +2977,8 @@ DN_API void *DN_Pool_Alloc(DN_Pool *pool, DN_USize size)
return result;
DN_USize slot_size_in_bytes = 1ULL << (slot_index + size_to_slot_offset);
DN_Assert(required_size <= (slot_size_in_bytes << 0));
DN_Assert(required_size >= (slot_size_in_bytes >> 1));
DN_AssertF(required_size <= (slot_size_in_bytes << 0), "slot_index=%zu, lhs=%zu, rhs=%zu", slot_index, required_size, (slot_size_in_bytes << 0));
DN_AssertF(required_size >= (slot_size_in_bytes >> 1), "slot_index=%zu, lhs=%zu, rhs=%zu", slot_index, required_size, (slot_size_in_bytes >> 1));
DN_PoolSlot *slot = nullptr;
if (pool->slots[slot_index]) {
@ -4905,8 +4912,12 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args)
os->logical_processor_count = system_info.dwNumberOfProcessors;
os->page_size = system_info.dwPageSize;
os->alloc_granularity = system_info.dwAllocationGranularity;
#else
#if defined(DN_PLATFORM_EMSCRIPTEN)
os->logical_processor_count = 1;
#else
os->logical_processor_count = get_nprocs();
#endif
os->page_size = getpagesize();
os->alloc_granularity = os->page_size;
#endif
@ -4916,10 +4927,15 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args)
DN_OS_EmitLogsWithOSPrintFunctions(os);
{
#if defined(DN_PLATFORM_EMSCRIPTEN)
os->arena = DN_Arena_InitFromOSVMem(DN_Megabytes(1), DN_Kilobytes(4), DN_ArenaFlags_NoAllocTrack);
#else
os->arena = DN_Arena_InitFromOSHeap(DN_Megabytes(1), DN_ArenaFlags_NoAllocTrack);
#endif
#if defined(DN_PLATFORM_WIN32)
os->platform_context = DN_Arena_New(&os->arena, DN_W32Core, DN_ZeroMem_Yes);
#elif defined(DN_PLATFORM_POSIX)
#elif defined(DN_PLATFORM_POSIX) || defined(DN_PLATFORM_EMSCRIPTEN)
os->platform_context = DN_Arena_New(&os->arena, DN_POSIXCore, DN_ZeroMem_Yes);
#endif
@ -6267,7 +6283,7 @@ DN_API DN_Str8 DN_Str8Builder_BuildFromOSHeap(DN_Str8Builder const *builder)
return result;
}
#if defined(DN_PLATFORM_POSIX)
#if defined(DN_PLATFORM_POSIX) || defined(DN_PLATFORM_EMSCRIPTEN)
// DN: Single header generator inlined this file => #include "OS/dn_os_posix.cpp"
#define DN_OS_POSIX_CPP
@ -7278,14 +7294,14 @@ static DN_POSIXCore *DN_OS_GetPOSIXCore_()
static DN_POSIXSyncPrimitive *DN_OS_U64ToPOSIXSyncPrimitive_(DN_U64 u64)
{
DN_POSIXSyncPrimitive *result = nullptr;
DN_Memcpy(&result, &u64, sizeof(u64));
DN_Memcpy(&result, &u64, sizeof(result));
return result;
}
static DN_U64 DN_POSIX_SyncPrimitiveToU64(DN_POSIXSyncPrimitive *primitive)
{
DN_U64 result = 0;
static_assert(sizeof(result) == sizeof(primitive), "Pointer size mis-match");
static_assert(sizeof(result) >= sizeof(primitive), "Pointer size mis-match");
DN_Memcpy(&result, &primitive, sizeof(result));
return result;
}
@ -7563,10 +7579,14 @@ DN_API DN_U32 DN_OS_ThreadID()
DN_API void DN_Posix_ThreadSetName(DN_Str8 name)
{
#if defined(DN_PLATFORM_EMSCRIPTEN)
(void)name;
#else
DN_OSTLSTMem tmem = DN_OS_TLSPushTMem(nullptr);
DN_Str8 copy = DN_Str8_Copy(tmem.arena, name);
pthread_t thread = pthread_self();
pthread_setname_np(thread, (char *)copy.data);
#endif
}
DN_API DN_POSIXProcSelfStatus DN_Posix_ProcSelfStatus()
@ -7758,7 +7778,7 @@ DN_API void DN_OS_HttpRequestAsync(DN_OSHttpResponse *response,
fetch_attribs.onerror = DN_OS_HttpRequestEMFetchOnErrorCallback;
fetch_attribs.userData = response;
DN_Str8 url = DN_Str8_InitF(scratch_arena, "%.*s%.*s", DN_STR_FMT(host), DN_STR_FMT(path));
DN_Str8 url = DN_Str8_InitF(tmem, "%.*s%.*s", DN_STR_FMT(host), DN_STR_FMT(path));
DN_LOG_InfoF("Initiating HTTP '%s' request to '%.*s' with payload '%.*s'",
fetch_attribs.requestMethod,
DN_STR_FMT(url),

View File

@ -1,4 +1,4 @@
// Generated by the DN single header generator 2025-06-30 22:12:04
// Generated by the DN single header generator 2025-07-14 23:14:47
#if !defined(DN_BASE_INC_H)
#define DN_BASE_INC_H
@ -465,12 +465,16 @@ struct DN_CallSite
#define DN_Atomic_SubU64(target, value) DN_Atomic_AddU64(target, (DN_U64) - value)
#define DN_CountLeadingZerosU64(value) __lzcnt64(value)
#define DN_CountLeadingZerosU32(value) __lzcnt(value)
#define DN_CPU_TSC() __rdtsc()
#define DN_CompilerReadBarrierAndCPUReadFence _ReadBarrier(); _mm_lfence()
#define DN_CompilerWriteBarrierAndCPUWriteFence _WriteBarrier(); _mm_sfence()
#elif defined(DN_COMPILER_GCC) || defined(DN_COMPILER_CLANG)
#if defined(__ANDROID__)
#elif defined(DN_PLATFORM_EMSCRIPTEN)
#if !defined(__wasm_simd128__)
#error DN_Base requires -msse2 to be passed to Emscripten
#endif
#include <emmintrin.h>
#else
#include <x86intrin.h>
@ -484,6 +488,8 @@ struct DN_CallSite
#define DN_Atomic_SubU64(target, value) __atomic_fetch_sub(target, value, __ATOMIC_ACQ_REL)
#define DN_CountLeadingZerosU64(value) __builtin_clzll(value)
#define DN_CountLeadingZerosU32(value) __builtin_clzl(value)
#if defined(DN_COMPILER_GCC)
#define DN_CPU_TSC() __rdtsc()
#else
@ -501,6 +507,12 @@ struct DN_CallSite
#error "Compiler not supported"
#endif
#if defined(__aarch64__) || defined(_M_X64) || defined(__x86_64__) || defined(__x86_64)
#define DN_CountLeadingZerosUSize(value) DN_CountLeadingZerosU64(value)
#else
#define DN_CountLeadingZerosUSize(value) DN_CountLeadingZerosU32(value)
#endif
#if !defined(DN_PLATFORM_ARM64)
struct DN_CPURegisters
{
@ -5607,7 +5619,7 @@ DN_API DN_Str8 DN_W32_WorkingDir (DN_Arena *arena, DN_Str8 suffix);
DN_API DN_Str16 DN_W32_WorkingDirW (DN_Arena *arena, DN_Str16 suffix);
DN_API bool DN_W32_DirWIterate (DN_Str16 path, DN_W32FolderIteratorW *it);
#endif // !defined(DN_OS_WIN32)
#elif defined(DN_PLATFORM_POSIX)
#elif defined(DN_PLATFORM_POSIX) || defined(DN_PLATFORM_EMSCRIPTEN)
// DN: Single header generator inlined this file => #include "OS/dn_os_posix.h"
#pragma once

View File

@ -3,17 +3,24 @@
#include "../dn_clangd.h"
// NOTE: [$INTR] Intrinsics ////////////////////////////////////////////////////////////////////////
DN_CPUFeatureDecl g_dn_cpu_feature_decl[DN_CPUFeature_Count];
#if !defined(DN_PLATFORM_ARM64) && !defined(DN_PLATFORM_EMSCRIPTEN)
#define DN_SUPPORTS_CPU_ID
#endif
#if defined(DN_SUPPORTS_CPU_ID)
#if defined(DN_COMPILER_GCC) || defined(DN_COMPILER_CLANG)
#include <cpuid.h>
#endif
DN_CPUFeatureDecl g_dn_cpu_feature_decl[DN_CPUFeature_Count];
#endif // defined(DN_SUPPORTS_CPU_ID)
DN_API DN_CPUIDResult DN_CPU_ID(DN_CPUIDArgs args)
{
DN_CPUIDResult result = {};
#if defined(DN_SUPPORTS_CPU_ID)
__cpuidex(result.values, args.eax, args.ecx);
#endif
return result;
}
@ -61,6 +68,7 @@ DN_API void DN_CPU_SetFeature(DN_CPUReport *report, DN_CPUFeature feature)
DN_API DN_CPUReport DN_CPU_Report()
{
DN_CPUReport result = {};
#if defined(DN_SUPPORTS_CPU_ID)
DN_CPUIDResult fn_0000_[500] = {};
DN_CPUIDResult fn_8000_[500] = {};
int const EXTENDED_FUNC_BASE_EAX = 0x8000'0000;
@ -201,10 +209,9 @@ DN_API DN_CPUReport DN_CPU_Report()
if (available)
DN_CPU_SetFeature(&result, DN_CAST(DN_CPUFeature) ext_index);
}
#endif // DN_SUPPORTS_CPU_ID
return result;
}
#endif // !defined(DN_PLATFORM_ARM64) && !defined(DN_PLATFORM_EMSCRIPTEN)
// NOTE: DN_TicketMutex ////////////////////////////////////////////////////////////////////////////
DN_API void DN_TicketMutex_Begin(DN_TicketMutex *mutex)

View File

@ -255,12 +255,16 @@ struct DN_CallSite
#define DN_Atomic_SubU64(target, value) DN_Atomic_AddU64(target, (DN_U64) - value)
#define DN_CountLeadingZerosU64(value) __lzcnt64(value)
#define DN_CountLeadingZerosU32(value) __lzcnt(value)
#define DN_CPU_TSC() __rdtsc()
#define DN_CompilerReadBarrierAndCPUReadFence _ReadBarrier(); _mm_lfence()
#define DN_CompilerWriteBarrierAndCPUWriteFence _WriteBarrier(); _mm_sfence()
#elif defined(DN_COMPILER_GCC) || defined(DN_COMPILER_CLANG)
#if defined(__ANDROID__)
#elif defined(DN_PLATFORM_EMSCRIPTEN)
#if !defined(__wasm_simd128__)
#error DN_Base requires -msse2 to be passed to Emscripten
#endif
#include <emmintrin.h>
#else
#include <x86intrin.h>
@ -274,6 +278,8 @@ struct DN_CallSite
#define DN_Atomic_SubU64(target, value) __atomic_fetch_sub(target, value, __ATOMIC_ACQ_REL)
#define DN_CountLeadingZerosU64(value) __builtin_clzll(value)
#define DN_CountLeadingZerosU32(value) __builtin_clzl(value)
#if defined(DN_COMPILER_GCC)
#define DN_CPU_TSC() __rdtsc()
#else
@ -291,6 +297,12 @@ struct DN_CallSite
#error "Compiler not supported"
#endif
#if defined(__aarch64__) || defined(_M_X64) || defined(__x86_64__) || defined(__x86_64)
#define DN_CountLeadingZerosUSize(value) DN_CountLeadingZerosU64(value)
#else
#define DN_CountLeadingZerosUSize(value) DN_CountLeadingZerosU32(value)
#endif
#if !defined(DN_PLATFORM_ARM64)
struct DN_CPURegisters
{

View File

@ -421,11 +421,11 @@ DN_API void *DN_Pool_Alloc(DN_Pool *pool, DN_USize size)
DN_USize slot_index = 0;
if (required_size > 32) {
// NOTE: Round up if not PoT as the low bits are set.
DN_USize dist_to_next_msb = DN_CountLeadingZerosU64(required_size) + 1;
DN_USize dist_to_next_msb = DN_CountLeadingZerosUSize(required_size) + 1;
dist_to_next_msb -= DN_CAST(DN_USize)(!DN_IsPowerOfTwo(required_size));
DN_USize const register_size = sizeof(DN_USize) * 8;
DN_Assert(register_size >= dist_to_next_msb + size_to_slot_offset);
DN_AssertF(register_size >= (dist_to_next_msb - size_to_slot_offset), "lhs=%zu, rhs=%zu");
slot_index = register_size - dist_to_next_msb - size_to_slot_offset;
}
@ -433,8 +433,8 @@ DN_API void *DN_Pool_Alloc(DN_Pool *pool, DN_USize size)
return result;
DN_USize slot_size_in_bytes = 1ULL << (slot_index + size_to_slot_offset);
DN_Assert(required_size <= (slot_size_in_bytes << 0));
DN_Assert(required_size >= (slot_size_in_bytes >> 1));
DN_AssertF(required_size <= (slot_size_in_bytes << 0), "slot_index=%zu, lhs=%zu, rhs=%zu", slot_index, required_size, (slot_size_in_bytes << 0));
DN_AssertF(required_size >= (slot_size_in_bytes >> 1), "slot_index=%zu, lhs=%zu, rhs=%zu", slot_index, required_size, (slot_size_in_bytes >> 1));
DN_PoolSlot *slot = nullptr;
if (pool->slots[slot_index]) {

View File

@ -95,8 +95,12 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args)
os->logical_processor_count = system_info.dwNumberOfProcessors;
os->page_size = system_info.dwPageSize;
os->alloc_granularity = system_info.dwAllocationGranularity;
#else
#if defined(DN_PLATFORM_EMSCRIPTEN)
os->logical_processor_count = 1;
#else
os->logical_processor_count = get_nprocs();
#endif
os->page_size = getpagesize();
os->alloc_granularity = os->page_size;
#endif
@ -106,10 +110,15 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args)
DN_OS_EmitLogsWithOSPrintFunctions(os);
{
#if defined(DN_PLATFORM_EMSCRIPTEN)
os->arena = DN_Arena_InitFromOSVMem(DN_Megabytes(1), DN_Kilobytes(4), DN_ArenaFlags_NoAllocTrack);
#else
os->arena = DN_Arena_InitFromOSHeap(DN_Megabytes(1), DN_ArenaFlags_NoAllocTrack);
#endif
#if defined(DN_PLATFORM_WIN32)
os->platform_context = DN_Arena_New(&os->arena, DN_W32Core, DN_ZeroMem_Yes);
#elif defined(DN_PLATFORM_POSIX)
#elif defined(DN_PLATFORM_POSIX) || defined(DN_PLATFORM_EMSCRIPTEN)
os->platform_context = DN_Arena_New(&os->arena, DN_POSIXCore, DN_ZeroMem_Yes);
#endif

View File

@ -1007,14 +1007,14 @@ static DN_POSIXCore *DN_OS_GetPOSIXCore_()
static DN_POSIXSyncPrimitive *DN_OS_U64ToPOSIXSyncPrimitive_(DN_U64 u64)
{
DN_POSIXSyncPrimitive *result = nullptr;
DN_Memcpy(&result, &u64, sizeof(u64));
DN_Memcpy(&result, &u64, sizeof(result));
return result;
}
static DN_U64 DN_POSIX_SyncPrimitiveToU64(DN_POSIXSyncPrimitive *primitive)
{
DN_U64 result = 0;
static_assert(sizeof(result) == sizeof(primitive), "Pointer size mis-match");
static_assert(sizeof(result) >= sizeof(primitive), "Pointer size mis-match");
DN_Memcpy(&result, &primitive, sizeof(result));
return result;
}
@ -1292,10 +1292,14 @@ DN_API DN_U32 DN_OS_ThreadID()
DN_API void DN_Posix_ThreadSetName(DN_Str8 name)
{
#if defined(DN_PLATFORM_EMSCRIPTEN)
(void)name;
#else
DN_OSTLSTMem tmem = DN_OS_TLSPushTMem(nullptr);
DN_Str8 copy = DN_Str8_Copy(tmem.arena, name);
pthread_t thread = pthread_self();
pthread_setname_np(thread, (char *)copy.data);
#endif
}
DN_API DN_POSIXProcSelfStatus DN_Posix_ProcSelfStatus()
@ -1487,7 +1491,7 @@ DN_API void DN_OS_HttpRequestAsync(DN_OSHttpResponse *response,
fetch_attribs.onerror = DN_OS_HttpRequestEMFetchOnErrorCallback;
fetch_attribs.userData = response;
DN_Str8 url = DN_Str8_InitF(scratch_arena, "%.*s%.*s", DN_STR_FMT(host), DN_STR_FMT(path));
DN_Str8 url = DN_Str8_InitF(tmem, "%.*s%.*s", DN_STR_FMT(host), DN_STR_FMT(path));
DN_LOG_InfoF("Initiating HTTP '%s' request to '%.*s' with payload '%.*s'",
fetch_attribs.requestMethod,
DN_STR_FMT(url),

View File

@ -7,7 +7,7 @@
#include "OS/dn_os_print.cpp"
#include "OS/dn_os_string.cpp"
#if defined(DN_PLATFORM_POSIX)
#if defined(DN_PLATFORM_POSIX) || defined(DN_PLATFORM_EMSCRIPTEN)
#include "OS/dn_os_posix.cpp"
#elif defined(DN_PLATFORM_WIN32)
#include "OS/dn_os_w32.cpp"

View File

@ -4,7 +4,7 @@
#if defined(DN_PLATFORM_WIN32)
#include "OS/dn_os_windows.h"
#include "OS/dn_os_w32.h"
#elif defined(DN_PLATFORM_POSIX)
#elif defined(DN_PLATFORM_POSIX) || defined(DN_PLATFORM_EMSCRIPTEN)
#include "OS/dn_os_posix.h"
#else
#error Please define a platform e.g. 'DN_PLATFORM_WIN32' to enable the correct implementation for platform APIs