applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[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 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.
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  * Lesser General Public License for more details.
13  *
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.
18  */
19
20 /*
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/. 
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                 gconstpointer  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   g_return_if_fail (node != NULL);
161
162   node->ref_count -= 1;
163   if (node->ref_count == 0)
164     {
165       g_hash_table_remove (rcache->value_table, value);
166       g_hash_table_remove (rcache->key_table, key);
167
168       (* rcache->key_destroy_func) (key);
169       (* rcache->value_destroy_func) (node->value);
170       g_cache_node_destroy (node);
171     }
172 }
173
174 void
175 g_cache_key_foreach (GCache   *cache,
176                      GHFunc    func,
177                      gpointer  user_data)
178 {
179   GRealCache *rcache;
180
181   g_return_if_fail (cache != NULL);
182   g_return_if_fail (func != NULL);
183
184   rcache = (GRealCache*) cache;
185
186   g_hash_table_foreach (rcache->value_table, func, user_data);
187 }
188
189 void
190 g_cache_value_foreach (GCache   *cache,
191                        GHFunc    func,
192                        gpointer  user_data)
193 {
194   GRealCache *rcache;
195
196   g_return_if_fail (cache != NULL);
197   g_return_if_fail (func != NULL);
198
199   rcache = (GRealCache*) cache;
200
201   g_hash_table_foreach (rcache->key_table, func, user_data);
202 }
203
204
205 static GCacheNode*
206 g_cache_node_new (gpointer value)
207 {
208   GCacheNode *node;
209
210   G_LOCK (node_mem_chunk);
211   if (!node_mem_chunk)
212     node_mem_chunk = g_mem_chunk_new ("cache node mem chunk", sizeof (GCacheNode),
213                                       1024, G_ALLOC_AND_FREE);
214
215   node = g_chunk_new (GCacheNode, node_mem_chunk);
216   G_UNLOCK (node_mem_chunk);
217
218   node->value = value;
219   node->ref_count = 1;
220
221   return node;
222 }
223
224 static void
225 g_cache_node_destroy (GCacheNode *node)
226 {
227   G_LOCK (node_mem_chunk);
228   g_mem_chunk_free (node_mem_chunk, node);
229   G_UNLOCK (node_mem_chunk);
230 }