social cli
[oweals/gnunet.git] / src / rps / gnunet-service-rps_view.c
index 2cf0287bb8f09c0e870f11362ff0cb86e9f07f39..b7a0ba28732b3139a6fd4148f4620eab483b603a 100644 (file)
@@ -62,6 +62,8 @@ View_create (uint32_t len)
 /**
  * Change length of view
  *
+ * If size is decreased, peers with higher indices are removed.
+ *
  * @param len the (maximum) length for the view
  */
 void
@@ -175,25 +177,27 @@ int
 View_remove_peer (const struct GNUNET_PeerIdentity *peer)
 {
   uint32_t *index;
+  uint32_t *swap_index;
+  uint32_t last_index;
 
-  if (GNUNET_YES == View_contains_peer (peer))
-  {
-    index = GNUNET_CONTAINER_multipeermap_get (mpm, peer);
-    GNUNET_assert (NULL != index);
-    if (*index == GNUNET_CONTAINER_multipeermap_size (mpm) - 1)
-    { /* Last peer in array - simply remove */
-    }
-    else
-    { /* Fill the 'gap' in the array with the last peer */
-      array[*index] = array[View_size ()];
-    }
-    GNUNET_CONTAINER_multipeermap_remove_all (mpm, peer);
-    return GNUNET_OK;
-  }
-  else
+  if (GNUNET_NO == View_contains_peer (peer))
   {
     return GNUNET_NO;
   }
+  index = GNUNET_CONTAINER_multipeermap_get (mpm, peer);
+  GNUNET_assert (NULL != index);
+  last_index = View_size () - 1;
+  if (*index < last_index)
+  { /* Fill the 'gap' in the array with the last peer */
+    array[*index] = array[last_index];
+    GNUNET_assert (GNUNET_YES == View_contains_peer (&array[last_index]));
+    swap_index = GNUNET_CONTAINER_multipeermap_get (mpm, &array[last_index]);
+    GNUNET_assert (NULL != swap_index);
+    *swap_index = *index;
+    GNUNET_free (index);
+  }
+  GNUNET_CONTAINER_multipeermap_remove_all (mpm, peer);
+  return GNUNET_OK;
 }
 
 /**
@@ -230,14 +234,16 @@ View_clear ()
   uint32_t i;
   uint32_t *index;
 
-  for (i = 0; i < GNUNET_CONTAINER_multipeermap_size (mpm); i++)
+  for (i = 0; 0 < View_size (); i++)
   { /* Need to free indices stored at peers */
+    GNUNET_assert (GNUNET_YES ==
+        GNUNET_CONTAINER_multipeermap_contains (mpm, &array[i]));
     index = GNUNET_CONTAINER_multipeermap_get (mpm, &array[i]);
     GNUNET_assert (NULL != index);
     GNUNET_free (index);
     GNUNET_CONTAINER_multipeermap_remove_all (mpm, &array[i]);
   }
-  GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (mpm));
+  GNUNET_assert (0 == View_size ());
 }
 
 /**