Merged from silc_1_0_branch.
[silc.git] / lib / silccore / silcidcache.c
index 245e4dfb1c40e49334d9480fe17d5dc90862cc22..3d6904e74421b00d962d69e55466f92c9240fa0a 100644 (file)
@@ -85,11 +85,12 @@ struct SilcIDCacheStruct {
 
 */
 struct SilcIDCacheListStruct {
-  SilcIDCacheEntry cache[64];
+  SilcIDCacheEntry cache[128];
   SilcIDCacheEntry *cache_dyn;
-  uint32 cache_dyn_count;
-  uint32 cache_count;
-  uint32 pos;
+  SilcUInt32 cache_dyn_count;
+  SilcUInt32 cache_count;
+  SilcUInt32 pos;
+  bool dyn;
 };
 
 /* Allocates new ID cache object. The initial amount of allocated entries
@@ -97,7 +98,7 @@ struct SilcIDCacheListStruct {
    The `id_type' defines the types of the ID's that will be saved to the
    cache. */
 
-SilcIDCache silc_idcache_alloc(uint32 count, SilcIdType id_type,
+SilcIDCache silc_idcache_alloc(SilcUInt32 count, SilcIdType id_type,
                               SilcIDCacheDestructor destructor)
 {
   SilcIDCache cache;
@@ -105,20 +106,32 @@ SilcIDCache silc_idcache_alloc(uint32 count, SilcIdType id_type,
   SILC_LOG_DEBUG(("Allocating new cache"));
 
   cache = silc_calloc(1, sizeof(*cache));
-  cache->id_table = silc_hash_table_alloc(count, silc_hash_id, 
-                                         (void *)(uint32)id_type,
-                                         silc_hash_id_compare, 
-                                         (void *)(uint32)id_type, 
-                                         silc_idcache_destructor, NULL, 
-                                         FALSE);
+  if (!cache)
+    return NULL;
+  cache->id_table = silc_hash_table_alloc(count, silc_hash_id,
+                                         (void *)(SilcUInt32)id_type,
+                                         silc_hash_id_compare,
+                                         (void *)(SilcUInt32)id_type,
+                                         silc_idcache_destructor, NULL, TRUE);
   cache->name_table = silc_hash_table_alloc(count, silc_hash_string, NULL,
-                                           silc_hash_string_compare, NULL, 
-                                           NULL, NULL, FALSE);
+                                           silc_hash_string_compare, NULL,
+                                           NULL, NULL, TRUE);
   cache->context_table = silc_hash_table_alloc(count, silc_hash_ptr, NULL,
-                                              NULL, NULL, NULL, NULL, FALSE);
+                                              NULL, NULL, NULL, NULL, TRUE);
   cache->destructor = destructor;
   cache->type = id_type;
 
+  if (!cache->id_table || !cache->name_table || !cache->context_table) {
+    if (cache->id_table)
+      silc_hash_table_free(cache->id_table);
+    if (cache->name_table)
+      silc_hash_table_free(cache->name_table);
+    if (cache->context_table)
+      silc_hash_table_free(cache->context_table);
+    silc_free(cache);
+    return NULL;
+  }
+
   return cache;
 }
 
@@ -141,18 +154,19 @@ void silc_idcache_free(SilcIDCache cache)
    the entry never expires from the cache. */
 
 bool silc_idcache_add(SilcIDCache cache, char *name, void *id, 
-                     void *context, int expire)
+                     void *context, int expire, SilcIDCacheEntry *ret)
 {
   SilcIDCacheEntry c;
-  uint32 curtime = time(NULL);
 
   SILC_LOG_DEBUG(("Adding cache entry"));
 
   /* Allocate new cache entry */
   c = silc_calloc(1, sizeof(*c));
+  if (!c)
+    return FALSE;
   c->id = id;
   c->name = name;
-  c->expire = (expire ? (curtime + SILC_ID_CACHE_EXPIRE) : 0);
+  c->expire = expire;
   c->context = context;
 
   /* Add the new entry to the hash tables */
@@ -164,13 +178,8 @@ bool silc_idcache_add(SilcIDCache cache, char *name, void *id,
   if (context)
     silc_hash_table_add(cache->context_table, context, c);
 
-  /* See whether we have time to rehash the tables */
-  if ((silc_hash_table_count(cache->id_table) / 2) >
-      silc_hash_table_size(cache->id_table)) {
-    silc_hash_table_rehash(cache->id_table, 0);
-    silc_hash_table_rehash(cache->name_table, 0);
-    silc_hash_table_rehash(cache->context_table, 0);
-  }
+  if (ret)
+    *ret = c;
 
   return TRUE;
 }
@@ -180,7 +189,11 @@ bool silc_idcache_add(SilcIDCache cache, char *name, void *id,
 static void silc_idcache_destructor(void *key, void *context,
                                    void *user_context)
 {
-  silc_free(context);
+  SilcIDCacheEntry c = context;
+  if (c) {
+    memset(c, 'F', sizeof(*c));
+    silc_free(c);
+  }
 }
 
 /* Delete cache entry from cache. */
@@ -197,6 +210,8 @@ bool silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry old)
     ret = silc_hash_table_del(cache->context_table, old->context);
   if (old->id)
     ret = silc_hash_table_del(cache->id_table, old->id);
+  else
+    silc_idcache_destructor(NULL, old, NULL);
 
   return ret;
 }
@@ -262,6 +277,8 @@ bool silc_idcache_del_by_context(SilcIDCache cache, void *context)
     ret = silc_hash_table_del(cache->context_table, c->context);
   if (c->id)
     ret = silc_hash_table_del_by_context(cache->id_table, c->id, c);
+  else
+    silc_idcache_destructor(NULL, c, NULL);
 
   return ret;
 }
@@ -289,26 +306,33 @@ static void silc_idcache_purge_foreach(void *key, void *context,
                                       void *user_context)
 {
   SilcIDCache cache = (SilcIDCache)user_context;
-  uint32 curtime = time(NULL);
+  SilcUInt32 curtime = time(NULL);
   SilcIDCacheEntry c = (SilcIDCacheEntry)context;
+  bool ret = FALSE;
+
+  if (!context)
+    return;
 
   if (c->expire && c->expire < curtime) {
     /* Remove the entry from the hash tables */
     if (c->name)
-      silc_hash_table_del_by_context(cache->name_table, c->name, c);
+      ret = silc_hash_table_del_by_context(cache->name_table, c->name, c);
     if (c->context)
-      silc_hash_table_del(cache->context_table, c->context);
+      ret = silc_hash_table_del(cache->context_table, c->context);
     if (c->id)
-      silc_hash_table_del_by_context_ext(cache->id_table, c->id, c,
-                                        NULL, NULL, NULL, NULL, 
-                                        silc_idcache_destructor_dummy, NULL);
-
-    /* Call the destructor */
-    if (cache->destructor)
-      cache->destructor(cache, c);
-
-    /* Free the entry, it has been deleted from the hash tables */
-    silc_free(c);
+      ret =
+       silc_hash_table_del_by_context_ext(cache->id_table, c->id, c,
+                                          NULL, NULL, NULL, NULL, 
+                                          silc_idcache_destructor_dummy,
+                                          NULL);
+    if (ret == TRUE) {
+      /* Call the destructor */
+      if (cache->destructor)
+       cache->destructor(cache, c);
+
+      /* Free the entry, it has been deleted from the hash tables */
+      silc_idcache_destructor(NULL, c, NULL);
+    }
   }
 }
 
@@ -342,13 +366,14 @@ bool silc_idcache_purge_by_context(SilcIDCache cache, void *context)
       silc_hash_table_del_by_context_ext(cache->id_table, c->id, c,
                                         NULL, NULL, NULL, NULL, 
                                         silc_idcache_destructor_dummy, NULL);
-  
-  /* Call the destructor */
-  if (cache->destructor)
-    cache->destructor(cache, c);
+  if (ret == TRUE) {
+    /* Call the destructor */
+    if (cache->destructor)
+      cache->destructor(cache, c);
 
-  /* Free the entry, it has been deleted from the hash tables */
-  silc_free(c);
+    /* Free the entry, it has been deleted from the hash tables */
+    silc_idcache_destructor(NULL, c, NULL);
+  }
 
   return ret;
 }
