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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, 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
27 #include "gnunet_peer_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-peer", __VA_ARGS__)
35 * The identifier itself
37 struct GNUNET_PeerIdentity id;
40 * Short version of the identifier; if the RC==0, then index of next
41 * free slot in table, otherwise equal to this slot in the table.
46 * Reference counter, 0 if this slot is not used.
53 * Table with our interned peer IDs.
55 static struct PeerEntry **table;
58 * Peermap of PeerIdentities to "struct PeerEntry"
59 * (for fast lookup). NULL until the library
60 * is actually being used.
62 static struct GNUNET_CONTAINER_MultiPeerMap *map;
65 * Size of the "table".
67 static unsigned int size;
70 * Index of the beginning of the free list in the table; set to "size"
71 * if no slots are free in the table.
73 static unsigned int free_list_start;
77 * Search for a peer identity. The reference counter is not changed.
79 * @param pid identity to find
80 * @return the interned identity or 0.
83 GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
91 e = GNUNET_CONTAINER_multipeermap_get (map, pid);
94 GNUNET_assert (e->rc > 0);
100 * Intern an peer identity. If the identity is already known, its
101 * reference counter will be increased by one.
103 * @param pid identity to intern
104 * @return the interned identity.
107 GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
116 map = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
117 e = GNUNET_CONTAINER_multipeermap_get (map, pid);
120 GNUNET_assert (e->rc > 0);
124 ret = free_list_start;
127 GNUNET_array_grow (table, size, size + 16);
128 for (i = ret; i < size; i++)
130 table[i] = GNUNET_new (struct PeerEntry);
131 table[i]->pid = i + 1;
136 memset (&table[0]->id, 0, sizeof (struct GNUNET_PeerIdentity));
141 GNUNET_assert (ret < size);
142 GNUNET_assert (0 == table[ret]->rc);
143 free_list_start = table[ret]->pid;
144 table[ret]->id = *pid;
146 table[ret]->pid = ret;
147 GNUNET_break (GNUNET_OK ==
148 GNUNET_CONTAINER_multipeermap_put (map,
151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
157 * Decrement multiple RCs of peer identities by one.
159 * @param ids array of PIDs to decrement the RCs of
160 * @param count size of the ids array
163 GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
170 for (i = count - 1; i >= 0; i--)
175 GNUNET_assert (id < size);
176 GNUNET_assert (table[id]->rc > 0);
178 if (0 == table[id]->rc)
180 GNUNET_break (GNUNET_OK ==
181 GNUNET_CONTAINER_multipeermap_remove (map,
184 table[id]->pid = free_list_start;
185 free_list_start = id;
192 * Change the reference counter of an interned PID.
194 * @param id identity to change the RC of
195 * @param delta how much to change the RC
198 GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
202 GNUNET_assert (id < size);
203 GNUNET_assert (table[id]->rc > 0);
204 GNUNET_assert ((delta >= 0) || (table[id]->rc >= -delta));
205 table[id]->rc += delta;
206 if (0 == table[id]->rc)
208 GNUNET_break (GNUNET_OK ==
209 GNUNET_CONTAINER_multipeermap_remove (map,
212 table[id]->pid = free_list_start;
213 free_list_start = id;
219 * Convert an interned PID to a normal peer identity.
221 * @param id interned PID to convert
222 * @param pid where to write the normal peer identity
225 GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
229 memset (pid, 0, sizeof (struct GNUNET_PeerIdentity));
232 GNUNET_assert (id < size);
233 GNUNET_assert (table[id]->rc > 0);
234 *pid = table[id]->id;
239 * Convert an interned PID to a normal peer identity.
241 * @param id interned PID to convert
242 * @return pointer to peer identity, valid as long 'id' is valid
244 const struct GNUNET_PeerIdentity *
245 GNUNET_PEER_resolve2 (GNUNET_PEER_Id id)
247 GNUNET_assert (id < size);
248 GNUNET_assert (table[id]->rc > 0);
249 return &table[id]->id;