[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] [gnunet] 01/03: Refactor copying of handler arrays
From: |
gnunet |
Subject: |
[GNUnet-SVN] [gnunet] 01/03: Refactor copying of handler arrays |
Date: |
Mon, 20 Feb 2017 19:22:19 +0100 |
This is an automated email from the git hooks/post-receive script.
bart-polot pushed a commit to branch master
in repository gnunet.
commit ba5817a7dbaef67b871606431d9a9a7f82d5bdf8
Author: Bart Polot <address@hidden>
AuthorDate: Mon Feb 20 19:13:59 2017 +0100
Refactor copying of handler arrays
---
src/cadet/cadet_api_new.c | 12 +--------
src/core/core_api.c | 16 +++---------
src/include/gnunet_mq_lib.h | 24 ++++++++++++++++++
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,
p->cls = connects_cls;
p->window_changes = window_changes;
p->disconnects = disconnects;
- if (NULL != handlers)
- {
- unsigned int i;
-
- for (i=0;NULL != handlers[i].cb; i++) ;
- p->handlers = GNUNET_new_array (i + 1,
- struct GNUNET_MQ_MessageHandler);
- GNUNET_memcpy ((struct GNUNET_MQ_MessageHandler *) p->handlers,
- handlers,
- i * sizeof (struct GNUNET_MQ_MessageHandler));
- }
+ p->handlers = GNUNET_MQ_copy_handlers (handlers);
GNUNET_assert (GNUNET_OK ==
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,
const struct GNUNET_MQ_MessageHandler *handlers)
{
struct GNUNET_CORE_Handle *h;
- unsigned int hcnt;
h = GNUNET_new (struct GNUNET_CORE_Handle);
h->cfg = cfg;
@@ -794,18 +793,9 @@ GNUNET_CORE_connect (const struct
GNUNET_CONFIGURATION_Handle *cfg,
h->disconnects = disconnects;
h->peers = GNUNET_CONTAINER_multipeermap_create (128,
GNUNET_NO);
- hcnt = 0;
- if (NULL != handlers)
- while (NULL != handlers[hcnt].cb)
- hcnt++;
- h->handlers = GNUNET_new_array (hcnt + 1,
- struct GNUNET_MQ_MessageHandler);
- if (NULL != handlers)
- GNUNET_memcpy (h->handlers,
- handlers,
- hcnt * sizeof (struct GNUNET_MQ_MessageHandler));
- h->hcnt = hcnt;
- GNUNET_assert (hcnt <
+ h->handlers = GNUNET_MQ_copy_handlers (handlers);
+ h->hcnt = GNUNET_MQ_count_handlers (handlers);
+ GNUNET_assert (h->hcnt <
(GNUNET_SERVER_MAX_MESSAGE_SIZE -
sizeof (struct InitMessage)) / sizeof (uint16_t));
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,
/**
+ * Copy an array of handlers.
+ *
+ * Useful if the array has been delared in local memory and needs to be
+ * persisted for future use.
+ *
+ * @param handlers Array of handlers to be copied.
+ * @return A newly allocated array of handlers.
+ * Needs to be freed with #GNUNET_free.
+ */
+struct GNUNET_MQ_MessageHandler *
+GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers);
+
+
+/**
+ * Count the handlers in a handler array.
+ *
+ * @param handlers Array of handlers to be counted.
+ * @return The number of handlers in the array.
+ */
+unsigned int
+GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers);
+
+
+/**
* Message handler for a specific message type.
*/
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,
void *error_handler_cls)
{
struct GNUNET_MQ_Handle *mq;
- unsigned int i;
mq = GNUNET_new (struct GNUNET_MQ_Handle);
mq->send_impl = send;
mq->destroy_impl = destroy;
mq->cancel_impl = cancel;
- if (NULL != handlers)
- {
- for (i=0;NULL != handlers[i].cb; i++) ;
- mq->handlers = GNUNET_new_array (i + 1,
- struct GNUNET_MQ_MessageHandler);
- GNUNET_memcpy (mq->handlers,
- handlers,
- i * sizeof (struct GNUNET_MQ_MessageHandler));
- }
+ mq->handlers = GNUNET_MQ_copy_handlers (handlers);
mq->error_handler = error_handler;
mq->error_handler_cls = error_handler_cls;
mq->impl_state = impl_state;
@@ -1184,4 +1175,54 @@ GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope
**env_head,
}
+/**
+ * Copy an array of handlers.
+ *
+ * Useful if the array has been delared in local memory and needs to be
+ * persisted for future use.
+ *
+ * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
+ * @return A newly allocated array of handlers.
+ * Needs to be freed with #GNUNET_free.
+ */
+struct GNUNET_MQ_MessageHandler *
+GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
+{
+ struct GNUNET_MQ_MessageHandler *copy;
+ unsigned int count;
+
+ if (NULL == handlers)
+ return NULL;
+
+ count = GNUNET_MQ_count_handlers (handlers);
+ copy = GNUNET_new_array (count + 1,
+ struct GNUNET_MQ_MessageHandler);
+ GNUNET_memcpy (copy,
+ handlers,
+ count * sizeof (struct GNUNET_MQ_MessageHandler));
+ return copy;
+}
+
+
+/**
+ * Count the handlers in a handler array.
+ *
+ * @param handlers Array of handlers to be counted.
+ * @return The number of handlers in the array.
+ */
+unsigned int
+GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
+{
+ unsigned int i;
+
+ if (NULL == handlers)
+ return 0;
+
+ for (i=0; NULL != handlers[i].cb; i++) ;
+
+ return i;
+}
+
+
+
/* end of mq.c */
--
To stop receiving notification emails like this one, please contact
address@hidden