]> git.street.me.uk Git - andy/viking.git/blobdiff - src/mapcache.c
Rename file to prevent any case-insensitive issues.
[andy/viking.git] / src / mapcache.c
index 3dd8023a118d7146d9f89cee08a00d44f7584aa5..5485f1789b719d9e08a8d6a4d0d0f85d47736217 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 
 #include <gtk/gtk.h>
 
 #include <gtk/gtk.h>
+#include <glib/gi18n.h>
 #include <string.h>
 #include <string.h>
+#include "globals.h"
 #include "mapcache.h"
 #include "mapcache.h"
+#include "preferences.h"
 
 #include "config.h"
 
 
 #include "config.h"
 
@@ -36,15 +42,32 @@ static List *queue_tail = NULL;
 static int queue_count = 0;
 
 static guint32 queue_size = 0;
 static int queue_count = 0;
 
 static guint32 queue_size = 0;
+static guint32 max_queue_size = VIK_CONFIG_MAPCACHE_SIZE;
 
 
 static GHashTable *cache = NULL;
 
 
 
 static GHashTable *cache = NULL;
 
+static GMutex *mc_mutex = NULL;
+
 #define HASHKEY_FORMAT_STRING "%d-%d-%d-%d-%d-%d-%.3f-%.3f"
 #define HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA "%d-%d-%d-%d-%d-"
 
 #define HASHKEY_FORMAT_STRING "%d-%d-%d-%d-%d-%d-%.3f-%.3f"
 #define HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA "%d-%d-%d-%d-%d-"
 
+static VikLayerParamScale params_scales[] = {
+  /* min, max, step, digits (decimal places) */
+ { 1, 300, 1, 0 },
+};
+
+static VikLayerParam prefs[] = {
+  { 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 },
+};
+
 void a_mapcache_init ()
 {
 void a_mapcache_init ()
 {
+  VikLayerParamData tmp;
+  tmp.u = VIK_CONFIG_MAPCACHE_SIZE / 1024 / 1024;
+  a_preferences_register(prefs, tmp, VIKING_PREFERENCES_GROUP_KEY);
+
+  mc_mutex = g_mutex_new();
   cache = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, g_object_unref );
 }
 
   cache = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, g_object_unref );
 }
 
@@ -106,13 +129,17 @@ void a_mapcache_add ( GdkPixbuf *pixbuf, gint x, gint y, gint z, guint8 type, gu
   gchar *key = g_strdup_printf ( HASHKEY_FORMAT_STRING, x, y, z, type, zoom, alpha, xshrinkfactor, yshrinkfactor );
   static int tmp = 0;
 
   gchar *key = g_strdup_printf ( HASHKEY_FORMAT_STRING, x, y, z, type, zoom, alpha, xshrinkfactor, yshrinkfactor );
   static int tmp = 0;
 
+  g_mutex_lock(mc_mutex);
   cache_add(key, pixbuf);
 
   cache_add(key, pixbuf);
 
-  if ( queue_size > VIK_CONFIG_MAPCACHE_SIZE ) {
+  // TODO: that should be done on preference change only...
+  max_queue_size = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "mapcache_size")->u * 1024 * 1024;
+
+  if ( queue_size > max_queue_size ) {
     gchar *oldkey = list_shift_add_entry ( key );
     cache_remove(oldkey);
 
     gchar *oldkey = list_shift_add_entry ( key );
     cache_remove(oldkey);
 
-    while ( queue_size > VIK_CONFIG_MAPCACHE_SIZE &&
+    while ( queue_size > max_queue_size &&
         (queue_tail->next != queue_tail) ) { /* make sure there's more than one thing to delete */
       oldkey = list_shift ();
       cache_remove(oldkey);
         (queue_tail->next != queue_tail) ) { /* make sure there's more than one thing to delete */
       oldkey = list_shift ();
       cache_remove(oldkey);
@@ -123,8 +150,9 @@ void a_mapcache_add ( GdkPixbuf *pixbuf, gint x, gint y, gint z, guint8 type, gu
     list_add_entry ( key );
     /* business as usual */
   }
     list_add_entry ( key );
     /* business as usual */
   }
+  g_mutex_unlock(mc_mutex);
 
 
-  if ( (++tmp == 100 ))  { g_print("DEBUG: queue count=%d %u\n", queue_count, queue_size ); tmp=0; }
+  if ( (++tmp == 100 ))  { g_print("DEBUG: queue count=%d size=%u\n", queue_count, queue_size ); tmp=0; }
 }
 
 GdkPixbuf *a_mapcache_get ( gint x, gint y, gint z, guint8 type, guint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor )
 }
 
 GdkPixbuf *a_mapcache_get ( gint x, gint y, gint z, guint8 type, guint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor )
@@ -147,6 +175,7 @@ void a_mapcache_remove_all_shrinkfactors ( gint x, gint y, gint z, guint8 type,
   g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA, x, y, z, type, zoom );
   len = strlen(key);
 
   g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA, x, y, z, type, zoom );
   len = strlen(key);
 
+  g_mutex_lock(mc_mutex);
   /* TODO: check logic here */
   do {
     tmp = loop->next;
   /* TODO: check logic here */
   do {
     tmp = loop->next;
@@ -170,6 +199,30 @@ void a_mapcache_remove_all_shrinkfactors ( gint x, gint y, gint z, guint8 type,
 
   /* loop thru list, looking for the one, compare first whatever chars */
   cache_remove(key);
 
   /* loop thru list, looking for the one, compare first whatever chars */
   cache_remove(key);
+  g_mutex_unlock(mc_mutex);
+}
+
+void a_mapcache_flush ()
+{
+  List *loop = queue_tail;
+  List *tmp;
+
+  if ( queue_tail == NULL )
+    return;
+
+  g_mutex_lock(mc_mutex);
+  do {
+    tmp = loop->next;
+    cache_remove(tmp->key);
+    if ( tmp == queue_tail ) /* we deleted the last thing in the queue */
+      loop = queue_tail = NULL;
+    else
+      loop->next = tmp->next;
+    g_free ( tmp );
+    tmp = NULL;
+  } while ( loop );
+
+  g_mutex_unlock(mc_mutex);
 }
 
 void a_mapcache_uninit ()
 }
 
 void a_mapcache_uninit ()