Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / glib / gcache.c
blobbba791024855e5a6eb5d58b4f46488a08d01978e
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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
27 /*
28 * MT safe
31 #include "glib.h"
34 typedef struct _GCacheNode GCacheNode;
35 typedef struct _GRealCache GRealCache;
37 struct _GCacheNode
39 /* A reference counted node */
40 gpointer value;
41 gint ref_count;
44 struct _GRealCache
46 /* Called to create a value from a key */
47 GCacheNewFunc value_new_func;
49 /* Called to destroy a value */
50 GCacheDestroyFunc value_destroy_func;
52 /* Called to duplicate a key */
53 GCacheDupFunc key_dup_func;
55 /* Called to destroy a key */
56 GCacheDestroyFunc key_destroy_func;
58 /* Associates keys with nodes */
59 GHashTable *key_table;
61 /* Associates nodes with keys */
62 GHashTable *value_table;
66 static GCacheNode* g_cache_node_new (gpointer value);
67 static void g_cache_node_destroy (GCacheNode *node);
70 static GMemChunk *node_mem_chunk = NULL;
71 G_LOCK_DEFINE_STATIC (node_mem_chunk);
73 GCache*
74 g_cache_new (GCacheNewFunc value_new_func,
75 GCacheDestroyFunc value_destroy_func,
76 GCacheDupFunc key_dup_func,
77 GCacheDestroyFunc key_destroy_func,
78 GHashFunc hash_key_func,
79 GHashFunc hash_value_func,
80 GCompareFunc key_compare_func)
82 GRealCache *cache;
84 g_return_val_if_fail (value_new_func != NULL, NULL);
85 g_return_val_if_fail (value_destroy_func != NULL, NULL);
86 g_return_val_if_fail (key_dup_func != NULL, NULL);
87 g_return_val_if_fail (key_destroy_func != NULL, NULL);
88 g_return_val_if_fail (hash_key_func != NULL, NULL);
89 g_return_val_if_fail (hash_value_func != NULL, NULL);
90 g_return_val_if_fail (key_compare_func != NULL, NULL);
92 cache = g_new (GRealCache, 1);
93 cache->value_new_func = value_new_func;
94 cache->value_destroy_func = value_destroy_func;
95 cache->key_dup_func = key_dup_func;
96 cache->key_destroy_func = key_destroy_func;
97 cache->key_table = g_hash_table_new (hash_key_func, key_compare_func);
98 cache->value_table = g_hash_table_new (hash_value_func, NULL);
100 return (GCache*) cache;
103 void
104 g_cache_destroy (GCache *cache)
106 GRealCache *rcache;
108 g_return_if_fail (cache != NULL);
110 rcache = (GRealCache*) cache;
111 g_hash_table_destroy (rcache->key_table);
112 g_hash_table_destroy (rcache->value_table);
113 g_free (rcache);
116 gpointer
117 g_cache_insert (GCache *cache,
118 gpointer key)
120 GRealCache *rcache;
121 GCacheNode *node;
122 gpointer value;
124 g_return_val_if_fail (cache != NULL, NULL);
126 rcache = (GRealCache*) cache;
128 node = g_hash_table_lookup (rcache->key_table, key);
129 if (node)
131 node->ref_count += 1;
132 return node->value;
135 key = (* rcache->key_dup_func) (key);
136 value = (* rcache->value_new_func) (key);
137 node = g_cache_node_new (value);
139 g_hash_table_insert (rcache->key_table, key, node);
140 g_hash_table_insert (rcache->value_table, value, key);
142 return node->value;
145 void
146 g_cache_remove (GCache *cache,
147 gpointer value)
149 GRealCache *rcache;
150 GCacheNode *node;
151 gpointer key;
153 g_return_if_fail (cache != NULL);
155 rcache = (GRealCache*) cache;
157 key = g_hash_table_lookup (rcache->value_table, value);
158 node = g_hash_table_lookup (rcache->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 (rcache->value_table, value);
166 g_hash_table_remove (rcache->key_table, key);
168 (* rcache->key_destroy_func) (key);
169 (* rcache->value_destroy_func) (node->value);
170 g_cache_node_destroy (node);
174 void
175 g_cache_key_foreach (GCache *cache,
176 GHFunc func,
177 gpointer user_data)
179 GRealCache *rcache;
181 g_return_if_fail (cache != NULL);
182 g_return_if_fail (func != NULL);
184 rcache = (GRealCache*) cache;
186 g_hash_table_foreach (rcache->value_table, func, user_data);
189 void
190 g_cache_value_foreach (GCache *cache,
191 GHFunc func,
192 gpointer user_data)
194 GRealCache *rcache;
196 g_return_if_fail (cache != NULL);
197 g_return_if_fail (func != NULL);
199 rcache = (GRealCache*) cache;
201 g_hash_table_foreach (rcache->key_table, func, user_data);
205 static GCacheNode*
206 g_cache_node_new (gpointer value)
208 GCacheNode *node;
210 G_LOCK (node_mem_chunk);
211 if (!node_mem_chunk)
212 node_mem_chunk = g_mem_chunk_new ("cache node mem chunk", sizeof (GCacheNode),
213 1024, G_ALLOC_AND_FREE);
215 node = g_chunk_new (GCacheNode, node_mem_chunk);
216 G_UNLOCK (node_mem_chunk);
218 node->value = value;
219 node->ref_count = 1;
221 return node;
224 static void
225 g_cache_node_destroy (GCacheNode *node)
227 G_LOCK (node_mem_chunk);
228 g_mem_chunk_free (node_mem_chunk, node);
229 G_UNLOCK (node_mem_chunk);