Don't crash if removing a nonexistent value.
[platform/upstream/glib.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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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-1999.  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 "glib.h"
32
33
34 typedef struct _GCacheNode  GCacheNode;
35 typedef struct _GRealCache  GRealCache;
36
37 struct _GCacheNode
38 {
39   /* A reference counted node */
40   gpointer value;
41   gint ref_count;
42 };
43
44 struct _GRealCache
45 {
46   /* Called to create a value from a key */
47   GCacheNewFunc value_new_func;
48
49   /* Called to destroy a value */
50   GCacheDestroyFunc value_destroy_func;
51
52   /* Called to duplicate a key */
53   GCacheDupFunc key_dup_func;
54
55   /* Called to destroy a key */
56   GCacheDestroyFunc key_destroy_func;
57
58   /* Associates keys with nodes */
59   GHashTable *key_table;
60
61   /* Associates nodes with keys */
62   GHashTable *value_table;
63 };
64
65
66 static GCacheNode* g_cache_node_new     (gpointer value);
67 static void        g_cache_node_destroy (GCacheNode *node);
68
69
70 static GMemChunk *node_mem_chunk = NULL;
71 G_LOCK_DEFINE_STATIC (node_mem_chunk);
72
73 GCache*
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)
81 {
82   GRealCache *cache;
83
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);
91
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);
99
100   return (GCache*) cache;
101 }
102
103 void
104 g_cache_destroy (GCache *cache)
105 {
106   GRealCache *rcache;
107
108   g_return_if_fail (cache != NULL);
109
110   rcache = (GRealCache*) cache;
111   g_hash_table_destroy (rcache->key_table);
112   g_hash_table_destroy (rcache->value_table);
113   g_free (rcache);
114 }
115
116 gpointer
117 g_cache_insert (GCache   *cache,
118                 gpointer  key)
119 {
120   GRealCache *rcache;
121   GCacheNode *node;
122   gpointer value;
123
124   g_return_val_if_fail (cache != NULL, NULL);
125
126   rcache = (GRealCache*) cache;
127
128   node = g_hash_table_lookup (rcache->key_table, key);
129   if (node)
130     {
131       node->ref_count += 1;
132       return node->value;
133     }
134
135   key = (* rcache->key_dup_func) (key);
136   value = (* rcache->value_new_func) (key);
137   node = g_cache_node_new (value);
138
139   g_hash_table_insert (rcache->key_table, key, node);
140   g_hash_table_insert (rcache->value_table, value, key);
141
142   return node->value;
143 }
144
145 void
146 g_cache_remove (GCache   *cache,
147                 gpointer  value)
148 {
149   GRealCache *rcache;
150   GCacheNode *node;
151   gpointer key;
152
153   g_return_if_fail (cache != NULL);
154
155   rcache = (GRealCache*) cache;
156
157   key = g_hash_table_lookup (rcache->value_table, value);
158   node = g_hash_table_lookup (rcache->key_table, key);
159
160   if (node == NULL)
161     return;
162
163   node->ref_count -= 1;
164   if (node->ref_count == 0)
165     {
166       g_hash_table_remove (rcache->value_table, value);
167       g_hash_table_remove (rcache->key_table, key);
168
169       (* rcache->key_destroy_func) (key);
170       (* rcache->value_destroy_func) (node->value);
171       g_cache_node_destroy (node);
172     }
173 }
174
175 void
176 g_cache_key_foreach (GCache   *cache,
177                      GHFunc    func,
178                      gpointer  user_data)
179 {
180   GRealCache *rcache;
181
182   g_return_if_fail (cache != NULL);
183   g_return_if_fail (func != NULL);
184
185   rcache = (GRealCache*) cache;
186
187   g_hash_table_foreach (rcache->value_table, func, user_data);
188 }
189
190 void
191 g_cache_value_foreach (GCache   *cache,
192                        GHFunc    func,
193                        gpointer  user_data)
194 {
195   GRealCache *rcache;
196
197   g_return_if_fail (cache != NULL);
198   g_return_if_fail (func != NULL);
199
200   rcache = (GRealCache*) cache;
201
202   g_hash_table_foreach (rcache->key_table, func, user_data);
203 }
204
205
206 static GCacheNode*
207 g_cache_node_new (gpointer value)
208 {
209   GCacheNode *node;
210
211   G_LOCK (node_mem_chunk);
212   if (!node_mem_chunk)
213     node_mem_chunk = g_mem_chunk_new ("cache node mem chunk", sizeof (GCacheNode),
214                                       1024, G_ALLOC_AND_FREE);
215
216   node = g_chunk_new (GCacheNode, node_mem_chunk);
217   G_UNLOCK (node_mem_chunk);
218
219   node->value = value;
220   node->ref_count = 1;
221
222   return node;
223 }
224
225 static void
226 g_cache_node_destroy (GCacheNode *node)
227 {
228   G_LOCK (node_mem_chunk);
229   g_mem_chunk_free (node_mem_chunk, node);
230   G_UNLOCK (node_mem_chunk);
231 }