Blame src/gdcache.h

Packit ed3af9
#ifdef __cplusplus
Packit ed3af9
extern "C" {
Packit ed3af9
#endif
Packit ed3af9
Packit ed3af9
	/*
Packit ed3af9
	 * gdcache.h
Packit ed3af9
	 *
Packit ed3af9
	 * Caches of pointers to user structs in which the least-recently-used
Packit ed3af9
	 * element is replaced in the event of a cache miss after the cache has
Packit ed3af9
	 * reached a given size.
Packit ed3af9
	 *
Packit ed3af9
	 * John Ellson  (ellson@graphviz.org)  Oct 31, 1997
Packit ed3af9
	 *
Packit ed3af9
	 * Test this with:
Packit ed3af9
	 *		 gcc -o gdcache -g -Wall -DTEST gdcache.c
Packit ed3af9
	 *
Packit ed3af9
	 * The cache is implemented by a singly-linked list of elements
Packit ed3af9
	 * each containing a pointer to a user struct that is being managed by
Packit ed3af9
	 * the cache.
Packit ed3af9
	 *
Packit ed3af9
	 * The head structure has a pointer to the most-recently-used
Packit ed3af9
	 * element, and elements are moved to this position in the list each
Packit ed3af9
	 * time they are used.  The head also contains pointers to three
Packit ed3af9
	 * user defined functions:
Packit ed3af9
	 *		- a function to test if a cached userdata matches some keydata
Packit ed3af9
	 *		- a function to provide a new userdata struct to the cache
Packit ed3af9
	 *          if there has been a cache miss.
Packit ed3af9
	 *		- a function to release a userdata struct when it is
Packit ed3af9
	 *          no longer being managed by the cache
Packit ed3af9
	 *
Packit ed3af9
	 * In the event of a cache miss the cache is allowed to grow up to
Packit ed3af9
	 * a specified maximum size.  After the maximum size is reached then
Packit ed3af9
	 * the least-recently-used element is discarded to make room for the
Packit ed3af9
	 * new.  The most-recently-returned value is always left at the
Packit ed3af9
	 * beginning of the list after retrieval.
Packit ed3af9
	 *
Packit ed3af9
	 * In the current implementation the cache is traversed by a linear
Packit ed3af9
	 * search from most-recent to least-recent.  This linear search
Packit ed3af9
	 * probably limits the usefulness of this implementation to cache
Packit ed3af9
	 * sizes of a few tens of elements.
Packit ed3af9
	 */
Packit ed3af9
Packit ed3af9
	/*********************************************************/
Packit ed3af9
	/* header                                                */
Packit ed3af9
	/*********************************************************/
Packit ed3af9
Packit ed3af9
#include <stdlib.h>
Packit ed3af9
#ifndef NULL
Packit ed3af9
#	define NULL (void *)0
Packit ed3af9
#endif
Packit ed3af9
Packit ed3af9
	/* user defined function templates */
Packit ed3af9
	typedef int (*gdCacheTestFn_t)(void *userdata, void *keydata);
Packit ed3af9
	typedef void *(*gdCacheFetchFn_t)(char **error, void *keydata);
Packit ed3af9
	typedef void (*gdCacheReleaseFn_t)(void *userdata);
Packit ed3af9
Packit ed3af9
	/* element structure */
Packit ed3af9
	typedef struct gdCache_element_s gdCache_element_t;
Packit ed3af9
	struct gdCache_element_s {
Packit ed3af9
		gdCache_element_t *next;
Packit ed3af9
		void *userdata;
Packit ed3af9
	};
Packit ed3af9
Packit ed3af9
	/* head structure */
Packit ed3af9
	typedef struct gdCache_head_s gdCache_head_t;
Packit ed3af9
	struct gdCache_head_s {
Packit ed3af9
		gdCache_element_t *mru;
Packit ed3af9
		int size;
Packit ed3af9
		char *error;
Packit ed3af9
		gdCacheTestFn_t gdCacheTest;
Packit ed3af9
		gdCacheFetchFn_t gdCacheFetch;
Packit ed3af9
		gdCacheReleaseFn_t gdCacheRelease;
Packit ed3af9
	};
Packit ed3af9
Packit ed3af9
	/* function templates */
Packit ed3af9
	gdCache_head_t *gdCacheCreate(int size,
Packit ed3af9
	                              gdCacheTestFn_t gdCacheTest,
Packit ed3af9
	                              gdCacheFetchFn_t gdCacheFetch,
Packit ed3af9
	                              gdCacheReleaseFn_t gdCacheRelease
Packit ed3af9
	                             );
Packit ed3af9
Packit ed3af9
	void gdCacheDelete(gdCache_head_t *head);
Packit ed3af9
Packit ed3af9
	void *gdCacheGet(gdCache_head_t *head, void *keydata);
Packit ed3af9
Packit ed3af9
#ifdef __cplusplus
Packit ed3af9
}
Packit ed3af9
#endif