Add _c versions of functions that otherwise return static buffers

We have a habit of returning static buffers from some functions,
particularly when generating some kind of string values.  This is
convenient in terms of memory management, but it comes at the expense
of not being thread-safe, and not allowing for two calls of the
related function within one printf() statement.

Let's introduce _c suffix versions of those functions where the
caller passes in a talloc context from which the output buffer shall
be allocated.

Change-Id: I8481c19b68ff67cfa22abb93c405ebcfcb0ab19b
diff --git a/src/msgb.c b/src/msgb.c
index 47b413b..5a154e5 100644
--- a/src/msgb.c
+++ b/src/msgb.c
@@ -64,9 +64,8 @@
 #include <osmocom/core/talloc.h>
 #include <osmocom/core/logging.h>
 
-void *tall_msgb_ctx = NULL;
-
-/*! Allocate a new message buffer
+/*! Allocate a new message buffer from given talloc cotext
+ * \param[in] ctx talloc context from which to allocate
  * \param[in] size Length in octets, including headroom
  * \param[in] name Human-readable name to be associated with msgb
  * \returns dynamically-allocated \ref msgb
@@ -75,11 +74,11 @@
  * memory buffer for the actual message data (size specified by \a size)
  * using the talloc memory context previously set by \ref msgb_set_talloc_ctx
  */
-struct msgb *msgb_alloc(uint16_t size, const char *name)
+struct msgb *msgb_alloc_c(const void *ctx, uint16_t size, const char *name)
 {
 	struct msgb *msg;
 
-	msg = talloc_named_const(tall_msgb_ctx, sizeof(*msg) + size, name);
+	msg = talloc_named_const(ctx, sizeof(*msg) + size, name);
 	if (!msg) {
 		LOGP(DLGLOBAL, LOGL_FATAL, "Unable to allocate a msgb: "
 			"name='%s', size=%u\n", name, size);
@@ -98,6 +97,24 @@
 	return msg;
 }
 
+/* default msgb allocation context for msgb_alloc() */
+void *tall_msgb_ctx = NULL;
+
+/*! Allocate a new message buffer from tall_msgb_ctx
+ * \param[in] size Length in octets, including headroom
+ * \param[in] name Human-readable name to be associated with msgb
+ * \returns dynamically-allocated \ref msgb
+ *
+ * This function allocates a 'struct msgb' as well as the underlying
+ * memory buffer for the actual message data (size specified by \a size)
+ * using the talloc memory context previously set by \ref msgb_set_talloc_ctx
+ */
+struct msgb *msgb_alloc(uint16_t size, const char *name)
+{
+	return msgb_alloc_c(tall_msgb_ctx, size, name);
+}
+
+
 /*! Release given message buffer
  * \param[in] m Message buffer to be freed
  */
@@ -309,11 +326,11 @@
  *  \param[in] msg  The old msgb object
  *  \param[in] name Human-readable name to be associated with msgb
  */
-struct msgb *msgb_copy(const struct msgb *msg, const char *name)
+struct msgb *msgb_copy_c(const void *ctx, const struct msgb *msg, const char *name)
 {
 	struct msgb *new_msg;
 
-	new_msg = msgb_alloc(msg->data_len, name);
+	new_msg = msgb_alloc_c(ctx, msg->data_len, name);
 	if (!new_msg)
 		return NULL;
 
@@ -338,6 +355,19 @@
 	return new_msg;
 }
 
+/*! Copy an msgb.
+ *
+ *  This function allocates a new msgb, copies the data buffer of msg,
+ *  and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
+ *  is not copied.
+ *  \param[in] msg  The old msgb object
+ *  \param[in] name Human-readable name to be associated with msgb
+ */
+struct msgb *msgb_copy(const struct msgb *msg, const char *name)
+{
+	return msgb_copy_c(tall_msgb_ctx, msg, name);
+}
+
 /*! Resize an area within an msgb
  *
  *  This resizes a sub area of the msgb data and adjusts the pointers (incl
@@ -485,6 +515,19 @@
 	return msgb_hexdump_buf(buf, sizeof(buf), msg);
 }
 
+/*! Return a dynamically allocated buffer containing a hexdump of the msg
+ * \param[in] ctx talloc context from where to allocate the output string
+ * \param[in] msg message buffer
+ * \returns a pointer to a static char array
+ */
+char *msgb_hexdump_c(const void *ctx, const struct msgb *msg)
+{
+	char *buf = talloc_size(ctx, msgb_length(msg)*3 + 100);
+	if (!buf)
+		return NULL;
+	return msgb_hexdump_buf(buf, sizeof(buf), msg);
+}
+
 /*! Print a string to the end of message buffer.
  * \param[in] msgb message buffer.
  * \param[in] format format string.