From 05ce4f32a1acfc07ab8412b36ced179138e24b6f Mon Sep 17 00:00:00 2001 From: jacob Date: Thu, 3 Jul 2025 12:29:09 -0500 Subject: [PATCH] remove scratch.h --- src/app.c | 1 - src/arena.h | 57 ++++++++++++ src/ase.c | 1 - src/asset_cache.c | 1 - src/bitbuff.c | 1 - src/collider.c | 1 - src/draw.c | 2 +- src/font.c | 1 - src/gp_dx12.c | 2 +- src/host.c | 3 +- src/job.c | 1 - src/json.c | 2 +- src/log.c | 2 +- src/mixer.c | 1 - src/phys.c | 2 +- src/playback_wasapi.c | 2 +- src/scratch.c | 31 ------- src/scratch.h | 197 ------------------------------------------ src/settings.c | 3 +- src/sim_step.c | 2 +- src/sock_win32.c | 4 +- src/sound.c | 1 - src/sprite.c | 1 - src/string.c | 1 - src/sys_win32.c | 3 +- src/ttf_dwrite.cpp | 1 - src/user.c | 1 - src/util.h | 1 - 28 files changed, 68 insertions(+), 258 deletions(-) delete mode 100644 src/scratch.c delete mode 100644 src/scratch.h diff --git a/src/app.c b/src/app.c index e234c49e..aa7ebca9 100644 --- a/src/app.c +++ b/src/app.c @@ -1,7 +1,6 @@ #include "app.h" #include "arena.h" #include "string.h" -#include "scratch.h" #include "sys.h" #include "job.h" #include "user.h" diff --git a/src/arena.h b/src/arena.h index d5faa7d4..43a44eaa 100644 --- a/src/arena.h +++ b/src/arena.h @@ -1,6 +1,7 @@ #ifndef ARENA_H #define ARENA_H +#include "sys.h" #include "memory.h" #define ARENA_HEADER_SIZE 256 @@ -123,4 +124,60 @@ INLINE void *_arena_push_dry(struct arena *arena, u64 align) return ptr; } +/* ========================== * + * Scratch + * ========================== */ + + /* Any parameterized arenas in the caller's scope should be passed into this + * function as a potential "conflict". This is to prevent friction in case the + * passed arena is itself a scratch arena from another scope (since + * parameterized arenas are often used to allocate persistent results for the + * caller). + * + * Use `scratch_begin_no_conflict` instead if there is no arena in the current + * scope that could potentially be a scratch arena from another scope. */ +#define scratch_begin(potential_conflict) _scratch_begin(potential_conflict) + +INLINE struct arena_temp _scratch_begin(struct arena *potential_conflict) +{ + /* This function is currently hard-coded to support 2 scratch arenas */ + CT_ASSERT(SYS_SCRATCH_ARENAS_PER_FIBER == 2); + + /* Use `scratch_begin_no_conflict` if no conflicts are present */ + ASSERT(potential_conflict != NULL); + + struct sys_scratch_ctx *ctx = sys_scratch_ctx_from_fiber_id(sys_current_fiber_id()); + struct arena *scratch_arena = ctx->arenas[0]; + if (potential_conflict && scratch_arena == potential_conflict) { + scratch_arena = ctx->arenas[1]; + } + struct arena_temp temp = arena_temp_begin(scratch_arena); + return temp; +} + +/* This macro declares an unused "arena" variable that will error if an existing "arena" + * variable is present (due to shadowing). This is for catching obvious cases of + * `scratch_begin_no_conflict` getting called when an `arena` variable already + * exists in the caller's scope (`scratch_begin(arena)` should be called + * instead). */ +#define scratch_begin_no_conflict() \ + _scratch_begin_no_conflict(); \ + do { \ + u8 arena = 0; \ + (UNUSED)arena; \ + } while (0) + +INLINE struct arena_temp _scratch_begin_no_conflict(void) +{ + struct sys_scratch_ctx *ctx = sys_scratch_ctx_from_fiber_id(sys_current_fiber_id()); + struct arena *scratch_arena = ctx->arenas[0]; + struct arena_temp temp = arena_temp_begin(scratch_arena); + return temp; +} + +INLINE void scratch_end(struct arena_temp scratch_temp) +{ + arena_temp_end(scratch_temp); +} + #endif diff --git a/src/ase.c b/src/ase.c index b5cccafc..3c98a275 100644 --- a/src/ase.c +++ b/src/ase.c @@ -6,7 +6,6 @@ #include "ase.h" #include "arena.h" -#include "scratch.h" #include "bitbuff.h" #include "string.h" #include "log.h" diff --git a/src/asset_cache.c b/src/asset_cache.c index 06065382..2e9498e8 100644 --- a/src/asset_cache.c +++ b/src/asset_cache.c @@ -3,7 +3,6 @@ #include "string.h" #include "memory.h" #include "arena.h" -#include "scratch.h" #include "util.h" #include "log.h" #include "job.h" diff --git a/src/bitbuff.c b/src/bitbuff.c index f60b5e34..7e7d4dca 100644 --- a/src/bitbuff.c +++ b/src/bitbuff.c @@ -683,7 +683,6 @@ void br_read_dbg_marker(struct bitbuff_reader *br, struct string name) #if BITBUFF_TEST #include "string.h" -#include "scratch.h" void bitbuff_test(void) { diff --git a/src/collider.c b/src/collider.c index 9bf65082..d0ad00ac 100644 --- a/src/collider.c +++ b/src/collider.c @@ -1,7 +1,6 @@ #include "collider.h" #include "math.h" #include "arena.h" -#include "scratch.h" /* How close can non-overlapping shapes be before collision is considered */ #define COLLISION_TOLERANCE 0.005f diff --git a/src/draw.c b/src/draw.c index 49dee0ed..5672e3a8 100644 --- a/src/draw.c +++ b/src/draw.c @@ -1,8 +1,8 @@ #include "draw.h" +#include "arena.h" #include "gp.h" #include "math.h" #include "font.h" -#include "scratch.h" #include "sprite.h" #include "collider.h" diff --git a/src/font.c b/src/font.c index 73426c3a..b7f07587 100644 --- a/src/font.c +++ b/src/font.c @@ -2,7 +2,6 @@ #include "arena.h" #include "ttf.h" #include "job.h" -#include "scratch.h" #include "asset_cache.h" #include "resource.h" #include "log.h" diff --git a/src/gp_dx12.c b/src/gp_dx12.c index 310325bd..0241068c 100644 --- a/src/gp_dx12.c +++ b/src/gp_dx12.c @@ -1,9 +1,9 @@ #include "gp.h" #include "sys.h" #include "arena.h" +#include "arena.h" #include "memory.h" #include "string.h" -#include "scratch.h" #include "app.h" #include "job.h" #include "log.h" diff --git a/src/host.c b/src/host.c index d4d09bfc..e7e80626 100644 --- a/src/host.c +++ b/src/host.c @@ -1,6 +1,5 @@ -#include "arena.h" #include "host.h" -#include "scratch.h" +#include "arena.h" #include "bitbuff.h" #include "sys.h" #include "util.h" diff --git a/src/job.c b/src/job.c index 6d3762b0..9a6559df 100644 --- a/src/job.c +++ b/src/job.c @@ -3,7 +3,6 @@ #include "arena.h" #include "atomic.h" #include "string.h" -#include "scratch.h" #include "app.h" #include "intrinsics.h" diff --git a/src/json.c b/src/json.c index f6860bbd..874d0349 100644 --- a/src/json.c +++ b/src/json.c @@ -1,7 +1,7 @@ #include "json.h" +#include "arena.h" #include "string.h" #include "arena.h" -#include "scratch.h" #include "math.h" /* TODO (if we want to be JSON standard compliant): diff --git a/src/log.c b/src/log.c index a0d935bb..70d20e2f 100644 --- a/src/log.c +++ b/src/log.c @@ -1,4 +1,4 @@ -#include "scratch.h" +#include "arena.h" #include "log.h" #include "string.h" #include "atomic.h" diff --git a/src/mixer.c b/src/mixer.c index 11e39faf..f1de21d7 100644 --- a/src/mixer.c +++ b/src/mixer.c @@ -1,6 +1,5 @@ #include "mixer.h" #include "arena.h" -#include "scratch.h" #include "sound.h" #include "sys.h" #include "math.h" diff --git a/src/phys.c b/src/phys.c index 7c0cadc7..06cb5a5b 100644 --- a/src/phys.c +++ b/src/phys.c @@ -1,8 +1,8 @@ #include "phys.h" +#include "arena.h" #include "sim_ent.h" #include "sim_step.h" #include "math.h" -#include "scratch.h" #include "space.h" #include "uid.h" diff --git a/src/playback_wasapi.c b/src/playback_wasapi.c index 9cf12b34..522c2035 100644 --- a/src/playback_wasapi.c +++ b/src/playback_wasapi.c @@ -6,7 +6,7 @@ * ========================== */ #include "playback.h" -#include "scratch.h" +#include "arena.h" #include "sys.h" #include "mixer.h" #include "atomic.h" diff --git a/src/scratch.c b/src/scratch.c deleted file mode 100644 index df145ae4..00000000 --- a/src/scratch.c +++ /dev/null @@ -1,31 +0,0 @@ -#if 0 -#include "scratch.h" - -INTERNAL THREAD_LOCAL_VAR_ALLOC_FUNC_DEF(scratch_context_alloc, vctx) -{ - __prof; - struct scratch_ctx *ctx = vctx; - for (u32 i = 0; i < countof(ctx->arenas); ++i) { - ctx->arenas[i] = arena_alloc(SCRATCH_ARENA_RESERVE); - } -} - -INTERNAL THREAD_LOCAL_VAR_RELEASE_FUNC_DEF(scratch_context_release, vctx) -{ - __prof; - struct scratch_ctx *ctx = vctx; - -#if RTC - /* If stack count is not 0, then a `scratch_end` is missing on a top-level - * scratch arena (The arena_temp with - * `scratch_id` = ctx->scratch_id_stack[ctx->scratch_id_stack_count - 1]) */ - ASSERT(ctx->scratch_id_stack_count == 0); -#endif - - for (u32 i = 0; i < countof(ctx->arenas); ++i) { - arena_release(ctx->arenas[i]); - } -} - -THREAD_LOCAL_VAR_DEF_EXTERN(tl_scratch_ctx, struct scratch_ctx, &scratch_context_alloc, &scratch_context_release); -#endif diff --git a/src/scratch.h b/src/scratch.h deleted file mode 100644 index 785ac96f..00000000 --- a/src/scratch.h +++ /dev/null @@ -1,197 +0,0 @@ -#if 1 - -#ifndef SCRATCH_H -#define SCRATCH_H - -#include "arena.h" -#include "sys.h" - -/* ========================== * - * Scratch begin - * ========================== */ - -/* Any parameterized arenas in the caller's scope should be passed into this - * function as a potential "conflict". This is to prevent friction in case the - * passed arena is itself a scratch arena from another scope (since - * parameterized arenas are often used to allocate persistent results for the - * caller). - * - * Use `scratch_begin_no_conflict` instead if there is no arena in the current - * scope that could potentially be a scratch arena from another scope. */ -#define scratch_begin(potential_conflict) _scratch_begin(potential_conflict) - -INLINE struct arena_temp _scratch_begin(struct arena *potential_conflict) -{ - /* This function is currently hard-coded to support 2 scratch arenas */ - CT_ASSERT(SYS_SCRATCH_ARENAS_PER_FIBER == 2); - - /* Use `scratch_begin_no_conflict` if no conflicts are present */ - ASSERT(potential_conflict != NULL); - - struct sys_scratch_ctx *ctx = sys_scratch_ctx_from_fiber_id(sys_current_fiber_id()); - struct arena *scratch_arena = ctx->arenas[0]; - if (potential_conflict && scratch_arena == potential_conflict) { - scratch_arena = ctx->arenas[1]; - } - struct arena_temp temp = arena_temp_begin(scratch_arena); - return temp; -} - -/* This macro declares an unused "arena" variable that will error if an existing "arena" - * variable is present (due to shadowing). This is for catching obvious cases of - * `scratch_begin_no_conflict` getting called when an `arena` variable already - * exists in the caller's scope (`scratch_begin(arena)` should be called - * instead). */ -#define scratch_begin_no_conflict() \ - _scratch_begin_no_conflict(); \ - do { \ - u8 arena = 0; \ - (UNUSED)arena; \ - } while (0) - -INLINE struct arena_temp _scratch_begin_no_conflict(void) -{ - struct sys_scratch_ctx *ctx = sys_scratch_ctx_from_fiber_id(sys_current_fiber_id()); - struct arena *scratch_arena = ctx->arenas[0]; - struct arena_temp temp = arena_temp_begin(scratch_arena); - return temp; -} - -/* ========================== * - * Scratch end - * ========================== */ - -INLINE void scratch_end(struct arena_temp scratch_temp) -{ - arena_temp_end(scratch_temp); -} - -#endif - - - - - - -#else - - - - -#ifndef SCRATCH_H -#define SCRATCH_H - -#include "arena.h" -#include "sys.h" -#include "thread_local.h" - -#define SYS_SCRATCH_ARENAS_PER_FIBER 2 -#define SCRATCH_ARENA_RESERVE (GIGABYTE(64)) - -/* ========================== * - * Thread local state - * ========================== */ - -struct scratch_ctx { - struct arena *arenas[SYS_SCRATCH_ARENAS_PER_FIBER]; - -#if RTC - u64 next_scratch_id; - u64 scratch_id_stack[16384]; - u64 scratch_id_stack_count; -#endif -}; - -THREAD_LOCAL_VAR_DECL_EXTERN(tl_scratch_ctx, struct scratch_ctx); - -/* ========================== * - * Scratch begin - * ========================== */ - -INLINE void scratch_dbg_push(struct scratch_ctx *ctx, struct arena_temp *temp) -{ -#if RTC - if (ctx->scratch_id_stack_count >= countof(ctx->scratch_id_stack)) { - sys_panic(LIT("Max debug scratch depth reached")); - } - temp->scratch_id = ctx->next_scratch_id++; - ctx->scratch_id_stack[ctx->scratch_id_stack_count++] = temp->scratch_id; -#else - (UNUSED)ctx; - (UNUSED)temp; -#endif -} - -/* Any parameterized arenas in the caller's scope should be passed into this - * function as a potential "conflict". This is to prevent friction in case the - * passed arena is itself a scratch arena from another scope (since - * parameterized arenas are often used to allocate persistent results for the - * caller). - * - * Use `scratch_begin_no_conflict` instead if there is no arena in the current - * scope that could potentially be a scratch arena from another scope. */ -#define scratch_begin(potential_conflict) _scratch_begin(potential_conflict) - -INLINE struct arena_temp _scratch_begin(struct arena *potential_conflict) -{ - /* This function is currently hard-coded to support 2 scratch arenas */ - CT_ASSERT(SYS_SCRATCH_ARENAS_PER_FIBER == 2); - - /* Use `scratch_begin_no_conflict` if no conflicts are present */ - ASSERT(potential_conflict != NULL); - - struct scratch_ctx *ctx = (struct scratch_ctx *)thread_local_var_fetch(&tl_scratch_ctx); - struct arena *scratch_arena = ctx->arenas[0]; - if (potential_conflict && scratch_arena == potential_conflict) { - scratch_arena = ctx->arenas[1]; - } - struct arena_temp temp = arena_temp_begin(scratch_arena); - scratch_dbg_push(ctx, &temp); - return temp; -} - -/* This macro declares an unused "arena" variable that will error if an existing "arena" - * variable is present (due to shadowing). This is for catching obvious cases of - * `scratch_begin_no_conflict` getting called when an `arena` variable already - * exists in the caller's scope (`scratch_begin(arena)` should be called - * instead). */ -#define scratch_begin_no_conflict() \ - _scratch_begin_no_conflict(); \ - do { \ - u8 arena = 0; \ - (UNUSED)arena; \ - } while (0) - -INLINE struct arena_temp _scratch_begin_no_conflict(void) -{ - struct scratch_ctx *ctx = (struct scratch_ctx *)thread_local_var_fetch(&tl_scratch_ctx); - struct arena *scratch_arena = ctx->arenas[0]; - struct arena_temp temp = arena_temp_begin(scratch_arena); - scratch_dbg_push(ctx, &temp); - return temp; -} - -/* ========================== * - * Scratch end - * ========================== */ - -INLINE void scratch_end(struct arena_temp scratch_temp) -{ -#if RTC - struct scratch_ctx *ctx = (struct scratch_ctx *)thread_local_var_fetch(&tl_scratch_ctx); - if (ctx->scratch_id_stack_count > 0) { - u64 scratch_id = scratch_temp.scratch_id; - u64 expected_id = ctx->scratch_id_stack[--ctx->scratch_id_stack_count]; - /* This assertion exists to catch cases where a scratch_end was forgotten. - * It will fail if a scratch arena is reset out of order. - * E.g. there is a missing scratch_end somewhere on a different scratch - * arena (one that was created between the scratch_begin & the - * scratch_end of the arena being reset here). */ - ASSERT(scratch_id == expected_id); - } -#endif - arena_temp_end(scratch_temp); -} - -#endif -#endif diff --git a/src/settings.c b/src/settings.c index 799d0211..59133ba7 100644 --- a/src/settings.c +++ b/src/settings.c @@ -1,8 +1,7 @@ #include "settings.h" +#include "arena.h" #include "json.h" #include "string.h" -#include "arena.h" -#include "scratch.h" #include "math.h" struct string settings_serialize(struct arena *arena, const struct sys_window_settings *settings) diff --git a/src/sim_step.c b/src/sim_step.c index 615923f7..485321df 100644 --- a/src/sim_step.c +++ b/src/sim_step.c @@ -2,10 +2,10 @@ #include "sim_ent.h" #include "sim.h" #include "sys.h" +#include "arena.h" #include "util.h" #include "sprite.h" #include "math.h" -#include "scratch.h" #include "atomic.h" #include "app.h" #include "log.h" diff --git a/src/sock_win32.c b/src/sock_win32.c index ed0a8eb3..9f1d0faf 100644 --- a/src/sock_win32.c +++ b/src/sock_win32.c @@ -1,10 +1,8 @@ #include "sock.h" #include "sys.h" -#include "log.h" #include "arena.h" -#include "scratch.h" -#include "string.h" #include "log.h" +#include "string.h" #include "gstat.h" #define WIN32_LEAN_AND_MEAN diff --git a/src/sound.c b/src/sound.c index e98df42e..f5075812 100644 --- a/src/sound.c +++ b/src/sound.c @@ -2,7 +2,6 @@ #include "arena.h" #include "log.h" #include "sys.h" -#include "scratch.h" #include "resource.h" #include "asset_cache.h" #include "mp3.h" diff --git a/src/sprite.c b/src/sprite.c index ce28f73a..0fbe105f 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -2,7 +2,6 @@ #include "arena.h" #include "log.h" #include "sys.h" -#include "scratch.h" #include "resource.h" #include "ase.h" #include "util.h" diff --git a/src/string.c b/src/string.c index 69666198..71b8e022 100644 --- a/src/string.c +++ b/src/string.c @@ -1,7 +1,6 @@ #include "string.h" #include "arena.h" #include "memory.h" -#include "scratch.h" #include "math.h" #include "uni.h" diff --git a/src/sys_win32.c b/src/sys_win32.c index 66871cfc..a2aaadd7 100644 --- a/src/sys_win32.c +++ b/src/sys_win32.c @@ -2,10 +2,9 @@ #include "sys.h" #include "memory.h" +#include "arena.h" #include "app.h" #include "string.h" -#include "arena.h" -#include "scratch.h" #include "atomic.h" #include "log.h" #include "math.h" diff --git a/src/ttf_dwrite.cpp b/src/ttf_dwrite.cpp index b879c874..de016ca8 100644 --- a/src/ttf_dwrite.cpp +++ b/src/ttf_dwrite.cpp @@ -4,7 +4,6 @@ extern "C" { #include "ttf.h" -#include "scratch.h" #include "util.h" #include "string.h" #include "memory.h" diff --git a/src/user.c b/src/user.c index debca6d0..f494eada 100644 --- a/src/user.c +++ b/src/user.c @@ -9,7 +9,6 @@ #include "intrinsics.h" #include "asset_cache.h" #include "string.h" -#include "scratch.h" #include "math.h" #include "sys.h" #include "mixer.h" diff --git a/src/util.h b/src/util.h index 51a25376..f4e464ed 100644 --- a/src/util.h +++ b/src/util.h @@ -7,7 +7,6 @@ #include "arena.h" #include "atomic.h" #include "math.h" -#include "scratch.h" /* Utility functions and stuff that don't have a home :( */