LRU dummy
[carveJwlIkooP6JGAAIwe30JlM.git] / ent_skateshop.c
index 99f9a7079d0ce6b6ead4021729f24f72a2d394ca..2957c8f4be07e8240af807a42feb931091a49b1d 100644 (file)
@@ -14,6 +14,7 @@
 #include "highscores.h"
 #include "steam.h"
 #include "addon.h"
+#include "save.h"
 
 /*
  * Checks string equality but does a hash check first
@@ -42,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;
@@ -58,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 ){
@@ -75,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;
    }
@@ -114,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;
@@ -240,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
@@ -270,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)
@@ -303,14 +341,12 @@ VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void)
 VG_STATIC void pointcloud_async_end(void *_, u32 __)
 {
    pointcloud_animate( k_pointcloud_anim_opening );
-   skaterift_end_op();
 }
 
 VG_STATIC void pointcloud_clear_async(void *_, u32 __)
 {
    pointcloud.count = 0;
    pointcloud_animate( k_pointcloud_anim_opening );
-   skaterift_end_op();
 }
 
 VG_STATIC void skateshop_preview_loader_thread( void *_data )
@@ -351,10 +387,15 @@ VG_STATIC void skateshop_preview_loader_thread( void *_data )
    }
 }
 
+VG_STATIC void skateshop_preview_loader_thread_and_end( void *_data ){
+   skateshop_preview_loader_thread( _data );
+   skaterift_end_op();
+}
+
 VG_STATIC void skateshop_load_world_preview( addon_reg *reg )
 {
    skaterift_begin_op( k_async_op_world_load_preview );
-   vg_loader_start( skateshop_preview_loader_thread, reg );
+   vg_loader_start( skateshop_preview_loader_thread_and_end, reg );
 }
 
 /*
@@ -453,6 +494,7 @@ VG_STATIC void global_skateshop_preupdate(void)
          localplayer.board_view_slot = selected_cache;
          watch_cache_board( localplayer.board_view_slot );
          global_skateshop_exit();
+         skaterift_write_savedata();
          return;
       }
    }