annotate src/filecache.c @ 1802:956aab097ea7

added 2010 to copyright text
author nadvornik
date Tue, 16 Feb 2010 21:18:03 +0000
parents 5f49f305a6b6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
1 /*
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
2 * Geeqie
1802
956aab097ea7 added 2010 to copyright text
nadvornik
parents: 1498
diff changeset
3 * Copyright (C) 2008 - 2010 The Geeqie Team
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
4 *
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
5 * Author: Vladimir Nadvornik
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
6 *
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
7 * This software is released under the GNU General Public License (GNU GPL).
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
8 * Please read the included file COPYING for more information.
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
9 * This software comes with no warranty of any kind, use at your own risk!
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
10 */
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
11
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
12
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
13 #include "main.h"
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
14 #include "filecache.h"
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
15
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
16 /* Set to TRUE to add file cache dumps to the debug output */
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
17 const gboolean debug_file_cache = FALSE;
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
18
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
19 /* this implements a simple LRU algorithm */
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
20
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
21 struct _FileCacheData {
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
22 FileCacheReleaseFunc release;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
23 GList *list;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
24 gulong max_size;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
25 gulong size;
1422
91bed0d66cf2 gint -> gboolean and tidy up.
zas_
parents: 1284
diff changeset
26 };
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
27
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
28 typedef struct _FileCacheEntry FileCacheEntry;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
29 struct _FileCacheEntry {
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
30 FileData *fd;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
31 gulong size;
1422
91bed0d66cf2 gint -> gboolean and tidy up.
zas_
parents: 1284
diff changeset
32 };
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
33
888
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
34 static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data);
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
35
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
36 FileCacheData *file_cache_new(FileCacheReleaseFunc release, gulong max_size)
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
37 {
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
38 FileCacheData *fc = g_new(FileCacheData, 1);
1422
91bed0d66cf2 gint -> gboolean and tidy up.
zas_
parents: 1284
diff changeset
39
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
40 fc->release = release;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
41 fc->list = NULL;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
42 fc->max_size = max_size;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
43 fc->size = 0;
888
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
44
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
45 file_data_register_notify_func(file_cache_notify_cb, fc, NOTIFY_PRIORITY_HIGH);
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
46
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
47 return fc;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
48 }
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
49
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
50 gboolean file_cache_get(FileCacheData *fc, FileData *fd)
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
51 {
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
52 GList *work;
855
0c3f6ef17d18 Tidy up.
zas_
parents: 847
diff changeset
53
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
54 g_assert(fc && fd);
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
55
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
56 work = fc->list;
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
57 while (work)
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
58 {
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
59 FileCacheEntry *fce = work->data;
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
60 if (fce->fd == fd)
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
61 {
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
62 /* entry exists */
1498
5f49f305a6b6 improved debug messages
nadvornik
parents: 1432
diff changeset
63 DEBUG_2("cache hit: fc=%p %s", fc, fd->path);
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
64 if (work == fc->list) return TRUE; /* already at the beginning */
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
65 /* move it to the beginning */
1498
5f49f305a6b6 improved debug messages
nadvornik
parents: 1432
diff changeset
66 DEBUG_2("cache move to front: fc=%p %s", fc, fd->path);
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
67 fc->list = g_list_remove_link(fc->list, work);
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
68 fc->list = g_list_concat(work, fc->list);
888
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
69
891
841b387dd877 fixed test for changed files in cache
nadvornik
parents: 888
diff changeset
70 if (file_data_check_changed_files(fd)) /* this will eventually remove changed files from cache via file_cache_notify_cb */
841b387dd877 fixed test for changed files in cache
nadvornik
parents: 888
diff changeset
71 return FALSE;
888
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
72
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
73 if (debug_file_cache) file_cache_dump(fc);
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
74 return TRUE;
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
75 }
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
76 work = work->next;
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
77 }
1498
5f49f305a6b6 improved debug messages
nadvornik
parents: 1432
diff changeset
78 DEBUG_2("cache miss: fc=%p %s", fc, fd->path);
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
79 return FALSE;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
80 }
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
81
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
82 void file_cache_set_size(FileCacheData *fc, gulong size)
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
83 {
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
84 GList *work;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
85 FileCacheEntry *last_fe;
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
86
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
87 if (debug_file_cache) file_cache_dump(fc);
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
88
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
89 work = g_list_last(fc->list);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
90 while (fc->size > size && work)
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
91 {
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
92 GList *prev;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
93 last_fe = work->data;
995
6ca2c5fd7b13 Whitespaces cleanup.
zas_
parents: 893
diff changeset
94 prev = work->prev;
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
95 fc->list = g_list_delete_link(fc->list, work);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
96 work = prev;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
97
1498
5f49f305a6b6 improved debug messages
nadvornik
parents: 1432
diff changeset
98 DEBUG_2("file changed - cache remove: fc=%p %s", fc, last_fe->fd->path);
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
99 fc->size -= last_fe->size;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
100 fc->release(last_fe->fd);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
101 file_data_unref(last_fe->fd);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
102 g_free(last_fe);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
103 }
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
104 }
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
105
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
106 void file_cache_put(FileCacheData *fc, FileData *fd, gulong size)
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
107 {
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
108 FileCacheEntry *fe;
855
0c3f6ef17d18 Tidy up.
zas_
parents: 847
diff changeset
109
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
110 if (file_cache_get(fc, fd)) return;
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
111
1498
5f49f305a6b6 improved debug messages
nadvornik
parents: 1432
diff changeset
112 DEBUG_2("cache add: fc=%p %s", fc, fd->path);
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
113 fe = g_new(FileCacheEntry, 1);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
114 fe->fd = file_data_ref(fd);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
115 fe->size = size;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
116 fc->list = g_list_prepend(fc->list, fe);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
117 fc->size += size;
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
118
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
119 file_cache_set_size(fc, fc->max_size);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
120 }
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
121
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
122 gulong file_cache_get_max_size(FileCacheData *fc)
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
123 {
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
124 return fc->max_size;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
125 }
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
126
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
127 gulong file_cache_get_size(FileCacheData *fc)
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
128 {
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
129 return fc->size;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
130 }
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
131
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
132 void file_cache_set_max_size(FileCacheData *fc, gulong size)
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
133 {
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
134 fc->max_size = size;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
135 file_cache_set_size(fc, fc->max_size);
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
136 }
846
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
137
888
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
138 static void file_cache_remove_fd(FileCacheData *fc, FileData *fd)
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
139 {
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
140 GList *work;
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
141 FileCacheEntry *fe;
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
142
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
143 if (debug_file_cache) file_cache_dump(fc);
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
144
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
145 work = fc->list;
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
146 while (work)
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
147 {
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
148 GList *current = work;
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
149 fe = work->data;
995
6ca2c5fd7b13 Whitespaces cleanup.
zas_
parents: 893
diff changeset
150 work = work->next;
888
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
151
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
152 if (fe->fd == fd)
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
153 {
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
154 fc->list = g_list_delete_link(fc->list, current);
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
155
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
156 DEBUG_1("cache remove: fc=%p %s", fc, fe->fd->path);
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
157 fc->size -= fe->size;
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
158 fc->release(fe->fd);
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
159 file_data_unref(fe->fd);
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
160 g_free(fe);
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
161 }
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
162 }
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
163 }
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
164
846
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
165 void file_cache_dump(FileCacheData *fc)
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
166 {
1422
91bed0d66cf2 gint -> gboolean and tidy up.
zas_
parents: 1284
diff changeset
167 GList *work = fc->list;
1164
3692efcbd325 Fix compilation warnings.
zas_
parents: 1055
diff changeset
168 gulong n = 0;
1422
91bed0d66cf2 gint -> gboolean and tidy up.
zas_
parents: 1284
diff changeset
169
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
170 DEBUG_1("cache dump: fc=%p max size:%ld size:%ld", fc, fc->max_size, fc->size);
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
171
855
0c3f6ef17d18 Tidy up.
zas_
parents: 847
diff changeset
172 while (work)
846
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
173 {
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
174 FileCacheEntry *fe = work->data;
846
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
175 work = work->next;
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
176 DEBUG_1("cache entry: fc=%p [%lu] %s %ld", fc, ++n, fe->fd->path, fe->size);
846
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
177 }
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
178 }
888
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
179
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
180 static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data)
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
181 {
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
182 FileCacheData *fc = data;
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
183
1432
cf4029d10d38 improved notification system
nadvornik
parents: 1422
diff changeset
184 if (type & (NOTIFY_REREAD | NOTIFY_CHANGE)) /* invalidate the entry on each file change */
888
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
185 {
1498
5f49f305a6b6 improved debug messages
nadvornik
parents: 1432
diff changeset
186 DEBUG_1("Notify cache: %s %04x", fd->path, type);
888
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
187 file_cache_remove_fd(fc, fd);
d8e1e820cee7 reload changed images
nadvornik
parents: 873
diff changeset
188 }
893
649c848333ee Fix missing newline at end of file.
zas_
parents: 891
diff changeset
189 }
1055
1646720364cf Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents: 995
diff changeset
190 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */