2023-04-01 19:21:46 -07:00
|
|
|
/*
|
|
|
|
BLOAT.
|
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
This contians all definitions not directly related to the project.
|
|
|
|
*/
|
2023-04-05 23:21:23 -07:00
|
|
|
#pragma once
|
2023-04-01 19:21:46 -07:00
|
|
|
|
|
|
|
#ifdef BLOAT_IMPL
|
|
|
|
# define ZPL_IMPLEMENTATION
|
|
|
|
#endif
|
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
// TODO: This will be removed when making the library have zero dependencies.
|
2023-04-01 19:21:46 -07:00
|
|
|
#pragma region ZPL INCLUDE
|
|
|
|
#if __clang__
|
2023-04-05 23:21:23 -07:00
|
|
|
# pragma clang diagnostic push
|
2023-04-01 19:21:46 -07:00
|
|
|
# pragma clang diagnostic ignored "-Wmissing-braces"
|
|
|
|
# pragma clang diagnostic ignored "-Wbraced-scalar-init"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// # define ZPL_HEAP_ANALYSIS
|
2023-04-09 10:59:39 -07:00
|
|
|
# define ZPL_WRAP_IN_NAMESPACE
|
2023-04-01 19:21:46 -07:00
|
|
|
# define ZPL_NO_MATH_H
|
|
|
|
# define ZPL_CUSTOM_MODULES
|
|
|
|
# define ZPL_MODULE_ESSENTIALS
|
|
|
|
# define ZPL_MODULE_CORE
|
|
|
|
# define ZPL_MODULE_TIMER
|
2023-04-03 00:55:28 -07:00
|
|
|
# define ZPL_MODULE_HASHING
|
2023-04-01 19:21:46 -07:00
|
|
|
#include "zpl.h"
|
|
|
|
|
2023-06-30 21:23:40 -07:00
|
|
|
using zpl::b32;
|
2023-04-09 10:59:39 -07:00
|
|
|
using zpl::s8;
|
|
|
|
using zpl::s16;
|
|
|
|
using zpl::s32;
|
|
|
|
using zpl::s64;
|
|
|
|
using zpl::u8;
|
|
|
|
using zpl::u32;
|
|
|
|
using zpl::u64;
|
|
|
|
using zpl::uw;
|
|
|
|
using zpl::sw;
|
2023-04-22 19:24:55 -07:00
|
|
|
using zpl::sptr;
|
|
|
|
using zpl::uptr;
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
using zpl::AllocType;
|
2023-04-09 10:59:39 -07:00
|
|
|
using zpl::Arena;
|
|
|
|
using zpl::AllocatorInfo;
|
|
|
|
using zpl::ArrayHeader;
|
|
|
|
using zpl::FileInfo;
|
|
|
|
using zpl::FileError;
|
|
|
|
using zpl::Pool;
|
2023-05-06 12:49:43 -07:00
|
|
|
// using zpl::String;
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
using zpl::EAllocation_ALLOC;
|
|
|
|
using zpl::EAllocation_FREE;
|
|
|
|
using zpl::EAllocation_FREE_ALL;
|
|
|
|
using zpl::EAllocation_RESIZE;
|
2023-04-09 10:59:39 -07:00
|
|
|
using zpl::EFileMode_WRITE;
|
|
|
|
using zpl::EFileError_NONE;
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
using zpl::ZPL_ALLOCATOR_FLAG_CLEAR_TO_ZERO;
|
|
|
|
|
|
|
|
using zpl::align_forward;
|
|
|
|
using zpl::align_forward_i64;
|
2023-04-09 21:38:47 -07:00
|
|
|
using zpl::alloc;
|
2023-07-09 09:35:48 -07:00
|
|
|
using zpl::alloc_align;
|
2023-04-09 10:59:39 -07:00
|
|
|
using zpl::arena_allocator;
|
|
|
|
using zpl::arena_init_from_memory;
|
2023-04-18 19:47:59 -07:00
|
|
|
using zpl::arena_init_from_allocator;
|
2023-04-09 10:59:39 -07:00
|
|
|
using zpl::arena_free;
|
2023-04-22 19:24:55 -07:00
|
|
|
using zpl::assert_crash;
|
2023-05-06 14:23:17 -07:00
|
|
|
using zpl::char_first_occurence;
|
2023-04-09 10:59:39 -07:00
|
|
|
using zpl::char_is_alpha;
|
2023-04-11 22:24:43 -07:00
|
|
|
using zpl::char_is_alphanumeric;
|
2023-07-09 09:35:48 -07:00
|
|
|
using zpl::char_is_digit;
|
|
|
|
using zpl::char_is_hex_digit;
|
2023-04-09 10:59:39 -07:00
|
|
|
using zpl::char_is_space;
|
|
|
|
using zpl::crc32;
|
2023-04-10 18:33:06 -07:00
|
|
|
using zpl::free_all;
|
2023-07-10 19:14:41 -07:00
|
|
|
using zpl::is_power_of_two;
|
2023-04-19 12:40:23 -07:00
|
|
|
using zpl::mem_copy;
|
2023-05-06 14:23:17 -07:00
|
|
|
using zpl::mem_move;
|
2023-04-19 12:40:23 -07:00
|
|
|
using zpl::mem_set;
|
2023-07-10 19:14:41 -07:00
|
|
|
using zpl::pointer_add;
|
2023-04-09 21:38:47 -07:00
|
|
|
using zpl::pool_allocator;
|
|
|
|
using zpl::pool_init;
|
2023-04-09 10:59:39 -07:00
|
|
|
using zpl::pool_free;
|
2023-04-19 12:40:23 -07:00
|
|
|
using zpl::process_exit;
|
2023-07-10 19:14:41 -07:00
|
|
|
using zpl::str_compare;
|
2023-05-06 12:49:43 -07:00
|
|
|
using zpl::str_copy;
|
2023-07-10 19:14:41 -07:00
|
|
|
using zpl::str_fmt_buf;
|
2023-05-07 12:03:24 -07:00
|
|
|
using zpl::str_fmt_va;
|
2023-04-19 12:40:23 -07:00
|
|
|
using zpl::str_fmt_out_va;
|
|
|
|
using zpl::str_fmt_out_err_va;
|
|
|
|
using zpl::str_len;
|
2023-07-10 19:14:41 -07:00
|
|
|
using zpl::zero_size;
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
#if __clang__
|
|
|
|
# pragma clang diagnostic pop
|
|
|
|
#endif
|
|
|
|
#pragma endregion ZPL INCLUDE
|
|
|
|
|
|
|
|
|
|
|
|
#if __clang__
|
|
|
|
# pragma clang diagnostic ignored "-Wunused-const-variable"
|
|
|
|
# pragma clang diagnostic ignored "-Wswitch"
|
|
|
|
# pragma clang diagnostic ignored "-Wunused-variable"
|
|
|
|
# pragma clang diagnostic ignored "-Wunknown-pragmas"
|
2023-04-05 23:21:23 -07:00
|
|
|
# pragma clang diagnostic ignored "-Wvarargs"
|
2023-05-07 12:03:24 -07:00
|
|
|
# pragma clang diagnostic ignored "-Wunused-function"
|
2023-04-01 19:21:46 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
#include "Banned.define.hpp"
|
|
|
|
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
#if defined(__GNUC__) || defined(__clang__)
|
2023-04-05 23:21:23 -07:00
|
|
|
// Supports 0-10 arguments
|
|
|
|
#define macro_num_args_impl( _0, \
|
|
|
|
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
|
|
|
|
_11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
|
|
|
|
N, ... \
|
|
|
|
) N
|
|
|
|
// _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
|
|
|
|
// _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
|
|
|
|
// _41, _42, _43, _44, _45, _46, _47, _48, _49, _50,
|
|
|
|
|
|
|
|
// ## deletes preceding comma if _VA_ARGS__ is empty (GCC, Clang)
|
|
|
|
#define macro_num_args(...) \
|
|
|
|
macro_num_args_impl(_, ## __VA_ARGS__, \
|
|
|
|
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
|
|
|
|
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, \
|
|
|
|
0 \
|
|
|
|
)
|
|
|
|
// 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
|
|
|
|
// 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
|
|
|
|
// 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
|
2023-04-03 23:04:19 -07:00
|
|
|
#else
|
2023-04-05 23:21:23 -07:00
|
|
|
// Supports 1-10 arguments
|
|
|
|
#define macro_num_args_impl( \
|
|
|
|
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
|
|
|
|
_11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
|
|
|
|
N, ... \
|
|
|
|
) N
|
|
|
|
|
|
|
|
#define macro_num_args(...) \
|
|
|
|
macro_num_args_impl( __VA_ARGS__, \
|
|
|
|
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
|
|
|
|
10, 9, 8, 7, 6, 5, 4, 3, 2, 1 \
|
|
|
|
)
|
2023-04-03 23:04:19 -07:00
|
|
|
#endif
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-05 23:21:23 -07:00
|
|
|
#define macro_expand( Expanded_ ) Expanded_
|
|
|
|
|
2023-05-05 21:18:44 -07:00
|
|
|
#define bit( Value_ ) ( 1 << Value_ )
|
|
|
|
#define bitfield_is_equal( Type_, Field_, Mask_ ) ( (Type_(Mask_) & Type_(Field_)) == Type_(Mask_) )
|
|
|
|
#define forceinline ZPL_ALWAYS_INLINE
|
|
|
|
#define ccast( Type_, Value_ ) * const_cast< Type_* >( & (Value_) )
|
|
|
|
#define scast( Type_, Value_ ) static_cast< Type_ >( Value_ )
|
|
|
|
#define rcast( Type_, Value_ ) reinterpret_cast< Type_ >( Value_ )
|
|
|
|
#define pcast( Type_, Value_ ) ( * (Type_*)( & (Value_) ) )
|
2023-07-10 19:14:41 -07:00
|
|
|
#define GEN_STRINGIZE_VA( ... ) #__VA_ARGS__
|
|
|
|
#define txt( ... ) GEN_STRINGIZE_VA( __VA_ARGS__ )
|
|
|
|
#define txt_to_StrC( ... ) sizeof( GEN_STRINGIZE_VA( __VA_ARGS__ ) ), GEN_STRINGIZE_VA( __VA_ARGS__ )
|
2023-04-01 19:21:46 -07:00
|
|
|
#define do_once() \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
static \
|
|
|
|
bool Done = false; \
|
|
|
|
if ( Done ) \
|
|
|
|
return; \
|
|
|
|
Done = true; \
|
|
|
|
} \
|
2023-04-03 23:04:19 -07:00
|
|
|
while(0)
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-02 09:35:14 -07:00
|
|
|
#define do_once_start \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
static \
|
|
|
|
bool Done = false; \
|
|
|
|
if ( Done ) \
|
|
|
|
break; \
|
|
|
|
Done = true;
|
|
|
|
|
|
|
|
#define do_once_end \
|
|
|
|
} \
|
|
|
|
while(0);
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
constexpr
|
|
|
|
char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED";
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
#pragma region Memory
|
|
|
|
|
|
|
|
// TODO : Use it.
|
|
|
|
struct gen_Arena
|
|
|
|
{
|
|
|
|
static
|
|
|
|
void* allocator_proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags );
|
|
|
|
|
|
|
|
static
|
|
|
|
gen_Arena init_from_memory( void* start, sw size )
|
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
|
|
|
{ nullptr, nullptr },
|
|
|
|
start,
|
|
|
|
size,
|
|
|
|
0,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
gen_Arena init_from_allocator( AllocatorInfo backing, sw size )
|
|
|
|
{
|
|
|
|
gen_Arena result =
|
|
|
|
{
|
|
|
|
backing,
|
|
|
|
alloc( backing, size),
|
|
|
|
size,
|
|
|
|
0,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
gen_Arena init_sub( gen_Arena& parent, sw size )
|
|
|
|
{
|
|
|
|
return init_from_allocator( parent.Backing, size );
|
|
|
|
}
|
|
|
|
|
|
|
|
sw alignment_of( sw alignment )
|
|
|
|
{
|
|
|
|
sw alignment_offset, result_pointer, mask;
|
|
|
|
ZPL_ASSERT( is_power_of_two( alignment ) );
|
|
|
|
|
|
|
|
alignment_offset = 0;
|
|
|
|
result_pointer = (sw) PhysicalStart + TotalUsed;
|
|
|
|
mask = alignment - 1;
|
|
|
|
|
|
|
|
if ( result_pointer & mask )
|
|
|
|
alignment_offset = alignment - ( result_pointer & mask );
|
|
|
|
|
|
|
|
return alignment_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void check()
|
|
|
|
{
|
|
|
|
ZPL_ASSERT( TempCount == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void free()
|
|
|
|
{
|
|
|
|
if ( Backing.proc )
|
|
|
|
{
|
|
|
|
zpl::free( Backing, PhysicalStart );
|
|
|
|
PhysicalStart = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sw size_remaining( sw alignment )
|
|
|
|
{
|
|
|
|
sw result = TotalSize - ( TotalUsed + alignment_of( alignment ) );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
AllocatorInfo Backing;
|
|
|
|
void* PhysicalStart;
|
|
|
|
sw TotalSize;
|
|
|
|
sw TotalUsed;
|
|
|
|
sw TempCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct gen_Pool
|
|
|
|
{
|
|
|
|
static
|
|
|
|
void* allocator_proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags );
|
|
|
|
|
|
|
|
static
|
|
|
|
gen_Pool init( AllocatorInfo backing, sw num_blocks, sw block_size )
|
|
|
|
{
|
|
|
|
return init_align( backing, num_blocks, block_size, ZPL_DEFAULT_MEMORY_ALIGNMENT );
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
gen_Pool init_align( AllocatorInfo backing, sw num_blocks, sw block_size, sw block_align );
|
|
|
|
|
|
|
|
void free()
|
|
|
|
{
|
|
|
|
if ( Backing.proc )
|
|
|
|
{
|
|
|
|
zpl::free( Backing, PhysicalStart );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AllocatorInfo Backing;
|
|
|
|
void* PhysicalStart;
|
|
|
|
void* FreeList;
|
|
|
|
sw BlockSize;
|
|
|
|
sw BlockAlign;
|
|
|
|
sw TotalSize;
|
|
|
|
sw NumBlocks;
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma endregion Memory
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
#pragma region Containers
|
|
|
|
#pragma push_macro("template")
|
|
|
|
#undef template
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
// TODO : Use it.
|
2023-07-09 22:15:25 -07:00
|
|
|
template<class Type>
|
|
|
|
struct TArray
|
|
|
|
{
|
|
|
|
struct Header
|
|
|
|
{
|
|
|
|
AllocatorInfo Allocator;
|
|
|
|
uw Capacity;
|
|
|
|
uw Num;
|
|
|
|
};
|
|
|
|
|
|
|
|
static
|
2023-07-10 19:14:41 -07:00
|
|
|
TArray init( AllocatorInfo allocator )
|
2023-07-09 22:15:25 -07:00
|
|
|
{
|
|
|
|
return init_reserve( allocator, grow_formula(0) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2023-07-10 19:14:41 -07:00
|
|
|
TArray init_reserve( AllocatorInfo allocator, sw capacity )
|
2023-07-09 22:15:25 -07:00
|
|
|
{
|
|
|
|
Header* header = rcast( Header*, alloc( allocator, sizeof(Header) + sizeof(Type) ));
|
|
|
|
|
|
|
|
if ( header == nullptr )
|
|
|
|
return { nullptr };
|
|
|
|
|
|
|
|
header->Allocator = allocator;
|
|
|
|
header->Capacity = capacity;
|
|
|
|
header->Num = 0;
|
|
|
|
|
|
|
|
return { rcast( Type*, header + 1) };
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
uw grow_formula( uw value )
|
|
|
|
{
|
|
|
|
return 2 * value * 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool append( Type value )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
|
|
|
|
|
|
|
if ( header.Num == header.Capacity )
|
|
|
|
{
|
|
|
|
if ( ! grow( header.Capacity ))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Data[ header.Num ] = value;
|
|
|
|
header.Num++;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type& back( void )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
|
|
|
return Data[ header.Num - 1 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear( void )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
|
|
|
header.Num = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fill( uw begin, uw end, Type value )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
|
|
|
|
|
|
|
if ( begin < 0 || end >= header.Num )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for ( sw idx = begin; idx < end; idx++ )
|
|
|
|
{
|
|
|
|
Data[ idx ] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free( void )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
|
|
|
zpl::free( header.Allocator, &header );
|
|
|
|
}
|
|
|
|
|
|
|
|
Header& get_header( void )
|
|
|
|
{
|
|
|
|
return *( reinterpret_cast< Header* >( Data ) - 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool grow( uw min_capacity )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
|
|
|
uw new_capacity = grow_formula( header.Capacity );
|
|
|
|
|
|
|
|
if ( new_capacity < min_capacity )
|
|
|
|
new_capacity = 8;
|
|
|
|
|
|
|
|
return set_capacity( new_capacity );
|
|
|
|
}
|
|
|
|
|
|
|
|
uw num( void )
|
|
|
|
{
|
|
|
|
return get_header().Num;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pop( void )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
|
|
|
|
|
|
|
ZPL_ASSERT( header.Num > 0 );
|
|
|
|
header.Num--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_at( uw idx )
|
|
|
|
{
|
|
|
|
Header* header = &get_header();
|
|
|
|
ZPL_ASSERT( idx < header->Num );
|
|
|
|
|
|
|
|
mem_move( header + idx, header + idx + 1, sizeof( Type ) * ( header->Num - idx - 1 ) );
|
|
|
|
header->Num--;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool reserve( uw new_capacity )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
|
|
|
|
|
|
|
if ( header.Capacity < new_capacity )
|
|
|
|
return set_capacity( new_capacity );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool resize( uw num )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
|
|
|
|
|
|
|
if ( num > header.Capacity )
|
|
|
|
{
|
|
|
|
if ( ! grow( header.Capacity ) )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
header.Num = num;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool set_capacity( uw new_capacity )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
|
|
|
|
|
|
|
if ( new_capacity == header.Capacity )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if ( new_capacity < header.Num )
|
|
|
|
header.Num = new_capacity;
|
|
|
|
|
|
|
|
sw size = sizeof( Header ) + sizeof( Type ) * new_capacity;
|
|
|
|
Header* new_header = reinterpret_cast< Header* >( alloc( header.Allocator, size ) );
|
|
|
|
|
|
|
|
if ( new_header == nullptr )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mem_move( new_header, &header, sizeof( Header ) + sizeof( Type ) * header.Num );
|
|
|
|
|
|
|
|
new_header->Allocator = header.Allocator;
|
|
|
|
new_header->Num = header.Num;
|
|
|
|
new_header->Capacity = new_capacity;
|
|
|
|
|
|
|
|
zpl::free( header.Allocator, &header );
|
|
|
|
|
|
|
|
Data = ( Type* )new_header + 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type* Data;
|
|
|
|
|
|
|
|
operator Type*()
|
|
|
|
{
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator Type const*() const
|
|
|
|
{
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
// TODO : Use it.
|
2023-07-09 22:15:25 -07:00
|
|
|
template<typename Type>
|
|
|
|
struct THashTable
|
|
|
|
{
|
|
|
|
struct FindResult
|
|
|
|
{
|
|
|
|
sw HashIndex;
|
|
|
|
sw PrevIndex;
|
|
|
|
sw EntryIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Entry
|
|
|
|
{
|
|
|
|
u64 Key;
|
|
|
|
sw Next;
|
|
|
|
Type Value;
|
|
|
|
};
|
|
|
|
|
|
|
|
static
|
2023-07-10 19:14:41 -07:00
|
|
|
THashTable init( AllocatorInfo allocator )
|
2023-07-09 22:15:25 -07:00
|
|
|
{
|
|
|
|
THashTable<Type> result = {0};
|
|
|
|
|
|
|
|
result.Hashes.init( allocator );
|
|
|
|
result.Entries.init( allocator );
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear( void )
|
|
|
|
{
|
|
|
|
for ( sw idx = 0; idx < Hashes.num(); idx++ )
|
|
|
|
Hashes[ idx ] = -1;
|
|
|
|
|
|
|
|
Hashes.clear();
|
|
|
|
Entries.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy( void )
|
|
|
|
{
|
|
|
|
if ( Hashes )
|
|
|
|
Hashes.free();
|
|
|
|
if ( Entries )
|
|
|
|
Entries.free();
|
|
|
|
}
|
|
|
|
|
|
|
|
Type* get( u64 key )
|
|
|
|
{
|
|
|
|
sw idx = find( key ).EntryIndex;
|
|
|
|
if ( idx > 0 )
|
|
|
|
return & Entries[ idx ].Value;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
using MapProc = void (*)( u64 key, Type value );
|
|
|
|
|
|
|
|
void map( MapProc map_proc )
|
|
|
|
{
|
|
|
|
ZPL_ASSERT_NOT_NULL( map_proc );
|
|
|
|
|
|
|
|
for ( sw idx = 0; idx < Entries.num(); idx++ )
|
|
|
|
{
|
|
|
|
map_proc( Entries[ idx ].Key, Entries[ idx ].Value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
using MapMutProc = void (*)( u64 key, Type* value );
|
|
|
|
|
|
|
|
void map_mut( MapMutProc map_proc )
|
|
|
|
{
|
|
|
|
ZPL_ASSERT_NOT_NULL( map_proc );
|
|
|
|
|
|
|
|
for ( sw idx = 0; idx < Entries.num(); idx++ )
|
|
|
|
{
|
|
|
|
map_proc( Entries[ idx ].Key, & Entries[ idx ].Value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void grow()
|
|
|
|
{
|
|
|
|
sw new_num = TArray<Entry>::grow_formula( Entries.num() )
|
|
|
|
rehash( new_num );
|
|
|
|
}
|
|
|
|
|
|
|
|
void rehash( sw new_num )
|
|
|
|
{
|
|
|
|
sw idx;
|
|
|
|
sw last_added_index;
|
|
|
|
|
|
|
|
THashTable<Type> new_ht = init( Hashes.get_header().Allocator );
|
|
|
|
|
|
|
|
new_ht.Hashes.resize( new_num );
|
|
|
|
new_ht.Entries.reserve( new_ht.Hashes.num() );
|
|
|
|
|
|
|
|
for ( idx = 0; idx < new_ht.Hashes.num(); ++idx )
|
|
|
|
new_ht.Hashes[ idx ] = -1;
|
|
|
|
|
|
|
|
for ( idx = 0; idx < Entries.num(); ++idx )
|
|
|
|
{
|
|
|
|
Entry& entry = Entries[ idx ];
|
|
|
|
|
|
|
|
FindResult find_result;
|
|
|
|
|
|
|
|
if ( new_ht.Hashes.num() == 0 )
|
|
|
|
new_ht.grow();
|
|
|
|
|
|
|
|
entry = Entries[ idx ];
|
|
|
|
find_result = new_ht.find( entry.Key );
|
|
|
|
last_added_index = new_ht.add_entry( entry.Key );
|
|
|
|
|
|
|
|
if ( find_result.PrevIndex < 0 )
|
|
|
|
new_ht.Hashes[ find_result.HashIndex ] = last_added_index;
|
|
|
|
|
|
|
|
else
|
|
|
|
new_ht.Entries[ find_result.PrevIndex ].Next = last_added_index;
|
|
|
|
|
|
|
|
new_ht.Entries[ last_added_index ].Next = find_result.EntryIndex;
|
|
|
|
new_ht.Entries[ last_added_index ].Value = entry.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// *this = new_ht;
|
|
|
|
|
|
|
|
// old_ht.destroy();
|
|
|
|
|
|
|
|
destroy();
|
|
|
|
Hashes = new_ht.Hashes;
|
|
|
|
Entries = new_ht.Entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rehash_fast()
|
|
|
|
{
|
|
|
|
sw idx;
|
|
|
|
|
|
|
|
for ( idx = 0; idx < Entries.num(); idx++ )
|
|
|
|
Entries[ idx ].Next = -1;
|
|
|
|
|
|
|
|
for ( idx = 0; idx < Hashes.num(); idx++ )
|
|
|
|
Hashes[ idx ] = -1;
|
|
|
|
|
|
|
|
for ( idx = 0; idx < Entries.num(); idx++ )
|
|
|
|
{
|
|
|
|
Entry* entry;
|
|
|
|
|
|
|
|
FindResult find_result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove( u64 key )
|
|
|
|
{
|
|
|
|
FindResult find_result = find( key);
|
|
|
|
|
|
|
|
if ( find_result.EntryIndex >= 0 )
|
|
|
|
{
|
|
|
|
Entries.remove_at( find_result.EntryIndex );
|
|
|
|
rehash_fast();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_entry( sw idx )
|
|
|
|
{
|
|
|
|
Entries.remove_at( idx );
|
|
|
|
}
|
|
|
|
|
|
|
|
void set( u64 key, Type value )
|
|
|
|
{
|
|
|
|
sw idx;
|
|
|
|
FindResult find_result;
|
|
|
|
|
|
|
|
if ( Hashes.num() == 0 )
|
|
|
|
grow();
|
|
|
|
|
|
|
|
find_result = find( key );
|
|
|
|
|
|
|
|
if ( find_result.EntryIndex >= 0 )
|
|
|
|
{
|
|
|
|
idx = find_result.EntryIndex;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
idx = add_entry( key );
|
|
|
|
|
|
|
|
if ( find_result.PrevIndex >= 0 )
|
|
|
|
{
|
|
|
|
Entries[ find_result.PrevIndex ].Next = idx;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Hashes[ find_result.HashIndex ] = idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Entries[ idx ].Value = value;
|
|
|
|
|
|
|
|
if ( full() )
|
|
|
|
grow();
|
|
|
|
}
|
|
|
|
|
|
|
|
sw slot( u64 key )
|
|
|
|
{
|
|
|
|
for ( sw idx = 0; idx < Hashes.num(); ++idx )
|
|
|
|
if ( Hashes[ idx ] == key )
|
|
|
|
return idx;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TArray< sw> Hashes;
|
|
|
|
TArray< Entry> Entries;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
sw add_entry( u64 key )
|
|
|
|
{
|
|
|
|
sw idx;
|
|
|
|
Entry entry = { key, -1 };
|
|
|
|
|
|
|
|
idx = Entries.num();
|
|
|
|
Entries.append( entry );
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
FindResult find( u64 key )
|
|
|
|
{
|
|
|
|
FindResult result = { -1, -1, -1 };
|
|
|
|
|
|
|
|
if ( Hashes.num() > 0 )
|
|
|
|
{
|
|
|
|
result.HashIndex = key % Hashes.num();
|
|
|
|
result.EntryIndex = Hashes[ result.HashIndex ];
|
|
|
|
|
|
|
|
while ( result.EntryIndex >= 0 )
|
|
|
|
{
|
|
|
|
if ( Entries[ result.EntryIndex ].Key == key )
|
|
|
|
break;
|
|
|
|
|
|
|
|
result.PrevIndex = result.EntryIndex;
|
|
|
|
result.EntryIndex = Entries[ result.EntryIndex ].Next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
b32 full()
|
|
|
|
{
|
|
|
|
return 0.75f * Hashes.num() < Entries.num();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pop_macro("template")
|
|
|
|
#pragma endregion Containers
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
#pragma region String
|
2023-05-06 12:49:43 -07:00
|
|
|
// Constant string with length.
|
|
|
|
struct StrC
|
|
|
|
{
|
|
|
|
sw Len;
|
|
|
|
char const* Ptr;
|
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
static constexpr
|
|
|
|
StrC from( char const* str )
|
2023-05-06 12:49:43 -07:00
|
|
|
{
|
|
|
|
return { str_len( str ), str };
|
|
|
|
}
|
|
|
|
|
|
|
|
operator char const* () const
|
|
|
|
{
|
|
|
|
return Ptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Dynamic String
|
2023-06-28 11:43:21 -07:00
|
|
|
// This is directly based off the ZPL string api.
|
2023-05-08 17:54:24 -07:00
|
|
|
// They used a header pattern
|
|
|
|
// I kept it for simplicty of porting but its not necessary to keep it that way.
|
2023-05-05 21:18:44 -07:00
|
|
|
struct String
|
|
|
|
{
|
|
|
|
struct Header
|
|
|
|
{
|
|
|
|
AllocatorInfo Allocator;
|
|
|
|
sw Length;
|
|
|
|
sw Capacity;
|
|
|
|
};
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
static
|
|
|
|
String make( AllocatorInfo allocator, char const* str )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
|
|
|
sw length = str ? str_len( str ) : 0;
|
|
|
|
return make_length( allocator, str, length );
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
static
|
|
|
|
String make( AllocatorInfo allocator, StrC str )
|
2023-05-06 13:15:49 -07:00
|
|
|
{
|
|
|
|
return make_length( allocator, str.Ptr, str.Len );
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
static
|
|
|
|
String make_reserve( AllocatorInfo allocator, sw capacity )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
|
|
|
constexpr sw header_size = sizeof( Header );
|
|
|
|
|
|
|
|
s32 alloc_size = header_size + capacity + 1;
|
|
|
|
void* allocation = alloc( allocator, alloc_size );
|
|
|
|
|
|
|
|
if ( allocation == nullptr )
|
|
|
|
return { nullptr };
|
|
|
|
|
|
|
|
mem_set( allocation, 0, alloc_size );
|
|
|
|
|
|
|
|
Header*
|
|
|
|
header = rcast(Header*, allocation);
|
|
|
|
header->Allocator = allocator;
|
|
|
|
header->Capacity = capacity;
|
|
|
|
header->Length = 0;
|
|
|
|
|
|
|
|
String result = { (char*)allocation + header_size };
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
static
|
|
|
|
String make_length( AllocatorInfo allocator, char const* str, sw length )
|
2023-05-06 14:23:17 -07:00
|
|
|
{
|
|
|
|
constexpr sw header_size = sizeof( Header );
|
|
|
|
|
|
|
|
s32 alloc_size = header_size + length + 1;
|
|
|
|
void* allocation = alloc( allocator, alloc_size );
|
|
|
|
|
|
|
|
if ( allocation == nullptr )
|
|
|
|
return { nullptr };
|
|
|
|
|
|
|
|
if ( ! str )
|
|
|
|
mem_set( allocation, 0, alloc_size );
|
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
Header&
|
2023-05-08 17:54:24 -07:00
|
|
|
header = * rcast(Header*, allocation);
|
|
|
|
header = { allocator, length, length };
|
|
|
|
|
|
|
|
String result = { rcast( char*, allocation) + header_size };
|
2023-05-06 14:23:17 -07:00
|
|
|
|
|
|
|
if ( length && str )
|
2023-05-07 12:03:24 -07:00
|
|
|
mem_copy( result, str, length );
|
2023-05-06 14:23:17 -07:00
|
|
|
|
|
|
|
result[ length ] = '\0';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
static
|
|
|
|
String fmt( AllocatorInfo allocator, char* buf, sw buf_size, char const* fmt, ... )
|
2023-05-06 14:23:17 -07:00
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
va_start( va, fmt );
|
|
|
|
str_fmt_va( buf, buf_size, fmt, va );
|
|
|
|
va_end( va );
|
|
|
|
|
|
|
|
return make( allocator, buf );
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
static
|
|
|
|
String fmt_buf( AllocatorInfo allocator, char const* fmt, ... )
|
2023-05-06 14:23:17 -07:00
|
|
|
{
|
|
|
|
local_persist thread_local
|
|
|
|
char buf[ ZPL_PRINTF_MAXLEN ] = { 0 };
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start( va, fmt );
|
|
|
|
str_fmt_va( buf, ZPL_PRINTF_MAXLEN, fmt, va );
|
|
|
|
va_end( va );
|
|
|
|
|
|
|
|
return make( allocator, buf );
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
static
|
|
|
|
String join( AllocatorInfo allocator, char const** parts, sw num_parts, char const* glue )
|
2023-05-06 14:23:17 -07:00
|
|
|
{
|
|
|
|
String result = make( allocator, "" );
|
|
|
|
|
|
|
|
for ( sw idx = 0; idx < num_parts; ++idx )
|
|
|
|
{
|
|
|
|
result.append( parts[ idx ] );
|
|
|
|
|
|
|
|
if ( idx < num_parts - 1 )
|
|
|
|
result.append( glue );
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
static
|
|
|
|
bool are_equal( String lhs, String rhs )
|
2023-05-06 14:23:17 -07:00
|
|
|
{
|
|
|
|
if ( lhs.length() != rhs.length() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for ( sw idx = 0; idx < lhs.length(); ++idx )
|
|
|
|
if ( lhs[ idx ] != rhs[ idx ] )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-05-07 12:03:24 -07:00
|
|
|
bool make_space_for( char const* str, sw add_len )
|
|
|
|
{
|
|
|
|
sw available = avail_space();
|
|
|
|
|
|
|
|
// NOTE: Return if there is enough space left
|
|
|
|
if ( available >= add_len )
|
|
|
|
{
|
2023-06-28 11:43:21 -07:00
|
|
|
return true;
|
2023-05-07 12:03:24 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sw new_len, old_size, new_size;
|
|
|
|
|
|
|
|
void* ptr;
|
|
|
|
void* new_ptr;
|
|
|
|
|
|
|
|
AllocatorInfo allocator = get_header().Allocator;
|
|
|
|
Header* header = nullptr;
|
|
|
|
|
|
|
|
new_len = length() + add_len;
|
|
|
|
ptr = & get_header();
|
|
|
|
old_size = size_of( Header ) + length() + 1;
|
|
|
|
new_size = size_of( Header ) + new_len + 1;
|
|
|
|
|
|
|
|
new_ptr = resize( allocator, ptr, old_size, new_size );
|
|
|
|
|
|
|
|
if ( new_ptr == nullptr )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
header = zpl_cast( Header* ) new_ptr;
|
|
|
|
header->Allocator = allocator;
|
|
|
|
header->Capacity = new_len;
|
|
|
|
|
|
|
|
Data = rcast( char*, header + 1 );
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
2023-05-06 14:23:17 -07:00
|
|
|
|
|
|
|
bool append( char const* str )
|
|
|
|
{
|
|
|
|
return append( str, str_len( str ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool append( char const* str, sw length )
|
|
|
|
{
|
2023-05-07 12:03:24 -07:00
|
|
|
if ( sptr(str) > 0 )
|
2023-05-06 14:23:17 -07:00
|
|
|
{
|
2023-06-28 11:43:21 -07:00
|
|
|
sw curr_len = this->length();
|
2023-05-06 14:23:17 -07:00
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
if ( ! make_space_for( str, length ) )
|
2023-05-06 14:23:17 -07:00
|
|
|
return false;
|
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
Header& header = get_header();
|
|
|
|
|
2023-05-06 14:23:17 -07:00
|
|
|
mem_copy( Data + curr_len, str, length );
|
|
|
|
|
|
|
|
Data[ curr_len + length ] = '\0';
|
|
|
|
|
|
|
|
header.Length = curr_len + length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool append( StrC str)
|
|
|
|
{
|
|
|
|
return append( str.Ptr, str.Len );
|
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-05-06 14:23:17 -07:00
|
|
|
bool append( const String other )
|
|
|
|
{
|
|
|
|
return append( other.Data, other.length() );
|
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-05-06 14:23:17 -07:00
|
|
|
bool append_fmt( char const* fmt, ... )
|
|
|
|
{
|
|
|
|
sw res;
|
|
|
|
char buf[ ZPL_PRINTF_MAXLEN ] = { 0 };
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-05-06 14:23:17 -07:00
|
|
|
va_list va;
|
|
|
|
va_start( va, fmt );
|
|
|
|
res = str_fmt_va( buf, count_of( buf ) - 1, fmt, va ) - 1;
|
|
|
|
va_end( va );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-05-06 14:23:17 -07:00
|
|
|
return append( buf, res );
|
|
|
|
}
|
|
|
|
|
|
|
|
sw avail_space() const
|
|
|
|
{
|
|
|
|
Header const&
|
|
|
|
header = * rcast( Header const*, Data - sizeof( Header ));
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-05-06 14:23:17 -07:00
|
|
|
return header.Capacity - header.Length;
|
|
|
|
}
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-05-06 14:23:17 -07:00
|
|
|
sw capacity() const
|
|
|
|
{
|
|
|
|
Header const&
|
|
|
|
header = * rcast( Header const*, Data - sizeof( Header ));
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-05-06 14:23:17 -07:00
|
|
|
return header.Capacity;
|
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-05-06 14:23:17 -07:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
get_header().Length = 0;
|
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-05-06 14:23:17 -07:00
|
|
|
String duplicate( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
return make_length( allocator, Data, length() );
|
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-05-06 14:23:17 -07:00
|
|
|
void free()
|
|
|
|
{
|
|
|
|
if ( ! Data )
|
|
|
|
return;
|
|
|
|
|
|
|
|
Header& header = get_header();
|
|
|
|
|
|
|
|
zpl::free( header.Allocator, & header );
|
|
|
|
}
|
|
|
|
|
|
|
|
Header& get_header()
|
2023-05-06 12:49:43 -07:00
|
|
|
{
|
2023-05-08 17:54:24 -07:00
|
|
|
return *(Header*)(Data - sizeof(Header));
|
2023-05-06 12:49:43 -07:00
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-05-06 14:23:17 -07:00
|
|
|
sw length() const
|
|
|
|
{
|
|
|
|
Header const&
|
|
|
|
header = * rcast( Header const*, Data - sizeof( Header ));
|
|
|
|
|
|
|
|
return header.Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void trim( char const* cut_set )
|
|
|
|
{
|
|
|
|
sw len = 0;
|
|
|
|
|
2023-05-07 12:03:24 -07:00
|
|
|
char* start_pos = Data;
|
|
|
|
char* end_pos = Data + length() - 1;
|
2023-05-06 14:23:17 -07:00
|
|
|
|
|
|
|
while ( start_pos <= end_pos && char_first_occurence( cut_set, *start_pos ) )
|
|
|
|
start_pos++;
|
|
|
|
|
|
|
|
while ( end_pos > start_pos && char_first_occurence( cut_set, *end_pos ) )
|
|
|
|
end_pos--;
|
|
|
|
|
|
|
|
len = scast( sw, ( start_pos > end_pos ) ? 0 : ( ( end_pos - start_pos ) + 1 ) );
|
|
|
|
|
|
|
|
if ( Data != start_pos )
|
|
|
|
mem_move( Data, start_pos, len );
|
|
|
|
|
|
|
|
Data[ len ] = '\0';
|
|
|
|
|
|
|
|
get_header().Length = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void trim_space()
|
|
|
|
{
|
|
|
|
return trim( " \t\r\n\v\f" );
|
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
operator bool()
|
|
|
|
{
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator char* ()
|
|
|
|
{
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator char const* () const
|
|
|
|
{
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
|
2023-06-30 21:23:40 -07:00
|
|
|
operator StrC() const
|
2023-05-06 12:49:43 -07:00
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
|
|
|
length(),
|
|
|
|
Data
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
// Used with cached strings
|
|
|
|
// Essentially makes the string a string view.
|
2023-05-06 12:49:43 -07:00
|
|
|
String const& operator = ( String const& other ) const
|
|
|
|
{
|
|
|
|
if ( this == & other )
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
String& this_ = ccast( String, *this );
|
|
|
|
|
|
|
|
this_.Data = other.Data;
|
|
|
|
|
|
|
|
return this_;
|
|
|
|
}
|
|
|
|
|
|
|
|
char& operator [] ( sw index )
|
|
|
|
{
|
|
|
|
return Data[ index ];
|
|
|
|
}
|
|
|
|
|
|
|
|
char const& operator [] ( sw index ) const
|
|
|
|
{
|
|
|
|
return Data[ index ];
|
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
|
|
|
|
char* Data = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct String_POD
|
|
|
|
{
|
|
|
|
char* Data;
|
2023-05-08 17:54:24 -07:00
|
|
|
|
|
|
|
operator String()
|
|
|
|
{
|
|
|
|
return * rcast(String*, this);
|
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
};
|
|
|
|
static_assert( sizeof( String_POD ) == sizeof( String ), "String is not a POD" );
|
|
|
|
#pragma endregion String
|
|
|
|
|
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
namespace Memory
|
|
|
|
{
|
2023-07-09 09:35:48 -07:00
|
|
|
// NOTE: This limits the size of the string that can be read from a file or generated to 10 megs.
|
|
|
|
// If you are generating a string larger than this, increase the size of the bucket here.
|
|
|
|
constexpr uw BucketSize = megabytes(10);
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
// Global allocator used for data with process lifetime.
|
|
|
|
extern AllocatorInfo GlobalAllocator;
|
2023-04-03 23:04:19 -07:00
|
|
|
|
|
|
|
// Heap allocator is being used for now to isolate errors from being memory related (tech debt till ready to address)
|
2023-07-09 09:35:48 -07:00
|
|
|
// #define g_allocator heap()
|
2023-04-01 19:21:46 -07:00
|
|
|
|
|
|
|
void setup();
|
|
|
|
void cleanup();
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
inline
|
2023-05-06 12:49:43 -07:00
|
|
|
sw log_fmt(char const* fmt, ...)
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
|
|
|
sw res;
|
|
|
|
va_list va;
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
va_start(va, fmt);
|
2023-04-19 12:40:23 -07:00
|
|
|
res = str_fmt_out_va(fmt, va);
|
2023-04-01 19:21:46 -07:00
|
|
|
va_end(va);
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2023-05-06 12:49:43 -07:00
|
|
|
sw fatal(char const* fmt, ...)
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-05 23:21:23 -07:00
|
|
|
local_persist thread_local
|
2023-04-01 19:21:46 -07:00
|
|
|
char buf[ZPL_PRINTF_MAXLEN] = { 0 };
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
#if Build_Debug
|
|
|
|
va_start(va, fmt);
|
2023-05-07 12:03:24 -07:00
|
|
|
str_fmt_va(buf, ZPL_PRINTF_MAXLEN, fmt, va);
|
2023-04-01 19:21:46 -07:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
assert_crash(buf);
|
2023-04-03 23:04:19 -07:00
|
|
|
return -1;
|
2023-04-01 19:21:46 -07:00
|
|
|
#else
|
|
|
|
va_start(va, fmt);
|
2023-04-19 12:40:23 -07:00
|
|
|
str_fmt_out_err_va( fmt, va);
|
2023-04-01 19:21:46 -07:00
|
|
|
va_end(va);
|
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
exit(1);
|
2023-04-03 23:04:19 -07:00
|
|
|
return -1;
|
2023-04-01 19:21:46 -07:00
|
|
|
#endif
|
|
|
|
}
|