GRand: move docs from tmpl to inline comments
[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 "glib.h"
34 #include "galias.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 static inline GCacheNode*
68 g_cache_node_new (gpointer value)
69 {
70   GCacheNode *node = g_slice_new (GCacheNode);
71   node->value = value;
72   node->ref_count = 1;
73   return node;
74 }
75
76 static inline void
77 g_cache_node_destroy (GCacheNode *node)
78 {
79   g_slice_free (GCacheNode, node);
80 }
81
82 GCache*
83 g_cache_new (GCacheNewFunc      value_new_func,
84              GCacheDestroyFunc  value_destroy_func,
85              GCacheDupFunc      key_dup_func,
86              GCacheDestroyFunc  key_destroy_func,
87              GHashFunc          hash_key_func,
88              GHashFunc          hash_value_func,
89              GEqualFunc         key_equal_func)
90 {
91   GCache *cache;
92
93   g_return_val_if_fail (value_new_func != NULL, NULL);
94   g_return_val_if_fail (value_destroy_func != NULL, NULL);
95   g_return_val_if_fail (key_dup_func != NULL, NULL);
96   g_return_val_if_fail (key_destroy_func != NULL, NULL);
97   g_return_val_if_fail (hash_key_func != NULL, NULL);
98   g_return_val_if_fail (hash_value_func != NULL, NULL);
99   g_return_val_if_fail (key_equal_func != NULL, NULL);
100
101   cache = g_slice_new (GCache);
102   cache->value_new_func = value_new_func;
103   cache->value_destroy_func = value_destroy_func;
104   cache->key_dup_func = key_dup_func;
105   cache->key_destroy_func = key_destroy_func;
106   cache->key_table = g_hash_table_new (hash_key_func, key_equal_func);
107   cache->value_table = g_hash_table_new (hash_value_func, NULL);
108
109   return cache;
110 }
111
112 void
113 g_cache_destroy (GCache *cache)
114 {
115   g_return_if_fail (cache != NULL);
116
117   g_hash_table_destroy (cache->key_table);
118   g_hash_table_destroy (cache->value_table);
119   g_slice_free (GCache, cache);
120 }
121
122 gpointer
123 g_cache_insert (GCache   *cache,
124                 gpointer  key)
125 {
126   GCacheNode *node;
127   gpointer value;
128
129   g_return_val_if_fail (cache != NULL, NULL);
130
131   node = g_hash_table_lookup (cache->key_table, key);
132   if (node)
133     {
134       node->ref_count += 1;
135       return node->value;
136     }
137
138   key = (* cache->key_dup_func) (key);
139   value = (* cache->value_new_func) (key);
140   node = g_cache_node_new (value);
141
142   g_hash_table_insert (cache->key_table, key, node);
143   g_hash_table_insert (cache->value_table, value, key);
144
145   return node->value;
146 }
147
148 void
149 g_cache_remove (GCache        *cache,
150                 gconstpointer  value)
151 {
152   GCacheNode *node;
153   gpointer key;
154
155   g_return_if_fail (cache != NULL);
156
157   key = g_hash_table_lookup (cache->value_table, value);
158   node = g_hash_table_lookup (cache->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 (cache->value_table, value);
166       g_hash_table_remove (cache->key_table, key);
167
168       (* cache->key_destroy_func) (key);
169       (* cache->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   g_return_if_fail (cache != NULL);
180   g_return_if_fail (func != NULL);
181
182   g_hash_table_foreach (cache->value_table, func, user_data);
183 }
184
185 void
186 g_cache_value_foreach (GCache   *cache,
187                        GHFunc    func,
188                        gpointer  user_data)
189 {
190   g_return_if_fail (cache != NULL);
191   g_return_if_fail (func != NULL);
192
193   g_hash_table_foreach (cache->key_table, func, user_data);
194 }
195
196 #define __G_CACHE_C__
197 #include "galiasdef.c"