[HQ](Debian) Package version up
[external/glib2.0.git] / glib / gcache.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 /* 
28  * MT safe
29  */
30
31 #include "config.h"
32
33 #include "glib.h"
34 #include "galias.h"
35
36 /**
37  * SECTION: caches
38  * @title: Caches
39  * @short_description: caches allow sharing of complex data structures
40  *                     to save resources
41  *
42  * A #GCache allows sharing of complex data structures, in order to
43  * save system resources.
44  *
45  * GTK+ uses caches for #GtkStyles and #GdkGCs. These consume a lot of
46  * resources, so a #GCache is used to see if a #GtkStyle or #GdkGC with
47  * the required properties already exists. If it does, then the
48  * existing object is used instead of creating a new one.
49  *
50  * #GCache uses keys and values. A #GCache key describes the properties
51  * of a particular resource. A #GCache value is the actual resource.
52  **/
53
54 typedef struct _GCacheNode  GCacheNode;
55
56 struct _GCacheNode
57 {
58   /* A reference counted node */
59   gpointer value;
60   gint ref_count;
61 };
62
63 /**
64  * GCache:
65  *
66  * The #GCache struct is an opaque data structure containing
67  * information about a #GCache. It should only be accessed via the
68  * following functions.
69  **/
70 struct _GCache
71 {
72   /* Called to create a value from a key */
73   GCacheNewFunc value_new_func;
74
75   /* Called to destroy a value */
76   GCacheDestroyFunc value_destroy_func;
77
78   /* Called to duplicate a key */
79   GCacheDupFunc key_dup_func;
80
81   /* Called to destroy a key */
82   GCacheDestroyFunc key_destroy_func;
83
84   /* Associates keys with nodes */
85   GHashTable *key_table;
86
87   /* Associates nodes with keys */
88   GHashTable *value_table;
89 };
90
91 static inline GCacheNode*
92 g_cache_node_new (gpointer value)
93 {
94   GCacheNode *node = g_slice_new (GCacheNode);
95   node->value = value;
96   node->ref_count = 1;
97   return node;
98 }
99
100 static inline void
101 g_cache_node_destroy (GCacheNode *node)
102 {
103   g_slice_free (GCacheNode, node);
104 }
105
106 /**
107  * g_cache_new:
108  * @value_new_func: a function to create a new object given a key.
109  *                  This is called by g_cache_insert() if an object
110  *                  with the given key does not already exist.
111  * @value_destroy_func: a function to destroy an object. It is called
112  *                      by g_cache_remove() when the object is no
113  *                      longer needed (i.e. its reference count drops
114  *                      to 0).
115  * @key_dup_func: a function to copy a key. It is called by
116  *                g_cache_insert() if the key does not already exist in
117  *                the #GCache.
118  * @key_destroy_func: a function to destroy a key. It is called by
119  *                    g_cache_remove() when the object is no longer
120  *                    needed (i.e. its reference count drops to 0).
121  * @hash_key_func: a function to create a hash value from a key.
122  * @hash_value_func: a function to create a hash value from a value.
123  * @key_equal_func: a function to compare two keys. It should return
124  *                  %TRUE if the two keys are equivalent.
125  * @Returns: a new #GCache.
126  *
127  * Creates a new #GCache.
128  **/
129 /**
130  * GCacheNewFunc:
131  * @key: a #GCache key.
132  * @Returns: a new #GCache value corresponding to the key.
133  *
134  * Specifies the type of the @value_new_func function passed to
135  * g_cache_new(). It is passed a #GCache key and should create the
136  * value corresponding to the key.
137  **/
138 /**
139  * GCacheDestroyFunc:
140  * @value: the #GCache value to destroy.
141  *
142  * Specifies the type of the @value_destroy_func and @key_destroy_func
143  * functions passed to g_cache_new(). The functions are passed a
144  * pointer to the #GCache key or #GCache value and should free any
145  * memory and other resources associated with it.
146  **/
147 /**
148  * GCacheDupFunc:
149  * @value: the #GCache key to destroy (<emphasis>not</emphasis> a
150  *         #GCache value as it seems).
151  * @Returns: a copy of the #GCache key.
152  *
153  * Specifies the type of the @key_dup_func function passed to
154  * g_cache_new(). The function is passed a key
155  * (<emphasis>not</emphasis> a value as the prototype implies) and
156  * should return a duplicate of the key.
157  **/
158 GCache*
159 g_cache_new (GCacheNewFunc      value_new_func,
160              GCacheDestroyFunc  value_destroy_func,
161              GCacheDupFunc      key_dup_func,
162              GCacheDestroyFunc  key_destroy_func,
163              GHashFunc          hash_key_func,
164              GHashFunc          hash_value_func,
165              GEqualFunc         key_equal_func)
166 {
167   GCache *cache;
168
169   g_return_val_if_fail (value_new_func != NULL, NULL);
170   g_return_val_if_fail (value_destroy_func != NULL, NULL);
171   g_return_val_if_fail (key_dup_func != NULL, NULL);
172   g_return_val_if_fail (key_destroy_func != NULL, NULL);
173   g_return_val_if_fail (hash_key_func != NULL, NULL);
174   g_return_val_if_fail (hash_value_func != NULL, NULL);
175   g_return_val_if_fail (key_equal_func != NULL, NULL);
176
177   cache = g_slice_new (GCache);
178   cache->value_new_func = value_new_func;
179   cache->value_destroy_func = value_destroy_func;
180   cache->key_dup_func = key_dup_func;
181   cache->key_destroy_func = key_destroy_func;
182   cache->key_table = g_hash_table_new (hash_key_func, key_equal_func);
183   cache->value_table = g_hash_table_new (hash_value_func, NULL);
184
185   return cache;
186 }
187
188 /**
189  * g_cache_destroy:
190  * @cache: a #GCache.
191  *
192  * Frees the memory allocated for the #GCache.
193  *
194  * Note that it does not destroy the keys and values which were
195  * contained in the #GCache.
196  **/
197 void
198 g_cache_destroy (GCache *cache)
199 {
200   g_return_if_fail (cache != NULL);
201
202   g_hash_table_destroy (cache->key_table);
203   g_hash_table_destroy (cache->value_table);
204   g_slice_free (GCache, cache);
205 }
206
207 /**
208  * g_cache_insert:
209  * @cache: a #GCache.
210  * @key: a key describing a #GCache object.
211  * @Returns: a pointer to a #GCache value.
212  *
213  * Gets the value corresponding to the given key, creating it if
214  * necessary. It first checks if the value already exists in the
215  * #GCache, by using the @key_equal_func function passed to
216  * g_cache_new(). If it does already exist it is returned, and its
217  * reference count is increased by one. If the value does not currently
218  * exist, if is created by calling the @value_new_func. The key is
219  * duplicated by calling @key_dup_func and the duplicated key and value
220  * are inserted into the #GCache.
221  **/
222 gpointer
223 g_cache_insert (GCache   *cache,
224                 gpointer  key)
225 {
226   GCacheNode *node;
227   gpointer value;
228
229   g_return_val_if_fail (cache != NULL, NULL);
230
231   node = g_hash_table_lookup (cache->key_table, key);
232   if (node)
233     {
234       node->ref_count += 1;
235       return node->value;
236     }
237
238   key = (* cache->key_dup_func) (key);
239   value = (* cache->value_new_func) (key);
240   node = g_cache_node_new (value);
241
242   g_hash_table_insert (cache->key_table, key, node);
243   g_hash_table_insert (cache->value_table, value, key);
244
245   return node->value;
246 }
247
248 /**
249  * g_cache_remove:
250  * @cache: a #GCache.
251  * @value: the value to remove.
252  *
253  * Decreases the reference count of the given value. If it drops to 0
254  * then the value and its corresponding key are destroyed, using the
255  * @value_destroy_func and @key_destroy_func passed to g_cache_new().
256  **/
257 void
258 g_cache_remove (GCache        *cache,
259                 gconstpointer  value)
260 {
261   GCacheNode *node;
262   gpointer key;
263
264   g_return_if_fail (cache != NULL);
265
266   key = g_hash_table_lookup (cache->value_table, value);
267   node = g_hash_table_lookup (cache->key_table, key);
268
269   g_return_if_fail (node != NULL);
270
271   node->ref_count -= 1;
272   if (node->ref_count == 0)
273     {
274       g_hash_table_remove (cache->value_table, value);
275       g_hash_table_remove (cache->key_table, key);
276
277       (* cache->key_destroy_func) (key);
278       (* cache->value_destroy_func) (node->value);
279       g_cache_node_destroy (node);
280     }
281 }
282
283 /**
284  * g_cache_key_foreach:
285  * @cache: a #GCache.
286  * @func: the function to call with each #GCache key.
287  * @user_data: user data to pass to the function.
288  *
289  * Calls the given function for each of the keys in the #GCache.
290  *
291  * NOTE @func is passed three parameters, the value and key of a cache
292  * entry and the @user_data. The order of value and key is different
293  * from the order in which g_hash_table_foreach() passes key-value
294  * pairs to its callback function !
295  **/
296 void
297 g_cache_key_foreach (GCache   *cache,
298                      GHFunc    func,
299                      gpointer  user_data)
300 {
301   g_return_if_fail (cache != NULL);
302   g_return_if_fail (func != NULL);
303
304   g_hash_table_foreach (cache->value_table, func, user_data);
305 }
306
307 /**
308  * g_cache_value_foreach:
309  * @cache: a #GCache.
310  * @func: the function to call with each #GCache value.
311  * @user_data: user data to pass to the function.
312  *
313  * Calls the given function for each of the values in the #GCache.
314  *
315  * Deprecated:2.10: The reason is that it passes pointers to internal
316  *                  data structures to @func; use g_cache_key_foreach()
317  *                  instead
318  **/
319 void
320 g_cache_value_foreach (GCache   *cache,
321                        GHFunc    func,
322                        gpointer  user_data)
323 {
324   g_return_if_fail (cache != NULL);
325   g_return_if_fail (func != NULL);
326
327   g_hash_table_foreach (cache->key_table, func, user_data);
328 }
329
330 #define __G_CACHE_C__
331 #include "galiasdef.c"