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.
21 #include "filecache.h"
27 /* Set to TRUE to add file cache dumps to the debug output */
28 const gboolean debug_file_cache = FALSE;
30 /* this implements a simple LRU algorithm */
32 struct FileCacheData {
33 FileCacheReleaseFunc release;
39 struct FileCacheEntry {
44 static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data);
45 static void file_cache_remove_fd(FileCacheData *fc, FileData *fd);
47 FileCacheData *file_cache_new(FileCacheReleaseFunc release, gulong max_size)
49 auto fc = g_new(FileCacheData, 1);
51 fc->release = release;
53 fc->max_size = max_size;
56 file_data_register_notify_func(file_cache_notify_cb, fc, NOTIFY_PRIORITY_HIGH);
61 gboolean file_cache_get(FileCacheData *fc, FileData *fd)
70 auto fce = static_cast<FileCacheEntry *>(work->data);
74 DEBUG_2("cache hit: fc=%p %s", (void *)fc, fd->path);
75 if (work == fc->list) return TRUE; /* already at the beginning */
76 /* move it to the beginning */
77 DEBUG_2("cache move to front: fc=%p %s", (void *)fc, fd->path);
78 fc->list = g_list_remove_link(fc->list, work);
79 fc->list = g_list_concat(work, fc->list);
81 if (file_data_check_changed_files(fd)) {
82 /* file has been changed, cance entry is no longer valid */
83 file_cache_remove_fd(fc, fd);
86 if (debug_file_cache) file_cache_dump(fc);
91 DEBUG_2("cache miss: fc=%p %s", (void *)fc, fd->path);
95 void file_cache_set_size(FileCacheData *fc, gulong size)
98 FileCacheEntry *last_fe;
100 if (debug_file_cache) file_cache_dump(fc);
102 work = g_list_last(fc->list);
103 while (fc->size > size && work)
106 last_fe = static_cast<FileCacheEntry *>(work->data);
108 fc->list = g_list_delete_link(fc->list, work);
111 DEBUG_2("file changed - cache remove: fc=%p %s", (void *)fc, last_fe->fd->path);
112 fc->size -= last_fe->size;
113 fc->release(last_fe->fd);
114 file_data_unref(last_fe->fd);
119 void file_cache_put(FileCacheData *fc, FileData *fd, gulong size)
123 if (file_cache_get(fc, fd)) return;
125 DEBUG_2("cache add: fc=%p %s", (void *)fc, fd->path);
126 fe = g_new(FileCacheEntry, 1);
127 fe->fd = file_data_ref(fd);
129 fc->list = g_list_prepend(fc->list, fe);
132 file_cache_set_size(fc, fc->max_size);
135 #pragma GCC diagnostic push
136 #pragma GCC diagnostic ignored "-Wunused-function"
137 gulong file_cache_get_max_size_unused(FileCacheData *fc)
142 gulong file_cache_get_size_unused(FileCacheData *fc)
146 #pragma GCC diagnostic pop
148 void file_cache_set_max_size(FileCacheData *fc, gulong size)
151 file_cache_set_size(fc, fc->max_size);
154 static void file_cache_remove_fd(FileCacheData *fc, FileData *fd)
159 if (debug_file_cache) file_cache_dump(fc);
164 GList *current = work;
165 fe = static_cast<FileCacheEntry *>(work->data);
170 fc->list = g_list_delete_link(fc->list, current);
172 DEBUG_1("cache remove: fc=%p %s", (void *)fc, fe->fd->path);
173 fc->size -= fe->size;
175 file_data_unref(fe->fd);
181 void file_cache_dump(FileCacheData *fc)
183 GList *work = fc->list;
186 DEBUG_1("cache dump: fc=%p max size:%ld size:%ld", (void *)fc, fc->max_size, fc->size);
190 auto fe = static_cast<FileCacheEntry *>(work->data);
192 DEBUG_1("cache entry: fc=%p [%lu] %s %ld", (void *)fc, ++n, fe->fd->path, fe->size);
196 static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data)
198 auto fc = static_cast<FileCacheData *>(data);
200 if (type & (NOTIFY_REREAD | NOTIFY_CHANGE)) /* invalidate the entry on each file change */
202 DEBUG_1("Notify cache: %s %04x", fd->path, type);
203 file_cache_remove_fd(fc, fd);
206 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */