[PATCH] libosmocore[master]: msgb: add msgb_talloc_ctx_init(), deprecate msgb_set_talloc_...

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Thu Sep 15 23:35:24 UTC 2016


Review at  https://gerrit.osmocom.org/847

msgb: add msgb_talloc_ctx_init(), deprecate msgb_set_talloc_ctx()

So far each and every main() scope creates a msgb talloc context and either
passes it to msgb_set_talloc_ctx() or sets tall_msgb_ctx directly (by defining
it extern first).

Remove some code duplication: add one central function that creates the "msgb"
talloc context for all.

Most users of msgb employ a talloc_named_const(), but osmo-bts uses a
talloc_pool() instead. Offer both ways by means of the pool_size argument, and
for both ways make sure the context is called "msgb".

Suggest that msgb users should move to this new function: deprecate
msgb_set_talloc_ctx(). To be able to do so, include core/defs.h in msgb.h.

There's a tradeoff between hiding the msgb talloc context behind API that tries
to guess all use cases versus avoiding code dup. This patch opts against code
dup and boldly assumes that all future use is covered.

Also, the new function suggests to not access tall_msgb_ctx directly, which can
be considered a style improvement.

It seems that not all main scopes that use msgb actually initialize the msgb
ctx. As a fallback for these, explicitly initialize tall_msgb_ctx to NULL.

Change-Id: I747fbbf977c4d2c868c8dead64cfc5fd86eb8d4c
---
M include/osmocom/core/msgb.h
M src/msgb.c
2 files changed, 24 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/47/847/1

diff --git a/include/osmocom/core/msgb.h b/include/osmocom/core/msgb.h
index 6f617e2..7c85771 100644
--- a/include/osmocom/core/msgb.h
+++ b/include/osmocom/core/msgb.h
@@ -23,6 +23,7 @@
 #include <osmocom/core/linuxlist.h>
 #include <osmocom/core/utils.h>
 #include <osmocom/core/bits.h>
+#include <osmocom/core/defs.h>
 
 /*! \defgroup msgb Message buffers
  *  @{
@@ -457,6 +458,8 @@
 /* non inline functions to ease binding */
 
 uint8_t *msgb_data(const struct msgb *msg);
-void msgb_set_talloc_ctx(void *ctx);
+
+void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size);
+void msgb_set_talloc_ctx(void *ctx) OSMO_DEPRECATED("Use msgb_talloc_ctx_init() instead");
 
 /*! @} */
diff --git a/src/msgb.c b/src/msgb.c
index ea8dc82..a27100c 100644
--- a/src/msgb.c
+++ b/src/msgb.c
@@ -35,7 +35,7 @@
 #include <osmocom/core/talloc.h>
 //#include <openbsc/debug.h>
 
-void *tall_msgb_ctx;
+void *tall_msgb_ctx = NULL;
 
 /*! \brief Allocate a new message buffer
  * \param[in] size Length in octets, including headroom
@@ -151,6 +151,7 @@
 }
 
 /*! \brief Set the talloc context for \ref msgb_alloc
+ * Deprecated, use msgb_talloc_ctx_init() instead.
  *  \param[in] ctx talloc context to be used as root for msgb allocations
  */
 void msgb_set_talloc_ctx(void *ctx)
@@ -158,6 +159,24 @@
 	tall_msgb_ctx = ctx;
 }
 
+/*! \brief Initialize a msgb talloc context for \ref msgb_alloc.
+ * Create a talloc context called "msgb". If \a pool_size is 0, create a named
+ * const as msgb talloc context. If \a pool_size is nonzero, create a talloc
+ * pool, possibly for faster msgb allocations (see talloc_pool()).
+ *  \param[in] root_ctx talloc context used as parent for the new "msgb" ctx.
+ *  \param[in] pool_size if nonzero, create a talloc pool of this size.
+ *  \returns the new msgb talloc context, e.g. for reporting
+ */
+void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size)
+{
+	if (!pool_size)
+		tall_msgb_ctx = talloc_size(root_ctx, 0);
+	else
+		tall_msgb_ctx = talloc_pool(root_ctx, pool_size);
+	talloc_set_name_const(tall_msgb_ctx, "msgb");
+	return tall_msgb_ctx;
+}
+
 /*! \brief Copy an msgb.
  *
  *  This function allocates a new msgb, copies the data buffer of msg,

-- 
To view, visit https://gerrit.osmocom.org/847
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I747fbbf977c4d2c868c8dead64cfc5fd86eb8d4c
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>



More information about the gerrit-log mailing list