Blind fix macOS build
[geeqie.git] / src / filecache.cc
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Author: Vladimir Nadvornik
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "filecache.h"
22
23 #include "debug.h"
24 #include "filedata.h"
25 #include "typedefs.h"
26
27 /* Set to TRUE to add file cache dumps to the debug output */
28 const gboolean debug_file_cache = FALSE;
29
30 /* this implements a simple LRU algorithm */
31
32 struct FileCacheData {
33         FileCacheReleaseFunc release;
34         GList *list;
35         gulong max_size;
36         gulong size;
37 };
38
39 struct FileCacheEntry {
40         FileData *fd;
41         gulong size;
42 };
43
44 static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data);
45 static void file_cache_remove_fd(FileCacheData *fc, FileData *fd);
46
47 FileCacheData *file_cache_new(FileCacheReleaseFunc release, gulong max_size)
48 {
49         auto fc = g_new(FileCacheData, 1);
50
51         fc->release = release;
52         fc->list = nullptr;
53         fc->max_size = max_size;
54         fc->size = 0;
55
56         file_data_register_notify_func(file_cache_notify_cb, fc, NOTIFY_PRIORITY_HIGH);
57
58         return fc;
59 }
60
61 gboolean file_cache_get(FileCacheData *fc, FileData *fd)
62 {
63         GList *work;
64
65         g_assert(fc && fd);
66
67         work = fc->list;
68         while (work)
69                 {
70                 auto fce = static_cast<FileCacheEntry *>(work->data);
71                 if (fce->fd == fd)
72                         {
73                         /* entry exists */
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);
80
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);
84                                 return FALSE;
85                         }
86                         if (debug_file_cache) file_cache_dump(fc);
87                         return TRUE;
88                         }
89                 work = work->next;
90                 }
91         DEBUG_2("cache miss: fc=%p %s", (void *)fc, fd->path);
92         return FALSE;
93 }
94
95 void file_cache_set_size(FileCacheData *fc, gulong size)
96 {
97         GList *work;
98         FileCacheEntry *last_fe;
99
100         if (debug_file_cache) file_cache_dump(fc);
101
102         work = g_list_last(fc->list);
103         while (fc->size > size && work)
104                 {
105                 GList *prev;
106                 last_fe = static_cast<FileCacheEntry *>(work->data);
107                 prev = work->prev;
108                 fc->list = g_list_delete_link(fc->list, work);
109                 work = prev;
110
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);
115                 g_free(last_fe);
116                 }
117 }
118
119 void file_cache_put(FileCacheData *fc, FileData *fd, gulong size)
120 {
121         FileCacheEntry *fe;
122
123         if (file_cache_get(fc, fd)) return;
124
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);
128         fe->size = size;
129         fc->list = g_list_prepend(fc->list, fe);
130         fc->size += size;
131
132         file_cache_set_size(fc, fc->max_size);
133 }
134
135 #pragma GCC diagnostic push
136 #pragma GCC diagnostic ignored "-Wunused-function"
137 gulong file_cache_get_max_size_unused(FileCacheData *fc)
138 {
139         return fc->max_size;
140 }
141
142 gulong file_cache_get_size_unused(FileCacheData *fc)
143 {
144         return fc->size;
145 }
146 #pragma GCC diagnostic pop
147
148 void file_cache_set_max_size(FileCacheData *fc, gulong size)
149 {
150         fc->max_size = size;
151         file_cache_set_size(fc, fc->max_size);
152 }
153
154 static void file_cache_remove_fd(FileCacheData *fc, FileData *fd)
155 {
156         GList *work;
157         FileCacheEntry *fe;
158
159         if (debug_file_cache) file_cache_dump(fc);
160
161         work = fc->list;
162         while (work)
163                 {
164                 GList *current = work;
165                 fe = static_cast<FileCacheEntry *>(work->data);
166                 work = work->next;
167
168                 if (fe->fd == fd)
169                         {
170                         fc->list = g_list_delete_link(fc->list, current);
171
172                         DEBUG_1("cache remove: fc=%p %s", (void *)fc, fe->fd->path);
173                         fc->size -= fe->size;
174                         fc->release(fe->fd);
175                         file_data_unref(fe->fd);
176                         g_free(fe);
177                         }
178                 }
179 }
180
181 void file_cache_dump(FileCacheData *fc)
182 {
183         GList *work = fc->list;
184         gulong n = 0;
185
186         DEBUG_1("cache dump: fc=%p max size:%ld size:%ld", (void *)fc, fc->max_size, fc->size);
187
188         while (work)
189                 {
190                 auto fe = static_cast<FileCacheEntry *>(work->data);
191                 work = work->next;
192                 DEBUG_1("cache entry: fc=%p [%lu] %s %ld", (void *)fc, ++n, fe->fd->path, fe->size);
193                 }
194 }
195
196 static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data)
197 {
198         auto fc = static_cast<FileCacheData *>(data);
199
200         if (type & (NOTIFY_REREAD | NOTIFY_CHANGE)) /* invalidate the entry on each file change */
201                 {
202                 DEBUG_1("Notify cache: %s %04x", fd->path, type);
203                 file_cache_remove_fd(fc, fd);
204                 }
205 }
206 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */