libgd/src/gdcache.c

232 lines
5.3 KiB
C
Raw Normal View History

2006-04-05 08:44:56 -07:00
#ifdef HAVE_CONFIG_H
2007-12-22 04:58:05 -08:00
# include "config.h"
2006-04-05 08:44:56 -07:00
#endif
2006-04-05 08:41:55 -07:00
#include "gd.h"
#include "gdhelpers.h"
2006-04-05 08:37:05 -07:00
#ifdef HAVE_LIBTTF
2007-12-22 04:58:05 -08:00
# define NEED_CACHE 1
2006-04-05 08:41:55 -07:00
#else
#ifdef HAVE_LIBFREETYPE
2007-12-22 04:58:05 -08:00
# define NEED_CACHE 1
2006-04-05 08:41:55 -07:00
#endif
#endif
#ifdef NEED_CACHE
2006-04-05 08:37:05 -07:00
2013-04-03 05:23:11 -07:00
/*
2006-04-05 08:37:05 -07:00
* gdcache.c
*
2013-04-03 05:23:11 -07:00
* Caches of pointers to user structs in which the least-recently-used
* element is replaced in the event of a cache miss after the cache has
2006-04-05 08:37:05 -07:00
* reached a given size.
*
2006-04-05 08:47:57 -07:00
* John Ellson (ellson@graphviz.org) Oct 31, 1997
2006-04-05 08:37:05 -07:00
*
* Test this with:
* gcc -o gdcache -g -Wall -DTEST -DNEED_CACHE gdcache.c -lgd
* or
* gcc -o gdcache -g -Wall -DTEST -DNEED_CACHE gdcache.c libgd.a
2006-04-05 08:37:05 -07:00
*
* The cache is implemented by a singly-linked list of elements
* each containing a pointer to a user struct that is being managed by
* the cache.
*
* The head structure has a pointer to the most-recently-used
* element, and elements are moved to this position in the list each
* time they are used. The head also contains pointers to three
2013-04-03 05:23:11 -07:00
* user defined functions:
* - a function to test if a cached userdata matches some keydata
* - a function to provide a new userdata struct to the cache
2006-04-05 08:37:05 -07:00
* if there has been a cache miss.
2006-04-05 08:42:56 -07:00
* - a function to release a userdata struct when it is
2006-04-05 08:37:05 -07:00
* no longer being managed by the cache
*
* In the event of a cache miss the cache is allowed to grow up to
* a specified maximum size. After the maximum size is reached then
2013-04-03 05:23:11 -07:00
* the least-recently-used element is discarded to make room for the
* new. The most-recently-returned value is always left at the
2006-04-05 08:37:05 -07:00
* beginning of the list after retrieval.
*
* In the current implementation the cache is traversed by a linear
* search from most-recent to least-recent. This linear search
* probably limits the usefulness of this implementation to cache
* sizes of a few tens of elements.
*/
#include "gdcache.h"
/*********************************************************/
/* implementation */
/*********************************************************/
/* create a new cache */
2007-12-22 04:58:05 -08:00
gdCache_head_t *gdCacheCreate(int size,
2013-04-03 05:23:11 -07:00
gdCacheTestFn_t gdCacheTest,
gdCacheFetchFn_t gdCacheFetch,
gdCacheReleaseFn_t gdCacheRelease)
2006-04-05 08:37:05 -07:00
{
2007-12-22 04:58:05 -08:00
gdCache_head_t *head;
2006-04-05 08:42:56 -07:00
2007-12-22 04:58:05 -08:00
head = (gdCache_head_t *)gdMalloc(sizeof(gdCache_head_t));
if(!head) {
return NULL;
}
2007-12-22 04:58:05 -08:00
head->mru = NULL;
head->size = size;
head->gdCacheTest = gdCacheTest;
head->gdCacheFetch = gdCacheFetch;
head->gdCacheRelease = gdCacheRelease;
return head;
2006-04-05 08:37:05 -07:00
}
2007-12-22 04:58:05 -08:00
void gdCacheDelete(gdCache_head_t *head)
2006-04-05 08:37:05 -07:00
{
2007-12-22 04:58:05 -08:00
gdCache_element_t *elem, *prev;
elem = head->mru;
while(elem) {
(*(head->gdCacheRelease))(elem->userdata);
prev = elem;
elem = elem->next;
gdFree((char *)prev);
}
gdFree((char *)head);
2006-04-05 08:37:05 -07:00
}
2007-12-22 04:58:05 -08:00
void * gdCacheGet(gdCache_head_t *head, void *keydata)
2006-04-05 08:37:05 -07:00
{
2007-12-22 04:58:05 -08:00
int i = 0;
gdCache_element_t *elem, *prev = NULL, *prevprev = NULL;
void *userdata;
elem = head->mru;
while(elem) {
if((*(head->gdCacheTest))(elem->userdata, keydata)) {
if(i) {
/* if not already most-recently-used */
/* relink to top of list */
prev->next = elem->next;
elem->next = head->mru;
head->mru = elem;
}
return elem->userdata;
}
prevprev = prev;
prev = elem;
elem = elem->next;
i++;
}
userdata = (*(head->gdCacheFetch))(&(head->error), keydata);
if(!userdata) {
/* if there was an error in the fetch then don't cache */
return NULL;
2006-04-05 08:37:05 -07:00
}
2007-12-22 04:58:05 -08:00
if(i < head->size) {
/* cache still growing - add new elem */
elem = (gdCache_element_t *)gdMalloc(sizeof(gdCache_element_t));
if(!elem) {
(*(head->gdCacheRelease)) (userdata);
return NULL;
2013-04-03 05:23:11 -07:00
}
2007-12-22 04:58:05 -08:00
} else {
/* cache full - replace least-recently-used */
if(!prevprev) {
/* cache size is 1 */
head->mru = NULL;
} else {
/* prevprev becomes new end of list */
prevprev->next = NULL;
}
2007-12-22 04:58:05 -08:00
elem = prev;
(*(head->gdCacheRelease))(elem->userdata);
}
2006-04-05 08:37:05 -07:00
2007-12-22 04:58:05 -08:00
/* relink to top of list */
elem->next = head->mru;
head->mru = elem;
elem->userdata = userdata;
return userdata;
}
2006-04-05 08:37:05 -07:00
/*********************************************************/
/* test stub */
/*********************************************************/
#ifdef TEST
#include <stdio.h>
2013-04-03 05:23:11 -07:00
typedef struct {
2007-12-22 04:58:05 -08:00
int key;
int value;
2006-04-05 08:42:56 -07:00
}
key_value_t;
2006-04-05 08:37:05 -07:00
2007-12-22 04:58:05 -08:00
static int cacheTest(void *map, void *key)
2006-04-05 08:37:05 -07:00
{
2007-12-22 04:58:05 -08:00
return (((key_value_t *)map)->key == *(int *)key);
2006-04-05 08:37:05 -07:00
}
2007-12-22 04:58:05 -08:00
static void *cacheFetch(char **error, void *key)
2006-04-05 08:37:05 -07:00
{
2007-12-22 04:58:05 -08:00
key_value_t *map;
map = (key_value_t *)gdMalloc(sizeof(key_value_t));
2011-01-19 20:48:40 -08:00
if (!map) {
*error = "gdMalloc failed";
return NULL;
}
2007-12-22 04:58:05 -08:00
map->key = *(int *)key;
map->value = 3;
2006-04-05 08:37:05 -07:00
2007-12-22 04:58:05 -08:00
*error = NULL;
2006-04-05 08:37:05 -07:00
2007-12-22 04:58:05 -08:00
return (void *)map;
2006-04-05 08:37:05 -07:00
}
2007-12-22 04:58:05 -08:00
static void cacheRelease(void *map)
2006-04-05 08:37:05 -07:00
{
2007-12-22 04:58:05 -08:00
gdFree((char *)map);
2006-04-05 08:37:05 -07:00
}
int main(int argc, char **argv)
2006-04-05 08:37:05 -07:00
{
2007-12-22 04:58:05 -08:00
gdCache_head_t *cacheTable;
int elem, key;
2006-04-05 08:42:56 -07:00
2007-12-22 04:58:05 -08:00
cacheTable = gdCacheCreate(3, cacheTest, cacheFetch, cacheRelease);
if(!cacheTable) {
exit(1);
}
2006-04-05 08:42:56 -07:00
2007-12-22 04:58:05 -08:00
key = 20;
elem = *(int *)gdCacheGet(cacheTable, &key);
key = 30;
elem = *(int *)gdCacheGet(cacheTable, &key);
key = 40;
elem = *(int *)gdCacheGet(cacheTable, &key);
key = 50;
elem = *(int *)gdCacheGet(cacheTable, &key);
key = 30;
elem = *(int *)gdCacheGet(cacheTable, &key);
key = 30;
elem = *(int *)gdCacheGet(cacheTable, &key);
gdCacheDelete(cacheTable);
return 0;
2006-04-05 08:37:05 -07:00
}
#endif /* TEST */
2009-04-19 20:24:15 -07:00
#endif /* NEED_CACHE */