5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 2000 - 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
22 #include "silcincludes.h"
25 /* Static prototypes */
26 static void silc_idcache_destructor(void *key, void *context,
28 static SilcIDCacheList silc_idcache_list_alloc();
29 static void silc_idcache_list_add(SilcIDCacheList list,
30 SilcIDCacheEntry cache);
35 This is context for the ID cache system. This includes all the cache
36 entries and other internal data. This is read-only object and not
37 visible outside this cache system.
39 Fields are as follows:
41 SilcHashTable id_table
43 Hash table using the ID as the key.
45 SilcHashTable data_table
47 Hash table using the data as the key.
49 SilcHashTable context_table
51 Hash table using the context as the key.
53 SilcIDCacheDestructor destructor
55 Destructor callback that is called when an cache entry expires or is
56 purged from the ID cache. The application must not free cache entry
57 because the library will do it automatically. The appliation, however,
58 is responsible of freeing any data in the entry.
62 Indicates the type of the ID's this cache holds.
65 struct SilcIDCacheStruct {
66 SilcHashTable id_table;
67 SilcHashTable data_table;
68 SilcHashTable context_table;
69 SilcIDCacheDestructor destructor;
76 This is returned when searching the cache. Enumeration functions are
77 provided to traverse the list; actually this is used as table not as
80 By default the found cache entries are saved into the static cache
81 table to provide access without reallocation. However, if the static
82 table is full, rest of the cache entries are dynamically allocated
83 into `cache_dyn' table. Traversing functions automatically handles
87 struct SilcIDCacheListStruct {
88 SilcIDCacheEntry cache[64];
89 SilcIDCacheEntry *cache_dyn;
90 uint32 cache_dyn_count;
95 /* Allocates new ID cache object. The initial amount of allocated entries
96 can be sent as argument. If `count' is 0 the system uses default values.
97 The `id_type' defines the types of the ID's that will be saved to the
100 SilcIDCache silc_idcache_alloc(uint32 count, SilcIdType id_type,
101 SilcIDCacheDestructor destructor)
105 SILC_LOG_DEBUG(("Allocating new cache"));
107 cache = silc_calloc(1, sizeof(*cache));
108 cache->id_table = silc_hash_table_alloc(count, silc_hash_id,
109 (void *)(uint32)id_type,
110 silc_hash_id_compare,
111 (void *)(uint32)id_type,
112 silc_idcache_destructor, NULL);
113 cache->data_table = silc_hash_table_alloc(count, silc_hash_data, NULL,
114 silc_hash_data_compare, NULL,
116 cache->context_table = silc_hash_table_alloc(count, silc_hash_ptr, NULL,
117 NULL, NULL, NULL, NULL);
118 cache->destructor = destructor;
119 cache->type = id_type;
124 /* Free's ID cache object and cache entries */
126 void silc_idcache_free(SilcIDCache cache)
129 silc_hash_table_free(cache->id_table);
130 silc_hash_table_free(cache->data_table);
131 silc_hash_table_free(cache->context_table);
136 /* Add new entry to the cache. */
138 bool silc_idcache_add(SilcIDCache cache, unsigned char *data,
139 uint32 data_len, void *id, void *context, int expire)
142 uint32 curtime = time(NULL);
144 SILC_LOG_DEBUG(("Adding cache entry"));
146 /* See if entry with this ID already exists. */
147 if (silc_hash_table_find(cache->id_table, id, NULL, NULL))
150 /* Allocate new cache entry */
151 c = silc_calloc(1, sizeof(*c));
153 c->data_len = data_len;
155 c->expire = (expire ? (curtime + SILC_ID_CACHE_EXPIRE) : 0);
156 c->context = context;
158 /* Add the new entry to the hash tables */
160 silc_hash_table_add(cache->id_table, id, c);
162 silc_hash_table_add_ext(cache->data_table, data, c,
163 silc_hash_data, (void *)data_len);
165 silc_hash_table_add(cache->context_table, context, c);
167 /* See whether we have time to rehash the tables */
168 if ((silc_hash_table_count(cache->id_table) * 2) >
169 silc_hash_table_size(cache->id_table)) {
170 silc_hash_table_rehash(cache->id_table, 0);
171 silc_hash_table_rehash(cache->data_table, 0);
172 silc_hash_table_rehash(cache->context_table, 0);
178 /* Destructor for the ID Cache entry */
180 static void silc_idcache_destructor(void *key, void *context,
186 /* Delete cache entry from cache. */
188 bool silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry old)
192 ret = silc_hash_table_del_by_context_ext(cache->data_table, old->data, old,
194 (void *)old->data_len,
195 silc_hash_data_compare,
196 (void *)old->data_len);
197 ret = silc_hash_table_del(cache->context_table, old->context);
198 ret = silc_hash_table_del(cache->id_table, old->id);
203 /* Deletes ID cache entry by ID. */
205 bool silc_idcache_del_by_id(SilcIDCache cache, void *id)
209 if (!silc_hash_table_find(cache->id_table, id, NULL, (void *)&c))
212 return silc_idcache_del(cache, c);
215 /* Deletes all ID entries from cache. Free's memory as well. */
217 bool silc_idcache_del_all(SilcIDCache cache)
219 silc_hash_table_free(cache->id_table);
220 silc_hash_table_free(cache->data_table);
221 silc_hash_table_free(cache->context_table);
226 /* Foreach callback fro silc_idcache_purge. */
228 static void silc_idcache_purge_foreach(void *key, void *context,
231 SilcIDCache cache = (SilcIDCache)user_context;
232 uint32 curtime = time(NULL);
233 SilcIDCacheEntry c = (SilcIDCacheEntry)context;
235 if (c->expire && c->expire < curtime) {
236 /* Call the destructor */
237 if (cache->destructor)
238 cache->destructor(cache, c);
240 /* Delete the entry */
241 silc_idcache_del(cache, c);
245 /* Purges the cache by removing expired cache entires. Note that this
246 may be very slow operation. */
248 bool silc_idcache_purge(SilcIDCache cache)
250 silc_hash_table_foreach(cache->id_table, silc_idcache_purge_foreach, cache);
254 /* Purges the specific entry by context. */
256 bool silc_idcache_purge_by_context(SilcIDCache cache, void *context)
258 SilcIDCacheEntry entry;
260 if (!silc_hash_table_find(cache->context_table, context, NULL,
264 /* Call the destructor */
265 if (cache->destructor)
266 cache->destructor(cache, entry);
268 return silc_idcache_del(cache, entry);
271 /* Callback that is called by the hash table routine when traversing
272 entrys in the hash table. */
274 static void silc_idcache_get_all_foreach(void *key, void *context,
277 SilcIDCacheList list = (SilcIDCacheList)user_context;
278 silc_idcache_list_add(list, (SilcIDCacheEntry)context);
281 /* Returns all cache entrys from the ID cache to the `ret' ID Cache List. */
283 bool silc_idcache_get_all(SilcIDCache cache, SilcIDCacheList *ret)
285 SilcIDCacheList list;
290 list = silc_idcache_list_alloc();
291 silc_hash_table_foreach(cache->id_table, silc_idcache_get_all_foreach, list);
293 if (silc_idcache_list_count(list) == 0) {
294 silc_idcache_list_free(list);
303 /* Find ID Cache entry by ID. */
305 bool silc_idcache_find_by_id(SilcIDCache cache, void *id,
306 SilcIDCacheEntry *ret)
308 return silc_hash_table_find(cache->id_table, id, NULL, (void *)ret);
311 /* Finds cache entry by context. */
313 bool silc_idcache_find_by_context(SilcIDCache cache, void *context,
314 SilcIDCacheEntry *ret)
316 return silc_hash_table_find(cache->context_table, context, NULL,
320 /* Find ID Cache entry by data. The data maybe anything that must
321 match exactly. Returns list of cache entries. */
323 bool silc_idcache_find_by_data(SilcIDCache cache, unsigned char *data,
324 unsigned int data_len, SilcIDCacheList *ret)
326 SilcIDCacheList list;
328 list = silc_idcache_list_alloc();
333 silc_hash_table_find_foreach_ext(cache->data_table, data,
334 silc_hash_data, (void *)data_len,
335 silc_hash_data_compare, (void *)data_len,
336 silc_idcache_get_all_foreach, list);
338 if (silc_idcache_list_count(list) == 0) {
339 silc_idcache_list_free(list);
348 /* Find ID Cache entry by data. The data maybe anything that must
349 match exactly. Returns one cache entry. */
351 bool silc_idcache_find_by_data_one(SilcIDCache cache, unsigned char *data,
352 unsigned int data_len,
353 SilcIDCacheEntry *ret)
357 if (!silc_hash_table_find_ext(cache->data_table, data, NULL, (void *)&c,
358 silc_hash_data, (void *)data_len,
359 silc_hash_data_compare, (void *)data_len))
368 /* Allocates ID cache list. */
370 static SilcIDCacheList silc_idcache_list_alloc()
372 SilcIDCacheList list;
374 list = silc_calloc(1, sizeof(*list));
379 /* Adds cache entry to the ID cache list. If needed reallocates memory
382 static void silc_idcache_list_add(SilcIDCacheList list, SilcIDCacheEntry cache)
386 /* Try to add to static cache */
387 if (!list->cache_dyn_count)
388 for (i = 0; i < sizeof(list->cache); i++) {
389 if (!list->cache[i]) {
390 list->cache[i] = cache;
396 /* Static cache is full, allocate dynamic cache */
397 for (i = 0; i < list->cache_dyn_count; i++) {
398 if (!list->cache_dyn[i]) {
399 list->cache_dyn[i] = cache;
405 if (i >= list->cache_dyn_count) {
409 list->cache_dyn = silc_realloc(list->cache_dyn,
410 sizeof(*list->cache) * (i));
412 /* NULL the reallocated area */
413 for (k = list->cache_dyn_count; k < i; k++)
414 list->cache_dyn[k] = NULL;
416 list->cache_dyn[list->cache_dyn_count] = cache;
417 list->cache_dyn_count = i;
422 /* Returns number of cache entries in the ID cache list. */
424 int silc_idcache_list_count(SilcIDCacheList list)
426 return list->cache_count;
429 /* Returns first entry from the ID cache list. */
431 bool silc_idcache_list_first(SilcIDCacheList list, SilcIDCacheEntry *ret)
435 if (!list->cache[list->pos])
439 *ret = list->cache[list->pos];
444 /* Returns next entry from the ID cache list. */
446 bool silc_idcache_list_next(SilcIDCacheList list, SilcIDCacheEntry *ret)
451 if (list->pos >= sizeof(list->cache)) {
456 if (dyn && list->pos >= list->cache_dyn_count)
459 if (!dyn && !list->cache[list->pos])
462 if (dyn && !list->cache_dyn[list->pos])
467 *ret = list->cache[list->pos];
469 *ret = list->cache_dyn[list->pos];
475 /* Free's ID cache list. User must free the list object returned by
476 any of the searching functions. */
478 void silc_idcache_list_free(SilcIDCacheList list)
482 silc_free(list->cache_dyn);