1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
37 typedef struct _GCacheNode GCacheNode
;
41 /* A reference counted node */
48 /* Called to create a value from a key */
49 GCacheNewFunc value_new_func
;
51 /* Called to destroy a value */
52 GCacheDestroyFunc value_destroy_func
;
54 /* Called to duplicate a key */
55 GCacheDupFunc key_dup_func
;
57 /* Called to destroy a key */
58 GCacheDestroyFunc key_destroy_func
;
60 /* Associates keys with nodes */
61 GHashTable
*key_table
;
63 /* Associates nodes with keys */
64 GHashTable
*value_table
;
67 static inline GCacheNode
*
68 g_cache_node_new (gpointer value
)
70 GCacheNode
*node
= g_slice_new (GCacheNode
);
77 g_cache_node_destroy (GCacheNode
*node
)
79 g_slice_free (GCacheNode
, node
);
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
)
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
);
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
);
113 g_cache_destroy (GCache
*cache
)
115 g_return_if_fail (cache
!= NULL
);
117 g_hash_table_destroy (cache
->key_table
);
118 g_hash_table_destroy (cache
->value_table
);
119 g_slice_free (GCache
, cache
);
123 g_cache_insert (GCache
*cache
,
129 g_return_val_if_fail (cache
!= NULL
, NULL
);
131 node
= g_hash_table_lookup (cache
->key_table
, key
);
134 node
->ref_count
+= 1;
138 key
= (* cache
->key_dup_func
) (key
);
139 value
= (* cache
->value_new_func
) (key
);
140 node
= g_cache_node_new (value
);
142 g_hash_table_insert (cache
->key_table
, key
, node
);
143 g_hash_table_insert (cache
->value_table
, value
, key
);
149 g_cache_remove (GCache
*cache
,
155 g_return_if_fail (cache
!= NULL
);
157 key
= g_hash_table_lookup (cache
->value_table
, value
);
158 node
= g_hash_table_lookup (cache
->key_table
, key
);
160 g_return_if_fail (node
!= NULL
);
162 node
->ref_count
-= 1;
163 if (node
->ref_count
== 0)
165 g_hash_table_remove (cache
->value_table
, value
);
166 g_hash_table_remove (cache
->key_table
, key
);
168 (* cache
->key_destroy_func
) (key
);
169 (* cache
->value_destroy_func
) (node
->value
);
170 g_cache_node_destroy (node
);
175 g_cache_key_foreach (GCache
*cache
,
179 g_return_if_fail (cache
!= NULL
);
180 g_return_if_fail (func
!= NULL
);
182 g_hash_table_foreach (cache
->value_table
, func
, user_data
);
186 g_cache_value_foreach (GCache
*cache
,
190 g_return_if_fail (cache
!= NULL
);
191 g_return_if_fail (func
!= NULL
);
193 g_hash_table_foreach (cache
->key_table
, func
, user_data
);
196 #define __G_CACHE_C__
197 #include "galiasdef.c"