@@ -360,6 +385,8 @@ static void silc_idcache_get_all_foreach(void *key, void *context,
                                         void *user_context)
 {
   SilcIDCacheList list = (SilcIDCacheList)user_context;
+  if (!context)
+    return;
   silc_idcache_list_add(list, (SilcIDCacheEntry)context);
 }
 
@@ -373,6 +400,8 @@ bool silc_idcache_get_all(SilcIDCache cache, SilcIDCacheList *ret)
     return TRUE;
 
   list = silc_idcache_list_alloc();
+  if (!list)
+    return FALSE;
   silc_hash_table_foreach(cache->id_table, silc_idcache_get_all_foreach, list);
 
   if (silc_idcache_list_count(list) == 0) {
@@ -393,6 +422,8 @@ bool silc_idcache_find_by_id(SilcIDCache cache, void *id,
   SilcIDCacheList list;
 
   list = silc_idcache_list_alloc();
+  if (!list)
+    return FALSE;
 
   if (!ret)
     return TRUE;
@@ -451,6 +482,8 @@ bool silc_idcache_find_by_name(SilcIDCache cache, char *name,
   SilcIDCacheList list;
 
   list = silc_idcache_list_alloc();
+  if (!list)
+    return FALSE;
 
   if (!ret)
     return TRUE;
@@ -486,6 +519,8 @@ static SilcIDCacheList silc_idcache_list_alloc()
   SilcIDCacheList list;
 
   list = silc_calloc(1, sizeof(*list));
+  if (!list)
+    return FALSE;
 
   return list;
 }
@@ -499,7 +534,7 @@ static void silc_idcache_list_add(SilcIDCacheList list, SilcIDCacheEntry cache)
 
   /* Try to add to static cache */
   if (!list->cache_dyn_count)
-    for (i = 0; i < sizeof(list->cache); i++) {
+    for (i = 0; i < (sizeof(list->cache) / sizeof(list->cache[0])); i++) {
       if (!list->cache[i]) {
        list->cache[i] = cache;
        list->cache_count++;
@@ -519,17 +554,19 @@ static void silc_idcache_list_add(SilcIDCacheList list, SilcIDCacheEntry cache)
   if (i >= list->cache_dyn_count) {
     int k;
 
-    i += 5;
+    i = list->cache_dyn_count;
     list->cache_dyn = silc_realloc(list->cache_dyn, 
-                                  sizeof(*list->cache) * (i));
+                                  sizeof(*list->cache_dyn) * (i + 5));
+    if (!list->cache_dyn)
+      return;
 
     /* NULL the reallocated area */
-    for (k = list->cache_dyn_count; k < i; k++)
+    for (k = i; k < (i + 5); k++)
       list->cache_dyn[k] = NULL;
 
-    list->cache_dyn[list->cache_dyn_count] = cache;
-    list->cache_dyn_count = i;
+    list->cache_dyn[i] = cache;
     list->cache_count++;
+    list->cache_dyn_count += 5;
   }
 }
 
@@ -559,25 +596,25 @@ bool silc_idcache_list_first(SilcIDCacheList list, SilcIDCacheEntry *ret)
 
 bool silc_idcache_list_next(SilcIDCacheList list, SilcIDCacheEntry *ret)
 {
-  int dyn = FALSE;
   list->pos++;
 
-  if (list->pos >= sizeof(list->cache)) {
+  if (!list->dyn &&
+      list->pos >= (sizeof(list->cache) / sizeof(list->cache[0]))) {
     list->pos = 0;
-    dyn = TRUE;
+    list->dyn = TRUE;
   }
 
-  if (dyn && list->pos >= list->cache_dyn_count)
+  if (list->dyn && list->pos >= list->cache_dyn_count)
     return FALSE;
 
-  if (!dyn && !list->cache[list->pos])
+  if (!list->dyn && !list->cache[list->pos])
     return FALSE;
   
-  if (dyn && !list->cache_dyn[list->pos])
+  if (list->dyn && !list->cache_dyn[list->pos])
     return FALSE;
   
   if (ret) {
-    if (!dyn)
+    if (!list->dyn)
       *ret = list->cache[list->pos];
     else
       *ret = list->cache_dyn[list->pos];