aboutsummaryrefslogtreecommitdiff
path: root/src/rps/gnunet-service-rps_view.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/rps/gnunet-service-rps_view.c
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/rps/gnunet-service-rps_view.c')
-rw-r--r--src/rps/gnunet-service-rps_view.c194
1 files changed, 98 insertions, 96 deletions
diff --git a/src/rps/gnunet-service-rps_view.c b/src/rps/gnunet-service-rps_view.c
index bedd2ae6c..5de7c84dc 100644
--- a/src/rps/gnunet-service-rps_view.c
+++ b/src/rps/gnunet-service-rps_view.c
@@ -28,7 +28,8 @@
28#include "gnunet-service-rps_view.h" 28#include "gnunet-service-rps_view.h"
29#include <inttypes.h> 29#include <inttypes.h>
30 30
31struct View { 31struct View
32{
32 /** 33 /**
33 * Array containing the peers 34 * Array containing the peers
34 */ 35 */
@@ -53,15 +54,15 @@ struct View {
53 * @return The newly created view 54 * @return The newly created view
54 */ 55 */
55struct View * 56struct View *
56View_create(uint32_t len) 57View_create (uint32_t len)
57{ 58{
58 struct View *view; 59 struct View *view;
59 60
60 view = GNUNET_new(struct View); 61 view = GNUNET_new (struct View);
61 view->length = len; 62 view->length = len;
62 view->array = GNUNET_new_array(len, struct GNUNET_PeerIdentity); 63 view->array = GNUNET_new_array (len, struct GNUNET_PeerIdentity);
63 view->mpm = 64 view->mpm =
64 GNUNET_CONTAINER_multipeermap_create(len, GNUNET_NO); /* might even be 65 GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO); /* might even be
65 * set to _YES */ 66 * set to _YES */
66 return view; 67 return view;
67} 68}
@@ -76,38 +77,38 @@ View_create(uint32_t len)
76 * @param len the (maximum) length for the view 77 * @param len the (maximum) length for the view
77 */ 78 */
78void 79void
79View_change_len(struct View *view, 80View_change_len (struct View *view,
80 uint32_t len) 81 uint32_t len)
81{ 82{
82 uint32_t i; 83 uint32_t i;
83 uint32_t *index; 84 uint32_t *index;
84 85
85 if (GNUNET_CONTAINER_multipeermap_size(view->mpm) < len) 86 if (GNUNET_CONTAINER_multipeermap_size (view->mpm) < len)
86 { /* Simply shrink */ 87 { /* Simply shrink */
87 /* We might simply clear and free the left over space */ 88 /* We might simply clear and free the left over space */
88 GNUNET_array_grow(view->array, view->length, len); 89 GNUNET_array_grow (view->array, view->length, len);
89 } 90 }
90 else /* We have to remove elements */ 91 else /* We have to remove elements */
92 {
93 /* TODO find a way to preserve indices */
94 for (i = 0; i < len; i++)
95 {
96 index = GNUNET_CONTAINER_multipeermap_get (view->mpm, &view->array[i]);
97 GNUNET_assert (NULL != index);
98 GNUNET_free (index);
99 }
100 GNUNET_array_grow (view->array, view->length, len);
101 GNUNET_CONTAINER_multipeermap_destroy (view->mpm);
102 view->mpm = GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO);
103 for (i = 0; i < len; i++)
91 { 104 {
92 /* TODO find a way to preserve indices */ 105 index = GNUNET_new (uint32_t);
93 for (i = 0; i < len; i++) 106 *index = i;
94 { 107 GNUNET_CONTAINER_multipeermap_put (view->mpm, &view->array[i], index,
95 index = GNUNET_CONTAINER_multipeermap_get(view->mpm, &view->array[i]); 108 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
96 GNUNET_assert(NULL != index);
97 GNUNET_free(index);
98 }
99 GNUNET_array_grow(view->array, view->length, len);
100 GNUNET_CONTAINER_multipeermap_destroy(view->mpm);
101 view->mpm = GNUNET_CONTAINER_multipeermap_create(len, GNUNET_NO);
102 for (i = 0; i < len; i++)
103 {
104 index = GNUNET_new(uint32_t);
105 *index = i;
106 GNUNET_CONTAINER_multipeermap_put(view->mpm, &view->array[i], index,
107 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
108 }
109 } 109 }
110 GNUNET_assert(view->length == len); 110 }
111 GNUNET_assert (view->length == len);
111} 112}
112 113
113 114
@@ -118,7 +119,7 @@ View_change_len(struct View *view,
118 * @return the view in array representation 119 * @return the view in array representation
119 */ 120 */
120const struct GNUNET_PeerIdentity * 121const struct GNUNET_PeerIdentity *
121View_get_as_array(const struct View *view) 122View_get_as_array (const struct View *view)
122{ 123{
123 return view->array; 124 return view->array;
124} 125}
@@ -131,9 +132,9 @@ View_get_as_array(const struct View *view)
131 * @return current number of actually contained peers 132 * @return current number of actually contained peers
132 */ 133 */
133unsigned int 134unsigned int
134View_size(const struct View *view) 135View_size (const struct View *view)
135{ 136{
136 return GNUNET_CONTAINER_multipeermap_size(view->mpm); 137 return GNUNET_CONTAINER_multipeermap_size (view->mpm);
137} 138}
138 139
139 140
@@ -147,25 +148,25 @@ View_size(const struct View *view)
147 * GNUNET_NO if peer was not inserted 148 * GNUNET_NO if peer was not inserted
148 */ 149 */
149int 150int
150View_put(struct View *view, 151View_put (struct View *view,
151 const struct GNUNET_PeerIdentity *peer) 152 const struct GNUNET_PeerIdentity *peer)
152{ 153{
153 uint32_t *index; 154 uint32_t *index;
154 155
155 if ((view->length <= View_size(view)) || /* If array is 'full' */ 156 if ((view->length <= View_size (view)) || /* If array is 'full' */
156 (GNUNET_YES == View_contains_peer(view, peer))) 157 (GNUNET_YES == View_contains_peer (view, peer)))
157 { 158 {
158 return GNUNET_NO; 159 return GNUNET_NO;
159 } 160 }
160 else 161 else
161 { 162 {
162 index = GNUNET_new(uint32_t); 163 index = GNUNET_new (uint32_t);
163 *index = (uint32_t)View_size(view); 164 *index = (uint32_t) View_size (view);
164 view->array[*index] = *peer; 165 view->array[*index] = *peer;
165 GNUNET_CONTAINER_multipeermap_put(view->mpm, peer, index, 166 GNUNET_CONTAINER_multipeermap_put (view->mpm, peer, index,
166 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); 167 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
167 return GNUNET_OK; 168 return GNUNET_OK;
168 } 169 }
169} 170}
170 171
171 172
@@ -179,10 +180,10 @@ View_put(struct View *view,
179 * GNUNET_NO otherwise 180 * GNUNET_NO otherwise
180 */ 181 */
181int 182int
182View_contains_peer(const struct View *view, 183View_contains_peer (const struct View *view,
183 const struct GNUNET_PeerIdentity *peer) 184 const struct GNUNET_PeerIdentity *peer)
184{ 185{
185 return GNUNET_CONTAINER_multipeermap_contains(view->mpm, peer); 186 return GNUNET_CONTAINER_multipeermap_contains (view->mpm, peer);
186} 187}
187 188
188 189
@@ -196,32 +197,32 @@ View_contains_peer(const struct View *view,
196 * GNUNET_NO if view does not contain peer 197 * GNUNET_NO if view does not contain peer
197 */ 198 */
198int 199int
199View_remove_peer(struct View *view, 200View_remove_peer (struct View *view,
200 const struct GNUNET_PeerIdentity *peer) 201 const struct GNUNET_PeerIdentity *peer)
201{ 202{
202 uint32_t *index; 203 uint32_t *index;
203 uint32_t *swap_index; 204 uint32_t *swap_index;
204 uint32_t last_index; 205 uint32_t last_index;
205 206
206 if (GNUNET_NO == View_contains_peer(view, peer)) 207 if (GNUNET_NO == View_contains_peer (view, peer))
207 { 208 {
208 return GNUNET_NO; 209 return GNUNET_NO;
209 } 210 }
210 index = GNUNET_CONTAINER_multipeermap_get(view->mpm, peer); 211 index = GNUNET_CONTAINER_multipeermap_get (view->mpm, peer);
211 GNUNET_assert(NULL != index); 212 GNUNET_assert (NULL != index);
212 last_index = View_size(view) - 1; 213 last_index = View_size (view) - 1;
213 if (*index < last_index) 214 if (*index < last_index)
214 { /* Fill the 'gap' in the array with the last peer */ 215 { /* Fill the 'gap' in the array with the last peer */
215 view->array[*index] = view->array[last_index]; 216 view->array[*index] = view->array[last_index];
216 GNUNET_assert(GNUNET_YES == View_contains_peer(view, 217 GNUNET_assert (GNUNET_YES == View_contains_peer (view,
217 &view->array[last_index])); 218 &view->array[last_index]));
218 swap_index = GNUNET_CONTAINER_multipeermap_get(view->mpm, 219 swap_index = GNUNET_CONTAINER_multipeermap_get (view->mpm,
219 &view->array[last_index]); 220 &view->array[last_index]);
220 GNUNET_assert(NULL != swap_index); 221 GNUNET_assert (NULL != swap_index);
221 *swap_index = *index; 222 *swap_index = *index;
222 GNUNET_free(index); 223 GNUNET_free (index);
223 } 224 }
224 GNUNET_CONTAINER_multipeermap_remove_all(view->mpm, peer); 225 GNUNET_CONTAINER_multipeermap_remove_all (view->mpm, peer);
225 return GNUNET_OK; 226 return GNUNET_OK;
226} 227}
227 228
@@ -236,17 +237,17 @@ View_remove_peer(struct View *view,
236 * NULL if this index is not known 237 * NULL if this index is not known
237 */ 238 */
238const struct GNUNET_PeerIdentity * 239const struct GNUNET_PeerIdentity *
239View_get_peer_by_index(const struct View *view, 240View_get_peer_by_index (const struct View *view,
240 uint32_t index) 241 uint32_t index)
241{ 242{
242 if (index < GNUNET_CONTAINER_multipeermap_size(view->mpm)) 243 if (index < GNUNET_CONTAINER_multipeermap_size (view->mpm))
243 { 244 {
244 return &view->array[index]; 245 return &view->array[index];
245 } 246 }
246 else 247 else
247 { 248 {
248 return NULL; 249 return NULL;
249 } 250 }
250} 251}
251 252
252 253
@@ -256,20 +257,21 @@ View_get_peer_by_index(const struct View *view,
256 * @param view The view to clear 257 * @param view The view to clear
257 */ 258 */
258void 259void
259View_clear(struct View *view) 260View_clear (struct View *view)
260{ 261{
261 for (uint32_t i = 0; 0 < View_size(view); i++) 262 for (uint32_t i = 0; 0 < View_size (view); i++)
262 { /* Need to free indices stored at peers */ 263 { /* Need to free indices stored at peers */
263 uint32_t *index; 264 uint32_t *index;
264 265
265 GNUNET_assert(GNUNET_YES == 266 GNUNET_assert (GNUNET_YES ==
266 GNUNET_CONTAINER_multipeermap_contains(view->mpm, &view->array[i])); 267 GNUNET_CONTAINER_multipeermap_contains (view->mpm,
267 index = GNUNET_CONTAINER_multipeermap_get(view->mpm, &view->array[i]); 268 &view->array[i]));
268 GNUNET_assert(NULL != index); 269 index = GNUNET_CONTAINER_multipeermap_get (view->mpm, &view->array[i]);
269 GNUNET_free(index); 270 GNUNET_assert (NULL != index);
270 GNUNET_CONTAINER_multipeermap_remove_all(view->mpm, &view->array[i]); 271 GNUNET_free (index);
271 } 272 GNUNET_CONTAINER_multipeermap_remove_all (view->mpm, &view->array[i]);
272 GNUNET_assert(0 == View_size(view)); 273 }
274 GNUNET_assert (0 == View_size (view));
273} 275}
274 276
275 277
@@ -279,13 +281,13 @@ View_clear(struct View *view)
279 * @param view the view to destroy 281 * @param view the view to destroy
280 */ 282 */
281void 283void
282View_destroy(struct View *view) 284View_destroy (struct View *view)
283{ 285{
284 View_clear(view); 286 View_clear (view);
285 GNUNET_free(view->array); 287 GNUNET_free (view->array);
286 view->array = NULL; 288 view->array = NULL;
287 GNUNET_CONTAINER_multipeermap_destroy(view->mpm); 289 GNUNET_CONTAINER_multipeermap_destroy (view->mpm);
288 GNUNET_free(view); 290 GNUNET_free (view);
289} 291}
290 292
291/* end of gnunet-service-rps_view.c */ 293/* end of gnunet-service-rps_view.c */