fix entities arena not growing

This commit is contained in:
jacob 2024-03-11 12:16:34 -05:00
parent 6cdcb60784
commit 76c609f8d5
11 changed files with 45 additions and 35 deletions

View File

@ -50,7 +50,6 @@ void arena_release(struct arena *arena)
/* NOTE: Application will exit if arena fails to commit memory */
void *_arena_push_bytes(struct arena *arena, u64 size, u64 align)
{
__prof;
ASSERT(align > 0);
void *start = NULL;
@ -62,6 +61,7 @@ void *_arena_push_bytes(struct arena *arena, u64 size, u64 align)
u64 new_pos = aligned_start_pos + size;
if (new_pos > arena->committed) {
__profscope(_arena_push_bytes_COMMIT);
/* Commit new block(s) */
u64 blocks_needed = (new_pos - arena->committed + ARENA_BLOCK_SIZE - 1) / ARENA_BLOCK_SIZE;
u64 commit_bytes = blocks_needed * ARENA_BLOCK_SIZE;
@ -95,17 +95,14 @@ void *_arena_push_bytes(struct arena *arena, u64 size, u64 align)
void arena_copy_replace(struct arena *dest, struct arena *src)
{
arena_reset(dest);
if (dest->committed < src->committed) {
u64 diff = src->committed - dest->committed;
_arena_push_bytes(dest, diff, 1);
}
MEMCPY(dest->base, src->base, src->committed);
u64 data_size = src->pos;
u8 *data_src = src->base;
u8 *data_dest = _arena_push_bytes(dest, data_size, 1);
MEMCPY(data_dest, data_src, data_size);
}
void arena_decommit_unused_blocks(struct arena *arena)
{
__prof;
u64 next_block_pos = ARENA_BLOCK_SIZE * ((arena->pos + (ARENA_BLOCK_SIZE - 1)) / ARENA_BLOCK_SIZE);
if (arena->committed > next_block_pos) {
u8 *decommit_start = arena->base + next_block_pos;

View File

@ -34,18 +34,13 @@ void arena_decommit_unused_blocks(struct arena *arena);
INLINE void *_arena_push_bytes_zero(struct arena *arena, u64 size, u64 align)
{
__prof;
void *p = _arena_push_bytes(arena, size, align);
{
__profscope(_arena_push_bytes_zero_MEMZERO);
MEMZERO(p, size);
}
return p;
}
INLINE void arena_pop_to(struct arena *arena, u64 pos)
{
__prof;
ASSERT(arena->pos >= pos);
ASAN_POISON(arena->base + pos, arena->pos - pos);
arena->pos = pos;
@ -53,7 +48,6 @@ INLINE void arena_pop_to(struct arena *arena, u64 pos)
INLINE struct temp_arena arena_push_temp(struct arena *arena)
{
__prof;
struct temp_arena t;
t.arena = arena;
t.start_pos = arena->pos;

View File

@ -109,8 +109,6 @@ u64 asset_cache_hash(struct string key)
* */
struct asset *asset_cache_touch(struct string key, u64 hash, b32 *is_first_touch)
{
__prof;
struct asset *asset = NULL;
if (is_first_touch) {
*is_first_touch = false;
@ -196,7 +194,6 @@ void asset_cache_set_work(struct asset *asset, struct work_handle *handle)
void asset_cache_wait(struct asset *asset)
{
__prof;
if (asset->status != ASSET_STATUS_READY) {
/* Wait for work to be set */
sync_flag_wait(&asset->work_ready_sf);
@ -228,7 +225,6 @@ void *asset_cache_get_store_data(struct asset *asset)
/* Asset store should be opened to allocate memory to the store arena */
struct asset_cache_store asset_cache_store_open(void)
{
__prof;
struct asset_cache_store store = {
.rw_mutex = &L.store_rw_mutex,
.arena = &L.store_arena
@ -239,6 +235,5 @@ struct asset_cache_store asset_cache_store_open(void)
void asset_cache_store_close(struct asset_cache_store *store)
{
__prof;
sys_rw_mutex_unlock_exclusive(store->rw_mutex);
}

View File

@ -208,7 +208,6 @@ struct font *font_load(struct string path, f32 point_size)
struct font_glyph *font_get_glyph(struct font *font, u8 c)
{
__prof;
struct font_glyph *g;
u16 index = font->lookup[c];
if (index < LOOKUP_TABLE_SIZE) {

View File

@ -68,6 +68,7 @@ INTERNAL struct game_cmd_array pop_cmds(struct arena *arena)
INTERNAL void publish_game_tick(void)
{
__prof;
sys_mutex_lock(&L.published_tick_mutex);
L.world.tick_published_ts = sys_timestamp();
world_copy_replace(&L.published_tick, &L.world);
@ -213,8 +214,8 @@ INTERNAL void game_update(void)
L.world.player_move_dir = V2(0, 0);
struct game_cmd_array game_cmds = pop_cmds(scratch.arena);
for (u64 i = 0; i < game_cmds.count; ++i) {
struct game_cmd cmd = game_cmds.cmds[i];
for (u64 cmd_index = 0; cmd_index < game_cmds.count; ++cmd_index) {
struct game_cmd cmd = game_cmds.cmds[cmd_index];
switch (cmd.kind) {
/* Movement */
@ -229,6 +230,16 @@ INTERNAL void game_update(void)
L.world.player_focus = cmd.pos;
} break;
/* Clear level */
case GAME_CMD_KIND_CLEAR_ALL: {
for (u64 i = 0; i < L.world.entities_count; ++i) {
struct entity *ent = &L.world.entities[i];
if (ent->valid) {
world_release_entity(&L.world, ent);
}
}
} break;
default: break;
};
}

View File

@ -7,9 +7,11 @@ enum game_cmd_kind {
GAME_CMD_KIND_NONE,
GAME_CMD_KIND_PLAYER_MOVE,
GAME_CMD_KIND_PLAYER_FOCUS,
/* Testing */
GAME_CMD_KIND_CLEAR_ALL,
GAME_CMD_KIND_COUNT
};

View File

@ -5,7 +5,6 @@
__attribute((section(".text.memcpy")))
void *memcpy(void *restrict dest, const void *restrict src, u64 n)
{
__prof;
/* TODO: Faster memcpy */
for (u64 i = 0; i < n; ++i) {
((u8 *)dest)[i] = ((u8 *)src)[i];
@ -16,7 +15,6 @@ void *memcpy(void *restrict dest, const void *restrict src, u64 n)
__attribute((section(".text.memset")))
void *memset(void *dest, int c, u64 n)
{
__prof;
/* TODO: Faster memset */
for (u64 i = 0; i < (n); ++i) {
((u8 *)dest)[i] = c;

View File

@ -659,8 +659,6 @@ void renderer_canvas_set_view(struct renderer_canvas *canvas, struct xform view)
u32 renderer_canvas_push_vertices(struct renderer_canvas *canvas, u8 **vertices_out, vidx **indices_out, u32 vertices_count, u32 indices_count)
{
__prof;
struct renderer_cmd *cmd = canvas->cpu_cmd_store.cmd_last;
if (!cmd) {
@ -692,8 +690,6 @@ u32 renderer_canvas_push_vertices(struct renderer_canvas *canvas, u8 **vertices_
void renderer_canvas_ensure_texture_cmd(struct renderer_canvas *canvas, struct texture_shader_parameters params)
{
__prof;
struct renderer_cmd *last_cmd = canvas->cpu_cmd_store.cmd_last;
if (!last_cmd || last_cmd->shader->kind != SHADER_TEXTURE || !handle_eq(last_cmd->texture, params.texture)) {
/* Command parameters are not the same, insert new command */

View File

@ -31,7 +31,7 @@ struct view {
};
struct bind_state {
b32 pressed; /* Is this bind held down this frame */
b32 is_held; /* Is this bind held down this frame */
u32 num_presses; /* How many times was this bind pressed since last frame */
};
@ -82,6 +82,7 @@ GLOBAL READONLY enum user_bind_kind g_binds[SYS_BTN_COUNT] = {
/* Testing */
[SYS_BTN_C] = USER_BIND_KIND_DEBUG_CLEAR,
[SYS_BTN_F6] = USER_BIND_KIND_DEBUG_DRAW,
[SYS_BTN_F7] = USER_BIND_KIND_DEBUG_CAMERA,
[SYS_BTN_MWHEELUP] = USER_BIND_KIND_ZOOM_IN,
@ -319,7 +320,7 @@ INTERNAL void user_update(void)
/* Reset bind states "was_pressed" */
for (u32 i = 0; i < ARRAY_COUNT(L.bind_states); ++i) {
L.bind_states[i] = (struct bind_state) {
.pressed = L.bind_states[i].pressed
.is_held = L.bind_states[i].is_held
};
}
@ -374,7 +375,7 @@ INTERNAL void user_update(void)
enum user_bind_kind bind = g_binds[button];
if (bind) {
b32 pressed = event->kind == SYS_EVENT_KIND_BUTTON_DOWN;
L.bind_states[bind].pressed = pressed;
L.bind_states[bind].is_held = pressed;
if (pressed) {
++L.bind_states[bind].num_presses;
}
@ -392,7 +393,7 @@ INTERNAL void user_update(void)
for (enum user_bind_kind bind = 0; bind < (i32)ARRAY_COUNT(L.bind_states); ++bind) {
struct bind_state state = L.bind_states[bind];
if (!state.pressed && state.num_presses <= 0) {
if (!state.is_held && state.num_presses <= 0) {
continue;
}
@ -423,6 +424,20 @@ INTERNAL void user_update(void)
});
}
/* ========================== *
* Debug commands
* ========================== */
/* Test clear world */
{
struct bind_state state = L.bind_states[USER_BIND_KIND_DEBUG_CLEAR];
if (state.num_presses || state.is_held) {
queue_game_cmd(&cmd_list, (struct game_cmd) {
.kind = GAME_CMD_KIND_CLEAR_ALL
});
}
}
/* ========================== *
* Update view from debug camera
* ========================== */
@ -589,6 +604,8 @@ INTERNAL void user_update(void)
/* Iterate entities */
for (u64 entity_index = 0; entity_index < L.world.entities_count; ++entity_index) {
__profscope(user_entity_iter);
struct entity *ent = &L.world.entities[entity_index];
if (!ent->valid) continue;

View File

@ -13,6 +13,7 @@ enum user_bind_kind {
/* Testing */
USER_BIND_KIND_DEBUG_CLEAR,
USER_BIND_KIND_DEBUG_DRAW,
USER_BIND_KIND_DEBUG_CAMERA,
USER_BIND_KIND_ZOOM_IN,

View File

@ -40,7 +40,7 @@ struct entity *world_alloc_entity(struct world *world)
} else {
/* Make new */
u64 idx = world->entities_count++;
entity = &world->entities[idx];
entity = arena_push(&world->entities_arena, struct entity);
*entity = (struct entity) {
.handle = { .gen = 1, .idx = idx }
};