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 uint32 cache_dyn_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(uint32 count, SilcIdType id_type,
102 SilcIDCacheDestructor destructor)
106 SILC_LOG_DEBUG(("Allocating new cache"));
108 cache = silc_calloc(1, sizeof(*cache));
109 cache->id_table = silc_hash_table_alloc(count, silc_hash_id,
110 (void *)(uint32)id_type,
111 silc_hash_id_compare,
112 (void *)(uint32)id_type,
113 silc_idcache_destructor, NULL,
115 cache->name_table = silc_hash_table_alloc(count, silc_hash_string, NULL,
116 silc_hash_string_compare, NULL,
118 cache->context_table = silc_hash_table_alloc(count, silc_hash_ptr, NULL,
119 NULL, NULL, NULL, NULL, FALSE);
120 cache->destructor = destructor;
121 cache->type = id_type;
126 /* Frees ID cache object and cache entries */
128 void silc_idcache_free(SilcIDCache cache)
131 silc_hash_table_free(cache->id_table);
132 silc_hash_table_free(cache->name_table);
133 silc_hash_table_free(cache->context_table);
138 /* Add new entry to the cache. Returns TRUE if the entry was added and
139 FALSE if it could not be added. The `name' is the name associated with
140 the ID, the `id' the actual ID and the `context' a used specific context.
141 If the `expire' is TRUE the entry expires in default time and if FALSE
142 the entry never expires from the cache. */
144 bool silc_idcache_add(SilcIDCache cache, char *name, void *id,
145 void *context, int expire, SilcIDCacheEntry *ret)
149 SILC_LOG_DEBUG(("Adding cache entry"));
151 /* Allocate new cache entry */
152 c = silc_calloc(1, sizeof(*c));
156 c->context = context;
158 /* Add the new entry to the hash tables */
161 silc_hash_table_add(cache->id_table, id, c);
163 silc_hash_table_add(cache->name_table, name, c);
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->name_table, 0);
172 silc_hash_table_rehash(cache->context_table, 0);
181 /* Destructor for the ID Cache entry */
183 static void silc_idcache_destructor(void *key, void *context,
189 /* Delete cache entry from cache. */
191 bool silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry old)
195 SILC_LOG_DEBUG(("Deleting cache entry"));
198 ret = silc_hash_table_del_by_context(cache->name_table, old->name, old);
200 ret = silc_hash_table_del(cache->context_table, old->context);
202 ret = silc_hash_table_del(cache->id_table, old->id);
209 /* Deletes ID cache entry by ID. */
211 bool silc_idcache_del_by_id(SilcIDCache cache, void *id)
215 if (!silc_hash_table_find(cache->id_table, id, NULL, (void *)&c))
218 return silc_idcache_del(cache, c);
221 /* Same as above but with specific hash and comparison functions. If the
222 functions are NULL then default values are used. */
224 bool silc_idcache_del_by_id_ext(SilcIDCache cache, void *id,
225 SilcHashFunction hash,
227 SilcHashCompare compare,
228 void *compare_context)
233 SILC_LOG_DEBUG(("Deleting cache entry"));
235 if (!silc_hash_table_find_ext(cache->id_table, id, NULL, (void *)&c,
236 hash, hash_context, compare,
241 ret = silc_hash_table_del_by_context(cache->name_table, c->name, c);
243 ret = silc_hash_table_del(cache->context_table, c->context);
245 ret = silc_hash_table_del_ext(cache->id_table, c->id, hash,
246 hash_context, compare, compare_context,
252 /* Deletes ID cache entry by context. */
254 bool silc_idcache_del_by_context(SilcIDCache cache, void *context)
259 SILC_LOG_DEBUG(("Deleting cache entry"));
261 if (!silc_hash_table_find(cache->context_table, context, NULL, (void *)&c))
265 ret = silc_hash_table_del_by_context(cache->name_table, c->name, c);
267 ret = silc_hash_table_del(cache->context_table, c->context);
269 ret = silc_hash_table_del_by_context(cache->id_table, c->id, c);
276 /* Deletes all ID entries from cache. Free's memory as well. */
278 bool silc_idcache_del_all(SilcIDCache cache)
280 silc_hash_table_free(cache->id_table);
281 silc_hash_table_free(cache->name_table);
282 silc_hash_table_free(cache->context_table);
287 static void silc_idcache_destructor_dummy(void *key, void *context,
290 /* Dummy - nothing */
293 /* Foreach callback fro silc_idcache_purge. */
295 static void silc_idcache_purge_foreach(void *key, void *context,
298 SilcIDCache cache = (SilcIDCache)user_context;
299 uint32 curtime = time(NULL);
300 SilcIDCacheEntry c = (SilcIDCacheEntry)context;
302 if (c->expire && c->expire < curtime) {
303 /* Remove the entry from the hash tables */
305 silc_hash_table_del_by_context(cache->name_table, c->name, c);
307 silc_hash_table_del(cache->context_table, c->context);
309 silc_hash_table_del_by_context_ext(cache->id_table, c->id, c,
310 NULL, NULL, NULL, NULL,
311 silc_idcache_destructor_dummy, NULL);
313 /* Call the destructor */
314 if (cache->destructor)
315 cache->destructor(cache, c);
317 /* Free the entry, it has been deleted from the hash tables */
322 /* Purges the cache by removing expired cache entires. Note that this
323 may be very slow operation. */
325 bool silc_idcache_purge(SilcIDCache cache)
327 silc_hash_table_foreach(cache->id_table, silc_idcache_purge_foreach, cache);
331 /* Purges the specific entry by context. */
333 bool silc_idcache_purge_by_context(SilcIDCache cache, void *context)
338 if (!silc_hash_table_find(cache->context_table, context, NULL,
342 /* Remove the entry from the hash tables */
344 ret = silc_hash_table_del_by_context(cache->name_table, c->name, c);
346 ret = silc_hash_table_del(cache->context_table, c->context);
349 silc_hash_table_del_by_context_ext(cache->id_table, c->id, c,
350 NULL, NULL, NULL, NULL,
351 silc_idcache_destructor_dummy, NULL);
353 /* Call the destructor */
354 if (cache->destructor)
355 cache->destructor(cache, c);
357 /* Free the entry, it has been deleted from the hash tables */
363 /* Callback that is called by the hash table routine when traversing
364 entrys in the hash table. */
366 static void silc_idcache_get_all_foreach(void *key, void *context,
369 SilcIDCacheList list = (SilcIDCacheList)user_context;
370 silc_idcache_list_add(list, (SilcIDCacheEntry)context);
373 /* Returns all cache entrys from the ID cache to the `ret' ID Cache List. */
375 bool silc_idcache_get_all(SilcIDCache cache, SilcIDCacheList *ret)
377 SilcIDCacheList list;
382 list = silc_idcache_list_alloc();
383 silc_hash_table_foreach(cache->id_table, silc_idcache_get_all_foreach, list);
385 if (silc_idcache_list_count(list) == 0) {
386 silc_idcache_list_free(list);
395 /* Find ID Cache entry by ID. May return multiple entries. */
397 bool silc_idcache_find_by_id(SilcIDCache cache, void *id,
398 SilcIDCacheList *ret)
400 SilcIDCacheList list;
402 list = silc_idcache_list_alloc();
407 silc_hash_table_find_foreach(cache->id_table, id,
408 silc_idcache_get_all_foreach, list);
410 if (silc_idcache_list_count(list) == 0) {
411 silc_idcache_list_free(list);
420 /* Find specific ID with specific hash function and comparison functions.
421 If `hash' is NULL then the default hash funtion is used and if `compare'
422 is NULL default comparison function is used. */
424 bool silc_idcache_find_by_id_one_ext(SilcIDCache cache, void *id,
425 SilcHashFunction hash,
427 SilcHashCompare compare,
428 void *compare_context,
429 SilcIDCacheEntry *ret)
431 return silc_hash_table_find_ext(cache->id_table, id, NULL, (void *)ret,
432 hash, hash_context, compare,
436 /* Find one specific ID entry. */
438 bool silc_idcache_find_by_id_one(SilcIDCache cache, void *id,
439 SilcIDCacheEntry *ret)
441 return silc_hash_table_find(cache->id_table, id, NULL, (void *)ret);
444 /* Finds cache entry by context. */
446 bool silc_idcache_find_by_context(SilcIDCache cache, void *context,
447 SilcIDCacheEntry *ret)
449 return silc_hash_table_find(cache->context_table, context, NULL,
453 /* Find ID Cache entry by name. Returns list of cache entries. */
455 bool silc_idcache_find_by_name(SilcIDCache cache, char *name,
456 SilcIDCacheList *ret)
458 SilcIDCacheList list;
460 list = silc_idcache_list_alloc();
465 silc_hash_table_find_foreach(cache->name_table, name,
466 silc_idcache_get_all_foreach, list);
468 if (silc_idcache_list_count(list) == 0) {
469 silc_idcache_list_free(list);
478 /* Find ID Cache entry by name. Returns one cache entry. */
480 bool silc_idcache_find_by_name_one(SilcIDCache cache, char *name,
481 SilcIDCacheEntry *ret)
483 if (!silc_hash_table_find(cache->name_table, name, NULL, (void *)ret))
489 /* Allocates ID cache list. */
491 static SilcIDCacheList silc_idcache_list_alloc()
493 SilcIDCacheList list;
495 list = silc_calloc(1, sizeof(*list));
500 /* Adds cache entry to the ID cache list. If needed reallocates memory
503 static void silc_idcache_list_add(SilcIDCacheList list, SilcIDCacheEntry cache)
507 /* Try to add to static cache */
508 if (!list->cache_dyn_count)
509 for (i = 0; i < (sizeof(list->cache) / sizeof(list->cache[0])); i++) {
510 if (!list->cache[i]) {
511 list->cache[i] = cache;
517 /* Static cache is full, allocate dynamic cache */
518 for (i = 0; i < list->cache_dyn_count; i++) {
519 if (!list->cache_dyn[i]) {
520 list->cache_dyn[i] = cache;
526 if (i >= list->cache_dyn_count) {
529 i = list->cache_dyn_count;
530 list->cache_dyn = silc_realloc(list->cache_dyn,
531 sizeof(*list->cache_dyn) * (i + 5));
533 /* NULL the reallocated area */
534 for (k = i; k < (i + 5); k++)
535 list->cache_dyn[k] = NULL;
537 list->cache_dyn[i] = cache;
539 list->cache_dyn_count += 5;
543 /* Returns number of cache entries in the ID cache list. */
545 int silc_idcache_list_count(SilcIDCacheList list)
547 return list->cache_count;
550 /* Returns first entry from the ID cache list. */
552 bool silc_idcache_list_first(SilcIDCacheList list, SilcIDCacheEntry *ret)
556 if (!list->cache[list->pos])
560 *ret = list->cache[list->pos];
565 /* Returns next entry from the ID cache list. */
567 bool silc_idcache_list_next(SilcIDCacheList list, SilcIDCacheEntry *ret)
572 list->pos >= (sizeof(list->cache) / sizeof(list->cache[0]))) {
577 if (list->dyn && list->pos >= list->cache_dyn_count)
580 if (!list->dyn && !list->cache[list->pos])
583 if (list->dyn && !list->cache_dyn[list->pos])
588 *ret = list->cache[list->pos];
590 *ret = list->cache_dyn[list->pos];
596 /* Frees ID cache list. User must free the list object returned by
597 any of the searching functions. */
599 void silc_idcache_list_free(SilcIDCacheList list)
603 silc_free(list->cache_dyn);