run indent twice, it alternates between two 'canonical' forms, also run whitespace...
[oweals/gnunet.git] / src / util / peer.c
index f30c720aa073ad8e97165d8af1a079165bec3103..1491211c5e7f2c2a2fca3c550c44b662d4d11673 100644 (file)
@@ -72,6 +72,35 @@ static unsigned int size;
 static unsigned int free_list_start;
 
 
+/**
+ * Search for a peer identity. The reference counter is not changed.
+ *
+ * @param pid identity to find
+ * @return the interned identity or 0.
+ */
+GNUNET_PEER_Id
+GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
+{
+  struct PeerEntry *e;
+  long off;
+
+  if (pid == NULL)
+    return 0;
+  if (NULL == map)
+    return 0;
+  off = (long) GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey);
+  e = (off == 0) ? NULL : &table[off];
+  if (e != NULL)
+  {
+    GNUNET_assert (e->rc > 0);
+    return e->pid;
+  }
+  else
+  {
+    return 0;
+  }
+}
+
 /**
  * Intern an peer identity.  If the identity is already known, its
  * reference counter will be increased by one.
@@ -85,31 +114,33 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
   GNUNET_PEER_Id ret;
   struct PeerEntry *e;
   unsigned int i;
+  long off;
 
   if (pid == NULL)
     return 0;
   if (NULL == map)
     map = GNUNET_CONTAINER_multihashmap_create (32);
-  e = GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey);
+  off = (long) GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey);
+  e = (off == 0) ? NULL : &table[off];
   if (e != NULL)
-    {
-      GNUNET_assert (e->rc > 0);
-      e->rc++;
-      return e->pid;
-    }
+  {
+    GNUNET_assert (e->rc > 0);
+    e->rc++;
+    return e->pid;
+  }
   ret = free_list_start;
   if (ret == size)
-    {
-      GNUNET_array_grow (table, size, size + 16);
-      for (i = ret; i < size; i++)
-        table[i].pid = i + 1;
-    }
+  {
+    GNUNET_array_grow (table, size, size + 16);
+    for (i = ret; i < size; i++)
+      table[i].pid = i + 1;
+  }
   if (ret == 0)
-    {
-      table[0].pid = 0;
-      table[0].rc = 1;
-      ret = 1;
-    }
+  {
+    table[0].pid = 0;
+    table[0].rc = 1;
+    ret = 1;
+  }
   GNUNET_assert (ret < size);
   GNUNET_assert (table[ret].rc == 0);
   free_list_start = table[ret].pid;
@@ -117,22 +148,21 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
   table[ret].rc = 1;
   table[ret].pid = ret;
   GNUNET_break (GNUNET_OK ==
-               GNUNET_CONTAINER_multihashmap_put (map,
-                                                  &pid->hashPubKey,
-                                                  &table[ret],
-                                                  GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+                GNUNET_CONTAINER_multihashmap_put (map, &pid->hashPubKey,
+                                                   (void *) (long) ret,
+                                                   GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
   return ret;
 }
 
 
 /**
  * Decrement multiple RCs of peer identities by one.
- * 
+ *
  * @param ids array of PIDs to decrement the RCs of
  * @param count size of the ids array
  */
 void
-GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id * ids, unsigned int count)
+GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
 {
   int i;
   GNUNET_PEER_Id id;
@@ -140,23 +170,24 @@ GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id * ids, unsigned int count)
   if (count == 0)
     return;
   for (i = count - 1; i >= 0; i--)
+  {
+    id = ids[i];
+    if (id == 0)
+      continue;
+    GNUNET_assert (id < size);
+    GNUNET_assert (table[id].rc > 0);
+    table[id].rc--;
+    if (table[id].rc == 0)
     {
-      id = ids[i];
-      if (id == 0)
-       continue;
-      GNUNET_assert (id < size);
-      GNUNET_assert (table[id].rc > 0);
-      table[id].rc--;
-      if (table[id].rc == 0)
-        {
-          GNUNET_break (GNUNET_OK ==
-                       GNUNET_CONTAINER_multihashmap_remove (map,
-                                                             &table[id].id.hashPubKey,
-                                                             &table[id]));
-          table[id].pid = free_list_start;
-          free_list_start = id;
-        }
+      GNUNET_break (GNUNET_OK ==
+                    GNUNET_CONTAINER_multihashmap_remove (map,
+                                                          &table[id].
+                                                          id.hashPubKey,
+                                                          (void *) (long) id));
+      table[id].pid = free_list_start;
+      free_list_start = id;
     }
+  }
 }
 
 
@@ -176,14 +207,15 @@ GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
   GNUNET_assert ((delta >= 0) || (table[id].rc >= -delta));
   table[id].rc += delta;
   if (table[id].rc == 0)
-    {
-      GNUNET_break (GNUNET_OK ==
-                   GNUNET_CONTAINER_multihashmap_remove (map,
-                                                         &table[id].id.hashPubKey,
-                                                         &table[id]));
-      table[id].pid = free_list_start;
-      free_list_start = id;
-    }
+  {
+    GNUNET_break (GNUNET_OK ==
+                  GNUNET_CONTAINER_multihashmap_remove (map,
+                                                        &table[id].
+                                                        id.hashPubKey,
+                                                        (void *) (long) id));
+    table[id].pid = free_list_start;
+    free_list_start = id;
+  }
 }
 
 
@@ -197,11 +229,11 @@ void
 GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
 {
   if (id == 0)
-    {
-      memset (pid, 0, sizeof (struct GNUNET_PeerIdentity));
-      GNUNET_break (0);
-      return;
-    }
+  {
+    memset (pid, 0, sizeof (struct GNUNET_PeerIdentity));
+    GNUNET_break (0);
+    return;
+  }
   GNUNET_assert (id < size);
   GNUNET_assert (table[id].rc > 0);
   *pid = table[id].id;