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,
const struct GNUNET_MQ_MessageHandler *handlers)
{
struct GNUNET_CORE_Handle *h;
- unsigned int hcnt;
h = GNUNET_new (struct GNUNET_CORE_Handle);
h->cfg = 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,
struct GNUNET_MQ_Envelope *env);
+/**
+ * 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.
*/
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;
}
+/**
+ * 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 */