Cleanup CVT functions
This commit is contained in:
parent
804e5ed0a8
commit
dbc3fe63f8
@ -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
|
||||
|
||||
@ -2214,7 +2214,7 @@ DN_API int DN_CVT_FmtBuffer3DotTruncate(char *buffer, int size, DN_FMT_ATTRIB ch
|
||||
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 = {};
|
||||
if (val == 0) {
|
||||
@ -2247,44 +2247,41 @@ DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8(uint64_t val, char separator)
|
||||
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;
|
||||
if (!DN_Check(desired_type != DN_CVTU64ByteSizeType_Count)) {
|
||||
result.suffix = DN_CVT_U64ByteSizeTypeString(result.type);
|
||||
return result;
|
||||
}
|
||||
|
||||
if (desired_type == DN_CVTU64ByteSizeType_Auto)
|
||||
for (; result.type < DN_CVTU64ByteSizeType_Count && result.bytes >= 1024.0; result.type = DN_CAST(DN_CVTU64ByteSizeType)(DN_CAST(DN_USize) result.type + 1))
|
||||
if (type == DN_CVTBytesType_Auto)
|
||||
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;
|
||||
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.suffix = DN_CVT_U64ByteSizeTypeString(result.type);
|
||||
result.suffix = DN_CVT_BytesTypeToStr8(result.type);
|
||||
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));
|
||||
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("");
|
||||
switch (type) {
|
||||
case DN_CVTU64ByteSizeType_B: result = DN_STR8("B"); break;
|
||||
case DN_CVTU64ByteSizeType_KiB: result = DN_STR8("KiB"); break;
|
||||
case DN_CVTU64ByteSizeType_MiB: result = DN_STR8("MiB"); break;
|
||||
case DN_CVTU64ByteSizeType_GiB: result = DN_STR8("GiB"); break;
|
||||
case DN_CVTU64ByteSizeType_TiB: result = DN_STR8("TiB"); break;
|
||||
case DN_CVTU64ByteSizeType_Count: result = DN_STR8(""); break;
|
||||
case DN_CVTU64ByteSizeType_Auto: result = DN_STR8(""); break;
|
||||
case DN_CVTBytesType_B: result = DN_STR8("B"); break;
|
||||
case DN_CVTBytesType_KiB: result = DN_STR8("KiB"); break;
|
||||
case DN_CVTBytesType_MiB: result = DN_STR8("MiB"); break;
|
||||
case DN_CVTBytesType_GiB: result = DN_STR8("GiB"); break;
|
||||
case DN_CVTBytesType_TiB: result = DN_STR8("TiB"); break;
|
||||
case DN_CVTBytesType_Count: result = DN_STR8(""); break;
|
||||
case DN_CVTBytesType_Auto: result = DN_STR8(""); break;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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_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_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++) {
|
||||
char ch = real_hex.data[index];
|
||||
DN_CharHexToU8 val = DN_Char_HexToU8(ch);
|
||||
@ -2423,13 +2420,13 @@ DN_API uint64_t DN_CVT_HexToU64(DN_Str8 hex)
|
||||
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 = {};
|
||||
if ((flags & DN_CVTHexU64Str8Flags_0xPrefix))
|
||||
if ((flags & DN_CVTU64HexStrFlags_0xPrefix))
|
||||
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_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;
|
||||
}
|
||||
|
||||
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 = {};
|
||||
if (flags & DN_CVTHexU64Str8Flags_0xPrefix)
|
||||
if (flags & DN_CVTU64HexStrFlags_0xPrefix)
|
||||
prefix = DN_STR8("0x");
|
||||
|
||||
DN_CVTU64HexStr8 result = {};
|
||||
DN_CVTU64HexStr result = {};
|
||||
DN_Memcpy(result.data, prefix.data, 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);
|
||||
result.size += DN_CAST(uint8_t) size;
|
||||
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);
|
||||
if (!DN_Str8_HasData(result)) {
|
||||
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_Arena_TempMemEnd(temp_mem);
|
||||
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);
|
||||
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(
|
||||
err,
|
||||
1 /*error_code*/,
|
||||
@ -8501,7 +8498,7 @@ 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_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));
|
||||
}
|
||||
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;
|
||||
if (alloc_entry.found) {
|
||||
if ((alloc->flags & DN_DebugAllocFlag_Freed) == 0) {
|
||||
DN_Str8 alloc_size = DN_CVT_U64ToByteSizeStr8(alloc_table->arena, alloc->size, DN_CVTU64ByteSizeType_Auto);
|
||||
DN_Str8 new_alloc_size = DN_CVT_U64ToByteSizeStr8(alloc_table->arena, size, DN_CVTU64ByteSizeType_Auto);
|
||||
DN_Str8 alloc_size = DN_CVT_U64ToBytesStr8Auto(alloc_table->arena, alloc->size);
|
||||
DN_Str8 new_alloc_size = DN_CVT_U64ToBytesStr8Auto(alloc_table->arena, size);
|
||||
DN_HardAssertF(
|
||||
alloc->flags & DN_DebugAllocFlag_Freed,
|
||||
"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;
|
||||
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,
|
||||
"Double free detected, pointer to free was already marked "
|
||||
"as freed. Either the pointer was reallocated but not "
|
||||
@ -10120,7 +10117,7 @@ DN_API void DN_Debug_DumpLeaks()
|
||||
if (alloc_leaked && !leak_permitted) {
|
||||
leaked_bytes += alloc->size;
|
||||
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"
|
||||
"%.*s",
|
||||
alloc->ptr, DN_STR_FMT(alloc_size),
|
||||
@ -10131,7 +10128,7 @@ DN_API void DN_Debug_DumpLeaks()
|
||||
if (leak_count) {
|
||||
char buffer[512];
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
#define DN_BASE_INC_H
|
||||
@ -1195,11 +1195,11 @@ struct DN_Pool
|
||||
{
|
||||
DN_Arena *arena;
|
||||
DN_PoolSlot *slots[DN_PoolSlotSize_Count];
|
||||
uint8_t align;
|
||||
DN_U8 align;
|
||||
};
|
||||
|
||||
// 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 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);
|
||||
@ -4133,42 +4133,44 @@ template <typename T> DN_Slice<T> DN_List_To
|
||||
#if !defined(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
|
||||
{
|
||||
char data[27 + 1]; // NOTE(dn): 27 is the maximum size of DN_U64 including a separator
|
||||
DN_U8 size;
|
||||
};
|
||||
|
||||
enum DN_CVTU64ByteSizeType
|
||||
enum DN_CVTBytesType
|
||||
{
|
||||
DN_CVTU64ByteSizeType_B,
|
||||
DN_CVTU64ByteSizeType_KiB,
|
||||
DN_CVTU64ByteSizeType_MiB,
|
||||
DN_CVTU64ByteSizeType_GiB,
|
||||
DN_CVTU64ByteSizeType_TiB,
|
||||
DN_CVTU64ByteSizeType_Count,
|
||||
DN_CVTU64ByteSizeType_Auto,
|
||||
DN_CVTBytesType_B,
|
||||
DN_CVTBytesType_KiB,
|
||||
DN_CVTBytesType_MiB,
|
||||
DN_CVTBytesType_GiB,
|
||||
DN_CVTBytesType_TiB,
|
||||
DN_CVTBytesType_Count,
|
||||
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_F64 bytes;
|
||||
};
|
||||
|
||||
struct DN_CVTU64HexStr8
|
||||
struct DN_CVTU64HexStr
|
||||
{
|
||||
char data[2 /*0x*/ + 16 /*hex*/ + 1 /*null-terminator*/];
|
||||
DN_U8 size;
|
||||
};
|
||||
|
||||
typedef DN_U32 DN_CVTHexU64Str8Flags;
|
||||
enum DN_CVTHexU64Str8Flags_
|
||||
typedef DN_U32 DN_CVTU64HexStrFlags;
|
||||
enum DN_CVTU64HexStrFlags_
|
||||
{
|
||||
DN_CVTHexU64Str8Flags_Nil = 0,
|
||||
DN_CVTHexU64Str8Flags_0xPrefix = 1 << 0, /// Add the '0x' prefix from the string
|
||||
DN_CVTHexU64Str8Flags_UppercaseHex = 1 << 1, /// Use uppercase ascii characters for hex
|
||||
DN_CVTU64HexStrFlags_Nil = 0,
|
||||
DN_CVTU64HexStrFlags_0xPrefix = 1 << 0, /// Add the '0x' prefix from the string
|
||||
DN_CVTU64HexStrFlags_UppercaseHex = 1 << 1, /// Use uppercase ascii characters for hex
|
||||
};
|
||||
|
||||
typedef DN_U32 DN_CVTU64AgeUnit;
|
||||
@ -4186,15 +4188,22 @@ enum DN_CVTU64AgeUnit_
|
||||
|
||||
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_CVTU64ByteSize DN_CVT_U64ToByteSize (DN_U64 bytes, DN_CVTU64ByteSizeType type);
|
||||
DN_API DN_Str8 DN_CVT_U64ToByteSizeStr8 (DN_Arena *arena, DN_U64 bytes, DN_CVTU64ByteSizeType desired_type);
|
||||
DN_API DN_Str8 DN_CVT_U64ByteSizeTypeString (DN_CVTU64ByteSizeType type);
|
||||
DN_API DN_CVTU64Bytes DN_CVT_U64ToBytes (DN_U64 bytes, DN_CVTBytesType type);
|
||||
#define DN_CVT_U64ToBytesAuto(bytes) DN_CVT_U64ToBytes(bytes, DN_CVTBytesType_Auto)
|
||||
DN_API DN_Str8 DN_CVT_U64ToBytesStr8 (DN_Arena *arena, DN_U64 bytes, DN_CVTBytesType type);
|
||||
#define DN_CVT_U64ToBytesStr8Auto(arena, bytes) DN_CVT_U64ToBytesStr8(arena, bytes, DN_CVTBytesType_Auto)
|
||||
#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_Str8 DN_CVT_U64ToHex (DN_Arena *arena, DN_U64 number, DN_CVTHexU64Str8Flags flags);
|
||||
DN_API DN_CVTU64HexStr8 DN_CVT_U64ToHexStr8 (DN_U64 number, DN_U32 flags);
|
||||
DN_API DN_Str8 DN_CVT_U64ToHex (DN_Arena *arena, DN_U64 number, DN_CVTU64HexStrFlags 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 DN_Str8 DN_CVT_BytesToHex (DN_Arena *arena, void const *src, DN_USize size);
|
||||
|
@ -14,7 +14,7 @@ DN_API int DN_CVT_FmtBuffer3DotTruncate(char *buffer, int size, DN_FMT_ATTRIB ch
|
||||
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 = {};
|
||||
if (val == 0) {
|
||||
@ -47,44 +47,41 @@ DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8(uint64_t val, char separator)
|
||||
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;
|
||||
if (!DN_Check(desired_type != DN_CVTU64ByteSizeType_Count)) {
|
||||
result.suffix = DN_CVT_U64ByteSizeTypeString(result.type);
|
||||
return result;
|
||||
}
|
||||
|
||||
if (desired_type == DN_CVTU64ByteSizeType_Auto)
|
||||
for (; result.type < DN_CVTU64ByteSizeType_Count && result.bytes >= 1024.0; result.type = DN_CAST(DN_CVTU64ByteSizeType)(DN_CAST(DN_USize) result.type + 1))
|
||||
if (type == DN_CVTBytesType_Auto)
|
||||
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;
|
||||
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.suffix = DN_CVT_U64ByteSizeTypeString(result.type);
|
||||
result.suffix = DN_CVT_BytesTypeToStr8(result.type);
|
||||
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));
|
||||
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("");
|
||||
switch (type) {
|
||||
case DN_CVTU64ByteSizeType_B: result = DN_STR8("B"); break;
|
||||
case DN_CVTU64ByteSizeType_KiB: result = DN_STR8("KiB"); break;
|
||||
case DN_CVTU64ByteSizeType_MiB: result = DN_STR8("MiB"); break;
|
||||
case DN_CVTU64ByteSizeType_GiB: result = DN_STR8("GiB"); break;
|
||||
case DN_CVTU64ByteSizeType_TiB: result = DN_STR8("TiB"); break;
|
||||
case DN_CVTU64ByteSizeType_Count: result = DN_STR8(""); break;
|
||||
case DN_CVTU64ByteSizeType_Auto: result = DN_STR8(""); break;
|
||||
case DN_CVTBytesType_B: result = DN_STR8("B"); break;
|
||||
case DN_CVTBytesType_KiB: result = DN_STR8("KiB"); break;
|
||||
case DN_CVTBytesType_MiB: result = DN_STR8("MiB"); break;
|
||||
case DN_CVTBytesType_GiB: result = DN_STR8("GiB"); break;
|
||||
case DN_CVTBytesType_TiB: result = DN_STR8("TiB"); break;
|
||||
case DN_CVTBytesType_Count: result = DN_STR8(""); break;
|
||||
case DN_CVTBytesType_Auto: result = DN_STR8(""); break;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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_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_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++) {
|
||||
char ch = real_hex.data[index];
|
||||
DN_CharHexToU8 val = DN_Char_HexToU8(ch);
|
||||
@ -223,13 +220,13 @@ DN_API uint64_t DN_CVT_HexToU64(DN_Str8 hex)
|
||||
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 = {};
|
||||
if ((flags & DN_CVTHexU64Str8Flags_0xPrefix))
|
||||
if ((flags & DN_CVTU64HexStrFlags_0xPrefix))
|
||||
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_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;
|
||||
}
|
||||
|
||||
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 = {};
|
||||
if (flags & DN_CVTHexU64Str8Flags_0xPrefix)
|
||||
if (flags & DN_CVTU64HexStrFlags_0xPrefix)
|
||||
prefix = DN_STR8("0x");
|
||||
|
||||
DN_CVTU64HexStr8 result = {};
|
||||
DN_CVTU64HexStr result = {};
|
||||
DN_Memcpy(result.data, prefix.data, 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);
|
||||
result.size += DN_CAST(uint8_t) size;
|
||||
DN_Assert(result.size < DN_ArrayCountU(result.data));
|
||||
|
@ -1,42 +1,44 @@
|
||||
#if !defined(DN_BASE_CONVERT_H)
|
||||
#define DN_BASE_CONVERT_H
|
||||
|
||||
#include "../dn_base_inc.h"
|
||||
|
||||
struct DN_CVTU64Str8
|
||||
{
|
||||
char data[27 + 1]; // NOTE(dn): 27 is the maximum size of DN_U64 including a separator
|
||||
DN_U8 size;
|
||||
};
|
||||
|
||||
enum DN_CVTU64ByteSizeType
|
||||
enum DN_CVTBytesType
|
||||
{
|
||||
DN_CVTU64ByteSizeType_B,
|
||||
DN_CVTU64ByteSizeType_KiB,
|
||||
DN_CVTU64ByteSizeType_MiB,
|
||||
DN_CVTU64ByteSizeType_GiB,
|
||||
DN_CVTU64ByteSizeType_TiB,
|
||||
DN_CVTU64ByteSizeType_Count,
|
||||
DN_CVTU64ByteSizeType_Auto,
|
||||
DN_CVTBytesType_B,
|
||||
DN_CVTBytesType_KiB,
|
||||
DN_CVTBytesType_MiB,
|
||||
DN_CVTBytesType_GiB,
|
||||
DN_CVTBytesType_TiB,
|
||||
DN_CVTBytesType_Count,
|
||||
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_F64 bytes;
|
||||
};
|
||||
|
||||
struct DN_CVTU64HexStr8
|
||||
struct DN_CVTU64HexStr
|
||||
{
|
||||
char data[2 /*0x*/ + 16 /*hex*/ + 1 /*null-terminator*/];
|
||||
DN_U8 size;
|
||||
};
|
||||
|
||||
typedef DN_U32 DN_CVTHexU64Str8Flags;
|
||||
enum DN_CVTHexU64Str8Flags_
|
||||
typedef DN_U32 DN_CVTU64HexStrFlags;
|
||||
enum DN_CVTU64HexStrFlags_
|
||||
{
|
||||
DN_CVTHexU64Str8Flags_Nil = 0,
|
||||
DN_CVTHexU64Str8Flags_0xPrefix = 1 << 0, /// Add the '0x' prefix from the string
|
||||
DN_CVTHexU64Str8Flags_UppercaseHex = 1 << 1, /// Use uppercase ascii characters for hex
|
||||
DN_CVTU64HexStrFlags_Nil = 0,
|
||||
DN_CVTU64HexStrFlags_0xPrefix = 1 << 0, /// Add the '0x' prefix from the string
|
||||
DN_CVTU64HexStrFlags_UppercaseHex = 1 << 1, /// Use uppercase ascii characters for hex
|
||||
};
|
||||
|
||||
typedef DN_U32 DN_CVTU64AgeUnit;
|
||||
@ -54,15 +56,22 @@ enum DN_CVTU64AgeUnit_
|
||||
|
||||
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_CVTU64ByteSize DN_CVT_U64ToByteSize (DN_U64 bytes, DN_CVTU64ByteSizeType type);
|
||||
DN_API DN_Str8 DN_CVT_U64ToByteSizeStr8 (DN_Arena *arena, DN_U64 bytes, DN_CVTU64ByteSizeType desired_type);
|
||||
DN_API DN_Str8 DN_CVT_U64ByteSizeTypeString (DN_CVTU64ByteSizeType type);
|
||||
DN_API DN_CVTU64Bytes DN_CVT_U64ToBytes (DN_U64 bytes, DN_CVTBytesType type);
|
||||
#define DN_CVT_U64ToBytesAuto(bytes) DN_CVT_U64ToBytes(bytes, DN_CVTBytesType_Auto)
|
||||
DN_API DN_Str8 DN_CVT_U64ToBytesStr8 (DN_Arena *arena, DN_U64 bytes, DN_CVTBytesType type);
|
||||
#define DN_CVT_U64ToBytesStr8Auto(arena, bytes) DN_CVT_U64ToBytesStr8(arena, bytes, DN_CVTBytesType_Auto)
|
||||
#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_Str8 DN_CVT_U64ToHex (DN_Arena *arena, DN_U64 number, DN_CVTHexU64Str8Flags flags);
|
||||
DN_API DN_CVTU64HexStr8 DN_CVT_U64ToHexStr8 (DN_U64 number, DN_U32 flags);
|
||||
DN_API DN_Str8 DN_CVT_U64ToHex (DN_Arena *arena, DN_U64 number, DN_CVTU64HexStrFlags 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 DN_Str8 DN_CVT_BytesToHex (DN_Arena *arena, void const *src, DN_USize size);
|
||||
|
@ -222,11 +222,11 @@ struct DN_Pool
|
||||
{
|
||||
DN_Arena *arena;
|
||||
DN_PoolSlot *slots[DN_PoolSlotSize_Count];
|
||||
uint8_t align;
|
||||
DN_U8 align;
|
||||
};
|
||||
|
||||
// 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 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);
|
||||
|
@ -229,8 +229,8 @@ DN_API void DN_Debug_TrackAlloc(void *ptr, DN_USize size, bool leak_permitted)
|
||||
DN_DebugAlloc *alloc = alloc_entry.value;
|
||||
if (alloc_entry.found) {
|
||||
if ((alloc->flags & DN_DebugAllocFlag_Freed) == 0) {
|
||||
DN_Str8 alloc_size = DN_CVT_U64ToByteSizeStr8(alloc_table->arena, alloc->size, DN_CVTU64ByteSizeType_Auto);
|
||||
DN_Str8 new_alloc_size = DN_CVT_U64ToByteSizeStr8(alloc_table->arena, size, DN_CVTU64ByteSizeType_Auto);
|
||||
DN_Str8 alloc_size = DN_CVT_U64ToBytesStr8Auto(alloc_table->arena, alloc->size);
|
||||
DN_Str8 new_alloc_size = DN_CVT_U64ToBytesStr8Auto(alloc_table->arena, size);
|
||||
DN_HardAssertF(
|
||||
alloc->flags & DN_DebugAllocFlag_Freed,
|
||||
"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;
|
||||
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,
|
||||
"Double free detected, pointer to free was already marked "
|
||||
"as freed. Either the pointer was reallocated but not "
|
||||
@ -329,7 +329,7 @@ DN_API void DN_Debug_DumpLeaks()
|
||||
if (alloc_leaked && !leak_permitted) {
|
||||
leaked_bytes += alloc->size;
|
||||
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"
|
||||
"%.*s",
|
||||
alloc->ptr, DN_STR_FMT(alloc_size),
|
||||
@ -340,7 +340,7 @@ DN_API void DN_Debug_DumpLeaks()
|
||||
if (leak_count) {
|
||||
char buffer[512];
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
if (!DN_Str8_HasData(result)) {
|
||||
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_Arena_TempMemEnd(temp_mem);
|
||||
result = {};
|
||||
|
@ -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);
|
||||
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(
|
||||
err,
|
||||
1 /*error_code*/,
|
||||
@ -660,7 +660,7 @@ 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_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));
|
||||
}
|
||||
return result;
|
||||
|
Loading…
x
Reference in New Issue
Block a user