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