4c10c0cedeb2c0d1267dd7eaf79da29f406707cf
[geeqie.git] / src / filecache.c
1 /*
2  * Geeqie
3  * Copyright (C) 2008 - 2012 The Geeqie Team
4  *
5  * Author: Vladimir Nadvornik
6  *
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!
10  */
11
12
13 #include "main.h"
14 #include "filecache.h"
15
16 /* Set to TRUE to add file cache dumps to the debug output */
17 const gboolean debug_file_cache = FALSE;
18
19 /* this implements a simple LRU algorithm */
20
21 struct _FileCacheData {
22         FileCacheReleaseFunc release;
23         GList *list;
24         gulong max_size;
25         gulong size;
26 };
27
28 typedef struct _FileCacheEntry FileCacheEntry;
29 struct _FileCacheEntry {
30         FileData *fd;
31         gulong size;
32 };
33
34 static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data);
35 static void file_cache_remove_fd(FileCacheData *fc, FileData *fd);
36
37 FileCacheData *file_cache_new(FileCacheReleaseFunc release, gulong max_size)
38 {
39         FileCacheData *fc = g_new(FileCacheData, 1);
40
41         fc->release = release;
42         fc->list = NULL;
43         fc->max_size = max_size;
44         fc->size = 0;
45
46         file_data_register_notify_func(file_cache_notify_cb, fc, NOTIFY_PRIORITY_HIGH);
47
48         return fc;
49 }
50
51 gboolean file_cache_get(FileCacheData *fc, FileData *fd)
52 {
53         GList *work;
54         
55         g_assert(fc && fd);
56
57         work = fc->list;
58         while (work)
59                 {
60                 FileCacheEntry *fce = work->data;
61                 if (fce->fd == fd)
62                         {
63                         /* entry exists */
64                         DEBUG_2("cache hit: fc=%p %s", fc, fd->path);
65                         if (work == fc->list) return TRUE; /* already at the beginning */
66                         /* move it to the beginning */
67                         DEBUG_2("cache move to front: fc=%p %s", fc, fd->path);
68                         fc->list = g_list_remove_link(fc->list, work);
69                         fc->list = g_list_concat(work, fc->list);
70                         
71                         if (file_data_check_changed_files(fd)) {
72                                 /* file has been changed, cance entry is no longer valid */
73                                 file_cache_remove_fd(fc, fd); 
74                                 return FALSE;
75                         }
76                         if (debug_file_cache) file_cache_dump(fc);
77                         return TRUE;
78                         }
79                 work = work->next;
80                 }
81         DEBUG_2("cache miss: fc=%p %s", fc, fd->path);
82         return FALSE;
83 }
84
85 void file_cache_set_size(FileCacheData *fc, gulong size)
86 {
87         GList *work;
88         FileCacheEntry *last_fe;
89
90         if (debug_file_cache) file_cache_dump(fc);
91
92         work = g_list_last(fc->list);
93         while (fc->size > size && work)
94                 {
95                 GList *prev;
96                 last_fe = work->data;
97                 prev = work->prev;
98                 fc->list = g_list_delete_link(fc->list, work);
99                 work = prev;
100                 
101                 DEBUG_2("file changed - cache remove: fc=%p %s", fc, last_fe->fd->path);
102                 fc->size -= last_fe->size;
103                 fc->release(last_fe->fd);
104                 file_data_unref(last_fe->fd);
105                 g_free(last_fe);
106                 }
107 }
108
109 void file_cache_put(FileCacheData *fc, FileData *fd, gulong size)
110 {
111         FileCacheEntry *fe;
112
113         if (file_cache_get(fc, fd)) return;
114         
115         DEBUG_2("cache add: fc=%p %s", fc, fd->path);
116         fe = g_new(FileCacheEntry, 1);
117         fe->fd = file_data_ref(fd);
118         fe->size = size;
119         fc->list = g_list_prepend(fc->list, fe);
120         fc->size += size;
121         
122         file_cache_set_size(fc, fc->max_size);
123 }
124
125 gulong file_cache_get_max_size(FileCacheData *fc)
126 {
127         return fc->max_size;
128 }
129
130 gulong file_cache_get_size(FileCacheData *fc)
131 {
132         return fc->size;
133 }
134
135 void file_cache_set_max_size(FileCacheData *fc, gulong size)
136 {
137         fc->max_size = size;
138         file_cache_set_size(fc, fc->max_size);
139 }
140
141 static void file_cache_remove_fd(FileCacheData *fc, FileData *fd)
142 {
143         GList *work;
144         FileCacheEntry *fe;
145
146         if (debug_file_cache) file_cache_dump(fc);
147
148         work = fc->list;
149         while (work)
150                 {
151                 GList *current = work;
152                 fe = work->data;
153                 work = work->next;
154
155                 if (fe->fd == fd)
156                         {
157                         fc->list = g_list_delete_link(fc->list, current);
158                 
159                         DEBUG_1("cache remove: fc=%p %s", fc, fe->fd->path);
160                         fc->size -= fe->size;
161                         fc->release(fe->fd);
162                         file_data_unref(fe->fd);
163                         g_free(fe);
164                         }
165                 }
166 }
167
168 void file_cache_dump(FileCacheData *fc)
169 {
170         GList *work = fc->list;
171         gulong n = 0;
172
173         DEBUG_1("cache dump: fc=%p max size:%ld size:%ld", fc, fc->max_size, fc->size);
174                 
175         while (work)
176                 {
177                 FileCacheEntry *fe = work->data;
178                 work = work->next;
179                 DEBUG_1("cache entry: fc=%p [%lu] %s %ld", fc, ++n, fe->fd->path, fe->size);
180                 }
181 }
182
183 static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data)
184 {
185         FileCacheData *fc = data;
186
187         if (type & (NOTIFY_REREAD | NOTIFY_CHANGE)) /* invalidate the entry on each file change */
188                 {
189                 DEBUG_1("Notify cache: %s %04x", fd->path, type);
190                 file_cache_remove_fd(fc, fd);
191                 }
192 }
193 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */