2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <glib/gi18n.h>
30 #include "preferences.h"
34 typedef struct _List {
39 /* a circular linked list, a pointer to the tail, and the tail points to the head */
40 /* this is so we can free the last */
41 static List *queue_tail = NULL;
42 static int queue_count = 0;
44 static guint32 queue_size = 0;
45 static guint32 max_queue_size = VIK_CONFIG_MAPCACHE_SIZE;
48 static GHashTable *cache = NULL;
50 static GMutex *mc_mutex = NULL;
52 #define HASHKEY_FORMAT_STRING "%d-%d-%d-%d-%d-%d-%.3f-%.3f"
53 #define HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA "%d-%d-%d-%d-%d-"
55 static VikLayerParamScale params_scales[] = {
56 /* min, max, step, digits (decimal places) */
60 static VikLayerParam prefs[] = {
61 { VIKING_PREFERENCES_NAMESPACE "mapcache_size", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, N_("Mapcache memory size (MB):"), VIK_LAYER_WIDGET_HSCALE, params_scales, NULL },
64 void a_mapcache_init ()
66 VikLayerParamData tmp;
67 tmp.u = VIK_CONFIG_MAPCACHE_SIZE / 1024 / 1024;
68 a_preferences_register(prefs, tmp, VIKING_PREFERENCES_GROUP_KEY);
70 mc_mutex = g_mutex_new();
71 cache = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, g_object_unref );
74 static void cache_add(gchar *key, GdkPixbuf *pixbuf)
76 /* TODO: Check if already exists */
77 g_hash_table_insert ( cache, key, pixbuf );
78 queue_size += gdk_pixbuf_get_rowstride(pixbuf) * gdk_pixbuf_get_height(pixbuf);
83 static void cache_remove(const gchar *key)
85 GdkPixbuf *buf = g_hash_table_lookup ( cache, key );
87 queue_size -= gdk_pixbuf_get_rowstride(buf) * gdk_pixbuf_get_height(buf);
90 g_hash_table_remove ( cache, key );
94 /* returns key from head, adds on newtailkey to tail. */
95 static gchar *list_shift_add_entry ( gchar *newtailkey )
97 gchar *oldheadkey = queue_tail->next->key;
98 queue_tail->next->key = newtailkey;
99 queue_tail = queue_tail->next;
103 static gchar *list_shift ()
105 gchar *oldheadkey = queue_tail->next->key;
106 List *oldhead = queue_tail->next;
107 queue_tail->next = queue_tail->next->next;
112 /* adds key to tail */
113 static void list_add_entry ( gchar *key )
115 List *newlist = g_malloc ( sizeof ( List ) );
118 newlist->next = queue_tail->next;
119 queue_tail->next = newlist;
120 queue_tail = newlist;
122 newlist->next = newlist;
123 queue_tail = newlist;
127 void a_mapcache_add ( GdkPixbuf *pixbuf, gint x, gint y, gint z, guint8 type, guint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor )
129 gchar *key = g_strdup_printf ( HASHKEY_FORMAT_STRING, x, y, z, type, zoom, alpha, xshrinkfactor, yshrinkfactor );
132 g_mutex_lock(mc_mutex);
133 cache_add(key, pixbuf);
135 // TODO: that should be done on preference change only...
136 max_queue_size = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "mapcache_size")->u * 1024 * 1024;
138 if ( queue_size > max_queue_size ) {
139 gchar *oldkey = list_shift_add_entry ( key );
140 cache_remove(oldkey);
142 while ( queue_size > max_queue_size &&
143 (queue_tail->next != queue_tail) ) { /* make sure there's more than one thing to delete */
144 oldkey = list_shift ();
145 cache_remove(oldkey);
148 /* chop off 'start' etc */
150 list_add_entry ( key );
151 /* business as usual */
153 g_mutex_unlock(mc_mutex);
155 if ( (++tmp == 100 )) { g_print("DEBUG: queue count=%d size=%u\n", queue_count, queue_size ); tmp=0; }
158 GdkPixbuf *a_mapcache_get ( gint x, gint y, gint z, guint8 type, guint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor )
161 g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING, x, y, z, type, zoom, alpha, xshrinkfactor, yshrinkfactor );
162 return g_hash_table_lookup ( cache, key );
165 void a_mapcache_remove_all_shrinkfactors ( gint x, gint y, gint z, guint8 type, guint zoom )
168 List *loop = queue_tail;
172 if ( queue_tail == NULL )
175 g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA, x, y, z, type, zoom );
178 g_mutex_lock(mc_mutex);
179 /* TODO: check logic here */
182 if ( strncmp(tmp->key, key, len) == 0 )
184 cache_remove(tmp->key);
185 if ( tmp == loop ) /* we deleted the last thing in the queue! */
186 loop = queue_tail = NULL;
188 loop->next = tmp->next;
189 if ( tmp == queue_tail )
190 queue_tail = tmp->next;
198 } while ( loop && (loop != queue_tail || tmp == NULL) );
200 /* loop thru list, looking for the one, compare first whatever chars */
202 g_mutex_unlock(mc_mutex);
205 void a_mapcache_flush ()
207 List *loop = queue_tail;
210 if ( queue_tail == NULL )
213 g_mutex_lock(mc_mutex);
216 cache_remove(tmp->key);
217 if ( tmp == queue_tail ) /* we deleted the last thing in the queue */
218 loop = queue_tail = NULL;
220 loop->next = tmp->next;
225 g_mutex_unlock(mc_mutex);
228 void a_mapcache_uninit ()
230 g_hash_table_destroy ( cache );