]> git.street.me.uk Git - andy/viking.git/blame - src/mapcache.c
Fix stdout/stderr variable usage.
[andy/viking.git] / src / mapcache.c
CommitLineData
50a14534
EB
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 */
065a9ace
GB
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
50a14534 24
e8518759 25#include <glib.h>
9de4d421 26#include <glib/gi18n.h>
50a14534 27#include <string.h>
44b37676 28#include "globals.h"
50a14534 29#include "mapcache.h"
9de4d421 30#include "preferences.h"
50a14534 31
50a14534
EB
32typedef struct _List {
33 struct _List *next;
34 gchar *key;
35} List;
36
37/* a circular linked list, a pointer to the tail, and the tail points to the head */
38/* this is so we can free the last */
39static List *queue_tail = NULL;
40static int queue_count = 0;
41
e8518759
RN
42static guint32 cache_size = 0;
43static guint32 max_cache_size = VIK_CONFIG_MAPCACHE_SIZE * 1024 * 1024;
50a14534
EB
44
45static GHashTable *cache = NULL;
46
9b79169d
QT
47static GMutex *mc_mutex = NULL;
48
df5b2993 49#define HASHKEY_FORMAT_STRING "%d-%d-%d-%d-%d-%d-%d-%.3f-%.3f"
57c1c96a 50#define HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA "%d-%d-%d-%d-%d-%d-"
50a14534 51
9de4d421
JJ
52static VikLayerParamScale params_scales[] = {
53 /* min, max, step, digits (decimal places) */
e6e277a8 54 { 1, 1024, 1, 0 },
9de4d421
JJ
55};
56
57static VikLayerParam prefs[] = {
63959706 58 { 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 },
9de4d421
JJ
59};
60
50a14534
EB
61void a_mapcache_init ()
62{
9de4d421 63 VikLayerParamData tmp;
e6e277a8 64 tmp.u = VIK_CONFIG_MAPCACHE_SIZE;
9de4d421
JJ
65 a_preferences_register(prefs, tmp, VIKING_PREFERENCES_GROUP_KEY);
66
9b79169d 67 mc_mutex = g_mutex_new();
50a14534
EB
68 cache = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, g_object_unref );
69}
70
fd4536cf
QT
71static void cache_add(gchar *key, GdkPixbuf *pixbuf)
72{
02a5b4d7
RN
73#if !GLIB_CHECK_VERSION(2,26,0)
74 // Only later versions of GLib actually return a value for this function
75 // Annoyingly the documentation doesn't say anything about this interface change :(
76 if ( g_hash_table_insert ( cache, key, pixbuf ) )
77#else
78 g_hash_table_insert ( cache, key, pixbuf );
79#endif
80 {
e8518759
RN
81 cache_size += gdk_pixbuf_get_rowstride(pixbuf) * gdk_pixbuf_get_height(pixbuf);
82 cache_size += 100;
83 }
fd4536cf
QT
84}
85
86static void cache_remove(const gchar *key)
87{
e8518759
RN
88 GdkPixbuf *buf = g_hash_table_lookup ( cache, key );
89 if (buf) {
90 cache_size -= gdk_pixbuf_get_rowstride(buf) * gdk_pixbuf_get_height(buf);
91 cache_size -= 100;
92 g_hash_table_remove ( cache, key );
93 }
fd4536cf
QT
94}
95
50a14534
EB
96/* returns key from head, adds on newtailkey to tail. */
97static gchar *list_shift_add_entry ( gchar *newtailkey )
98{
99 gchar *oldheadkey = queue_tail->next->key;
100 queue_tail->next->key = newtailkey;
101 queue_tail = queue_tail->next;
102 return oldheadkey;
103}
104
105static gchar *list_shift ()
106{
107 gchar *oldheadkey = queue_tail->next->key;
108 List *oldhead = queue_tail->next;
109 queue_tail->next = queue_tail->next->next;
110 g_free ( oldhead );
e8518759 111 queue_count--;
50a14534
EB
112 return oldheadkey;
113}
114
115/* adds key to tail */
116static void list_add_entry ( gchar *key )
117{
118 List *newlist = g_malloc ( sizeof ( List ) );
119 newlist->key = key;
120 if ( queue_tail ) {
121 newlist->next = queue_tail->next;
122 queue_tail->next = newlist;
123 queue_tail = newlist;
124 } else {
125 newlist->next = newlist;
126 queue_tail = newlist;
127 }
e8518759 128 queue_count++;
50a14534
EB
129}
130
b7464e99 131void a_mapcache_add ( GdkPixbuf *pixbuf, gint x, gint y, gint z, guint16 type, gint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor, const gchar* name )
50a14534 132{
df5b2993
RN
133 guint nn = name ? g_str_hash ( name ) : 0;
134 gchar *key = g_strdup_printf ( HASHKEY_FORMAT_STRING, x, y, z, type, zoom, nn, alpha, xshrinkfactor, yshrinkfactor );
50a14534 135
9b79169d 136 g_mutex_lock(mc_mutex);
fd4536cf 137 cache_add(key, pixbuf);
50a14534 138
9de4d421 139 // TODO: that should be done on preference change only...
e8518759 140 max_cache_size = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "mapcache_size")->u * 1024 * 1024;
9de4d421 141
e8518759 142 if ( cache_size > max_cache_size ) {
df05b6e9
RN
143 if ( queue_tail ) {
144 gchar *oldkey = list_shift_add_entry ( key );
fd4536cf 145 cache_remove(oldkey);
50a14534 146
e8518759 147 while ( cache_size > max_cache_size &&
df05b6e9
RN
148 (queue_tail->next != queue_tail) ) { /* make sure there's more than one thing to delete */
149 oldkey = list_shift ();
150 cache_remove(oldkey);
151 }
152 }
50a14534
EB
153 /* chop off 'start' etc */
154 } else {
155 list_add_entry ( key );
156 /* business as usual */
157 }
9b79169d 158 g_mutex_unlock(mc_mutex);
50a14534 159
a3040a49
RN
160 static int tmp = 0;
161 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; }
50a14534
EB
162}
163
b7464e99 164GdkPixbuf *a_mapcache_get ( gint x, gint y, gint z, guint16 type, gint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor, const gchar* name )
50a14534 165{
df5b2993
RN
166 static char key[64];
167 guint nn = name ? g_str_hash ( name ) : 0;
168 g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING, x, y, z, type, zoom, nn, alpha, xshrinkfactor, yshrinkfactor );
50a14534
EB
169 return g_hash_table_lookup ( cache, key );
170}
171
df5b2993
RN
172/**
173 * Appears this is only used when redownloading tiles (i.e. to invalidate old images)
174 */
b7464e99 175void a_mapcache_remove_all_shrinkfactors ( gint x, gint y, gint z, guint16 type, gint zoom )
50a14534 176{
df5b2993 177 char key[64];
50a14534
EB
178 List *loop = queue_tail;
179 List *tmp;
180 gint len;
181
182 if ( queue_tail == NULL )
183 return;
184
57c1c96a 185 g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA, x, y, z, type, zoom, 0 );
50a14534
EB
186 len = strlen(key);
187
9b79169d 188 g_mutex_lock(mc_mutex);
50a14534
EB
189 /* TODO: check logic here */
190 do {
191 tmp = loop->next;
2ae6f542 192 if ( tmp ) {
50a14534
EB
193 if ( strncmp(tmp->key, key, len) == 0 )
194 {
fd4536cf 195 cache_remove(tmp->key);
093c5c71 196 if ( tmp == loop ) /* we deleted the last thing in the queue! */
50a14534 197 loop = queue_tail = NULL;
093c5c71 198 else {
50a14534 199 loop->next = tmp->next;
093c5c71
EB
200 if ( tmp == queue_tail )
201 queue_tail = tmp->next;
202 }
50a14534
EB
203 g_free ( tmp );
204 tmp = NULL;
e8518759 205 queue_count--;
50a14534
EB
206 }
207 else
208 loop = tmp;
2ae6f542
RN
209 } else
210 loop = NULL;
50a14534
EB
211 } while ( loop && (loop != queue_tail || tmp == NULL) );
212
213 /* loop thru list, looking for the one, compare first whatever chars */
fd4536cf 214 cache_remove(key);
9b79169d 215 g_mutex_unlock(mc_mutex);
50a14534
EB
216}
217
c57a0943
JJ
218void a_mapcache_flush ()
219{
220 List *loop = queue_tail;
221 List *tmp;
222
223 if ( queue_tail == NULL )
224 return;
225
226 g_mutex_lock(mc_mutex);
227 do {
228 tmp = loop->next;
229 cache_remove(tmp->key);
230 if ( tmp == queue_tail ) /* we deleted the last thing in the queue */
231 loop = queue_tail = NULL;
232 else
233 loop->next = tmp->next;
234 g_free ( tmp );
235 tmp = NULL;
236 } while ( loop );
237
238 g_mutex_unlock(mc_mutex);
239}
240
50a14534
EB
241void a_mapcache_uninit ()
242{
243 g_hash_table_destroy ( cache );
244 /* free list */
245 cache = NULL;
246}
a3040a49
RN
247
248// Size of mapcache in memory
249gint a_mapcache_get_size ()
250{
251 return cache_size;
252}
253
254// Count of items in the mapcache
255gint a_mapcache_get_count ()
256{
257 return g_hash_table_size ( cache );
258}