]> git.street.me.uk Git - andy/viking.git/blob - src/mapcache.c
[QA] Use labs() instead of abs() in comparison of timestamps to avoid truncation...
[andy/viking.git] / src / mapcache.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <string.h>
28 #include "globals.h"
29 #include "mapcache.h"
30 #include "preferences.h"
31 #include "vik_compat.h"
32
33 #define MC_KEY_SIZE 64
34
35 typedef struct _List {
36   struct _List *next;
37   gchar *key;
38 } List;
39
40 /* a circular linked list, a pointer to the tail, and the tail points to the head */
41 /* this is so we can free the last */
42 static List *queue_tail = NULL;
43 static int queue_count = 0;
44
45 static guint32 cache_size = 0;
46 static guint32 max_cache_size = VIK_CONFIG_MAPCACHE_SIZE * 1024 * 1024;
47
48 static GHashTable *cache = NULL;
49
50 typedef struct {
51   GdkPixbuf *pixbuf;
52   mapcache_extra_t extra;
53 } cache_item_t;
54
55 static GMutex *mc_mutex = NULL;
56
57 #define HASHKEY_FORMAT_STRING "%d-%d-%d-%d-%d-%d-%d-%.3f-%.3f"
58 #define HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA "%d-%d-%d-%d-%d-%d-"
59 #define HASHKEY_FORMAT_STRING_TYPE "%d-"
60
61 static VikLayerParamScale params_scales[] = {
62   /* min, max, step, digits (decimal places) */
63  { 1, 1024, 1, 0 },
64 };
65
66 static VikLayerParam prefs[] = {
67   { VIK_LAYER_NUM_TYPES, VIKING_PREFERENCES_NAMESPACE "mapcache_size", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, N_("Map cache memory size (MB):"), VIK_LAYER_WIDGET_HSCALE, params_scales, NULL, NULL, NULL, NULL, NULL },
68 };
69
70 static void cache_item_free (cache_item_t *ci)
71 {
72   g_object_unref ( ci->pixbuf );
73   g_free ( ci );
74 }
75
76 void a_mapcache_init ()
77 {
78   VikLayerParamData tmp;
79   tmp.u = VIK_CONFIG_MAPCACHE_SIZE;
80   a_preferences_register(prefs, tmp, VIKING_PREFERENCES_GROUP_KEY);
81
82   mc_mutex = vik_mutex_new ();
83   cache = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, (GDestroyNotify) cache_item_free );
84 }
85
86 static void cache_add(gchar *key, GdkPixbuf *pixbuf, mapcache_extra_t extra)
87 {
88   cache_item_t *ci = g_malloc ( sizeof(cache_item_t) );
89   ci->pixbuf = pixbuf;
90   ci->extra = extra;
91 #if !GLIB_CHECK_VERSION(2,26,0)
92   // Only later versions of GLib actually return a value for this function
93   // Annoyingly the documentation doesn't say anything about this interface change :(
94   if ( g_hash_table_insert ( cache, key, ci ) )
95 #else
96   g_hash_table_insert ( cache, key, ci );
97 #endif
98   {
99     cache_size += gdk_pixbuf_get_rowstride(pixbuf) * gdk_pixbuf_get_height(pixbuf);
100     // ATM size of 'extra' data hardly worth trying to count (compared to pixbuf sizes)
101     // Not sure what this 100 represents anyway - probably a guess at an average pixbuf metadata size
102     cache_size += 100;
103   }
104 }
105
106 static void cache_remove(const gchar *key)
107 {
108   cache_item_t *ci = g_hash_table_lookup ( cache, key );
109   if (ci && ci->pixbuf) {
110     cache_size -= gdk_pixbuf_get_rowstride(ci->pixbuf) * gdk_pixbuf_get_height(ci->pixbuf);
111     cache_size -= 100;
112     g_hash_table_remove ( cache, key );
113   }
114 }
115
116 /* returns key from head, adds on newtailkey to tail. */
117 static gchar *list_shift_add_entry ( gchar *newtailkey )
118 {
119   gchar *oldheadkey = queue_tail->next->key;
120   queue_tail->next->key = newtailkey;
121   queue_tail = queue_tail->next;
122   return oldheadkey;
123 }
124
125 static gchar *list_shift ()
126 {
127   gchar *oldheadkey = queue_tail->next->key;
128   List *oldhead = queue_tail->next;
129   queue_tail->next = queue_tail->next->next;
130   g_free ( oldhead );
131   queue_count--;
132   return oldheadkey;
133 }
134
135 /* adds key to tail */
136 static void list_add_entry ( gchar *key )
137 {
138   List *newlist = g_malloc ( sizeof ( List ) );
139   newlist->key = key;
140   if ( queue_tail ) {
141     newlist->next = queue_tail->next;
142     queue_tail->next = newlist;
143     queue_tail = newlist;
144   } else {
145     newlist->next = newlist;
146     queue_tail = newlist;
147   }
148   queue_count++;
149 }
150
151 void a_mapcache_add ( GdkPixbuf *pixbuf, mapcache_extra_t extra, gint x, gint y, gint z, guint16 type, gint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor, const gchar* name )
152 {
153   guint nn = name ? g_str_hash ( name ) : 0;
154   gchar *key = g_strdup_printf ( HASHKEY_FORMAT_STRING, type, x, y, z, zoom, nn, alpha, xshrinkfactor, yshrinkfactor );
155
156   g_mutex_lock(mc_mutex);
157   cache_add(key, pixbuf, extra);
158
159   // TODO: that should be done on preference change only...
160   max_cache_size = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "mapcache_size")->u * 1024 * 1024;
161
162   if ( cache_size > max_cache_size ) {
163     if ( queue_tail ) {
164       gchar *oldkey = list_shift_add_entry ( key );
165       cache_remove(oldkey);
166
167       while ( cache_size > max_cache_size &&
168              (queue_tail->next != queue_tail) ) { /* make sure there's more than one thing to delete */
169         oldkey = list_shift ();
170         cache_remove(oldkey);
171       }
172     }
173     /* chop off 'start' etc */
174   } else {
175     list_add_entry ( key );
176     /* business as usual */
177   }
178   g_mutex_unlock(mc_mutex);
179
180   static int tmp = 0;
181   if ( (++tmp == 100 )) { g_debug("DEBUG: cache count=%d size=%u list count=%d\n", g_hash_table_size(cache), cache_size, queue_count ); tmp=0; }
182 }
183
184 GdkPixbuf *a_mapcache_get ( gint x, gint y, gint z, guint16 type, gint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor, const gchar* name )
185 {
186   static char key[MC_KEY_SIZE];
187   guint nn = name ? g_str_hash ( name ) : 0;
188   g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING, type, x, y, z, zoom, nn, alpha, xshrinkfactor, yshrinkfactor );
189   cache_item_t *ci = g_hash_table_lookup ( cache, key );
190   if ( ci )
191     return ci->pixbuf;
192   else
193     return NULL;
194 }
195
196 mapcache_extra_t a_mapcache_get_extra ( gint x, gint y, gint z, guint16 type, gint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor, const gchar* name )
197 {
198   static char key[MC_KEY_SIZE];
199   guint nn = name ? g_str_hash ( name ) : 0;
200   g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING, type, x, y, z, zoom, nn, alpha, xshrinkfactor, yshrinkfactor );
201   cache_item_t *ci = g_hash_table_lookup ( cache, key );
202   if ( ci )
203     return ci->extra;
204   else
205     return (mapcache_extra_t) { 0.0 };
206 }
207
208 /**
209  * Common function to remove cache items for keys starting with the specified string
210  */
211 static void flush_matching ( gchar *str )
212 {
213   if ( queue_tail == NULL )
214     return;
215
216   List *loop = queue_tail;
217   List *tmp;
218   gint len = strlen(str);
219
220   g_mutex_lock(mc_mutex);
221   do {
222     tmp = loop->next;
223     if ( tmp ) {
224     if ( strncmp(tmp->key, str, len) == 0 )
225     {
226       cache_remove(tmp->key);
227       if ( tmp == loop ) /* we deleted the last thing in the queue! */
228         loop = queue_tail = NULL;
229       else {
230         loop->next = tmp->next;
231         if ( tmp == queue_tail )
232           queue_tail = tmp->next;
233       }
234       g_free ( tmp );
235       tmp = NULL;
236       queue_count--;
237     }
238     else
239       loop = tmp;
240     } else
241       loop = NULL;
242   } while ( loop && (loop != queue_tail || tmp == NULL) );
243   /* loop thru list, looking for the one, compare first whatever chars */
244
245   cache_remove(str);
246   g_mutex_unlock(mc_mutex);
247 }
248
249 /**
250  * Appears this is only used when redownloading tiles (i.e. to invalidate old images)
251  */
252 void a_mapcache_remove_all_shrinkfactors ( gint x, gint y, gint z, guint16 type, gint zoom, const gchar* name )
253 {
254   char key[MC_KEY_SIZE];
255   guint nn = name ? g_str_hash ( name ) : 0;
256   g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA, type, x, y, z, zoom, nn );
257   flush_matching ( key );
258 }
259
260 void a_mapcache_flush ()
261 {
262   List *loop = queue_tail;
263   List *tmp;
264
265   if ( queue_tail == NULL )
266     return;
267
268   g_mutex_lock(mc_mutex);
269   do {
270     tmp = loop->next;
271     cache_remove(tmp->key);
272     if ( tmp == queue_tail ) /* we deleted the last thing in the queue */
273       loop = queue_tail = NULL;
274     else
275       loop->next = tmp->next;
276     g_free ( tmp );
277     tmp = NULL;
278   } while ( loop );
279
280   g_mutex_unlock(mc_mutex);
281 }
282
283 /**
284  * a_mapcache_flush_type:
285  *  @type: Specified map type
286  *
287  * Just remove cache items for the specified map type
288  *  i.e. all related xyz+zoom+alpha+etc...
289  */
290 void a_mapcache_flush_type ( guint16 type )
291 {
292   char key[MC_KEY_SIZE];
293   g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING_TYPE, type );
294   flush_matching ( key );
295 }
296
297 void a_mapcache_uninit ()
298 {
299   g_hash_table_destroy ( cache );
300   /* free list */
301   cache = NULL;
302   vik_mutex_free (mc_mutex);
303 }
304
305 // Size of mapcache in memory
306 gint a_mapcache_get_size ()
307 {
308   return cache_size;
309 }
310
311 // Count of items in the mapcache
312 gint a_mapcache_get_count ()
313 {
314   return g_hash_table_size ( cache );
315 }