Cleanup CVT functions

This commit is contained in:
doylet 2025-07-20 15:13:13 +10:00
parent 804e5ed0a8
commit dbc3fe63f8
9 changed files with 202 additions and 190 deletions

View File

@ -1,4 +1,4 @@
// Generated by the DN single header generator 2025-07-16 19:35:09 // Generated by the DN single header generator 2025-07-20 15:12:51
#define DN_BASE_INC_CPP #define DN_BASE_INC_CPP
@ -1478,9 +1478,9 @@ DN_U32 DN_DSMap_Hash(DN_DSMap<T> const *map, DN_DSMapKey key)
DN_U32 len = 0; DN_U32 len = 0;
DN_U32 h = seed; DN_U32 h = seed;
switch (key.type) { switch (key.type) {
case DN_DSMapKeyType_BufferAsU64NoHash: /*FALLTHRU*/ case DN_DSMapKeyType_BufferAsU64NoHash: /*FALLTHRU*/
case DN_DSMapKeyType_U64NoHash: DN_InvalidCodePath; /*FALLTHRU*/ case DN_DSMapKeyType_U64NoHash: DN_InvalidCodePath; /*FALLTHRU*/
case DN_DSMapKeyType_Invalid: break; case DN_DSMapKeyType_Invalid: break;
case DN_DSMapKeyType_Buffer: case DN_DSMapKeyType_Buffer:
key_ptr = DN_CAST(char const *) key.buffer_data; key_ptr = DN_CAST(char const *) key.buffer_data;
@ -2214,7 +2214,7 @@ DN_API int DN_CVT_FmtBuffer3DotTruncate(char *buffer, int size, DN_FMT_ATTRIB ch
return result; return result;
} }
DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8(uint64_t val, char separator) DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8(DN_U64 val, char separator)
{ {
DN_CVTU64Str8 result = {}; DN_CVTU64Str8 result = {};
if (val == 0) { if (val == 0) {
@ -2247,44 +2247,41 @@ DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8(uint64_t val, char separator)
return result; return result;
} }
DN_API DN_CVTU64ByteSize DN_CVT_U64ToByteSize(uint64_t bytes, DN_CVTU64ByteSizeType desired_type) DN_API DN_CVTU64Bytes DN_CVT_U64ToBytes(DN_U64 bytes, DN_CVTBytesType type)
{ {
DN_CVTU64ByteSize result = {}; DN_Assert(type != DN_CVTBytesType_Count);
DN_CVTU64Bytes result = {};
result.bytes = DN_CAST(DN_F64) bytes; result.bytes = DN_CAST(DN_F64) bytes;
if (!DN_Check(desired_type != DN_CVTU64ByteSizeType_Count)) {
result.suffix = DN_CVT_U64ByteSizeTypeString(result.type);
return result;
}
if (desired_type == DN_CVTU64ByteSizeType_Auto) if (type == DN_CVTBytesType_Auto)
for (; result.type < DN_CVTU64ByteSizeType_Count && result.bytes >= 1024.0; result.type = DN_CAST(DN_CVTU64ByteSizeType)(DN_CAST(DN_USize) result.type + 1)) for (; result.type < DN_CVTBytesType_Count && result.bytes >= 1024.0; result.type = DN_CAST(DN_CVTBytesType)(DN_CAST(DN_USize) result.type + 1))
result.bytes /= 1024.0; result.bytes /= 1024.0;
else else
for (; result.type < desired_type; result.type = DN_CAST(DN_CVTU64ByteSizeType)(DN_CAST(DN_USize) result.type + 1)) for (; result.type < type; result.type = DN_CAST(DN_CVTBytesType)(DN_CAST(DN_USize) result.type + 1))
result.bytes /= 1024.0; result.bytes /= 1024.0;
result.suffix = DN_CVT_U64ByteSizeTypeString(result.type); result.suffix = DN_CVT_BytesTypeToStr8(result.type);
return result; return result;
} }
DN_API DN_Str8 DN_CVT_U64ToByteSizeStr8(DN_Arena *arena, uint64_t bytes, DN_CVTU64ByteSizeType desired_type) DN_API DN_Str8 DN_CVT_U64ToBytesStr8(DN_Arena *arena, DN_U64 bytes, DN_CVTBytesType desired_type)
{ {
DN_CVTU64ByteSize byte_size = DN_CVT_U64ToByteSize(bytes, desired_type); DN_CVTU64Bytes byte_size = DN_CVT_U64ToBytes(bytes, desired_type);
DN_Str8 result = DN_Str8_InitF(arena, "%.2f%.*s", byte_size.bytes, DN_STR_FMT(byte_size.suffix)); DN_Str8 result = DN_Str8_InitF(arena, "%.2f%.*s", byte_size.bytes, DN_STR_FMT(byte_size.suffix));
return result; return result;
} }
DN_API DN_Str8 DN_CVT_U64ByteSizeTypeString(DN_CVTU64ByteSizeType type) DN_API DN_Str8 DN_CVT_BytesTypeToStr8(DN_CVTBytesType type)
{ {
DN_Str8 result = DN_STR8(""); DN_Str8 result = DN_STR8("");
switch (type) { switch (type) {
case DN_CVTU64ByteSizeType_B: result = DN_STR8("B"); break; case DN_CVTBytesType_B: result = DN_STR8("B"); break;
case DN_CVTU64ByteSizeType_KiB: result = DN_STR8("KiB"); break; case DN_CVTBytesType_KiB: result = DN_STR8("KiB"); break;
case DN_CVTU64ByteSizeType_MiB: result = DN_STR8("MiB"); break; case DN_CVTBytesType_MiB: result = DN_STR8("MiB"); break;
case DN_CVTU64ByteSizeType_GiB: result = DN_STR8("GiB"); break; case DN_CVTBytesType_GiB: result = DN_STR8("GiB"); break;
case DN_CVTU64ByteSizeType_TiB: result = DN_STR8("TiB"); break; case DN_CVTBytesType_TiB: result = DN_STR8("TiB"); break;
case DN_CVTU64ByteSizeType_Count: result = DN_STR8(""); break; case DN_CVTBytesType_Count: result = DN_STR8(""); break;
case DN_CVTU64ByteSizeType_Auto: result = DN_STR8(""); break; case DN_CVTBytesType_Auto: result = DN_STR8(""); break;
} }
return result; return result;
} }
@ -2405,14 +2402,14 @@ DN_API DN_Str8 DN_CVT_F64ToAge(DN_Arena *arena, DN_F64 age_s, DN_CVTU64AgeUnit u
return result; return result;
} }
DN_API uint64_t DN_CVT_HexToU64(DN_Str8 hex) DN_API DN_U64 DN_CVT_HexToU64(DN_Str8 hex)
{ {
DN_Str8 real_hex = DN_Str8_TrimPrefix(DN_Str8_TrimPrefix(hex, DN_STR8("0x")), DN_STR8("0X")); DN_Str8 real_hex = DN_Str8_TrimPrefix(DN_Str8_TrimPrefix(hex, DN_STR8("0x")), DN_STR8("0X"));
DN_USize max_hex_size = sizeof(uint64_t) * 2 /*hex chars per byte*/; DN_USize max_hex_size = sizeof(DN_U64) * 2 /*hex chars per byte*/;
DN_Assert(real_hex.size <= max_hex_size); DN_Assert(real_hex.size <= max_hex_size);
DN_USize size = DN_Min(max_hex_size, real_hex.size); DN_USize size = DN_Min(max_hex_size, real_hex.size);
uint64_t result = 0; DN_U64 result = 0;
for (DN_USize index = 0; index < size; index++) { for (DN_USize index = 0; index < size; index++) {
char ch = real_hex.data[index]; char ch = real_hex.data[index];
DN_CharHexToU8 val = DN_Char_HexToU8(ch); DN_CharHexToU8 val = DN_Char_HexToU8(ch);
@ -2423,13 +2420,13 @@ DN_API uint64_t DN_CVT_HexToU64(DN_Str8 hex)
return result; return result;
} }
DN_API DN_Str8 DN_CVT_U64ToHex(DN_Arena *arena, uint64_t number, uint32_t flags) DN_API DN_Str8 DN_CVT_U64ToHex(DN_Arena *arena, DN_U64 number, uint32_t flags)
{ {
DN_Str8 prefix = {}; DN_Str8 prefix = {};
if ((flags & DN_CVTHexU64Str8Flags_0xPrefix)) if ((flags & DN_CVTU64HexStrFlags_0xPrefix))
prefix = DN_STR8("0x"); prefix = DN_STR8("0x");
char const *fmt = (flags & DN_CVTHexU64Str8Flags_UppercaseHex) ? "%I64X" : "%I64x"; char const *fmt = (flags & DN_CVTU64HexStrFlags_UppercaseHex) ? "%I64X" : "%I64x";
DN_USize required_size = DN_CStr8_FSize(fmt, number) + prefix.size; DN_USize required_size = DN_CStr8_FSize(fmt, number) + prefix.size;
DN_Str8 result = DN_Str8_Alloc(arena, required_size, DN_ZeroMem_No); DN_Str8 result = DN_Str8_Alloc(arena, required_size, DN_ZeroMem_No);
@ -2441,17 +2438,17 @@ DN_API DN_Str8 DN_CVT_U64ToHex(DN_Arena *arena, uint64_t number, uint32_t flags)
return result; return result;
} }
DN_API DN_CVTU64HexStr8 DN_CVT_U64ToHexStr8(uint64_t number, DN_CVTHexU64Str8Flags flags) DN_API DN_CVTU64HexStr DN_CVT_U64ToHexStr8(DN_U64 number, DN_CVTU64HexStrFlags flags)
{ {
DN_Str8 prefix = {}; DN_Str8 prefix = {};
if (flags & DN_CVTHexU64Str8Flags_0xPrefix) if (flags & DN_CVTU64HexStrFlags_0xPrefix)
prefix = DN_STR8("0x"); prefix = DN_STR8("0x");
DN_CVTU64HexStr8 result = {}; DN_CVTU64HexStr result = {};
DN_Memcpy(result.data, prefix.data, prefix.size); DN_Memcpy(result.data, prefix.data, prefix.size);
result.size += DN_CAST(int8_t) prefix.size; result.size += DN_CAST(int8_t) prefix.size;
char const *fmt = (flags & DN_CVTHexU64Str8Flags_UppercaseHex) ? "%I64X" : "%I64x"; char const *fmt = (flags & DN_CVTU64HexStrFlags_UppercaseHex) ? "%I64X" : "%I64x";
int size = DN_SNPrintF(result.data + result.size, DN_ArrayCountU(result.data) - result.size, fmt, number); int size = DN_SNPrintF(result.data + result.size, DN_ArrayCountU(result.data) - result.size, fmt, number);
result.size += DN_CAST(uint8_t) size; result.size += DN_CAST(uint8_t) size;
DN_Assert(result.size < DN_ArrayCountU(result.data)); DN_Assert(result.size < DN_ArrayCountU(result.data));
@ -5301,7 +5298,7 @@ DN_API DN_Str8 DN_OS_ReadAll(DN_Arena *arena, DN_Str8 path, DN_OSErrSink *error)
result = DN_Str8_Alloc(arena, path_info.size, DN_ZeroMem_No); result = DN_Str8_Alloc(arena, path_info.size, DN_ZeroMem_No);
if (!DN_Str8_HasData(result)) { if (!DN_Str8_HasData(result)) {
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr); DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_Str8 buffer_size_str8 = DN_CVT_U64ToByteSizeStr8(tmem.arena, path_info.size, DN_CVTU64ByteSizeType_Auto); DN_Str8 buffer_size_str8 = DN_CVT_U64ToBytesStr8AutoFromTLS(path_info.size);
DN_OS_ErrSinkAppendF(error, 1 /*error_code*/, "Failed to allocate %.*s for reading file '%.*s'", DN_STR_FMT(buffer_size_str8), DN_STR_FMT(path)); DN_OS_ErrSinkAppendF(error, 1 /*error_code*/, "Failed to allocate %.*s for reading file '%.*s'", DN_STR_FMT(buffer_size_str8), DN_STR_FMT(path));
DN_Arena_TempMemEnd(temp_mem); DN_Arena_TempMemEnd(temp_mem);
result = {}; result = {};
@ -8445,7 +8442,7 @@ DN_API DN_OSFileRead DN_OS_FileRead(DN_OSFile *file, void *buffer, DN_USize size
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr); DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
if (!DN_Check(size <= (unsigned long)-1)) { if (!DN_Check(size <= (unsigned long)-1)) {
DN_Str8 buffer_size_str8 = DN_CVT_U64ToByteSizeStr8(tmem.arena, size, DN_CVTU64ByteSizeType_Auto); DN_Str8 buffer_size_str8 = DN_CVT_U64ToBytesStr8AutoFromTLS(size);
DN_OS_ErrSinkAppendF( DN_OS_ErrSinkAppendF(
err, err,
1 /*error_code*/, 1 /*error_code*/,
@ -8499,9 +8496,9 @@ DN_API bool DN_OS_FileWritePtr(DN_OSFile *file, void const *buffer, DN_USize siz
} }
if (!result) { if (!result) {
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr); DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_W32Error win_error = DN_W32_LastError(tmem.arena); DN_W32Error win_error = DN_W32_LastError(tmem.arena);
DN_Str8 buffer_size_str8 = DN_CVT_U64ToByteSizeStr8(tmem.arena, size, DN_CVTU64ByteSizeType_Auto); DN_Str8 buffer_size_str8 = DN_CVT_U64ToBytesStr8AutoFromTLS(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_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));
} }
return result; return result;
@ -10020,8 +10017,8 @@ DN_API void DN_Debug_TrackAlloc(void *ptr, DN_USize size, bool leak_permitted)
DN_DebugAlloc *alloc = alloc_entry.value; DN_DebugAlloc *alloc = alloc_entry.value;
if (alloc_entry.found) { if (alloc_entry.found) {
if ((alloc->flags & DN_DebugAllocFlag_Freed) == 0) { if ((alloc->flags & DN_DebugAllocFlag_Freed) == 0) {
DN_Str8 alloc_size = DN_CVT_U64ToByteSizeStr8(alloc_table->arena, alloc->size, DN_CVTU64ByteSizeType_Auto); DN_Str8 alloc_size = DN_CVT_U64ToBytesStr8Auto(alloc_table->arena, alloc->size);
DN_Str8 new_alloc_size = DN_CVT_U64ToByteSizeStr8(alloc_table->arena, size, DN_CVTU64ByteSizeType_Auto); DN_Str8 new_alloc_size = DN_CVT_U64ToBytesStr8Auto(alloc_table->arena, size);
DN_HardAssertF( DN_HardAssertF(
alloc->flags & DN_DebugAllocFlag_Freed, alloc->flags & DN_DebugAllocFlag_Freed,
"This pointer is already in the leak tracker, however it has not been freed yet. This " "This pointer is already in the leak tracker, however it has not been freed yet. This "
@ -10078,7 +10075,7 @@ DN_API void DN_Debug_TrackDealloc(void *ptr)
DN_DebugAlloc *alloc = alloc_entry.value; DN_DebugAlloc *alloc = alloc_entry.value;
if (alloc->flags & DN_DebugAllocFlag_Freed) { if (alloc->flags & DN_DebugAllocFlag_Freed) {
DN_Str8 freed_size = DN_CVT_U64ToByteSizeStr8(alloc_table->arena, alloc->freed_size, DN_CVTU64ByteSizeType_Auto); DN_Str8 freed_size = DN_CVT_U64ToBytesStr8Auto(alloc_table->arena, alloc->freed_size);
DN_HardAssertF((alloc->flags & DN_DebugAllocFlag_Freed) == 0, DN_HardAssertF((alloc->flags & DN_DebugAllocFlag_Freed) == 0,
"Double free detected, pointer to free was already marked " "Double free detected, pointer to free was already marked "
"as freed. Either the pointer was reallocated but not " "as freed. Either the pointer was reallocated but not "
@ -10120,7 +10117,7 @@ DN_API void DN_Debug_DumpLeaks()
if (alloc_leaked && !leak_permitted) { if (alloc_leaked && !leak_permitted) {
leaked_bytes += alloc->size; leaked_bytes += alloc->size;
leak_count++; leak_count++;
DN_Str8 alloc_size = DN_CVT_U64ToByteSizeStr8(g_dn_core->alloc_table.arena, alloc->size, DN_CVTU64ByteSizeType_Auto); DN_Str8 alloc_size = DN_CVT_U64ToBytesStr8Auto(g_dn_core->alloc_table.arena, alloc->size);
DN_LOG_WarningF("Pointer (0x%p) leaked %.*s at:\n" DN_LOG_WarningF("Pointer (0x%p) leaked %.*s at:\n"
"%.*s", "%.*s",
alloc->ptr, DN_STR_FMT(alloc_size), alloc->ptr, DN_STR_FMT(alloc_size),
@ -10131,7 +10128,7 @@ DN_API void DN_Debug_DumpLeaks()
if (leak_count) { if (leak_count) {
char buffer[512]; char buffer[512];
DN_Arena arena = DN_Arena_InitFromBuffer(buffer, sizeof(buffer), DN_ArenaFlags_Nil); DN_Arena arena = DN_Arena_InitFromBuffer(buffer, sizeof(buffer), DN_ArenaFlags_Nil);
DN_Str8 leak_size = DN_CVT_U64ToByteSizeStr8(&arena, leaked_bytes, DN_CVTU64ByteSizeType_Auto); DN_Str8 leak_size = DN_CVT_U64ToBytesStr8Auto(&arena, leaked_bytes);
DN_LOG_WarningF("There were %I64u leaked allocations totalling %.*s", leak_count, DN_STR_FMT(leak_size)); DN_LOG_WarningF("There were %I64u leaked allocations totalling %.*s", leak_count, DN_STR_FMT(leak_size));
} }
} }

View File

@ -1,4 +1,4 @@
// Generated by the DN single header generator 2025-07-16 19:35:09 // Generated by the DN single header generator 2025-07-20 15:12:51
#if !defined(DN_BASE_INC_H) #if !defined(DN_BASE_INC_H)
#define DN_BASE_INC_H #define DN_BASE_INC_H
@ -1195,11 +1195,11 @@ struct DN_Pool
{ {
DN_Arena *arena; DN_Arena *arena;
DN_PoolSlot *slots[DN_PoolSlotSize_Count]; DN_PoolSlot *slots[DN_PoolSlotSize_Count];
uint8_t align; DN_U8 align;
}; };
// NOTE: DN_Pool /////////////////////////////////////////////////////////////////////////////////// // NOTE: DN_Pool ///////////////////////////////////////////////////////////////////////////////////
DN_API DN_Pool DN_Pool_Init (DN_Arena *arena, uint8_t align); DN_API DN_Pool DN_Pool_Init (DN_Arena *arena, DN_U8 align);
DN_API bool DN_Pool_IsValid (DN_Pool const *pool); DN_API bool DN_Pool_IsValid (DN_Pool const *pool);
DN_API void * DN_Pool_Alloc (DN_Pool *pool, DN_USize size); DN_API void * DN_Pool_Alloc (DN_Pool *pool, DN_USize size);
DN_API DN_Str8 DN_Pool_AllocStr8FV (DN_Pool *pool, DN_FMT_ATTRIB char const *fmt, va_list args); DN_API DN_Str8 DN_Pool_AllocStr8FV (DN_Pool *pool, DN_FMT_ATTRIB char const *fmt, va_list args);
@ -4133,81 +4133,90 @@ template <typename T> DN_Slice<T> DN_List_To
#if !defined(DN_BASE_CONVERT_H) #if !defined(DN_BASE_CONVERT_H)
#define DN_BASE_CONVERT_H #define DN_BASE_CONVERT_H
// DN: Single header generator commented out this header => #include "../dn_base_inc.h"
struct DN_CVTU64Str8 struct DN_CVTU64Str8
{ {
char data[27 + 1]; // NOTE(dn): 27 is the maximum size of DN_U64 including a separator char data[27 + 1]; // NOTE(dn): 27 is the maximum size of DN_U64 including a separator
DN_U8 size; DN_U8 size;
}; };
enum DN_CVTU64ByteSizeType enum DN_CVTBytesType
{ {
DN_CVTU64ByteSizeType_B, DN_CVTBytesType_B,
DN_CVTU64ByteSizeType_KiB, DN_CVTBytesType_KiB,
DN_CVTU64ByteSizeType_MiB, DN_CVTBytesType_MiB,
DN_CVTU64ByteSizeType_GiB, DN_CVTBytesType_GiB,
DN_CVTU64ByteSizeType_TiB, DN_CVTBytesType_TiB,
DN_CVTU64ByteSizeType_Count, DN_CVTBytesType_Count,
DN_CVTU64ByteSizeType_Auto, DN_CVTBytesType_Auto,
}; };
struct DN_CVTU64ByteSize struct DN_CVTU64Bytes
{ {
DN_CVTU64ByteSizeType type; DN_CVTBytesType type;
DN_Str8 suffix; // "KiB", "MiB", "GiB" .. e.t.c DN_Str8 suffix; // "KiB", "MiB", "GiB" .. e.t.c
DN_F64 bytes; DN_F64 bytes;
}; };
struct DN_CVTU64HexStr8 struct DN_CVTU64HexStr
{ {
char data[2 /*0x*/ + 16 /*hex*/ + 1 /*null-terminator*/]; char data[2 /*0x*/ + 16 /*hex*/ + 1 /*null-terminator*/];
DN_U8 size; DN_U8 size;
}; };
typedef DN_U32 DN_CVTHexU64Str8Flags; typedef DN_U32 DN_CVTU64HexStrFlags;
enum DN_CVTHexU64Str8Flags_ enum DN_CVTU64HexStrFlags_
{ {
DN_CVTHexU64Str8Flags_Nil = 0, DN_CVTU64HexStrFlags_Nil = 0,
DN_CVTHexU64Str8Flags_0xPrefix = 1 << 0, /// Add the '0x' prefix from the string DN_CVTU64HexStrFlags_0xPrefix = 1 << 0, /// Add the '0x' prefix from the string
DN_CVTHexU64Str8Flags_UppercaseHex = 1 << 1, /// Use uppercase ascii characters for hex DN_CVTU64HexStrFlags_UppercaseHex = 1 << 1, /// Use uppercase ascii characters for hex
}; };
typedef DN_U32 DN_CVTU64AgeUnit; typedef DN_U32 DN_CVTU64AgeUnit;
enum DN_CVTU64AgeUnit_ enum DN_CVTU64AgeUnit_
{ {
DN_CVTU64AgeUnit_Sec = 1 << 0, DN_CVTU64AgeUnit_Sec = 1 << 0,
DN_CVTU64AgeUnit_Min = 1 << 1, DN_CVTU64AgeUnit_Min = 1 << 1,
DN_CVTU64AgeUnit_Hr = 1 << 2, DN_CVTU64AgeUnit_Hr = 1 << 2,
DN_CVTU64AgeUnit_Day = 1 << 3, DN_CVTU64AgeUnit_Day = 1 << 3,
DN_CVTU64AgeUnit_Week = 1 << 4, DN_CVTU64AgeUnit_Week = 1 << 4,
DN_CVTU64AgeUnit_Year = 1 << 5, DN_CVTU64AgeUnit_Year = 1 << 5,
DN_CVTU64AgeUnit_HMS = DN_CVTU64AgeUnit_Sec | DN_CVTU64AgeUnit_Min | DN_CVTU64AgeUnit_Hr, DN_CVTU64AgeUnit_HMS = DN_CVTU64AgeUnit_Sec | DN_CVTU64AgeUnit_Min | DN_CVTU64AgeUnit_Hr,
DN_CVTU64AgeUnit_All = DN_CVTU64AgeUnit_HMS | DN_CVTU64AgeUnit_Day | DN_CVTU64AgeUnit_Week | DN_CVTU64AgeUnit_Year, DN_CVTU64AgeUnit_All = DN_CVTU64AgeUnit_HMS | DN_CVTU64AgeUnit_Day | DN_CVTU64AgeUnit_Week | DN_CVTU64AgeUnit_Year,
}; };
DN_API int DN_CVT_FmtBuffer3DotTruncate (char *buffer, int size, DN_FMT_ATTRIB char const *fmt, ...); DN_API int DN_CVT_FmtBuffer3DotTruncate (char *buffer, int size, DN_FMT_ATTRIB char const *fmt, ...);
DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8 (DN_U64 val, char separator); DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8 (DN_U64 val, char separator);
DN_API DN_CVTU64ByteSize DN_CVT_U64ToByteSize (DN_U64 bytes, DN_CVTU64ByteSizeType type); DN_API DN_CVTU64Bytes DN_CVT_U64ToBytes (DN_U64 bytes, DN_CVTBytesType type);
DN_API DN_Str8 DN_CVT_U64ToByteSizeStr8 (DN_Arena *arena, DN_U64 bytes, DN_CVTU64ByteSizeType desired_type); #define DN_CVT_U64ToBytesAuto(bytes) DN_CVT_U64ToBytes(bytes, DN_CVTBytesType_Auto)
DN_API DN_Str8 DN_CVT_U64ByteSizeTypeString (DN_CVTU64ByteSizeType type); DN_API DN_Str8 DN_CVT_U64ToBytesStr8 (DN_Arena *arena, DN_U64 bytes, DN_CVTBytesType type);
DN_API DN_Str8 DN_CVT_U64ToAge (DN_Arena *arena, DN_U64 age_s, DN_CVTU64AgeUnit unit); #define DN_CVT_U64ToBytesStr8Auto(arena, bytes) DN_CVT_U64ToBytesStr8(arena, bytes, DN_CVTBytesType_Auto)
DN_API DN_Str8 DN_CVT_F64ToAge (DN_Arena *arena, DN_F64 age_s, DN_CVTU64AgeUnit unit); #define DN_CVT_U64ToBytesStr8FromTLS(bytes, type) DN_CVT_U64ToBytesStr8(DN_OS_TLSTopArena(), bytes, type)
#define DN_CVT_U64ToBytesStr8AutoFromTLS(bytes) DN_CVT_U64ToBytesStr8(DN_OS_TLSTopArena(), bytes, DN_CVTBytesType_Auto)
#define DN_CVT_U64ToBytesStr8FromFrame(bytes, type) DN_CVT_U64ToBytesStr8(DN_OS_TLSFrameArena(), bytes, type)
#define DN_CVT_U64ToBytesStr8AutoFromFrame(bytes) DN_CVT_U64ToBytesStr8(DN_OS_TLSFrameArena(), bytes, DN_CVTBytesType_Auto)
DN_API DN_Str8 DN_CVT_BytesTypeToStr8 (DN_CVTBytesType type);
DN_API DN_Str8 DN_CVT_U64ToAge (DN_Arena *arena, DN_U64 age_s, DN_CVTU64AgeUnit unit);
DN_API DN_Str8 DN_CVT_F64ToAge (DN_Arena *arena, DN_F64 age_s, DN_CVTU64AgeUnit unit);
DN_API DN_U64 DN_CVT_HexToU64 (DN_Str8 hex); DN_API DN_U64 DN_CVT_HexToU64 (DN_Str8 hex);
DN_API DN_Str8 DN_CVT_U64ToHex (DN_Arena *arena, DN_U64 number, DN_CVTHexU64Str8Flags flags); DN_API DN_Str8 DN_CVT_U64ToHex (DN_Arena *arena, DN_U64 number, DN_CVTU64HexStrFlags flags);
DN_API DN_CVTU64HexStr8 DN_CVT_U64ToHexStr8 (DN_U64 number, DN_U32 flags); #define DN_CVT_U64ToHexFromFrame(number, flags) DN_CVT_U64ToHex(DN_OS_TLSFrameArena(), number, flags)
DN_API DN_CVTU64HexStr DN_CVT_U64ToHexStr (DN_U64 number, DN_CVTU64HexStrFlags flags);
DN_API bool DN_CVT_BytesToHexPtr (void const *src, DN_USize src_size, char *dest, DN_USize dest_size); DN_API bool DN_CVT_BytesToHexPtr (void const *src, DN_USize src_size, char *dest, DN_USize dest_size);
DN_API DN_Str8 DN_CVT_BytesToHex (DN_Arena *arena, void const *src, DN_USize size); DN_API DN_Str8 DN_CVT_BytesToHex (DN_Arena *arena, void const *src, DN_USize size);
#define DN_CVT_BytesToHexFromTLS(...) DN_CVT_BytesToHex(DN_OS_TLSTopArena(), __VA_ARGS__) #define DN_CVT_BytesToHexFromTLS(...) DN_CVT_BytesToHex(DN_OS_TLSTopArena(), __VA_ARGS__)
#define DN_CVT_BytesToHexFromFrame(...) DN_CVT_BytesToHex(DN_OS_TLSFrameArena(), __VA_ARGS__) #define DN_CVT_BytesToHexFromFrame(...) DN_CVT_BytesToHex(DN_OS_TLSFrameArena(), __VA_ARGS__)
DN_API DN_USize DN_CVT_HexToBytesPtrUnchecked (DN_Str8 hex, void *dest, DN_USize dest_size); DN_API DN_USize DN_CVT_HexToBytesPtrUnchecked (DN_Str8 hex, void *dest, DN_USize dest_size);
DN_API DN_USize DN_CVT_HexToBytesPtr (DN_Str8 hex, void *dest, DN_USize dest_size); DN_API DN_USize DN_CVT_HexToBytesPtr (DN_Str8 hex, void *dest, DN_USize dest_size);
DN_API DN_Str8 DN_CVT_HexToBytesUnchecked (DN_Arena *arena, DN_Str8 hex); DN_API DN_Str8 DN_CVT_HexToBytesUnchecked (DN_Arena *arena, DN_Str8 hex);
#define DN_CVT_HexToBytesUncheckedFromTLS(...) DN_CVT_HexToBytesUnchecked(DN_OS_TLSTopArena(), __VA_ARGS__) #define DN_CVT_HexToBytesUncheckedFromTLS(...) DN_CVT_HexToBytesUnchecked(DN_OS_TLSTopArena(), __VA_ARGS__)
DN_API DN_Str8 DN_CVT_HexToBytes (DN_Arena *arena, DN_Str8 hex); DN_API DN_Str8 DN_CVT_HexToBytes (DN_Arena *arena, DN_Str8 hex);
#define DN_CVT_HexToBytesFromFrame(...) DN_CVT_HexToBytes(DN_OS_TLSFrameArena(), __VA_ARGS__) #define DN_CVT_HexToBytesFromFrame(...) DN_CVT_HexToBytes(DN_OS_TLSFrameArena(), __VA_ARGS__)
#define DN_CVT_HexToBytesFromTLS(...) DN_CVT_HexToBytes(DN_OS_TLSTopArena(), __VA_ARGS__) #define DN_CVT_HexToBytesFromTLS(...) DN_CVT_HexToBytes(DN_OS_TLSTopArena(), __VA_ARGS__)
#endif // defined(DN_BASE_CONVERT_H) #endif // defined(DN_BASE_CONVERT_H)
#endif // !defined(DN_BASE_INC_H) #endif // !defined(DN_BASE_INC_H)

View File

@ -737,9 +737,9 @@ DN_U32 DN_DSMap_Hash(DN_DSMap<T> const *map, DN_DSMapKey key)
DN_U32 len = 0; DN_U32 len = 0;
DN_U32 h = seed; DN_U32 h = seed;
switch (key.type) { switch (key.type) {
case DN_DSMapKeyType_BufferAsU64NoHash: /*FALLTHRU*/ case DN_DSMapKeyType_BufferAsU64NoHash: /*FALLTHRU*/
case DN_DSMapKeyType_U64NoHash: DN_InvalidCodePath; /*FALLTHRU*/ case DN_DSMapKeyType_U64NoHash: DN_InvalidCodePath; /*FALLTHRU*/
case DN_DSMapKeyType_Invalid: break; case DN_DSMapKeyType_Invalid: break;
case DN_DSMapKeyType_Buffer: case DN_DSMapKeyType_Buffer:
key_ptr = DN_CAST(char const *) key.buffer_data; key_ptr = DN_CAST(char const *) key.buffer_data;

View File

@ -14,7 +14,7 @@ DN_API int DN_CVT_FmtBuffer3DotTruncate(char *buffer, int size, DN_FMT_ATTRIB ch
return result; return result;
} }
DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8(uint64_t val, char separator) DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8(DN_U64 val, char separator)
{ {
DN_CVTU64Str8 result = {}; DN_CVTU64Str8 result = {};
if (val == 0) { if (val == 0) {
@ -47,44 +47,41 @@ DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8(uint64_t val, char separator)
return result; return result;
} }
DN_API DN_CVTU64ByteSize DN_CVT_U64ToByteSize(uint64_t bytes, DN_CVTU64ByteSizeType desired_type) DN_API DN_CVTU64Bytes DN_CVT_U64ToBytes(DN_U64 bytes, DN_CVTBytesType type)
{ {
DN_CVTU64ByteSize result = {}; DN_Assert(type != DN_CVTBytesType_Count);
DN_CVTU64Bytes result = {};
result.bytes = DN_CAST(DN_F64) bytes; result.bytes = DN_CAST(DN_F64) bytes;
if (!DN_Check(desired_type != DN_CVTU64ByteSizeType_Count)) {
result.suffix = DN_CVT_U64ByteSizeTypeString(result.type);
return result;
}
if (desired_type == DN_CVTU64ByteSizeType_Auto) if (type == DN_CVTBytesType_Auto)
for (; result.type < DN_CVTU64ByteSizeType_Count && result.bytes >= 1024.0; result.type = DN_CAST(DN_CVTU64ByteSizeType)(DN_CAST(DN_USize) result.type + 1)) for (; result.type < DN_CVTBytesType_Count && result.bytes >= 1024.0; result.type = DN_CAST(DN_CVTBytesType)(DN_CAST(DN_USize) result.type + 1))
result.bytes /= 1024.0; result.bytes /= 1024.0;
else else
for (; result.type < desired_type; result.type = DN_CAST(DN_CVTU64ByteSizeType)(DN_CAST(DN_USize) result.type + 1)) for (; result.type < type; result.type = DN_CAST(DN_CVTBytesType)(DN_CAST(DN_USize) result.type + 1))
result.bytes /= 1024.0; result.bytes /= 1024.0;
result.suffix = DN_CVT_U64ByteSizeTypeString(result.type); result.suffix = DN_CVT_BytesTypeToStr8(result.type);
return result; return result;
} }
DN_API DN_Str8 DN_CVT_U64ToByteSizeStr8(DN_Arena *arena, uint64_t bytes, DN_CVTU64ByteSizeType desired_type) DN_API DN_Str8 DN_CVT_U64ToBytesStr8(DN_Arena *arena, DN_U64 bytes, DN_CVTBytesType desired_type)
{ {
DN_CVTU64ByteSize byte_size = DN_CVT_U64ToByteSize(bytes, desired_type); DN_CVTU64Bytes byte_size = DN_CVT_U64ToBytes(bytes, desired_type);
DN_Str8 result = DN_Str8_InitF(arena, "%.2f%.*s", byte_size.bytes, DN_STR_FMT(byte_size.suffix)); DN_Str8 result = DN_Str8_InitF(arena, "%.2f%.*s", byte_size.bytes, DN_STR_FMT(byte_size.suffix));
return result; return result;
} }
DN_API DN_Str8 DN_CVT_U64ByteSizeTypeString(DN_CVTU64ByteSizeType type) DN_API DN_Str8 DN_CVT_BytesTypeToStr8(DN_CVTBytesType type)
{ {
DN_Str8 result = DN_STR8(""); DN_Str8 result = DN_STR8("");
switch (type) { switch (type) {
case DN_CVTU64ByteSizeType_B: result = DN_STR8("B"); break; case DN_CVTBytesType_B: result = DN_STR8("B"); break;
case DN_CVTU64ByteSizeType_KiB: result = DN_STR8("KiB"); break; case DN_CVTBytesType_KiB: result = DN_STR8("KiB"); break;
case DN_CVTU64ByteSizeType_MiB: result = DN_STR8("MiB"); break; case DN_CVTBytesType_MiB: result = DN_STR8("MiB"); break;
case DN_CVTU64ByteSizeType_GiB: result = DN_STR8("GiB"); break; case DN_CVTBytesType_GiB: result = DN_STR8("GiB"); break;
case DN_CVTU64ByteSizeType_TiB: result = DN_STR8("TiB"); break; case DN_CVTBytesType_TiB: result = DN_STR8("TiB"); break;
case DN_CVTU64ByteSizeType_Count: result = DN_STR8(""); break; case DN_CVTBytesType_Count: result = DN_STR8(""); break;
case DN_CVTU64ByteSizeType_Auto: result = DN_STR8(""); break; case DN_CVTBytesType_Auto: result = DN_STR8(""); break;
} }
return result; return result;
} }
@ -205,14 +202,14 @@ DN_API DN_Str8 DN_CVT_F64ToAge(DN_Arena *arena, DN_F64 age_s, DN_CVTU64AgeUnit u
return result; return result;
} }
DN_API uint64_t DN_CVT_HexToU64(DN_Str8 hex) DN_API DN_U64 DN_CVT_HexToU64(DN_Str8 hex)
{ {
DN_Str8 real_hex = DN_Str8_TrimPrefix(DN_Str8_TrimPrefix(hex, DN_STR8("0x")), DN_STR8("0X")); DN_Str8 real_hex = DN_Str8_TrimPrefix(DN_Str8_TrimPrefix(hex, DN_STR8("0x")), DN_STR8("0X"));
DN_USize max_hex_size = sizeof(uint64_t) * 2 /*hex chars per byte*/; DN_USize max_hex_size = sizeof(DN_U64) * 2 /*hex chars per byte*/;
DN_Assert(real_hex.size <= max_hex_size); DN_Assert(real_hex.size <= max_hex_size);
DN_USize size = DN_Min(max_hex_size, real_hex.size); DN_USize size = DN_Min(max_hex_size, real_hex.size);
uint64_t result = 0; DN_U64 result = 0;
for (DN_USize index = 0; index < size; index++) { for (DN_USize index = 0; index < size; index++) {
char ch = real_hex.data[index]; char ch = real_hex.data[index];
DN_CharHexToU8 val = DN_Char_HexToU8(ch); DN_CharHexToU8 val = DN_Char_HexToU8(ch);
@ -223,13 +220,13 @@ DN_API uint64_t DN_CVT_HexToU64(DN_Str8 hex)
return result; return result;
} }
DN_API DN_Str8 DN_CVT_U64ToHex(DN_Arena *arena, uint64_t number, uint32_t flags) DN_API DN_Str8 DN_CVT_U64ToHex(DN_Arena *arena, DN_U64 number, uint32_t flags)
{ {
DN_Str8 prefix = {}; DN_Str8 prefix = {};
if ((flags & DN_CVTHexU64Str8Flags_0xPrefix)) if ((flags & DN_CVTU64HexStrFlags_0xPrefix))
prefix = DN_STR8("0x"); prefix = DN_STR8("0x");
char const *fmt = (flags & DN_CVTHexU64Str8Flags_UppercaseHex) ? "%I64X" : "%I64x"; char const *fmt = (flags & DN_CVTU64HexStrFlags_UppercaseHex) ? "%I64X" : "%I64x";
DN_USize required_size = DN_CStr8_FSize(fmt, number) + prefix.size; DN_USize required_size = DN_CStr8_FSize(fmt, number) + prefix.size;
DN_Str8 result = DN_Str8_Alloc(arena, required_size, DN_ZeroMem_No); DN_Str8 result = DN_Str8_Alloc(arena, required_size, DN_ZeroMem_No);
@ -241,17 +238,17 @@ DN_API DN_Str8 DN_CVT_U64ToHex(DN_Arena *arena, uint64_t number, uint32_t flags)
return result; return result;
} }
DN_API DN_CVTU64HexStr8 DN_CVT_U64ToHexStr8(uint64_t number, DN_CVTHexU64Str8Flags flags) DN_API DN_CVTU64HexStr DN_CVT_U64ToHexStr8(DN_U64 number, DN_CVTU64HexStrFlags flags)
{ {
DN_Str8 prefix = {}; DN_Str8 prefix = {};
if (flags & DN_CVTHexU64Str8Flags_0xPrefix) if (flags & DN_CVTU64HexStrFlags_0xPrefix)
prefix = DN_STR8("0x"); prefix = DN_STR8("0x");
DN_CVTU64HexStr8 result = {}; DN_CVTU64HexStr result = {};
DN_Memcpy(result.data, prefix.data, prefix.size); DN_Memcpy(result.data, prefix.data, prefix.size);
result.size += DN_CAST(int8_t) prefix.size; result.size += DN_CAST(int8_t) prefix.size;
char const *fmt = (flags & DN_CVTHexU64Str8Flags_UppercaseHex) ? "%I64X" : "%I64x"; char const *fmt = (flags & DN_CVTU64HexStrFlags_UppercaseHex) ? "%I64X" : "%I64x";
int size = DN_SNPrintF(result.data + result.size, DN_ArrayCountU(result.data) - result.size, fmt, number); int size = DN_SNPrintF(result.data + result.size, DN_ArrayCountU(result.data) - result.size, fmt, number);
result.size += DN_CAST(uint8_t) size; result.size += DN_CAST(uint8_t) size;
DN_Assert(result.size < DN_ArrayCountU(result.data)); DN_Assert(result.size < DN_ArrayCountU(result.data));

View File

@ -1,79 +1,88 @@
#if !defined(DN_BASE_CONVERT_H) #if !defined(DN_BASE_CONVERT_H)
#define DN_BASE_CONVERT_H #define DN_BASE_CONVERT_H
#include "../dn_base_inc.h"
struct DN_CVTU64Str8 struct DN_CVTU64Str8
{ {
char data[27 + 1]; // NOTE(dn): 27 is the maximum size of DN_U64 including a separator char data[27 + 1]; // NOTE(dn): 27 is the maximum size of DN_U64 including a separator
DN_U8 size; DN_U8 size;
}; };
enum DN_CVTU64ByteSizeType enum DN_CVTBytesType
{ {
DN_CVTU64ByteSizeType_B, DN_CVTBytesType_B,
DN_CVTU64ByteSizeType_KiB, DN_CVTBytesType_KiB,
DN_CVTU64ByteSizeType_MiB, DN_CVTBytesType_MiB,
DN_CVTU64ByteSizeType_GiB, DN_CVTBytesType_GiB,
DN_CVTU64ByteSizeType_TiB, DN_CVTBytesType_TiB,
DN_CVTU64ByteSizeType_Count, DN_CVTBytesType_Count,
DN_CVTU64ByteSizeType_Auto, DN_CVTBytesType_Auto,
}; };
struct DN_CVTU64ByteSize struct DN_CVTU64Bytes
{ {
DN_CVTU64ByteSizeType type; DN_CVTBytesType type;
DN_Str8 suffix; // "KiB", "MiB", "GiB" .. e.t.c DN_Str8 suffix; // "KiB", "MiB", "GiB" .. e.t.c
DN_F64 bytes; DN_F64 bytes;
}; };
struct DN_CVTU64HexStr8 struct DN_CVTU64HexStr
{ {
char data[2 /*0x*/ + 16 /*hex*/ + 1 /*null-terminator*/]; char data[2 /*0x*/ + 16 /*hex*/ + 1 /*null-terminator*/];
DN_U8 size; DN_U8 size;
}; };
typedef DN_U32 DN_CVTHexU64Str8Flags; typedef DN_U32 DN_CVTU64HexStrFlags;
enum DN_CVTHexU64Str8Flags_ enum DN_CVTU64HexStrFlags_
{ {
DN_CVTHexU64Str8Flags_Nil = 0, DN_CVTU64HexStrFlags_Nil = 0,
DN_CVTHexU64Str8Flags_0xPrefix = 1 << 0, /// Add the '0x' prefix from the string DN_CVTU64HexStrFlags_0xPrefix = 1 << 0, /// Add the '0x' prefix from the string
DN_CVTHexU64Str8Flags_UppercaseHex = 1 << 1, /// Use uppercase ascii characters for hex DN_CVTU64HexStrFlags_UppercaseHex = 1 << 1, /// Use uppercase ascii characters for hex
}; };
typedef DN_U32 DN_CVTU64AgeUnit; typedef DN_U32 DN_CVTU64AgeUnit;
enum DN_CVTU64AgeUnit_ enum DN_CVTU64AgeUnit_
{ {
DN_CVTU64AgeUnit_Sec = 1 << 0, DN_CVTU64AgeUnit_Sec = 1 << 0,
DN_CVTU64AgeUnit_Min = 1 << 1, DN_CVTU64AgeUnit_Min = 1 << 1,
DN_CVTU64AgeUnit_Hr = 1 << 2, DN_CVTU64AgeUnit_Hr = 1 << 2,
DN_CVTU64AgeUnit_Day = 1 << 3, DN_CVTU64AgeUnit_Day = 1 << 3,
DN_CVTU64AgeUnit_Week = 1 << 4, DN_CVTU64AgeUnit_Week = 1 << 4,
DN_CVTU64AgeUnit_Year = 1 << 5, DN_CVTU64AgeUnit_Year = 1 << 5,
DN_CVTU64AgeUnit_HMS = DN_CVTU64AgeUnit_Sec | DN_CVTU64AgeUnit_Min | DN_CVTU64AgeUnit_Hr, DN_CVTU64AgeUnit_HMS = DN_CVTU64AgeUnit_Sec | DN_CVTU64AgeUnit_Min | DN_CVTU64AgeUnit_Hr,
DN_CVTU64AgeUnit_All = DN_CVTU64AgeUnit_HMS | DN_CVTU64AgeUnit_Day | DN_CVTU64AgeUnit_Week | DN_CVTU64AgeUnit_Year, DN_CVTU64AgeUnit_All = DN_CVTU64AgeUnit_HMS | DN_CVTU64AgeUnit_Day | DN_CVTU64AgeUnit_Week | DN_CVTU64AgeUnit_Year,
}; };
DN_API int DN_CVT_FmtBuffer3DotTruncate (char *buffer, int size, DN_FMT_ATTRIB char const *fmt, ...); DN_API int DN_CVT_FmtBuffer3DotTruncate (char *buffer, int size, DN_FMT_ATTRIB char const *fmt, ...);
DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8 (DN_U64 val, char separator); DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8 (DN_U64 val, char separator);
DN_API DN_CVTU64ByteSize DN_CVT_U64ToByteSize (DN_U64 bytes, DN_CVTU64ByteSizeType type); DN_API DN_CVTU64Bytes DN_CVT_U64ToBytes (DN_U64 bytes, DN_CVTBytesType type);
DN_API DN_Str8 DN_CVT_U64ToByteSizeStr8 (DN_Arena *arena, DN_U64 bytes, DN_CVTU64ByteSizeType desired_type); #define DN_CVT_U64ToBytesAuto(bytes) DN_CVT_U64ToBytes(bytes, DN_CVTBytesType_Auto)
DN_API DN_Str8 DN_CVT_U64ByteSizeTypeString (DN_CVTU64ByteSizeType type); DN_API DN_Str8 DN_CVT_U64ToBytesStr8 (DN_Arena *arena, DN_U64 bytes, DN_CVTBytesType type);
DN_API DN_Str8 DN_CVT_U64ToAge (DN_Arena *arena, DN_U64 age_s, DN_CVTU64AgeUnit unit); #define DN_CVT_U64ToBytesStr8Auto(arena, bytes) DN_CVT_U64ToBytesStr8(arena, bytes, DN_CVTBytesType_Auto)
DN_API DN_Str8 DN_CVT_F64ToAge (DN_Arena *arena, DN_F64 age_s, DN_CVTU64AgeUnit unit); #define DN_CVT_U64ToBytesStr8FromTLS(bytes, type) DN_CVT_U64ToBytesStr8(DN_OS_TLSTopArena(), bytes, type)
#define DN_CVT_U64ToBytesStr8AutoFromTLS(bytes) DN_CVT_U64ToBytesStr8(DN_OS_TLSTopArena(), bytes, DN_CVTBytesType_Auto)
#define DN_CVT_U64ToBytesStr8FromFrame(bytes, type) DN_CVT_U64ToBytesStr8(DN_OS_TLSFrameArena(), bytes, type)
#define DN_CVT_U64ToBytesStr8AutoFromFrame(bytes) DN_CVT_U64ToBytesStr8(DN_OS_TLSFrameArena(), bytes, DN_CVTBytesType_Auto)
DN_API DN_Str8 DN_CVT_BytesTypeToStr8 (DN_CVTBytesType type);
DN_API DN_Str8 DN_CVT_U64ToAge (DN_Arena *arena, DN_U64 age_s, DN_CVTU64AgeUnit unit);
DN_API DN_Str8 DN_CVT_F64ToAge (DN_Arena *arena, DN_F64 age_s, DN_CVTU64AgeUnit unit);
DN_API DN_U64 DN_CVT_HexToU64 (DN_Str8 hex); DN_API DN_U64 DN_CVT_HexToU64 (DN_Str8 hex);
DN_API DN_Str8 DN_CVT_U64ToHex (DN_Arena *arena, DN_U64 number, DN_CVTHexU64Str8Flags flags); DN_API DN_Str8 DN_CVT_U64ToHex (DN_Arena *arena, DN_U64 number, DN_CVTU64HexStrFlags flags);
DN_API DN_CVTU64HexStr8 DN_CVT_U64ToHexStr8 (DN_U64 number, DN_U32 flags); #define DN_CVT_U64ToHexFromFrame(number, flags) DN_CVT_U64ToHex(DN_OS_TLSFrameArena(), number, flags)
DN_API DN_CVTU64HexStr DN_CVT_U64ToHexStr (DN_U64 number, DN_CVTU64HexStrFlags flags);
DN_API bool DN_CVT_BytesToHexPtr (void const *src, DN_USize src_size, char *dest, DN_USize dest_size); DN_API bool DN_CVT_BytesToHexPtr (void const *src, DN_USize src_size, char *dest, DN_USize dest_size);
DN_API DN_Str8 DN_CVT_BytesToHex (DN_Arena *arena, void const *src, DN_USize size); DN_API DN_Str8 DN_CVT_BytesToHex (DN_Arena *arena, void const *src, DN_USize size);
#define DN_CVT_BytesToHexFromTLS(...) DN_CVT_BytesToHex(DN_OS_TLSTopArena(), __VA_ARGS__) #define DN_CVT_BytesToHexFromTLS(...) DN_CVT_BytesToHex(DN_OS_TLSTopArena(), __VA_ARGS__)
#define DN_CVT_BytesToHexFromFrame(...) DN_CVT_BytesToHex(DN_OS_TLSFrameArena(), __VA_ARGS__) #define DN_CVT_BytesToHexFromFrame(...) DN_CVT_BytesToHex(DN_OS_TLSFrameArena(), __VA_ARGS__)
DN_API DN_USize DN_CVT_HexToBytesPtrUnchecked (DN_Str8 hex, void *dest, DN_USize dest_size); DN_API DN_USize DN_CVT_HexToBytesPtrUnchecked (DN_Str8 hex, void *dest, DN_USize dest_size);
DN_API DN_USize DN_CVT_HexToBytesPtr (DN_Str8 hex, void *dest, DN_USize dest_size); DN_API DN_USize DN_CVT_HexToBytesPtr (DN_Str8 hex, void *dest, DN_USize dest_size);
DN_API DN_Str8 DN_CVT_HexToBytesUnchecked (DN_Arena *arena, DN_Str8 hex); DN_API DN_Str8 DN_CVT_HexToBytesUnchecked (DN_Arena *arena, DN_Str8 hex);
#define DN_CVT_HexToBytesUncheckedFromTLS(...) DN_CVT_HexToBytesUnchecked(DN_OS_TLSTopArena(), __VA_ARGS__) #define DN_CVT_HexToBytesUncheckedFromTLS(...) DN_CVT_HexToBytesUnchecked(DN_OS_TLSTopArena(), __VA_ARGS__)
DN_API DN_Str8 DN_CVT_HexToBytes (DN_Arena *arena, DN_Str8 hex); DN_API DN_Str8 DN_CVT_HexToBytes (DN_Arena *arena, DN_Str8 hex);
#define DN_CVT_HexToBytesFromFrame(...) DN_CVT_HexToBytes(DN_OS_TLSFrameArena(), __VA_ARGS__) #define DN_CVT_HexToBytesFromFrame(...) DN_CVT_HexToBytes(DN_OS_TLSFrameArena(), __VA_ARGS__)
#define DN_CVT_HexToBytesFromTLS(...) DN_CVT_HexToBytes(DN_OS_TLSTopArena(), __VA_ARGS__) #define DN_CVT_HexToBytesFromTLS(...) DN_CVT_HexToBytes(DN_OS_TLSTopArena(), __VA_ARGS__)
#endif // defined(DN_BASE_CONVERT_H) #endif // defined(DN_BASE_CONVERT_H)

View File

@ -222,11 +222,11 @@ struct DN_Pool
{ {
DN_Arena *arena; DN_Arena *arena;
DN_PoolSlot *slots[DN_PoolSlotSize_Count]; DN_PoolSlot *slots[DN_PoolSlotSize_Count];
uint8_t align; DN_U8 align;
}; };
// NOTE: DN_Pool /////////////////////////////////////////////////////////////////////////////////// // NOTE: DN_Pool ///////////////////////////////////////////////////////////////////////////////////
DN_API DN_Pool DN_Pool_Init (DN_Arena *arena, uint8_t align); DN_API DN_Pool DN_Pool_Init (DN_Arena *arena, DN_U8 align);
DN_API bool DN_Pool_IsValid (DN_Pool const *pool); DN_API bool DN_Pool_IsValid (DN_Pool const *pool);
DN_API void * DN_Pool_Alloc (DN_Pool *pool, DN_USize size); DN_API void * DN_Pool_Alloc (DN_Pool *pool, DN_USize size);
DN_API DN_Str8 DN_Pool_AllocStr8FV (DN_Pool *pool, DN_FMT_ATTRIB char const *fmt, va_list args); DN_API DN_Str8 DN_Pool_AllocStr8FV (DN_Pool *pool, DN_FMT_ATTRIB char const *fmt, va_list args);

View File

@ -229,8 +229,8 @@ DN_API void DN_Debug_TrackAlloc(void *ptr, DN_USize size, bool leak_permitted)
DN_DebugAlloc *alloc = alloc_entry.value; DN_DebugAlloc *alloc = alloc_entry.value;
if (alloc_entry.found) { if (alloc_entry.found) {
if ((alloc->flags & DN_DebugAllocFlag_Freed) == 0) { if ((alloc->flags & DN_DebugAllocFlag_Freed) == 0) {
DN_Str8 alloc_size = DN_CVT_U64ToByteSizeStr8(alloc_table->arena, alloc->size, DN_CVTU64ByteSizeType_Auto); DN_Str8 alloc_size = DN_CVT_U64ToBytesStr8Auto(alloc_table->arena, alloc->size);
DN_Str8 new_alloc_size = DN_CVT_U64ToByteSizeStr8(alloc_table->arena, size, DN_CVTU64ByteSizeType_Auto); DN_Str8 new_alloc_size = DN_CVT_U64ToBytesStr8Auto(alloc_table->arena, size);
DN_HardAssertF( DN_HardAssertF(
alloc->flags & DN_DebugAllocFlag_Freed, alloc->flags & DN_DebugAllocFlag_Freed,
"This pointer is already in the leak tracker, however it has not been freed yet. This " "This pointer is already in the leak tracker, however it has not been freed yet. This "
@ -287,7 +287,7 @@ DN_API void DN_Debug_TrackDealloc(void *ptr)
DN_DebugAlloc *alloc = alloc_entry.value; DN_DebugAlloc *alloc = alloc_entry.value;
if (alloc->flags & DN_DebugAllocFlag_Freed) { if (alloc->flags & DN_DebugAllocFlag_Freed) {
DN_Str8 freed_size = DN_CVT_U64ToByteSizeStr8(alloc_table->arena, alloc->freed_size, DN_CVTU64ByteSizeType_Auto); DN_Str8 freed_size = DN_CVT_U64ToBytesStr8Auto(alloc_table->arena, alloc->freed_size);
DN_HardAssertF((alloc->flags & DN_DebugAllocFlag_Freed) == 0, DN_HardAssertF((alloc->flags & DN_DebugAllocFlag_Freed) == 0,
"Double free detected, pointer to free was already marked " "Double free detected, pointer to free was already marked "
"as freed. Either the pointer was reallocated but not " "as freed. Either the pointer was reallocated but not "
@ -329,7 +329,7 @@ DN_API void DN_Debug_DumpLeaks()
if (alloc_leaked && !leak_permitted) { if (alloc_leaked && !leak_permitted) {
leaked_bytes += alloc->size; leaked_bytes += alloc->size;
leak_count++; leak_count++;
DN_Str8 alloc_size = DN_CVT_U64ToByteSizeStr8(g_dn_core->alloc_table.arena, alloc->size, DN_CVTU64ByteSizeType_Auto); DN_Str8 alloc_size = DN_CVT_U64ToBytesStr8Auto(g_dn_core->alloc_table.arena, alloc->size);
DN_LOG_WarningF("Pointer (0x%p) leaked %.*s at:\n" DN_LOG_WarningF("Pointer (0x%p) leaked %.*s at:\n"
"%.*s", "%.*s",
alloc->ptr, DN_STR_FMT(alloc_size), alloc->ptr, DN_STR_FMT(alloc_size),
@ -340,7 +340,7 @@ DN_API void DN_Debug_DumpLeaks()
if (leak_count) { if (leak_count) {
char buffer[512]; char buffer[512];
DN_Arena arena = DN_Arena_InitFromBuffer(buffer, sizeof(buffer), DN_ArenaFlags_Nil); DN_Arena arena = DN_Arena_InitFromBuffer(buffer, sizeof(buffer), DN_ArenaFlags_Nil);
DN_Str8 leak_size = DN_CVT_U64ToByteSizeStr8(&arena, leaked_bytes, DN_CVTU64ByteSizeType_Auto); DN_Str8 leak_size = DN_CVT_U64ToBytesStr8Auto(&arena, leaked_bytes);
DN_LOG_WarningF("There were %I64u leaked allocations totalling %.*s", leak_count, DN_STR_FMT(leak_size)); DN_LOG_WarningF("There were %I64u leaked allocations totalling %.*s", leak_count, DN_STR_FMT(leak_size));
} }
} }

View File

@ -463,7 +463,7 @@ DN_API DN_Str8 DN_OS_ReadAll(DN_Arena *arena, DN_Str8 path, DN_OSErrSink *error)
result = DN_Str8_Alloc(arena, path_info.size, DN_ZeroMem_No); result = DN_Str8_Alloc(arena, path_info.size, DN_ZeroMem_No);
if (!DN_Str8_HasData(result)) { if (!DN_Str8_HasData(result)) {
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr); DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_Str8 buffer_size_str8 = DN_CVT_U64ToByteSizeStr8(tmem.arena, path_info.size, DN_CVTU64ByteSizeType_Auto); DN_Str8 buffer_size_str8 = DN_CVT_U64ToBytesStr8AutoFromTLS(path_info.size);
DN_OS_ErrSinkAppendF(error, 1 /*error_code*/, "Failed to allocate %.*s for reading file '%.*s'", DN_STR_FMT(buffer_size_str8), DN_STR_FMT(path)); DN_OS_ErrSinkAppendF(error, 1 /*error_code*/, "Failed to allocate %.*s for reading file '%.*s'", DN_STR_FMT(buffer_size_str8), DN_STR_FMT(path));
DN_Arena_TempMemEnd(temp_mem); DN_Arena_TempMemEnd(temp_mem);
result = {}; result = {};

View File

@ -604,7 +604,7 @@ DN_API DN_OSFileRead DN_OS_FileRead(DN_OSFile *file, void *buffer, DN_USize size
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr); DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
if (!DN_Check(size <= (unsigned long)-1)) { if (!DN_Check(size <= (unsigned long)-1)) {
DN_Str8 buffer_size_str8 = DN_CVT_U64ToByteSizeStr8(tmem.arena, size, DN_CVTU64ByteSizeType_Auto); DN_Str8 buffer_size_str8 = DN_CVT_U64ToBytesStr8AutoFromTLS(size);
DN_OS_ErrSinkAppendF( DN_OS_ErrSinkAppendF(
err, err,
1 /*error_code*/, 1 /*error_code*/,
@ -658,9 +658,9 @@ DN_API bool DN_OS_FileWritePtr(DN_OSFile *file, void const *buffer, DN_USize siz
} }
if (!result) { if (!result) {
DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr); DN_OSTLSTMem tmem = DN_OS_TLSTMem(nullptr);
DN_W32Error win_error = DN_W32_LastError(tmem.arena); DN_W32Error win_error = DN_W32_LastError(tmem.arena);
DN_Str8 buffer_size_str8 = DN_CVT_U64ToByteSizeStr8(tmem.arena, size, DN_CVTU64ByteSizeType_Auto); DN_Str8 buffer_size_str8 = DN_CVT_U64ToBytesStr8AutoFromTLS(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_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));
} }
return result; return result;