summaryrefslogtreecommitdiffstats
path: root/src/gsm/gsm0808_utils.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/gsm/gsm0808_utils.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/gsm/gsm0808_utils.c')
-rw-r--r--src/gsm/gsm0808_utils.c40
1 files changed, 36 insertions, 4 deletions
diff --git a/src/gsm/gsm0808_utils.c b/src/gsm/gsm0808_utils.c
index 52e46743..99cf1881 100644
--- a/src/gsm/gsm0808_utils.c
+++ b/src/gsm/gsm0808_utils.c
@@ -622,6 +622,14 @@ char *osmo_lcls_dump(const struct osmo_lcls *lcls)
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 @@ const struct value_string gsm0808_cell_id_discr_names[] = {
#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 @@ static const char *gsm0808_cell_id_name_buf(const struct gsm0808_cell_id *cid,
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_name(const struct gsm0808_cell_id *cid)
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 @@ const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
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 @@ const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
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);
+}
+
/*! @} */