Fixed bugs in basalt memory tag name generation. Centralised alignment values to basalt::mem::get_memory_tag_alignment_bytes.

This commit is contained in:
2025-06-22 14:58:20 +10:00
parent b5ef8e4ab5
commit bff17a13b6
2 changed files with 41 additions and 20 deletions

View File

@@ -74,7 +74,8 @@ namespace basalt
char* get_memory_usage_string(void);
i64 get_memory_tag_class_name(MEMORY_TAG memory_class, char* out_buf, u64 out_buf_size);
i64 get_memory_tag_zone_name(MEMORY_TAG zone_class, char* out_buf, u64 out_buf_size);
i64 get_memory_tag_zone_name(MEMORY_TAG memory_zone, char* out_buf, u64 out_buf_size);
i64 get_memory_tag_alignment_bytes(MEMORY_TAG memory_alignment);
template<typename T>
T* allocT(u64 num_elements, MEMORY_TAG tag)

View File

@@ -32,14 +32,15 @@ void basalt::mem::terminate_memory(void) {}
void* basalt::mem::alloc(u64 num_bytes, MEMORY_TAG tag)
{
void* ptr = nullptr;
u64 alignment = get_memory_tag_alignment_bytes(tag);
if ((tag & MEMORY_TAG_MASK_ALIGN) == MEMORY_TAG_ALIGN_ANY)
ptr = malloc(num_bytes);
ptr = _mm_malloc(num_bytes, alignment);
else if ((tag & MEMORY_TAG_MASK_ALIGN) == MEMORY_TAG_ALIGN_32)
ptr = _mm_malloc(num_bytes, 32);
ptr = _mm_malloc(num_bytes, alignment);
else if ((tag & MEMORY_TAG_MASK_ALIGN) == MEMORY_TAG_ALIGN_64)
ptr = _mm_malloc(num_bytes, 64);
ptr = _mm_malloc(num_bytes, alignment);
else if ((tag & MEMORY_TAG_MASK_ALIGN) == MEMORY_TAG_ALIGN_PAGE)
ptr = _mm_malloc(num_bytes, 4096);
ptr = _mm_malloc(num_bytes, alignment);
if (ptr != nullptr)
{
basalt_memory_state.alloc_total += num_bytes;
@@ -187,49 +188,68 @@ static const char* basalt_memory_zone_names[MEMORY_TAG_ZONE_MAX_BUILTIN] = {
i64 basalt::mem::get_memory_tag_class_name(MEMORY_TAG memory_class, char* out_buf, u64 out_buf_size)
{
constexpr u16 user_index_max = MEMORY_TAG_CLASS_MAX - MEMORY_TAG_CLASS_MAX_BUILTIN + 1;
memory_class = (memory_class & MEMORY_TAG_MASK_CLASS) >> MEMORY_TAG_SHIFT_CLASS;
// Calculate the user postfix value
if (memory_class > user_index_max)
return 0;
if (memory_class < MEMORY_TAG_CLASS_MAX_BUILTIN)
{
if (out_buf_size < basalt_memory_class_name_lengths[memory_class])
return 0;
memcpy(out_buf, basalt_memory_class_names[memory_class], basalt_memory_class_name_lengths[memory_class]);
return basalt_memory_class_name_lengths[memory_class];
}
// Calculate the user postfix value
constexpr u16 user_index_max = MEMORY_TAG_CLASS_MAX - MEMORY_TAG_CLASS_MAX_BUILTIN + 1;
u16 user_index = ((memory_class & MEMORY_TAG_ZONE_MAX) >> MEMORY_TAG_SHIFT_ZONE);
if (user_index > user_index_max)
return 0;
user_index = user_index_max - user_index;
memory_class = user_index_max - memory_class;
// Calculate the number of digits the number will contain
u16 ndigits = (u16)ceil(log10l(user_index));
u16 ndigits = (u16)ceil(log10l(memory_class));
if ((ndigits + 7) > out_buf_size)
return 0;
// Convert the string to an int and prefix it with USER_
snprintf(out_buf, out_buf_size, "USER_%u", user_index);
snprintf(out_buf, out_buf_size, "USER_%u", memory_class);
return 7+ndigits*sizeof(char);
}
i64 basalt::mem::get_memory_tag_zone_name(MEMORY_TAG zone_class, char* out_buf, u64 out_buf_size)
{
constexpr u16 user_index_max = MEMORY_TAG_CLASS_MAX - MEMORY_TAG_CLASS_MAX_BUILTIN + 1;
if (zone_class > user_index_max)
return 0;
zone_class = (zone_class & MEMORY_TAG_MASK_ZONE) >> MEMORY_TAG_SHIFT_ZONE;
if (zone_class < MEMORY_TAG_ZONE_MAX_BUILTIN)
{
if (out_buf_size < basalt_memory_zone_name_lengths[zone_class])
return 0;
memcpy(out_buf, basalt_memory_zone_names[zone_class], basalt_memory_zone_name_lengths[zone_class]);
return basalt_memory_zone_name_lengths[zone_class];
}
constexpr u16 user_index_max = MEMORY_TAG_CLASS_MAX - MEMORY_TAG_CLASS_MAX_BUILTIN + 1;
u16 user_index = ((zone_class & MEMORY_TAG_ZONE_MAX) >> MEMORY_TAG_SHIFT_ZONE);
if (user_index > user_index_max)
return 0;
user_index = user_index_max - user_index;
zone_class = user_index_max - zone_class;
u16 ndigits = (u16)ceil(log10l(user_index));
u16 ndigits = (u16)ceil(log10l(zone_class));
if ((ndigits + 7) > out_buf_size)
return 0;
snprintf(out_buf, out_buf_size, "USER_%u", user_index);
snprintf(out_buf, out_buf_size, "USER_%u", zone_class);
return 7 + ndigits * sizeof(char);
}
i64 basalt::mem::get_memory_tag_alignment_bytes(MEMORY_TAG memory_alignment)
{
switch ((memory_alignment & MEMORY_TAG_MASK_ALIGN) >> MEMORY_TAG_SHIFT_ALIGN)
{
case MEMORY_TAG_ALIGN_ANY:
return sizeof(max_align_t);
case MEMORY_TAG_ALIGN_32:
return 32;
case MEMORY_TAG_ALIGN_64:
return 64;
case MEMORY_TAG_ALIGN_PAGE:
return 4096;
default:
return 1;
}
}