-#ifndef ADDON_CACHE_C
-#define ADDON_CACHE_C
-
-#include "addon_cache.h"
-
-static addon_cache_node *addon_cache_nodeptr( addon_cache *cache, void *item ){
- if( !item ) return NULL;
- else return item + cache->offset;
-}
-
-static void *addon_cache_item( addon_cache *cache, u16 index ){
- if( index == ADDON_CACHE_NIL ) return NULL;
- else return cache->buffer + cache->stride*(size_t)index;
-}
-
-static void addon_cache_init( addon_cache *cache ){
- cache->head = 0;
- cache->tail = cache->count -1;
- for( i32 ib=0; ib < cache->count; ib++ ){
- void *vb = addon_cache_item( cache, ib );
- addon_cache_node *nb = addon_cache_nodeptr( cache, vb );
-
- i32 ia = ib-1, ic = ib+1;
- nb->l = ia>=0? ia: ADDON_CACHE_NIL,
- nb->r = ic<cache->count? ic: ADDON_CACHE_NIL;
- nb->ref_count = 0;
- }
-}
-
-static u16 addon_item_index( addon_cache *cache, void *item ){
- return (item - cache->buffer) / cache->stride;
-}
-
-static void *addon_cache_lru( addon_cache *cache ){
- u16 head = cache->head;
- if( head == ADDON_CACHE_NIL ) return NULL;
-
- void *item = addon_cache_item( cache, head );
- addon_cache_node *node = addon_cache_nodeptr( cache, item );
-
- if( cache->head == cache->tail ) cache->tail = ADDON_CACHE_NIL;
- cache->head = node->r;
-
- node->l = ADDON_CACHE_NIL;
- node->r = ADDON_CACHE_NIL;
- return item;
-}
-
-static void addon_cache_watch( addon_cache *cache, void *item ){
- addon_cache_node *node = addon_cache_nodeptr( cache, item );
-
- if( node->ref_count >= 32 )
- vg_fatal_error( "dynamic board watch missmatch (limit is 32)\n" );
-
- node->ref_count ++;
-}
-
-/* if after this no more watches, places back into the volatile list */
-static void addon_cache_unwatch( addon_cache *cache, void *item ){
- addon_cache_node *node = addon_cache_nodeptr( cache, item );
-
- if( node->ref_count == 0 )
- vg_fatal_error( "dynamic board unwatch missmatch (no watchers)\n" );
-
- node->ref_count --;
- if( !node->ref_count ){
- void *item_head = addon_cache_item( cache, cache->head ),
- *item_tail = addon_cache_item( cache, cache->tail );
- addon_cache_node *head = addon_cache_nodeptr( cache, item_head ),
- *tail = addon_cache_nodeptr( cache, item_tail );
-
- u16 index = addon_item_index( cache, item );
- if( tail ) tail->r = index;
- node->l = cache->tail;
- cache->tail = index;
- if( !head ) cache->head = index;
- }
-}
-
-#endif /* ADDON_CACHE_C */