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"
23 #include "silcidcache.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 name_table
47 Hash table using the name 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 name_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 SilcUInt32 cache_dyn_count;
91 SilcUInt32 cache_count;
96 /* Allocates new ID cache object. The initial amount of allocated entries
97 can be sent as argument. If `count' is 0 the system uses default values.
98 The `id_type' defines the types of the ID's that will be saved to the
101 SilcIDCache silc_idcache_alloc(SilcUInt32 count, SilcIdType id_type,
102 SilcIDCacheDestructor destructor)
106 SILC_LOG_DEBUG(("Allocating new cache"));
108 cache = silc_calloc(1, sizeof(*cache));
111 cache->id_table = silc_hash_table_alloc(count, silc_hash_id,
112 (void *)(SilcUInt32)id_type,
113 silc_hash_id_compare,
114 (void *)(SilcUInt32)id_type,
115 silc_idcache_destructor, NULL, TRUE);
116 cache->name_table = silc_hash_table_alloc(count, silc_hash_string, NULL,
117 silc_hash_string_compare, NULL,
119 cache->context_table = silc_hash_table_alloc(count, silc_hash_ptr, NULL,
120 NULL, NULL, NULL, NULL, TRUE);
121 cache->destructor = destructor;
122 cache->type = id_type;
124 if (!cache->id_table || !cache->name_table || !cache->context_table) {
126 silc_hash_table_free(cache->id_table);
127 if (cache->name_table)
128 silc_hash_table_free(cache->name_table);
129 if (cache->context_table)
130 silc_hash_table_free(cache->context_table);
138 /* Frees ID cache object and cache entries */
140 void silc_idcache_free(SilcIDCache cache)
143 silc_hash_table_free(cache->id_table);
144 silc_hash_table_free(cache->name_table);
145 silc_hash_table_free(cache->context_table);
150 /* Add new entry to the cache. Returns TRUE if the entry was added and
151 FALSE if it could not be added. The `name' is the name associated with
152 the ID, the `id' the actual ID and the `context' a used specific context.
153 If the `expire' is TRUE the entry expires in default time and if FALSE
154 the entry never expires from the cache. */
156 bool silc_idcache_add(SilcIDCache cache, char *name, void *id,
157 void *context, int expire, SilcIDCacheEntry *ret)
161 SILC_LOG_DEBUG(("Adding cache entry"));
163 /* Allocate new cache entry */
164 c = silc_calloc(1, sizeof(*c));
170 c->context = context;
172 /* Add the new entry to the hash tables */
175 silc_hash_table_add(cache->id_table, id, c);
177 silc_hash_table_add(cache->name_table, name, c);
179 silc_hash_table_add(cache->context_table, context, c);
187 /* Destructor for the ID Cache entry */
189 static void silc_idcache_destructor(void *key, void *context,
195 /* Delete cache entry from cache. */
197 bool silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry old)
201 SILC_LOG_DEBUG(("Deleting cache entry"));
204 ret = silc_hash_table_del_by_context(cache->name_table, old->name, old);
206 ret = silc_hash_table_del(cache->context_table, old->context);
208 ret = silc_hash_table_del(cache->id_table, old->id);
215 /* Deletes ID cache entry by ID. */
217 bool silc_idcache_del_by_id(SilcIDCache cache, void *id)
221 if (!silc_hash_table_find(cache->id_table, id, NULL, (void *)&c))
224 return silc_idcache_del(cache, c);
227 /* Same as above but with specific hash and comparison functions. If the
228 functions are NULL then default values are used. */
230 bool silc_idcache_del_by_id_ext(SilcIDCache cache, void *id,
231 SilcHashFunction hash,
233 SilcHashCompare compare,
234 void *compare_context)
239 SILC_LOG_DEBUG(("Deleting cache entry"));
241 if (!silc_hash_table_find_ext(cache->id_table, id, NULL, (void *)&c,
242 hash, hash_context, compare,
247 ret = silc_hash_table_del_by_context(cache->name_table, c->name, c);
249 ret = silc_hash_table_del(cache->context_table, c->context);
251 ret = silc_hash_table_del_ext(cache->id_table, c->id, hash,
252 hash_context, compare, compare_context,
258 /* Deletes ID cache entry by context. */
260 bool silc_idcache_del_by_context(SilcIDCache cache, void *context)
265 SILC_LOG_DEBUG(("Deleting cache entry"));
267 if (!silc_hash_table_find(cache->context_table, context, NULL, (void *)&c))
271 ret = silc_hash_table_del_by_context(cache->name_table, c->name, c);
273 ret = silc_hash_table_del(cache->context_table, c->context);
275 ret = silc_hash_table_del_by_context(cache->id_table, c->id, c);
282 /* Deletes all ID entries from cache. Free's memory as well. */
284 bool silc_idcache_del_all(SilcIDCache cache)
286 silc_hash_table_free(cache->id_table);
287 silc_hash_table_free(cache->name_table);
288 silc_hash_table_free(cache->context_table);
293 static void silc_idcache_destructor_dummy(void *key, void *context,
296 /* Dummy - nothing */
299 /* Foreach callback fro silc_idcache_purge. */
301 static void silc_idcache_purge_foreach(void *key, void *context,
304 SilcIDCache cache = (SilcIDCache)user_context;
305 SilcUInt32 curtime = time(NULL);
306 SilcIDCacheEntry c = (SilcIDCacheEntry)context;
308 if (c->expire && c->expire < curtime) {
309 /* Remove the entry from the hash tables */
311 silc_hash_table_del_by_context(cache->name_table, c->name, c);
313 silc_hash_table_del(cache->context_table, c->context);
315 silc_hash_table_del_by_context_ext(cache->id_table, c->id, c,
316 NULL, NULL, NULL, NULL,
317 silc_idcache_destructor_dummy, NULL);
319 /* Call the destructor */
320 if (cache->destructor)
321 cache->destructor(cache, c);
323 /* Free the entry, it has been deleted from the hash tables */
328 /* Purges the cache by removing expired cache entires. Note that this
329 may be very slow operation. */
331 bool silc_idcache_purge(SilcIDCache cache)
333 silc_hash_table_foreach(cache->id_table, silc_idcache_purge_foreach, cache);
337 /* Purges the specific entry by context. */
339 bool silc_idcache_purge_by_context(SilcIDCache cache, void *context)
344 if (!silc_hash_table_find(cache->context_table, context, NULL,
348 /* Remove the entry from the hash tables */
350 ret = silc_hash_table_del_by_context(cache->name_table, c->name, c);
352 ret = silc_hash_table_del(cache->context_table, c->context);
355 silc_hash_table_del_by_context_ext(cache->id_table, c->id, c,
356 NULL, NULL, NULL, NULL,
357 silc_idcache_destructor_dummy, NULL);
359 /* Call the destructor */
360 if (cache->destructor)
361 cache->destructor(cache, c);
363 /* Free the entry, it has been deleted from the hash tables */
369 /* Callback that is called by the hash table routine when traversing
370 entrys in the hash table. */
372 static void silc_idcache_get_all_foreach(void *key, void *context,
375 SilcIDCacheList list = (SilcIDCacheList)user_context;
376 silc_idcache_list_add(list, (SilcIDCacheEntry)context);
379 /* Returns all cache entrys from the ID cache to the `ret' ID Cache List. */
381 bool silc_idcache_get_all(SilcIDCache cache, SilcIDCacheList *ret)
383 SilcIDCacheList list;
388 list = silc_idcache_list_alloc();
391 silc_hash_table_foreach(cache->id_table, silc_idcache_get_all_foreach, list);
393 if (silc_idcache_list_count(list) == 0) {
394 silc_idcache_list_free(list);
403 /* Find ID Cache entry by ID. May return multiple entries. */
405 bool silc_idcache_find_by_id(SilcIDCache cache, void *id,
406 SilcIDCacheList *ret)
408 SilcIDCacheList list;
410 list = silc_idcache_list_alloc();
417 silc_hash_table_find_foreach(cache->id_table, id,
418 silc_idcache_get_all_foreach, list);
420 if (silc_idcache_list_count(list) == 0) {
421 silc_idcache_list_free(list);
430 /* Find specific ID with specific hash function and comparison functions.
431 If `hash' is NULL then the default hash funtion is used and if `compare'
432 is NULL default comparison function is used. */
434 bool silc_idcache_find_by_id_one_ext(SilcIDCache cache, void *id,
435 SilcHashFunction hash,
437 SilcHashCompare compare,
438 void *compare_context,
439 SilcIDCacheEntry *ret)
441 return silc_hash_table_find_ext(cache->id_table, id, NULL, (void *)ret,
442 hash, hash_context, compare,
446 /* Find one specific ID entry. */
448 bool silc_idcache_find_by_id_one(SilcIDCache cache, void *id,
449 SilcIDCacheEntry *ret)
451 return silc_hash_table_find(cache->id_table, id, NULL, (void *)ret);
454 /* Finds cache entry by context. */
456 bool silc_idcache_find_by_context(SilcIDCache cache, void *context,
457 SilcIDCacheEntry *ret)
459 return silc_hash_table_find(cache->context_table, context, NULL,
463 /* Find ID Cache entry by name. Returns list of cache entries. */
465 bool silc_idcache_find_by_name(SilcIDCache cache, char *name,
466 SilcIDCacheList *ret)
468 SilcIDCacheList list;
470 list = silc_idcache_list_alloc();
477 silc_hash_table_find_foreach(cache->name_table, name,
478 silc_idcache_get_all_foreach, list);
480 if (silc_idcache_list_count(list) == 0) {
481 silc_idcache_list_free(list);
490 /* Find ID Cache entry by name. Returns one cache entry. */
492 bool silc_idcache_find_by_name_one(SilcIDCache cache, char *name,
493 SilcIDCacheEntry *ret)
495 if (!silc_hash_table_find(cache->name_table, name, NULL, (void *)ret))
501 /* Allocates ID cache list. */
503 static SilcIDCacheList silc_idcache_list_alloc()
505 SilcIDCacheList list;
507 list = silc_calloc(1, sizeof(*list));
514 /* Adds cache entry to the ID cache list. If needed reallocates memory
517 static void silc_idcache_list_add(SilcIDCacheList list, SilcIDCacheEntry cache)
521 /* Try to add to static cache */
522 if (!list->cache_dyn_count)
523 for (i = 0; i < (sizeof(list->cache) / sizeof(list->cache[0])); i++) {
524 if (!list->cache[i]) {
525 list->cache[i] = cache;
531 /* Static cache is full, allocate dynamic cache */
532 for (i = 0; i < list->cache_dyn_count; i++) {
533 if (!list->cache_dyn[i]) {
534 list->cache_dyn[i] = cache;
540 if (i >= list->cache_dyn_count) {
543 i = list->cache_dyn_count;
544 list->cache_dyn = silc_realloc(list->cache_dyn,
545 sizeof(*list->cache_dyn) * (i + 5));
546 if (!list->cache_dyn)
549 /* NULL the reallocated area */
550 for (k = i; k < (i + 5); k++)
551 list->cache_dyn[k] = NULL;
553 list->cache_dyn[i] = cache;
555 list->cache_dyn_count += 5;
559 /* Returns number of cache entries in the ID cache list. */
561 int silc_idcache_list_count(SilcIDCacheList list)
563 return list->cache_count;
566 /* Returns first entry from the ID cache list. */
568 bool silc_idcache_list_first(SilcIDCacheList list, SilcIDCacheEntry *ret)
572 if (!list->cache[list->pos])
576 *ret = list->cache[list->pos];
581 /* Returns next entry from the ID cache list. */
583 bool silc_idcache_list_next(SilcIDCacheList list, SilcIDCacheEntry *ret)
588 list->pos >= (sizeof(list->cache) / sizeof(list->cache[0]))) {
593 if (list->dyn && list->pos >= list->cache_dyn_count)
596 if (!list->dyn && !list->cache[list->pos])
599 if (list->dyn && !list->cache_dyn[list->pos])
604 *ret = list->cache[list->pos];
606 *ret = list->cache_dyn[list->pos];
612 /* Frees ID cache list. User must free the list object returned by
613 any of the searching functions. */
615 void silc_idcache_list_free(SilcIDCacheList list)
619 silc_free(list->cache_dyn);