-remove async ecc key generation, not needed
[oweals/gnunet.git] / src / util / peer.c
1 /*
2       This file is part of GNUnet
3      (C) 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
22  * @file util/peer.c
23  * @brief peer-ID table that assigns integer IDs to peer-IDs to save memory
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_peer_lib.h"
29
30 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
31
32
33 struct PeerEntry
34 {
35   /**
36    * The identifier itself
37    */
38   struct GNUNET_PeerIdentity id;
39
40   /**
41    * Short version of the identifier; if the RC==0, then index of next
42    * free slot in table, otherwise equal to this slot in the table.
43    */
44   GNUNET_PEER_Id pid;
45
46   /**
47    * Reference counter, 0 if this slot is not used.
48    */
49   unsigned int rc;
50 };
51
52
53 /**
54  * Table with our interned peer IDs.
55  */
56 static struct PeerEntry **table;
57
58 /**
59  * Hashmap of PeerIdentities to "struct PeerEntry"
60  * (for fast lookup).  NULL until the library
61  * is actually being used.
62  */
63 static struct GNUNET_CONTAINER_MultiHashMap *map;
64
65 /**
66  * Size of the "table".
67  */
68 static unsigned int size;
69
70 /**
71  * Index of the beginning of the free list in the table; set to "size"
72  * if no slots are free in the table.
73  */
74 static unsigned int free_list_start;
75
76
77 /**
78  * Search for a peer identity. The reference counter is not changed.
79  *
80  * @param pid identity to find
81  * @return the interned identity or 0.
82  */
83 GNUNET_PEER_Id
84 GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
85 {
86   struct PeerEntry *e;
87
88   if (NULL == pid)
89     return 0;
90   if (NULL == map)
91     return 0;
92   e = GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey);
93   if (NULL == e)
94     return 0;
95   GNUNET_assert (e->rc > 0);
96   return e->pid;
97 }
98
99
100 /**
101  * Intern an peer identity.  If the identity is already known, its
102  * reference counter will be increased by one.
103  *
104  * @param pid identity to intern
105  * @return the interned identity.
106  */
107 GNUNET_PEER_Id
108 GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
109 {
110   GNUNET_PEER_Id ret;
111   struct PeerEntry *e;
112   unsigned int i;
113
114   if (NULL == pid)
115     return 0;
116   if (NULL == map)
117     map = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
118   e = GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey);
119   if (NULL != e)
120   {
121     GNUNET_assert (e->rc > 0);
122     e->rc++;
123     return e->pid;
124   }
125   ret = free_list_start;
126   if (ret == size)
127   {
128     GNUNET_array_grow (table, size, size + 16);
129     for (i = ret; i < size; i++)
130     {
131       table[i] = GNUNET_malloc (sizeof (struct PeerEntry));
132       table[i]->pid = i + 1;
133     }
134   }
135   if (0 == ret)
136   {
137     table[0]->pid = 0;
138     table[0]->rc = 1;
139     ret = 1;
140   }
141   GNUNET_assert (ret < size);
142   GNUNET_assert (0 == table[ret]->rc);
143   free_list_start = table[ret]->pid;
144   table[ret]->id = *pid;
145   table[ret]->rc = 1;
146   table[ret]->pid = ret;
147   GNUNET_break (GNUNET_OK ==
148                 GNUNET_CONTAINER_multihashmap_put (map, 
149                                                    &table[ret]->id.hashPubKey,
150                                                    table[ret],
151                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
152   return ret;
153 }
154
155
156 /**
157  * Decrement multiple RCs of peer identities by one.
158  *
159  * @param ids array of PIDs to decrement the RCs of
160  * @param count size of the ids array
161  */
162 void
163 GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
164 {
165   int i;
166   GNUNET_PEER_Id id;
167
168   if (0 == count)
169     return;
170   for (i = count - 1; i >= 0; i--)
171   {
172     id = ids[i];
173     if (0 == id)
174       continue;
175     GNUNET_assert (id < size);
176     GNUNET_assert (table[id]->rc > 0);
177     table[id]->rc--;
178     if (0 == table[id]->rc)
179     {
180       GNUNET_break (GNUNET_OK ==
181                     GNUNET_CONTAINER_multihashmap_remove (map,
182                                                           &table[id]->id.hashPubKey,
183                                                           table[id]));
184       table[id]->pid = free_list_start;
185       free_list_start = id;
186     }
187   }
188 }
189
190
191 /**
192  * Change the reference counter of an interned PID.
193  *
194  * @param id identity to change the RC of
195  * @param delta how much to change the RC
196  */
197 void
198 GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
199 {
200   if (0 == id)
201     return;
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)
207   {
208     GNUNET_break (GNUNET_OK ==
209                   GNUNET_CONTAINER_multihashmap_remove (map,
210                                                         &table[id]->id.hashPubKey,
211                                                         table[id]));
212     table[id]->pid = free_list_start;
213     free_list_start = id;
214   }
215 }
216
217
218 /**
219  * Convert an interned PID to a normal peer identity.
220  *
221  * @param id interned PID to convert
222  * @param pid where to write the normal peer identity
223  */
224 void
225 GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
226 {
227   if (0 == id)
228   {
229     memset (pid, 0, sizeof (struct GNUNET_PeerIdentity));
230     return;
231   }
232   GNUNET_assert (id < size);
233   GNUNET_assert (table[id]->rc > 0);
234   *pid = table[id]->id;
235 }
236
237
238 /**
239  * Convert an interned PID to a normal peer identity.
240  *
241  * @param id interned PID to convert
242  * @return pointer to peer identity, valid as long 'id' is valid
243  */
244 const struct GNUNET_PeerIdentity *
245 GNUNET_PEER_resolve2 (GNUNET_PEER_Id id)
246 {
247   return &table[id]->id;
248 }
249
250
251
252 /* end of peer.c */