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/util/mq.c | |
parent | 6b6a966864221ffa0d52d311da3b68f6c42549be (diff) | |
download | gnunet-ba5817a7dbaef67b871606431d9a9a7f82d5bdf8.tar.gz gnunet-ba5817a7dbaef67b871606431d9a9a7f82d5bdf8.zip |
Refactor copying of handler arrays
Diffstat (limited to 'src/util/mq.c')
-rw-r--r-- | src/util/mq.c | 61 |
1 files changed, 51 insertions, 10 deletions
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 */ |