diff options
Diffstat (limited to 'src/lib/util/peer.c')
-rw-r--r-- | src/lib/util/peer.c | 242 |
1 files changed, 242 insertions, 0 deletions
diff --git a/src/lib/util/peer.c b/src/lib/util/peer.c new file mode 100644 index 000000000..e5c15b098 --- /dev/null +++ b/src/lib/util/peer.c | |||
@@ -0,0 +1,242 @@ | |||
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 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
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_util_lib.h" | ||
29 | |||
30 | #define LOG(kind, ...) GNUNET_log_from (kind, "util-peer", __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 | * Peermap of PeerIdentities to "struct PeerEntry" | ||
60 | * (for fast lookup). NULL until the library | ||
61 | * is actually being used. | ||
62 | */ | ||
63 | static struct GNUNET_CONTAINER_MultiPeerMap *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_multipeermap_get (map, pid); | ||
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 | |||
113 | if (NULL == pid) | ||
114 | return 0; | ||
115 | if (NULL == map) | ||
116 | map = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES); | ||
117 | e = GNUNET_CONTAINER_multipeermap_get (map, pid); | ||
118 | if (NULL != e) | ||
119 | { | ||
120 | GNUNET_assert (e->rc > 0); | ||
121 | e->rc++; | ||
122 | return e->pid; | ||
123 | } | ||
124 | ret = free_list_start; | ||
125 | if (ret == size) | ||
126 | { | ||
127 | GNUNET_array_grow (table, size, size + 16); | ||
128 | for (unsigned int i = ret; i < size; i++) | ||
129 | { | ||
130 | table[i] = GNUNET_new (struct PeerEntry); | ||
131 | table[i]->pid = i + 1; | ||
132 | } | ||
133 | } | ||
134 | if (0 == ret) | ||
135 | { | ||
136 | memset (&table[0]->id, 0, sizeof(struct GNUNET_PeerIdentity)); | ||
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_multipeermap_put (map, | ||
149 | &table[ret]->id, | ||
150 | table[ret], | ||
151 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
152 | return ret; | ||
153 | } | ||
154 | |||
155 | |||
156 | void | ||
157 | GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count) | ||
158 | { | ||
159 | int i; | ||
160 | GNUNET_PEER_Id id; | ||
161 | |||
162 | if (0 == count) | ||
163 | return; | ||
164 | for (i = count - 1; i >= 0; i--) | ||
165 | { | ||
166 | id = ids[i]; | ||
167 | if (0 == id) | ||
168 | continue; | ||
169 | GNUNET_assert (id < size); | ||
170 | GNUNET_assert (table[id]->rc > 0); | ||
171 | table[id]->rc--; | ||
172 | if (0 == table[id]->rc) | ||
173 | { | ||
174 | GNUNET_break (GNUNET_OK == | ||
175 | GNUNET_CONTAINER_multipeermap_remove (map, | ||
176 | &table[id]->id, | ||
177 | table[id])); | ||
178 | table[id]->pid = free_list_start; | ||
179 | free_list_start = id; | ||
180 | } | ||
181 | } | ||
182 | } | ||
183 | |||
184 | |||
185 | /** | ||
186 | * Change the reference counter of an interned PID. | ||
187 | * | ||
188 | * @param id identity to change the RC of | ||
189 | * @param delta how much to change the RC | ||
190 | */ | ||
191 | void | ||
192 | GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta) | ||
193 | { | ||
194 | if (0 == id) | ||
195 | return; | ||
196 | GNUNET_assert (id < size); | ||
197 | GNUNET_assert (table[id]->rc > 0); | ||
198 | GNUNET_assert ((delta >= 0) || | ||
199 | (table[id]->rc >= (unsigned int) (-delta))); | ||
200 | table[id]->rc += delta; | ||
201 | if (0 == table[id]->rc) | ||
202 | { | ||
203 | GNUNET_break (GNUNET_OK == | ||
204 | GNUNET_CONTAINER_multipeermap_remove (map, | ||
205 | &table[id]->id, | ||
206 | table[id])); | ||
207 | table[id]->pid = free_list_start; | ||
208 | free_list_start = id; | ||
209 | } | ||
210 | } | ||
211 | |||
212 | |||
213 | /** | ||
214 | * Convert an interned PID to a normal peer identity. | ||
215 | * | ||
216 | * @param id interned PID to convert | ||
217 | * @param pid where to write the normal peer identity | ||
218 | */ | ||
219 | void | ||
220 | GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid) | ||
221 | { | ||
222 | if (0 == id) | ||
223 | { | ||
224 | memset (pid, 0, sizeof(struct GNUNET_PeerIdentity)); | ||
225 | return; | ||
226 | } | ||
227 | GNUNET_assert (id < size); | ||
228 | GNUNET_assert (table[id]->rc > 0); | ||
229 | *pid = table[id]->id; | ||
230 | } | ||
231 | |||
232 | |||
233 | const struct GNUNET_PeerIdentity * | ||
234 | GNUNET_PEER_resolve2 (GNUNET_PEER_Id id) | ||
235 | { | ||
236 | GNUNET_assert (id < size); | ||
237 | GNUNET_assert (table[id]->rc > 0); | ||
238 | return &table[id]->id; | ||
239 | } | ||
240 | |||
241 | |||
242 | /* end of peer.c */ | ||