make third argument a gboolean. (g_scanner_add_symbol,
[glib.git] / glib / gcache.c
blob12e99a66b3c573044e169ae67cd097fba006125e
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 "glib.h"
34 typedef struct _GCacheNode GCacheNode;
36 struct _GCacheNode
38 /* A reference counted node */
39 gpointer value;
40 gint ref_count;
43 struct _GCache
45 /* Called to create a value from a key */
46 GCacheNewFunc value_new_func;
48 /* Called to destroy a value */
49 GCacheDestroyFunc value_destroy_func;
51 /* Called to duplicate a key */
52 GCacheDupFunc key_dup_func;
54 /* Called to destroy a key */
55 GCacheDestroyFunc key_destroy_func;
57 /* Associates keys with nodes */
58 GHashTable *key_table;
60 /* Associates nodes with keys */
61 GHashTable *value_table;
65 static GCacheNode* g_cache_node_new (gpointer value);
66 static void g_cache_node_destroy (GCacheNode *node);
69 static GMemChunk *node_mem_chunk = NULL;
70 G_LOCK_DEFINE_STATIC (node_mem_chunk);
72 GCache*
73 g_cache_new (GCacheNewFunc value_new_func,
74 GCacheDestroyFunc value_destroy_func,
75 GCacheDupFunc key_dup_func,
76 GCacheDestroyFunc key_destroy_func,
77 GHashFunc hash_key_func,
78 GHashFunc hash_value_func,
79 GEqualFunc key_equal_func)
81 GCache *cache;
83 g_return_val_if_fail (value_new_func != NULL, NULL);
84 g_return_val_if_fail (value_destroy_func != NULL, NULL);
85 g_return_val_if_fail (key_dup_func != NULL, NULL);
86 g_return_val_if_fail (key_destroy_func != NULL, NULL);
87 g_return_val_if_fail (hash_key_func != NULL, NULL);
88 g_return_val_if_fail (hash_value_func != NULL, NULL);
89 g_return_val_if_fail (key_equal_func != NULL, NULL);
91 cache = g_new (GCache, 1);
92 cache->value_new_func = value_new_func;
93 cache->value_destroy_func = value_destroy_func;
94 cache->key_dup_func = key_dup_func;
95 cache->key_destroy_func = key_destroy_func;
96 cache->key_table = g_hash_table_new (hash_key_func, key_equal_func);
97 cache->value_table = g_hash_table_new (hash_value_func, NULL);
99 return cache;
102 void
103 g_cache_destroy (GCache *cache)
105 g_return_if_fail (cache != NULL);
107 g_hash_table_destroy (cache->key_table);
108 g_hash_table_destroy (cache->value_table);
109 g_free (cache);
112 gpointer
113 g_cache_insert (GCache *cache,
114 gpointer key)
116 GCacheNode *node;
117 gpointer value;
119 g_return_val_if_fail (cache != NULL, NULL);
121 node = g_hash_table_lookup (cache->key_table, key);
122 if (node)
124 node->ref_count += 1;
125 return node->value;
128 key = (* cache->key_dup_func) (key);
129 value = (* cache->value_new_func) (key);
130 node = g_cache_node_new (value);
132 g_hash_table_insert (cache->key_table, key, node);
133 g_hash_table_insert (cache->value_table, value, key);
135 return node->value;
138 void
139 g_cache_remove (GCache *cache,
140 gconstpointer value)
142 GCacheNode *node;
143 gpointer key;
145 g_return_if_fail (cache != NULL);
147 key = g_hash_table_lookup (cache->value_table, value);
148 node = g_hash_table_lookup (cache->key_table, key);
150 g_return_if_fail (node != NULL);
152 node->ref_count -= 1;
153 if (node->ref_count == 0)
155 g_hash_table_remove (cache->value_table, value);
156 g_hash_table_remove (cache->key_table, key);
158 (* cache->key_destroy_func) (key);
159 (* cache->value_destroy_func) (node->value);
160 g_cache_node_destroy (node);
164 void
165 g_cache_key_foreach (GCache *cache,
166 GHFunc func,
167 gpointer user_data)
169 g_return_if_fail (cache != NULL);
170 g_return_if_fail (func != NULL);
172 g_hash_table_foreach (cache->value_table, func, user_data);
175 void
176 g_cache_value_foreach (GCache *cache,
177 GHFunc func,
178 gpointer user_data)
180 g_return_if_fail (cache != NULL);
181 g_return_if_fail (func != NULL);
183 g_hash_table_foreach (cache->key_table, func, user_data);
187 static GCacheNode*
188 g_cache_node_new (gpointer value)
190 GCacheNode *node;
192 G_LOCK (node_mem_chunk);
193 if (!node_mem_chunk)
194 node_mem_chunk = g_mem_chunk_new ("cache node mem chunk", sizeof (GCacheNode),
195 1024, G_ALLOC_AND_FREE);
197 node = g_chunk_new (GCacheNode, node_mem_chunk);
198 G_UNLOCK (node_mem_chunk);
200 node->value = value;
201 node->ref_count = 1;
203 return node;
206 static void
207 g_cache_node_destroy (GCacheNode *node)
209 G_LOCK (node_mem_chunk);
210 g_mem_chunk_free (node_mem_chunk, node);
211 G_UNLOCK (node_mem_chunk);