LRU dummy
authorhgn <hgodden00@gmail.com>
Tue, 13 Jun 2023 11:56:27 +0000 (12:56 +0100)
committerhgn <hgodden00@gmail.com>
Tue, 13 Jun 2023 11:56:27 +0000 (12:56 +0100)
ent_skateshop.c
ent_skateshop.h
save.c

index 91947fbe2893dc3ecf55741270ec0ff5a366e902..2957c8f4be07e8240af807a42feb931091a49b1d 100644 (file)
@@ -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; i<SKATESHOP_BOARD_CACHE_MAX; i++ ){
-      struct cache_board *board = &global_skateshop.cache[i];
-      board->state = 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<SKATESHOP_BOARD_CACHE_MAX; ib++ ){
+      i32 ia = ib-1, ic = ib+1;
+      struct cache_board *arr = global_skateshop.cache,
+                         *pb = &arr[ib],
+                         *pa = ia>=0? &arr[ia]: NULL,
+                         *pc = ic<SKATESHOP_BOARD_CACHE_MAX? &arr[ic]: NULL;
+      pb->left = 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)
index ceefc8bd74d08b231b225cfb52ecb65fd2c0274b..12cfd218e1b1675270673c552f262dba388c1c44 100644 (file)
@@ -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 2c5e8fd3b8b1302a4db0fe4315290b2bf1d04dc7..b163cb46ed8fc903dab2ca91d83e2629246ecbd3 100644 (file)
--- 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 );