updated [and finally fixed my script to produce ready to go de-in(ed)
[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 "config.h"
32
33 #include "galias.h"
34 #include "glib.h"
35
36
37 typedef struct _GCacheNode  GCacheNode;
38
39 struct _GCacheNode
40 {
41   /* A reference counted node */
42   gpointer value;
43   gint ref_count;
44 };
45
46 struct _GCache
47 {
48   /* Called to create a value from a key */
49   GCacheNewFunc value_new_func;
50
51   /* Called to destroy a value */
52   GCacheDestroyFunc value_destroy_func;
53
54   /* Called to duplicate a key */
55   GCacheDupFunc key_dup_func;
56
57   /* Called to destroy a key */
58   GCacheDestroyFunc key_destroy_func;
59
60   /* Associates keys with nodes */
61   GHashTable *key_table;
62
63   /* Associates nodes with keys */
64   GHashTable *value_table;
65 };
66
67
68 static GCacheNode* g_cache_node_new     (gpointer value);
69 static void        g_cache_node_destroy (GCacheNode *node);
70
71
72 static GMemChunk *node_mem_chunk = NULL;
73 G_LOCK_DEFINE_STATIC (node_mem_chunk);
74
75 GCache*
76 g_cache_new (GCacheNewFunc      value_new_func,
77              GCacheDestroyFunc  value_destroy_func,
78              GCacheDupFunc      key_dup_func,
79              GCacheDestroyFunc  key_destroy_func,
80              GHashFunc          hash_key_func,
81              GHashFunc          hash_value_func,
82              GEqualFunc         key_equal_func)
83 {
84   GCache *cache;
85
86   g_return_val_if_fail (value_new_func != NULL, NULL);
87   g_return_val_if_fail (value_destroy_func != NULL, NULL);
88   g_return_val_if_fail (key_dup_func != NULL, NULL);
89   g_return_val_if_fail (key_destroy_func != NULL, NULL);
90   g_return_val_if_fail (hash_key_func != NULL, NULL);
91   g_return_val_if_fail (hash_value_func != NULL, NULL);
92   g_return_val_if_fail (key_equal_func != NULL, NULL);
93
94   cache = g_new (GCache, 1);
95   cache->value_new_func = value_new_func;
96   cache->value_destroy_func = value_destroy_func;
97   cache->key_dup_func = key_dup_func;
98   cache->key_destroy_func = key_destroy_func;
99   cache->key_table = g_hash_table_new (hash_key_func, key_equal_func);
100   cache->value_table = g_hash_table_new (hash_value_func, NULL);
101
102   return cache;
103 }
104
105 void
106 g_cache_destroy (GCache *cache)
107 {
108   g_return_if_fail (cache != NULL);
109
110   g_hash_table_destroy (cache->key_table);
111   g_hash_table_destroy (cache->value_table);
112   g_free (cache);
113 }
114
115 gpointer
116 g_cache_insert (GCache   *cache,
117                 gpointer  key)
118 {
119   GCacheNode *node;
120   gpointer value;
121
122   g_return_val_if_fail (cache != NULL, NULL);
123
124   node = g_hash_table_lookup (cache->key_table, key);
125   if (node)
126     {
127       node->ref_count += 1;
128       return node->value;
129     }
130
131   key = (* cache->key_dup_func) (key);
132   value = (* cache->value_new_func) (key);
133   node = g_cache_node_new (value);
134
135   g_hash_table_insert (cache->key_table, key, node);
136   g_hash_table_insert (cache->value_table, value, key);
137
138   return node->value;
139 }
140
141 void
142 g_cache_remove (GCache        *cache,
143                 gconstpointer  value)
144 {
145   GCacheNode *node;
146   gpointer key;
147
148   g_return_if_fail (cache != NULL);
149
150   key = g_hash_table_lookup (cache->value_table, value);
151   node = g_hash_table_lookup (cache->key_table, key);
152
153   g_return_if_fail (node != NULL);
154
155   node->ref_count -= 1;
156   if (node->ref_count == 0)
157     {
158       g_hash_table_remove (cache->value_table, value);
159       g_hash_table_remove (cache->key_table, key);
160
161       (* cache->key_destroy_func) (key);
162       (* cache->value_destroy_func) (node->value);
163       g_cache_node_destroy (node);
164     }
165 }
166
167 void
168 g_cache_key_foreach (GCache   *cache,
169                      GHFunc    func,
170                      gpointer  user_data)
171 {
172   g_return_if_fail (cache != NULL);
173   g_return_if_fail (func != NULL);
174
175   g_hash_table_foreach (cache->value_table, func, user_data);
176 }
177
178 void
179 g_cache_value_foreach (GCache   *cache,
180                        GHFunc    func,
181                        gpointer  user_data)
182 {
183   g_return_if_fail (cache != NULL);
184   g_return_if_fail (func != NULL);
185
186   g_hash_table_foreach (cache->key_table, func, user_data);
187 }
188
189
190 static GCacheNode*
191 g_cache_node_new (gpointer value)
192 {
193   GCacheNode *node;
194
195   G_LOCK (node_mem_chunk);
196   if (!node_mem_chunk)
197     node_mem_chunk = g_mem_chunk_new ("cache node mem chunk", sizeof (GCacheNode),
198                                       1024, G_ALLOC_AND_FREE);
199
200   node = g_chunk_new (GCacheNode, node_mem_chunk);
201   G_UNLOCK (node_mem_chunk);
202
203   node->value = value;
204   node->ref_count = 1;
205
206   return node;
207 }
208
209 static void
210 g_cache_node_destroy (GCacheNode *node)
211 {
212   G_LOCK (node_mem_chunk);
213   g_mem_chunk_free (node_mem_chunk, node);
214   G_UNLOCK (node_mem_chunk);
215 }