2 * Copyright (C) 2008 - 2016 The Geeqie Team
4 * Author: Vladimir Nadvornik
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.
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.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "filecache.h"
24 /* Set to TRUE to add file cache dumps to the debug output */
25 const gboolean debug_file_cache = FALSE;
27 /* this implements a simple LRU algorithm */
29 struct _FileCacheData {
30 FileCacheReleaseFunc release;
36 typedef struct _FileCacheEntry FileCacheEntry;
37 struct _FileCacheEntry {
42 static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data);
43 static void file_cache_remove_fd(FileCacheData *fc, FileData *fd);
45 FileCacheData *file_cache_new(FileCacheReleaseFunc release, gulong max_size)
47 FileCacheData *fc = g_new(FileCacheData, 1);
49 fc->release = release;
51 fc->max_size = max_size;
54 file_data_register_notify_func(file_cache_notify_cb, fc, NOTIFY_PRIORITY_HIGH);
59 gboolean file_cache_get(FileCacheData *fc, FileData *fd)
68 FileCacheEntry *fce = work->data;
72 DEBUG_2("cache hit: fc=%p %s", fc, fd->path);
73 if (work == fc->list) return TRUE; /* already at the beginning */
74 /* move it to the beginning */
75 DEBUG_2("cache move to front: fc=%p %s", fc, fd->path);
76 fc->list = g_list_remove_link(fc->list, work);
77 fc->list = g_list_concat(work, fc->list);
79 if (file_data_check_changed_files(fd)) {
80 /* file has been changed, cance entry is no longer valid */
81 file_cache_remove_fd(fc, fd);
84 if (debug_file_cache) file_cache_dump(fc);
89 DEBUG_2("cache miss: fc=%p %s", fc, fd->path);
93 void file_cache_set_size(FileCacheData *fc, gulong size)
96 FileCacheEntry *last_fe;
98 if (debug_file_cache) file_cache_dump(fc);
100 work = g_list_last(fc->list);
101 while (fc->size > size && work)
104 last_fe = work->data;
106 fc->list = g_list_delete_link(fc->list, work);
109 DEBUG_2("file changed - cache remove: fc=%p %s", fc, last_fe->fd->path);
110 fc->size -= last_fe->size;
111 fc->release(last_fe->fd);
112 file_data_unref(last_fe->fd);
117 void file_cache_put(FileCacheData *fc, FileData *fd, gulong size)
121 if (file_cache_get(fc, fd)) return;
123 DEBUG_2("cache add: fc=%p %s", fc, fd->path);
124 fe = g_new(FileCacheEntry, 1);
125 fe->fd = file_data_ref(fd);
127 fc->list = g_list_prepend(fc->list, fe);
130 file_cache_set_size(fc, fc->max_size);
133 gulong file_cache_get_max_size(FileCacheData *fc)
138 gulong file_cache_get_size(FileCacheData *fc)
143 void file_cache_set_max_size(FileCacheData *fc, gulong size)
146 file_cache_set_size(fc, fc->max_size);
149 static void file_cache_remove_fd(FileCacheData *fc, FileData *fd)
154 if (debug_file_cache) file_cache_dump(fc);
159 GList *current = work;
165 fc->list = g_list_delete_link(fc->list, current);
167 DEBUG_1("cache remove: fc=%p %s", fc, fe->fd->path);
168 fc->size -= fe->size;
170 file_data_unref(fe->fd);
176 void file_cache_dump(FileCacheData *fc)
178 GList *work = fc->list;
181 DEBUG_1("cache dump: fc=%p max size:%ld size:%ld", fc, fc->max_size, fc->size);
185 FileCacheEntry *fe = work->data;
187 DEBUG_1("cache entry: fc=%p [%lu] %s %ld", fc, ++n, fe->fd->path, fe->size);
191 static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data)
193 FileCacheData *fc = data;
195 if (type & (NOTIFY_REREAD | NOTIFY_CHANGE)) /* invalidate the entry on each file change */
197 DEBUG_1("Notify cache: %s %04x", fd->path, type);
198 file_cache_remove_fd(fc, fd);
201 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */