5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 2000 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 int silc_idcache_sorter(const void *a, const void *b);
27 static SilcIDCacheList silc_idcache_list_alloc();
28 static void silc_idcache_list_add(SilcIDCacheList list,
29 SilcIDCacheEntry cache);
34 This is context for the ID cache system. This includes all the cache
35 entries and other internal data. This is read-only object and not
36 visible outside this cache system.
38 Fields are as follows:
40 SilcIDCacheEntry cache
42 Table of the cache entries allocated by silc_idcache_add function.
43 This table is reallocated when new entry is added into the cache.
47 Number of cache entries in the cache.
51 Boolean value to indicate whether the cache is sorted or not. If
52 cache is not sorted the fast access (described next) cannot be used.
53 Sorting can be done by calling sorting function or when adding new
58 Table to provide fast access into the cache by index. When searching
59 by data this table is used to get the index to the first occurence
60 of that data (or part of the data) in the cache. Purpose of this
61 is to provide faster access to the cache when searching by data.
62 This is updated by silc_idcache_add and sorting functions.
64 SilcIDCacheDestructor destructor
66 Destructor callback that is called when an cache entry expires or is
67 purged from the ID cache. The application must not free cache entry
68 because the library will do it automatically. The appliation, however,
69 is responsible of freeing any data in the entry.
72 struct SilcIDCacheStruct {
73 SilcIDCacheEntry cache;
77 SilcIDCacheDestructor destructor;
83 This is returned when searching the cache. Enumeration functions are
84 provided to traverse the list; actually this is used as table not as
87 By default the found cache entries are saved into the static cache
88 table to provide access without reallocation. However, if the static
89 table is full, rest of the cache entries are dynamically allocated
90 into `cache_dyn' table. Traversing functions automatically handles
94 struct SilcIDCacheListStruct {
95 SilcIDCacheEntry cache[64];
96 SilcIDCacheEntry *cache_dyn;
97 uint32 cache_dyn_count;
102 /* Allocates new ID cache object. The initial amount of allocated entries
103 can be sent as argument. If `count' is 0 the system uses default values. */
105 SilcIDCache silc_idcache_alloc(uint32 count,
106 SilcIDCacheDestructor destructor)
110 SILC_LOG_DEBUG(("Allocating new cache"));
112 cache = silc_calloc(1, sizeof(*cache));
113 cache->cache = silc_calloc(count ? count : 5, sizeof(*cache->cache));
114 cache->cache_count = count ? count : 5;
115 memset(cache->fast_access, -1, sizeof(cache->fast_access));
116 cache->destructor = destructor;
121 /* Free's ID cache object and cache entries */
123 void silc_idcache_free(SilcIDCache cache)
126 silc_free(cache->cache);
131 /* qsort() sorter function. */
133 static int silc_idcache_sorter(const void *a, const void *b)
135 SilcIDCacheEntry a1, b1;
137 a1 = (SilcIDCacheEntry)a;
138 b1 = (SilcIDCacheEntry)b;
140 if (!a1->data && !b1->data)
149 return a1->data[0] - b1->data[0];
152 /* Sorts given cache by data. After sorting this updates the fast access
153 table that can be used to access the cache faster. */
155 void silc_idcache_sort_by_data(SilcIDCache cache)
159 qsort(cache->cache, cache->cache_count,
160 sizeof(*cache->cache), silc_idcache_sorter);
162 memset(cache->fast_access, -1, sizeof(cache->fast_access));
164 /* Update the fast access table (this of course takes its own time when
165 the cache is very large). */
166 for (i = 0; i < cache->cache_count; i++) {
167 if (cache->cache[i].data &&
168 cache->fast_access[(int)cache->cache[i].data[0]] == -1)
169 cache->fast_access[(int)cache->cache[i].data[0]] = i;
172 cache->sorted = TRUE;
175 /* Find ID Cache entry by data. The data maybe anything that must
176 match exactly. Returns list of cache entries. */
178 int silc_idcache_find_by_data(SilcIDCache cache, unsigned char *data,
179 SilcIDCacheList *ret)
182 SilcIDCacheList list;
184 if (!cache || !cache->cache || !data)
187 list = silc_idcache_list_alloc();
190 i = cache->fast_access[(int)data[0]];
197 for (i = i; i < cache->cache_count; i++) {
198 if (cache->sorted && cache->cache[i].data &&
199 cache->cache[i].data[0] != data[0])
202 if (cache->cache[i].data &&
203 !memcmp(cache->cache[i].data, data, cache->cache[i].data_len))
204 silc_idcache_list_add(list, &(cache->cache[i]));
207 if (!silc_idcache_list_count(list)) {
208 silc_idcache_list_free(list);
215 silc_idcache_list_free(list);
220 /* Find ID Cache entry by data. The data maybe anything that must
221 match exactly. Returns one cache entry. */
223 int silc_idcache_find_by_data_one(SilcIDCache cache, unsigned char *data,
224 SilcIDCacheEntry *ret)
228 if (!cache || !cache->cache || !data)
232 i = cache->fast_access[(int)data[0]];
239 for (i = i; i < cache->cache_count; i++)
240 if (cache->cache[i].data &&
241 !memcmp(cache->cache[i].data, data, cache->cache[i].data_len)) {
243 *ret = &(cache->cache[i]);
250 /* Find ID Cache entry by data, loosely. The data don't have to be 100%
251 match. This ignores data case-sensitivity when searching with this
252 function. Returns list of cache entries. */
254 int silc_idcache_find_by_data_loose(SilcIDCache cache, unsigned char *data,
255 SilcIDCacheList *ret)
258 SilcIDCacheList list;
260 if (!cache || !cache->cache || !data)
263 list = silc_idcache_list_alloc();
265 c = tolower((int)data[0]);
268 i = cache->fast_access[c];
275 for (i = i; i < cache->cache_count; i++) {
276 if (cache->sorted && cache->cache[i].data &&
277 cache->cache[i].data[0] != (char)c)
280 if (cache->cache[i].data &&
281 !strcasecmp(cache->cache[i].data, data))
282 silc_idcache_list_add(list, &(cache->cache[i]));
286 c = toupper((int)data[0]);
287 i = cache->fast_access[c];
289 for (i = i; i < cache->cache_count; i++) {
290 if (cache->sorted && cache->cache[i].data &&
291 cache->cache[i].data[0] != (char)c)
294 if (cache->cache[i].data &&
295 !strcasecmp(cache->cache[i].data, data))
296 silc_idcache_list_add(list, &(cache->cache[i]));
300 if (!silc_idcache_list_count(list)) {
301 silc_idcache_list_free(list);
308 silc_idcache_list_free(list);
313 /* Find ID Cache entry by ID. Returns list of cache entries. If `id' is
314 SILC_ID_CACHE_ANY this returns all ID's of type `type'. */
316 int silc_idcache_find_by_id(SilcIDCache cache, void *id, SilcIdType type,
317 SilcIDCacheList *ret)
320 SilcIDCacheList list;
322 if (!cache || !cache->cache || !id)
325 list = silc_idcache_list_alloc();
327 if (id != SILC_ID_CACHE_ANY) {
328 for (i = 0; i < cache->cache_count; i++)
329 if (cache->cache[i].id && silc_id_compare(cache->cache[i].id, id, type))
330 silc_idcache_list_add(list, &(cache->cache[i]));
332 for (i = 0; i < cache->cache_count; i++)
333 if (cache->cache[i].id && cache->cache[i].type == type)
334 silc_idcache_list_add(list, &(cache->cache[i]));
337 if (!silc_idcache_list_count(list)) {
338 silc_idcache_list_free(list);
345 silc_idcache_list_free(list);
350 /* Find ID Cache entry by ID. Returns one cache entry. */
352 int silc_idcache_find_by_id_one(SilcIDCache cache, void *id, SilcIdType type,
353 SilcIDCacheEntry *ret)
357 if (!cache || !cache->cache || !id)
360 for (i = 0; i < cache->cache_count; i++)
361 if (cache->cache[i].id && silc_id_compare(cache->cache[i].id, id, type)) {
363 *ret = &(cache->cache[i]);
370 /* Finds cache entry by context. */
372 int silc_idcache_find_by_context(SilcIDCache cache, void *context,
373 SilcIDCacheEntry *ret)
377 if (!cache || !cache->cache || !context)
380 for (i = 0; i < cache->cache_count; i++)
381 if (cache->cache[i].context && cache->cache[i].context == context) {
383 *ret = &(cache->cache[i]);
390 /* Add new entry to the cache. Returns TRUE or FALSE. If `sort' is TRUE
391 then the cache is sorted after the new entry has been added. The
392 cache must be sorted in order for the fast access feature to work,
393 however, it is not mandatory. */
395 int silc_idcache_add(SilcIDCache cache, unsigned char *data,
396 uint32 data_len, SilcIdType id_type, void *id,
397 void *context, int sort, int expire)
401 uint32 curtime = time(NULL);
404 if (!cache || !cache->cache)
407 SILC_LOG_DEBUG(("Adding cache entry"));
410 count = cache->cache_count;
413 c = silc_calloc(5, sizeof(*c));
417 /* See if it exists already */
418 /* XXX this slows down this function. */
419 if (silc_idcache_find_by_id(cache, id, id_type, NULL))
422 for (i = 0; i < count; i++) {
423 if (c[i].data == NULL && c[i].id == NULL) {
425 c[i].data_len = data_len;
428 c[i].expire = (expire ? (curtime + SILC_ID_CACHE_EXPIRE) : 0);
429 c[i].context = context;
435 c = silc_realloc(c, sizeof(*c) * (count + 5));
436 for (i = count; i < count + 5; i++) {
440 c[count].data = data;
441 c[count].data_len = data_len;
442 c[count].type = id_type;
444 c[count].expire = (expire ? (curtime + SILC_ID_CACHE_EXPIRE) : 0);
445 c[count].context = context;
450 cache->cache_count = count;
451 cache->sorted = sort;
454 silc_idcache_sort_by_data(cache);
459 /* Delete cache entry from cache. */
462 int silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry old)
470 int silc_idcache_del_by_data(SilcIDCache cache, unsigned char *data)
476 /* Deletes ID cache entry by ID. */
478 int silc_idcache_del_by_id(SilcIDCache cache, SilcIdType type, void *id)
482 if (!cache || !cache->cache || !id)
485 for (i = 0; i < cache->cache_count; i++)
486 if (cache->cache[i].id && silc_id_compare(cache->cache[i].id, id, type)) {
487 cache->cache[i].id = NULL;
488 cache->cache[i].data = NULL;
489 cache->cache[i].type = 0;
490 cache->cache[i].context = NULL;
497 /* Deletes all ID entries from cache. Free's memory as well. */
499 int silc_idcache_del_all(SilcIDCache cache)
501 if (!cache || !cache->cache)
504 silc_free(cache->cache);
506 cache->cache_count = 0;
511 /* Purges the cache by removing expired cache entires. This does not
512 free any memory though. */
514 int silc_idcache_purge(SilcIDCache cache)
517 uint32 curtime = time(NULL);
520 if (!cache || !cache->cache)
525 for (i = 0; i < cache->cache_count; i++) {
526 if (c[i].data && c[i].expire && c[i].expire < curtime) {
528 /* Call the destructor */
529 if (cache->destructor)
530 cache->destructor(cache, &c[i]);
543 /* Purges the specific entry by context. */
545 int silc_idcache_purge_by_context(SilcIDCache cache, void *context)
547 SilcIDCacheEntry entry;
549 if (!silc_idcache_find_by_context(cache, context, &entry))
552 /* Call the destructor */
553 if (cache->destructor)
554 cache->destructor(cache, entry);
560 entry->context = NULL;
564 /* Allocates ID cache list. */
566 static SilcIDCacheList silc_idcache_list_alloc()
568 SilcIDCacheList list;
570 list = silc_calloc(1, sizeof(*list));
575 /* Adds cache entry to the ID cache list. If needed reallocates memory
578 static void silc_idcache_list_add(SilcIDCacheList list, SilcIDCacheEntry cache)
582 /* Try to add to static cache */
583 if (!list->cache_dyn_count)
584 for (i = 0; i < sizeof(list->cache); i++) {
585 if (!list->cache[i]) {
586 list->cache[i] = cache;
592 /* Static cache is full, allocate dynamic cache */
593 for (i = 0; i < list->cache_dyn_count; i++) {
594 if (!list->cache_dyn[i]) {
595 list->cache_dyn[i] = cache;
601 if (i >= list->cache_dyn_count) {
605 list->cache_dyn = silc_realloc(list->cache_dyn,
606 sizeof(*list->cache) * (i));
608 /* NULL the reallocated area */
609 for (k = list->cache_dyn_count; k < i; k++)
610 list->cache_dyn[k] = NULL;
612 list->cache_dyn[list->cache_dyn_count] = cache;
613 list->cache_dyn_count = i;
618 /* Returns number of cache entries in the ID cache list. */
620 int silc_idcache_list_count(SilcIDCacheList list)
622 return list->cache_count;
625 /* Returns first entry from the ID cache list. */
627 int silc_idcache_list_first(SilcIDCacheList list, SilcIDCacheEntry *ret)
631 if (!list->cache[list->pos])
635 *ret = list->cache[list->pos];
640 /* Returns next entry from the ID cache list. */
642 int silc_idcache_list_next(SilcIDCacheList list, SilcIDCacheEntry *ret)
647 if (list->pos >= sizeof(list->cache)) {
652 if (dyn && list->pos >= list->cache_dyn_count)
655 if (!dyn && !list->cache[list->pos])
658 if (dyn && !list->cache_dyn[list->pos])
663 *ret = list->cache[list->pos];
665 *ret = list->cache_dyn[list->pos];
671 /* Free's ID cache list. User must free the list object returned by
672 any of the searching functions. */
674 void silc_idcache_list_free(SilcIDCacheList list)
678 silc_free(list->cache_dyn);