remove scratch.h

This commit is contained in:
jacob 2025-07-03 12:29:09 -05:00
parent 6841ff9784
commit 05ce4f32a1
28 changed files with 68 additions and 258 deletions

View File

@ -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"

View File

@ -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

View File

@ -6,7 +6,6 @@
#include "ase.h"
#include "arena.h"
#include "scratch.h"
#include "bitbuff.h"
#include "string.h"
#include "log.h"

View File

@ -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"

View File

@ -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)
{

View File

@ -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

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -3,7 +3,6 @@
#include "arena.h"
#include "atomic.h"
#include "string.h"
#include "scratch.h"
#include "app.h"
#include "intrinsics.h"

View File

@ -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):

View File

@ -1,4 +1,4 @@
#include "scratch.h"
#include "arena.h"
#include "log.h"
#include "string.h"
#include "atomic.h"

View File

@ -1,6 +1,5 @@
#include "mixer.h"
#include "arena.h"
#include "scratch.h"
#include "sound.h"
#include "sys.h"
#include "math.h"

View File

@ -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"

View File

@ -6,7 +6,7 @@
* ========================== */
#include "playback.h"
#include "scratch.h"
#include "arena.h"
#include "sys.h"
#include "mixer.h"
#include "atomic.h"

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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"

View File

@ -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

View File

@ -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"

View File

@ -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"

View File

@ -1,7 +1,6 @@
#include "string.h"
#include "arena.h"
#include "memory.h"
#include "scratch.h"
#include "math.h"
#include "uni.h"

View File

@ -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"

View File

@ -4,7 +4,6 @@
extern "C"
{
#include "ttf.h"
#include "scratch.h"
#include "util.h"
#include "string.h"
#include "memory.h"

View File

@ -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"

View File

@ -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 :( */