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 struct FileCacheEntry {
41 static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data);
42 static void file_cache_remove_fd(FileCacheData *fc, FileData *fd);
44 FileCacheData *file_cache_new(FileCacheReleaseFunc release, gulong max_size)
46 auto fc = g_new(FileCacheData, 1);
48 fc->release = release;
50 fc->max_size = max_size;
53 file_data_register_notify_func(file_cache_notify_cb, fc, NOTIFY_PRIORITY_HIGH);
58 gboolean file_cache_get(FileCacheData *fc, FileData *fd)
67 auto fce = static_cast<FileCacheEntry *>(work->data);
71 DEBUG_2("cache hit: fc=%p %s", (void *)fc, fd->path);
72 if (work == fc->list) return TRUE; /* already at the beginning */
73 /* move it to the beginning */
74 DEBUG_2("cache move to front: fc=%p %s", (void *)fc, fd->path);
75 fc->list = g_list_remove_link(fc->list, work);
76 fc->list = g_list_concat(work, fc->list);
78 if (file_data_check_changed_files(fd)) {
79 /* file has been changed, cance entry is no longer valid */
80 file_cache_remove_fd(fc, fd);
83 if (debug_file_cache) file_cache_dump(fc);
88 DEBUG_2("cache miss: fc=%p %s", (void *)fc, fd->path);
92 void file_cache_set_size(FileCacheData *fc, gulong size)
95 FileCacheEntry *last_fe;
97 if (debug_file_cache) file_cache_dump(fc);
99 work = g_list_last(fc->list);
100 while (fc->size > size && work)
103 last_fe = static_cast<FileCacheEntry *>(work->data);
105 fc->list = g_list_delete_link(fc->list, work);
108 DEBUG_2("file changed - cache remove: fc=%p %s", (void *)fc, last_fe->fd->path);
109 fc->size -= last_fe->size;
110 fc->release(last_fe->fd);
111 file_data_unref(last_fe->fd);
116 void file_cache_put(FileCacheData *fc, FileData *fd, gulong size)
120 if (file_cache_get(fc, fd)) return;
122 DEBUG_2("cache add: fc=%p %s", (void *)fc, fd->path);
123 fe = g_new(FileCacheEntry, 1);
124 fe->fd = file_data_ref(fd);
126 fc->list = g_list_prepend(fc->list, fe);
129 file_cache_set_size(fc, fc->max_size);
132 #pragma GCC diagnostic push
133 #pragma GCC diagnostic ignored "-Wunused-function"
134 gulong file_cache_get_max_size_unused(FileCacheData *fc)
139 gulong file_cache_get_size_unused(FileCacheData *fc)
143 #pragma GCC diagnostic pop
145 void file_cache_set_max_size(FileCacheData *fc, gulong size)
148 file_cache_set_size(fc, fc->max_size);
151 static void file_cache_remove_fd(FileCacheData *fc, FileData *fd)
156 if (debug_file_cache) file_cache_dump(fc);
161 GList *current = work;
162 fe = static_cast<FileCacheEntry *>(work->data);
167 fc->list = g_list_delete_link(fc->list, current);
169 DEBUG_1("cache remove: fc=%p %s", (void *)fc, fe->fd->path);
170 fc->size -= fe->size;
172 file_data_unref(fe->fd);
178 void file_cache_dump(FileCacheData *fc)
180 GList *work = fc->list;
183 DEBUG_1("cache dump: fc=%p max size:%ld size:%ld", (void *)fc, fc->max_size, fc->size);
187 auto fe = static_cast<FileCacheEntry *>(work->data);
189 DEBUG_1("cache entry: fc=%p [%lu] %s %ld", (void *)fc, ++n, fe->fd->path, fe->size);
193 static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data)
195 auto fc = static_cast<FileCacheData *>(data);
197 if (type & (NOTIFY_REREAD | NOTIFY_CHANGE)) /* invalidate the entry on each file change */
199 DEBUG_1("Notify cache: %s %04x", fd->path, type);
200 file_cache_remove_fd(fc, fd);
203 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */