-fix channel data range to make -1 legal value
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed_cache.c
index 69f820a30cf6d99a5a75ff5555da08c8e267577e..32f63f3e30b2ada0dd96d169bfdd7c91e809ea8b 100644 (file)
@@ -92,8 +92,8 @@ static unsigned int cache_size;
 /**
  * Looks up in the cache and returns the entry
  *
- * @param key the peer identity of the peer whose corresponding entry has to be
- *          looked up
+ * @param peer_id the peer identity of the peer whose corresponding entry has to
+ *          be looked up
  * @return the HELLO message; NULL if not found
  */
 static struct CacheEntry *
@@ -111,6 +111,20 @@ cache_lookup (unsigned int peer_id)
 }
 
 
+/**
+ * Free the resources occupied by a cache entry
+ *
+ * @param entry the cache entry to free
+ */
+static void
+free_entry (struct CacheEntry *entry)
+{
+  GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, entry);
+  GNUNET_free_non_null (entry->hello);
+  GNUNET_free (entry);
+}
+
+
 /**
  * Creates a new cache entry and then puts it into the cache's hashtable.
  *
@@ -121,7 +135,7 @@ static struct CacheEntry *
 add_entry (unsigned int peer_id)
 {
   struct CacheEntry *entry;
-  
+
   GNUNET_assert (NULL != cache);
   if (cache_size == GNUNET_CONTAINER_multihashmap32_size (cache))
   {
@@ -131,8 +145,9 @@ add_entry (unsigned int peer_id)
                    GNUNET_CONTAINER_multihashmap32_remove (cache, (uint32_t)
                                                            entry->peer_id,
                                                            entry));
+    free_entry (entry);
   }
-  entry = GNUNET_malloc (sizeof (struct CacheEntry));
+  entry = GNUNET_new (struct CacheEntry);
   entry->peer_id = peer_id;
   GNUNET_assert (GNUNET_OK ==
                  GNUNET_CONTAINER_multihashmap32_put (cache,
@@ -162,8 +177,7 @@ cache_clear_iterator (void *cls, uint32_t key, void *value)
   GNUNET_assert (NULL != entry);
   GNUNET_assert (GNUNET_YES ==
                  GNUNET_CONTAINER_multihashmap32_remove (cache, key, value));
-  GNUNET_free_non_null (entry->hello);
-  GNUNET_free (entry);
+  free_entry (entry);
   return GNUNET_YES;
 }