From 9faca77c74dd0190833088cd3226b640a027f41b Mon Sep 17 00:00:00 2001 From: Ed_ Date: Wed, 5 Feb 2025 18:13:22 -0500 Subject: [PATCH] base progress --- code/base/arena.c | 110 ++++++------ code/base/arena.h | 1 + code/base/constants.h | 326 +++++++++++++++++------------------ code/base/debug.c | 25 +++ code/base/debug.h | 19 +- code/base/memory_substrate.c | 70 ++++++-- code/base/memory_substrate.h | 26 +-- code/base/strings.h | 20 ++- 8 files changed, 349 insertions(+), 248 deletions(-) create mode 100644 code/base/debug.c diff --git a/code/base/arena.c b/code/base/arena.c index 552b5de..2730517 100644 --- a/code/base/arena.c +++ b/code/base/arena.c @@ -15,15 +15,14 @@ Arena* arena_alloc_(ArenaParams* params) { void* base = alloc(params->backing, params->block_size); - // rjf: extract arena header & fill Arena* arena = (Arena*) base; + arena->prev = nullptr; arena->current = arena; arena->backing = params->backing; arena->pos = size_of(Arena); arena->block_size = params->block_size; arena->flags = params->flags; - asan_unpoison_memory_region(base, sizeof(Arena)); return arena; } @@ -33,59 +32,66 @@ arena_alloc_(ArenaParams* params) internal void * arena_push(Arena *arena, U64 size, U64 align) { - Arena *current = arena->current; - U64 pos_pre = AlignPow2(current->pos, align); - U64 pos_pst = pos_pre + size; + SPTR header_size = size_of(Arena); + + Arena *current = arena->current; + SPTR pos_pre = AlignPow2(current->pos, align); + SPTR pos_pst = pos_pre + size; - // rjf: chain, if needed - if(current->res < pos_pst && !(arena->flags & ArenaFlag_NoChain)) - { - U64 res_size = current->res_size; - U64 cmt_size = current->cmt_size; - if(size + ARENA_HEADER_SIZE > res_size) - { - res_size = size + ARENA_HEADER_SIZE; - cmt_size = size + ARENA_HEADER_SIZE; - } - Arena *new_block = arena_alloc(.reserve_size = res_size, - .commit_size = cmt_size, - .flags = current->flags); - new_block->base_pos = current->base_pos + current->res; - SLLStackPush_N(arena->current, new_block, prev); - current = new_block; - pos_pre = AlignPow2(current->pos, align); - pos_pst = pos_pre + size; - } + // rjf: chain, if needed + if ( current->block_size < pos_pst && !(arena->flags & ArenaFlag_NoChain) ) + { + SSIZE res_size = current->block_size; + + if(size + > res_size) + { + res_size = size + ARENA_HEADER_SIZE; + } + + Arena *new_block = arena_alloc( + .reserve_size = res_size, + .commit_size = cmt_size, + .flags = current->flags + ); + + new_block->base_pos = current->base_pos + current->res; + + SLLStackPush_N(arena->current, new_block, prev); + + current = new_block; + pos_pre = AlignPow2(current->pos, align); + pos_pst = pos_pre + size; + } - // rjf: commit new pages, if needed - if(current->cmt < pos_pst && !(current->flags & ArenaFlag_LargePages)) - { - U64 cmt_pst_aligned = AlignPow2(pos_pst, current->cmt_size); - U64 cmt_pst_clamped = ClampTop(cmt_pst_aligned, current->res); - U64 cmt_size = cmt_pst_clamped - current->cmt; - os_commit((U8 *)current + current->cmt, cmt_size); - current->cmt = cmt_pst_clamped; - } + // rjf: commit new pages, if needed + if(current->cmt < pos_pst && !(current->flags & ArenaFlag_LargePages)) + { + U64 cmt_pst_aligned = AlignPow2(pos_pst, current->cmt_size); + U64 cmt_pst_clamped = ClampTop(cmt_pst_aligned, current->res); + U64 cmt_size = cmt_pst_clamped - current->cmt; + os_commit((U8 *)current + current->cmt, cmt_size); + current->cmt = cmt_pst_clamped; + } - // rjf: push onto current block - void *result = 0; - if(current->cmt >= pos_pst) - { - result = (U8 *)current+pos_pre; - current->pos = pos_pst; - AsanUnpoisonMemoryRegion(result, size); - } - - // rjf: panic on failure -#if OS_FEATURE_GRAPHICAL - if(Unlikely(result == 0)) - { - os_graphical_message(1, str8_lit("Fatal Allocation Failure"), str8_lit("Unexpected memory allocation failure.")); - os_abort(1); - } -#endif - - return result; + // rjf: push onto current block + void *result = 0; + if(current->cmt >= pos_pst) + { + result = (U8 *)current+pos_pre; + current->pos = pos_pst; + AsanUnpoisonMemoryRegion(result, size); + } + + // rjf: panic on failure + #if OS_FEATURE_GRAPHICAL + if(Unlikely(result == 0)) + { + os_graphical_message(1, str8_lit("Fatal Allocation Failure"), str8_lit("Unexpected memory allocation failure.")); + os_abort(1); + } + #endif + + return result; } internal U64 diff --git a/code/base/arena.h b/code/base/arena.h index ed0d798..994b27b 100644 --- a/code/base/arena.h +++ b/code/base/arena.h @@ -126,6 +126,7 @@ arena_release(Arena* arena) inline AllocatorInfo default_allocator() { + // NOTE(Ed): Technically we don't need the backing_vmem var tracked here, but its nice for debug. local_persist thread_local VArena* backing_vmem = nullptr; local_persist thread_local Arena* arena = nullptr; if (arena == nullptr) { diff --git a/code/base/constants.h b/code/base/constants.h index 1e4c299..858a337 100644 --- a/code/base/constants.h +++ b/code/base/constants.h @@ -9,184 +9,184 @@ //////////////////////////////// //~ NOTE(allen): Constants -#define MD_SIGN32 0x80000000; -#define MD_EXPONENT32 0x7F800000; -#define MD_MANTISSA32 0x007FFFFF; +#define SIGN32 0x80000000; +#define EXPONENT32 0x7F800000; +#define MANTISSA32 0x007FFFFF; -#define MD_BIG_GOLDEN32 1.61803398875f; -#define MD_SMALL_GOLDEN32 0.61803398875f; +#define BIG_GOLDEN32 1.61803398875f; +#define SMALL_GOLDEN32 0.61803398875f; -#define MD_PI32 3.1415926535897f; +#define PI32 3.1415926535897f; -#define MD_MACHINE_EPSILON64 4.94065645841247e-324; +#define MACHINE_EPSILON64 4.94065645841247e-324; -#define MD_U8_MIN 0u -#define MD_U8_MAX 0xffu -#define MD_S8_MIN ( -0x7f - 1 ) -#define MD_S8_MAX 0x7f +#define U8_MIN 0u +#define U8_MAX 0xffu +#define S8_MIN ( -0x7f - 1 ) +#define S8_MAX 0x7f -#define MD_U16_MIN 0u -#define MD_U16_MAX 0xffffu -#define MD_S16_MIN ( -0x7fff - 1 ) -#define MD_S16_MAX 0x7fff +#define U16_MIN 0u +#define U16_MAX 0xffffu +#define S16_MIN ( -0x7fff - 1 ) +#define S16_MAX 0x7fff -#define MD_U32_MIN 0u -#define MD_U32_MAX 0xffffffffu -#define MD_S32_MIN ( -0x7fffffff - 1 ) -#define MD_S32_MAX 0x7fffffff +#define U32_MIN 0u +#define U32_MAX 0xffffffffu +#define S32_MIN ( -0x7fffffff - 1 ) +#define S32_MAX 0x7fffffff -#define MD_U64_MIN 0ull -#define MD_U64_MAX 0xffffffffffffffffull -#define MD_S64_MIN ( -0x7fffffffffffffffll - 1 ) -#define MD_S64_MAX 0x7fffffffffffffffll +#define U64_MIN 0ull +#define U64_MAX 0xffffffffffffffffull +#define S64_MIN ( -0x7fffffffffffffffll - 1 ) +#define S64_MAX 0x7fffffffffffffffll #if defined( ARCH_32_BIT ) -# define MD_USIZE_MIN MD_U32_MIN -# define MD_USIZE_MAX MD_U32_MAX -# define MD_ISIZE_MIN MD_S32_MIN -# define MD_ISIZE_MAX MD_S32_MAX +# define USIZE_MIN U32_MIN +# define USIZE_MAX U32_MAX +# define ISIZE_MIN S32_MIN +# define ISIZE_MAX S32_MAX #elif defined( ARCH_64_BIT ) -# define MD_USIZE_MIN MD_U64_MIN -# define MD_USIZE_MAX MD_U64_MAX -# define MD_ISIZE_MIN MD_S64_MIN -# define MD_ISIZE_MAX MD_S64_MAX +# define USIZE_MIN U64_MIN +# define USIZE_MAX U64_MAX +# define ISIZE_MIN S64_MIN +# define ISIZE_MAX S64_MAX #else # error Unknown architecture size. This library only supports 32 bit and 64 bit architectures. #endif -#define MD_F32_MIN 1.17549435e-38f -#define MD_F32_MAX 3.40282347e+38f -#define MD_F64_MIN 2.2250738585072014e-308 -#define MD_F64_MAX 1.7976931348623157e+308 +#define F32_MIN 1.17549435e-38f +#define F32_MAX 3.40282347e+38f +#define F64_MIN 2.2250738585072014e-308 +#define F64_MAX 1.7976931348623157e+308 -#define MD_BITMASK1 0x00000001 -#define MD_BITMASK2 0x00000003 -#define MD_BITMASK3 0x00000007 -#define MD_BITMASK4 0x0000000f -#define MD_BITMASK5 0x0000001f -#define MD_BITMASK6 0x0000003f -#define MD_BITMASK7 0x0000007f -#define MD_BITMASK8 0x000000ff -#define MD_BITMASK9 0x000001ff -#define MD_BITMASK10 0x000003ff -#define MD_BITMASK11 0x000007ff -#define MD_BITMASK12 0x00000fff -#define MD_BITMASK13 0x00001fff -#define MD_BITMASK14 0x00003fff -#define MD_BITMASK15 0x00007fff -#define MD_BITMASK16 0x0000ffff -#define MD_BITMASK17 0x0001ffff -#define MD_BITMASK18 0x0003ffff -#define MD_BITMASK19 0x0007ffff -#define MD_BITMASK20 0x000fffff -#define MD_BITMASK21 0x001fffff -#define MD_BITMASK22 0x003fffff -#define MD_BITMASK23 0x007fffff -#define MD_BITMASK24 0x00ffffff -#define MD_BITMASK25 0x01ffffff -#define MD_BITMASK26 0x03ffffff -#define MD_BITMASK27 0x07ffffff -#define MD_BITMASK28 0x0fffffff -#define MD_BITMASK29 0x1fffffff -#define MD_BITMASK30 0x3fffffff -#define MD_BITMASK31 0x7fffffff -#define MD_BITMASK32 0xffffffff +#define BITMASK1 0x00000001 +#define BITMASK2 0x00000003 +#define BITMASK3 0x00000007 +#define BITMASK4 0x0000000f +#define BITMASK5 0x0000001f +#define BITMASK6 0x0000003f +#define BITMASK7 0x0000007f +#define BITMASK8 0x000000ff +#define BITMASK9 0x000001ff +#define BITMASK10 0x000003ff +#define BITMASK11 0x000007ff +#define BITMASK12 0x00000fff +#define BITMASK13 0x00001fff +#define BITMASK14 0x00003fff +#define BITMASK15 0x00007fff +#define BITMASK16 0x0000ffff +#define BITMASK17 0x0001ffff +#define BITMASK18 0x0003ffff +#define BITMASK19 0x0007ffff +#define BITMASK20 0x000fffff +#define BITMASK21 0x001fffff +#define BITMASK22 0x003fffff +#define BITMASK23 0x007fffff +#define BITMASK24 0x00ffffff +#define BITMASK25 0x01ffffff +#define BITMASK26 0x03ffffff +#define BITMASK27 0x07ffffff +#define BITMASK28 0x0fffffff +#define BITMASK29 0x1fffffff +#define BITMASK30 0x3fffffff +#define BITMASK31 0x7fffffff +#define BITMASK32 0xffffffff -#define MD_BITMASK33 0x00000001ffffffffull -#define MD_BITMASK34 0x00000003ffffffffull -#define MD_BITMASK35 0x00000007ffffffffull -#define MD_BITMASK36 0x0000000fffffffffull -#define MD_BITMASK37 0x0000001fffffffffull -#define MD_BITMASK38 0x0000003fffffffffull -#define MD_BITMASK39 0x0000007fffffffffull -#define MD_BITMASK40 0x000000ffffffffffull -#define MD_BITMASK41 0x000001ffffffffffull -#define MD_BITMASK42 0x000003ffffffffffull -#define MD_BITMASK43 0x000007ffffffffffull -#define MD_BITMASK44 0x00000fffffffffffull -#define MD_BITMASK45 0x00001fffffffffffull -#define MD_BITMASK46 0x00003fffffffffffull -#define MD_BITMASK47 0x00007fffffffffffull -#define MD_BITMASK48 0x0000ffffffffffffull -#define MD_BITMASK49 0x0001ffffffffffffull -#define MD_BITMASK50 0x0003ffffffffffffull -#define MD_BITMASK51 0x0007ffffffffffffull -#define MD_BITMASK52 0x000fffffffffffffull -#define MD_BITMASK53 0x001fffffffffffffull -#define MD_BITMASK54 0x003fffffffffffffull -#define MD_BITMASK55 0x007fffffffffffffull -#define MD_BITMASK56 0x00ffffffffffffffull -#define MD_BITMASK57 0x01ffffffffffffffull -#define MD_BITMASK58 0x03ffffffffffffffull -#define MD_BITMASK59 0x07ffffffffffffffull -#define MD_BITMASK60 0x0fffffffffffffffull -#define MD_BITMASK61 0x1fffffffffffffffull -#define MD_BITMASK62 0x3fffffffffffffffull -#define MD_BITMASK63 0x7fffffffffffffffull -#define MD_BITMASK64 0xffffffffffffffffull +#define BITMASK33 0x00000001ffffffffull +#define BITMASK34 0x00000003ffffffffull +#define BITMASK35 0x00000007ffffffffull +#define BITMASK36 0x0000000fffffffffull +#define BITMASK37 0x0000001fffffffffull +#define BITMASK38 0x0000003fffffffffull +#define BITMASK39 0x0000007fffffffffull +#define BITMASK40 0x000000ffffffffffull +#define BITMASK41 0x000001ffffffffffull +#define BITMASK42 0x000003ffffffffffull +#define BITMASK43 0x000007ffffffffffull +#define BITMASK44 0x00000fffffffffffull +#define BITMASK45 0x00001fffffffffffull +#define BITMASK46 0x00003fffffffffffull +#define BITMASK47 0x00007fffffffffffull +#define BITMASK48 0x0000ffffffffffffull +#define BITMASK49 0x0001ffffffffffffull +#define BITMASK50 0x0003ffffffffffffull +#define BITMASK51 0x0007ffffffffffffull +#define BITMASK52 0x000fffffffffffffull +#define BITMASK53 0x001fffffffffffffull +#define BITMASK54 0x003fffffffffffffull +#define BITMASK55 0x007fffffffffffffull +#define BITMASK56 0x00ffffffffffffffull +#define BITMASK57 0x01ffffffffffffffull +#define BITMASK58 0x03ffffffffffffffull +#define BITMASK59 0x07ffffffffffffffull +#define BITMASK60 0x0fffffffffffffffull +#define BITMASK61 0x1fffffffffffffffull +#define BITMASK62 0x3fffffffffffffffull +#define BITMASK63 0x7fffffffffffffffull +#define BITMASK64 0xffffffffffffffffull -#define MD_BIT1 (1 << 0) -#define MD_BIT2 (1 << 1) -#define MD_BIT3 (1 << 2) -#define MD_BIT4 (1 << 3) -#define MD_BIT5 (1 << 4) -#define MD_BIT6 (1 << 5) -#define MD_BIT7 (1 << 6) -#define MD_BIT8 (1 << 7) -#define MD_BIT9 (1 << 8) -#define MD_BIT10 (1 << 9) -#define MD_BIT11 (1 << 10) -#define MD_BIT12 (1 << 11) -#define MD_BIT13 (1 << 12) -#define MD_BIT14 (1 << 13) -#define MD_BIT15 (1 << 14) -#define MD_BIT16 (1 << 15) -#define MD_BIT17 (1 << 16) -#define MD_BIT18 (1 << 17) -#define MD_BIT19 (1 << 18) -#define MD_BIT20 (1 << 19) -#define MD_BIT21 (1 << 20) -#define MD_BIT22 (1 << 21) -#define MD_BIT23 (1 << 22) -#define MD_BIT24 (1 << 23) -#define MD_BIT25 (1 << 24) -#define MD_BIT26 (1 << 25) -#define MD_BIT27 (1 << 26) -#define MD_BIT28 (1 << 27) -#define MD_BIT29 (1 << 28) -#define MD_BIT30 (1 << 29) -#define MD_BIT31 (1 << 30) -#define MD_BIT32 (1 << 31) +#define BIT1 (1 << 0) +#define BIT2 (1 << 1) +#define BIT3 (1 << 2) +#define BIT4 (1 << 3) +#define BIT5 (1 << 4) +#define BIT6 (1 << 5) +#define BIT7 (1 << 6) +#define BIT8 (1 << 7) +#define BIT9 (1 << 8) +#define BIT10 (1 << 9) +#define BIT11 (1 << 10) +#define BIT12 (1 << 11) +#define BIT13 (1 << 12) +#define BIT14 (1 << 13) +#define BIT15 (1 << 14) +#define BIT16 (1 << 15) +#define BIT17 (1 << 16) +#define BIT18 (1 << 17) +#define BIT19 (1 << 18) +#define BIT20 (1 << 19) +#define BIT21 (1 << 20) +#define BIT22 (1 << 21) +#define BIT23 (1 << 22) +#define BIT24 (1 << 23) +#define BIT25 (1 << 24) +#define BIT26 (1 << 25) +#define BIT27 (1 << 26) +#define BIT28 (1 << 27) +#define BIT29 (1 << 28) +#define BIT30 (1 << 29) +#define BIT31 (1 << 30) +#define BIT32 (1 << 31) -#define MD_BIT33 (1ull << 32) -#define MD_BIT34 (1ull << 33) -#define MD_BIT35 (1ull << 34) -#define MD_BIT36 (1ull << 35) -#define MD_BIT37 (1ull << 36) -#define MD_BIT38 (1ull << 37) -#define MD_BIT39 (1ull << 38) -#define MD_BIT40 (1ull << 39) -#define MD_BIT41 (1ull << 40) -#define MD_BIT42 (1ull << 41) -#define MD_BIT43 (1ull << 42) -#define MD_BIT44 (1ull << 43) -#define MD_BIT45 (1ull << 44) -#define MD_BIT46 (1ull << 45) -#define MD_BIT47 (1ull << 46) -#define MD_BIT48 (1ull << 47) -#define MD_BIT49 (1ull << 48) -#define MD_BIT50 (1ull << 49) -#define MD_BIT51 (1ull << 50) -#define MD_BIT52 (1ull << 51) -#define MD_BIT53 (1ull << 52) -#define MD_BIT54 (1ull << 53) -#define MD_BIT55 (1ull << 54) -#define MD_BIT56 (1ull << 55) -#define MD_BIT57 (1ull << 56) -#define MD_BIT58 (1ull << 57) -#define MD_BIT59 (1ull << 58) -#define MD_BIT60 (1ull << 59) -#define MD_BIT61 (1ull << 60) -#define MD_BIT62 (1ull << 61) -#define MD_BIT63 (1ull << 62) -#define MD_BIT64 (1ull << 63) +#define BIT33 (1ull << 32) +#define BIT34 (1ull << 33) +#define BIT35 (1ull << 34) +#define BIT36 (1ull << 35) +#define BIT37 (1ull << 36) +#define BIT38 (1ull << 37) +#define BIT39 (1ull << 38) +#define BIT40 (1ull << 39) +#define BIT41 (1ull << 40) +#define BIT42 (1ull << 41) +#define BIT43 (1ull << 42) +#define BIT44 (1ull << 43) +#define BIT45 (1ull << 44) +#define BIT46 (1ull << 45) +#define BIT47 (1ull << 46) +#define BIT48 (1ull << 47) +#define BIT49 (1ull << 48) +#define BIT50 (1ull << 49) +#define BIT51 (1ull << 50) +#define BIT52 (1ull << 51) +#define BIT53 (1ull << 52) +#define BIT54 (1ull << 53) +#define BIT55 (1ull << 54) +#define BIT56 (1ull << 55) +#define BIT57 (1ull << 56) +#define BIT58 (1ull << 57) +#define BIT59 (1ull << 58) +#define BIT60 (1ull << 59) +#define BIT61 (1ull << 60) +#define BIT62 (1ull << 61) +#define BIT63 (1ull << 62) +#define BIT64 (1ull << 63) diff --git a/code/base/debug.c b/code/base/debug.c new file mode 100644 index 0000000..5de2530 --- /dev/null +++ b/code/base/debug.c @@ -0,0 +1,25 @@ +#ifdef INTELLISENSE_DIRECTIVES +# include "strings.h" +# include "debug.h" +#endif + +void assert_handler( char const* condition, char const* file, char const* function, S32 line, char const* msg, ... ) +{ + // TODO(Ed): Change this to metadesks's procs + _printf_err( "%s - %s:(%d): Assert Failure: ", file, function, line ); + + if ( condition ) + // TODO(Ed): Change this to metadesks's procs + _printf_err( "`%s` \n", condition ); + + if ( msg ) + { + va_list va; + va_start( va, msg ); + _printf_err_va( msg, va ); + va_end( va ); + } + + // TODO(Ed): Change this to metadesks's procs + _printf_err( "%s", "\n" ); +} diff --git a/code/base/debug.h b/code/base/debug.h index 811e923..e7a15de 100644 --- a/code/base/debug.h +++ b/code/base/debug.h @@ -1,6 +1,7 @@ #ifdef INTELLISENSE_DIRECTIVES # pragma once # include "context_cracking.h" +# include "base_types.h" # include "macros.h" #endif @@ -9,7 +10,11 @@ #ifndef trap # if COMPILER_MSVC -# define trap() __debugbreak() +# if _MSC_VER < 1300 +# define GEN_DEBUG_TRAP() __asm int 3 /* Trap to debugger! */ +# else +# define GEN_DEBUG_TRAP() __debugbreak() +# endif # elif COMPILER_CLANG || COMPILER_GCC # define trap() __builtin_trap() # else @@ -17,6 +22,16 @@ # endif #endif +#define assert_msg( cond, msg, ... ) \ + do \ + { \ + if ( ! ( cond ) ) \ + { \ + assert_handler( #cond, __FILE__, __func__, scast( s64, __LINE__ ), msg, ##__VA_ARGS__ ); \ + GEN_DEBUG_TRAP(); \ + } \ + } while ( 0 ) + #ifndef assert_always #define assert_always(x) do { if ( !(x) ) { trap(); } } while(0) #endif @@ -41,3 +56,5 @@ #ifndef md_static_assert #define md_static_assert(C, ID) global U8 glue(ID, __LINE__)[ (C) ? 1 : -1 ] #endif + +MD_API void assert_handler( char const* condition, char const* file, char const* function, S32 line, char const* msg, ... ); diff --git a/code/base/memory_substrate.c b/code/base/memory_substrate.c index 0f8b93b..d6a3967 100644 --- a/code/base/memory_substrate.c +++ b/code/base/memory_substrate.c @@ -99,7 +99,7 @@ void* heap_allocator_proc( void* allocator_data, AllocatorMode mode, SSIZE size, _aligned_free( old_memory ); break; - case AllocatorMode_Reisze: + case AllocatorMode_Resize: { AllocatorInfo a = heap(); ptr = default_resize_align( a, old_memory, old_size, size, alignment ); @@ -164,7 +164,7 @@ void* heap_allocator_proc( void* allocator_data, AllocatorMode mode, SSIZE size, case AllocatorMode_QuerySupport: return (void*) ( - AllocatorQuery_Alloc | AllocatorQuery_Free | AllocatorQuery_Reisze + AllocatorQuery_Alloc | AllocatorQuery_Free | AllocatorQuery_Resize ); } @@ -220,6 +220,7 @@ VArena varena__alloc(VArenaParams params) #endif SPTR header_size = size_of(VArena); + asan_unpoison_memory_region(base, header_size); VArena* vm = (VArena* ) base; vm->reserve = reserve_size; @@ -227,20 +228,69 @@ VArena varena__alloc(VArenaParams params) vm->reserve_start = (SPTR)base + header_size; vm->flags = params.flags; vm->commit_used = 0; - asan_unpoison_memory_region(vm, header_size); } -void Varena_release(VArena* arena) +void varena_release(VArena* arena) { - - + os_release(arena, arena->reserve); arena = nullptr; } void* varena_allocator_proc(void* allocator_data, AllocatorMode mode, SSIZE size, SSIZE alignment, void* old_memory, SSIZE old_size, U64 flags) { - void* result = nullptr; + OS_SystemInfo const* info = os_get_system_info(); + SPTR requested_size = size; + + void* result = nullptr; + switch (mode) + { + case AllocatorMode_Alloc: + { + if (requested_size == 0) + assert(requested_size != 0); + return result; + + + } + break; + + case AllocatorMode_Free: + { + + } + break; + + case AllocatorMode_FreeAll: + { + + } + break; + + case AllocatorMode_Resize: + { + + } + break; + + case AllocatorMode_QueryType: + { + + } + break; + + case AllocatorMode_QuerySupport: + { + + } + break; + } + return result; +} + +void* farena_allocator_proc(void* allocator_data, AllocatorMode mode, SSIZE size, SSIZE alignment, void* old_memory, SSIZE old_size, U64 flags) +{ + void* result = nullptr; switch (mode) { case AllocatorMode_Alloc: @@ -253,7 +303,7 @@ void* varena_allocator_proc(void* allocator_data, AllocatorMode mode, SSIZE size case AllocatorMode_FreeAll: break; - case AllocatorMode_Reisze: + case AllocatorMode_Resize: break; case AllocatorMode_QueryType: @@ -262,9 +312,5 @@ void* varena_allocator_proc(void* allocator_data, AllocatorMode mode, SSIZE size case AllocatorMode_QuerySupport: break; } - - - return result; } - diff --git a/code/base/memory_substrate.h b/code/base/memory_substrate.h index 654c015..f9a1bd4 100644 --- a/code/base/memory_substrate.h +++ b/code/base/memory_substrate.h @@ -35,7 +35,7 @@ enum AllocatorMode AllocatorMode_Alloc, AllocatorMode_Free, AllocatorMode_FreeAll, - AllocatorMode_Reisze, + AllocatorMode_Resize, AllocatorMode_QueryType, AllocatorMode_QuerySupport, }; @@ -45,7 +45,7 @@ enum AllocatorQuery_Alloc = (1 << 0), AllocatorQuery_Free = (1 << 1), AllocatorQuery_FreeAll = (1 << 2), - AllocatorQuery_Reisze = (1 << 3), + AllocatorQuery_Resize = (1 << 3), }; typedef void*(AllocatorProc)( void* allocator_data, AllocatorMode type, SSIZE size, SSIZE alignment, void* old_memory, SSIZE old_size, U64 flags ); @@ -125,17 +125,17 @@ MD_API void heap_stats_check( void ); MD_API void* heap_allocator_proc( void* allocator_data, AllocatorMode mode, SSIZE size, SSIZE alignment, void* old_memory, SSIZE old_size, U64 flags ); #ifndef heap -//! The heap allocator backed by operating system's memory manager. +// The heap allocator backed by the platform vendor's malloc & free. #define heap() (AllocatorInfo){ heap_allocator_proc, nullptr } #endif #ifndef md_malloc -//! Helper to allocate memory using heap allocator. +// Helper to allocate memory using heap allocator. #define md_malloc( sz ) alloc( heap(), sz ) #endif #ifndef md_free -//! Helper to free memory allocated by heap allocator. +// Helper to free memory allocated by heap allocator. #define md_free( ptr ) alloc_free( heap(), ptr ) #endif @@ -186,11 +186,11 @@ AllocatorInfo vm_allocator(VArena* vm) { return info; } -VArena* varena__alloc(VArenaParams params PARAM_DEFAULT); +MD_API VArena* varena__alloc(VArenaParams params PARAM_DEFAULT); #define varena_alloc(...) varena__alloc( (VArenaParams){__VA_ARGS__} ) -void varena_commit (VArena vm, SSIZE commit_size); -VArenaParams varena_release (VArena vm); +MD_API void varena_commit (VArena vm, SSIZE commit_size); +MD_API VArenaParams varena_release (VArena vm); MD_API void* varena_allocator_proc(void* allocator_data, AllocatorMode mode, SSIZE size, SSIZE alignment, void* old_memory, SSIZE old_size, U64 flags); @@ -227,7 +227,7 @@ AllocatorQueryFlags allocator_query_support(AllocatorInfo a) { inline void* alloc_align( AllocatorInfo a, SSIZE size, SSIZE alignment ) { - return a.proc( a.data, AllocatorMode_ALLOC, size, alignment, nullptr, 0, MD_DEFAULT_ALLOCATOR_FLAGS ); + return a.proc( a.data, AllocatorMode_Alloc, size, alignment, nullptr, 0, MD_DEFAULT_ALLOCATOR_FLAGS ); } inline @@ -236,14 +236,14 @@ void* alloc( AllocatorInfo a, SSIZE size ) { } inline -void allocator_free( AllocatorInfo a, void* ptr ) { +void alloc_free( AllocatorInfo a, void* ptr ) { if ( ptr != nullptr ) - a.proc( a.data, AllocatorMode_FREE, 0, 0, ptr, 0, MD_DEFAULT_ALLOCATOR_FLAGS ); + a.proc( a.data, AllocatorMode_Free, 0, 0, ptr, 0, MD_DEFAULT_ALLOCATOR_FLAGS ); } inline void free_all( AllocatorInfo a ) { - a.proc( a.data, AllocatorMode_FREE_ALL, 0, 0, nullptr, 0, MD_DEFAULT_ALLOCATOR_FLAGS ); + a.proc( a.data, AllocatorMode_FreeAll, 0, 0, nullptr, 0, MD_DEFAULT_ALLOCATOR_FLAGS ); } inline @@ -253,7 +253,7 @@ void* resize( AllocatorInfo a, void* ptr, SSIZE old_size, SSIZE new_size ) { inline void* resize_align( AllocatorInfo a, void* ptr, SSIZE old_size, SSIZE new_size, SSIZE alignment ) { - return a.proc( a.data, AllocatorMode_RESIZE, new_size, alignment, ptr, old_size, MD_DEFAULT_ALLOCATOR_FLAGS ); + return a.proc( a.data, AllocatorMode_Resize, new_size, alignment, ptr, old_size, MD_DEFAULT_ALLOCATOR_FLAGS ); } inline diff --git a/code/base/strings.h b/code/base/strings.h index 9466da3..5950638 100644 --- a/code/base/strings.h +++ b/code/base/strings.h @@ -5,12 +5,14 @@ # include "platform.h" # include "macros.h" # include "base_types.h" -# include "constants.h" -# include "math.h" -# include "space.h" -# include "thread_context.h" # include "memory.h" +# include "memory_substrate.h" # include "arena.h" +# include "constants.h" +# include "space.h" +# include "math.h" +# include "thread_context.h" +# include "toolchain.h" #endif // Copyright (c) 2024 Epic Games Tools @@ -268,7 +270,13 @@ internal String8List str8_list_copy (Arena *arena, String8 //////////////////////////////// //~ rjf: String Splitting & Joining -internal String8List str8_split (Arena *arena, String8 string, U8 *split_chars, U64 split_char_count, StringSplitFlags flags); +String8List str8_split_arena(Arena *arena, String8 string, U8 *split_chars, U64 split_char_count, StringSplitFlags flags); + +// Example pattern for general allocator variants + +// String8List str8__split_ainfo(String8 string, U8* split_chars, U64 split_char_count, StringSplitFlags flags, AllocatorInfo allocator); +// #define str8_split_ainfo(string, split_chars, split_char_count, flags, ...) str8__split_ainfo(string, split_chars, split_char_count, flags, (AllocatorInfo){__VA_ARGS__}); + internal String8List str8_split_by_string_chars (Arena *arena, String8 string, String8 split_chars, StringSplitFlags flags); internal String8List str8_list_split_by_string_chars(Arena *arena, String8List list, String8 split_chars, StringSplitFlags flags); internal String8 str8_list_join (Arena *arena, String8List *list, StringJoin *optional_params); @@ -376,5 +384,3 @@ internal U64 str8_deserial_read_block (String8 string, U64 of #define str8_deserial_read_array(string, off, ptr, count) str8_deserial_read((string), (off), (ptr), sizeof(*(ptr)) * (count), sizeof( *(ptr))) #define str8_deserial_read_struct(string, off, ptr) str8_deserial_read((string), (off), (ptr), sizeof(*(ptr)), sizeof( *(ptr))) - -#endif // BASE_STRINGS_H