home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / gdcache.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-06-28  |  2.7 KB  |  94 lines

  1. #ifdef __cplusplus
  2. extern "C" {
  3. #endif
  4.  
  5. /* 
  6.  * gdcache.h
  7.  *
  8.  * Caches of pointers to user structs in which the least-recently-used 
  9.  * element is replaced in the event of a cache miss after the cache has 
  10.  * reached a given size.
  11.  *
  12.  * John Ellson  (ellson@graphviz.org)  Oct 31, 1997
  13.  *
  14.  * Test this with:
  15.  *         gcc -o gdcache -g -Wall -DTEST gdcache.c
  16.  *
  17.  * The cache is implemented by a singly-linked list of elements
  18.  * each containing a pointer to a user struct that is being managed by
  19.  * the cache.
  20.  *
  21.  * The head structure has a pointer to the most-recently-used
  22.  * element, and elements are moved to this position in the list each
  23.  * time they are used.  The head also contains pointers to three
  24.  * user defined functions: 
  25.  *        - a function to test if a cached userdata matches some keydata 
  26.  *        - a function to provide a new userdata struct to the cache 
  27.  *          if there has been a cache miss.
  28.  *        - a function to release a userdata struct when it is
  29.  *          no longer being managed by the cache
  30.  *
  31.  * In the event of a cache miss the cache is allowed to grow up to
  32.  * a specified maximum size.  After the maximum size is reached then
  33.  * the least-recently-used element is discarded to make room for the 
  34.  * new.  The most-recently-returned value is always left at the 
  35.  * beginning of the list after retrieval.
  36.  *
  37.  * In the current implementation the cache is traversed by a linear
  38.  * search from most-recent to least-recent.  This linear search
  39.  * probably limits the usefulness of this implementation to cache
  40.  * sizes of a few tens of elements.
  41.  */
  42.  
  43. /*********************************************************/
  44. /* header                                                */
  45. /*********************************************************/
  46.  
  47. #ifdef HAVE_CONFIG_H
  48. #include "config.h"
  49. #endif
  50.  
  51. #include <stdlib.h>
  52. #ifndef NULL
  53. #define NULL (void *)0
  54. #endif
  55.  
  56. /* user defined function templates */
  57. typedef int (*gdCacheTestFn_t) (void *userdata, void *keydata);
  58. typedef void *(*gdCacheFetchFn_t) (char **error, void *keydata);
  59. typedef void (*gdCacheReleaseFn_t) (void *userdata);
  60.  
  61. /* element structure */
  62. typedef struct gdCache_element_s gdCache_element_t;
  63. struct gdCache_element_s
  64. {
  65.   gdCache_element_t *next;
  66.   void *userdata;
  67. };
  68.  
  69. /* head structure */
  70. typedef struct gdCache_head_s gdCache_head_t;
  71. struct gdCache_head_s
  72. {
  73.   gdCache_element_t *mru;
  74.   int size;
  75.   char *error;
  76.   gdCacheTestFn_t gdCacheTest;
  77.   gdCacheFetchFn_t gdCacheFetch;
  78.   gdCacheReleaseFn_t gdCacheRelease;
  79. };
  80.  
  81. /* function templates */
  82. gdCache_head_t *gdCacheCreate (int size,
  83.                    gdCacheTestFn_t gdCacheTest,
  84.                    gdCacheFetchFn_t gdCacheFetch,
  85.                    gdCacheReleaseFn_t gdCacheRelease);
  86.  
  87. void gdCacheDelete (gdCache_head_t * head);
  88.  
  89. void *gdCacheGet (gdCache_head_t * head, void *keydata);
  90.  
  91. #ifdef __cplusplus
  92. }
  93. #endif
  94.