log
[oweals/gnunet.git] / src / util / peer.c
index a0e9656057d96f37b070b206f5380f7f75472a5d..6d7a3a654d034ab26edb5a8bc0641d4a1d5c99b0 100644 (file)
@@ -1,10 +1,10 @@
 /*
       This file is part of GNUnet
 /*
       This file is part of GNUnet
-     (C) 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2006, 2008, 2009 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
 
       GNUnet is free software; you can redistribute it and/or modify
       it under the terms of the GNU General Public License as published
-      by the Free Software Foundation; either version 2, or (at your
+      by the Free Software Foundation; either version 3, or (at your
       option) any later version.
 
       GNUnet is distributed in the hope that it will be useful, but
       option) any later version.
 
       GNUnet is distributed in the hope that it will be useful, but
@@ -23,7 +23,6 @@
  * @brief peer-ID table that assigns integer IDs to peer-IDs to save memory
  * @author Christian Grothoff
  */
  * @brief peer-ID table that assigns integer IDs to peer-IDs to save memory
  * @author Christian Grothoff
  */
-
 #include "platform.h"
 #include "gnunet_peer_lib.h"
 
 #include "platform.h"
 #include "gnunet_peer_lib.h"
 
@@ -53,14 +52,14 @@ struct PeerEntry
 /**
  * Table with our interned peer IDs.
  */
 /**
  * Table with our interned peer IDs.
  */
-static struct PeerEntry *table;
+static struct PeerEntry **table;
 
 /**
 
 /**
- * Hashmap of PeerIdentities to "struct PeerEntry"
+ * Peermap of PeerIdentities to "struct PeerEntry"
  * (for fast lookup).  NULL until the library
  * is actually being used.
  */
  * (for fast lookup).  NULL until the library
  * is actually being used.
  */
