Cleanup base layer

This commit is contained in:
2025-10-19 22:31:56 +11:00
parent fd7a543e34
commit fff3fe1988
50 changed files with 5056 additions and 4848 deletions
+85 -58
View File
@@ -13,7 +13,7 @@ static DN_OSCore *g_dn_os_core_;
static void DN_OS_LOGEmitFromTypeTypeFV_(DN_LOGTypeParam type, void *user_data, DN_CallSite call_site, DN_FMT_ATTRIB char const *fmt, va_list args)
{
DN_Assert(user_data);
DN_OSCore *core = DN_CAST(DN_OSCore *)user_data;
DN_OSCore *core = DN_Cast(DN_OSCore *)user_data;
// NOTE: Open log file for appending if requested ////////////////////////////////////////////////
DN_TicketMutex_Begin(&core->log_file_mutex);
@@ -71,17 +71,17 @@ static void DN_OS_LOGEmitFromTypeTypeFV_(DN_LOGTypeParam type, void *user_data,
va_copy(args_copy, args);
DN_TicketMutex_Begin(&core->log_file_mutex);
{
DN_OS_FileWrite(&core->log_file, DN_Str8_Init(prefix_buffer, prefix_size.size), nullptr);
DN_OS_FileWriteF(&core->log_file, nullptr, "%*s ", DN_CAST(int)prefix_size.padding, "");
DN_OS_FileWrite(&core->log_file, DN_Str8FromPtr(prefix_buffer, prefix_size.size), nullptr);
DN_OS_FileWriteF(&core->log_file, nullptr, "%*s ", DN_Cast(int)prefix_size.padding, "");
DN_OS_FileWriteFV(&core->log_file, nullptr, fmt, args_copy);
DN_OS_FileWrite(&core->log_file, DN_STR8("\n"), nullptr);
DN_OS_FileWrite(&core->log_file, DN_Str8Lit("\n"), nullptr);
}
DN_TicketMutex_End(&core->log_file_mutex);
va_end(args_copy);
DN_OSPrintDest dest = (type.is_u32_enum && type.u32 == DN_LOGType_Error) ? DN_OSPrintDest_Err : DN_OSPrintDest_Out;
DN_OS_Print(dest, DN_Str8_Init(prefix_buffer, prefix_size.size));
DN_OS_PrintF(dest, "%*s ", DN_CAST(int)prefix_size.padding, "");
DN_OS_Print(dest, DN_Str8FromPtr(prefix_buffer, prefix_size.size));
DN_OS_PrintF(dest, "%*s ", DN_Cast(int)prefix_size.padding, "");
DN_OS_PrintLnFV(dest, fmt, args);
}
@@ -114,24 +114,24 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args)
{
#if defined(DN_PLATFORM_EMSCRIPTEN)
os->arena = DN_Arena_FromHeap(DN_Megabytes(1), DN_ArenaFlags_NoAllocTrack);
os->arena = DN_ArenaFromHeap(DN_Megabytes(1), DN_ArenaFlags_NoAllocTrack);
#else
os->arena = DN_Arena_FromVMem(DN_Megabytes(1), DN_Kilobytes(4), DN_ArenaFlags_NoAllocTrack);
os->arena = DN_ArenaFromVMem(DN_Megabytes(1), DN_Kilobytes(4), DN_ArenaFlags_NoAllocTrack);
#endif
#if defined(DN_PLATFORM_WIN32)
os->platform_context = DN_Arena_New(&os->arena, DN_W32Core, DN_ZeroMem_Yes);
os->platform_context = DN_ArenaNew(&os->arena, DN_W32Core, DN_ZMem_Yes);
#elif defined(DN_PLATFORM_POSIX) || defined(DN_PLATFORM_EMSCRIPTEN)
os->platform_context = DN_Arena_New(&os->arena, DN_POSIXCore, DN_ZeroMem_Yes);
os->platform_context = DN_ArenaNew(&os->arena, DN_POSIXCore, DN_ZMem_Yes);
#endif
#if defined(DN_PLATFORM_WIN32)
DN_W32Core *w32 = DN_CAST(DN_W32Core *) os->platform_context;
DN_W32Core *w32 = DN_Cast(DN_W32Core *) os->platform_context;
InitializeCriticalSection(&w32->sync_primitive_free_list_mutex);
QueryPerformanceFrequency(&w32->qpc_frequency);
HMODULE module = LoadLibraryA("kernel32.dll");
w32->set_thread_description = DN_CAST(DN_W32SetThreadDescriptionFunc *) GetProcAddress(module, "SetThreadDescription");
w32->set_thread_description = DN_Cast(DN_W32SetThreadDescriptionFunc *) GetProcAddress(module, "SetThreadDescription");
FreeLibrary(module);
// NOTE: win32 bcrypt
@@ -142,7 +142,7 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args)
else
DN_LOG_ErrorF("Failed to initialise Windows secure random number generator, error: %d", init_status);
#else
DN_Posix_Init(DN_CAST(DN_POSIXCore *)os->platform_context);
DN_Posix_Init(DN_Cast(DN_POSIXCore *)os->platform_context);
#endif
}
@@ -163,7 +163,7 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args)
DN_OS_TLSSetCurrentThreadTLS(&os->tls);
os->cpu_report = DN_CPUGetReport();
#define DN_CPU_FEAT_XENTRY(label) g_dn_cpu_feature_decl[DN_CPUFeature_##label] = {DN_CPUFeature_##label, DN_STR8(#label)};
#define DN_CPU_FEAT_XENTRY(label) g_dn_cpu_feature_decl[DN_CPUFeature_##label] = {DN_CPUFeature_##label, DN_Str8Lit(#label)};
DN_CPU_FEAT_XMACRO
#undef DN_CPU_FEAT_XENTRY
DN_Assert(g_dn_os_core_);
@@ -182,7 +182,7 @@ DN_API void DN_OS_DumpThreadContextArenaStat(DN_Str8 file_path)
FILE *file = nullptr;
fopen_s(&file, file_path.data, "a+b");
if (file) {
DN_LOG_ErrorF("Failed to dump thread context arenas [file=%.*s]", DN_STR_FMT(file_path));
DN_LOG_ErrorF("Failed to dump thread context arenas [file=%.*s]", DN_Str8PrintFmt(file_path));
return;
}
@@ -223,29 +223,56 @@ DN_API void DN_OS_DumpThreadContextArenaStat(DN_Str8 file_path)
stat.blocks_hwm = DN_Max(stat.blocks_hwm, current->blocks_hwm);
}
DN_ArenaStatStr stats_string = DN_Arena_StatStr(&stat);
DN_ArenaStatStr stats_string = DN_ArenaStatStr(&stat);
fprintf(file, " [ALL] CURR %.*s\n", stats_string.size, stats_string.data);
}
// NOTE: Print individual thread arena data
for (DN_USize index = 0; index < stats_size; index++) {
DN_ArenaStat const *current = stats + index;
DN_ArenaStatStr current_string = DN_Arena_StatStr(current);
fprintf(file, " [%03d] CURR %.*s\n", DN_CAST(int) index, current_string.size, current_string.data);
DN_ArenaStatStr current_string = DN_ArenaStatStr(current);
fprintf(file, " [%03d] CURR %.*s\n", DN_Cast(int) index, current_string.size, current_string.data);
}
fclose(file);
DN_LOG_InfoF("Dumped thread context arenas [file=%.*s]", DN_STR_FMT(file_path));
DN_LOG_InfoF("Dumped thread context arenas [file=%.*s]", DN_Str8PrintFmt(file_path));
#else
(void)file_path;
#endif // #if defined(DN_DEBUG_THREAD_CONTEXT)
}
DN_API DN_Str8 DN_OS_BytesFromHexPtrArenaFrame(void const *hex, DN_USize hex_count)
{
DN_Arena *frame_arena = DN_OS_TLSFrameArena();
DN_Str8 result = DN_BytesFromHexPtrArena(hex, hex_count, frame_arena);
return result;
}
DN_API DN_Str8 DN_OS_BytesFromHexStr8ArenaFrame(DN_Str8 hex)
{
DN_Str8 result = DN_OS_BytesFromHexPtrArenaFrame(hex.data, hex.size);
return result;
}
DN_API DN_Str8 DN_OS_HexFromBytesPtrArenaFrame(void const *bytes, DN_USize bytes_count)
{
DN_Arena *frame_arena = DN_OS_TLSFrameArena();
DN_Str8 result = DN_HexFromBytesPtrArena(bytes, bytes_count, frame_arena);
return result;
}
DN_API DN_Str8 DN_OS_HexFromBytesPtrArenaTLS(void const *bytes, DN_USize bytes_count)
{
DN_Arena *tls_arena = DN_OS_TLSArena();
DN_Str8 result = DN_HexFromBytesPtrArena(bytes, bytes_count, tls_arena);
return result;
}
// NOTE: Date //////////////////////////////////////////////////////////////////////////////////////
DN_API DN_OSDateTimeStr8 DN_OS_DateLocalTimeStr8(DN_OSDateTime time, char date_separator, char hms_separator)
{
DN_OSDateTimeStr8 result = {};
result.hms_size = DN_CAST(uint8_t) DN_SNPrintF(result.hms,
result.hms_size = DN_Cast(uint8_t) DN_SNPrintF(result.hms,
DN_ArrayCountI(result.hms),
"%02hhu%c%02hhu%c%02hhu",
time.hour,
@@ -254,7 +281,7 @@ DN_API DN_OSDateTimeStr8 DN_OS_DateLocalTimeStr8(DN_OSDateTime time, char date_s
hms_separator,
time.seconds);
result.date_size = DN_CAST(uint8_t) DN_SNPrintF(result.date,
result.date_size = DN_Cast(uint8_t) DN_SNPrintF(result.date,
DN_ArrayCountI(result.date),
"%hu%c%02hhu%c%02hhu",
time.year,
@@ -300,9 +327,9 @@ DN_API DN_Str8 DN_OS_EXEDir(DN_Arena *arena)
return result;
DN_OSTLSTMem tmem = DN_OS_TLSTMem(arena);
DN_Str8 exe_path = DN_OS_EXEPath(tmem.arena);
DN_Str8 separators[] = {DN_STR8("/"), DN_STR8("\\")};
DN_Str8BSplitResult split = DN_Str8_BSplitLastArray(exe_path, separators, DN_ArrayCountU(separators));
result = DN_Str8_FromStr8(arena, split.lhs);
DN_Str8 separators[] = {DN_Str8Lit("/"), DN_Str8Lit("\\")};
DN_Str8BSplitResult split = DN_Str8BSplitLastArray(exe_path, separators, DN_ArrayCountU(separators));
result = DN_Str8FromStr8Arena(arena, split.lhs);
return result;
}
@@ -311,7 +338,7 @@ DN_API DN_F64 DN_OS_PerfCounterS(uint64_t begin, uint64_t end)
{
uint64_t frequency = DN_OS_PerfCounterFrequency();
uint64_t ticks = end - begin;
DN_F64 result = ticks / DN_CAST(DN_F64) frequency;
DN_F64 result = ticks / DN_Cast(DN_F64) frequency;
return result;
}
@@ -319,7 +346,7 @@ DN_API DN_F64 DN_OS_PerfCounterMs(uint64_t begin, uint64_t end)
{
uint64_t frequency = DN_OS_PerfCounterFrequency();
uint64_t ticks = end - begin;
DN_F64 result = (ticks * 1'000) / DN_CAST(DN_F64) frequency;
DN_F64 result = (ticks * 1'000) / DN_Cast(DN_F64) frequency;
return result;
}
@@ -327,7 +354,7 @@ DN_API DN_F64 DN_OS_PerfCounterUs(uint64_t begin, uint64_t end)
{
uint64_t frequency = DN_OS_PerfCounterFrequency();
uint64_t ticks = end - begin;
DN_F64 result = (ticks * 1'000'000) / DN_CAST(DN_F64) frequency;
DN_F64 result = (ticks * 1'000'000) / DN_Cast(DN_F64) frequency;
return result;
}
@@ -335,7 +362,7 @@ DN_API DN_F64 DN_OS_PerfCounterNs(uint64_t begin, uint64_t end)
{
uint64_t frequency = DN_OS_PerfCounterFrequency();
uint64_t ticks = end - begin;
DN_F64 result = (ticks * 1'000'000'000) / DN_CAST(DN_F64) frequency;
DN_F64 result = (ticks * 1'000'000'000) / DN_Cast(DN_F64) frequency;
return result;
}
@@ -415,9 +442,9 @@ struct DN_OSFileWriteChunker_
static char *DN_OS_FileWriteChunker_(const char *buf, void *user, int len)
{
DN_OSFileWriteChunker_ *chunker = DN_CAST(DN_OSFileWriteChunker_ *)user;
DN_OSFileWriteChunker_ *chunker = DN_Cast(DN_OSFileWriteChunker_ *)user;
chunker->success = DN_OS_FileWritePtr(chunker->file, buf, len, chunker->err);
char *result = chunker->success ? DN_CAST(char *) buf : nullptr;
char *result = chunker->success ? DN_Cast(char *) buf : nullptr;
return result;
}
@@ -452,24 +479,24 @@ DN_API DN_Str8 DN_OS_FileReadAll(DN_Allocator alloc_type, void *allocator, DN_St
DN_Str8 result = {};
DN_OSPathInfo path_info = DN_OS_PathInfo(path);
if (!path_info.exists) {
DN_OS_ErrSinkAppendF(err, 1, "File does not exist/could not be queried for reading '%.*s'", DN_STR_FMT(path));
DN_OS_ErrSinkAppendF(err, 1, "File does not exist/could not be queried for reading '%.*s'", DN_Str8PrintFmt(path));
return result;
}
// NOTE: Allocate
DN_ArenaTempMem arena_tmp = {};
if (alloc_type == DN_Allocator_Arena) {
DN_Arena *arena = DN_CAST(DN_Arena *) allocator;
arena_tmp = DN_Arena_TempMemBegin(arena);
result = DN_Str8_Alloc(arena, path_info.size, DN_ZeroMem_No);
DN_Arena *arena = DN_Cast(DN_Arena *) allocator;
arena_tmp = DN_ArenaTempMemBegin(arena);
result = DN_Str8FromArena(arena, path_info.size, DN_ZMem_No);
} else {
DN_Pool *pool = DN_CAST(DN_Pool *) allocator;
result = DN_Str8_AllocPool(pool, path_info.size);
DN_Pool *pool = DN_Cast(DN_Pool *) allocator;
result = DN_Str8FromPool(pool, path_info.size);
}
if (!result.data) {
DN_CVTU64Bytes bytes_str = DN_CVT_BytesFromU64Auto(path_info.size);
DN_OS_ErrSinkAppendF(err, 1 /*err_code*/, "Failed to allocate %.1f %.*s for reading file '%.*s'", bytes_str.bytes, DN_STR_FMT(bytes_str.suffix), DN_STR_FMT(path));
DN_Str8x32 bytes_str = DN_ByteCountStr8x32(path_info.size);
DN_OS_ErrSinkAppendF(err, 1 /*err_code*/, "Failed to allocate %.*s for reading file '%.*s'", DN_Str8PrintFmt(bytes_str), DN_Str8PrintFmt(path));
return result;
}
@@ -478,10 +505,10 @@ DN_API DN_Str8 DN_OS_FileReadAll(DN_Allocator alloc_type, void *allocator, DN_St
DN_OSFileRead read = DN_OS_FileRead(&file, result.data, result.size, err);
if (file.error || !read.success) {
if (alloc_type == DN_Allocator_Arena) {
DN_Arena_TempMemEnd(arena_tmp);
DN_ArenaTempMemEnd(arena_tmp);
} else {
DN_Pool *pool = DN_CAST(DN_Pool *) allocator;
DN_Pool_Dealloc(pool, result.data);
DN_Pool *pool = DN_Cast(DN_Pool *) allocator;
DN_PoolDealloc(pool, result.data);
}
result = {};
}
@@ -519,7 +546,7 @@ DN_API bool DN_OS_FileWriteAll(DN_Str8 path, DN_Str8 buffer, DN_OSErrSink *error
DN_API bool DN_OS_FileWriteAllFV(DN_Str8 file_path, DN_OSErrSink *error, DN_FMT_ATTRIB char const *fmt, va_list args)
{
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_Str8 buffer = DN_Str8_FromFV(tmem.arena, fmt, args);
DN_Str8 buffer = DN_Str8FromFmtVArena(tmem.arena, fmt, args);
bool result = DN_OS_FileWriteAll(file_path, buffer, error);
return result;
}
@@ -536,7 +563,7 @@ DN_API bool DN_OS_FileWriteAllF(DN_Str8 file_path, DN_OSErrSink *error, DN_FMT_A
DN_API bool DN_OS_FileWriteAllSafe(DN_Str8 path, DN_Str8 buffer, DN_OSErrSink *error)
{
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_Str8 tmp_path = DN_Str8_FromF(tmem.arena, "%.*s.tmp", DN_STR_FMT(path));
DN_Str8 tmp_path = DN_Str8FromFmtArena(tmem.arena, "%.*s.tmp", DN_Str8PrintFmt(path));
if (!DN_OS_FileWriteAll(tmp_path, buffer, error))
return false;
if (!DN_OS_FileCopy(tmp_path, path, true /*overwrite*/, error))
@@ -549,7 +576,7 @@ DN_API bool DN_OS_FileWriteAllSafe(DN_Str8 path, DN_Str8 buffer, DN_OSErrSink *e
DN_API bool DN_OS_FileWriteAllSafeFV(DN_Str8 path, DN_OSErrSink *error, DN_FMT_ATTRIB char const *fmt, va_list args)
{
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_Str8 buffer = DN_Str8_FromFV(tmem.arena, fmt, args);
DN_Str8 buffer = DN_Str8FromFmtVArena(tmem.arena, fmt, args);
bool result = DN_OS_FileWriteAllSafe(path, buffer, error);
return result;
}
@@ -564,26 +591,26 @@ DN_API bool DN_OS_FileWriteAllSafeF(DN_Str8 path, DN_OSErrSink *error, DN_FMT_AT
DN_API bool DN_OS_PathAddRef(DN_Arena *arena, DN_OSPath *fs_path, DN_Str8 path)
{
if (!arena || !fs_path || !DN_Str8_HasData(path))
if (!arena || !fs_path || path.size == 0)
return false;
if (path.size <= 0)
return true;
DN_Str8 const delimiter_array[] = {
DN_STR8("\\"),
DN_STR8("/")};
DN_Str8Lit("\\"),
DN_Str8Lit("/")};
if (fs_path->links_size == 0)
fs_path->has_prefix_path_separator = (path.data[0] == '/');
for (;;) {
DN_Str8BSplitResult delimiter = DN_Str8_BSplitArray(path, delimiter_array, DN_ArrayCountU(delimiter_array));
for (; delimiter.lhs.data; delimiter = DN_Str8_BSplitArray(delimiter.rhs, delimiter_array, DN_ArrayCountU(delimiter_array))) {
DN_Str8BSplitResult delimiter = DN_Str8BSplitArray(path, delimiter_array, DN_ArrayCountU(delimiter_array));
for (; delimiter.lhs.data; delimiter = DN_Str8BSplitArray(delimiter.rhs, delimiter_array, DN_ArrayCountU(delimiter_array))) {
if (delimiter.lhs.size <= 0)
continue;
DN_OSPathLink *link = DN_Arena_New(arena, DN_OSPathLink, DN_ZeroMem_Yes);
DN_OSPathLink *link = DN_ArenaNew(arena, DN_OSPathLink, DN_ZMem_Yes);
if (!link)
return false;
@@ -619,8 +646,8 @@ DN_API bool DN_OS_PathAddRefFrame(DN_OSPath *fs_path, DN_Str8 path)
DN_API bool DN_OS_PathAdd(DN_Arena *arena, DN_OSPath *fs_path, DN_Str8 path)
{
DN_Str8 copy = DN_Str8_FromStr8(arena, path);
bool result = DN_Str8_HasData(copy) ? true : DN_OS_PathAddRef(arena, fs_path, copy);
DN_Str8 copy = DN_Str8FromStr8Arena(arena, path);
bool result = copy.size ? true : DN_OS_PathAddRef(arena, fs_path, copy);
return result;
}
@@ -628,7 +655,7 @@ DN_API bool DN_OS_PathAddF(DN_Arena *arena, DN_OSPath *fs_path, DN_FMT_ATTRIB ch
{
va_list args;
va_start(args, fmt);
DN_Str8 path = DN_Str8_FromFV(arena, fmt, args);
DN_Str8 path = DN_Str8FromFmtVArena(arena, fmt, args);
va_end(args);
bool result = DN_OS_PathAddRef(arena, fs_path, path);
return result;
@@ -668,7 +695,7 @@ DN_API DN_Str8 DN_OS_PathToF(DN_Arena *arena, DN_Str8 path_separator, DN_FMT_ATT
DN_OSTLSTMem tmem = DN_OS_TLSTMem(arena);
va_list args;
va_start(args, fmt);
DN_Str8 path = DN_Str8_FromFV(tmem.arena, fmt, args);
DN_Str8 path = DN_Str8FromFmtVArena(tmem.arena, fmt, args);
va_end(args);
DN_Str8 result = DN_OS_PathTo(arena, path, path_separator);
return result;
@@ -685,7 +712,7 @@ DN_API DN_Str8 DN_OS_PathF(DN_Arena *arena, DN_FMT_ATTRIB char const *fmt, ...)
DN_OSTLSTMem tmem = DN_OS_TLSTMem(arena);
va_list args;
va_start(args, fmt);
DN_Str8 path = DN_Str8_FromFV(tmem.arena, fmt, args);
DN_Str8 path = DN_Str8FromFmtVArena(tmem.arena, fmt, args);
va_end(args);
DN_Str8 result = DN_OS_Path(arena, path);
return result;
@@ -699,7 +726,7 @@ DN_API DN_Str8 DN_OS_PathBuildWithSeparator(DN_Arena *arena, DN_OSPath const *fs
// NOTE: Each link except the last one needs the path separator appended to it, '/' or '\\'
DN_USize string_size = (fs_path->has_prefix_path_separator ? path_separator.size : 0) + fs_path->string_size + ((fs_path->links_size - 1) * path_separator.size);
result = DN_Str8_Alloc(arena, string_size, DN_ZeroMem_No);
result = DN_Str8FromArena(arena, string_size, DN_ZMem_No);
if (result.data) {
char *dest = result.data;
if (fs_path->has_prefix_path_separator) {
@@ -750,7 +777,7 @@ DN_API DN_OSExecResult DN_OS_ExecOrAbort(DN_Slice<DN_Str8> cmd_line, DN_OSExecAr
// NOTE: DN_OSThread ///////////////////////////////////////////////////////////////////////////////
static void DN_OS_ThreadExecute_(void *user_context)
{
DN_OSThread *thread = DN_CAST(DN_OSThread *) user_context;
DN_OSThread *thread = DN_Cast(DN_OSThread *) user_context;
DN_OS_TLSInit(&thread->tls, thread->tls_init_args);
DN_OS_TLSSetCurrentThreadTLS(&thread->tls);
DN_OS_SemaphoreWait(&thread->init_semaphore, DN_OS_SEMAPHORE_INFINITE_TIMEOUT);
@@ -760,7 +787,7 @@ static void DN_OS_ThreadExecute_(void *user_context)
DN_API void DN_OS_ThreadSetName(DN_Str8 name)
{
DN_OSTLS *tls = DN_OS_TLSGet();
tls->name_size = DN_CAST(uint8_t) DN_Min(name.size, sizeof(tls->name) - 1);
tls->name_size = DN_Cast(uint8_t) DN_Min(name.size, sizeof(tls->name) - 1);
DN_Memcpy(tls->name, name.data, tls->name_size);
tls->name[tls->name_size] = 0;
+11 -6
View File
@@ -124,7 +124,7 @@ enum DN_OSFileAccess_
#else
#define DN_OSPathSeperator "/"
#endif
#define DN_OSPathSeperatorString DN_STR8(DN_OSPathSeperator)
#define DN_OSPathSeperatorString DN_Str8Lit(DN_OSPathSeperator)
#endif
struct DN_OSPathLink
@@ -208,7 +208,7 @@ struct DN_OSConditionVariable
DN_U64 handle;
};
// NOTE: DN_OSThread ///////////////////////////////////////////////////////////////////////////////
// NOTE: DN_OSThread
typedef DN_I32(DN_OSThreadFunc)(struct DN_OSThread *);
struct DN_OSThread
@@ -223,7 +223,7 @@ struct DN_OSThread
DN_OSSemaphore init_semaphore;
};
// NOTE: DN_OSHttp /////////////////////////////////////////////////////////////////////////////////
// NOTE: DN_OSHttp
enum DN_OSHttpRequestSecure
{
DN_OSHttpRequestSecure_No,
@@ -309,13 +309,18 @@ DN_API void DN_OS_Init (D
DN_API void DN_OS_EmitLogsWithOSPrintFunctions (DN_OSCore *os);
DN_API void DN_OS_DumpThreadContextArenaStat (DN_Str8 file_path);
DN_API DN_Str8 DN_OS_BytesFromHexPtrArenaFrame (void const *hex, DN_USize hex_count);
DN_API DN_Str8 DN_OS_BytesFromHexStr8ArenaFrame (DN_Str8 hex);
DN_API DN_Str8 DN_OS_HexFromBytesPtrArenaFrame (void const *bytes, DN_USize bytes_count);
DN_API DN_Str8 DN_OS_HexFromBytesPtrArenaTLS (void const *bytes, DN_USize bytes_count);
DN_API void * DN_OS_MemReserve (DN_USize size, DN_MemCommit commit, DN_MemPage page_flags);
DN_API bool DN_OS_MemCommit (void *ptr, DN_USize size, DN_U32 page_flags);
DN_API void DN_OS_MemDecommit (void *ptr, DN_USize size);
DN_API void DN_OS_MemRelease (void *ptr, DN_USize size);
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_ZeroMem zero_mem);
DN_API void * DN_OS_MemAlloc (DN_USize size, DN_ZMem z_mem);
DN_API void DN_OS_MemDealloc (void *ptr);
DN_API DN_OSDateTime DN_OS_DateLocalTimeNow ();
@@ -409,8 +414,8 @@ DN_API DN_Str8 DN_OS_PathF (D
#define DN_OS_PathFFromTLS(...) DN_OS_PathF(DN_OS_TLSTopArena(), ##__VA_ARGS__)
#define DN_OS_PathFFromFrame(...) DN_OS_PathF(DN_OS_TLSFrameArena(), ##__VA_ARGS__)
#define DN_OS_PathBuildFwdSlash(allocator, fs_path) DN_OS_PathBuildWithSeparator(allocator, fs_path, DN_STR8("/"))
#define DN_OS_PathBuildBackSlash(allocator, fs_path) DN_OS_PathBuildWithSeparator(allocator, fs_path, DN_STR8("\\"))
#define DN_OS_PathBuildFwdSlash(allocator, fs_path) DN_OS_PathBuildWithSeparator(allocator, fs_path, DN_Str8Lit("/"))
#define DN_OS_PathBuildBackSlash(allocator, fs_path) DN_OS_PathBuildWithSeparator(allocator, fs_path, DN_Str8Lit("\\"))
#define DN_OS_PathBuild(allocator, fs_path) DN_OS_PathBuildWithSeparator(allocator, fs_path, DN_OSPathSeparatorString)
DN_API void DN_OS_Exit (int32_t exit_code);
+7 -7
View File
@@ -3,23 +3,23 @@
#include "../dn_base_inc.h"
#include "../dn_os_inc.h"
static void *DN_Arena_BasicAllocFromOSHeap(DN_USize size)
static void *DN_ArenaBasicAllocFromOSHeap(DN_USize size)
{
void *result = DN_OS_MemAlloc(size, DN_ZeroMem_Yes);
void *result = DN_OS_MemAlloc(size, DN_ZMem_Yes);
return result;
}
DN_API DN_Arena DN_Arena_FromHeap(DN_U64 size, DN_ArenaFlags flags)
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_Arena_BasicAllocFromOSHeap;
mem_funcs.basic_alloc = DN_ArenaBasicAllocFromOSHeap;
mem_funcs.basic_dealloc = DN_OS_MemDealloc;
DN_Arena result = DN_Arena_FromMemFuncs(size, size, flags, mem_funcs);
DN_Arena result = DN_ArenaFromMemFuncs(size, size, flags, mem_funcs);
return result;
}
DN_API DN_Arena DN_Arena_FromVMem(DN_U64 reserve, DN_U64 commit, DN_ArenaFlags flags)
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;
@@ -27,7 +27,7 @@ DN_API DN_Arena DN_Arena_FromVMem(DN_U64 reserve, DN_U64 commit, DN_ArenaFlags f
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_Arena_FromMemFuncs(reserve, commit, flags, mem_funcs);
DN_Arena result = DN_ArenaFromMemFuncs(reserve, commit, flags, mem_funcs);
return result;
}
+2 -2
View File
@@ -1,7 +1,7 @@
#if !defined(DN_OS_ALLOCATOR_H)
#define DN_OS_ALLOCATOR_H
DN_API DN_Arena DN_Arena_FromHeap(DN_U64 size, DN_ArenaFlags flags);
DN_API DN_Arena DN_Arena_FromVMem(DN_U64 reserve, DN_U64 commit, DN_ArenaFlags flags);
DN_API DN_Arena DN_ArenaFromHeap(DN_U64 size, DN_ArenaFlags flags);
DN_API DN_Arena DN_ArenaFromVMem(DN_U64 reserve, DN_U64 commit, DN_ArenaFlags flags);
#endif // !defined(DN_OS_ALLOCATOR_H)
+8 -8
View File
@@ -22,7 +22,7 @@ template <typename T>
DN_VArray<T> DN_VArray_InitByteSize(DN_USize byte_size)
{
DN_VArray<T> result = {};
result.data = DN_CAST(T *) DN_OS_MemReserve(byte_size, DN_MemCommit_No, DN_MemPage_ReadWrite);
result.data = DN_Cast(T *) DN_OS_MemReserve(byte_size, DN_MemCommit_No, DN_MemPage_ReadWrite);
if (result.data)
result.max = byte_size / sizeof(T);
return result;
@@ -80,7 +80,7 @@ DN_Slice<T> DN_VArray_Slice(DN_VArray<T> const *array)
template <typename T>
T *DN_VArray_AddArray(DN_VArray<T> *array, T const *items, DN_USize count)
{
T *result = DN_VArray_MakeArray(array, count, DN_ZeroMem_No);
T *result = DN_VArray_MakeArray(array, count, DN_ZMem_No);
if (result)
DN_Memcpy(result, items, count * sizeof(T));
return result;
@@ -101,7 +101,7 @@ T *DN_VArray_Add(DN_VArray<T> *array, T const &item)
}
template <typename T>
T *DN_VArray_MakeArray(DN_VArray<T> *array, DN_USize count, DN_ZeroMem zero_mem)
T *DN_VArray_MakeArray(DN_VArray<T> *array, DN_USize count, DN_ZMem z_mem)
{
if (!DN_VArray_IsValid(array))
return nullptr;
@@ -115,15 +115,15 @@ T *DN_VArray_MakeArray(DN_VArray<T> *array, DN_USize count, DN_ZeroMem zero_mem)
// TODO: Use placement new
T *result = array->data + array->size;
array->size += count;
if (zero_mem == DN_ZeroMem_Yes)
if (z_mem == DN_ZMem_Yes)
DN_Memset(result, 0, count * sizeof(T));
return result;
}
template <typename T>
T *DN_VArray_Make(DN_VArray<T> *array, DN_ZeroMem zero_mem)
T *DN_VArray_Make(DN_VArray<T> *array, DN_ZMem z_mem)
{
T *result = DN_VArray_MakeArray(array, 1, zero_mem);
T *result = DN_VArray_MakeArray(array, 1, z_mem);
return result;
}
@@ -177,10 +177,10 @@ DN_ArrayEraseResult DN_VArray_EraseRange(DN_VArray<T> *array, DN_USize begin_ind
}
template <typename T>
void DN_VArray_Clear(DN_VArray<T> *array, DN_ZeroMem zero_mem)
void DN_VArray_Clear(DN_VArray<T> *array, DN_ZMem z_mem)
{
if (array) {
if (zero_mem == DN_ZeroMem_Yes)
if (z_mem == DN_ZMem_Yes)
DN_Memset(array->data, 0, array->size * sizeof(T));
array->size = 0;
}
+3 -3
View File
@@ -30,8 +30,8 @@ template <typename T> T * DN_VArray_
#define DN_VArray_AddArrayAssert(...) DN_HardAssert(DN_VArray_AddArray(__VA_ARGS__))
#define DN_VArray_AddCArrayAssert(...) DN_HardAssert(DN_VArray_AddCArray(__VA_ARGS__))
#define DN_VArray_AddAssert(...) DN_HardAssert(DN_VArray_Add(__VA_ARGS__))
template <typename T> T * DN_VArray_MakeArray (DN_VArray<T> *array, DN_USize count, DN_ZeroMem zero_mem);
template <typename T> T * DN_VArray_Make (DN_VArray<T> *array, DN_ZeroMem zero_mem);
template <typename T> T * DN_VArray_MakeArray (DN_VArray<T> *array, DN_USize count, DN_ZMem z_mem);
template <typename T> T * DN_VArray_Make (DN_VArray<T> *array, DN_ZMem z_mem);
#define DN_VArray_MakeArrayAssert(...) DN_HardAssert(DN_VArray_MakeArray(__VA_ARGS__))
#define DN_VArray_MakeAssert(...) DN_HardAssert(DN_VArray_Make(__VA_ARGS__))
template <typename T> T * DN_VArray_InsertArray (DN_VArray<T> *array, DN_USize index, T const *items, DN_USize count);
@@ -43,5 +43,5 @@ template <typename T> T * DN_VArray_
template <typename T> T DN_VArray_PopFront (DN_VArray<T> *array, DN_USize count);
template <typename T> T DN_VArray_PopBack (DN_VArray<T> *array, DN_USize count);
template <typename T> DN_ArrayEraseResult DN_VArray_EraseRange (DN_VArray<T> *array, DN_USize begin_index, DN_ISize count, DN_ArrayErase erase);
template <typename T> void DN_VArray_Clear (DN_VArray<T> *array, DN_ZeroMem zero_mem);
template <typename T> void DN_VArray_Clear (DN_VArray<T> *array, DN_ZMem z_mem);
#endif // !defined(DN_OS_CONTAINERS_H)
+108 -108
View File
@@ -65,10 +65,10 @@ 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_STR8(
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_core_->page_size),
DN_AssertF(DN_IsPowerOfTwoAligned(DN_Cast(uintptr_t) ptr, g_dn_os_core_->page_size),
"%s",
ALIGNMENT_ERROR_MSG.data);
DN_AssertF(
@@ -80,9 +80,9 @@ DN_API int DN_OS_MemProtect(void *ptr, DN_USize size, DN_U32 page_flags)
return result;
}
DN_API void *DN_OS_MemAlloc(DN_USize size, DN_ZeroMem zero_mem)
DN_API void *DN_OS_MemAlloc(DN_USize size, DN_ZMem z_mem)
{
void *result = zero_mem == DN_ZeroMem_Yes ? calloc(1, size) : malloc(size);
void *result = z_mem == DN_ZMem_Yes ? calloc(1, size) : malloc(size);
return result;
}
@@ -113,9 +113,9 @@ DN_API DN_OSDateTime DN_OS_DateLocalTimeNow()
result.minutes = time.tm_min;
result.seconds = time.tm_sec;
result.day = DN_CAST(uint8_t) time.tm_mday;
result.month = DN_CAST(uint8_t) time.tm_mon + 1;
result.year = 1900 + DN_CAST(int16_t) time.tm_year;
result.day = DN_Cast(uint8_t) time.tm_mday;
result.month = DN_Cast(uint8_t) time.tm_mon + 1;
result.year = 1900 + DN_Cast(int16_t) time.tm_year;
return result;
}
@@ -150,7 +150,7 @@ DN_API uint64_t DN_OS_DateToUnixTimeS(DN_OSDateTime date)
DN_API DN_OSDateTime DN_OS_DateUnixTimeSToDate(uint64_t time)
{
time_t posix_time = DN_CAST(time_t) time;
time_t posix_time = DN_Cast(time_t) time;
struct tm posix_date = *gmtime(&posix_time);
DN_OSDateTime result = {};
result.year = posix_date.tm_year + 1900;
@@ -196,7 +196,7 @@ DN_API DN_OSDiskSpace DN_OS_DiskSpace(DN_Str8 path)
{
DN_OSTLSTMem tmem = DN_OS_TLSPushTMem(nullptr);
DN_OSDiskSpace result = {};
DN_Str8 path_z_terminated = DN_Str8_FromStr8(tmem.arena, path);
DN_Str8 path_z_terminated = DN_Str8FromStr8(tmem.arena, path);
struct statvfs info = {};
if (statvfs(path_z_terminated.data, &info) != 0)
@@ -217,7 +217,7 @@ DN_API DN_Str8 DN_OS_EXEPath(DN_Arena *arena)
int required_size_wo_null_terminator = 0;
for (int try_size = 128;; try_size *= 2) {
auto scoped_arena = DN_ArenaTempMemScope(arena);
char *try_buf = DN_Arena_NewArray(arena, char, try_size, DN_ZeroMem_No);
char *try_buf = DN_ArenaNewArray(arena, char, try_size, DN_ZMem_No);
int bytes_written = readlink("/proc/self/exe", try_buf, try_size);
if (bytes_written == -1) {
// Failed, we're unable to determine the executable directory
@@ -245,9 +245,9 @@ DN_API DN_Str8 DN_OS_EXEPath(DN_Arena *arena)
}
if (required_size_wo_null_terminator) {
DN_ArenaTempMem temp_mem = DN_Arena_TempMemBegin(arena);
DN_ArenaTempMem temp_mem = DN_ArenaTempMemBegin(arena);
char *exe_path =
DN_Arena_NewArray(arena, char, required_size_wo_null_terminator + 1, DN_ZeroMem_No);
DN_ArenaNewArray(arena, char, required_size_wo_null_terminator + 1, DN_ZMem_No);
exe_path[required_size_wo_null_terminator] = 0;
int bytes_written = readlink("/proc/self/exe", exe_path, required_size_wo_null_terminator);
@@ -255,9 +255,9 @@ DN_API DN_Str8 DN_OS_EXEPath(DN_Arena *arena)
// Note that if read-link fails again can be because there's
// a potential race condition here, our exe or directory could have
// been deleted since the last call, so we need to be careful.
DN_Arena_TempMemEnd(temp_mem);
DN_ArenaTempMemEnd(temp_mem);
} else {
result = DN_Str8_Init(exe_path, required_size_wo_null_terminator);
result = DN_Str8FromPtr(exe_path, required_size_wo_null_terminator);
}
}
return result;
@@ -284,7 +284,7 @@ DN_API DN_U64 DN_OS_PerfCounterFrequency()
static DN_POSIXCore *DN_OS_GetPOSIXCore_()
{
DN_Assert(g_dn_os_core_ && g_dn_os_core_->platform_context);
DN_POSIXCore *result = DN_CAST(DN_POSIXCore *)g_dn_os_core_->platform_context;
DN_POSIXCore *result = DN_Cast(DN_POSIXCore *)g_dn_os_core_->platform_context;
return result;
}
@@ -293,7 +293,7 @@ DN_API DN_U64 DN_OS_PerfCounterNow()
DN_POSIXCore *posix = DN_OS_GetPOSIXCore_();
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;
DN_U64 result = DN_Cast(DN_U64) ts.tv_sec * 1'000'000'000 + DN_Cast(DN_U64) ts.tv_nsec;
return result;
}
@@ -309,7 +309,7 @@ DN_API bool DN_OS_FileCopy(DN_Str8 src, DN_Str8 dest, bool overwrite, DN_OSErrSi
DN_OS_ErrSinkAppendF(error,
error_code,
"Failed to open file '%.*s' for copying: (%d) %s",
DN_STR_FMT(src),
DN_Str8PrintFmt(src),
error_code,
strerror(error_code));
return result;
@@ -326,7 +326,7 @@ DN_API bool DN_OS_FileCopy(DN_Str8 src, DN_Str8 dest, bool overwrite, DN_OSErrSi
DN_OS_ErrSinkAppendF(error,
error_code,
"Failed to open file destination '%.*s' for copying to: (%d) %s",
DN_STR_FMT(src),
DN_Str8PrintFmt(src),
error_code,
strerror(error_code));
return result;
@@ -343,7 +343,7 @@ DN_API bool DN_OS_FileCopy(DN_Str8 src, DN_Str8 dest, bool overwrite, DN_OSErrSi
DN_OS_ErrSinkAppendF(error,
error_code,
"Failed to query file size of '%.*s' for copying: (%d) %s",
DN_STR_FMT(src),
DN_Str8PrintFmt(src),
error_code,
strerror(error_code));
return result;
@@ -354,16 +354,16 @@ DN_API bool DN_OS_FileCopy(DN_Str8 src, DN_Str8 dest, bool overwrite, DN_OSErrSi
if (!result) {
int error_code = errno;
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_Str8 file_size_str8 = DN_CVT_BytesStr8FromU64(tmem.arena, stat_existing.st_size, DN_CVTBytesType_Auto);
DN_Str8 bytes_written_str8 = DN_CVT_BytesStr8FromU64(tmem.arena, bytes_written, DN_CVTBytesType_Auto);
DN_Str8 file_size_str8 = DN_Str8FromByteCount(tmem.arena, stat_existing.st_size, DN_ByteCountType_Auto);
DN_Str8 bytes_written_str8 = DN_Str8FromByteCount(tmem.arena, bytes_written, DN_ByteCountType_Auto);
DN_OS_ErrSinkAppendF(error,
error_code,
"Failed to copy file '%.*s' to '%.*s', we copied %.*s but the file "
"size is %.*s: (%d) %s",
DN_STR_FMT(src),
DN_STR_FMT(dest),
DN_STR_FMT(bytes_written_str8),
DN_STR_FMT(file_size_str8),
DN_Str8PrintFmt(src),
DN_Str8PrintFmt(dest),
DN_Str8PrintFmt(bytes_written_str8),
DN_Str8PrintFmt(file_size_str8),
error_code,
strerror(error_code));
}
@@ -392,7 +392,7 @@ DN_API bool DN_OS_FileMove(DN_Str8 src, DN_Str8 dest, bool overwrite, DN_OSErrSi
error,
error_code,
"File '%.*s' was moved but failed to be unlinked from old location: (%d) %s",
DN_STR_FMT(src),
DN_Str8PrintFmt(src),
error_code,
strerror(error_code));
}
@@ -406,7 +406,7 @@ DN_API DN_OSFile DN_OS_FileOpen(DN_Str8 path,
DN_OSErrSink *error)
{
DN_OSFile result = {};
if (!DN_Str8_HasData(path) || path.size <= 0)
if (path.size == 0 || path.size <= 0)
return result;
if ((access & ~(DN_OSFileAccess_All) || ((access & DN_OSFileAccess_All) == 0))) {
@@ -420,7 +420,7 @@ DN_API DN_OSFile DN_OS_FileOpen(DN_Str8 path,
error,
1,
"Failed to open file '%.*s': File access flag 'execute' is not supported",
DN_STR_FMT(path));
DN_Str8PrintFmt(path));
DN_InvalidCodePath; // TODO: Not supported via fopen
return result;
}
@@ -444,7 +444,7 @@ DN_API DN_OSFile DN_OS_FileOpen(DN_Str8 path,
1,
"Failed to open file '%.*s': File could not be opened in requested "
"mode 'DN_OSFileOpen' flag %d",
DN_STR_FMT(path),
DN_Str8PrintFmt(path),
open_mode);
return result;
}
@@ -480,10 +480,10 @@ DN_API DN_OSFileRead DN_OS_FileRead(DN_OSFile *file, void *buffer, DN_USize size
if (!file || !file->handle || file->error || !buffer || size <= 0)
return result;
result.bytes_read = fread(buffer, 1, size, DN_CAST(FILE *) file->handle);
if (feof(DN_CAST(FILE*)file->handle)) {
result.bytes_read = fread(buffer, 1, size, DN_Cast(FILE *) file->handle);
if (feof(DN_Cast(FILE*)file->handle)) {
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_Str8 buffer_size_str8 = DN_CVT_BytesStr8FromU64AutoTLS(size);
DN_Str8 buffer_size_str8 = DN_ByteCountStr8x32TLS(size);
DN_OS_ErrSinkAppendF(err, 1, "Failed to read %S from file", buffer_size_str8);
return result;
}
@@ -497,12 +497,12 @@ DN_API bool DN_OS_FileWritePtr(DN_OSFile *file, void const *buffer, DN_USize siz
if (!file || !file->handle || file->error || !buffer || size <= 0)
return false;
bool result =
fwrite(buffer, DN_CAST(DN_USize) size, 1 /*count*/, DN_CAST(FILE *) file->handle) ==
fwrite(buffer, DN_Cast(DN_USize) size, 1 /*count*/, DN_Cast(FILE *) file->handle) ==
1 /*count*/;
if (!result) {
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_Str8 buffer_size_str8 = DN_CVT_BytesStr8FromU64AutoTLS(size);
DN_OS_ErrSinkAppendF(err, 1, "Failed to write buffer (%s) to file handle", DN_STR_FMT(buffer_size_str8));
DN_Str8 buffer_size_str8 = DN_ByteCountStr8x32TLS(size);
DN_OS_ErrSinkAppendF(err, 1, "Failed to write buffer (%s) to file handle", DN_Str8PrintFmt(buffer_size_str8));
}
return result;
}
@@ -510,7 +510,7 @@ DN_API bool DN_OS_FileWritePtr(DN_OSFile *file, void const *buffer, DN_USize siz
DN_API bool DN_OS_FileFlush(DN_OSFile *file, DN_OSErrSink *err)
{
// TODO: errno is not thread safe
int fd = fileno(DN_CAST(FILE *) file->handle);
int fd = fileno(DN_Cast(FILE *) file->handle);
if (fd == -1) {
DN_OS_ErrSinkAppendF(err, errno, "Failed to flush file buffer to disk, file handle could not be converted to descriptor (%d): %s", fd, strerror(errno));
return false;
@@ -528,14 +528,14 @@ DN_API void DN_OS_FileClose(DN_OSFile *file)
{
if (!file || !file->handle || file->error)
return;
fclose(DN_CAST(FILE *) file->handle);
fclose(DN_Cast(FILE *) file->handle);
*file = {};
}
DN_API DN_OSPathInfo DN_OS_PathInfo(DN_Str8 path)
{
DN_OSPathInfo result = {};
if (!DN_Str8_HasData(path))
if (path.size == 0)
return result;
struct stat file_stat;
@@ -559,7 +559,7 @@ DN_API DN_OSPathInfo DN_OS_PathInfo(DN_Str8 path)
DN_API bool DN_OS_PathDelete(DN_Str8 path)
{
bool result = false;
if (DN_Str8_HasData(path))
if (path.size)
result = remove(path.data) == 0;
return result;
}
@@ -567,7 +567,7 @@ DN_API bool DN_OS_PathDelete(DN_Str8 path)
DN_API bool DN_OS_PathIsFile(DN_Str8 path)
{
bool result = false;
if (!DN_Str8_HasData(path))
if (path.size == 0)
return result;
struct stat stat_result;
@@ -579,7 +579,7 @@ DN_API bool DN_OS_PathIsFile(DN_Str8 path)
DN_API bool DN_OS_PathIsDir(DN_Str8 path)
{
bool result = false;
if (!DN_Str8_HasData(path))
if (path.size == 0)
return result;
struct stat stat_result;
@@ -598,7 +598,7 @@ DN_API bool DN_OS_PathMakeDir(DN_Str8 path)
DN_USize path_indexes_size = 0;
uint16_t path_indexes[64] = {};
DN_Str8 copy = DN_Str8_FromStr8(tmem.arena, path);
DN_Str8 copy = DN_Str8FromStr8(tmem.arena, path);
for (DN_USize index = copy.size - 1; index < copy.size; index--) {
bool first_char = index == (copy.size - 1);
char ch = copy.data[index];
@@ -626,7 +626,7 @@ DN_API bool DN_OS_PathMakeDir(DN_Str8 path)
} else {
// NOTE: There's nothing that exists at this path, we can
// create a directory here
path_indexes[path_indexes_size++] = DN_CAST(uint16_t) index;
path_indexes[path_indexes_size++] = DN_Cast(uint16_t) index;
}
}
}
@@ -654,23 +654,23 @@ DN_API bool DN_OS_PathIterateDir(DN_Str8 path, DN_OSDirIterator *it)
struct dirent *entry;
for (;;) {
entry = readdir(DN_CAST(DIR *) it->handle);
entry = readdir(DN_Cast(DIR *) it->handle);
if (entry == NULL)
break;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
DN_USize name_size = DN_CStr8_Size(entry->d_name);
DN_USize name_size = DN_CStrSize(entry->d_name);
DN_USize clamped_size = DN_Min(sizeof(it->buffer) - 1, name_size);
DN_AssertF(name_size == clamped_size, "name: %s, name_size: %zu, clamped_size: %zu", entry->d_name, name_size, clamped_size);
DN_Memcpy(it->buffer, entry->d_name, clamped_size);
it->buffer[clamped_size] = 0;
it->file_name = DN_Str8_Init(it->buffer, clamped_size);
it->file_name = DN_Str8FromPtr(it->buffer, clamped_size);
return true;
}
closedir(DN_CAST(DIR *) it->handle);
closedir(DN_Cast(DIR *) it->handle);
it->handle = NULL;
it->file_name = {};
it->buffer[0] = 0;
@@ -679,7 +679,7 @@ DN_API bool DN_OS_PathIterateDir(DN_Str8 path, DN_OSDirIterator *it)
DN_API void DN_OS_Exit(int32_t exit_code)
{
exit(DN_CAST(int) exit_code);
exit(DN_Cast(int) exit_code);
}
enum DN_OSPipeType_
@@ -757,30 +757,30 @@ DN_API DN_OSExecResult DN_OS_ExecWait(DN_OSExecAsyncHandle handle,
DN_OSTLSTMem tmem = DN_OS_TLSTMem(arena);
if (arena && handle.stdout_read) {
char buffer[4096];
DN_Str8Builder builder = DN_Str8Builder_FromArena(tmem.arena);
DN_Str8Builder builder = DN_Str8BuilderFromArena(tmem.arena);
for (;;) {
ssize_t bytes_read =
read(stdout_pipe[DN_OSPipeType__Read], buffer, sizeof(buffer));
if (bytes_read <= 0)
break;
DN_Str8Builder_AppendF(&builder, "%.*s", bytes_read, buffer);
DN_Str8BuilderAppendF(&builder, "%.*s", bytes_read, buffer);
}
result.stdout_text = DN_Str8Builder_Build(&builder, arena);
result.stdout_text = DN_Str8BuilderBuild(&builder, arena);
}
if (arena && handle.stderr_read) {
char buffer[4096];
DN_Str8Builder builder = DN_Str8Builder_FromArena(tmem.arena);
DN_Str8Builder builder = DN_Str8BuilderFromArena(tmem.arena);
for (;;) {
ssize_t bytes_read =
read(stderr_pipe[DN_OSPipeType__Read], buffer, sizeof(buffer));
if (bytes_read <= 0)
break;
DN_Str8Builder_AppendF(&builder, "%.*s", bytes_read, buffer);
DN_Str8BuilderAppendF(&builder, "%.*s", bytes_read, buffer);
}
result.stderr_text = DN_Str8Builder_Build(&builder, arena);
result.stderr_text = DN_Str8BuilderBuild(&builder, arena);
}
}
@@ -803,7 +803,7 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line,
return result;
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_Str8 cmd_rendered = DN_Slice_Str8Render(tmem.arena, cmd_line, DN_STR8(" "));
DN_Str8 cmd_rendered = DN_Slice_Str8Render(tmem.arena, cmd_line, DN_Str8Lit(" "));
int stdout_pipe[DN_OSPipeType__Count] = {};
int stderr_pipe[DN_OSPipeType__Count] = {};
@@ -815,7 +815,7 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line,
error,
result.os_error_code,
"Failed to create stdout pipe to redirect the output of the command '%.*s': %s",
DN_STR_FMT(cmd_rendered),
DN_Str8PrintFmt(cmd_rendered),
strerror(result.os_error_code));
return result;
}
@@ -842,7 +842,7 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line,
error,
result.os_error_code,
"Failed to create stderr pipe to redirect the output of the command '%.*s': %s",
DN_STR_FMT(cmd_rendered),
DN_Str8PrintFmt(cmd_rendered),
strerror(result.os_error_code));
return result;
}
@@ -865,7 +865,7 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line,
error,
result.os_error_code,
"Failed to fork process to execute the command '%.*s': %s",
DN_STR_FMT(cmd_rendered),
DN_Str8PrintFmt(cmd_rendered),
strerror(result.os_error_code));
return result;
}
@@ -878,7 +878,7 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line,
error,
result.os_error_code,
"Failed to redirect stdout 'write' pipe for output of command '%.*s': %s",
DN_STR_FMT(cmd_rendered),
DN_Str8PrintFmt(cmd_rendered),
strerror(result.os_error_code));
return result;
}
@@ -890,27 +890,27 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line,
error,
result.os_error_code,
"Failed to redirect stderr 'read' pipe for output of command '%.*s': %s",
DN_STR_FMT(cmd_rendered),
DN_Str8PrintFmt(cmd_rendered),
strerror(result.os_error_code));
return result;
}
// NOTE: Convert the command into something suitable for execvp
char **argv =
DN_Arena_NewArray(tmem.arena, char *, cmd_line.size + 1 /*null*/, DN_ZeroMem_Yes);
DN_ArenaNewArray(tmem.arena, char *, cmd_line.size + 1 /*null*/, DN_ZMem_Yes);
if (!argv) {
result.exit_code = -1;
DN_OS_ErrSinkAppendF(
error,
result.os_error_code,
"Failed to create argument values from command line '%.*s': Out of memory",
DN_STR_FMT(cmd_rendered));
DN_Str8PrintFmt(cmd_rendered));
return result;
}
for (DN_ForIndexU(arg_index, cmd_line.size)) {
DN_Str8 arg = cmd_line.data[arg_index];
argv[arg_index] = DN_Str8_FromStr8(tmem.arena, arg).data; // NOTE: Copy string to guarantee it is null-terminated
argv[arg_index] = DN_Str8FromStr8(tmem.arena, arg).data; // NOTE: Copy string to guarantee it is null-terminated
}
// NOTE: Change the working directory if there is one
@@ -928,14 +928,14 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line,
if (args->working_dir.size) {
prev_working_dir = get_current_dir_name();
DN_Str8 working_dir = DN_Str8_FromStr8(tmem.arena, args->working_dir);
DN_Str8 working_dir = DN_Str8FromStr8(tmem.arena, args->working_dir);
if (chdir(working_dir.data) == -1) {
result.os_error_code = errno;
DN_OS_ErrSinkAppendF(
error,
result.os_error_code,
"Failed to create argument values from command line '%.*s': %s",
DN_STR_FMT(cmd_rendered),
DN_Str8PrintFmt(cmd_rendered),
strerror(result.os_error_code));
return result;
}
@@ -949,7 +949,7 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line,
error,
result.os_error_code,
"Failed to execute command'%.*s': %s",
DN_STR_FMT(cmd_rendered),
DN_Str8PrintFmt(cmd_rendered),
strerror(result.os_error_code));
return result;
}
@@ -1016,7 +1016,7 @@ static DN_POSIXSyncPrimitive *DN_POSIX_AllocSyncPrimitive_()
result->next = nullptr;
} else {
DN_OSCore *os = g_dn_os_core_;
result = DN_Arena_New(&os->arena, DN_POSIXSyncPrimitive, DN_ZeroMem_Yes);
result = DN_ArenaNew(&os->arena, DN_POSIXSyncPrimitive, DN_ZMem_Yes);
}
}
pthread_mutex_unlock(&posix->sync_primitive_free_list_mutex);
@@ -1272,7 +1272,7 @@ DN_API DN_U32 DN_OS_ThreadID()
{
pid_t result = gettid();
DN_Assert(gettid() >= 0);
return DN_CAST(DN_U32) result;
return DN_Cast(DN_U32) result;
}
DN_API void DN_Posix_Init(DN_POSIXCore *posix)
@@ -1294,7 +1294,7 @@ DN_API void DN_Posix_ThreadSetName(DN_Str8 name)
(void)name;
#else
DN_OSTLSTMem tmem = DN_OS_TLSPushTMem(nullptr);
DN_Str8 copy = DN_Str8_FromStr8(tmem.arena, name);
DN_Str8 copy = DN_Str8FromStr8(tmem.arena, name);
pthread_t thread = pthread_self();
pthread_setname_np(thread, (char *)copy.data);
#endif
@@ -1313,49 +1313,49 @@ DN_API DN_POSIXProcSelfStatus DN_Posix_ProcSelfStatus()
// ...
//
// VmSize is the total virtual memory used
DN_OSFile file = DN_OS_FileOpen(DN_STR8("/proc/self/status"), DN_OSFileOpen_OpenIfExist, DN_OSFileAccess_Read, nullptr);
DN_OSFile file = DN_OS_FileOpen(DN_Str8Lit("/proc/self/status"), DN_OSFileOpen_OpenIfExist, DN_OSFileAccess_Read, nullptr);
DN_OSTLSTMem tmem = DN_OS_TLSPushTMem(nullptr);
if (!file.error) {
char buf[256];
DN_Str8Builder builder = DN_Str8Builder_FromTLS();
DN_Str8Builder builder = DN_Str8BuilderFromTLS();
for (;;) {
DN_OSFileRead read = DN_OS_FileRead(&file, buf, sizeof(buf), nullptr);
if (!read.success || read.bytes_read == 0)
break;
DN_Str8Builder_AppendF(&builder, "%.*s", DN_CAST(int)read.bytes_read, buf);
DN_Str8BuilderAppendF(&builder, "%.*s", DN_Cast(int)read.bytes_read, buf);
}
DN_Str8 const NAME = DN_STR8("Name:");
DN_Str8 const PID = DN_STR8("Pid:");
DN_Str8 const VM_PEAK = DN_STR8("VmPeak:");
DN_Str8 const VM_SIZE = DN_STR8("VmSize:");
DN_Str8 status_buf = DN_Str8Builder_BuildFromTLS(&builder);
DN_Slice<DN_Str8> lines = DN_Str8_SplitFromTLS(status_buf, DN_STR8("\n"), DN_Str8SplitIncludeEmptyStrings_No);
DN_Str8 const NAME = DN_Str8Lit("Name:");
DN_Str8 const PID = DN_Str8Lit("Pid:");
DN_Str8 const VM_PEAK = DN_Str8Lit("VmPeak:");
DN_Str8 const VM_SIZE = DN_Str8Lit("VmSize:");
DN_Str8 status_buf = DN_Str8BuilderBuildFromTLS(&builder);
DN_Slice<DN_Str8> lines = DN_Str8SplitFromTLS(status_buf, DN_Str8Lit("\n"), DN_Str8SplitIncludeEmptyStrings_No);
for (DN_ForIt(line_it, DN_Str8, &lines)) {
DN_Str8 line = DN_Str8_TrimWhitespaceAround(*line_it.data);
if (DN_Str8_StartsWith(line, NAME, DN_Str8EqCase_Insensitive)) {
DN_Str8 str8 = DN_Str8_TrimWhitespaceAround(DN_Str8_Slice(line, NAME.size, line.size));
DN_Str8 line = DN_Str8TrimWhitespaceAround(*line_it.data);
if (DN_Str8StartsWith(line, NAME, DN_Str8EqCase_Insensitive)) {
DN_Str8 str8 = DN_Str8TrimWhitespaceAround(DN_Str8Slice(line, NAME.size, line.size));
result.name_size = DN_Min(str8.size, sizeof(result.name));
DN_Memcpy(result.name, str8.data, result.name_size);
} else if (DN_Str8_StartsWith(line, PID, DN_Str8EqCase_Insensitive)) {
DN_Str8 str8 = DN_Str8_TrimWhitespaceAround(DN_Str8_Slice(line, PID.size, line.size));
DN_Str8ToU64Result to_u64 = DN_Str8_ToU64(str8, 0);
} else if (DN_Str8StartsWith(line, PID, DN_Str8EqCase_Insensitive)) {
DN_Str8 str8 = DN_Str8TrimWhitespaceAround(DN_Str8Slice(line, PID.size, line.size));
DN_Str8ToU64Result to_u64 = DN_Str8ToU64(str8, 0);
result.pid = to_u64.value;
DN_Assert(to_u64.success);
} else if (DN_Str8_StartsWith(line, VM_SIZE, DN_Str8EqCase_Insensitive)) {
DN_Str8 size_with_kb = DN_Str8_TrimWhitespaceAround(DN_Str8_Slice(line, VM_SIZE.size, line.size));
DN_Assert(DN_Str8_EndsWith(size_with_kb, DN_STR8("kB")));
DN_Str8 vm_size = DN_Str8_BSplit(size_with_kb, DN_STR8(" ")).lhs;
DN_Str8ToU64Result to_u64 = DN_Str8_ToU64(vm_size, 0);
} else if (DN_Str8StartsWith(line, VM_SIZE, DN_Str8EqCase_Insensitive)) {
DN_Str8 size_with_kb = DN_Str8TrimWhitespaceAround(DN_Str8Slice(line, VM_SIZE.size, line.size));
DN_Assert(DN_Str8EndsWith(size_with_kb, DN_Str8Lit("kB")));
DN_Str8 vm_size = DN_Str8BSplit(size_with_kb, DN_Str8Lit(" ")).lhs;
DN_Str8ToU64Result to_u64 = DN_Str8ToU64(vm_size, 0);
result.vm_size = DN_Kilobytes(to_u64.value);
DN_Assert(to_u64.success);
} else if (DN_Str8_StartsWith(line, VM_PEAK, DN_Str8EqCase_Insensitive)) {
DN_Str8 size_with_kb = DN_Str8_TrimWhitespaceAround(DN_Str8_Slice(line, VM_PEAK.size, line.size));
DN_Assert(DN_Str8_EndsWith(size_with_kb, DN_STR8("kB")));
DN_Str8 vm_size = DN_Str8_BSplit(size_with_kb, DN_STR8(" ")).lhs;
DN_Str8ToU64Result to_u64 = DN_Str8_ToU64(vm_size, 0);
} else if (DN_Str8StartsWith(line, VM_PEAK, DN_Str8EqCase_Insensitive)) {
DN_Str8 size_with_kb = DN_Str8TrimWhitespaceAround(DN_Str8Slice(line, VM_PEAK.size, line.size));
DN_Assert(DN_Str8EndsWith(size_with_kb, DN_Str8Lit("kB")));
DN_Str8 vm_size = DN_Str8BSplit(size_with_kb, DN_Str8Lit(" ")).lhs;
DN_Str8ToU64Result to_u64 = DN_Str8ToU64(vm_size, 0);
result.vm_peak = DN_Kilobytes(to_u64.value);
DN_Assert(to_u64.success);
}
@@ -1411,12 +1411,12 @@ static EM_BOOL EMWebSocketOnCloseCallback(int type, const EmscriptenWebSocketClo
#if defined(DN_PLATFORM_EMSCRIPTEN)
static void DN_OS_HttpRequestEMFetchOnSuccessCallback(emscripten_fetch_t *fetch)
{
DN_OSHttpResponse *response = DN_CAST(DN_OSHttpResponse *) fetch->userData;
DN_OSHttpResponse *response = DN_Cast(DN_OSHttpResponse *) fetch->userData;
if (!DN_Check(response))
return;
response->http_status = DN_CAST(DN_U32) fetch->status;
response->body = DN_Str8_Alloc(response->arena, fetch->numBytes, DN_ZeroMem_No);
response->http_status = DN_Cast(DN_U32) fetch->status;
response->body = DN_Str8FromArena(response->arena, fetch->numBytes, DN_ZMem_No);
if (response->body.data)
DN_Memcpy(response->body.data, fetch->data, fetch->numBytes);
@@ -1426,12 +1426,12 @@ static void DN_OS_HttpRequestEMFetchOnSuccessCallback(emscripten_fetch_t *fetch)
static void DN_OS_HttpRequestEMFetchOnErrorCallback(emscripten_fetch_t *fetch)
{
DN_OSHttpResponse *response = DN_CAST(DN_OSHttpResponse *) fetch->userData;
DN_OSHttpResponse *response = DN_Cast(DN_OSHttpResponse *) fetch->userData;
if (!DN_Check(response))
return;
response->http_status = DN_CAST(DN_U32) fetch->status;
response->body = DN_Str8_Alloc(response->arena, fetch->numBytes, DN_ZeroMem_No);
response->http_status = DN_Cast(DN_U32) fetch->status;
response->body = DN_Str8FromArena(response->arena, fetch->numBytes, DN_ZMem_No);
if (response->body.size)
DN_Memcpy(response->body.data, fetch->data, fetch->numBytes);
@@ -1467,15 +1467,15 @@ DN_API void DN_OS_HttpRequestAsync(DN_OSHttpResponse *response,
if (method.size >= sizeof(fetch_attribs.requestMethod)) {
response->error_msg =
DN_Str8_FromF(arena,
DN_Str8FromFmtArena(arena,
"Request method in EM has a size limit of 31 characters, method was "
"'%.*s' which is %zu characters long",
DN_STR_FMT(method),
DN_Str8PrintFmt(method),
method.size);
DN_CheckF(method.size < sizeof(fetch_attribs.requestMethod),
"%.*s",
DN_STR_FMT(response->error_msg));
response->error_code = DN_CAST(DN_U32) - 1;
DN_Str8PrintFmt(response->error_msg));
response->error_code = DN_Cast(DN_U32) - 1;
DN_AtomicAddU32(&response->done, 1);
return;
}
@@ -1489,11 +1489,11 @@ DN_API void DN_OS_HttpRequestAsync(DN_OSHttpResponse *response,
fetch_attribs.onerror = DN_OS_HttpRequestEMFetchOnErrorCallback;
fetch_attribs.userData = response;
DN_Str8 url = DN_Str8_FromF(tmem, "%.*s%.*s", DN_STR_FMT(host), DN_STR_FMT(path));
DN_Str8 url = DN_Str8FromFmtArena(tmem, "%.*s%.*s", DN_Str8PrintFmt(host), DN_Str8PrintFmt(path));
DN_LOG_InfoF("Initiating HTTP '%s' request to '%.*s' with payload '%.*s'",
fetch_attribs.requestMethod,
DN_STR_FMT(url),
DN_STR_FMT(body));
DN_Str8PrintFmt(url),
DN_Str8PrintFmt(body));
response->on_complete_semaphore = DN_OS_SemaphoreInit(0);
response->em_handle = emscripten_fetch(&fetch_attribs, url.data);
#else // #elif defined(DN_OS_WIN32)
@@ -1511,7 +1511,7 @@ DN_API void DN_OS_HttpRequestFree(DN_OSHttpResponse *response)
}
#endif // #elif defined(DN_OS_WIN32)
DN_Arena_Deinit(&response->tmp_arena);
DN_ArenaDeinit(&response->tmp_arena);
DN_OS_SemaphoreDeinit(&response->on_complete_semaphore);
*response = {};
}
+15 -15
View File
@@ -57,15 +57,15 @@ DN_API void DN_OS_Print(DN_OSPrintDest dest, DN_Str8 string)
}
// NOTE: Write the string //////////////////////////////////////////////////////////////////////
DN_Assert(string.size < DN_CAST(unsigned long) - 1);
DN_Assert(string.size < DN_Cast(unsigned long) - 1);
unsigned long bytes_written = 0;
(void)bytes_written;
if (print_to_console)
WriteConsoleA(print_handle, string.data, DN_CAST(unsigned long) string.size, &bytes_written, nullptr);
WriteConsoleA(print_handle, string.data, DN_Cast(unsigned long) string.size, &bytes_written, nullptr);
else
WriteFile(print_handle, string.data, DN_CAST(unsigned long) string.size, &bytes_written, nullptr);
WriteFile(print_handle, string.data, DN_Cast(unsigned long) string.size, &bytes_written, nullptr);
#else
fprintf(dest == DN_OSPrintDest_Out ? stdout : stderr, "%.*s", DN_STR_FMT(string));
fprintf(dest == DN_OSPrintDest_Out ? stdout : stderr, "%.*s", DN_Str8PrintFmt(string));
#endif
}
@@ -91,20 +91,20 @@ DN_API void DN_OS_PrintStyle(DN_OSPrintDest dest, DN_LOGStyle style, DN_Str8 str
if (style.colour)
DN_OS_Print(dest, DN_LOG_ColourEscapeCodeStr8FromRGB(DN_LOGColourType_Fg, style.r, style.g, style.b));
if (style.bold == DN_LOGBold_Yes)
DN_OS_Print(dest, DN_STR8(DN_LOG_BoldEscapeCode));
DN_OS_Print(dest, DN_Str8Lit(DN_LOG_BoldEscapeCode));
DN_OS_Print(dest, string);
if (style.colour || style.bold == DN_LOGBold_Yes)
DN_OS_Print(dest, DN_STR8(DN_LOG_ResetEscapeCode));
DN_OS_Print(dest, DN_Str8Lit(DN_LOG_ResetEscapeCode));
}
}
static char *DN_OS_PrintVSPrintfChunker_(const char *buf, void *user, int len)
{
DN_Str8 string = {};
string.data = DN_CAST(char *) buf;
string.data = DN_Cast(char *) buf;
string.size = len;
DN_OSPrintDest dest = DN_CAST(DN_OSPrintDest) DN_CAST(uintptr_t) user;
DN_OSPrintDest dest = DN_Cast(DN_OSPrintDest) DN_Cast(uintptr_t) user;
DN_OS_Print(dest, string);
return (char *)buf;
}
@@ -113,7 +113,7 @@ DN_API void DN_OS_PrintFV(DN_OSPrintDest dest, DN_FMT_ATTRIB char const *fmt, va
{
char buffer[STB_SPRINTF_MIN];
STB_SPRINTF_DECORATE(vsprintfcb)
(DN_OS_PrintVSPrintfChunker_, DN_CAST(void *) DN_CAST(uintptr_t) dest, buffer, fmt, args);
(DN_OS_PrintVSPrintfChunker_, DN_Cast(void *) DN_Cast(uintptr_t) dest, buffer, fmt, args);
}
DN_API void DN_OS_PrintFVStyle(DN_OSPrintDest dest, DN_LOGStyle style, DN_FMT_ATTRIB char const *fmt, va_list args)
@@ -122,17 +122,17 @@ DN_API void DN_OS_PrintFVStyle(DN_OSPrintDest dest, DN_LOGStyle style, DN_FMT_AT
if (style.colour)
DN_OS_Print(dest, DN_LOG_ColourEscapeCodeStr8FromRGB(DN_LOGColourType_Fg, style.r, style.g, style.b));
if (style.bold == DN_LOGBold_Yes)
DN_OS_Print(dest, DN_STR8(DN_LOG_BoldEscapeCode));
DN_OS_Print(dest, DN_Str8Lit(DN_LOG_BoldEscapeCode));
DN_OS_PrintFV(dest, fmt, args);
if (style.colour || style.bold == DN_LOGBold_Yes)
DN_OS_Print(dest, DN_STR8(DN_LOG_ResetEscapeCode));
DN_OS_Print(dest, DN_Str8Lit(DN_LOG_ResetEscapeCode));
}
}
DN_API void DN_OS_PrintLn(DN_OSPrintDest dest, DN_Str8 string)
{
DN_OS_Print(dest, string);
DN_OS_Print(dest, DN_STR8("\n"));
DN_OS_Print(dest, DN_Str8Lit("\n"));
}
DN_API void DN_OS_PrintLnF(DN_OSPrintDest dest, DN_FMT_ATTRIB char const *fmt, ...)
@@ -146,13 +146,13 @@ DN_API void DN_OS_PrintLnF(DN_OSPrintDest dest, DN_FMT_ATTRIB char const *fmt, .
DN_API void DN_OS_PrintLnFV(DN_OSPrintDest dest, DN_FMT_ATTRIB char const *fmt, va_list args)
{
DN_OS_PrintFV(dest, fmt, args);
DN_OS_Print(dest, DN_STR8("\n"));
DN_OS_Print(dest, DN_Str8Lit("\n"));
}
DN_API void DN_OS_PrintLnStyle(DN_OSPrintDest dest, DN_LOGStyle style, DN_Str8 string)
{
DN_OS_PrintStyle(dest, style, string);
DN_OS_Print(dest, DN_STR8("\n"));
DN_OS_Print(dest, DN_Str8Lit("\n"));
}
DN_API void DN_OS_PrintLnFStyle(DN_OSPrintDest dest, DN_LOGStyle style, DN_FMT_ATTRIB char const *fmt, ...)
@@ -166,5 +166,5 @@ DN_API void DN_OS_PrintLnFStyle(DN_OSPrintDest dest, DN_LOGStyle style, DN_FMT_A
DN_API void DN_OS_PrintLnFVStyle(DN_OSPrintDest dest, DN_LOGStyle style, DN_FMT_ATTRIB char const *fmt, va_list args)
{
DN_OS_PrintFVStyle(dest, style, fmt, args);
DN_OS_Print(dest, DN_STR8("\n"));
DN_OS_Print(dest, DN_Str8Lit("\n"));
}
+93 -96
View File
@@ -3,230 +3,227 @@
#include "../dn_base_inc.h"
#include "../dn_os_inc.h"
// NOTE: DN_Str8 ///////////////////////////////////////////////////////////////////////////////////
DN_API DN_Str8 DN_Str8_FromFrameF(DN_FMT_ATTRIB char const *fmt, ...)
// NOTE: DN_Str8
DN_API DN_Str8 DN_Str8FromFmtArenaFrame(DN_FMT_ATTRIB char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
DN_Str8 result = DN_Str8_FromFV(DN_OS_TLSGet()->frame_arena, fmt, args);
DN_Arena *frame_arena = DN_OS_TLSGet()->frame_arena;
DN_Str8 result = DN_Str8FromFmtVArena(frame_arena, fmt, args);
va_end(args);
return result;
}
DN_API DN_Str8 DN_Str8_FromFrameFV(DN_FMT_ATTRIB char const *fmt, va_list args)
DN_API DN_Str8 DN_Str8FromFmtVArenaFrame(DN_FMT_ATTRIB char const *fmt, va_list args)
{
DN_Str8 result = DN_Str8_FromFV(DN_OS_TLSGet()->frame_arena, fmt, args);
DN_Arena *frame_arena = DN_OS_TLSGet()->frame_arena;
DN_Str8 result = DN_Str8FromFmtVArena(frame_arena, fmt, args);
return result;
}
DN_API DN_Str8 DN_Str8_FromFrame(DN_USize size, DN_ZeroMem zero_mem)
DN_API DN_Str8 DN_Str8FromArenaFrame(DN_USize size, DN_ZMem z_mem)
{
DN_Str8 result = DN_Str8_Alloc(DN_OS_TLSGet()->frame_arena, size, zero_mem);
DN_Arena *frame_arena = DN_OS_TLSGet()->frame_arena;
DN_Str8 result = DN_Str8FromArena(frame_arena, size, z_mem);
return result;
}
DN_API DN_Str8 DN_Str8_FromHeapF(DN_FMT_ATTRIB char const *fmt, ...)
DN_API DN_Str8 DN_Str8FromHeapF(DN_FMT_ATTRIB char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
DN_Str8 result = {};
DN_USize size = DN_CStr8_FVSize(fmt, args);
if (size) {
result = DN_Str8_FromHeap(size, DN_ZeroMem_No);
if (DN_Str8_HasData(result))
DN_VSNPrintF(result.data, DN_SaturateCastISizeToInt(size + 1 /*null-terminator*/), fmt, args);
}
DN_USize size = DN_FmtVSize(fmt, args);
DN_Str8 result = DN_Str8FromHeap(size, DN_ZMem_No);
DN_VSNPrintF(result.data, DN_Cast(int)(result.size + 1), fmt, args);
va_end(args);
return result;
}
DN_API DN_Str8 DN_Str8_FromHeap(DN_USize size, DN_ZeroMem zero_mem)
DN_API DN_Str8 DN_Str8FromHeap(DN_USize size, DN_ZMem z_mem)
{
DN_Str8 result = {};
result.data = DN_CAST(char *)DN_OS_MemAlloc(size + 1, zero_mem);
if (result.data)
result.size = size;
result.data[result.size] = 0;
result.data = DN_Cast(char *)DN_OS_MemAlloc(size + 1, z_mem);
if (result.data) {
result.size = size;
result.data[result.size] = 0;
}
return result;
}
DN_API DN_Str8 DN_Str8_FromTLSFV(DN_FMT_ATTRIB char const *fmt, va_list args)
DN_API DN_Str8 DN_Str8FromTLSFV(DN_FMT_ATTRIB char const *fmt, va_list args)
{
DN_Str8 result = DN_Str8_FromFV(DN_OS_TLSTopArena(), fmt, args);
DN_Str8 result = DN_Str8FromFmtVArena(DN_OS_TLSTopArena(), fmt, args);
return result;
}
DN_API DN_Str8 DN_Str8_FromTLSF(DN_FMT_ATTRIB char const *fmt, ...)
DN_API DN_Str8 DN_Str8FromTLSF(DN_FMT_ATTRIB char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
DN_Str8 result = DN_Str8_FromFV(DN_OS_TLSTopArena(), fmt, args);
DN_Str8 result = DN_Str8FromFmtVArena(DN_OS_TLSTopArena(), fmt, args);
va_end(args);
return result;
}
DN_API DN_Str8 DN_Str8_FromTLS(DN_USize size, DN_ZeroMem zero_mem)
DN_API DN_Str8 DN_Str8FromTLS(DN_USize size, DN_ZMem z_mem)
{
DN_Str8 result = DN_Str8_Alloc(DN_OS_TLSTopArena(), size, zero_mem);
DN_Str8 result = DN_Str8FromArena(DN_OS_TLSTopArena(), size, z_mem);
return result;
}
DN_API DN_Str8 DN_Str8_FromStr8Frame(DN_Str8 string)
DN_API DN_Str8 DN_Str8FromStr8Frame(DN_Str8 string)
{
DN_Str8 result = DN_Str8_FromStr8(DN_OS_TLSGet()->frame_arena, string);
DN_Str8 result = DN_Str8FromStr8Arena(DN_OS_TLSGet()->frame_arena, string);
return result;
}
DN_API DN_Str8 DN_Str8_FromStr8TLS(DN_Str8 string)
DN_API DN_Str8 DN_Str8FromStr8TLS(DN_Str8 string)
{
DN_Str8 result = DN_Str8_FromStr8(DN_OS_TLSTopArena(), string);
DN_Str8 result = DN_Str8FromStr8Arena(DN_OS_TLSTopArena(), string);
return result;
}
DN_API DN_Slice<DN_Str8> DN_Str8_SplitFromFrame(DN_Str8 string, DN_Str8 delimiter, DN_Str8SplitIncludeEmptyStrings mode)
DN_API DN_Str8SplitResult DN_Str8SplitFromFrame(DN_Str8 string, DN_Str8 delimiter, DN_Str8SplitIncludeEmptyStrings mode)
{
DN_Slice<DN_Str8> result = DN_Str8_SplitAlloc(DN_OS_TLSGet()->frame_arena, string, delimiter, mode);
DN_Str8SplitResult result = DN_Str8SplitArena(DN_OS_TLSGet()->frame_arena, string, delimiter, mode);
return result;
}
DN_API DN_Slice<DN_Str8> DN_Str8_SplitFromTLS(DN_Str8 string, DN_Str8 delimiter, DN_Str8SplitIncludeEmptyStrings mode)
DN_API DN_Str8SplitResult DN_Str8SplitFromTLS(DN_Str8 string, DN_Str8 delimiter, DN_Str8SplitIncludeEmptyStrings mode)
{
DN_Slice<DN_Str8> result = DN_Str8_SplitAlloc(DN_OS_TLSTopArena(), string, delimiter, mode);
DN_Str8SplitResult result = DN_Str8SplitArena(DN_OS_TLSTopArena(), string, delimiter, mode);
return result;
}
DN_API DN_Str8 DN_Str8_SegmentFromFrame(DN_Str8 src, DN_USize segment_size, char segment_char)
DN_API DN_Str8 DN_Str8SegmentFromFrame(DN_Str8 src, DN_USize segment_size, char segment_char)
{
DN_Str8 result = DN_Str8_Segment(DN_OS_TLSGet()->frame_arena, src, segment_size, segment_char);
DN_Str8 result = DN_Str8Segment(DN_OS_TLSGet()->frame_arena, src, segment_size, segment_char);
return result;
}
DN_API DN_Str8 DN_Str8_SegmentFromTLS(DN_Str8 src, DN_USize segment_size, char segment_char)
DN_API DN_Str8 DN_Str8SegmentFromTLS(DN_Str8 src, DN_USize segment_size, char segment_char)
{
DN_Str8 result = DN_Str8_Segment(DN_OS_TLSTopArena(), src, segment_size, segment_char);
DN_Str8 result = DN_Str8Segment(DN_OS_TLSTopArena(), src, segment_size, segment_char);
return result;
}
DN_API DN_Str8 DN_Str8_ReverseSegmentFromFrame(DN_Str8 src, DN_USize segment_size, char segment_char)
DN_API DN_Str8 DN_Str8ReverseSegmentFromFrame(DN_Str8 src, DN_USize segment_size, char segment_char)
{
DN_Str8 result = DN_Str8_ReverseSegment(DN_OS_TLSGet()->frame_arena, src, segment_size, segment_char);
DN_Str8 result = DN_Str8ReverseSegment(DN_OS_TLSGet()->frame_arena, src, segment_size, segment_char);
return result;
}
DN_API DN_Str8 DN_Str8_ReverseSegmentFromTLS(DN_Str8 src, DN_USize segment_size, char segment_char)
DN_API DN_Str8 DN_Str8ReverseSegmentFromTLS(DN_Str8 src, DN_USize segment_size, char segment_char)
{
DN_Str8 result = DN_Str8_ReverseSegment(DN_OS_TLSTopArena(), src, segment_size, segment_char);
DN_Str8 result = DN_Str8ReverseSegment(DN_OS_TLSTopArena(), src, segment_size, segment_char);
return result;
}
DN_API DN_Str8 DN_Str8_AppendFFromFrame(DN_Str8 string, char const *fmt, ...)
DN_API DN_Str8 DN_Str8AppendFFromFrame(DN_Str8 string, char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
DN_Str8 result = DN_Str8_AppendFV(DN_OS_TLSGet()->frame_arena, string, fmt, args);
DN_Str8 result = DN_Str8AppendFV(DN_OS_TLSGet()->frame_arena, string, fmt, args);
va_end(args);
return result;
}
DN_API DN_Str8 DN_Str8_AppendFFromTLS(DN_Str8 string, char const *fmt, ...)
DN_API DN_Str8 DN_Str8AppendFFromTLS(DN_Str8 string, char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
DN_Str8 result = DN_Str8_AppendFV(DN_OS_TLSTopArena(), string, fmt, args);
DN_Str8 result = DN_Str8AppendFV(DN_OS_TLSTopArena(), string, fmt, args);
va_end(args);
return result;
}
DN_API DN_Str8 DN_Str8_FillFFromFrame(DN_USize count, char const *fmt, ...)
DN_API DN_Str8 DN_Str8FillFFromFrame(DN_USize count, char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
DN_Str8 result = DN_Str8_FillFV(DN_OS_TLSGet()->frame_arena, count, fmt, args);
DN_Str8 result = DN_Str8FillFV(DN_OS_TLSGet()->frame_arena, count, fmt, args);
va_end(args);
return result;
}
DN_API DN_Str8 DN_Str8_FillFFromTLS(DN_USize count, char const *fmt, ...)
DN_API DN_Str8 DN_Str8FillFFromTLS(DN_USize count, char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
DN_Str8 result = DN_Str8_FillFV(DN_OS_TLSTopArena(), count, fmt, args);
DN_Str8 result = DN_Str8FillFV(DN_OS_TLSTopArena(), count, fmt, args);
va_end(args);
return result;
}
DN_API DN_Str8DotTruncateResult DN_Str8_DotTruncateMiddleFromFrame(DN_Str8 str8, uint32_t side_size, DN_Str8 truncator)
DN_API DN_Str8TruncateResult DN_Str8TruncateMiddleArenaFrame(DN_Str8 str8, uint32_t side_size, DN_Str8 truncator)
{
DN_Str8DotTruncateResult result = DN_Str8_DotTruncateMiddle(DN_OS_TLSGet()->frame_arena, str8, side_size, truncator);
DN_Str8TruncateResult result = DN_Str8TruncateMiddle(DN_OS_TLSGet()->frame_arena, str8, side_size, truncator);
return result;
}
DN_API DN_Str8DotTruncateResult DN_Str8_DotTruncateMiddleFromTLS(DN_Str8 str8, uint32_t side_size, DN_Str8 truncator)
DN_API DN_Str8TruncateResult DN_Str8TruncateMiddleArenaTLS(DN_Str8 str8, uint32_t side_size, DN_Str8 truncator)
{
DN_Str8DotTruncateResult result = DN_Str8_DotTruncateMiddle(DN_OS_TLSTopArena(), str8, side_size, truncator);
DN_Str8TruncateResult result = DN_Str8TruncateMiddle(DN_OS_TLSTopArena(), str8, side_size, truncator);
return result;
}
DN_API DN_Str8 DN_Str8_PadNewLines(DN_Arena *arena, DN_Str8 src, DN_Str8 pad)
DN_API DN_Str8 DN_Str8PadNewLines(DN_Arena *arena, DN_Str8 src, DN_Str8 pad)
{
// TODO: Implement this without requiring TLS so it can go into base strings
DN_OSTLSTMem tmem = DN_OS_TLSPushTMem(arena);
DN_Str8Builder builder = DN_Str8Builder_FromTLS();
DN_Str8Builder builder = DN_Str8BuilderFromTLS();
DN_Str8BSplitResult split = DN_Str8_BSplit(src, DN_STR8("\n"));
DN_Str8BSplitResult split = DN_Str8BSplit(src, DN_Str8Lit("\n"));
while (split.lhs.size) {
DN_Str8Builder_AppendRef(&builder, pad);
DN_Str8Builder_AppendRef(&builder, split.lhs);
split = DN_Str8_BSplit(split.rhs, DN_STR8("\n"));
DN_Str8BuilderAppendRef(&builder, pad);
DN_Str8BuilderAppendRef(&builder, split.lhs);
split = DN_Str8BSplit(split.rhs, DN_Str8Lit("\n"));
if (split.lhs.size)
DN_Str8Builder_AppendRef(&builder, DN_STR8("\n"));
DN_Str8BuilderAppendRef(&builder, DN_Str8Lit("\n"));
}
DN_Str8 result = DN_Str8Builder_Build(&builder, arena);
DN_Str8 result = DN_Str8BuilderBuild(&builder, arena);
return result;
}
DN_API DN_Str8 DN_Str8_PadNewLinesFromFrame(DN_Str8 src, DN_Str8 pad)
DN_API DN_Str8 DN_Str8PadNewLinesFromFrame(DN_Str8 src, DN_Str8 pad)
{
DN_Str8 result = DN_Str8_PadNewLines(DN_OS_TLSGet()->frame_arena, src, pad);
DN_Str8 result = DN_Str8PadNewLines(DN_OS_TLSGet()->frame_arena, src, pad);
return result;
}
DN_API DN_Str8 DN_Str8_PadNewLinesFromTLS(DN_Str8 src, DN_Str8 pad)
DN_API DN_Str8 DN_Str8PadNewLinesFromTLS(DN_Str8 src, DN_Str8 pad)
{
DN_Str8 result = DN_Str8_PadNewLines(DN_OS_TLSTopArena(), src, pad);
DN_Str8 result = DN_Str8PadNewLines(DN_OS_TLSTopArena(), src, pad);
return result;
}
DN_API DN_Str8 DN_Str8_UpperFromFrame(DN_Str8 string)
DN_API DN_Str8 DN_Str8UpperFromFrame(DN_Str8 string)
{
DN_Str8 result = DN_Str8_Upper(DN_OS_TLSGet()->frame_arena, string);
DN_Str8 result = DN_Str8Upper(DN_OS_TLSGet()->frame_arena, string);
return result;
}
DN_API DN_Str8 DN_Str8_UpperFromTLS(DN_Str8 string)
DN_API DN_Str8 DN_Str8UpperFromTLS(DN_Str8 string)
{
DN_Str8 result = DN_Str8_Upper(DN_OS_TLSTopArena(), string);
DN_Str8 result = DN_Str8Upper(DN_OS_TLSTopArena(), string);
return result;
}
DN_API DN_Str8 DN_Str8_LowerFromFrame(DN_Str8 string)
DN_API DN_Str8 DN_Str8LowerFromFrame(DN_Str8 string)
{
DN_Str8 result = DN_Str8_Lower(DN_OS_TLSGet()->frame_arena, string);
DN_Str8 result = DN_Str8Lower(DN_OS_TLSGet()->frame_arena, string);
return result;
}
DN_API DN_Str8 DN_Str8_LowerFromTLS(DN_Str8 string)
DN_API DN_Str8 DN_Str8LowerFromTLS(DN_Str8 string)
{
DN_Str8 result = DN_Str8_Lower(DN_OS_TLSTopArena(), string);
DN_Str8 result = DN_Str8Lower(DN_OS_TLSTopArena(), string);
return result;
}
DN_API DN_Str8 DN_Str8_Replace(DN_Str8 string,
DN_API DN_Str8 DN_Str8Replace(DN_Str8 string,
DN_Str8 find,
DN_Str8 replace,
DN_USize start_index,
@@ -235,19 +232,19 @@ DN_API DN_Str8 DN_Str8_Replace(DN_Str8 string,
{
// TODO: Implement this without requiring TLS so it can go into base strings
DN_Str8 result = {};
if (!DN_Str8_HasData(string) || !DN_Str8_HasData(find) || find.size > string.size || find.size == 0 || string.size == 0) {
result = DN_Str8_FromStr8(arena, string);
if (string.size == 0 || find.size == 0 || find.size > string.size || find.size == 0 || string.size == 0) {
result = DN_Str8FromStr8Arena(arena, string);
return result;
}
DN_OSTLSTMem tmem = DN_OS_TLSTMem(arena);
DN_Str8Builder string_builder = DN_Str8Builder_FromArena(tmem.arena);
DN_Str8Builder string_builder = DN_Str8BuilderFromArena(tmem.arena);
DN_USize max = string.size - find.size;
DN_USize head = start_index;
for (DN_USize tail = head; tail <= max; tail++) {
DN_Str8 check = DN_Str8_Slice(string, tail, find.size);
if (!DN_Str8_Eq(check, find, eq_case))
DN_Str8 check = DN_Str8Slice(string, tail, find.size);
if (!DN_Str8Eq(check, find, eq_case))
continue;
if (start_index > 0 && string_builder.string_size == 0) {
@@ -255,43 +252,43 @@ DN_API DN_Str8 DN_Str8_Replace(DN_Str8 string,
// need to add the string up to the hint. We only do this if there's
// a replacement action, otherwise we have a special case for no
// replacements, where the entire string gets copied.
DN_Str8 slice = DN_Str8_Init(string.data, head);
DN_Str8Builder_AppendRef(&string_builder, slice);
DN_Str8 slice = DN_Str8FromPtr(string.data, head);
DN_Str8BuilderAppendRef(&string_builder, slice);
}
DN_Str8 range = DN_Str8_Slice(string, head, (tail - head));
DN_Str8Builder_AppendRef(&string_builder, range);
DN_Str8Builder_AppendRef(&string_builder, replace);
DN_Str8 range = DN_Str8Slice(string, head, (tail - head));
DN_Str8BuilderAppendRef(&string_builder, range);
DN_Str8BuilderAppendRef(&string_builder, replace);
head = tail + find.size;
tail += find.size - 1; // NOTE: -1 since the for loop will post increment us past the end of the find string
}
if (string_builder.string_size == 0) {
// NOTE: No replacement possible, so we just do a full-copy
result = DN_Str8_FromStr8(arena, string);
result = DN_Str8FromStr8Arena(arena, string);
} else {
DN_Str8 remainder = DN_Str8_Init(string.data + head, string.size - head);
DN_Str8Builder_AppendRef(&string_builder, remainder);
result = DN_Str8Builder_Build(&string_builder, arena);
DN_Str8 remainder = DN_Str8FromPtr(string.data + head, string.size - head);
DN_Str8BuilderAppendRef(&string_builder, remainder);
result = DN_Str8BuilderBuild(&string_builder, arena);
}
return result;
}
DN_API DN_Str8 DN_Str8_ReplaceInsensitive(DN_Str8 string, DN_Str8 find, DN_Str8 replace, DN_USize start_index, DN_Arena *arena)
DN_API DN_Str8 DN_Str8ReplaceInsensitive(DN_Str8 string, DN_Str8 find, DN_Str8 replace, DN_USize start_index, DN_Arena *arena)
{
DN_Str8 result = DN_Str8_Replace(string, find, replace, start_index, arena, DN_Str8EqCase_Insensitive);
DN_Str8 result = DN_Str8Replace(string, find, replace, start_index, arena, DN_Str8EqCase_Insensitive);
return result;
}
// NOTE: DN_Str8Builder ////////////////////////////////////////////////////////////////////////////
DN_API DN_Str8 DN_Str8Builder_BuildFromOSHeap(DN_Str8Builder const *builder)
DN_API DN_Str8 DN_Str8BuilderBuildFromOSHeap(DN_Str8Builder const *builder)
{
DN_Str8 result = DN_ZeroInit;
if (!builder || builder->string_size <= 0 || builder->count <= 0)
return result;
result.data = DN_CAST(char *) DN_OS_MemAlloc(builder->string_size + 1, DN_ZeroMem_No);
result.data = DN_Cast(char *) DN_OS_MemAlloc(builder->string_size + 1, DN_ZMem_No);
if (!result.data)
return result;
+46 -46
View File
@@ -6,69 +6,69 @@
// NOTE: DN_Str8 ///////////////////////////////////////////////////////////////////////////////////
DN_API DN_Str8 DN_Str8_FromFrameFV (DN_FMT_ATTRIB char const *fmt, va_list args);
DN_API DN_Str8 DN_Str8_FromFrameF (DN_FMT_ATTRIB char const *fmt, ...);
DN_API DN_Str8 DN_Str8_FromFrame (DN_USize size, DN_ZeroMem zero_mem);
DN_API DN_Str8 DN_Str8_FromHeapF (DN_FMT_ATTRIB char const *fmt, ...);
DN_API DN_Str8 DN_Str8_FromHeap (DN_USize size, DN_ZeroMem zero_mem);
DN_API DN_Str8 DN_Str8_FromTLSFV (DN_FMT_ATTRIB char const *fmt, va_list args);
DN_API DN_Str8 DN_Str8_FromTLSF (DN_FMT_ATTRIB char const *fmt, ...);
DN_API DN_Str8 DN_Str8_FromTLS (DN_USize size, DN_ZeroMem zero_mem);
DN_API DN_Str8 DN_Str8_FromStr8Frame (DN_Str8 string);
DN_API DN_Str8 DN_Str8_FromStr8TLS (DN_Str8 string);
DN_API DN_Str8 DN_Str8FromFmtVArenaFrame (DN_FMT_ATTRIB char const *fmt, va_list args);
DN_API DN_Str8 DN_Str8FromFmtArenaFrame (DN_FMT_ATTRIB char const *fmt, ...);
DN_API DN_Str8 DN_Str8FromArenaFrame (DN_USize size, DN_ZMem z_mem);
DN_API DN_Str8 DN_Str8FromHeapF (DN_FMT_ATTRIB char const *fmt, ...);
DN_API DN_Str8 DN_Str8FromHeap (DN_USize size, DN_ZMem z_mem);
DN_API DN_Str8 DN_Str8FromTLSFV (DN_FMT_ATTRIB char const *fmt, va_list args);
DN_API DN_Str8 DN_Str8FromTLSF (DN_FMT_ATTRIB char const *fmt, ...);
DN_API DN_Str8 DN_Str8FromTLS (DN_USize size, DN_ZMem z_mem);
DN_API DN_Str8 DN_Str8FromStr8Frame (DN_Str8 string);
DN_API DN_Str8 DN_Str8FromStr8TLS (DN_Str8 string);
DN_API DN_Slice<DN_Str8> DN_Str8_SplitFromFrame (DN_Str8 string, DN_Str8 delimiter, DN_Str8SplitIncludeEmptyStrings mode);
DN_API DN_Slice<DN_Str8> DN_Str8_SplitFromTLS (DN_Str8 string, DN_Str8 delimiter, DN_Str8SplitIncludeEmptyStrings mode);
DN_API DN_Str8SplitResult DN_Str8SplitFromFrame (DN_Str8 string, DN_Str8 delimiter, DN_Str8SplitIncludeEmptyStrings mode);
DN_API DN_Str8SplitResult DN_Str8SplitFromTLS (DN_Str8 string, DN_Str8 delimiter, DN_Str8SplitIncludeEmptyStrings mode);
DN_API DN_Str8 DN_Str8_SegmentFromFrame (DN_Str8 src, DN_USize segment_size, char segment_char);
DN_API DN_Str8 DN_Str8_SegmentFromTLS (DN_Str8 src, DN_USize segment_size, char segment_char);
DN_API DN_Str8 DN_Str8SegmentFromFrame (DN_Str8 src, DN_USize segment_size, char segment_char);
DN_API DN_Str8 DN_Str8SegmentFromTLS (DN_Str8 src, DN_USize segment_size, char segment_char);
DN_API DN_Str8 DN_Str8_ReverseSegmentFromFrame (DN_Str8 src, DN_USize segment_size, char segment_char);
DN_API DN_Str8 DN_Str8_ReverseSegmentFromTLS (DN_Str8 src, DN_USize segment_size, char segment_char);
DN_API DN_Str8 DN_Str8ReverseSegmentFromFrame (DN_Str8 src, DN_USize segment_size, char segment_char);
DN_API DN_Str8 DN_Str8ReverseSegmentFromTLS (DN_Str8 src, DN_USize segment_size, char segment_char);
DN_API DN_Str8 DN_Str8_AppendFFromFrame (DN_Str8 string, char const *fmt, ...);
DN_API DN_Str8 DN_Str8_AppendFFromTLS (DN_Str8 string, char const *fmt, ...);
DN_API DN_Str8 DN_Str8AppendFFromFrame (DN_Str8 string, char const *fmt, ...);
DN_API DN_Str8 DN_Str8AppendFFromTLS (DN_Str8 string, char const *fmt, ...);
DN_API DN_Str8 DN_Str8_FillFFromFrame (DN_Str8 string, char const *fmt, ...);
DN_API DN_Str8 DN_Str8_FillFFromTLS (DN_Str8 string, char const *fmt, ...);
DN_API DN_Str8 DN_Str8FillFFromFrame (DN_Str8 string, char const *fmt, ...);
DN_API DN_Str8 DN_Str8FillFFromTLS (DN_Str8 string, char const *fmt, ...);
DN_API DN_Str8DotTruncateResult DN_Str8_DotTruncateMiddleFromFrame (DN_Str8 str8, uint32_t side_size, DN_Str8 truncator);
DN_API DN_Str8DotTruncateResult DN_Str8_DotTruncateMiddleFromTLS (DN_Str8 str8, uint32_t side_size, DN_Str8 truncator);
DN_API DN_Str8TruncateResult DN_Str8TruncateMiddleArenaFrame (DN_Str8 str8, uint32_t side_size, DN_Str8 truncator);
DN_API DN_Str8TruncateResult DN_Str8TruncateMiddleArenaTLS (DN_Str8 str8, uint32_t side_size, DN_Str8 truncator);
DN_API DN_Str8 DN_Str8_PadNewLines (DN_Arena *arena, DN_Str8 src, DN_Str8 pad);
DN_API DN_Str8 DN_Str8_PadNewLinesFromFrame (DN_Str8 src, DN_Str8 pad);
DN_API DN_Str8 DN_Str8_PadNewLinesFromTLS (DN_Str8 src, DN_Str8 pad);
DN_API DN_Str8 DN_Str8PadNewLines (DN_Arena *arena, DN_Str8 src, DN_Str8 pad);
DN_API DN_Str8 DN_Str8PadNewLinesFromFrame (DN_Str8 src, DN_Str8 pad);
DN_API DN_Str8 DN_Str8PadNewLinesFromTLS (DN_Str8 src, DN_Str8 pad);
DN_API DN_Str8 DN_Str8_UpperFromFrame (DN_Str8 string);
DN_API DN_Str8 DN_Str8_UpperFromTLS (DN_Str8 string);
DN_API DN_Str8 DN_Str8UpperFromFrame (DN_Str8 string);
DN_API DN_Str8 DN_Str8UpperFromTLS (DN_Str8 string);
DN_API DN_Str8 DN_Str8_LowerFromFrame (DN_Str8 string);
DN_API DN_Str8 DN_Str8_LowerFromTLS (DN_Str8 string);
DN_API DN_Str8 DN_Str8LowerFromFrame (DN_Str8 string);
DN_API DN_Str8 DN_Str8LowerFromTLS (DN_Str8 string);
DN_API DN_Str8 DN_Str8_Replace (DN_Str8 string, DN_Str8 find, DN_Str8 replace, DN_USize start_index, DN_Arena *arena, DN_Str8EqCase eq_case = DN_Str8EqCase_Sensitive);
DN_API DN_Str8 DN_Str8_ReplaceInsensitive (DN_Str8 string, DN_Str8 find, DN_Str8 replace, DN_USize start_index, DN_Arena *arena);
DN_API DN_Str8 DN_Str8Replace (DN_Str8 string, DN_Str8 find, DN_Str8 replace, DN_USize start_index, DN_Arena *arena, DN_Str8EqCase eq_case = DN_Str8EqCase_Sensitive);
DN_API DN_Str8 DN_Str8ReplaceInsensitive (DN_Str8 string, DN_Str8 find, DN_Str8 replace, DN_USize start_index, DN_Arena *arena);
// NOTE: DN_Str8Builder ////////////////////////////////////////////////////////////////////////////
DN_API DN_Str8Builder DN_Str8Builder_FromArena () { return DN_Str8Builder_FromArena(DN_OS_TLSGet()->frame_arena); }
DN_API DN_Str8Builder DN_Str8Builder_FromTLS () { return DN_Str8Builder_FromArena(DN_OS_TLSTopArena()); }
DN_API DN_Str8Builder DN_Str8BuilderFromArena () { return DN_Str8BuilderFromArena(DN_OS_TLSGet()->frame_arena); }
DN_API DN_Str8Builder DN_Str8BuilderFromTLS () { return DN_Str8BuilderFromArena(DN_OS_TLSTopArena()); }
DN_API DN_Str8Builder DN_Str8Builder_FromStr8PtrRefFrame (DN_Str8 const *strings, DN_USize size) { return DN_Str8Builder_FromStr8PtrRef(DN_OS_TLSGet()->frame_arena, strings, size); }
DN_API DN_Str8Builder DN_Str8Builder_FromStr8PtrRefTLS (DN_Str8 const *strings, DN_USize size) { return DN_Str8Builder_FromStr8PtrRef(DN_OS_TLSTopArena(), strings, size); }
DN_API DN_Str8Builder DN_Str8Builder_FromStr8PtrCopyFrame (DN_Str8 const *strings, DN_USize size) { return DN_Str8Builder_FromStr8PtrCopy(DN_OS_TLSGet()->frame_arena, strings, size); }
DN_API DN_Str8Builder DN_Str8Builder_FromStr8PtrCopyTLS (DN_Str8 const *strings, DN_USize size) { return DN_Str8Builder_FromStr8PtrCopy(DN_OS_TLSTopArena(), strings, size); }
DN_API DN_Str8Builder DN_Str8BuilderFromStr8PtrRefFrame (DN_Str8 const *strings, DN_USize size) { return DN_Str8BuilderFromStr8PtrRef(DN_OS_TLSGet()->frame_arena, strings, size); }
DN_API DN_Str8Builder DN_Str8BuilderFromStr8PtrRefTLS (DN_Str8 const *strings, DN_USize size) { return DN_Str8BuilderFromStr8PtrRef(DN_OS_TLSTopArena(), strings, size); }
DN_API DN_Str8Builder DN_Str8BuilderFromStr8PtrCopyFrame (DN_Str8 const *strings, DN_USize size) { return DN_Str8BuilderFromStr8PtrCopy(DN_OS_TLSGet()->frame_arena, strings, size); }
DN_API DN_Str8Builder DN_Str8BuilderFromStr8PtrCopyTLS (DN_Str8 const *strings, DN_USize size) { return DN_Str8BuilderFromStr8PtrCopy(DN_OS_TLSTopArena(), strings, size); }
DN_API DN_Str8Builder DN_Str8Builder_FromBuilderFrame (DN_Str8Builder const *builder) { return DN_Str8Builder_FromBuilder(DN_OS_TLSGet()->frame_arena, builder); }
DN_API DN_Str8Builder DN_Str8Builder_FromBuilderTLS (DN_Str8Builder const *builder) { return DN_Str8Builder_FromBuilder(DN_OS_TLSTopArena(), builder); }
DN_API DN_Str8Builder DN_Str8BuilderFromBuilderFrame (DN_Str8Builder const *builder) { return DN_Str8BuilderFromBuilder(DN_OS_TLSGet()->frame_arena, builder); }
DN_API DN_Str8Builder DN_Str8BuilderFromBuilderTLS (DN_Str8Builder const *builder) { return DN_Str8BuilderFromBuilder(DN_OS_TLSTopArena(), builder); }
DN_API DN_Str8 DN_Str8Builder_BuildFromFrame (DN_Str8Builder const *builder) { return DN_Str8Builder_Build(builder, DN_OS_TLSGet()->frame_arena); }
DN_API DN_Str8 DN_Str8Builder_BuildFromHeap (DN_Str8Builder const *builder, DN_Arena *arena);
DN_API DN_Str8 DN_Str8Builder_BuildFromTLS (DN_Str8Builder const *builder) { return DN_Str8Builder_Build(builder, DN_OS_TLSTopArena()); }
DN_API DN_Str8 DN_Str8BuilderBuildFromFrame (DN_Str8Builder const *builder) { return DN_Str8BuilderBuild(builder, DN_OS_TLSGet()->frame_arena); }
DN_API DN_Str8 DN_Str8BuilderBuildFromHeap (DN_Str8Builder const *builder, DN_Arena *arena);
DN_API DN_Str8 DN_Str8BuilderBuildFromTLS (DN_Str8Builder const *builder) { return DN_Str8BuilderBuild(builder, DN_OS_TLSTopArena()); }
DN_API DN_Str8 DN_Str8Builder_BuildDelimitedFromFrame(DN_Str8Builder const *builder, DN_Str8 delimiter) { return DN_Str8Builder_BuildDelimited(builder, delimiter, DN_OS_TLSGet()->frame_arena); }
DN_API DN_Str8 DN_Str8Builder_BuildDelimitedFromTLS (DN_Str8Builder const *builder, DN_Str8 delimiter) { return DN_Str8Builder_BuildDelimited(builder, delimiter, DN_OS_TLSTopArena()); }
DN_API DN_Str8 DN_Str8BuilderBuildDelimitedFromFrame(DN_Str8Builder const *builder, DN_Str8 delimiter) { return DN_Str8BuilderBuildDelimited(builder, delimiter, DN_OS_TLSGet()->frame_arena); }
DN_API DN_Str8 DN_Str8BuilderBuildDelimitedFromTLS (DN_Str8Builder const *builder, DN_Str8 delimiter) { return DN_Str8BuilderBuildDelimited(builder, delimiter, DN_OS_TLSTopArena()); }
DN_API DN_Slice<DN_Str8> DN_Str8Builder_BuildSliceFromFrame (DN_Str8Builder const *builder) { return DN_Str8Builder_BuildSlice(builder, DN_OS_TLSGet()->frame_arena); }
DN_API DN_Slice<DN_Str8> DN_Str8Builder_BuildSliceFromTLS (DN_Str8Builder const *builder) { return DN_Str8Builder_BuildSlice(builder, DN_OS_TLSTopArena()); }
DN_API DN_Slice<DN_Str8> DN_Str8BuilderBuildSliceFromFrame (DN_Str8Builder const *builder) { return DN_Str8BuilderBuildSlice(builder, DN_OS_TLSGet()->frame_arena); }
DN_API DN_Slice<DN_Str8> DN_Str8BuilderBuildSliceFromTLS (DN_Str8Builder const *builder) { return DN_Str8BuilderBuildSlice(builder, DN_OS_TLSTopArena()); }
#endif // !defined(DN_OS_STRING_H)
+44 -44
View File
@@ -5,7 +5,7 @@ DN_OSTLSTMem::DN_OSTLSTMem(DN_OSTLS *tls, DN_U8 arena_index, DN_OSTLSPushTMem pu
{
DN_Assert(arena_index == DN_OSTLSArena_TMem0 || arena_index == DN_OSTLSArena_TMem1);
arena = tls->arenas + arena_index;
temp_mem = DN_Arena_TempMemBegin(arena);
temp_mem = DN_ArenaTempMemBegin(arena);
destructed = false;
push_arena = push_tmem;
if (push_arena)
@@ -15,7 +15,7 @@ DN_OSTLSTMem::DN_OSTLSTMem(DN_OSTLS *tls, DN_U8 arena_index, DN_OSTLSPushTMem pu
DN_OSTLSTMem::~DN_OSTLSTMem()
{
DN_Assert(destructed == false);
DN_Arena_TempMemEnd(temp_mem);
DN_ArenaTempMemEnd(temp_mem);
destructed = true;
if (push_arena)
DN_OS_TLSPopArena();
@@ -37,9 +37,9 @@ DN_API void DN_OS_TLSInit(DN_OSTLS *tls, DN_OSTLSInitArgs args)
// for setting up the alloc tracking data structures.
for (DN_ForItCArray(it, DN_Arena, tls->arenas)) {
DN_Arena *arena = it.data;
switch (DN_CAST(DN_OSTLSArena) it.index) {
default: *arena = DN_Arena_FromVMem(reserve, commit, DN_ArenaFlags_AllocCanLeak | DN_ArenaFlags_NoAllocTrack); break;
case DN_OSTLSArena_ErrorSink: *arena = DN_Arena_FromVMem(err_sink_reserve, err_sink_commit, DN_ArenaFlags_AllocCanLeak | DN_ArenaFlags_NoAllocTrack); break;
switch (DN_Cast(DN_OSTLSArena) it.index) {
default: *arena = DN_ArenaFromVMem(reserve, commit, DN_ArenaFlags_AllocCanLeak | DN_ArenaFlags_NoAllocTrack); break;
case DN_OSTLSArena_ErrorSink: *arena = DN_ArenaFromVMem(err_sink_reserve, err_sink_commit, DN_ArenaFlags_AllocCanLeak | DN_ArenaFlags_NoAllocTrack); break;
case DN_OSTLSArena_Count: DN_InvalidCodePath; break;
}
}
@@ -55,7 +55,7 @@ DN_API void DN_OS_TLSDeinit(DN_OSTLS *tls)
tls->err_sink = {};
tls->arena_stack_index = {};
for (DN_ForItCArray(it, DN_Arena, tls->arenas))
DN_Arena_Deinit(it.data);
DN_ArenaDeinit(it.data);
}
DN_THREAD_LOCAL DN_OSTLS *g_dn_curr_thread_tls;
@@ -163,22 +163,22 @@ DN_API DN_OSErrSink *DN_OS_ErrSinkBegin_(DN_OSErrSinkMode mode, DN_CallSite call
DN_OSTLS *tls = DN_OS_TLSGet();
DN_OSErrSink *err = &tls->err_sink;
DN_OSErrSink *result = err;
DN_USize arena_pos = DN_Arena_Pos(result->arena);
DN_USize arena_pos = DN_ArenaPos(result->arena);
if (tls->err_sink.stack_size == DN_ArrayCountU(err->stack)) {
DN_Str8Builder builder = DN_Str8Builder_FromTLS();
DN_Str8Builder builder = DN_Str8BuilderFromTLS();
DN_USize counter = 0;
for (DN_ForItSize(it, DN_OSErrSinkNode, err->stack, err->stack_size)) {
DN_MSVC_WARNING_PUSH
DN_MSVC_WARNING_DISABLE(6284) // Object passed as _Param_(4) when a string is required in call to 'DN_Str8Builder_AppendF' Actual type: 'struct DN_Str8'.
DN_Str8Builder_AppendF(&builder, " [%04zu] %S:%u %S\n", counter++, it.data->call_site.file, it.data->call_site.line, it.data->call_site.function);
DN_MSVC_WARNING_DISABLE(6284) // Object passed as _Param_(4) when a string is required in call to 'DN_Str8BuilderAppendF' Actual type: 'struct DN_Str8'.
DN_Str8BuilderAppendF(&builder, " [%04zu] %S:%u %S\n", counter++, it.data->call_site.file, it.data->call_site.line, it.data->call_site.function);
DN_MSVC_WARNING_POP
}
DN_MSVC_WARNING_PUSH
DN_MSVC_WARNING_DISABLE(6284) // Object passed as _Param_(6) when a string is required in call to 'DN_LOG_EmitFromType' Actual type: 'struct DN_Str8'.
DN_AssertF(tls->err_sink.stack_size < DN_ArrayCountU(err->stack),
"Error sink has run out of error scopes, potential leak. Scopes were\n%S", DN_Str8Builder_BuildFromTLS(&builder));
"Error sink has run out of error scopes, potential leak. Scopes were\n%S", DN_Str8BuilderBuildFromTLS(&builder));
DN_MSVC_WARNING_POP
}
@@ -190,7 +190,7 @@ DN_API DN_OSErrSink *DN_OS_ErrSinkBegin_(DN_OSErrSinkMode mode, DN_CallSite call
// NOTE: Handle allocation error
if (!DN_Check(node && node->msg_sentinel)) {
DN_Arena_PopTo(result->arena, arena_pos);
DN_ArenaPopTo(result->arena, arena_pos);
node->msg_sentinel = nullptr;
tls->err_sink.stack_size--;
}
@@ -221,8 +221,8 @@ DN_API DN_OSErrSinkMsg *DN_OS_ErrSinkEnd(DN_Arena *arena, DN_OSErrSink *err)
DN_OSErrSinkNode *node = err->stack + (err->stack_size - 1);
DN_OSErrSinkMsg *prev = nullptr;
for (DN_OSErrSinkMsg *it = node->msg_sentinel->next; it != node->msg_sentinel; it = it->next) {
DN_OSErrSinkMsg *entry = DN_Arena_New(arena, DN_OSErrSinkMsg, DN_ZeroMem_Yes);
entry->msg = DN_Str8_FromStr8(arena, it->msg);
DN_OSErrSinkMsg *entry = DN_ArenaNew(arena, DN_OSErrSinkMsg, DN_ZMem_Yes);
entry->msg = DN_Str8FromStr8Arena(arena, it->msg);
entry->call_site = it->call_site;
entry->error_code = it->error_code;
if (!result)
@@ -234,36 +234,36 @@ DN_API DN_OSErrSinkMsg *DN_OS_ErrSinkEnd(DN_Arena *arena, DN_OSErrSink *err)
// NOTE: Deallocate all the memory for this scope
err->stack_size--;
DN_Arena_PopTo(err->arena, node->arena_pos);
DN_ArenaPopTo(err->arena, node->arena_pos);
return result;
}
static void DN_OS_ErrSinkAddMsgToStr8Builder_(DN_Str8Builder *builder, DN_OSErrSinkMsg *msg, DN_OSErrSinkMsg *end)
static void DN_OS_ErrSinkAddMsgToStr8Builder(DN_Str8Builder *builder, DN_OSErrSinkMsg *msg, DN_OSErrSinkMsg *end)
{
if (msg == end) // NOTE: No error messages to add
return;
if (msg->next == end) {
DN_OSErrSinkMsg *it = msg;
DN_Str8 file_name = DN_Str8_FileNameFromPath(it->call_site.file);
DN_Str8Builder_AppendF(builder,
DN_Str8 file_name = DN_Str8FileNameFromPath(it->call_site.file);
DN_Str8BuilderAppendF(builder,
"%.*s:%05I32u:%.*s %.*s",
DN_STR_FMT(file_name),
DN_Str8PrintFmt(file_name),
it->call_site.line,
DN_STR_FMT(it->call_site.function),
DN_STR_FMT(it->msg));
DN_Str8PrintFmt(it->call_site.function),
DN_Str8PrintFmt(it->msg));
} else {
// NOTE: More than one message
for (DN_OSErrSinkMsg *it = msg; it != end; it = it->next) {
DN_Str8 file_name = DN_Str8_FileNameFromPath(it->call_site.file);
DN_Str8Builder_AppendF(builder,
DN_Str8 file_name = DN_Str8FileNameFromPath(it->call_site.file);
DN_Str8BuilderAppendF(builder,
"%s - %.*s:%05I32u:%.*s%s%.*s",
it == msg ? "" : "\n",
DN_STR_FMT(file_name),
DN_Str8PrintFmt(file_name),
it->call_site.line,
DN_STR_FMT(it->call_site.function),
DN_Str8_HasData(it->msg) ? " " : "",
DN_STR_FMT(it->msg));
DN_Str8PrintFmt(it->call_site.function),
it->msg.size ? " " : "",
DN_Str8PrintFmt(it->msg));
}
}
}
@@ -280,16 +280,16 @@ DN_API DN_Str8 DN_OS_ErrSinkEndStr8(DN_Arena *arena, DN_OSErrSink *err)
// NOTE: Walk the list and allocate it onto the user's arena
DN_OSTLSTMem tmem = DN_OS_TLSPushTMem(arena);
DN_Str8Builder builder = DN_Str8Builder_FromTLS();
DN_Str8Builder builder = DN_Str8BuilderFromTLS();
DN_OSErrSinkNode *node = err->stack + (err->stack_size - 1);
DN_OS_ErrSinkAddMsgToStr8Builder_(&builder, node->msg_sentinel->next, node->msg_sentinel);
DN_OS_ErrSinkAddMsgToStr8Builder(&builder, node->msg_sentinel->next, node->msg_sentinel);
// NOTE: Deallocate all the memory for this scope
err->stack_size--;
DN_U64 arena_pos = node->arena_pos;
DN_Arena_PopTo(err->arena, arena_pos);
DN_ArenaPopTo(err->arena, arena_pos);
result = DN_Str8Builder_Build(&builder, arena);
result = DN_Str8BuilderBuild(&builder, arena);
return result;
}
@@ -310,20 +310,20 @@ DN_API bool DN_OS_ErrSinkEndAndLogError_(DN_OSErrSink *err, DN_CallSite call_sit
if (!msg)
return false;
DN_Str8Builder builder = DN_Str8Builder_FromTLS();
if (DN_Str8_HasData(err_msg)) {
DN_Str8Builder_AppendRef(&builder, err_msg);
DN_Str8Builder_AppendRef(&builder, DN_STR8(":"));
DN_Str8Builder builder = DN_Str8BuilderFromTLS();
if (err_msg.size) {
DN_Str8BuilderAppendRef(&builder, err_msg);
DN_Str8BuilderAppendRef(&builder, DN_Str8Lit(":"));
} else {
DN_Str8Builder_AppendRef(&builder, DN_STR8("Error(s) encountered:"));
DN_Str8BuilderAppendRef(&builder, DN_Str8Lit("Error(s) encountered:"));
}
if (msg->next) // NOTE: More than 1 message
DN_Str8Builder_AppendRef(&builder, DN_STR8("\n"));
DN_OS_ErrSinkAddMsgToStr8Builder_(&builder, msg, nullptr);
DN_Str8BuilderAppendRef(&builder, DN_Str8Lit("\n"));
DN_OS_ErrSinkAddMsgToStr8Builder(&builder, msg, nullptr);
DN_Str8 log = DN_Str8Builder_BuildFromTLS(&builder);
DN_LOG_EmitFromType(DN_LOG_MakeU32LogTypeParam(DN_LOGType_Error), call_site, "%.*s", DN_STR_FMT(log));
DN_Str8 log = DN_Str8BuilderBuildFromTLS(&builder);
DN_LOG_EmitFromType(DN_LOG_MakeU32LogTypeParam(DN_LOGType_Error), call_site, "%.*s", DN_Str8PrintFmt(log));
if (mode == DN_OSErrSinkMode_DebugBreakOnEndAndLog)
DN_DebugBreak;
@@ -333,7 +333,7 @@ DN_API bool DN_OS_ErrSinkEndAndLogError_(DN_OSErrSink *err, DN_CallSite call_sit
DN_API bool DN_OS_ErrSinkEndAndLogErrorFV_(DN_OSErrSink *err, DN_CallSite call_site, DN_FMT_ATTRIB char const *fmt, va_list args)
{
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_Str8 log = DN_Str8_FromFV(tmem.arena, fmt, args);
DN_Str8 log = DN_Str8FromFmtVArena(tmem.arena, fmt, args);
bool result = DN_OS_ErrSinkEndAndLogError_(err, call_site, log);
return result;
}
@@ -343,7 +343,7 @@ DN_API bool DN_OS_ErrSinkEndAndLogErrorF_(DN_OSErrSink *err, DN_CallSite call_si
va_list args;
va_start(args, fmt);
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_Str8 log = DN_Str8_FromFV(tmem.arena, fmt, args);
DN_Str8 log = DN_Str8FromFmtVArena(tmem.arena, fmt, args);
bool result = DN_OS_ErrSinkEndAndLogError_(err, call_site, log);
va_end(args);
return result;
@@ -373,9 +373,9 @@ DN_API void DN_OS_ErrSinkAppendFV_(DN_OSErrSink *err, DN_U32 error_code, DN_FMT_
DN_OSErrSinkNode *node = err->stack + (err->stack_size - 1);
DN_AssertF(node, "Error sink must be begun by calling 'Begin' before using this function.");
DN_OSErrSinkMsg *msg = DN_Arena_New(err->arena, DN_OSErrSinkMsg, DN_ZeroMem_Yes);
DN_OSErrSinkMsg *msg = DN_ArenaNew(err->arena, DN_OSErrSinkMsg, DN_ZMem_Yes);
if (DN_Check(msg)) {
msg->msg = DN_Str8_FromFV(err->arena, fmt, args);
msg->msg = DN_Str8FromFmtVArena(err->arena, fmt, args);
msg->error_code = error_code;
msg->call_site = DN_OS_TLSGet()->call_site;
DN_DLList_Prepend(node->msg_sentinel, msg);
+140 -140
View File
@@ -94,8 +94,8 @@ DN_API int DN_OS_MemProtect(void *ptr, DN_USize size, DN_U32 page_flags)
return 0;
static DN_Str8 const ALIGNMENT_ERROR_MSG =
DN_STR8("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_core_->page_size), "%s", ALIGNMENT_ERROR_MSG.data);
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_core_->page_size), "%s", ALIGNMENT_ERROR_MSG.data);
DN_AssertF(DN_IsPowerOfTwoAligned(size, g_dn_os_core_->page_size), "%s", ALIGNMENT_ERROR_MSG.data);
unsigned long os_page_flags = DN_OS_MemConvertPageToOSFlags_(page_flags);
@@ -108,11 +108,11 @@ DN_API int DN_OS_MemProtect(void *ptr, DN_USize size, DN_U32 page_flags)
return result;
}
DN_API void *DN_OS_MemAlloc(DN_USize size, DN_ZeroMem zero_mem)
DN_API void *DN_OS_MemAlloc(DN_USize size, DN_ZMem z_mem)
{
DN_U32 flags = zero_mem == DN_ZeroMem_Yes ? HEAP_ZERO_MEMORY : 0;
DN_Assert(size <= DN_CAST(DWORD)(-1));
void *result = HeapAlloc(GetProcessHeap(), flags, DN_CAST(DWORD) size);
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_os_core_);
DN_AtomicAddU64(&g_dn_os_core_->mem_allocs_total, 1);
DN_AtomicAddU64(&g_dn_os_core_->mem_allocs_frame, 1);
@@ -131,12 +131,12 @@ DN_API DN_OSDateTime DN_OS_DateLocalTimeNow()
GetLocalTime(&sys_time);
DN_OSDateTime result = {};
result.hour = DN_CAST(uint8_t) sys_time.wHour;
result.minutes = DN_CAST(uint8_t) sys_time.wMinute;
result.seconds = DN_CAST(uint8_t) sys_time.wSecond;
result.day = DN_CAST(uint8_t) sys_time.wDay;
result.month = DN_CAST(uint8_t) sys_time.wMonth;
result.year = DN_CAST(int16_t) sys_time.wYear;
result.hour = DN_Cast(uint8_t) sys_time.wHour;
result.minutes = DN_Cast(uint8_t) sys_time.wMinute;
result.seconds = DN_Cast(uint8_t) sys_time.wSecond;
result.day = DN_Cast(uint8_t) sys_time.wDay;
result.month = DN_Cast(uint8_t) sys_time.wMonth;
result.year = DN_Cast(int16_t) sys_time.wYear;
return result;
}
@@ -214,22 +214,22 @@ DN_API DN_OSDateTime DN_OS_DateUnixTimeSToDate(DN_U64 time)
FileTimeToSystemTime(&file_time, &sys_time);
DN_OSDateTime result = {};
result.year = DN_CAST(uint16_t) sys_time.wYear;
result.month = DN_CAST(uint8_t) sys_time.wMonth;
result.day = DN_CAST(uint8_t) sys_time.wDay;
result.hour = DN_CAST(uint8_t) sys_time.wHour;
result.minutes = DN_CAST(uint8_t) sys_time.wMinute;
result.seconds = DN_CAST(uint8_t) sys_time.wSecond;
result.year = DN_Cast(uint16_t) sys_time.wYear;
result.month = DN_Cast(uint8_t) sys_time.wMonth;
result.day = DN_Cast(uint8_t) sys_time.wDay;
result.hour = DN_Cast(uint8_t) sys_time.wHour;
result.minutes = DN_Cast(uint8_t) sys_time.wMinute;
result.seconds = DN_Cast(uint8_t) sys_time.wSecond;
return result;
}
DN_API void DN_OS_GenBytesSecure(void *buffer, DN_U32 size)
{
DN_Assert(g_dn_os_core_);
DN_W32Core *w32 = DN_CAST(DN_W32Core *) g_dn_os_core_->platform_context;
DN_W32Core *w32 = DN_Cast(DN_W32Core *) g_dn_os_core_->platform_context;
DN_Assert(w32->bcrypt_init_success);
long gen_status = BCryptGenRandom(w32->bcrypt_rng_handle, DN_CAST(unsigned char *) buffer, size, 0 /*flags*/);
long gen_status = BCryptGenRandom(w32->bcrypt_rng_handle, DN_Cast(unsigned char *) buffer, size, 0 /*flags*/);
// NOTE: This can only fail if the handle is invalid or one or more parameters are invalid. We
// validate our parameters so this shouldn't be the case.
DN_Assert(gen_status == 0);
@@ -284,7 +284,7 @@ DN_API void DN_OS_SleepMs(DN_UInt milliseconds)
DN_API DN_U64 DN_OS_PerfCounterFrequency()
{
DN_Assert(g_dn_os_core_);
DN_W32Core *w32 = DN_CAST(DN_W32Core *) g_dn_os_core_->platform_context;
DN_W32Core *w32 = DN_Cast(DN_W32Core *) g_dn_os_core_->platform_context;
DN_Assert(w32->qpc_frequency.QuadPart);
DN_U64 result = w32->qpc_frequency.QuadPart;
return result;
@@ -322,10 +322,10 @@ DN_API bool DN_OS_FileCopy(DN_Str8 src, DN_Str8 dest, bool overwrite, DN_OSErrSi
DN_OS_ErrSinkAppendF(err,
win_error.code,
"Failed to copy file '%.*s' to '%.*s': (%u) %.*s",
DN_STR_FMT(src),
DN_STR_FMT(dest),
DN_Str8PrintFmt(src),
DN_Str8PrintFmt(dest),
win_error.code,
DN_STR_FMT(win_error.msg));
DN_Str8PrintFmt(win_error.msg));
}
return result;
}
@@ -347,10 +347,10 @@ DN_API bool DN_OS_FileMove(DN_Str8 src, DN_Str8 dest, bool overwrite, DN_OSErrSi
DN_OS_ErrSinkAppendF(err,
win_error.code,
"Failed to move file '%.*s' to '%.*s': (%u) %.*s",
DN_STR_FMT(src),
DN_STR_FMT(dest),
DN_Str8PrintFmt(src),
DN_Str8PrintFmt(dest),
win_error.code,
DN_STR_FMT(win_error.msg));
DN_Str8PrintFmt(win_error.msg));
}
return result;
}
@@ -358,7 +358,7 @@ DN_API bool DN_OS_FileMove(DN_Str8 src, DN_Str8 dest, bool overwrite, DN_OSErrSi
DN_API DN_OSFile DN_OS_FileOpen(DN_Str8 path, DN_OSFileOpen open_mode, DN_U32 access, DN_OSErrSink *err)
{
DN_OSFile result = {};
if (!DN_Str8_HasData(path) || path.size <= 0)
if (path.size == 0 || path.size <= 0)
return result;
if ((access & ~DN_OSFileAccess_All) || ((access & DN_OSFileAccess_All) == 0)) {
@@ -401,7 +401,7 @@ DN_API DN_OSFile DN_OS_FileOpen(DN_Str8 path, DN_OSFileOpen open_mode, DN_U32 ac
if (handle == INVALID_HANDLE_VALUE) {
DN_W32Error win_error = DN_W32_LastError(tmem.arena);
result.error = true;
DN_OS_ErrSinkAppendF(err, win_error.code, "Failed to open file at '%.*s': '%.*s'", DN_STR_FMT(path), DN_STR_FMT(win_error.msg));
DN_OS_ErrSinkAppendF(err, win_error.code, "Failed to open file at '%.*s': '%.*s'", DN_Str8PrintFmt(path), DN_Str8PrintFmt(win_error.msg));
return result;
}
@@ -417,24 +417,24 @@ DN_API DN_OSFileRead DN_OS_FileRead(DN_OSFile *file, void *buffer, DN_USize size
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
if (!DN_Check(size <= (unsigned long)-1)) {
DN_Str8 buffer_size_str8 = DN_CVT_BytesStr8FromU64AutoTLS(size);
DN_Str8x32 buffer_size_str8 = DN_ByteCountStr8x32(size);
DN_OS_ErrSinkAppendF(
err,
1 /*error_code*/,
"Current implementation doesn't support reading >4GiB file (requested %.*s), implement Win32 overlapped IO",
DN_STR_FMT(buffer_size_str8));
DN_Str8PrintFmt(buffer_size_str8));
return result;
}
unsigned long bytes_read = 0;
unsigned long read_result = ReadFile(/*HANDLE hFile*/ file->handle,
/*LPVOID lpBuffer*/ buffer,
/*DWORD nNumberOfBytesToRead*/ DN_CAST(unsigned long) size,
/*DWORD nNumberOfBytesToRead*/ DN_Cast(unsigned long) size,
/*LPDWORD lpNumberOfByesRead*/ &bytes_read,
/*LPOVERLAPPED lpOverlapped*/ nullptr);
if (read_result == 0) {
DN_W32Error win_error = DN_W32_LastError(tmem.arena);
DN_OS_ErrSinkAppendF(err, win_error.code, "Failed to read data from file: (%u) %.*s", win_error.code, DN_STR_FMT(win_error.msg));
DN_OS_ErrSinkAppendF(err, win_error.code, "Failed to read data from file: (%u) %.*s", win_error.code, DN_Str8PrintFmt(win_error.msg));
return result;
}
@@ -445,9 +445,9 @@ DN_API DN_OSFileRead DN_OS_FileRead(DN_OSFile *file, void *buffer, DN_USize size
win_error.code,
"Failed to read the desired number of bytes from file, we read %uB but we expected %uB: (%u) %.*s",
bytes_read,
DN_CAST(unsigned long) size,
DN_Cast(unsigned long) size,
win_error.code,
DN_STR_FMT(win_error.msg));
DN_Str8PrintFmt(win_error.msg));
return result;
}
@@ -462,9 +462,9 @@ DN_API bool DN_OS_FileWritePtr(DN_OSFile *file, void const *buffer, DN_USize siz
return false;
bool result = true;
char const *end = DN_CAST(char *) buffer + size;
for (char const *ptr = DN_CAST(char const *) buffer; result && ptr != end;) {
unsigned long write_size = DN_CAST(unsigned long) DN_Min((unsigned long)-1, end - ptr);
char const *end = DN_Cast(char *) buffer + size;
for (char const *ptr = DN_Cast(char const *) buffer; result && ptr != end;) {
unsigned long write_size = DN_Cast(unsigned long) DN_Min((unsigned long)-1, end - ptr);
unsigned long bytes_written = 0;
result = WriteFile(file->handle, ptr, write_size, &bytes_written, nullptr /*lpOverlapped*/) != 0;
ptr += bytes_written;
@@ -473,8 +473,8 @@ DN_API bool DN_OS_FileWritePtr(DN_OSFile *file, void const *buffer, DN_USize siz
if (!result) {
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_W32Error win_error = DN_W32_LastError(tmem.arena);
DN_Str8 buffer_size_str8 = DN_CVT_BytesStr8FromU64AutoTLS(size);
DN_OS_ErrSinkAppendF(err, win_error.code, "Failed to write buffer (%.*s) to file handle: %.*s", DN_STR_FMT(buffer_size_str8), DN_STR_FMT(win_error.msg));
DN_Str8x32 buffer_size_str8 = DN_ByteCountStr8x32(size);
DN_OS_ErrSinkAppendF(err, win_error.code, "Failed to write buffer (%.*s) to file handle: %.*s", DN_Str8PrintFmt(buffer_size_str8), DN_Str8PrintFmt(win_error.msg));
}
return result;
}
@@ -484,14 +484,14 @@ DN_API bool DN_OS_FileFlush(DN_OSFile *file, DN_OSErrSink *err)
if (!file || !file->handle || file->error)
return false;
BOOL result = FlushFileBuffers(DN_CAST(HANDLE) file->handle);
BOOL result = FlushFileBuffers(DN_Cast(HANDLE) file->handle);
if (!result) {
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_W32Error win_error = DN_W32_LastError(tmem.arena);
DN_OS_ErrSinkAppendF(err, win_error.code, "Failed to flush file buffer to disk: %.*s", DN_STR_FMT(win_error.msg));
DN_OS_ErrSinkAppendF(err, win_error.code, "Failed to flush file buffer to disk: %.*s", DN_Str8PrintFmt(win_error.msg));
}
return DN_CAST(bool) result;
return DN_Cast(bool) result;
}
DN_API void DN_OS_FileClose(DN_OSFile *file)
@@ -505,7 +505,7 @@ DN_API void DN_OS_FileClose(DN_OSFile *file)
DN_API DN_OSPathInfo DN_OS_PathInfo(DN_Str8 path)
{
DN_OSPathInfo result = {};
if (!DN_Str8_HasData(path))
if (path.size == 0)
return result;
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
@@ -521,7 +521,7 @@ DN_API DN_OSPathInfo DN_OS_PathInfo(DN_Str8 path)
result.last_write_time_in_s = DN_W32_FileTimeToSeconds_(&attrib_data.ftLastWriteTime);
LARGE_INTEGER large_int = {};
large_int.u.HighPart = DN_CAST(int32_t) attrib_data.nFileSizeHigh;
large_int.u.HighPart = DN_Cast(int32_t) attrib_data.nFileSizeHigh;
large_int.u.LowPart = attrib_data.nFileSizeLow;
result.size = (DN_U64)large_int.QuadPart;
@@ -538,7 +538,7 @@ DN_API DN_OSPathInfo DN_OS_PathInfo(DN_Str8 path)
DN_API bool DN_OS_PathDelete(DN_Str8 path)
{
bool result = false;
if (!DN_Str8_HasData(path))
if (path.size == 0)
return result;
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
@@ -554,7 +554,7 @@ DN_API bool DN_OS_PathDelete(DN_Str8 path)
DN_API bool DN_OS_PathIsFile(DN_Str8 path)
{
bool result = false;
if (!DN_Str8_HasData(path))
if (path.size == 0)
return result;
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
@@ -571,7 +571,7 @@ DN_API bool DN_OS_PathIsFile(DN_Str8 path)
DN_API bool DN_OS_PathIsDir(DN_Str8 path)
{
bool result = false;
if (!DN_Str8_HasData(path))
if (path.size == 0)
return result;
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
@@ -639,7 +639,7 @@ DN_API bool DN_OS_PathMakeDir(DN_Str8 path)
DN_API bool DN_OS_PathIterateDir(DN_Str8 path, DN_OSDirIterator *it)
{
if (!DN_Str8_HasData(path) || !it || path.size <= 0)
if (path.size == 0 || !it || path.size <= 0)
return false;
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
@@ -648,10 +648,10 @@ DN_API bool DN_OS_PathIterateDir(DN_Str8 path, DN_OSDirIterator *it)
if (it->handle) {
wide_it.handle = it->handle;
} else {
bool needs_asterisks = DN_Str8_EndsWith(path, DN_STR8("\\")) ||
DN_Str8_EndsWith(path, DN_STR8("/"));
bool has_glob = DN_Str8_EndsWith(path, DN_STR8("\\*")) ||
DN_Str8_EndsWith(path, DN_STR8("/*"));
bool needs_asterisks = DN_Str8EndsWith(path, DN_Str8Lit("\\")) ||
DN_Str8EndsWith(path, DN_Str8Lit("/"));
bool has_glob = DN_Str8EndsWith(path, DN_Str8Lit("\\*")) ||
DN_Str8EndsWith(path, DN_Str8Lit("/*"));
DN_Str8 adjusted_path = path;
if (!has_glob) {
@@ -659,9 +659,9 @@ DN_API bool DN_OS_PathIterateDir(DN_Str8 path, DN_OSDirIterator *it)
// add those characters in this branch, so overwrite the null
// character, add the glob and re-null terminate the buffer.
if (needs_asterisks)
adjusted_path = DN_OS_PathF(tmem.arena, "%.*s*", DN_STR_FMT(path));
adjusted_path = DN_OS_PathF(tmem.arena, "%.*s*", DN_Str8PrintFmt(path));
else
adjusted_path = DN_OS_PathF(tmem.arena, "%.*s/*", DN_STR_FMT(path));
adjusted_path = DN_OS_PathF(tmem.arena, "%.*s/*", DN_Str8PrintFmt(path));
}
path16 = DN_W32_Str8ToStr16(tmem.arena, adjusted_path);
@@ -673,7 +673,7 @@ DN_API bool DN_OS_PathIterateDir(DN_Str8 path, DN_OSDirIterator *it)
it->handle = wide_it.handle;
if (result) {
int size = DN_W32_Str16ToStr8Buffer(wide_it.file_name, it->buffer, DN_ArrayCountU(it->buffer));
it->file_name = DN_Str8_Init(it->buffer, size);
it->file_name = DN_Str8FromPtr(it->buffer, size);
}
return result;
@@ -683,7 +683,7 @@ DN_API bool DN_OS_PathIterateDir(DN_Str8 path, DN_OSDirIterator *it)
// NOTE: DN_OSExec /////////////////////////////////////////////////////////////////////////////////
DN_API void DN_OS_Exit(int32_t exit_code)
{
ExitProcess(DN_CAST(UINT) exit_code);
ExitProcess(DN_Cast(UINT) exit_code);
}
DN_API DN_OSExecResult DN_OS_ExecPump(DN_OSExecAsyncHandle handle,
@@ -734,7 +734,7 @@ DN_API DN_OSExecResult DN_OS_ExecPump(DN_OSExecAsyncHandle handle,
if (exec_result == WAIT_FAILED) {
DN_W32Error win_error = DN_W32_LastError(tmem.arena);
result.os_error_code = win_error.code;
DN_OS_ErrSinkAppendF(err, result.os_error_code, "Executed command failed to terminate: %.*s", DN_STR_FMT(win_error.msg));
DN_OS_ErrSinkAppendF(err, result.os_error_code, "Executed command failed to terminate: %.*s", DN_Str8PrintFmt(win_error.msg));
} else if (DN_Check(exec_result == WAIT_TIMEOUT || exec_result == WAIT_OBJECT_0)) {
// NOTE: Read stdout from process //////////////////////////////////////////////////////
// If the pipes are full, the process will block. We periodically
@@ -746,7 +746,7 @@ DN_API DN_OSExecResult DN_OS_ExecPump(DN_OSExecAsyncHandle handle,
DWORD bytes_read = 0;
char *dest_buffer = handle.stdout_write && stdout_buffer ? stdout_buffer : sink;
size_t dest_size = handle.stdout_write && stdout_buffer ? stdout_buffer_size : DN_ArrayCountU(sink);
BOOL success = ReadFile(handle.stdout_read, dest_buffer, DN_CAST(DWORD) dest_size, &bytes_read, NULL);
BOOL success = ReadFile(handle.stdout_read, dest_buffer, DN_Cast(DWORD) dest_size, &bytes_read, NULL);
(void)success; // TODO:
if (stdout_size)
*stdout_size = bytes_read;
@@ -760,7 +760,7 @@ DN_API DN_OSExecResult DN_OS_ExecPump(DN_OSExecAsyncHandle handle,
char *dest_buffer = handle.stderr_write && stderr_buffer ? stderr_buffer : sink;
size_t dest_size = handle.stderr_write && stderr_buffer ? stderr_buffer_size : DN_ArrayCountU(sink);
DWORD bytes_read = 0;
BOOL success = ReadFile(handle.stderr_read, dest_buffer, DN_CAST(DWORD) dest_size, &bytes_read, NULL);
BOOL success = ReadFile(handle.stderr_read, dest_buffer, DN_Cast(DWORD) dest_size, &bytes_read, NULL);
(void)success; // TODO:
if (stderr_size)
*stderr_size = bytes_read;
@@ -779,7 +779,7 @@ DN_API DN_OSExecResult DN_OS_ExecPump(DN_OSExecAsyncHandle handle,
DN_OS_ErrSinkAppendF(err,
result.os_error_code,
"Failed to retrieve command exit code: %.*s",
DN_STR_FMT(win_error.msg));
DN_Str8PrintFmt(win_error.msg));
}
// NOTE: Cleanup ///////////////////////////////////////////////////////////////////////////////
@@ -790,8 +790,8 @@ DN_API DN_OSExecResult DN_OS_ExecPump(DN_OSExecAsyncHandle handle,
CloseHandle(handle.process);
}
result.stdout_text = DN_Str8_Init(stdout_buffer, stdout_size ? *stdout_size : 0);
result.stderr_text = DN_Str8_Init(stderr_buffer, stderr_size ? *stderr_size : 0);
result.stdout_text = DN_Str8FromPtr(stdout_buffer, stdout_size ? *stdout_size : 0);
result.stderr_text = DN_Str8FromPtr(stderr_buffer, stderr_size ? *stderr_size : 0);
return result;
}
@@ -827,17 +827,17 @@ DN_API DN_OSExecResult DN_OS_ExecWait(DN_OSExecAsyncHandle handle, DN_Arena *are
while (!result.finished) {
size_t stdout_size = DN_Kilobytes(8);
size_t stderr_size = DN_Kilobytes(8);
char *stdout_buffer = DN_Arena_NewArray(tmem.arena, char, stdout_size, DN_ZeroMem_No);
char *stderr_buffer = DN_Arena_NewArray(tmem.arena, char, stderr_size, DN_ZeroMem_No);
char *stdout_buffer = DN_ArenaNewArray(tmem.arena, char, stdout_size, DN_ZMem_No);
char *stderr_buffer = DN_ArenaNewArray(tmem.arena, char, stderr_size, DN_ZMem_No);
result = DN_OS_ExecPump(handle, stdout_buffer, &stdout_size, stderr_buffer, &stderr_size, wait_ms, err);
DN_Str8Builder_AppendCopy(&stdout_builder, result.stdout_text);
DN_Str8Builder_AppendCopy(&stderr_builder, result.stderr_text);
wait_ms = (DN_Str8_HasData(result.stdout_text) || DN_Str8_HasData(result.stderr_text)) ? FAST_WAIT_TIME_MS : SLOW_WAIT_TIME_MS;
DN_Str8BuilderAppendCopy(&stdout_builder, result.stdout_text);
DN_Str8BuilderAppendCopy(&stderr_builder, result.stderr_text);
wait_ms = (result.stdout_text.size || result.stderr_text.size) ? FAST_WAIT_TIME_MS : SLOW_WAIT_TIME_MS;
}
// NOTE: Get stdout/stderr. If no arena is passed this is a no-op //////////////////////////////
result.stdout_text = DN_Str8Builder_Build(&stdout_builder, arena);
result.stderr_text = DN_Str8Builder_Build(&stderr_builder, arena);
result.stdout_text = DN_Str8BuilderBuild(&stdout_builder, arena);
result.stderr_text = DN_Str8BuilderBuild(&stderr_builder, arena);
return result;
}
@@ -849,16 +849,16 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line, DN_OSExe
return result;
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_Str8 cmd_rendered = DN_Slice_Str8Render(tmem.arena, cmd_line, DN_STR8(" "));
DN_Str8 cmd_rendered = DN_Slice_Str8Render(tmem.arena, cmd_line, DN_Str8Lit(" "));
DN_Str16 cmd16 = DN_W32_Str8ToStr16(tmem.arena, cmd_rendered);
DN_Str16 working_dir16 = DN_W32_Str8ToStr16(tmem.arena, args->working_dir);
DN_Str8Builder env_builder = DN_Str8Builder_FromTLS();
DN_Str8Builder_AppendArrayRef(&env_builder, args->environment.data, args->environment.size);
DN_Str8Builder env_builder = DN_Str8BuilderFromTLS();
DN_Str8BuilderAppendArrayRef(&env_builder, args->environment.data, args->environment.size);
if (env_builder.string_size)
DN_Str8Builder_AppendRef(&env_builder, DN_STR8("\0"));
DN_Str8BuilderAppendRef(&env_builder, DN_Str8Lit("\0"));
DN_Str8 env_block8 = DN_Str8Builder_BuildDelimitedFromTLS(&env_builder, DN_STR8("\0"));
DN_Str8 env_block8 = DN_Str8BuilderBuildDelimitedFromTLS(&env_builder, DN_Str8Lit("\0"));
DN_Str16 env_block16 = {};
if (env_block8.size)
env_block16 = DN_W32_Str8ToStr16(tmem.arena, env_block8);
@@ -887,8 +887,8 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line, DN_OSExe
err,
result.os_error_code,
"Failed to create stdout pipe to redirect the output of the command '%.*s': %.*s",
DN_STR_FMT(cmd_rendered),
DN_STR_FMT(win_error.msg));
DN_Str8PrintFmt(cmd_rendered),
DN_Str8PrintFmt(win_error.msg));
return result;
}
@@ -899,8 +899,8 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line, DN_OSExe
result.os_error_code,
"Failed to make stdout 'read' pipe non-inheritable when trying to "
"execute command '%.*s': %.*s",
DN_STR_FMT(cmd_rendered),
DN_STR_FMT(win_error.msg));
DN_Str8PrintFmt(cmd_rendered),
DN_Str8PrintFmt(win_error.msg));
return result;
}
}
@@ -928,8 +928,8 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line, DN_OSExe
err,
result.os_error_code,
"Failed to create stderr pipe to redirect the output of the command '%.*s': %.*s",
DN_STR_FMT(cmd_rendered),
DN_STR_FMT(win_error.msg));
DN_Str8PrintFmt(cmd_rendered),
DN_Str8PrintFmt(win_error.msg));
return result;
}
@@ -940,8 +940,8 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line, DN_OSExe
result.os_error_code,
"Failed to make stderr 'read' pipe non-inheritable when trying to "
"execute command '%.*s': %.*s",
DN_STR_FMT(cmd_rendered),
DN_STR_FMT(win_error.msg));
DN_Str8PrintFmt(cmd_rendered),
DN_Str8PrintFmt(win_error.msg));
return result;
}
}
@@ -971,8 +971,8 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line, DN_OSExe
DN_OS_ErrSinkAppendF(err,
result.os_error_code,
"Failed to execute command '%.*s': %.*s",
DN_STR_FMT(cmd_rendered),
DN_STR_FMT(win_error.msg));
DN_Str8PrintFmt(cmd_rendered),
DN_Str8PrintFmt(win_error.msg));
return result;
}
@@ -992,7 +992,7 @@ DN_API DN_OSExecAsyncHandle DN_OS_ExecAsync(DN_Slice<DN_Str8> cmd_line, DN_OSExe
static DN_W32Core *DN_OS_GetW32Core_()
{
DN_Assert(g_dn_os_core_ && g_dn_os_core_->platform_context);
DN_W32Core *result = DN_CAST(DN_W32Core *)g_dn_os_core_->platform_context;
DN_W32Core *result = DN_Cast(DN_W32Core *)g_dn_os_core_->platform_context;
return result;
}
@@ -1023,7 +1023,7 @@ static DN_W32SyncPrimitive *DN_W32_AllocSyncPrimitive_()
result->next = nullptr;
} else {
DN_OSCore *os = g_dn_os_core_;
result = DN_Arena_New(&os->arena, DN_W32SyncPrimitive, DN_ZeroMem_Yes);
result = DN_ArenaNew(&os->arena, DN_W32SyncPrimitive, DN_ZMem_Yes);
}
}
LeaveCriticalSection(&w32->sync_primitive_free_list_mutex);
@@ -1164,7 +1164,7 @@ DN_API bool DN_OS_ConditionVariableWait(DN_OSConditionVariable *cv, DN_OSMutex *
if (mutex && cv && mutex->handle != 0 && cv->handle != 0 && sleep_ms > 0) {
DN_W32SyncPrimitive *mutex_primitive = DN_OS_U64ToW32SyncPrimitive_(mutex->handle);
DN_W32SyncPrimitive *cv_primitive = DN_OS_U64ToW32SyncPrimitive_(cv->handle);
result = SleepConditionVariableCS(&cv_primitive->cv, &mutex_primitive->mutex, DN_CAST(DWORD) sleep_ms);
result = SleepConditionVariableCS(&cv_primitive->cv, &mutex_primitive->mutex, DN_Cast(DWORD) sleep_ms);
}
return result;
}
@@ -1248,7 +1248,7 @@ DN_API DN_U32 DN_OS_ThreadID()
DN_API void DN_W32_ThreadSetName(DN_Str8 name)
{
DN_OSTLS *tls = DN_OS_TLSGet();
DN_ArenaTempMem tmem = DN_Arena_TempMemBegin(tls->arenas + DN_OSTLSArena_TMem0);
DN_ArenaTempMem tmem = DN_ArenaTempMemBegin(tls->arenas + DN_OSTLSArena_TMem0);
// NOTE: SetThreadDescription is only available in
// Windows Server 2016, Windows 10 LTSB 2016 and Windows 10 version 1607
@@ -1258,7 +1258,7 @@ DN_API void DN_W32_ThreadSetName(DN_Str8 name)
if (w32->set_thread_description) {
DN_Str16 name16 = DN_W32_Str8ToStr16(tmem.arena, name);
w32->set_thread_description(GetCurrentThread(), (WCHAR *)name16.data);
DN_Arena_TempMemEnd(tmem);
DN_ArenaTempMemEnd(tmem);
return;
}
@@ -1275,7 +1275,7 @@ DN_API void DN_W32_ThreadSetName(DN_Str8 name)
#pragma pack(pop)
DN_Str8 copy = DN_Str8_FromStr8(tmem.arena, name);
DN_Str8 copy = DN_Str8FromStr8Arena(tmem.arena, name);
DN_W32ThreadNameInfo info = {};
info.dwType = 0x1000;
info.szName = (char *)copy.data;
@@ -1291,7 +1291,7 @@ DN_API void DN_W32_ThreadSetName(DN_Str8 name)
}
DN_MSVC_WARNING_POP
DN_Arena_TempMemEnd(tmem);
DN_ArenaTempMemEnd(tmem);
}
// NOTE: DN_OSHttp /////////////////////////////////////////////////////////////////////////////////
@@ -1300,8 +1300,8 @@ void DN_OS_HttpRequestWin32Callback(HINTERNET session, DWORD *dwContext, DWORD d
(void)session;
(void)dwStatusInformationLength;
DN_OSHttpResponse *response = DN_CAST(DN_OSHttpResponse *) dwContext;
HINTERNET request = DN_CAST(HINTERNET) response->w32_request_handle;
DN_OSHttpResponse *response = DN_Cast(DN_OSHttpResponse *) dwContext;
HINTERNET request = DN_Cast(HINTERNET) response->w32_request_handle;
DN_W32Error error = {};
DWORD const READ_BUFFER_SIZE = DN_Megabytes(1);
@@ -1331,7 +1331,7 @@ void DN_OS_HttpRequestWin32Callback(HINTERNET session, DWORD *dwContext, DWORD d
&status,
&status_size,
WINHTTP_NO_HEADER_INDEX)) {
response->http_status = DN_CAST(uint16_t) status;
response->http_status = DN_Cast(uint16_t) status;
// NOTE: You can normally call into WinHttpQueryDataAvailable which means the kernel
// will buffer the response into a single buffer and return us the full size of the
@@ -1343,7 +1343,7 @@ void DN_OS_HttpRequestWin32Callback(HINTERNET session, DWORD *dwContext, DWORD d
// This is advantageous to avoid a copy from the kernel buffer into our buffer. If the
// end user application knows the typical payload size then they can optimise for this
// to prevent unnecessary allocation on the user side.
void *buffer = DN_Arena_Alloc(response->builder.arena, READ_BUFFER_SIZE, 1 /*align*/, DN_ZeroMem_No);
void *buffer = DN_ArenaAlloc(response->builder.arena, READ_BUFFER_SIZE, 1 /*align*/, DN_ZMem_No);
if (!WinHttpReadData(request, buffer, READ_BUFFER_SIZE, nullptr))
error = DN_W32_LastError(&response->tmp_arena);
} else {
@@ -1353,17 +1353,17 @@ void DN_OS_HttpRequestWin32Callback(HINTERNET session, DWORD *dwContext, DWORD d
} else if (dwInternetStatus == WINHTTP_CALLBACK_STATUS_READ_COMPLETE) {
DWORD bytes_read = dwStatusInformationLength;
if (bytes_read) {
DN_Str8 prev_buffer = DN_Str8_Init(DN_CAST(char *) lpvStatusInformation, bytes_read);
DN_Str8Builder_AppendRef(&response->builder, prev_buffer);
DN_Str8 prev_buffer = DN_Str8FromPtr(DN_Cast(char *) lpvStatusInformation, bytes_read);
DN_Str8BuilderAppendRef(&response->builder, prev_buffer);
void *buffer = DN_Arena_Alloc(response->builder.arena, READ_BUFFER_SIZE, 1 /*align*/, DN_ZeroMem_No);
void *buffer = DN_ArenaAlloc(response->builder.arena, READ_BUFFER_SIZE, 1 /*align*/, DN_ZMem_No);
if (!WinHttpReadData(request, buffer, READ_BUFFER_SIZE, nullptr))
error = DN_W32_LastError(&response->tmp_arena);
}
} else if (dwInternetStatus == WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE) {
} else if (dwInternetStatus == WINHTTP_CALLBACK_STATUS_REQUEST_ERROR) {
WINHTTP_ASYNC_RESULT *async_result = DN_CAST(WINHTTP_ASYNC_RESULT *) lpvStatusInformation;
error = DN_W32_ErrorCodeToMsg(&response->tmp_arena, DN_CAST(DN_U32) async_result->dwError);
WINHTTP_ASYNC_RESULT *async_result = DN_Cast(WINHTTP_ASYNC_RESULT *) lpvStatusInformation;
error = DN_W32_ErrorCodeToMsg(&response->tmp_arena, DN_Cast(DN_U32) async_result->dwError);
} else if (dwInternetStatus == WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE) {
if (!WinHttpReceiveResponse(request, 0))
error = DN_W32_LastError(&response->tmp_arena);
@@ -1375,7 +1375,7 @@ void DN_OS_HttpRequestWin32Callback(HINTERNET session, DWORD *dwContext, DWORD d
if (request) {
bool read_complete = dwInternetStatus == WINHTTP_CALLBACK_STATUS_READ_COMPLETE && dwStatusInformationLength == 0;
if (read_complete)
response->body = DN_Str8Builder_Build(&response->builder, response->arena);
response->body = DN_Str8BuilderBuild(&response->builder, response->arena);
if (read_complete || dwInternetStatus == WINHTTP_CALLBACK_STATUS_REQUEST_ERROR || error.code) {
DN_OS_SemaphoreIncrement(&response->on_complete_semaphore, 1);
@@ -1433,9 +1433,9 @@ DN_API void DN_OS_HttpRequestAsync(DN_OSHttpResponse *response,
WINHTTP_CALLBACK_STATUS_REQUEST_ERROR |
WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE;
if (WinHttpSetStatusCallback(response->w32_request_session,
DN_CAST(WINHTTP_STATUS_CALLBACK) DN_OS_HttpRequestWin32Callback,
DN_Cast(WINHTTP_STATUS_CALLBACK) DN_OS_HttpRequestWin32Callback,
callback_flags,
DN_CAST(DWORD_PTR) nullptr /*dwReserved*/) == WINHTTP_INVALID_STATUS_CALLBACK) {
DN_Cast(DWORD_PTR) nullptr /*dwReserved*/) == WINHTTP_INVALID_STATUS_CALLBACK) {
error = DN_W32_LastError(&response->tmp_arena);
return;
}
@@ -1465,11 +1465,11 @@ DN_API void DN_OS_HttpRequestAsync(DN_OSHttpResponse *response,
response->on_complete_semaphore = DN_OS_SemaphoreInit(0);
if (!WinHttpSendRequest(response->w32_request_handle,
headers16.data,
DN_CAST(DWORD) headers16.size,
DN_Cast(DWORD) headers16.size,
body.data /*optional data*/,
DN_CAST(DWORD) body.size /*optional length*/,
DN_CAST(DWORD) body.size /*total content length*/,
DN_CAST(DWORD_PTR) response)) {
DN_Cast(DWORD) body.size /*optional length*/,
DN_Cast(DWORD) body.size /*total content length*/,
DN_Cast(DWORD_PTR) response)) {
error = DN_W32_LastError(&response->tmp_arena);
return;
}
@@ -1486,7 +1486,7 @@ DN_API void DN_OS_HttpRequestFree(DN_OSHttpResponse *response)
response->w32_request_session = nullptr;
response->w32_request_connection = nullptr;
response->w32_request_handle = nullptr;
DN_Arena_Deinit(&response->tmp_arena);
DN_ArenaDeinit(&response->tmp_arena);
DN_OS_SemaphoreDeinit(&response->on_complete_semaphore);
*response = {};
@@ -1569,11 +1569,11 @@ DN_API void DN_W32_MakeProcessDPIAware()
if (!lib_handle)
return;
if (auto *set_process_dpi_awareness_context = DN_CAST(SetProcessDpiAwarenessContextProc *) GetProcAddress(DN_CAST(HMODULE) lib_handle, "SetProcessDpiAwarenessContext"))
if (auto *set_process_dpi_awareness_context = DN_Cast(SetProcessDpiAwarenessContextProc *) GetProcAddress(DN_Cast(HMODULE) lib_handle, "SetProcessDpiAwarenessContext"))
set_process_dpi_awareness_context(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
else if (auto *set_process_dpi_awareness = DN_CAST(SetProcessDpiAwarenessProc *) GetProcAddress(DN_CAST(HMODULE) lib_handle, "SetProcessDpiAwareness"))
else if (auto *set_process_dpi_awareness = DN_Cast(SetProcessDpiAwarenessProc *) GetProcAddress(DN_Cast(HMODULE) lib_handle, "SetProcessDpiAwareness"))
set_process_dpi_awareness(DPI_AWARENESS_PER_MONITOR_AWARE);
else if (auto *set_process_dpi_aware = DN_CAST(SetProcessDpiAwareProc *) GetProcAddress(DN_CAST(HMODULE) lib_handle, "SetProcessDpiAware"))
else if (auto *set_process_dpi_aware = DN_Cast(SetProcessDpiAwareProc *) GetProcAddress(DN_Cast(HMODULE) lib_handle, "SetProcessDpiAware"))
set_process_dpi_aware();
}
@@ -1581,18 +1581,18 @@ DN_API void DN_W32_MakeProcessDPIAware()
DN_API DN_Str16 DN_W32_Str8ToStr16(DN_Arena *arena, DN_Str8 src)
{
DN_Str16 result = {};
if (!arena || !DN_Str8_HasData(src))
if (!arena || src.size == 0)
return result;
int required_size = MultiByteToWideChar(CP_UTF8, 0 /*dwFlags*/, src.data, DN_CAST(int) src.size, nullptr /*dest*/, 0 /*dest size*/);
int required_size = MultiByteToWideChar(CP_UTF8, 0 /*dwFlags*/, src.data, DN_Cast(int) src.size, nullptr /*dest*/, 0 /*dest size*/);
if (required_size <= 0)
return result;
wchar_t *buffer = DN_Arena_NewArray(arena, wchar_t, required_size + 1, DN_ZeroMem_No);
wchar_t *buffer = DN_ArenaNewArray(arena, wchar_t, required_size + 1, DN_ZMem_No);
if (!buffer)
return result;
int chars_written = MultiByteToWideChar(CP_UTF8, 0 /*dwFlags*/, src.data, DN_CAST(int) src.size, buffer, required_size);
int chars_written = MultiByteToWideChar(CP_UTF8, 0 /*dwFlags*/, src.data, DN_Cast(int) src.size, buffer, required_size);
if (DN_Check(chars_written == required_size)) {
result.data = buffer;
result.size = chars_written;
@@ -1604,14 +1604,14 @@ DN_API DN_Str16 DN_W32_Str8ToStr16(DN_Arena *arena, DN_Str8 src)
DN_API int DN_W32_Str8ToStr16Buffer(DN_Str8 src, wchar_t *dest, int dest_size)
{
int result = 0;
if (!DN_Str8_HasData(src))
if (src.size == 0)
return result;
result = MultiByteToWideChar(CP_UTF8, 0 /*dwFlags*/, src.data, DN_CAST(int) src.size, nullptr /*dest*/, 0 /*dest size*/);
result = MultiByteToWideChar(CP_UTF8, 0 /*dwFlags*/, src.data, DN_Cast(int) src.size, nullptr /*dest*/, 0 /*dest size*/);
if (result <= 0 || result > dest_size || !dest)
return result;
result = MultiByteToWideChar(CP_UTF8, 0 /*dwFlags*/, src.data, DN_CAST(int) src.size, dest, DN_CAST(int) dest_size);
result = MultiByteToWideChar(CP_UTF8, 0 /*dwFlags*/, src.data, DN_Cast(int) src.size, dest, DN_Cast(int) dest_size);
dest[DN_Min(result, dest_size - 1)] = 0;
return result;
}
@@ -1620,7 +1620,7 @@ DN_API int DN_W32_Str8ToStr16Buffer(DN_Str8 src, wchar_t *dest, int dest_size)
DN_API int DN_W32_Str16ToStr8Buffer(DN_Str16 src, char *dest, int dest_size)
{
int result = 0;
if (!DN_Str16_HasData(src))
if (src.size == 0)
return result;
int src_size = DN_SaturateCastISizeToInt(src.size);
@@ -1631,7 +1631,7 @@ DN_API int DN_W32_Str16ToStr8Buffer(DN_Str16 src, char *dest, int dest_size)
if (result <= 0 || result > dest_size || !dest)
return result;
result = WideCharToMultiByte(CP_UTF8, 0 /*dwFlags*/, src.data, src_size, dest, DN_CAST(int) dest_size, nullptr, nullptr);
result = WideCharToMultiByte(CP_UTF8, 0 /*dwFlags*/, src.data, src_size, dest, DN_Cast(int) dest_size, nullptr, nullptr);
dest[DN_Min(result, dest_size - 1)] = 0;
return result;
}
@@ -1639,7 +1639,7 @@ DN_API int DN_W32_Str16ToStr8Buffer(DN_Str16 src, char *dest, int dest_size)
DN_API DN_Str8 DN_W32_Str16ToStr8(DN_Arena *arena, DN_Str16 src)
{
DN_Str8 result = {};
if (!arena || !DN_Str16_HasData(src))
if (!arena || src.size == 0)
return result;
int src_size = DN_SaturateCastISizeToInt(src.size);
@@ -1653,11 +1653,11 @@ DN_API DN_Str8 DN_W32_Str16ToStr8(DN_Arena *arena, DN_Str16 src)
// NOTE: Str8 allocate ensures there's one extra byte for
// null-termination already so no-need to +1 the required size
DN_ArenaTempMemScope temp_mem = DN_ArenaTempMemScope(arena);
DN_Str8 buffer = DN_Str8_Alloc(arena, required_size, DN_ZeroMem_No);
if (!DN_Str8_HasData(buffer))
DN_Str8 buffer = DN_Str8FromArena(arena, required_size, DN_ZMem_No);
if (buffer.size == 0)
return result;
int chars_written = WideCharToMultiByte(CP_UTF8, 0 /*dwFlags*/, src.data, src_size, buffer.data, DN_CAST(int) buffer.size, nullptr, nullptr);
int chars_written = WideCharToMultiByte(CP_UTF8, 0 /*dwFlags*/, src.data, src_size, buffer.data, DN_Cast(int) buffer.size, nullptr, nullptr);
if (DN_Check(chars_written == required_size)) {
result = buffer;
result.data[result.size] = 0;
@@ -1670,7 +1670,7 @@ DN_API DN_Str8 DN_W32_Str16ToStr8(DN_Arena *arena, DN_Str16 src)
DN_API DN_Str8 DN_W32_Str16ToStr8FromHeap(DN_Str16 src)
{
DN_Str8 result = {};
if (!DN_Str16_HasData(src))
if (src.size == 0)
return result;
int src_size = DN_SaturateCastISizeToInt(src.size);
@@ -1683,11 +1683,11 @@ DN_API DN_Str8 DN_W32_Str16ToStr8FromHeap(DN_Str16 src)
// NOTE: Str8 allocate ensures there's one extra byte for
// null-termination already so no-need to +1 the required size
DN_Str8 buffer = DN_Str8_FromHeap(required_size, DN_ZeroMem_No);
if (!DN_Str8_HasData(buffer))
DN_Str8 buffer = DN_Str8FromHeap(required_size, DN_ZMem_No);
if (buffer.size == 0)
return result;
int chars_written = WideCharToMultiByte(CP_UTF8, 0 /*dwFlags*/, src.data, src_size, buffer.data, DN_CAST(int) buffer.size, nullptr, nullptr);
int chars_written = WideCharToMultiByte(CP_UTF8, 0 /*dwFlags*/, src.data, src_size, buffer.data, DN_Cast(int) buffer.size, nullptr, nullptr);
if (DN_Check(chars_written == required_size)) {
result = buffer;
result.data[result.size] = 0;
@@ -1708,17 +1708,17 @@ DN_API DN_Str16 DN_W32_EXEPathW(DN_Arena *arena)
wchar_t *module_path = nullptr;
do {
module_size += 256;
module_path = DN_Arena_NewArray(tmem.arena, wchar_t, module_size, DN_ZeroMem_No);
module_path = DN_ArenaNewArray(tmem.arena, wchar_t, module_size, DN_ZMem_No);
if (!module_path)
return result;
module_size = DN_CAST(DN_USize) GetModuleFileNameW(nullptr /*module*/, module_path, DN_CAST(int) module_size);
module_size = DN_Cast(DN_USize) GetModuleFileNameW(nullptr /*module*/, module_path, DN_Cast(int) module_size);
} while (GetLastError() == ERROR_INSUFFICIENT_BUFFER);
DN_USize index_of_last_slash = 0;
for (DN_USize index = module_size - 1; !index_of_last_slash && index < module_size; index--)
index_of_last_slash = module_path[index] == '\\' ? index : 0;
result.data = DN_Arena_NewArray(arena, wchar_t, module_size + 1, DN_ZeroMem_No);
result.data = DN_ArenaNewArray(arena, wchar_t, module_size + 1, DN_ZMem_No);
result.size = module_size;
DN_Memcpy(result.data, module_path, sizeof(wchar_t) * result.size);
result.data[result.size] = 0;
@@ -1734,17 +1734,17 @@ DN_API DN_Str16 DN_W32_EXEDirW(DN_Arena *arena)
wchar_t *module_path = nullptr;
do {
module_size += 256;
module_path = DN_Arena_NewArray(tmem.arena, wchar_t, module_size, DN_ZeroMem_No);
module_path = DN_ArenaNewArray(tmem.arena, wchar_t, module_size, DN_ZMem_No);
if (!module_path)
return result;
module_size = DN_CAST(DN_USize) GetModuleFileNameW(nullptr /*module*/, module_path, DN_CAST(int) module_size);
module_size = DN_Cast(DN_USize) GetModuleFileNameW(nullptr /*module*/, module_path, DN_Cast(int) module_size);
} while (GetLastError() == ERROR_INSUFFICIENT_BUFFER);
DN_USize index_of_last_slash = 0;
for (DN_USize index = module_size - 1; !index_of_last_slash && index < module_size; index--)
index_of_last_slash = module_path[index] == '\\' ? index : 0;
result.data = DN_Arena_NewArray(arena, wchar_t, index_of_last_slash + 1, DN_ZeroMem_No);
result.data = DN_ArenaNewArray(arena, wchar_t, index_of_last_slash + 1, DN_ZMem_No);
result.size = index_of_last_slash;
DN_Memcpy(result.data, module_path, sizeof(wchar_t) * result.size);
result.data[result.size] = 0;
@@ -1768,9 +1768,9 @@ DN_API DN_Str16 DN_W32_WorkingDirW(DN_Arena *arena, DN_Str16 suffix)
// NOTE: required_size is the size required *including* the null-terminator
DN_OSTLSTMem tmem = DN_OS_TLSTMem(arena);
unsigned long required_size = GetCurrentDirectoryW(0, nullptr);
unsigned long desired_size = required_size + DN_CAST(unsigned long) suffix.size;
unsigned long desired_size = required_size + DN_Cast(unsigned long) suffix.size;
wchar_t *tmem_w_path = DN_Arena_NewArray(tmem.arena, wchar_t, desired_size, DN_ZeroMem_No);
wchar_t *tmem_w_path = DN_ArenaNewArray(tmem.arena, wchar_t, desired_size, DN_ZMem_No);
if (!tmem_w_path)
return result;
@@ -1780,7 +1780,7 @@ DN_API DN_Str16 DN_W32_WorkingDirW(DN_Arena *arena, DN_Str16 suffix)
return result;
}
wchar_t *w_path = DN_Arena_NewArray(arena, wchar_t, desired_size, DN_ZeroMem_No);
wchar_t *w_path = DN_ArenaNewArray(arena, wchar_t, desired_size, DN_ZMem_No);
if (!w_path)
return result;
@@ -1790,7 +1790,7 @@ DN_API DN_Str16 DN_W32_WorkingDirW(DN_Arena *arena, DN_Str16 suffix)
w_path[desired_size] = 0;
}
result = DN_Str16{w_path, DN_CAST(DN_USize)(desired_size - 1)};
result = DN_Str16{w_path, DN_Cast(DN_USize)(desired_size - 1)};
return result;
}
@@ -1821,7 +1821,7 @@ DN_API bool DN_W32_DirWIterate(DN_Str16 path, DN_W32FolderIteratorW *it)
if (find_data.cFileName[0] == '.' || (find_data.cFileName[0] == '.' && find_data.cFileName[1] == '.'))
continue;
it->file_name.size = DN_CStr16_Size(find_data.cFileName);
it->file_name.size = DN_CStr16Size(find_data.cFileName);
DN_Assert(it->file_name.size < (DN_ArrayCountU(it->file_name_buf) - 1));
DN_Memcpy(it->file_name.data, find_data.cFileName, it->file_name.size * sizeof(wchar_t));
it->file_name_buf[it->file_name.size] = 0;