[PATCH] libosmocore[master]: add osmo_talloc_asprintf() and ctrl_cmd_reply_printf()

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Thu Oct 19 01:08:59 UTC 2017

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

add osmo_talloc_asprintf() and ctrl_cmd_reply_printf()

Add macro to append to a CTRL commands' reply string, ctrl_cmd_reply_printf().
The talloc_asprintf() part of it is generic enough to qualify for a separate
macro, osmo_talloc_asprintf().

The idea is to not have to decide for each bit added to a string whether the
string is already allocated or not, but simply be able to issue printf commands
and let the macro worry about initial allocation or reallocation.

This originally came from osmo-hlr change
I1bd62ae0d4eefde7e1517db15a2155640a1bab58, where it was requested to move this
bit to libosmocore.

Change-Id: Ic9dba0e4a1eb5a7dc3cee2f181b9024ed4fc7005
M include/osmocom/core/utils.h
M include/osmocom/ctrl/control_cmd.h
2 files changed, 21 insertions(+), 0 deletions(-)

  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/40/4340/1

diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index 5f41213..793f8bb 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -85,6 +85,24 @@
 	*dst = talloc_strdup(ctx, newstr);
+/*! Append to a string and re-/allocate if necessary.
+ * \param[in] ctx  Talloc context to use for initial allocation.
+ * \param[inout] dest  char* to re-/allocate and append to.
+ * \param[in] fmt  printf-like string format.
+ * \param[in] args  Arguments for fmt.
+ *
+ * \a dest may be passed in NULL, or a string previously allocated by talloc.
+ * If an existing string is passed in, it will remain associated with whichever
+ * ctx it was allocated before, regardless whether it matches \a ctx or not.
+ */
+#define osmo_talloc_asprintf(ctx, dest, fmt, args ...) \
+	do { \
+		if (!dest) \
+			dest = talloc_asprintf(ctx, fmt, ## args); \
+		else \
+			dest = talloc_asprintf_append((char*)dest, fmt, ## args); \
+	} while (0)
 int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count);
 uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len);
 uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len);
diff --git a/include/osmocom/ctrl/control_cmd.h b/include/osmocom/ctrl/control_cmd.h
index 77532e6..4372e25 100644
--- a/include/osmocom/ctrl/control_cmd.h
+++ b/include/osmocom/ctrl/control_cmd.h
@@ -66,6 +66,9 @@
 	char *reply;
+#define ctrl_cmd_reply_printf(cmd, fmt, args ...) \
+	osmo_talloc_asprintf(cmd, cmd->reply, fmt, ## args)
 struct ctrl_cmd_struct {
 	int nr_commands;
 	char **command;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic9dba0e4a1eb5a7dc3cee2f181b9024ed4fc7005
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