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.
18 * @brief peer-ID table that assigns integer IDs to peer-IDs to save memory
19 * @author Christian Grothoff
22 #include "gnunet_peer_lib.h"
24 #define LOG(kind,...) GNUNET_log_from (kind, "util-peer", __VA_ARGS__)
30 * The identifier itself
32 struct GNUNET_PeerIdentity id;
35 * Short version of the identifier; if the RC==0, then index of next
36 * free slot in table, otherwise equal to this slot in the table.
41 * Reference counter, 0 if this slot is not used.
48 * Table with our interned peer IDs.
50 static struct PeerEntry **table;
53 * Peermap of PeerIdentities to "struct PeerEntry"
54 * (for fast lookup). NULL until the library
55 * is actually being used.
57 static struct GNUNET_CONTAINER_MultiPeerMap *map;
60 * Size of the "table".
62 static unsigned int size;
65 * Index of the beginning of the free list in the table; set to "size"
66 * if no slots are free in the table.
68 static unsigned int free_list_start;
72 * Search for a peer identity. The reference counter is not changed.
74 * @param pid identity to find
75 * @return the interned identity or 0.
78 GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
86 e = GNUNET_CONTAINER_multipeermap_get (map, pid);
89 GNUNET_assert (e->rc > 0);
95 * Intern an peer identity. If the identity is already known, its
96 * reference counter will be increased by one.
98 * @param pid identity to intern
99 * @return the interned identity.
102 GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
111 map = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
112 e = GNUNET_CONTAINER_multipeermap_get (map, pid);
115 GNUNET_assert (e->rc > 0);
119 ret = free_list_start;
122 GNUNET_array_grow (table, size, size + 16);
123 for (i = ret; i < size; i++)
125 table[i] = GNUNET_new (struct PeerEntry);
126 table[i]->pid = i + 1;
131 memset (&table[0]->id, 0, sizeof (struct GNUNET_PeerIdentity));
136 GNUNET_assert (ret < size);
137 GNUNET_assert (0 == table[ret]->rc);
138 free_list_start = table[ret]->pid;
139 table[ret]->id = *pid;
141 table[ret]->pid = ret;
142 GNUNET_break (GNUNET_OK ==
143 GNUNET_CONTAINER_multipeermap_put (map,
146 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
152 * Decrement multiple RCs of peer identities by one.
154 * @param ids array of PIDs to decrement the RCs of
155 * @param count size of the ids array
158 GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
165 for (i = count - 1; i >= 0; i--)
170 GNUNET_assert (id < size);
171 GNUNET_assert (table[id]->rc > 0);
173 if (0 == table[id]->rc)
175 GNUNET_break (GNUNET_OK ==
176 GNUNET_CONTAINER_multipeermap_remove (map,
179 table[id]->pid = free_list_start;
180 free_list_start = id;
187 * Change the reference counter of an interned PID.
189 * @param id identity to change the RC of
190 * @param delta how much to change the RC
193 GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
197 GNUNET_assert (id < size);
198 GNUNET_assert (table[id]->rc > 0);
199 GNUNET_assert ( (delta >= 0) ||
200 (table[id]->rc >= (unsigned int) (-delta)) );
201 table[id]->rc += delta;
202 if (0 == table[id]->rc)
204 GNUNET_break (GNUNET_OK ==
205 GNUNET_CONTAINER_multipeermap_remove (map,
208 table[id]->pid = free_list_start;
209 free_list_start = id;
215 * Convert an interned PID to a normal peer identity.
217 * @param id interned PID to convert
218 * @param pid where to write the normal peer identity
221 GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
225 memset (pid, 0, sizeof (struct GNUNET_PeerIdentity));
228 GNUNET_assert (id < size);
229 GNUNET_assert (table[id]->rc > 0);
230 *pid = table[id]->id;
235 * Convert an interned PID to a normal peer identity.
237 * @param id interned PID to convert
238 * @return pointer to peer identity, valid as long 'id' is valid
240 const struct GNUNET_PeerIdentity *
241 GNUNET_PEER_resolve2 (GNUNET_PEER_Id id)
243 GNUNET_assert (id < size);
244 GNUNET_assert (table[id]->rc > 0);
245 return &table[id]->id;