Put the g_return_val_if_fail() in the right place.
[glib.git] / glib / gcache.c
bloba462b027579ed6b0930b26093484fe7192fd635d
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"
36 typedef struct _GCacheNode GCacheNode;
38 struct _GCacheNode
40 /* A reference counted node */
41 gpointer value;
42 gint ref_count;
45 struct _GCache
47 /* Called to create a value from a key */
48 GCacheNewFunc value_new_func;
50 /* Called to destroy a value */
51 GCacheDestroyFunc value_destroy_func;
53 /* Called to duplicate a key */
54 GCacheDupFunc key_dup_func;
56 /* Called to destroy a key */
57 GCacheDestroyFunc key_destroy_func;
59 /* Associates keys with nodes */
60 GHashTable *key_table;
62 /* Associates nodes with keys */
63 GHashTable *value_table;
67 static GCacheNode* g_cache_node_new (gpointer value);
68 static void g_cache_node_destroy (GCacheNode *node);
71 static GMemChunk *node_mem_chunk = NULL;
72 G_LOCK_DEFINE_STATIC (node_mem_chunk);
74 GCache*
75 g_cache_new (GCacheNewFunc value_new_func,
76 GCacheDestroyFunc value_destroy_func,
77 GCacheDupFunc key_dup_func,
78 GCacheDestroyFunc key_destroy_func,
79 GHashFunc hash_key_func,
80 GHashFunc hash_value_func,
81 GEqualFunc key_equal_func)
83 GCache *cache;
85 g_return_val_if_fail (value_new_func != NULL, NULL);
86 g_return_val_if_fail (value_destroy_func != NULL, NULL);
87 g_return_val_if_fail (key_dup_func != NULL, NULL);
88 g_return_val_if_fail (key_destroy_func != NULL, NULL);
89 g_return_val_if_fail (hash_key_func != NULL, NULL);
90 g_return_val_if_fail (hash_value_func != NULL, NULL);
91 g_return_val_if_fail (key_equal_func != NULL, NULL);
93 cache = g_new (GCache, 1);
94 cache->value_new_func = value_new_func;
95 cache->value_destroy_func = value_destroy_func;
96 cache->key_dup_func = key_dup_func;
97 cache->key_destroy_func = key_destroy_func;
98 cache->key_table = g_hash_table_new (hash_key_func, key_equal_func);
99 cache->value_table = g_hash_table_new (hash_value_func, NULL);
101 return cache;
104 void
105 g_cache_destroy (GCache *cache)
107 g_return_if_fail (cache != NULL);
109 g_hash_table_destroy (cache->key_table);
110 g_hash_table_destroy (cache->value_table);
111 g_free (cache);
114 gpointer
115 g_cache_insert (GCache *cache,
116 gpointer key)
118 GCacheNode *node;
119 gpointer value;
121 g_return_val_if_fail (cache != NULL, NULL);
123 node = g_hash_table_lookup (cache->key_table, key);
124 if (node)
126 node->ref_count += 1;
127 return node->value;
130 key = (* cache->key_dup_func) (key);
131 value = (* cache->value_new_func) (key);
132 node = g_cache_node_new (value);
134 g_hash_table_insert (cache->key_table, key, node);
135 g_hash_table_insert (cache->value_table, value, key);
137 return node->value;
140 void
141 g_cache_remove (GCache *cache,
142 gconstpointer value)
144 GCacheNode *node;
145 gpointer key;
147 g_return_if_fail (cache != NULL);
149 key = g_hash_table_lookup (cache->value_table, value);
150 node = g_hash_table_lookup (cache->key_table, key);
152 g_return_if_fail (node != NULL);
154 node->ref_count -= 1;
155 if (node->ref_count == 0)
157 g_hash_table_remove (cache->value_table, value);
158 g_hash_table_remove (cache->key_table, key);
160 (* cache->key_destroy_func) (key);
161 (* cache->value_destroy_func) (node->value);
162 g_cache_node_destroy (node);
166 void
167 g_cache_key_foreach (GCache *cache,
168 GHFunc func,
169 gpointer user_data)
171 g_return_if_fail (cache != NULL);
172 g_return_if_fail (func != NULL);
174 g_hash_table_foreach (cache->value_table, func, user_data);
177 void
178 g_cache_value_foreach (GCache *cache,
179 GHFunc func,
180 gpointer user_data)
182 g_return_if_fail (cache != NULL);
183 g_return_if_fail (func != NULL);
185 g_hash_table_foreach (cache->key_table, func, user_data);
189 static GCacheNode*
190 g_cache_node_new (gpointer value)
192 GCacheNode *node;
194 G_LOCK (node_mem_chunk);
195 if (!node_mem_chunk)
196 node_mem_chunk = g_mem_chunk_new ("cache node mem chunk", sizeof (GCacheNode),
197 1024, G_ALLOC_AND_FREE);
199 node = g_chunk_new (GCacheNode, node_mem_chunk);
200 G_UNLOCK (node_mem_chunk);
202 node->value = value;
203 node->ref_count = 1;
205 return node;
208 static void
209 g_cache_node_destroy (GCacheNode *node)
211 G_LOCK (node_mem_chunk);
212 g_mem_chunk_free (node_mem_chunk, node);
213 G_UNLOCK (node_mem_chunk);