From b1d22bbb87583cea9fde7620eb1fc16189be5113 Mon Sep 17 00:00:00 2001 From: hgn Date: Tue, 13 Jun 2023 12:56:27 +0100 Subject: [PATCH] LRU dummy --- ent_skateshop.c | 65 ++++++++++++++++++++++++++++++++++++++----------- ent_skateshop.h | 8 ++++-- save.c | 2 +- 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/ent_skateshop.c b/ent_skateshop.c index 91947fb..2957c8f 100644 --- a/ent_skateshop.c +++ b/ent_skateshop.c @@ -43,6 +43,7 @@ VG_STATIC struct cache_board *skateshop_cache_fetch_board( u32 registry_index ) } } +#if 0 /* lru eviction. should be a linked list maybe... */ double min_time = 1e300; struct cache_board *min_board = NULL; @@ -59,6 +60,12 @@ VG_STATIC struct cache_board *skateshop_cache_fetch_board( u32 registry_index ) min_board = cache_ptr; } } +#else + + SDL_AtomicLock( &global_skateshop.sl_cache_access ); + struct cache_board *min_board = lru_volatile_cache_board(); + +#endif if( min_board ){ if( min_board->state == k_cache_board_state_loaded ){ @@ -76,7 +83,6 @@ VG_STATIC struct cache_board *skateshop_cache_fetch_board( u32 registry_index ) min_board->reg_ptr = reg; min_board->reg_index = registry_index; - min_board->last_use_time = vg.time; min_board->ref_count = 0; min_board->state = k_cache_board_state_load_request; } @@ -115,7 +121,6 @@ VG_STATIC void skateshop_async_board_loaded( void *payload, u32 size ) { SDL_AtomicLock( &global_skateshop.sl_cache_access ); struct cache_board *cache_ptr = payload; - cache_ptr->last_use_time = vg.time; cache_ptr->state = k_cache_board_state_loaded; cache_ptr->reg_ptr->userdata = cache_ptr; @@ -241,27 +246,49 @@ VG_STATIC void skateshop_op_processview(void){ * ----------------------------------------------------------------------------- */ -/* we can only keep using a viewslot pointer for multiple frames if we watch it - * using this function */ +/* adds one more watch */ VG_STATIC void watch_cache_board( struct cache_board *ptr ){ if( ptr->ref_count >= 32 ){ vg_fatal_error( "dynamic board watch missmatch (limit is 32)\n" ); } - ptr->last_use_time = vg.time; ptr->ref_count ++; } -/* after this is called, the calling code only has access to the pointer for the - * duration of the rest of the frame */ +/* if after this no more watches, places back into the volatile list */ VG_STATIC void unwatch_cache_board( struct cache_board *ptr ){ if( ptr->ref_count == 0 ){ vg_fatal_error( "dynamic board unwatch missmatch (no watchers)\n" ); } ptr->ref_count --; + if( !ptr->ref_count ){ + struct cache_board *head = global_skateshop.cache_head, + *tail = global_skateshop.cache_tail; + + if( tail ) tail->right = ptr; + ptr->left = tail; + global_skateshop.cache_tail = ptr; + + if( !head ) global_skateshop.cache_head = ptr; + } } +/* retrieve oldest pointer from the volatile list (and remove it) */ +VG_STATIC struct cache_board *lru_volatile_cache_board(void){ + struct cache_board *head = global_skateshop.cache_head, + *tail = global_skateshop.cache_tail; + + if( head ){ + if( head == tail ) global_skateshop.cache_tail = NULL; + global_skateshop.cache_head = head->right; + + head->left = NULL; + head->right = NULL; + return head; + } + else return NULL; +} /* * VG event init @@ -271,14 +298,24 @@ VG_STATIC void skateshop_init(void){ global_skateshop.cache = vg_linear_alloc( vg_mem.rtmemory, cache_size ); memset( global_skateshop.cache, 0, cache_size ); - for( u32 i=0; istate = k_cache_board_state_none; - board->reg_ptr= NULL; - board->reg_index = 0xffffffff; - board->last_use_time = -99999.9; - board->ref_count = 0; + for( i32 ib=0; ib=0? &arr[ia]: NULL, + *pc = icleft = pa; + pb->right = pc; + + pb->state = k_cache_board_state_none; + pb->reg_ptr= NULL; + pb->reg_index = 0xffffffff; + pb->ref_count = 0; } + + global_skateshop.cache_head = global_skateshop.cache; + global_skateshop.cache_tail = + &global_skateshop.cache[SKATESHOP_BOARD_CACHE_MAX-1]; } VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void) diff --git a/ent_skateshop.h b/ent_skateshop.h index ceefc8b..12cfd21 100644 --- a/ent_skateshop.h +++ b/ent_skateshop.h @@ -31,9 +31,12 @@ struct{ addon_reg *reg_ptr; u32 ref_count; - double last_use_time; + + struct cache_board *left, *right; + /* double last_use_time; */ } - *cache; + *cache, *cache_head, *cache_tail; + SDL_SpinLock sl_cache_access; struct shop_view_slot{ @@ -60,5 +63,6 @@ static global_skateshop={.render={.reg_id=0xffffffff,.world_reg=0xffffffff}}; VG_STATIC void global_skateshop_exit(void); VG_STATIC void watch_cache_board( struct cache_board *ptr ); VG_STATIC void unwatch_cache_board( struct cache_board *ptr ); +VG_STATIC struct cache_board *lru_volatile_cache_board(void); #endif /* ENT_SKATESHOP_H */ diff --git a/save.c b/save.c index 2c5e8fd..b163cb4 100644 --- a/save.c +++ b/save.c @@ -7,7 +7,7 @@ struct { } static savedata; -static void skaterift_write_savedata_thread(void *){ +static void skaterift_write_savedata_thread(void *_){ FILE *fp = fopen( "save.bkv", "wb" ); if( fp ){ fwrite( savedata.buf, savedata.len, 1, fp ); -- 2.25.1