2 This file is part of GNUnet
3 Copyright (C) 2006, 2008, 2009 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @brief peer-ID table that assigns integer IDs to peer-IDs to save memory
22 * @author Christian Grothoff
25 #include "gnunet_peer_lib.h"
27 #define LOG(kind,...) GNUNET_log_from (kind, "util-peer", __VA_ARGS__)
33 * The identifier itself
35 struct GNUNET_PeerIdentity id;
38 * Short version of the identifier; if the RC==0, then index of next
39 * free slot in table, otherwise equal to this slot in the table.
44 * Reference counter, 0 if this slot is not used.
51 * Table with our interned peer IDs.
53 static struct PeerEntry **table;
56 * Peermap of PeerIdentities to "struct PeerEntry"
57 * (for fast lookup). NULL until the library
58 * is actually being used.
60 static struct GNUNET_CONTAINER_MultiPeerMap *map;
63 * Size of the "table".
65 static unsigned int size;
68 * Index of the beginning of the free list in the table; set to "size"
69 * if no slots are free in the table.
71 static unsigned int free_list_start;
75 * Search for a peer identity. The reference counter is not changed.
77 * @param pid identity to find
78 * @return the interned identity or 0.
81 GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
89 e = GNUNET_CONTAINER_multipeermap_get (map, pid);
92 GNUNET_assert (e->rc > 0);
98 * Intern an peer identity. If the identity is already known, its
99 * reference counter will be increased by one.
101 * @param pid identity to intern
102 * @return the interned identity.
105 GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
114 map = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
115 e = GNUNET_CONTAINER_multipeermap_get (map, pid);
118 GNUNET_assert (e->rc > 0);
122 ret = free_list_start;
125 GNUNET_array_grow (table, size, size + 16);
126 for (i = ret; i < size; i++)
128 table[i] = GNUNET_new (struct PeerEntry);
129 table[i]->pid = i + 1;
134 memset (&table[0]->id, 0, sizeof (struct GNUNET_PeerIdentity));
139 GNUNET_assert (ret < size);
140 GNUNET_assert (0 == table[ret]->rc);
141 free_list_start = table[ret]->pid;
142 table[ret]->id = *pid;
144 table[ret]->pid = ret;
145 GNUNET_break (GNUNET_OK ==
146 GNUNET_CONTAINER_multipeermap_put (map,
149 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
155 * Decrement multiple RCs of peer identities by one.
157 * @param ids array of PIDs to decrement the RCs of
158 * @param count size of the ids array
161 GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
168 for (i = count - 1; i >= 0; i--)
173 GNUNET_assert (id < size);
174 GNUNET_assert (table[id]->rc > 0);
176 if (0 == table[id]->rc)
178 GNUNET_break (GNUNET_OK ==
179 GNUNET_CONTAINER_multipeermap_remove (map,
182 table[id]->pid = free_list_start;
183 free_list_start = id;
190 * Change the reference counter of an interned PID.
192 * @param id identity to change the RC of
193 * @param delta how much to change the RC
196 GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
200 GNUNET_assert (id < size);
201 GNUNET_assert (table[id]->rc > 0);
202 GNUNET_assert ( (delta >= 0) ||
203 (table[id]->rc >= (unsigned int) (-delta)) );
204 table[id]->rc += delta;
205 if (0 == table[id]->rc)
207 GNUNET_break (GNUNET_OK ==
208 GNUNET_CONTAINER_multipeermap_remove (map,
211 table[id]->pid = free_list_start;
212 free_list_start = id;
218 * Convert an interned PID to a normal peer identity.
220 * @param id interned PID to convert
221 * @param pid where to write the normal peer identity
224 GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
228 memset (pid, 0, sizeof (struct GNUNET_PeerIdentity));
231 GNUNET_assert (id < size);
232 GNUNET_assert (table[id]->rc > 0);
233 *pid = table[id]->id;
238 * Convert an interned PID to a normal peer identity.
240 * @param id interned PID to convert
241 * @return pointer to peer identity, valid as long 'id' is valid
243 const struct GNUNET_PeerIdentity *
244 GNUNET_PEER_resolve2 (GNUNET_PEER_Id id)
246 GNUNET_assert (id < size);
247 GNUNET_assert (table[id]->rc > 0);
248 return &table[id]->id;