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
;
68 static GCacheNode
* g_cache_node_new (gpointer value
);
69 static void g_cache_node_destroy (GCacheNode
*node
);
72 static GMemChunk
*node_mem_chunk
= NULL
;
73 G_LOCK_DEFINE_STATIC (node_mem_chunk
);
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
)
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
);
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
);
106 g_cache_destroy (GCache
*cache
)
108 g_return_if_fail (cache
!= NULL
);
110 g_hash_table_destroy (cache
->key_table
);
111 g_hash_table_destroy (cache
->value_table
);
116 g_cache_insert (GCache
*cache
,
122 g_return_val_if_fail (cache
!= NULL
, NULL
);
124 node
= g_hash_table_lookup (cache
->key_table
, key
);
127 node
->ref_count
+= 1;
131 key
= (* cache
->key_dup_func
) (key
);
132 value
= (* cache
->value_new_func
) (key
);
133 node
= g_cache_node_new (value
);
135 g_hash_table_insert (cache
->key_table
, key
, node
);
136 g_hash_table_insert (cache
->value_table
, value
, key
);
142 g_cache_remove (GCache
*cache
,
148 g_return_if_fail (cache
!= NULL
);
150 key
= g_hash_table_lookup (cache
->value_table
, value
);
151 node
= g_hash_table_lookup (cache
->key_table
, key
);
153 g_return_if_fail (node
!= NULL
);
155 node
->ref_count
-= 1;
156 if (node
->ref_count
== 0)
158 g_hash_table_remove (cache
->value_table
, value
);
159 g_hash_table_remove (cache
->key_table
, key
);
161 (* cache
->key_destroy_func
) (key
);
162 (* cache
->value_destroy_func
) (node
->value
);
163 g_cache_node_destroy (node
);
168 g_cache_key_foreach (GCache
*cache
,
172 g_return_if_fail (cache
!= NULL
);
173 g_return_if_fail (func
!= NULL
);
175 g_hash_table_foreach (cache
->value_table
, func
, user_data
);
179 g_cache_value_foreach (GCache
*cache
,
183 g_return_if_fail (cache
!= NULL
);
184 g_return_if_fail (func
!= NULL
);
186 g_hash_table_foreach (cache
->key_table
, func
, user_data
);
191 g_cache_node_new (gpointer value
)
195 G_LOCK (node_mem_chunk
);
197 node_mem_chunk
= g_mem_chunk_new ("cache node mem chunk", sizeof (GCacheNode
),
198 1024, G_ALLOC_AND_FREE
);
200 node
= g_chunk_new (GCacheNode
, node_mem_chunk
);
201 G_UNLOCK (node_mem_chunk
);
210 g_cache_node_destroy (GCacheNode
*node
)
212 G_LOCK (node_mem_chunk
);
213 g_mem_chunk_free (node_mem_chunk
, node
);
214 G_UNLOCK (node_mem_chunk
);