add a comment
[glib.git] / glib / gcache.c
bloba5b02974ceaaf275153a4cc112a1f0d2b0714edc
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 /*
28 * MT safe
31 #include "config.h"
33 #include "glib.h"
34 #include "galias.h"
37 typedef struct _GCacheNode GCacheNode;
39 struct _GCacheNode
41 /* A reference counted node */
42 gpointer value;
43 gint ref_count;
46 struct _GCache
48 /* Called to create a value from a key */
49 GCacheNewFunc value_new_func;
51 /* Called to destroy a value */
52 GCacheDestroyFunc value_destroy_func;
54 /* Called to duplicate a key */
55 GCacheDupFunc key_dup_func;
57 /* Called to destroy a key */
58 GCacheDestroyFunc key_destroy_func;
60 /* Associates keys with nodes */
61 GHashTable *key_table;
63 /* Associates nodes with keys */
64 GHashTable *value_table;
67 static inline GCacheNode*
68 g_cache_node_new (gpointer value)
70 GCacheNode *node = g_slice_new (GCacheNode);
71 node->value = value;
72 node->ref_count = 1;
73 return node;
76 static inline void
77 g_cache_node_destroy (GCacheNode *node)
79 g_slice_free (GCacheNode, node);
82 GCache*
83 g_cache_new (GCacheNewFunc value_new_func,
84 GCacheDestroyFunc value_destroy_func,
85 GCacheDupFunc key_dup_func,
86 GCacheDestroyFunc key_destroy_func,
87 GHashFunc hash_key_func,
88 GHashFunc hash_value_func,
89 GEqualFunc key_equal_func)
91 GCache *cache;
93 g_return_val_if_fail (value_new_func != NULL, NULL);
94 g_return_val_if_fail (value_destroy_func != NULL, NULL);
95 g_return_val_if_fail (key_dup_func != NULL, NULL);
96 g_return_val_if_fail (key_destroy_func != NULL, NULL);
97 g_return_val_if_fail (hash_key_func != NULL, NULL);
98 g_return_val_if_fail (hash_value_func != NULL, NULL);
99 g_return_val_if_fail (key_equal_func != NULL, NULL);
101 cache = g_slice_new (GCache);
102 cache->value_new_func = value_new_func;
103 cache->value_destroy_func = value_destroy_func;
104 cache->key_dup_func = key_dup_func;
105 cache->key_destroy_func = key_destroy_func;
106 cache->key_table = g_hash_table_new (hash_key_func, key_equal_func);
107 cache->value_table = g_hash_table_new (hash_value_func, NULL);
109 return cache;
112 void
113 g_cache_destroy (GCache *cache)
115 g_return_if_fail (cache != NULL);
117 g_hash_table_destroy (cache->key_table);
118 g_hash_table_destroy (cache->value_table);
119 g_slice_free (GCache, cache);
122 gpointer
123 g_cache_insert (GCache *cache,
124 gpointer key)
126 GCacheNode *node;
127 gpointer value;
129 g_return_val_if_fail (cache != NULL, NULL);
131 node = g_hash_table_lookup (cache->key_table, key);
132 if (node)
134 node->ref_count += 1;
135 return node->value;
138 key = (* cache->key_dup_func) (key);
139 value = (* cache->value_new_func) (key);
140 node = g_cache_node_new (value);
142 g_hash_table_insert (cache->key_table, key, node);
143 g_hash_table_insert (cache->value_table, value, key);
145 return node->value;
148 void
149 g_cache_remove (GCache *cache,
150 gconstpointer value)
152 GCacheNode *node;
153 gpointer key;
155 g_return_if_fail (cache != NULL);
157 key = g_hash_table_lookup (cache->value_table, value);
158 node = g_hash_table_lookup (cache->key_table, key);
160 g_return_if_fail (node != NULL);
162 node->ref_count -= 1;
163 if (node->ref_count == 0)
165 g_hash_table_remove (cache->value_table, value);
166 g_hash_table_remove (cache->key_table, key);
168 (* cache->key_destroy_func) (key);
169 (* cache->value_destroy_func) (node->value);
170 g_cache_node_destroy (node);
174 void
175 g_cache_key_foreach (GCache *cache,
176 GHFunc func,
177 gpointer user_data)
179 g_return_if_fail (cache != NULL);
180 g_return_if_fail (func != NULL);
182 g_hash_table_foreach (cache->value_table, func, user_data);
185 void
186 g_cache_value_foreach (GCache *cache,
187 GHFunc func,
188 gpointer user_data)
190 g_return_if_fail (cache != NULL);
191 g_return_if_fail (func != NULL);
193 g_hash_table_foreach (cache->key_table, func, user_data);
196 #define __G_CACHE_C__
197 #include "galiasdef.c"