X-Git-Url: https://harrygodden.com/git/?p=vg.git;a=blobdiff_plain;f=vg_mem_pool.h;fp=vg_mem_pool.h;h=3a61881ddca69ae9584f3243bc6121ebb318c993;hp=412d2c0a3b11be67d43c5e75f97a80d80bc3fb52;hb=3b14f3dcd5bf9dd3c85144f2123d667bfa4bb63f;hpb=fce86711735b15bff37de0f70716808410fcf269 diff --git a/vg_mem_pool.h b/vg_mem_pool.h index 412d2c0..3a61881 100644 --- a/vg_mem_pool.h +++ b/vg_mem_pool.h @@ -1,119 +1,26 @@ -#ifndef VG_MEM_POOL_H -#define VG_MEM_POOL_H - -#include "vg_mem.h" -#include "stddef.h" -#include "vg_stdint.h" +#pragma once +#include "vg_platform.h" typedef struct vg_pool vg_pool; typedef struct vg_pool_node vg_pool_node; /* this goes in your structures */ -struct vg_pool_node { +struct vg_pool_node +{ u16 l, r, ref_count; }; -struct vg_pool { +struct vg_pool +{ void *buffer; /* array which holds the real data */ u16 count, head, tail; size_t stride, offset; }; -static vg_pool_node *vg_pool_nodeptr ( vg_pool *pool, u16 id ); -static void *vg_pool_item ( vg_pool *pool, u16 id ); -static void vg_pool_init ( vg_pool *pool ); -static u16 vg_pool_id ( vg_pool *pool, void *item ); -static u16 vg_pool_lru ( vg_pool *pool ); -static void vg_pool_watch ( vg_pool *pool, u16 id ); -static void vg_pool_unwatch ( vg_pool *pool, u16 id ); - -/* implementation - * -------------------------------------------------------------------------- */ - -static vg_pool_node *vg_pool_nodeptr( vg_pool *pool, u16 id ){ - if( !id ) return NULL; - else { - return pool->buffer + (pool->stride*(id-1)) + pool->offset; - } -} - -static void *vg_pool_item( vg_pool *pool, u16 id ){ - if( (id == 0) || (id > pool->count) ) - return NULL; - - return pool->buffer + pool->stride*(size_t)(id-1); -} - -static void vg_pool_init( vg_pool *pool ){ - pool->head = 1; - pool->tail = pool->count; - for( u16 ib=1; ib <= pool->count; ib++ ){ - vg_pool_node *nb = vg_pool_nodeptr( pool, ib ); - - u16 ia = ib-1, ic = ib+1; - nb->l = ia; - nb->r = ic<=pool->count? ic: 0; - nb->ref_count = 0; - } -} - -static u16 vg_pool_id( vg_pool *pool, void *item ){ - return ((item - pool->buffer) / pool->stride) + 1; -} - -static void vg_pool_unlink( vg_pool *pool, u16 id ){ - vg_pool_node *node = vg_pool_nodeptr( pool, id ); - vg_pool_node *l = vg_pool_nodeptr( pool, node->l ), - *r = vg_pool_nodeptr( pool, node->r ); - - if( pool->head == id ) pool->head = node->r; - if( pool->tail == id ) pool->tail = node->l; - - if( l ) l->r = node->r; - if( r ) r->l = node->l; - - node->r = 0; - node->l = 0; -} - -static u16 vg_pool_lru( vg_pool *pool ){ - u16 head = pool->head; - if( !head ) return 0; - - vg_pool_unlink( pool, head ); - return head; -} - -static void vg_pool_watch( vg_pool *pool, u16 id ){ - vg_pool_node *node = vg_pool_nodeptr( pool, id ); - - if( !node->ref_count ){ - vg_pool_unlink( pool, id ); - } - - if( node->ref_count >= 128 ) - vg_fatal_error( "pool watch missmatch (limit is 128)\n" ); - - node->ref_count ++; -} - -/* if after this no more watches, places back into the volatile list */ -static void vg_pool_unwatch( vg_pool *pool, u16 id ){ - vg_pool_node *node = vg_pool_nodeptr( pool, id ); - - if( node->ref_count == 0 ) - vg_fatal_error( "pool unwatch missmatch (no watchers)\n" ); - - node->ref_count --; - if( !node->ref_count ){ - vg_pool_node *head = vg_pool_nodeptr( pool, pool->head ), - *tail = vg_pool_nodeptr( pool, pool->tail ); - - if( tail ) tail->r = id; - node->l = pool->tail; - pool->tail = id; - if( !head ) pool->head = id; - } -} - -#endif /* VG_MEM_POOL_H */ +vg_pool_node *vg_pool_nodeptr ( vg_pool *pool, u16 id ); +void *vg_pool_item ( vg_pool *pool, u16 id ); +void vg_pool_init ( vg_pool *pool ); +u16 vg_pool_id ( vg_pool *pool, void *item ); +u16 vg_pool_lru ( vg_pool *pool ); +void vg_pool_watch ( vg_pool *pool, u16 id ); +void vg_pool_unwatch ( vg_pool *pool, u16 id );