aboutsummaryrefslogtreecommitdiff
path: root/src/util/peer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/peer.c')
-rw-r--r--src/util/peer.c141
1 files changed, 70 insertions, 71 deletions
diff --git a/src/util/peer.c b/src/util/peer.c
index 00a259a3b..09347cbfd 100644
--- a/src/util/peer.c
+++ b/src/util/peer.c
@@ -11,7 +11,7 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
@@ -26,11 +26,10 @@
26#include "platform.h" 26#include "platform.h"
27#include "gnunet_peer_lib.h" 27#include "gnunet_peer_lib.h"
28 28
29#define LOG(kind,...) GNUNET_log_from (kind, "util-peer", __VA_ARGS__) 29#define LOG(kind, ...) GNUNET_log_from(kind, "util-peer", __VA_ARGS__)
30 30
31 31
32struct PeerEntry 32struct PeerEntry {
33{
34 /** 33 /**
35 * The identifier itself 34 * The identifier itself
36 */ 35 */
@@ -80,7 +79,7 @@ static unsigned int free_list_start;
80 * @return the interned identity or 0. 79 * @return the interned identity or 0.
81 */ 80 */
82GNUNET_PEER_Id 81GNUNET_PEER_Id
83GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid) 82GNUNET_PEER_search(const struct GNUNET_PeerIdentity *pid)
84{ 83{
85 struct PeerEntry *e; 84 struct PeerEntry *e;
86 85
@@ -88,10 +87,10 @@ GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
88 return 0; 87 return 0;
89 if (NULL == map) 88 if (NULL == map)
90 return 0; 89 return 0;
91 e = GNUNET_CONTAINER_multipeermap_get (map, pid); 90 e = GNUNET_CONTAINER_multipeermap_get(map, pid);
92 if (NULL == e) 91 if (NULL == e)
93 return 0; 92 return 0;
94 GNUNET_assert (e->rc > 0); 93 GNUNET_assert(e->rc > 0);
95 return e->pid; 94 return e->pid;
96} 95}
97 96
@@ -104,7 +103,7 @@ GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
104 * @return the interned identity. 103 * @return the interned identity.
105 */ 104 */
106GNUNET_PEER_Id 105GNUNET_PEER_Id
107GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid) 106GNUNET_PEER_intern(const struct GNUNET_PeerIdentity *pid)
108{ 107{
109 GNUNET_PEER_Id ret; 108 GNUNET_PEER_Id ret;
110 struct PeerEntry *e; 109 struct PeerEntry *e;
@@ -113,42 +112,42 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
113 if (NULL == pid) 112 if (NULL == pid)
114 return 0; 113 return 0;
115 if (NULL == map) 114 if (NULL == map)
116 map = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES); 115 map = GNUNET_CONTAINER_multipeermap_create(32, GNUNET_YES);
117 e = GNUNET_CONTAINER_multipeermap_get (map, pid); 116 e = GNUNET_CONTAINER_multipeermap_get(map, pid);
118 if (NULL != e) 117 if (NULL != e)
119 { 118 {
120 GNUNET_assert (e->rc > 0); 119 GNUNET_assert(e->rc > 0);
121 e->rc++; 120 e->rc++;
122 return e->pid; 121 return e->pid;
123 } 122 }
124 ret = free_list_start; 123 ret = free_list_start;
125 if (ret == size) 124 if (ret == size)
126 {
127 GNUNET_array_grow (table, size, size + 16);
128 for (i = ret; i < size; i++)
129 { 125 {
130 table[i] = GNUNET_new (struct PeerEntry); 126 GNUNET_array_grow(table, size, size + 16);
131 table[i]->pid = i + 1; 127 for (i = ret; i < size; i++)
128 {
129 table[i] = GNUNET_new(struct PeerEntry);
130 table[i]->pid = i + 1;
131 }
132 } 132 }
133 }
134 if (0 == ret) 133 if (0 == ret)
135 { 134 {
136 memset (&table[0]->id, 0, sizeof (struct GNUNET_PeerIdentity)); 135 memset(&table[0]->id, 0, sizeof(struct GNUNET_PeerIdentity));
137 table[0]->pid = 0; 136 table[0]->pid = 0;
138 table[0]->rc = 1; 137 table[0]->rc = 1;
139 ret = 1; 138 ret = 1;
140 } 139 }
141 GNUNET_assert (ret < size); 140 GNUNET_assert(ret < size);
142 GNUNET_assert (0 == table[ret]->rc); 141 GNUNET_assert(0 == table[ret]->rc);
143 free_list_start = table[ret]->pid; 142 free_list_start = table[ret]->pid;
144 table[ret]->id = *pid; 143 table[ret]->id = *pid;
145 table[ret]->rc = 1; 144 table[ret]->rc = 1;
146 table[ret]->pid = ret; 145 table[ret]->pid = ret;
147 GNUNET_break (GNUNET_OK == 146 GNUNET_break(GNUNET_OK ==
148 GNUNET_CONTAINER_multipeermap_put (map, 147 GNUNET_CONTAINER_multipeermap_put(map,
149 &table[ret]->id, 148 &table[ret]->id,
150 table[ret], 149 table[ret],
151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 150 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
152 return ret; 151 return ret;
153} 152}
154 153
@@ -160,7 +159,7 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
160 * @param count size of the ids array 159 * @param count size of the ids array
161 */ 160 */
162void 161void
163GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count) 162GNUNET_PEER_decrement_rcs(const GNUNET_PEER_Id *ids, unsigned int count)
164{ 163{
165 int i; 164 int i;
166 GNUNET_PEER_Id id; 165 GNUNET_PEER_Id id;
@@ -168,23 +167,23 @@ GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
168 if (0 == count) 167 if (0 == count)
169 return; 168 return;
170 for (i = count - 1; i >= 0; i--) 169 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 { 170 {
180 GNUNET_break (GNUNET_OK == 171 id = ids[i];
181 GNUNET_CONTAINER_multipeermap_remove (map, 172 if (0 == id)
182 &table[id]->id, 173 continue;
183 table[id])); 174 GNUNET_assert(id < size);
184 table[id]->pid = free_list_start; 175 GNUNET_assert(table[id]->rc > 0);
185 free_list_start = id; 176 table[id]->rc--;
177 if (0 == table[id]->rc)
178 {
179 GNUNET_break(GNUNET_OK ==
180 GNUNET_CONTAINER_multipeermap_remove(map,
181 &table[id]->id,
182 table[id]));
183 table[id]->pid = free_list_start;
184 free_list_start = id;
185 }
186 } 186 }
187 }
188} 187}
189 188
190 189
@@ -195,24 +194,24 @@ GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
195 * @param delta how much to change the RC 194 * @param delta how much to change the RC
196 */ 195 */
197void 196void
198GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta) 197GNUNET_PEER_change_rc(GNUNET_PEER_Id id, int delta)
199{ 198{
200 if (0 == id) 199 if (0 == id)
201 return; 200 return;
202 GNUNET_assert (id < size); 201 GNUNET_assert(id < size);
203 GNUNET_assert (table[id]->rc > 0); 202 GNUNET_assert(table[id]->rc > 0);
204 GNUNET_assert ( (delta >= 0) || 203 GNUNET_assert((delta >= 0) ||
205 (table[id]->rc >= (unsigned int) (-delta)) ); 204 (table[id]->rc >= (unsigned int)(-delta)));
206 table[id]->rc += delta; 205 table[id]->rc += delta;
207 if (0 == table[id]->rc) 206 if (0 == table[id]->rc)
208 { 207 {
209 GNUNET_break (GNUNET_OK == 208 GNUNET_break(GNUNET_OK ==
210 GNUNET_CONTAINER_multipeermap_remove (map, 209 GNUNET_CONTAINER_multipeermap_remove(map,
211 &table[id]->id, 210 &table[id]->id,
212 table[id])); 211 table[id]));
213 table[id]->pid = free_list_start; 212 table[id]->pid = free_list_start;
214 free_list_start = id; 213 free_list_start = id;
215 } 214 }
216} 215}
217 216
218 217
@@ -223,15 +222,15 @@ GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
223 * @param pid where to write the normal peer identity 222 * @param pid where to write the normal peer identity
224 */ 223 */
225void 224void
226GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid) 225GNUNET_PEER_resolve(GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
227{ 226{
228 if (0 == id) 227 if (0 == id)
229 { 228 {
230 memset (pid, 0, sizeof (struct GNUNET_PeerIdentity)); 229 memset(pid, 0, sizeof(struct GNUNET_PeerIdentity));
231 return; 230 return;
232 } 231 }
233 GNUNET_assert (id < size); 232 GNUNET_assert(id < size);
234 GNUNET_assert (table[id]->rc > 0); 233 GNUNET_assert(table[id]->rc > 0);
235 *pid = table[id]->id; 234 *pid = table[id]->id;
236} 235}
237 236
@@ -243,10 +242,10 @@ GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
243 * @return pointer to peer identity, valid as long 'id' is valid 242 * @return pointer to peer identity, valid as long 'id' is valid
244 */ 243 */
245const struct GNUNET_PeerIdentity * 244const struct GNUNET_PeerIdentity *
246GNUNET_PEER_resolve2 (GNUNET_PEER_Id id) 245GNUNET_PEER_resolve2(GNUNET_PEER_Id id)
247{ 246{
248 GNUNET_assert (id < size); 247 GNUNET_assert(id < size);
249 GNUNET_assert (table[id]->rc > 0); 248 GNUNET_assert(table[id]->rc > 0);
250 return &table[id]->id; 249 return &table[id]->id;
251} 250}
252 251