Say we require autoconf 2.52 as that is what configure.in does.
[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
36 struct _GCacheNode
37 {
38   /* A reference counted node */
39   gpointer value;
40   gint ref_count;
41 };
42
43 struct _GCache
44 {
45   /* Called to create a value from a key */
46   GCacheNewFunc value_new_func;
47
48   /* Called to destroy a value */
49   GCacheDestroyFunc value_destroy_func;
50
51   /* Called to duplicate a key */
52   GCacheDupFunc key_dup_func;
53
54   /* Called to destroy a key */
55   GCacheDestroyFunc key_destroy_func;
56
57   /* Associates keys with nodes */
58   GHashTable *key_table;
59
60   /* Associates nodes with keys */
61   GHashTable *value_table;
62 };
63
64
65 static GCacheNode* g_cache_node_new     (gpointer value);
66 static void        g_cache_node_destroy (GCacheNode *node);
67
68
69 static GMemChunk *node_mem_chunk = NULL;
70 G_LOCK_DEFINE_STATIC (node_mem_chunk);
71
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)
80 {
81   GCache *cache;
82
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);
90
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);
98
99   return cache;
100 }
101
102 void
103 g_cache_destroy (GCache *cache)
104 {
105   g_return_if_fail (cache != NULL);
106
107   g_hash_table_destroy (cache->key_table);
108   g_hash_table_destroy (cache->value_table);
109   g_free (cache);
110 }
111
112 gpointer
113 g_cache_insert (GCache   *cache,
114                 gpointer  key)
115 {
116   GCacheNode *node;
117   gpointer value;
118
119   g_return_val_if_fail (cache != NULL, NULL);
120
121   node = g_hash_table_lookup (cache->key_table, key);
122   if (node)
123     {
124       node->ref_count += 1;
125       return node->value;
126     }
127
128   key = (* cache->key_dup_func) (key);
129   value = (* cache->value_new_func) (key);
130   node = g_cache_node_new (value);
131
132   g_hash_table_insert (cache->key_table, key, node);
133   g_hash_table_insert (cache->value_table, value, key);
134
135   return node->value;
136 }
137
138 void
139 g_cache_remove (GCache        *cache,
140                 gconstpointer  value)
141 {
142   GCacheNode *node;
143   gpointer key;
144
145   g_return_if_fail (cache != NULL);
146
147   key = g_hash_table_lookup (cache->value_table, value);
148   node = g_hash_table_lookup (cache->key_table, key);
149
150   g_return_if_fail (node != NULL);
151
152   node->ref_count -= 1;
153   if (node->ref_count == 0)
154     {
155       g_hash_table_remove (cache->value_table, value);
156       g_hash_table_remove (cache->key_table, key);
157
158       (* cache->key_destroy_func) (key);
159       (* cache->value_destroy_func) (node->value);
160       g_cache_node_destroy (node);
161     }
162 }
163
164 void
165 g_cache_key_foreach (GCache   *cache,
166                      GHFunc    func,
167                      gpointer  user_data)
168 {
169   g_return_if_fail (cache != NULL);
170   g_return_if_fail (func != NULL);
171
172   g_hash_table_foreach (cache->value_table, func, user_data);
173 }
174
175 void
176 g_cache_value_foreach (GCache   *cache,
177                        GHFunc    func,
178                        gpointer  user_data)
179 {
180   g_return_if_fail (cache != NULL);
181   g_return_if_fail (func != NULL);
182
183   g_hash_table_foreach (cache->key_table, func, user_data);
184 }
185
186
187 static GCacheNode*
188 g_cache_node_new (gpointer value)
189 {
190   GCacheNode *node;
191
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);
196
197   node = g_chunk_new (GCacheNode, node_mem_chunk);
198   G_UNLOCK (node_mem_chunk);
199
200   node->value = value;
201   node->ref_count = 1;
202
203   return node;
204 }
205
206 static void
207 g_cache_node_destroy (GCacheNode *node)
208 {
209   G_LOCK (node_mem_chunk);
210   g_mem_chunk_free (node_mem_chunk, node);
211   G_UNLOCK (node_mem_chunk);
212 }