Trim trailing white spaces.
[geeqie.git] / src / filecache.c
index e2ccb0a..3443234 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Geeqie
- * Copyright (C) 2008 The Geeqie Team
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
  * Author: Vladimir Nadvornik
  *
@@ -13,6 +13,9 @@
 #include "main.h"
 #include "filecache.h"
 
+/* Set to TRUE to add file cache dumps to the debug output */
+const gboolean debug_file_cache = FALSE;
+
 /* this implements a simple LRU algorithm */
 
 struct _FileCacheData {
@@ -20,44 +23,62 @@ struct _FileCacheData {
        GList *list;
        gulong max_size;
        gulong size;
-       };
+};
 
 typedef struct _FileCacheEntry FileCacheEntry;
 struct _FileCacheEntry {
        FileData *fd;
        gulong size;
-       };
-
-static gint file_cache_entry_compare_cb(gconstpointer a, gconstpointer b)
-{
-       const FileCacheEntry *fca = a;
-       const FileData *fd = b;
-       if (fca->fd == fd) return 0;
-       return 1;
-}
+};
 
+static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data);
+static void file_cache_remove_fd(FileCacheData *fc, FileData *fd);
 
 FileCacheData *file_cache_new(FileCacheReleaseFunc release, gulong max_size)
 {
        FileCacheData *fc = g_new(FileCacheData, 1);
+
        fc->release = release;
        fc->list = NULL;
        fc->max_size = max_size;
        fc->size = 0;
+
+       file_data_register_notify_func(file_cache_notify_cb, fc, NOTIFY_PRIORITY_HIGH);
+
        return fc;
 }
 
-gint file_cache_get(FileCacheData *fc, FileData *fd)
+gboolean file_cache_get(FileCacheData *fc, FileData *fd)
 {
        GList *work;
-       if ((work = g_list_find_custom(fc->list, fd, file_cache_entry_compare_cb)))
+       
+       g_assert(fc && fd);
+
+       work = fc->list;
+       while (work)
                {
-               fc->list = g_list_remove_link(fc->list, work);
-               fc->list = g_list_concat(work, fc->list);
-               DEBUG_1("cache hit: %s", fd->path);
-               return TRUE;
+               FileCacheEntry *fce = work->data;
+               if (fce->fd == fd)
+                       {
+                       /* entry exists */
+                       DEBUG_2("cache hit: fc=%p %s", fc, fd->path);
+                       if (work == fc->list) return TRUE; /* already at the beginning */
+                       /* move it to the beginning */
+                       DEBUG_2("cache move to front: fc=%p %s", fc, fd->path);
+                       fc->list = g_list_remove_link(fc->list, work);
+                       fc->list = g_list_concat(work, fc->list);
+                       
+                       if (file_data_check_changed_files(fd)) {
+                               /* file has been changed, cance entry is no longer valid */
+                               file_cache_remove_fd(fc, fd);
+                               return FALSE;
+                       }
+                       if (debug_file_cache) file_cache_dump(fc);
+                       return TRUE;
+                       }
+               work = work->next;
                }
-       DEBUG_1("cache miss: %s", fd->path);
+       DEBUG_2("cache miss: fc=%p %s", fc, fd->path);
        return FALSE;
 }
 
@@ -65,16 +86,19 @@ void file_cache_set_size(FileCacheData *fc, gulong size)
 {
        GList *work;
        FileCacheEntry *last_fe;
+
+       if (debug_file_cache) file_cache_dump(fc);
+
        work = g_list_last(fc->list);
        while (fc->size > size && work)
                {
                GList *prev;
                last_fe = work->data;
-               prev = work->prev; 
+               prev = work->prev;
                fc->list = g_list_delete_link(fc->list, work);
                work = prev;
                
-               DEBUG_1("cache remove: %s", last_fe->fd->path);
+               DEBUG_2("file changed - cache remove: fc=%p %s", fc, last_fe->fd->path);
                fc->size -= last_fe->size;
                fc->release(last_fe->fd);
                file_data_unref(last_fe->fd);
@@ -84,17 +108,11 @@ void file_cache_set_size(FileCacheData *fc, gulong size)
 
 void file_cache_put(FileCacheData *fc, FileData *fd, gulong size)
 {
-       GList *work;
        FileCacheEntry *fe;
-       if ((work = g_list_find_custom(fc->list, fd, file_cache_entry_compare_cb)))
-               { 
-               /* entry already exists, move it to the beginning */
-               fc->list = g_list_remove_link(fc->list, work);
-               fc->list = g_list_concat(work, fc->list);
-               return;
-               }
+
+       if (file_cache_get(fc, fd)) return;
        
-       DEBUG_1("cache add: %s", fd->path);
+       DEBUG_2("cache add: fc=%p %s", fc, fd->path);
        fe = g_new(FileCacheEntry, 1);
        fe->fd = file_data_ref(fd);
        fe->size = size;
@@ -120,17 +138,56 @@ void file_cache_set_max_size(FileCacheData *fc, gulong size)
        file_cache_set_size(fc, fc->max_size);
 }
 
-void file_cache_dump(FileCacheData *fc)
+static void file_cache_remove_fd(FileCacheData *fc, FileData *fd)
 {
        GList *work;
+       FileCacheEntry *fe;
+
+       if (debug_file_cache) file_cache_dump(fc);
+
        work = fc->list;
-       
-       DEBUG_1("cache dump: max size:%ld size:%ld", fc->max_size, fc->size);
+       while (work)
+               {
+               GList *current = work;
+               fe = work->data;
+               work = work->next;
+
+               if (fe->fd == fd)
+                       {
+                       fc->list = g_list_delete_link(fc->list, current);
+               
+                       DEBUG_1("cache remove: fc=%p %s", fc, fe->fd->path);
+                       fc->size -= fe->size;
+                       fc->release(fe->fd);
+                       file_data_unref(fe->fd);
+                       g_free(fe);
+                       }
+               }
+}
+
+void file_cache_dump(FileCacheData *fc)
+{
+       GList *work = fc->list;
+       gulong n = 0;
+
+       DEBUG_1("cache dump: fc=%p max size:%ld size:%ld", fc, fc->max_size, fc->size);
                
-       while(work)
+       while (work)
                {
                FileCacheEntry *fe = work->data;
                work = work->next;
-               DEBUG_1("cache entry: %s %ld", fe->fd->path, fe->size);
+               DEBUG_1("cache entry: fc=%p [%lu] %s %ld", fc, ++n, fe->fd->path, fe->size);
+               }
+}
+
+static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data)
+{
+       FileCacheData *fc = data;
+
+       if (type & (NOTIFY_REREAD | NOTIFY_CHANGE)) /* invalidate the entry on each file change */
+               {
+               DEBUG_1("Notify cache: %s %04x", fd->path, type);
+               file_cache_remove_fd(fc, fd);
                }
 }
+/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */