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