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/gsm/gsm0808_utils.c b/src/gsm/gsm0808_utils.c
index 52e4674..99cf188 100644
--- a/src/gsm/gsm0808_utils.c
+++ b/src/gsm/gsm0808_utils.c
@@ -622,6 +622,14 @@
 	return osmo_lcls_dump_buf(dbuf, sizeof(dbuf), lcls);
 }
 
+char *osmo_lcls_dump_c(void *ctx, const struct osmo_lcls *lcls)
+{
+	char *buf = talloc_size(ctx, 256);
+	if (!buf)
+		return NULL;
+	return osmo_lcls_dump_buf(buf, 256, lcls);
+}
+
 /*! Dump GCR struct into string for printing.
  *  \param[out] buf caller-allocated output string buffer
  *  \param[in] buf_len size of buf in bytes
@@ -1775,8 +1783,7 @@
 #define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)
 #define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U)
 
-static const char *gsm0808_cell_id_name_buf(const struct gsm0808_cell_id *cid,
-					    char *buf, size_t buflen)
+char *gsm0808_cell_id_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id *cid)
 {
 	char *pos = buf;
 	int total_len = 0;
@@ -1793,7 +1800,7 @@
 const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
 {
 	static char buf[64];
-	return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf));
+	return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
 }
 
 /*! Like gsm0808_cell_id_name() but uses a different static buffer.
@@ -1803,7 +1810,15 @@
 const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
 {
 	static char buf[64];
-	return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf));
+	return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
+}
+
+char *gsm0808_cell_id_name_c(const void *ctx, const struct gsm0808_cell_id *cid)
+{
+	char *buf = talloc_size(ctx, 64);
+	if (!buf)
+		return NULL;
+	return gsm0808_cell_id_name_buf(buf, 64, cid);
 }
 
 /*! Return a human readable representation of the Cell Identifier List, like
@@ -1856,6 +1871,15 @@
 	return buf;
 }
 
+char *gsm0808_cell_id_list_name_c(const void *ctx, const struct gsm0808_cell_id_list2 *cil)
+{
+	char *buf = talloc_size(ctx, 1024);
+	if (!buf)
+		return NULL;
+	gsm0808_cell_id_list_name_buf(buf, 1024, cil);
+	return buf;
+}
+
 #undef APPEND_STR
 #undef APPEND_CELL_ID_U
 
@@ -1873,4 +1897,12 @@
 	return gsm0808_channel_type_name_buf(buf, sizeof(buf), ct);
 }
 
+char *gsm0808_channel_type_name_c(const void *ctx, const struct gsm0808_channel_type *ct)
+{
+	char *buf = talloc_size(ctx, 128);
+	if (!buf)
+		return NULL;
+	return gsm0808_channel_type_name_buf(buf, 128, ct);
+}
+
 /*! @} */