guix-env: some update.
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed_cache.c
index 69f820a30cf6d99a5a75ff5555da08c8e267577e..2e5c41d5b49e3749b048fdb9ea1ef2deaebeebb4 100644 (file)
@@ -1,6 +1,6 @@
 /*
   This file is part of GNUnet.
-  (C) 2008--2013 Christian Grothoff (and other contributing authors)
+  Copyright (C) 2008--2013 GNUnet e.V.
 
   GNUnet is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
   You should have received a copy of the GNU General Public License
   along with GNUnet; see the file COPYING.  If not, write to the
-  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-  Boston, MA 02111-1307, USA.
+  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+  Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -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;
 }