-static struct GNUNET_CONTAINER_MultiHashMap *map;
+static struct GNUNET_CONTAINER_MultiPeerMap *map;
 
 /**
  * Size of the "table".
 
 /**
  * Size of the "table".
@@ -84,15 +83,13 @@ GNUNET_PEER_Id
 GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
 {
   struct PeerEntry *e;
 GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
 {
   struct PeerEntry *e;
-  long off;
 
 
-  if (pid == NULL)
+  if (NULL == pid)
     return 0;
   if (NULL == map)
     return 0;
     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)
+  e = GNUNET_CONTAINER_multipeermap_get (map, pid);
+  if (NULL == e)
     return 0;
   GNUNET_assert (e->rc > 0);
   return e->pid;
     return 0;
   GNUNET_assert (e->rc > 0);
   return e->pid;
@@ -112,15 +109,13 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
   GNUNET_PEER_Id ret;
   struct PeerEntry *e;
   unsigned int i;
   GNUNET_PEER_Id ret;
   struct PeerEntry *e;
   unsigned int i;
-  long off;
 
 
-  if (pid == NULL)
+  if (NULL == pid)
     return 0;
   if (NULL == map)
     return 0;
   if (NULL == map)
-    map = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
-  off = (long) GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey);
-  e = (off == 0) ? NULL : &table[off];
-  if (e != NULL)
+    map = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
+  e = GNUNET_CONTAINER_multipeermap_get (map, pid);
+  if (NULL != e)
   {
     GNUNET_assert (e->rc > 0);
     e->rc++;
   {
     GNUNET_assert (e->rc > 0);
     e->rc++;
@@ -131,23 +126,27 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
   {
     GNUNET_array_grow (table, size, size + 16);
     for (i = ret; i < size; i++)
   {
     GNUNET_array_grow (table, size, size + 16);
     for (i = ret; i < size; i++)
-      table[i].pid = i + 1;
+    {
+      table[i] = GNUNET_new (struct PeerEntry);
+      table[i]->pid = i + 1;
+    }
   }
   }
-  if (ret == 0)
+  if (0 == ret)
   {
   {
-    table[0].pid = 0;
-    table[0].rc = 1;
+    table[0]->pid = 0;
+    table[0]->rc = 1;
     ret = 1;
   }
   GNUNET_assert (ret < size);
     ret = 1;
   }
   GNUNET_assert (ret < size);
-  GNUNET_assert (table[ret].rc == 0);
-  free_list_start = table[ret].pid;
-  table[ret].id = *pid;
-  table[ret].rc = 1;
-  table[ret].pid = ret;
+  GNUNET_assert (0 == table[ret]->rc);
+  free_list_start = table[ret]->pid;
+  table[ret]->id = *pid;
+  table[ret]->rc = 1;
+  table[ret]->pid = ret;
   GNUNET_break (GNUNET_OK ==
   GNUNET_break (GNUNET_OK ==
-                GNUNET_CONTAINER_multihashmap_put (map, &table[ret].id.hashPubKey,
-                                                   (void *) (long) ret,
+                GNUNET_CONTAINER_multipeermap_put (map,
+                                                  &table[ret]->id,
+                                                   table[ret],
                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
   return ret;
 }
                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
   return ret;
 }
@@ -165,24 +164,23 @@ GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
   int i;
   GNUNET_PEER_Id id;
 
   int i;
   GNUNET_PEER_Id id;
 
-  if (count == 0)
+  if (0 == count)
     return;
   for (i = count - 1; i >= 0; i--)
   {
     id = ids[i];
     return;
   for (i = count - 1; i >= 0; i--)
   {
     id = ids[i];
-    if (id == 0)
+    if (0 == id)
       continue;
     GNUNET_assert (id < size);
       continue;
     GNUNET_assert (id < size);
-    GNUNET_assert (table[id].rc > 0);
-    table[id].rc--;
-    if (table[id].rc == 0)
+    GNUNET_assert (table[id]->rc > 0);
+    table[id]->rc--;
+    if (0 == table[id]->rc)
     {
       GNUNET_break (GNUNET_OK ==
     {
       GNUNET_break (GNUNET_OK ==
-                    GNUNET_CONTAINER_multihashmap_remove (map,
-                                                          &table[id].
-                                                          id.hashPubKey,
-                                                          (void *) (long) id));
-      table[id].pid = free_list_start;
+                    GNUNET_CONTAINER_multipeermap_remove (map,
+                                                          &table[id]->id,
+                                                          table[id]));
+      table[id]->pid = free_list_start;
       free_list_start = id;
     }
   }
       free_list_start = id;
     }
   }
@@ -198,20 +196,19 @@ GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
 void
 GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
 {
 void
 GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
 {
-  if (id == 0)
+  if (0 == id)
     return;
   GNUNET_assert (id < size);
     return;
   GNUNET_assert (id < size);
-  GNUNET_assert (table[id].rc > 0);
-  GNUNET_assert ((delta >= 0) || (table[id].rc >= -delta));
-  table[id].rc += delta;
-  if (table[id].rc == 0)
+  GNUNET_assert (table[id]->rc > 0);
+  GNUNET_assert ((delta >= 0) || (table[id]->rc >= -delta));
+  table[id]->rc += delta;
+  if (0 == table[id]->rc)
   {
     GNUNET_break (GNUNET_OK ==
   {
     GNUNET_break (GNUNET_OK ==
-                  GNUNET_CONTAINER_multihashmap_remove (map,
-                                                        &table[id].
-                                                        id.hashPubKey,
-                                                        (void *) (long) id));
-    table[id].pid = free_list_start;
+                  GNUNET_CONTAINER_multipeermap_remove (map,
+                                                        &table[id]->id,
+                                                        table[id]));
+    table[id]->pid = free_list_start;
     free_list_start = id;
   }
 }
     free_list_start = id;
   }
 }
@@ -226,16 +223,29 @@ GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
 void
 GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
 {
 void
 GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
 {
-  if (id == 0)
+  if (0 == id)
   {
     memset (pid, 0, sizeof (struct GNUNET_PeerIdentity));
   {
     memset (pid, 0, sizeof (struct GNUNET_PeerIdentity));
-    GNUNET_break (0);
     return;
   }
   GNUNET_assert (id < size);
     return;
   }
   GNUNET_assert (id < size);
-  GNUNET_assert (table[id].rc > 0);
-  *pid = table[id].id;
+  GNUNET_assert (table[id]->rc > 0);
+  *pid = table[id]->id;
 }
 
 
 }
 
 
+/**
+ * Convert an interned PID to a normal peer identity.
+ *
+ * @param id interned PID to convert
+ * @return pointer to peer identity, valid as long 'id' is valid
+ */
+const struct GNUNET_PeerIdentity *
+GNUNET_PEER_resolve2 (GNUNET_PEER_Id id)
+{
+  return &table[id]->id;
+}
+
+
+
 /* end of peer.c */
 /* end of peer.c */