1. libgd
  2. Untitled project
  3. gd-libgd

Commits

mattias  committed 2f4063f

- ws + cs

  • Participants
  • Parent commits dc87e28
  • Branches GD-2.0

Comments (0)

Files changed (1)

File src/gdcache.c

View file
  • Ignore whitespace
-
 #ifdef HAVE_CONFIG_H
-#include "config.h"
+#	include "config.h"
 #endif
 
 #include "gd.h"
 #include "gdhelpers.h"
 
 #ifdef HAVE_LIBTTF
-#define NEED_CACHE 1
+#	define NEED_CACHE 1
 #else
 #ifdef HAVE_LIBFREETYPE
-#define NEED_CACHE 1
+#	define NEED_CACHE 1
 #endif
 #endif
 
 /* implementation                                        */
 /*********************************************************/
 
-
 /* create a new cache */
-gdCache_head_t *
-gdCacheCreate (int size,
-	       gdCacheTestFn_t gdCacheTest,
-	       gdCacheFetchFn_t gdCacheFetch,
-	       gdCacheReleaseFn_t gdCacheRelease)
+gdCache_head_t *gdCacheCreate(int size,
+	gdCacheTestFn_t gdCacheTest,
+	gdCacheFetchFn_t gdCacheFetch,
+	gdCacheReleaseFn_t gdCacheRelease)
 {
-  gdCache_head_t *head;
+	gdCache_head_t *head;
 
-  head = (gdCache_head_t *) gdMalloc (sizeof (gdCache_head_t));
-	if (!head) {
+	head = (gdCache_head_t *)gdMalloc(sizeof(gdCache_head_t));
+	if(!head) {
 		return NULL;
 	}
 
-  head->mru = NULL;
-  head->size = size;
-  head->gdCacheTest = gdCacheTest;
-  head->gdCacheFetch = gdCacheFetch;
-  head->gdCacheRelease = gdCacheRelease;
-  return head;
+	head->mru = NULL;
+	head->size = size;
+	head->gdCacheTest = gdCacheTest;
+	head->gdCacheFetch = gdCacheFetch;
+	head->gdCacheRelease = gdCacheRelease;
+
+	return head;
 }
 
-void
-gdCacheDelete (gdCache_head_t * head)
+void gdCacheDelete(gdCache_head_t *head)
 {
-  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);
+	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);
 }
 
-void *
-gdCacheGet (gdCache_head_t * head, void *keydata)
+void * gdCacheGet(gdCache_head_t *head, void *keydata)
 {
-  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;
+	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;
 	}
-      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;
-    }
-  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;
-			 }
-    }
-  else
-    {				/* cache full - replace least-recently-used */
-      /* preveprev becomes new end of list */
-      prevprev->next = NULL;
-      elem = prev;
-      (*(head->gdCacheRelease)) (elem->userdata);
-    }
-  /* relink to top of list */
-  elem->next = head->mru;
-  head->mru = elem;
-  elem->userdata = userdata;
-  return userdata;
-}
 
+	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;
+ 		}
+	} else {
+		/* cache full - replace least-recently-used */
+		/* preveprev becomes new end of list */
+		prevprev->next = NULL;
+		elem = prev;
+		(*(head->gdCacheRelease))(elem->userdata);
+	}
 
+	/* relink to top of list */
+	elem->next = head->mru;
+	head->mru = elem;
+	elem->userdata = userdata;
+
+	return userdata;
+}
 
 /*********************************************************/
 /* test stub                                             */
 /*********************************************************/
 
-
 #ifdef TEST
 
 #include <stdio.h>
 
 typedef struct
 {
-  int key;
-  int value;
+	int key;
+	int value;
 }
 key_value_t;
 
-static int
-cacheTest (void *map, void *key)
+static int cacheTest(void *map, void *key)
 {
-  return (((key_value_t *) map)->key == *(int *) key);
+	return (((key_value_t *)map)->key == *(int *)key);
 }
 
-static void *
-cacheFetch (char **error, void *key)
+static void *cacheFetch(char **error, void *key)
 {
-  key_value_t *map;
+	key_value_t *map;
+
+	map = (key_value_t *)gdMalloc(sizeof(key_value_t));
+	map->key = *(int *)key;
+	map->value = 3;
 
-  map = (key_value_t *) gdMalloc (sizeof (key_value_t));
-  map->key = *(int *) key;
-  map->value = 3;
+	*error = NULL;
 
-  *error = NULL;
-  return (void *) map;
+	return (void *)map;
 }
 
-static void
-cacheRelease (void *map)
+static void cacheRelease(void *map)
 {
-  gdFree ((char *) map);
+	gdFree((char *)map);
 }
 
-int
-main (char *argv[], int argc)
+int main(char *argv[], int argc)
 {
-  gdCache_head_t *cacheTable;
-  int elem, key;
+	gdCache_head_t *cacheTable;
+	int elem, key;
 
-  cacheTable = gdCacheCreate (3, cacheTest, cacheFetch, cacheRelease);
-	if (!cacheTable) {
+	cacheTable = gdCacheCreate(3, cacheTest, cacheFetch, cacheRelease);
+	if(!cacheTable) {
 		exit(1);
 	}
 
-  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;
+	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;
 }
 
 #endif /* TEST */