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/.
34 typedef struct _GCacheNode GCacheNode;
35 typedef struct _GRealCache GRealCache;
39 /* A reference counted node */
46 /* Called to create a value from a key */
47 GCacheNewFunc value_new_func;
49 /* Called to destroy a value */
50 GCacheDestroyFunc value_destroy_func;
52 /* Called to duplicate a key */
53 GCacheDupFunc key_dup_func;
55 /* Called to destroy a key */
56 GCacheDestroyFunc key_destroy_func;
58 /* Associates keys with nodes */
59 GHashTable *key_table;
61 /* Associates nodes with keys */
62 GHashTable *value_table;
66 static GCacheNode* g_cache_node_new (gpointer value);
67 static void g_cache_node_destroy (GCacheNode *node);
70 static GMemChunk *node_mem_chunk = NULL;
71 G_LOCK_DEFINE_STATIC (node_mem_chunk);
74 g_cache_new (GCacheNewFunc value_new_func,
75 GCacheDestroyFunc value_destroy_func,
76 GCacheDupFunc key_dup_func,
77 GCacheDestroyFunc key_destroy_func,
78 GHashFunc hash_key_func,
79 GHashFunc hash_value_func,
80 GCompareFunc key_compare_func)
84 g_return_val_if_fail (value_new_func != NULL, NULL);
85 g_return_val_if_fail (value_destroy_func != NULL, NULL);
86 g_return_val_if_fail (key_dup_func != NULL, NULL);
87 g_return_val_if_fail (key_destroy_func != NULL, NULL);
88 g_return_val_if_fail (hash_key_func != NULL, NULL);
89 g_return_val_if_fail (hash_value_func != NULL, NULL);
90 g_return_val_if_fail (key_compare_func != NULL, NULL);
92 cache = g_new (GRealCache, 1);
93 cache->value_new_func = value_new_func;
94 cache->value_destroy_func = value_destroy_func;
95 cache->key_dup_func = key_dup_func;
96 cache->key_destroy_func = key_destroy_func;
97 cache->key_table = g_hash_table_new (hash_key_func, key_compare_func);
98 cache->value_table = g_hash_table_new (hash_value_func, NULL);
100 return (GCache*) cache;
104 g_cache_destroy (GCache *cache)
108 g_return_if_fail (cache != NULL);
110 rcache = (GRealCache*) cache;
111 g_hash_table_destroy (rcache->key_table);
112 g_hash_table_destroy (rcache->value_table);
117 g_cache_insert (GCache *cache,
124 g_return_val_if_fail (cache != NULL, NULL);
126 rcache = (GRealCache*) cache;
128 node = g_hash_table_lookup (rcache->key_table, key);
131 node->ref_count += 1;
135 key = (* rcache->key_dup_func) (key);
136 value = (* rcache->value_new_func) (key);
137 node = g_cache_node_new (value);
139 g_hash_table_insert (rcache->key_table, key, node);
140 g_hash_table_insert (rcache->value_table, value, key);
146 g_cache_remove (GCache *cache,
153 g_return_if_fail (cache != NULL);
155 rcache = (GRealCache*) cache;
157 key = g_hash_table_lookup (rcache->value_table, value);
158 node = g_hash_table_lookup (rcache->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 (rcache->value_table, value);
166 g_hash_table_remove (rcache->key_table, key);
168 (* rcache->key_destroy_func) (key);
169 (* rcache->value_destroy_func) (node->value);
170 g_cache_node_destroy (node);
175 g_cache_key_foreach (GCache *cache,
181 g_return_if_fail (cache != NULL);
182 g_return_if_fail (func != NULL);
184 rcache = (GRealCache*) cache;
186 g_hash_table_foreach (rcache->value_table, func, user_data);
190 g_cache_value_foreach (GCache *cache,
196 g_return_if_fail (cache != NULL);
197 g_return_if_fail (func != NULL);
199 rcache = (GRealCache*) cache;
201 g_hash_table_foreach (rcache->key_table, func, user_data);
206 g_cache_node_new (gpointer value)
210 G_LOCK (node_mem_chunk);
212 node_mem_chunk = g_mem_chunk_new ("cache node mem chunk", sizeof (GCacheNode),
213 1024, G_ALLOC_AND_FREE);
215 node = g_chunk_new (GCacheNode, node_mem_chunk);
216 G_UNLOCK (node_mem_chunk);
225 g_cache_node_destroy (GCacheNode *node)
227 G_LOCK (node_mem_chunk);
228 g_mem_chunk_free (node_mem_chunk, node);
229 G_UNLOCK (node_mem_chunk);