2 This file is part of GNUnet
3 (C) 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 * @brief peer-ID table that assigns integer IDs to peer-IDs to save memory
24 * @author Christian Grothoff
28 #include "gnunet_peer_lib.h"
34 * the identifier itself
36 struct GNUNET_PeerIdentity id;
39 * Short version of the identifier;
40 * if the RC==0, then index of next
41 * free slot in table, otherwise
42 * equal to this slot in the table.
47 * Reference counter, 0 if this slot
55 * Table with our interned peer IDs.
57 static struct PeerEntry *table;
60 * Hashmap of PeerIdentities to "struct PeerEntry"
61 * (for fast lookup). NULL until the library
62 * is actually being used.
64 static struct GNUNET_CONTAINER_MultiHashMap *map;
67 * Size of the "table".
69 static unsigned int size;
72 * Index of the beginning of the free list in the table; set to "size"
73 * if no slots are free in the table.
75 static unsigned int free_list_start;
79 * Intern an peer identity. If the identity is already known, its
80 * reference counter will be increased by one.
82 * @param pid identity to intern
83 * @return the interned identity.
86 GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
95 map = GNUNET_CONTAINER_multihashmap_create (32);
96 e = GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey);
99 GNUNET_assert (e->rc > 0);
103 ret = free_list_start;
106 GNUNET_array_grow (table, size, size + 16);
107 for (i = ret; i < size; i++)
108 table[i].pid = i + 1;
116 GNUNET_assert (ret < size);
117 GNUNET_assert (table[ret].rc == 0);
118 free_list_start = table[ret].pid;
119 table[ret].id = *pid;
121 table[ret].pid = ret;
122 GNUNET_CONTAINER_multihashmap_put (map,
125 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
131 * Decrement multiple RCs of peer identities by one.
133 * @param ids array of PIDs to decrement the RCs of
134 * @param count size of the ids array
137 GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id * ids, unsigned int count)
144 for (i = count - 1; i >= 0; i--)
147 GNUNET_assert (id < size);
148 GNUNET_assert (table[id].rc > 0);
150 if (table[id].rc == 0)
152 GNUNET_CONTAINER_multihashmap_remove (map,
153 &table[id].id.hashPubKey,
155 table[id].pid = free_list_start;
156 free_list_start = id;
163 * Change the reference counter of an interned PID.
165 * @param id identity to change the RC of
166 * @param delta how much to change the RC
169 GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
173 GNUNET_assert (id < size);
174 GNUNET_assert (table[id].rc > 0);
175 GNUNET_assert ((delta >= 0) || (table[id].rc >= -delta));
176 table[id].rc += delta;
177 if (table[id].rc == 0)
179 GNUNET_CONTAINER_multihashmap_remove (map,
180 &table[id].id.hashPubKey,
182 table[id].pid = free_list_start;
183 free_list_start = id;
189 * Convert an interned PID to a normal peer identity.
191 * @param id interned PID to convert
192 * @param pid where to write the normal peer identity
195 GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
199 memset (pid, 0, sizeof (struct GNUNET_PeerIdentity));
203 GNUNET_assert (id < size);
204 GNUNET_assert (table[id].rc > 0);