|
1 /* GLIB - Library of useful routines for C programming |
|
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
|
3 * Portions copyright (c) 2006 Nokia Corporation. All rights reserved. |
|
4 * |
|
5 * This library is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU Lesser General Public |
|
7 * License as published by the Free Software Foundation; either |
|
8 * version 2 of the License, or (at your option) any later version. |
|
9 * |
|
10 * This library is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 * Lesser General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Lesser General Public |
|
16 * License along with this library; if not, write to the |
|
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
18 * Boston, MA 02111-1307, USA. |
|
19 */ |
|
20 |
|
21 /* |
|
22 * Modified by the GLib Team and others 1997-2000. See the AUTHORS |
|
23 * file for a list of people on the GLib Team. See the ChangeLog |
|
24 * files for a list of changes. These files are distributed with |
|
25 * GLib at ftp://ftp.gtk.org/pub/gtk/. |
|
26 */ |
|
27 |
|
28 /* |
|
29 * MT safe |
|
30 */ |
|
31 |
|
32 #include "config.h" |
|
33 |
|
34 #include "glib.h" |
|
35 #include "galias.h" |
|
36 |
|
37 |
|
38 typedef struct _GCacheNode GCacheNode; |
|
39 |
|
40 struct _GCacheNode |
|
41 { |
|
42 /* A reference counted node */ |
|
43 gpointer value; |
|
44 gint ref_count; |
|
45 }; |
|
46 |
|
47 struct _GCache |
|
48 { |
|
49 /* Called to create a value from a key */ |
|
50 GCacheNewFunc value_new_func; |
|
51 |
|
52 /* Called to destroy a value */ |
|
53 GCacheDestroyFunc value_destroy_func; |
|
54 |
|
55 /* Called to duplicate a key */ |
|
56 GCacheDupFunc key_dup_func; |
|
57 |
|
58 /* Called to destroy a key */ |
|
59 GCacheDestroyFunc key_destroy_func; |
|
60 |
|
61 /* Associates keys with nodes */ |
|
62 GHashTable *key_table; |
|
63 |
|
64 /* Associates nodes with keys */ |
|
65 GHashTable *value_table; |
|
66 }; |
|
67 |
|
68 static inline GCacheNode* |
|
69 g_cache_node_new (gpointer value) |
|
70 { |
|
71 GCacheNode *node = g_slice_new (GCacheNode); |
|
72 node->value = value; |
|
73 node->ref_count = 1; |
|
74 return node; |
|
75 } |
|
76 |
|
77 static inline void |
|
78 g_cache_node_destroy (GCacheNode *node) |
|
79 { |
|
80 g_slice_free (GCacheNode, node); |
|
81 } |
|
82 |
|
83 EXPORT_C GCache* |
|
84 g_cache_new (GCacheNewFunc value_new_func, |
|
85 GCacheDestroyFunc value_destroy_func, |
|
86 GCacheDupFunc key_dup_func, |
|
87 GCacheDestroyFunc key_destroy_func, |
|
88 GHashFunc hash_key_func, |
|
89 GHashFunc hash_value_func, |
|
90 GEqualFunc key_equal_func) |
|
91 { |
|
92 GCache *cache; |
|
93 |
|
94 g_return_val_if_fail (value_new_func != NULL, NULL); |
|
95 g_return_val_if_fail (value_destroy_func != NULL, NULL); |
|
96 g_return_val_if_fail (key_dup_func != NULL, NULL); |
|
97 g_return_val_if_fail (key_destroy_func != NULL, NULL); |
|
98 g_return_val_if_fail (hash_key_func != NULL, NULL); |
|
99 g_return_val_if_fail (hash_value_func != NULL, NULL); |
|
100 g_return_val_if_fail (key_equal_func != NULL, NULL); |
|
101 |
|
102 cache = g_slice_new (GCache); |
|
103 |
|
104 cache->value_new_func = value_new_func; |
|
105 cache->value_destroy_func = value_destroy_func; |
|
106 cache->key_dup_func = key_dup_func; |
|
107 cache->key_destroy_func = key_destroy_func; |
|
108 cache->key_table = g_hash_table_new (hash_key_func, key_equal_func); |
|
109 cache->value_table = g_hash_table_new (hash_value_func, NULL); |
|
110 |
|
111 return cache; |
|
112 } |
|
113 |
|
114 EXPORT_C void |
|
115 g_cache_destroy (GCache *cache) |
|
116 { |
|
117 g_return_if_fail (cache != NULL); |
|
118 |
|
119 g_hash_table_destroy (cache->key_table); |
|
120 g_hash_table_destroy (cache->value_table); |
|
121 g_slice_free (GCache, cache); |
|
122 } |
|
123 |
|
124 EXPORT_C gpointer |
|
125 g_cache_insert (GCache *cache, |
|
126 gpointer key) |
|
127 { |
|
128 GCacheNode *node; |
|
129 gpointer value; |
|
130 |
|
131 g_return_val_if_fail (cache != NULL, NULL); |
|
132 |
|
133 node = g_hash_table_lookup (cache->key_table, key); |
|
134 if (node) |
|
135 { |
|
136 node->ref_count += 1; |
|
137 return node->value; |
|
138 } |
|
139 |
|
140 key = (* cache->key_dup_func) (key); |
|
141 value = (* cache->value_new_func) (key); |
|
142 node = g_cache_node_new (value); |
|
143 |
|
144 g_hash_table_insert (cache->key_table, key, node); |
|
145 g_hash_table_insert (cache->value_table, value, key); |
|
146 |
|
147 return node->value; |
|
148 } |
|
149 |
|
150 EXPORT_C void |
|
151 g_cache_remove (GCache *cache, |
|
152 gconstpointer value) |
|
153 { |
|
154 GCacheNode *node; |
|
155 gpointer key; |
|
156 |
|
157 g_return_if_fail (cache != NULL); |
|
158 |
|
159 key = g_hash_table_lookup (cache->value_table, value); |
|
160 node = g_hash_table_lookup (cache->key_table, key); |
|
161 |
|
162 g_return_if_fail (node != NULL); |
|
163 |
|
164 node->ref_count -= 1; |
|
165 if (node->ref_count == 0) |
|
166 { |
|
167 g_hash_table_remove (cache->value_table, value); |
|
168 g_hash_table_remove (cache->key_table, key); |
|
169 |
|
170 (* cache->key_destroy_func) (key); |
|
171 (* cache->value_destroy_func) (node->value); |
|
172 g_cache_node_destroy (node); |
|
173 } |
|
174 } |
|
175 |
|
176 EXPORT_C void |
|
177 g_cache_key_foreach (GCache *cache, |
|
178 GHFunc func, |
|
179 gpointer user_data) |
|
180 { |
|
181 g_return_if_fail (cache != NULL); |
|
182 g_return_if_fail (func != NULL); |
|
183 |
|
184 g_hash_table_foreach (cache->value_table, func, user_data); |
|
185 } |
|
186 |
|
187 EXPORT_C void |
|
188 g_cache_value_foreach (GCache *cache, |
|
189 GHFunc func, |
|
190 gpointer user_data) |
|
191 { |
|
192 g_return_if_fail (cache != NULL); |
|
193 g_return_if_fail (func != NULL); |
|
194 |
|
195 g_hash_table_foreach (cache->key_table, func, user_data); |
|
196 } |
|
197 |
|
198 #define __G_CACHE_C__ |
|
199 #include "galiasdef.c" |