diff options
author | Bart Polot <bart.polot+gnunet@gmail.com> | 2017-02-20 19:13:59 +0100 |
---|---|---|
committer | Bart Polot <bart.polot+gnunet@gmail.com> | 2017-02-20 19:17:40 +0100 |
commit | ba5817a7dbaef67b871606431d9a9a7f82d5bdf8 (patch) | |
tree | 747bdbac51436264cbf64c59c594dbd356393f9e /src | |
parent | 6b6a966864221ffa0d52d311da3b68f6c42549be (diff) | |
download | gnunet-ba5817a7dbaef67b871606431d9a9a7f82d5bdf8.tar.gz gnunet-ba5817a7dbaef67b871606431d9a9a7f82d5bdf8.zip |
Refactor copying of handler arrays
Diffstat (limited to 'src')
-rw-r--r-- | src/cadet/cadet_api_new.c | 12 | ||||
-rw-r--r-- | src/core/core_api.c | 16 | ||||
-rw-r--r-- | src/include/gnunet_mq_lib.h | 24 | ||||
-rw-r--r-- | src/util/mq.c | 61 |
4 files changed, 79 insertions, 34 deletions
diff --git a/src/cadet/cadet_api_new.c b/src/cadet/cadet_api_new.c index 673764813..7ed681a9d 100644 --- a/src/cadet/cadet_api_new.c +++ b/src/cadet/cadet_api_new.c | |||
@@ -1635,17 +1635,7 @@ GNUNET_CADET_open_porT (struct GNUNET_CADET_Handle *h, | |||
1635 | p->cls = connects_cls; | 1635 | p->cls = connects_cls; |
1636 | p->window_changes = window_changes; | 1636 | p->window_changes = window_changes; |
1637 | p->disconnects = disconnects; | 1637 | p->disconnects = disconnects; |
1638 | if (NULL != handlers) | 1638 | p->handlers = GNUNET_MQ_copy_handlers (handlers); |
1639 | { | ||
1640 | unsigned int i; | ||
1641 | |||
1642 | for (i=0;NULL != handlers[i].cb; i++) ; | ||
1643 | p->handlers = GNUNET_new_array (i + 1, | ||
1644 | struct GNUNET_MQ_MessageHandler); | ||
1645 | GNUNET_memcpy ((struct GNUNET_MQ_MessageHandler *) p->handlers, | ||
1646 | handlers, | ||
1647 | i * sizeof (struct GNUNET_MQ_MessageHandler)); | ||
1648 | } | ||
1649 | 1639 | ||
1650 | GNUNET_assert (GNUNET_OK == | 1640 | GNUNET_assert (GNUNET_OK == |
1651 | GNUNET_CONTAINER_multihashmap_put (h->ports, | 1641 | GNUNET_CONTAINER_multihashmap_put (h->ports, |
diff --git a/src/core/core_api.c b/src/core/core_api.c index afae20850..c1cfdb62f 100644 --- a/src/core/core_api.c +++ b/src/core/core_api.c | |||
@@ -784,7 +784,6 @@ GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
784 | const struct GNUNET_MQ_MessageHandler *handlers) | 784 | const struct GNUNET_MQ_MessageHandler *handlers) |
785 | { | 785 | { |
786 | struct GNUNET_CORE_Handle *h; | 786 | struct GNUNET_CORE_Handle *h; |
787 | unsigned int hcnt; | ||
788 | 787 | ||
789 | h = GNUNET_new (struct GNUNET_CORE_Handle); | 788 | h = GNUNET_new (struct GNUNET_CORE_Handle); |
790 | h->cfg = cfg; | 789 | h->cfg = cfg; |
@@ -794,18 +793,9 @@ GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
794 | h->disconnects = disconnects; | 793 | h->disconnects = disconnects; |
795 | h->peers = GNUNET_CONTAINER_multipeermap_create (128, | 794 | h->peers = GNUNET_CONTAINER_multipeermap_create (128, |
796 | GNUNET_NO); | 795 | GNUNET_NO); |
797 | hcnt = 0; | 796 | h->handlers = GNUNET_MQ_copy_handlers (handlers); |
798 | if (NULL != handlers) | 797 | h->hcnt = GNUNET_MQ_count_handlers (handlers); |
799 | while (NULL != handlers[hcnt].cb) | 798 | GNUNET_assert (h->hcnt < |
800 | hcnt++; | ||
801 | h->handlers = GNUNET_new_array (hcnt + 1, | ||
802 | struct GNUNET_MQ_MessageHandler); | ||
803 | if (NULL != handlers) | ||
804 | GNUNET_memcpy (h->handlers, | ||
805 | handlers, | ||
806 | hcnt * sizeof (struct GNUNET_MQ_MessageHandler)); | ||
807 | h->hcnt = hcnt; | ||
808 | GNUNET_assert (hcnt < | ||
809 | (GNUNET_SERVER_MAX_MESSAGE_SIZE - | 799 | (GNUNET_SERVER_MAX_MESSAGE_SIZE - |
810 | sizeof (struct InitMessage)) / sizeof (uint16_t)); | 800 | sizeof (struct InitMessage)) / sizeof (uint16_t)); |
811 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 801 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
diff --git a/src/include/gnunet_mq_lib.h b/src/include/gnunet_mq_lib.h index b527b58e8..a50a59c49 100644 --- a/src/include/gnunet_mq_lib.h +++ b/src/include/gnunet_mq_lib.h | |||
@@ -305,6 +305,30 @@ GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head, | |||
305 | 305 | ||
306 | 306 | ||
307 | /** | 307 | /** |
308 | * Copy an array of handlers. | ||
309 | * | ||
310 | * Useful if the array has been delared in local memory and needs to be | ||
311 | * persisted for future use. | ||
312 | * | ||
313 | * @param handlers Array of handlers to be copied. | ||
314 | * @return A newly allocated array of handlers. | ||
315 | * Needs to be freed with #GNUNET_free. | ||
316 | */ | ||
317 | struct GNUNET_MQ_MessageHandler * | ||
318 | GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers); | ||
319 | |||
320 | |||
321 | /** | ||
322 | * Count the handlers in a handler array. | ||
323 | * | ||
324 | * @param handlers Array of handlers to be counted. | ||
325 | * @return The number of handlers in the array. | ||
326 | */ | ||
327 | unsigned int | ||
328 | GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers); | ||
329 | |||
330 | |||
331 | /** | ||
308 | * Message handler for a specific message type. | 332 | * Message handler for a specific message type. |
309 | */ | 333 | */ |
310 | struct GNUNET_MQ_MessageHandler | 334 | struct GNUNET_MQ_MessageHandler |
diff --git a/src/util/mq.c b/src/util/mq.c index 43926ed64..fe47f6ab4 100644 --- a/src/util/mq.c +++ b/src/util/mq.c | |||
@@ -515,21 +515,12 @@ GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send, | |||
515 | void *error_handler_cls) | 515 | void *error_handler_cls) |
516 | { | 516 | { |
517 | struct GNUNET_MQ_Handle *mq; | 517 | struct GNUNET_MQ_Handle *mq; |
518 | unsigned int i; | ||
519 | 518 | ||
520 | mq = GNUNET_new (struct GNUNET_MQ_Handle); | 519 | mq = GNUNET_new (struct GNUNET_MQ_Handle); |
521 | mq->send_impl = send; | 520 | mq->send_impl = send; |
522 | mq->destroy_impl = destroy; | 521 | mq->destroy_impl = destroy; |
523 | mq->cancel_impl = cancel; | 522 | mq->cancel_impl = cancel; |
524 | if (NULL != handlers) | 523 | mq->handlers = GNUNET_MQ_copy_handlers (handlers); |
525 | { | ||
526 | for (i=0;NULL != handlers[i].cb; i++) ; | ||
527 | mq->handlers = GNUNET_new_array (i + 1, | ||
528 | struct GNUNET_MQ_MessageHandler); | ||
529 | GNUNET_memcpy (mq->handlers, | ||
530 | handlers, | ||
531 | i * sizeof (struct GNUNET_MQ_MessageHandler)); | ||
532 | } | ||
533 | mq->error_handler = error_handler; | 524 | mq->error_handler = error_handler; |
534 | mq->error_handler_cls = error_handler_cls; | 525 | mq->error_handler_cls = error_handler_cls; |
535 | mq->impl_state = impl_state; | 526 | mq->impl_state = impl_state; |
@@ -1184,4 +1175,54 @@ GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head, | |||
1184 | } | 1175 | } |
1185 | 1176 | ||
1186 | 1177 | ||
1178 | /** | ||
1179 | * Copy an array of handlers. | ||
1180 | * | ||
1181 | * Useful if the array has been delared in local memory and needs to be | ||
1182 | * persisted for future use. | ||
1183 | * | ||
1184 | * @param handlers Array of handlers to be copied. Can be NULL (nothing done). | ||
1185 | * @return A newly allocated array of handlers. | ||
1186 | * Needs to be freed with #GNUNET_free. | ||
1187 | */ | ||
1188 | struct GNUNET_MQ_MessageHandler * | ||
1189 | GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers) | ||
1190 | { | ||
1191 | struct GNUNET_MQ_MessageHandler *copy; | ||
1192 | unsigned int count; | ||
1193 | |||
1194 | if (NULL == handlers) | ||
1195 | return NULL; | ||
1196 | |||
1197 | count = GNUNET_MQ_count_handlers (handlers); | ||
1198 | copy = GNUNET_new_array (count + 1, | ||
1199 | struct GNUNET_MQ_MessageHandler); | ||
1200 | GNUNET_memcpy (copy, | ||
1201 | handlers, | ||
1202 | count * sizeof (struct GNUNET_MQ_MessageHandler)); | ||
1203 | return copy; | ||
1204 | } | ||
1205 | |||
1206 | |||
1207 | /** | ||
1208 | * Count the handlers in a handler array. | ||
1209 | * | ||
1210 | * @param handlers Array of handlers to be counted. | ||
1211 | * @return The number of handlers in the array. | ||
1212 | */ | ||
1213 | unsigned int | ||
1214 | GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers) | ||
1215 | { | ||
1216 | unsigned int i; | ||
1217 | |||
1218 | if (NULL == handlers) | ||
1219 | return 0; | ||
1220 | |||
1221 | for (i=0; NULL != handlers[i].cb; i++) ; | ||
1222 | |||
1223 | return i; | ||
1224 | } | ||
1225 | |||
1226 | |||
1227 | |||
1187 | /* end of mq.c */ | 1228 | /* end of mq.c */ |