aboutsummaryrefslogtreecommitdiff
path: root/src/util/peer.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-10-14 18:27:22 +0000
committerChristian Grothoff <christian@grothoff.org>2012-10-14 18:27:22 +0000
commitf2061adfbfeabe988a185145d9df140d14131e74 (patch)
tree496e7deaebf0dc8c75c2c7f1e308ef9f3c633b2f /src/util/peer.c
parenta4582ea33040edb25b836eb4533ba085fe70752e (diff)
downloadgnunet-f2061adfbfeabe988a185145d9df140d14131e74.tar.gz
gnunet-f2061adfbfeabe988a185145d9df140d14131e74.zip
-enable peer API to return pointer to interned peer hash code
Diffstat (limited to 'src/util/peer.c')
-rw-r--r--src/util/peer.c98
1 files changed, 55 insertions, 43 deletions
diff --git a/src/util/peer.c b/src/util/peer.c
index a0e965605..1ad3ee264 100644
--- a/src/util/peer.c
+++ b/src/util/peer.c
@@ -53,7 +53,7 @@ struct PeerEntry
53/** 53/**
54 * Table with our interned peer IDs. 54 * Table with our interned peer IDs.
55 */ 55 */
56static struct PeerEntry *table; 56static struct PeerEntry **table;
57 57
58/** 58/**
59 * Hashmap of PeerIdentities to "struct PeerEntry" 59 * Hashmap of PeerIdentities to "struct PeerEntry"
@@ -84,15 +84,13 @@ GNUNET_PEER_Id
84GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid) 84GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
85{ 85{
86 struct PeerEntry *e; 86 struct PeerEntry *e;
87 long off;
88 87
89 if (pid == NULL) 88 if (NULL == pid)
90 return 0; 89 return 0;
91 if (NULL == map) 90 if (NULL == map)
92 return 0; 91 return 0;
93 off = (long) GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey); 92 e = GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey);
94 e = (off == 0) ? NULL : &table[off]; 93 if (NULL == e)
95 if (e == NULL)
96 return 0; 94 return 0;
97 GNUNET_assert (e->rc > 0); 95 GNUNET_assert (e->rc > 0);
98 return e->pid; 96 return e->pid;
@@ -112,15 +110,13 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
112 GNUNET_PEER_Id ret; 110 GNUNET_PEER_Id ret;
113 struct PeerEntry *e; 111 struct PeerEntry *e;
114 unsigned int i; 112 unsigned int i;
115 long off;
116 113
117 if (pid == NULL) 114 if (NULL == pid)
118 return 0; 115 return 0;
119 if (NULL == map) 116 if (NULL == map)
120 map = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES); 117 map = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
121 off = (long) GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey); 118 e = GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey);
122 e = (off == 0) ? NULL : &table[off]; 119 if (NULL != e)
123 if (e != NULL)
124 { 120 {
125 GNUNET_assert (e->rc > 0); 121 GNUNET_assert (e->rc > 0);
126 e->rc++; 122 e->rc++;
@@ -131,23 +127,27 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
131 { 127 {
132 GNUNET_array_grow (table, size, size + 16); 128 GNUNET_array_grow (table, size, size + 16);
133 for (i = ret; i < size; i++) 129 for (i = ret; i < size; i++)
134 table[i].pid = i + 1; 130 {
131 table[i] = GNUNET_malloc (sizeof (struct PeerEntry));
132 table[i]->pid = i + 1;
133 }
135 } 134 }
136 if (ret == 0) 135 if (0 == ret)
137 { 136 {
138 table[0].pid = 0; 137 table[0]->pid = 0;
139 table[0].rc = 1; 138 table[0]->rc = 1;
140 ret = 1; 139 ret = 1;
141 } 140 }
142 GNUNET_assert (ret < size); 141 GNUNET_assert (ret < size);
143 GNUNET_assert (table[ret].rc == 0); 142 GNUNET_assert (0 == table[ret]->rc);
144 free_list_start = table[ret].pid; 143 free_list_start = table[ret]->pid;
145 table[ret].id = *pid; 144 table[ret]->id = *pid;
146 table[ret].rc = 1; 145 table[ret]->rc = 1;
147 table[ret].pid = ret; 146 table[ret]->pid = ret;
148 GNUNET_break (GNUNET_OK == 147 GNUNET_break (GNUNET_OK ==
149 GNUNET_CONTAINER_multihashmap_put (map, &table[ret].id.hashPubKey, 148 GNUNET_CONTAINER_multihashmap_put (map,
150 (void *) (long) ret, 149 &table[ret]->id.hashPubKey,
150 table[ret],
151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
152 return ret; 152 return ret;
153} 153}
@@ -165,24 +165,23 @@ GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
165 int i; 165 int i;
166 GNUNET_PEER_Id id; 166 GNUNET_PEER_Id id;
167 167
168 if (count == 0) 168 if (0 == count)
169 return; 169 return;
170 for (i = count - 1; i >= 0; i--) 170 for (i = count - 1; i >= 0; i--)
171 { 171 {
172 id = ids[i]; 172 id = ids[i];
173 if (id == 0) 173 if (0 == id)
174 continue; 174 continue;
175 GNUNET_assert (id < size); 175 GNUNET_assert (id < size);
176 GNUNET_assert (table[id].rc > 0); 176 GNUNET_assert (table[id]->rc > 0);
177 table[id].rc--; 177 table[id]->rc--;
178 if (table[id].rc == 0) 178 if (0 == table[id]->rc)
179 { 179 {
180 GNUNET_break (GNUNET_OK == 180 GNUNET_break (GNUNET_OK ==
181 GNUNET_CONTAINER_multihashmap_remove (map, 181 GNUNET_CONTAINER_multihashmap_remove (map,
182 &table[id]. 182 &table[id]->id.hashPubKey,
183 id.hashPubKey, 183 table[id]));
184 (void *) (long) id)); 184 table[id]->pid = free_list_start;
185 table[id].pid = free_list_start;
186 free_list_start = id; 185 free_list_start = id;
187 } 186 }
188 } 187 }
@@ -198,20 +197,19 @@ GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
198void 197void
199GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta) 198GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
200{ 199{
201 if (id == 0) 200 if (0 == id)
202 return; 201 return;
203 GNUNET_assert (id < size); 202 GNUNET_assert (id < size);
204 GNUNET_assert (table[id].rc > 0); 203 GNUNET_assert (table[id]->rc > 0);
205 GNUNET_assert ((delta >= 0) || (table[id].rc >= -delta)); 204 GNUNET_assert ((delta >= 0) || (table[id]->rc >= -delta));
206 table[id].rc += delta; 205 table[id]->rc += delta;
207 if (table[id].rc == 0) 206 if (0 == table[id]->rc)
208 { 207 {
209 GNUNET_break (GNUNET_OK == 208 GNUNET_break (GNUNET_OK ==
210 GNUNET_CONTAINER_multihashmap_remove (map, 209 GNUNET_CONTAINER_multihashmap_remove (map,
211 &table[id]. 210 &table[id]->id.hashPubKey,
212 id.hashPubKey, 211 table[id]));
213 (void *) (long) id)); 212 table[id]->pid = free_list_start;
214 table[id].pid = free_list_start;
215 free_list_start = id; 213 free_list_start = id;
216 } 214 }
217} 215}
@@ -226,16 +224,30 @@ GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
226void 224void
227GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid) 225GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
228{ 226{
229 if (id == 0) 227 if (0 == id)
230 { 228 {
231 memset (pid, 0, sizeof (struct GNUNET_PeerIdentity)); 229 memset (pid, 0, sizeof (struct GNUNET_PeerIdentity));
232 GNUNET_break (0); 230 GNUNET_break (0);
233 return; 231 return;
234 } 232 }
235 GNUNET_assert (id < size); 233 GNUNET_assert (id < size);
236 GNUNET_assert (table[id].rc > 0); 234 GNUNET_assert (table[id]->rc > 0);
237 *pid = table[id].id; 235 *pid = table[id]->id;
236}
237
238
239/**
240 * Convert an interned PID to a normal peer identity.
241 *
242 * @param id interned PID to convert
243 * @return pointer to peer identity, valid as long 'id' is valid
244 */
245const struct GNUNET_PeerIdentity *
246GNUNET_PEER_resolve2 (GNUNET_PEER_Id id)
247{
248 return &table[id]->id;
238} 249}
239 250
240 251
252
241/* end of peer.c */ 253/* end of peer.c */