3 * Copyright (C) 2008 - 2009 The Geeqie Team
5 * Author: Vladimir Nadvornik
7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
14 #include "filecache.h"
16 /* Set to TRUE to add file cache dumps to the debug output */
17 const gboolean debug_file_cache = FALSE;
19 /* this implements a simple LRU algorithm */
21 struct _FileCacheData {
22 FileCacheReleaseFunc release;
28 typedef struct _FileCacheEntry FileCacheEntry;
29 struct _FileCacheEntry {
34 static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data);
36 FileCacheData *file_cache_new(FileCacheReleaseFunc release, gulong max_size)
38 FileCacheData *fc = g_new(FileCacheData, 1);
40 fc->release = release;
42 fc->max_size = max_size;
45 file_data_register_notify_func(file_cache_notify_cb, fc, NOTIFY_PRIORITY_HIGH);
50 gboolean file_cache_get(FileCacheData *fc, FileData *fd)
59 FileCacheEntry *fce = work->data;
63 DEBUG_1("cache hit: fc=%p %s", fc, fd->path);
64 if (work == fc->list) return TRUE; /* already at the beginning */
65 /* move it to the beginning */
66 DEBUG_1("cache move to front: fc=%p %s", fc, fd->path);
67 fc->list = g_list_remove_link(fc->list, work);
68 fc->list = g_list_concat(work, fc->list);
70 if (file_data_check_changed_files(fd)) /* this will eventually remove changed files from cache via file_cache_notify_cb */
73 if (debug_file_cache) file_cache_dump(fc);
78 DEBUG_1("cache miss: fc=%p %s", fc, fd->path);
82 void file_cache_set_size(FileCacheData *fc, gulong size)
85 FileCacheEntry *last_fe;
87 if (debug_file_cache) file_cache_dump(fc);
89 work = g_list_last(fc->list);
90 while (fc->size > size && work)
95 fc->list = g_list_delete_link(fc->list, work);
98 DEBUG_1("file changed - cache remove: fc=%p %s", fc, last_fe->fd->path);
99 fc->size -= last_fe->size;
100 fc->release(last_fe->fd);
101 file_data_unref(last_fe->fd);
106 void file_cache_put(FileCacheData *fc, FileData *fd, gulong size)
110 if (file_cache_get(fc, fd)) return;
112 DEBUG_1("cache add: fc=%p %s", fc, fd->path);
113 fe = g_new(FileCacheEntry, 1);
114 fe->fd = file_data_ref(fd);
116 fc->list = g_list_prepend(fc->list, fe);
119 file_cache_set_size(fc, fc->max_size);
122 gulong file_cache_get_max_size(FileCacheData *fc)
127 gulong file_cache_get_size(FileCacheData *fc)
132 void file_cache_set_max_size(FileCacheData *fc, gulong size)
135 file_cache_set_size(fc, fc->max_size);
138 static void file_cache_remove_fd(FileCacheData *fc, FileData *fd)
143 if (debug_file_cache) file_cache_dump(fc);
148 GList *current = work;
154 fc->list = g_list_delete_link(fc->list, current);
156 DEBUG_1("cache remove: fc=%p %s", fc, fe->fd->path);
157 fc->size -= fe->size;
159 file_data_unref(fe->fd);
165 void file_cache_dump(FileCacheData *fc)
167 GList *work = fc->list;
170 DEBUG_1("cache dump: fc=%p max size:%ld size:%ld", fc, fc->max_size, fc->size);
174 FileCacheEntry *fe = work->data;
176 DEBUG_1("cache entry: fc=%p [%lu] %s %ld", fc, ++n, fe->fd->path, fe->size);
180 static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data)
182 FileCacheData *fc = data;
184 if (type & (NOTIFY_REREAD | NOTIFY_CHANGE)) /* invalidate the entry on each file change */
186 file_cache_remove_fd(fc, fd);
189 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */