2023-04-01 19:21:46 -07:00
|
|
|
#define BLOAT_IMPL
|
2023-04-01 22:07:44 -07:00
|
|
|
#include "Bloat.hpp"
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
namespace Global
|
|
|
|
{
|
|
|
|
bool ShouldShowDebug = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Memory
|
|
|
|
{
|
2023-04-09 10:59:39 -07:00
|
|
|
using namespace zpl;
|
|
|
|
|
|
|
|
Arena Global_Arena {};
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
arena_init_from_allocator( & Global_Arena, heap(), Initial_Reserve );
|
|
|
|
|
|
|
|
if ( Global_Arena.total_size == 0 )
|
|
|
|
{
|
|
|
|
assert_crash( "Failed to reserve memory for Tests:: Global_Arena" );
|
|
|
|
}
|
|
|
|
}
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
void resize( uw new_size )
|
|
|
|
{
|
2023-04-05 23:21:23 -07:00
|
|
|
void* new_memory = resize( heap(), Global_Arena.physical_start, Global_Arena.total_size, new_size );
|
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
if ( new_memory == nullptr )
|
|
|
|
{
|
|
|
|
fatal("Failed to resize global arena!");
|
|
|
|
}
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
Global_Arena.physical_start = new_memory;
|
|
|
|
Global_Arena.total_size = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cleanup()
|
|
|
|
{
|
|
|
|
arena_free( & Global_Arena);
|
2023-04-05 23:21:23 -07:00
|
|
|
}
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
struct TokEntry
|
|
|
|
{
|
|
|
|
char const* Str;
|
2023-04-05 23:21:23 -07:00
|
|
|
sw Length;
|
2023-04-03 23:04:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
ZPL_TABLE( static, TokMap, tokmap_, TokEntry )
|
|
|
|
|
|
|
|
sw token_fmt_va( char* buf, uw buf_size, char const* fmt, s32 num_tokens, va_list va )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-03 23:04:19 -07:00
|
|
|
char const* buf_begin = buf;
|
|
|
|
sw remaining = buf_size;
|
|
|
|
|
|
|
|
TokMap tok_map;
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-03 23:04:19 -07:00
|
|
|
tokmap_init( & tok_map, g_allocator );
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
s32 left = num_tokens;
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
while ( left-- )
|
|
|
|
{
|
|
|
|
char const* token = va_arg( va, char const* );
|
|
|
|
char const* value = va_arg( va, char const* );
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
TokEntry entry
|
|
|
|
{
|
2023-04-03 23:04:19 -07:00
|
|
|
value,
|
2023-04-09 10:59:39 -07:00
|
|
|
strnlen(value, (sw)128)
|
2023-04-03 23:04:19 -07:00
|
|
|
};
|
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
u32 key = crc32( token, strnlen(token, 32) );
|
2023-04-03 23:04:19 -07:00
|
|
|
|
|
|
|
tokmap_set( & tok_map, key, entry );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sw result = 0;
|
|
|
|
char current = *fmt;
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
while ( current )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-03 23:04:19 -07:00
|
|
|
sw len = 0;
|
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
while ( current && current != '{' && remaining )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-03 23:04:19 -07:00
|
|
|
*buf = *fmt;
|
|
|
|
buf++;
|
|
|
|
fmt++;
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
current = *fmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( current == '{' )
|
|
|
|
{
|
|
|
|
char const* scanner = fmt;
|
|
|
|
|
|
|
|
s32 tok_len = 0;
|
|
|
|
|
|
|
|
while ( *scanner != '}' )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-03 23:04:19 -07:00
|
|
|
tok_len++;
|
|
|
|
scanner++;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const* token = fmt;
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
u32 key = crc32( token, tok_len );
|
2023-04-03 23:04:19 -07:00
|
|
|
TokEntry value = * tokmap_get( & tok_map, key );
|
2023-04-10 18:33:06 -07:00
|
|
|
sw left = value.Length;
|
2023-04-03 23:04:19 -07:00
|
|
|
|
|
|
|
while ( left-- )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-03 23:04:19 -07:00
|
|
|
*buf = *value.Str;
|
|
|
|
buf++;
|
|
|
|
value.Str++;
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
2023-04-03 23:04:19 -07:00
|
|
|
|
|
|
|
scanner++;
|
2023-04-05 23:21:23 -07:00
|
|
|
fmt = scanner;
|
2023-04-03 23:04:19 -07:00
|
|
|
current = *fmt;
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
tokmap_clear( & tok_map );
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
return result;
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|