}
}
+#if 0
/* lru eviction. should be a linked list maybe... */
double min_time = 1e300;
struct cache_board *min_board = NULL;
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 ){
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;
}
{
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;
* -----------------------------------------------------------------------------
*/
-/* 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
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)