summaryrefslogtreecommitdiffstats
path: root/src/msgb.c
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2019-03-18 18:38:47 +0100
committerHarald Welte <laforge@gnumonks.org>2019-04-10 22:42:32 +0000
commit179f35702ece13f4ab7fd1b331bef664834d8473 (patch)
tree9dad4da8eb773040ea8a2dc096190118f5d2abd6 /src/msgb.c
parentd08e9866a5c3da6edda574bbe6d277047d10fded (diff)
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
Diffstat (limited to 'src/msgb.c')
-rw-r--r--src/msgb.c57
1 files changed, 50 insertions, 7 deletions
diff --git a/src/msgb.c b/src/msgb.c
index 47b413b9..5a154e56 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 @@ void *tall_msgb_ctx = NULL;
* 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 @@ struct msgb *msgb_alloc(uint16_t size, const char *name)
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 @@ void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size)
* \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 @@ struct msgb *msgb_copy(const struct msgb *msg, const char *name)
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 @@ const char *msgb_hexdump(const struct msgb *msg)
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.