More fixes

This commit is contained in:
2026-03-08 16:05:02 +11:00
parent a8efa666b4
commit 2ef2e8d1e2
14 changed files with 258 additions and 215 deletions
+13 -18
View File
@@ -30,9 +30,10 @@ DN_API DN_ArenaMemFuncs DN_ArenaMemFuncsGet(DN_ArenaMemFuncType type)
};
case DN_ArenaMemFuncType_VMem: {
DN_Assert(g_dn_->init_flags & DN_InitFlags_OS);
DN_Core *dn = DN_Get();
DN_Assert(dn->init_flags & DN_InitFlags_OS);
result.type = DN_ArenaMemFuncType_VMem;
result.vmem_page_size = g_dn_->os.page_size;
result.vmem_page_size = dn->os.page_size;
result.vmem_reserve = DN_OS_MemReserve;
result.vmem_commit = DN_OS_MemCommit;
result.vmem_release = DN_OS_MemRelease;
@@ -43,8 +44,9 @@ DN_API DN_ArenaMemFuncs DN_ArenaMemFuncsGet(DN_ArenaMemFuncType type)
DN_API DN_ArenaMemFuncs DN_ArenaMemFuncsGetDefaults()
{
DN_Core *dn = DN_Get();
DN_ArenaMemFuncType type = DN_ArenaMemFuncType_Basic;
if (g_dn_->os_init) {
if (dn->os_init) {
#if !defined(DN_PLATFORM_EMSCRIPTEN)
type = DN_ArenaMemFuncType_VMem;
#endif
@@ -55,23 +57,15 @@ DN_API DN_ArenaMemFuncs DN_ArenaMemFuncsGetDefaults()
DN_API DN_Arena DN_ArenaFromHeap(DN_U64 size, DN_ArenaFlags flags)
{
DN_ArenaMemFuncs mem_funcs = {};
mem_funcs.type = DN_ArenaMemFuncType_Basic;
mem_funcs.basic_alloc = DN_ArenaBasicAllocFromOSHeap;
mem_funcs.basic_dealloc = DN_OS_MemDealloc;
DN_Arena result = DN_ArenaFromMemFuncs(size, size, flags, mem_funcs);
DN_ArenaMemFuncs mem_funcs = DN_ArenaMemFuncsGet(DN_ArenaMemFuncType_Basic);
DN_Arena result = DN_ArenaFromMemFuncs(size, size, flags, mem_funcs);
return result;
}
DN_API DN_Arena DN_ArenaFromVMem(DN_U64 reserve, DN_U64 commit, DN_ArenaFlags flags)
{
DN_ArenaMemFuncs mem_funcs = {};
mem_funcs.type = DN_ArenaMemFuncType_VMem;
mem_funcs.vmem_page_size = g_dn_->os.page_size;
mem_funcs.vmem_reserve = DN_OS_MemReserve;
mem_funcs.vmem_commit = DN_OS_MemCommit;
mem_funcs.vmem_release = DN_OS_MemRelease;
DN_Arena result = DN_ArenaFromMemFuncs(reserve, commit, flags, mem_funcs);
DN_ArenaMemFuncs mem_funcs = DN_ArenaMemFuncsGet(DN_ArenaMemFuncType_VMem);
DN_Arena result = DN_ArenaFromMemFuncs(reserve, commit, flags, mem_funcs);
return result;
}
@@ -211,7 +205,8 @@ DN_API void DN_OS_LogPrint(DN_LogTypeParam type, void *user_data, DN_CallSite ca
DN_API void DN_OS_SetLogPrintFuncToOS()
{
DN_LogSetPrintFunc(DN_OS_LogPrint, &g_dn_->os);
DN_Core *dn = DN_Get();
DN_LogSetPrintFunc(DN_OS_LogPrint, &dn->os);
}
// NOTE: Date
@@ -761,7 +756,7 @@ DN_API DN_V2USize DN_OS_ThreadLaneRange(DN_OSThreadLane *lane, DN_USize values_c
if (thread_has_leftovers)
leftovers_before_this_thread_index = lane->index;
else
leftovers_before_this_thread_index = leftovers_before_this_thread_index;
leftovers_before_this_thread_index = rem_values;
DN_USize thread_start_index = (values_per_thread * lane->index) + leftovers_before_this_thread_index;
DN_USize thread_values_count = values_per_thread + (thread_has_leftovers ? 1 : 0);
@@ -1144,7 +1139,7 @@ bool DN_OS_VArrayReserve(DN_VArray<T> *array, DN_USize count)
return false;
DN_USize real_commit = (array->size + count) * sizeof(T);
DN_USize aligned_commit = DN_AlignUpPowerOfTwo(real_commit, g_dn_->os.page_size);
DN_USize aligned_commit = DN_AlignUpPowerOfTwo(real_commit, DN_Get()->os.page_size);
if (array->commit >= aligned_commit)
return true;
bool result = DN_OS_MemCommit(array->data, aligned_commit, DN_MemPage_ReadWrite);
+7 -6
View File
@@ -318,16 +318,17 @@ DN_API DN_U64 DN_OS_PerfCounterFrequency()
return result;
}
static DN_OSPosixCore *DN_OS_GetPOSIXCore_()
static DN_OSPosixCore *DN_OS_PosixGetCore()
{
DN_Assert(g_dn_ && g_dn_->os.platform_context);
DN_OSPosixCore *result = DN_Cast(DN_OSPosixCore *)g_dn_->os.platform_context;
DN_Core *dn = DN_Get();
DN_Assert(dn && dn->os_init);
DN_OSPosixCore *result = DN_Cast(DN_OSPosixCore *)dn->os.platform_context;
return result;
}
DN_API DN_U64 DN_OS_PerfCounterNow()
{
DN_OSPosixCore *posix = DN_OS_GetPOSIXCore_();
DN_OSPosixCore *posix = DN_OS_PosixGetCore();
struct timespec ts;
clock_gettime(posix->clock_monotonic_raw ? CLOCK_MONOTONIC_RAW : CLOCK_MONOTONIC, &ts);
DN_U64 result = DN_Cast(DN_U64) ts.tv_sec * 1'000'000'000 + DN_Cast(DN_U64) ts.tv_nsec;
@@ -1050,7 +1051,7 @@ static DN_U64 DN_OS_PosixSyncPrimitiveToU64(DN_OSPosixSyncPrimitive *primitive)
static DN_OSPosixSyncPrimitive *DN_POSIX_AllocSyncPrimitive_()
{
DN_OSPosixCore *posix = DN_OS_GetPOSIXCore_();
DN_OSPosixCore *posix = DN_OS_PosixGetCore();
DN_OSPosixSyncPrimitive *result = nullptr;
pthread_mutex_lock(&posix->sync_primitive_free_list_mutex);
{
@@ -1070,7 +1071,7 @@ static DN_OSPosixSyncPrimitive *DN_POSIX_AllocSyncPrimitive_()
static void DN_OS_PosixDeallocSyncPrimitive_(DN_OSPosixSyncPrimitive *primitive)
{
if (primitive) {
DN_OSPosixCore *posix = DN_OS_GetPOSIXCore_();
DN_OSPosixCore *posix = DN_OS_PosixGetCore();
pthread_mutex_lock(&posix->sync_primitive_free_list_mutex);
primitive->next = posix->sync_primitive_free_list;
posix->sync_primitive_free_list = primitive;
+19 -21
View File
@@ -41,9 +41,9 @@ DN_API void *DN_OS_MemReserve(DN_USize size, DN_MemCommit commit, DN_U32 page_fl
void *result = VirtualAlloc(nullptr, size, flags, os_page_flags);
if (flags & MEM_COMMIT) {
DN_Assert(g_dn_);
DN_AtomicAddU64(&g_dn_->os.vmem_allocs_total, 1);
DN_AtomicAddU64(&g_dn_->os.vmem_allocs_frame, 1);
DN_Core *dn = DN_Get();
DN_AtomicAddU64(&dn->os.vmem_allocs_total, 1);
DN_AtomicAddU64(&dn->os.vmem_allocs_frame, 1);
}
return result;
}
@@ -55,9 +55,9 @@ DN_API bool DN_OS_MemCommit(void *ptr, DN_USize size, DN_U32 page_flags)
return false;
unsigned long os_page_flags = DN_OS_MemConvertPageToOSFlags_(page_flags);
result = VirtualAlloc(ptr, size, MEM_COMMIT, os_page_flags) != nullptr;
DN_Assert(g_dn_);
DN_AtomicAddU64(&g_dn_->os.vmem_allocs_total, 1);
DN_AtomicAddU64(&g_dn_->os.vmem_allocs_frame, 1);
DN_Core *dn = DN_Get();
DN_AtomicAddU64(&dn->os.vmem_allocs_total, 1);
DN_AtomicAddU64(&dn->os.vmem_allocs_frame, 1);
return result;
}
@@ -83,10 +83,9 @@ DN_API int DN_OS_MemProtect(void *ptr, DN_USize size, DN_U32 page_flags)
if (!ptr || size == 0)
return 0;
static DN_Str8 const ALIGNMENT_ERROR_MSG =
DN_Str8Lit("Page protection requires pointers to be page aligned because we can only guard memory at a multiple of the page boundary.");
DN_AssertF(DN_IsPowerOfTwoAligned(DN_Cast(uintptr_t) ptr, g_dn_->os.page_size), "%s", ALIGNMENT_ERROR_MSG.data);
DN_AssertF(DN_IsPowerOfTwoAligned(size, g_dn_->os.page_size), "%s", ALIGNMENT_ERROR_MSG.data);
static DN_Str8 const ALIGNMENT_ERROR_MSG = DN_Str8Lit("Page protection requires pointers to be page aligned because we can only guard memory at a multiple of the page boundary.");
DN_AssertF(DN_IsPowerOfTwoAligned(DN_Cast(uintptr_t) ptr, DN_Get()->os.page_size), "%s", ALIGNMENT_ERROR_MSG.data);
DN_AssertF(DN_IsPowerOfTwoAligned(size, DN_Get()->os.page_size), "%s", ALIGNMENT_ERROR_MSG.data);
unsigned long os_page_flags = DN_OS_MemConvertPageToOSFlags_(page_flags);
unsigned long prev_flags = 0;
@@ -100,13 +99,13 @@ DN_API int DN_OS_MemProtect(void *ptr, DN_USize size, DN_U32 page_flags)
DN_API void *DN_OS_MemAlloc(DN_USize size, DN_ZMem z_mem)
{
DN_RawAssert(g_dn_->init_flags & DN_InitFlags_OS && "DN must be initialised with the OS flag");
DN_Core *dn = DN_Get();
DN_RawAssert(dn->init_flags & DN_InitFlags_OS && "DN must be initialised with the OS flag");
DN_U32 flags = z_mem == DN_ZMem_Yes ? HEAP_ZERO_MEMORY : 0;
DN_Assert(size <= DN_Cast(DWORD)(-1));
void *result = HeapAlloc(GetProcessHeap(), flags, DN_Cast(DWORD) size);
DN_Assert(g_dn_);
DN_AtomicAddU64(&g_dn_->os.mem_allocs_total, 1);
DN_AtomicAddU64(&g_dn_->os.mem_allocs_frame, 1);
DN_AtomicAddU64(&dn->os.mem_allocs_total, 1);
DN_AtomicAddU64(&dn->os.mem_allocs_frame, 1);
return result;
}
@@ -199,8 +198,7 @@ DN_API DN_U64 DN_OS_DateLocalUnixTimeSFromUnixTimeS(DN_U64 unix_ts_s)
DN_API void DN_OS_GenBytesSecure(void *buffer, DN_U32 size)
{
DN_Assert(g_dn_);
DN_OSW32Core *w32 = DN_Cast(DN_OSW32Core *) g_dn_->os.platform_context;
DN_OSW32Core *w32 = DN_Cast(DN_OSW32Core *) DN_Get()->os.platform_context;
DN_Assert(w32->bcrypt_init_success);
long gen_status = BCryptGenRandom(w32->bcrypt_rng_handle, DN_Cast(unsigned char *) buffer, size, 0 /*flags*/);
@@ -262,8 +260,7 @@ DN_API void DN_OS_SleepMs(DN_UInt milliseconds)
DN_API DN_U64 DN_OS_PerfCounterFrequency()
{
DN_Assert(g_dn_);
DN_OSW32Core *w32 = DN_Cast(DN_OSW32Core *) g_dn_->os.platform_context;
DN_OSW32Core *w32 = DN_Cast(DN_OSW32Core *) DN_Get()->os.platform_context;
DN_Assert(w32->qpc_frequency.QuadPart);
DN_U64 result = w32->qpc_frequency.QuadPart;
return result;
@@ -1008,8 +1005,9 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Str8Slice cmd_line, DN_OSExecArgs
DN_API DN_OSW32Core *DN_OS_W32GetCore()
{
DN_Assert(g_dn_ && g_dn_->os.platform_context);
DN_OSW32Core *result = DN_Cast(DN_OSW32Core *)g_dn_->os.platform_context;
DN_Core *dn = DN_Get();
DN_Assert(dn && dn->os_init);
DN_OSW32Core *result = DN_Cast(DN_OSW32Core *)dn->os.platform_context;
return result;
}
@@ -1039,7 +1037,7 @@ static DN_OSW32SyncPrimitive *DN_OS_W32AllocSyncPrimitive_()
w32->sync_primitive_free_list = w32->sync_primitive_free_list->next;
result->next = nullptr;
} else {
DN_OSCore *os = &g_dn_->os;
DN_OSCore *os = &DN_Get()->os;
result = DN_ArenaNew(&os->arena, DN_OSW32SyncPrimitive, DN_ZMem_Yes);
}
}