diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-10-05 15:01:15 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-10-05 15:01:15 +0200 |
commit | fbb71d527c7d6babf269a8fefce1db291b9f7068 (patch) | |
tree | c4f099eb2c6c34bafc2a3a884a8c71ed9bad096a /src/util | |
parent | eb7335e9a9008d6367562483c70325e83a652429 (diff) | |
download | gnunet-fbb71d527c7d6babf269a8fefce1db291b9f7068.tar.gz gnunet-fbb71d527c7d6babf269a8fefce1db291b9f7068.zip |
reduce scope
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/peer.c | 140 |
1 files changed, 70 insertions, 70 deletions
diff --git a/src/util/peer.c b/src/util/peer.c index 09347cbfd..3216f1a57 100644 --- a/src/util/peer.c +++ b/src/util/peer.c | |||
@@ -26,10 +26,11 @@ | |||
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 | ||
32 | struct PeerEntry { | 32 | struct PeerEntry |
33 | { | ||
33 | /** | 34 | /** |
34 | * The identifier itself | 35 | * The identifier itself |
35 | */ | 36 | */ |
@@ -79,7 +80,7 @@ static unsigned int free_list_start; | |||
79 | * @return the interned identity or 0. | 80 | * @return the interned identity or 0. |
80 | */ | 81 | */ |
81 | GNUNET_PEER_Id | 82 | GNUNET_PEER_Id |
82 | GNUNET_PEER_search(const struct GNUNET_PeerIdentity *pid) | 83 | GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid) |
83 | { | 84 | { |
84 | struct PeerEntry *e; | 85 | struct PeerEntry *e; |
85 | 86 | ||
@@ -87,10 +88,10 @@ GNUNET_PEER_search(const struct GNUNET_PeerIdentity *pid) | |||
87 | return 0; | 88 | return 0; |
88 | if (NULL == map) | 89 | if (NULL == map) |
89 | return 0; | 90 | return 0; |
90 | e = GNUNET_CONTAINER_multipeermap_get(map, pid); | 91 | e = GNUNET_CONTAINER_multipeermap_get (map, pid); |
91 | if (NULL == e) | 92 | if (NULL == e) |
92 | return 0; | 93 | return 0; |
93 | GNUNET_assert(e->rc > 0); | 94 | GNUNET_assert (e->rc > 0); |
94 | return e->pid; | 95 | return e->pid; |
95 | } | 96 | } |
96 | 97 | ||
@@ -103,51 +104,50 @@ GNUNET_PEER_search(const struct GNUNET_PeerIdentity *pid) | |||
103 | * @return the interned identity. | 104 | * @return the interned identity. |
104 | */ | 105 | */ |
105 | GNUNET_PEER_Id | 106 | GNUNET_PEER_Id |
106 | GNUNET_PEER_intern(const struct GNUNET_PeerIdentity *pid) | 107 | GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid) |
107 | { | 108 | { |
108 | GNUNET_PEER_Id ret; | 109 | GNUNET_PEER_Id ret; |
109 | struct PeerEntry *e; | 110 | struct PeerEntry *e; |
110 | unsigned int i; | ||
111 | 111 | ||
112 | if (NULL == pid) | 112 | if (NULL == pid) |
113 | return 0; | 113 | return 0; |
114 | if (NULL == map) | 114 | if (NULL == map) |
115 | map = GNUNET_CONTAINER_multipeermap_create(32, GNUNET_YES); | 115 | map = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES); |
116 | e = GNUNET_CONTAINER_multipeermap_get(map, pid); | 116 | e = GNUNET_CONTAINER_multipeermap_get (map, pid); |
117 | if (NULL != e) | 117 | if (NULL != e) |
118 | { | 118 | { |
119 | GNUNET_assert(e->rc > 0); | 119 | GNUNET_assert (e->rc > 0); |
120 | e->rc++; | 120 | e->rc++; |
121 | return e->pid; | 121 | return e->pid; |
122 | } | 122 | } |
123 | ret = free_list_start; | 123 | ret = free_list_start; |
124 | if (ret == size) | 124 | if (ret == size) |
125 | { | ||
126 | GNUNET_array_grow (table, size, size + 16); | ||
127 | for (unsigned int i = ret; i < size; i++) | ||
125 | { | 128 | { |
126 | GNUNET_array_grow(table, size, size + 16); | 129 | table[i] = GNUNET_new (struct PeerEntry); |
127 | for (i = ret; i < size; i++) | 130 | table[i]->pid = i + 1; |
128 | { | ||
129 | table[i] = GNUNET_new(struct PeerEntry); | ||
130 | table[i]->pid = i + 1; | ||
131 | } | ||
132 | } | 131 | } |
132 | } | ||
133 | if (0 == ret) | 133 | if (0 == ret) |
134 | { | 134 | { |
135 | memset(&table[0]->id, 0, sizeof(struct GNUNET_PeerIdentity)); | 135 | memset (&table[0]->id, 0, sizeof(struct GNUNET_PeerIdentity)); |
136 | table[0]->pid = 0; | 136 | table[0]->pid = 0; |
137 | table[0]->rc = 1; | 137 | table[0]->rc = 1; |
138 | ret = 1; | 138 | ret = 1; |
139 | } | 139 | } |
140 | GNUNET_assert(ret < size); | 140 | GNUNET_assert (ret < size); |
141 | GNUNET_assert(0 == table[ret]->rc); | 141 | GNUNET_assert (0 == table[ret]->rc); |
142 | free_list_start = table[ret]->pid; | 142 | free_list_start = table[ret]->pid; |
143 | table[ret]->id = *pid; | 143 | table[ret]->id = *pid; |
144 | table[ret]->rc = 1; | 144 | table[ret]->rc = 1; |
145 | table[ret]->pid = ret; | 145 | table[ret]->pid = ret; |
146 | GNUNET_break(GNUNET_OK == | 146 | GNUNET_break (GNUNET_OK == |
147 | GNUNET_CONTAINER_multipeermap_put(map, | 147 | GNUNET_CONTAINER_multipeermap_put (map, |
148 | &table[ret]->id, | 148 | &table[ret]->id, |
149 | table[ret], | 149 | table[ret], |
150 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | 150 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); |
151 | return ret; | 151 | return ret; |
152 | } | 152 | } |
153 | 153 | ||
@@ -159,7 +159,7 @@ GNUNET_PEER_intern(const struct GNUNET_PeerIdentity *pid) | |||
159 | * @param count size of the ids array | 159 | * @param count size of the ids array |
160 | */ | 160 | */ |
161 | void | 161 | void |
162 | GNUNET_PEER_decrement_rcs(const GNUNET_PEER_Id *ids, unsigned int count) | 162 | GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count) |
163 | { | 163 | { |
164 | int i; | 164 | int i; |
165 | GNUNET_PEER_Id id; | 165 | GNUNET_PEER_Id id; |
@@ -167,23 +167,23 @@ GNUNET_PEER_decrement_rcs(const GNUNET_PEER_Id *ids, unsigned int count) | |||
167 | if (0 == count) | 167 | if (0 == count) |
168 | return; | 168 | return; |
169 | for (i = count - 1; i >= 0; i--) | 169 | for (i = count - 1; i >= 0; i--) |
170 | { | ||
171 | id = ids[i]; | ||
172 | if (0 == id) | ||
173 | continue; | ||
174 | GNUNET_assert (id < size); | ||
175 | GNUNET_assert (table[id]->rc > 0); | ||
176 | table[id]->rc--; | ||
177 | if (0 == table[id]->rc) | ||
170 | { | 178 | { |
171 | id = ids[i]; | 179 | GNUNET_break (GNUNET_OK == |
172 | if (0 == id) | 180 | GNUNET_CONTAINER_multipeermap_remove (map, |
173 | continue; | 181 | &table[id]->id, |
174 | GNUNET_assert(id < size); | 182 | table[id])); |
175 | GNUNET_assert(table[id]->rc > 0); | 183 | table[id]->pid = free_list_start; |
176 | table[id]->rc--; | 184 | free_list_start = id; |
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 | } | 185 | } |
186 | } | ||
187 | } | 187 | } |
188 | 188 | ||
189 | 189 | ||
@@ -194,24 +194,24 @@ GNUNET_PEER_decrement_rcs(const GNUNET_PEER_Id *ids, unsigned int count) | |||
194 | * @param delta how much to change the RC | 194 | * @param delta how much to change the RC |
195 | */ | 195 | */ |
196 | void | 196 | void |
197 | GNUNET_PEER_change_rc(GNUNET_PEER_Id id, int delta) | 197 | GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta) |
198 | { | 198 | { |
199 | if (0 == id) | 199 | if (0 == id) |
200 | return; | 200 | return; |
201 | GNUNET_assert(id < size); | 201 | GNUNET_assert (id < size); |
202 | GNUNET_assert(table[id]->rc > 0); | 202 | GNUNET_assert (table[id]->rc > 0); |
203 | GNUNET_assert((delta >= 0) || | 203 | GNUNET_assert ((delta >= 0) || |
204 | (table[id]->rc >= (unsigned int)(-delta))); | 204 | (table[id]->rc >= (unsigned int) (-delta))); |
205 | table[id]->rc += delta; | 205 | table[id]->rc += delta; |
206 | if (0 == table[id]->rc) | 206 | if (0 == table[id]->rc) |
207 | { | 207 | { |
208 | GNUNET_break(GNUNET_OK == | 208 | GNUNET_break (GNUNET_OK == |
209 | GNUNET_CONTAINER_multipeermap_remove(map, | 209 | GNUNET_CONTAINER_multipeermap_remove (map, |
210 | &table[id]->id, | 210 | &table[id]->id, |
211 | table[id])); | 211 | table[id])); |
212 | table[id]->pid = free_list_start; | 212 | table[id]->pid = free_list_start; |
213 | free_list_start = id; | 213 | free_list_start = id; |
214 | } | 214 | } |
215 | } | 215 | } |
216 | 216 | ||
217 | 217 | ||
@@ -222,15 +222,15 @@ GNUNET_PEER_change_rc(GNUNET_PEER_Id id, int delta) | |||
222 | * @param pid where to write the normal peer identity | 222 | * @param pid where to write the normal peer identity |
223 | */ | 223 | */ |
224 | void | 224 | void |
225 | GNUNET_PEER_resolve(GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid) | 225 | GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid) |
226 | { | 226 | { |
227 | if (0 == id) | 227 | if (0 == id) |
228 | { | 228 | { |
229 | memset(pid, 0, sizeof(struct GNUNET_PeerIdentity)); | 229 | memset (pid, 0, sizeof(struct GNUNET_PeerIdentity)); |
230 | return; | 230 | return; |
231 | } | 231 | } |
232 | GNUNET_assert(id < size); | 232 | GNUNET_assert (id < size); |
233 | GNUNET_assert(table[id]->rc > 0); | 233 | GNUNET_assert (table[id]->rc > 0); |
234 | *pid = table[id]->id; | 234 | *pid = table[id]->id; |
235 | } | 235 | } |
236 | 236 | ||
@@ -242,10 +242,10 @@ GNUNET_PEER_resolve(GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid) | |||
242 | * @return pointer to peer identity, valid as long 'id' is valid | 242 | * @return pointer to peer identity, valid as long 'id' is valid |
243 | */ | 243 | */ |
244 | const struct GNUNET_PeerIdentity * | 244 | const struct GNUNET_PeerIdentity * |
245 | GNUNET_PEER_resolve2(GNUNET_PEER_Id id) | 245 | GNUNET_PEER_resolve2 (GNUNET_PEER_Id id) |
246 | { | 246 | { |
247 | GNUNET_assert(id < size); | 247 | GNUNET_assert (id < size); |
248 | GNUNET_assert(table[id]->rc > 0); | 248 | GNUNET_assert (table[id]->rc > 0); |
249 | return &table[id]->id; | 249 | return &table[id]->id; |
250 | } | 250 | } |
251 | 251 | ||