aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2020-09-15 00:48:36 +0000
committerlaforge <laforge@osmocom.org>2020-09-20 09:51:32 +0000
commit9277b97d0b2cf96ea422dc3b7fd0c2027d242890 (patch)
tree3df8c66c6c51370ea776ecd32c8173939d7b63f9
parentb40cfdc54ba6a64a87b48aba8d3b6e39dac4e329 (diff)
add osmo_use_count_to_str_c()
So far there is only osmo_use_count_name_buf(). Also provide a use count to string using a talloc context, allowing to use OTC_SELECT. - instead of foo_name(), rather use foo_to_str(). - osmo_use_count_name_buf() returns the buf and not the chars_needed. So add osmo_use_count_to_str_buf() with a signature that is usable by OSMO_NAME_C_IMPL(). - provide osmo_use_count_to_str_c() using OSMO_NAME_C_IMPL(). Change-Id: I1d2e7ee979f8c316ef99f7c65675b36d092ddfca
-rw-r--r--include/osmocom/core/use_count.h2
-rw-r--r--src/use_count.c26
2 files changed, 27 insertions, 1 deletions
diff --git a/include/osmocom/core/use_count.h b/include/osmocom/core/use_count.h
index a0195259..cc5493c7 100644
--- a/include/osmocom/core/use_count.h
+++ b/include/osmocom/core/use_count.h
@@ -216,6 +216,8 @@ int _osmo_use_count_get_put(struct osmo_use_count *uc, const char *use, int32_t
const char *file, int line);
const char *osmo_use_count_name_buf(char *buf, size_t buf_len, const struct osmo_use_count *uc);
+int osmo_use_count_to_str_buf(char *buf, size_t buf_len, const struct osmo_use_count *uc);
+char *osmo_use_count_to_str_c(void *ctx, const struct osmo_use_count *uc);
int32_t osmo_use_count_total(const struct osmo_use_count *uc);
int32_t osmo_use_count_by(const struct osmo_use_count *uc, const char *use);
diff --git a/src/use_count.c b/src/use_count.c
index 453d2ae8..738cc5da 100644
--- a/src/use_count.c
+++ b/src/use_count.c
@@ -107,6 +107,19 @@ int32_t osmo_use_count_by(const struct osmo_use_count *uc, const char *use)
*/
const char *osmo_use_count_name_buf(char *buf, size_t buf_len, const struct osmo_use_count *uc)
{
+ osmo_use_count_to_str_buf(buf, buf_len, uc);
+ return buf;
+}
+
+/*! Write a comprehensive listing of use counts to a string buffer.
+ * Reads like "12 (3*barring,fighting,8*kungfoo)".
+ * \param[inout] buf Destination buffer.
+ * \param[in] buf_len sizeof(buf).
+ * \param[in] uc Use counts to print.
+ * \return number of bytes that would be written, like snprintf().
+ */
+int osmo_use_count_to_str_buf(char *buf, size_t buf_len, const struct osmo_use_count *uc)
+{
int32_t count = osmo_use_count_total(uc);
struct osmo_strbuf sb = { .buf = buf, .len = buf_len };
struct osmo_use_count_entry *e;
@@ -128,7 +141,18 @@ const char *osmo_use_count_name_buf(char *buf, size_t buf_len, const struct osmo
if (first)
OSMO_STRBUF_PRINTF(sb, "-");
OSMO_STRBUF_PRINTF(sb, ")");
- return buf;
+ return sb.chars_needed;
+}
+
+/*! Write a comprehensive listing of use counts to a talloc allocated string buffer.
+ * Reads like "12 (3*barring,fighting,8*kungfoo)".
+ * \param[in] ctx talloc pool to allocate from.
+ * \param[in] uc Use counts to print.
+ * \return buf, always nul-terminated.
+ */
+char *osmo_use_count_to_str_c(void *ctx, const struct osmo_use_count *uc)
+{
+ OSMO_NAME_C_IMPL(ctx, 32, "ERROR", osmo_use_count_to_str_buf, uc)
}
/* Return a use token's use count entry -- probably you want osmo_use_count_by() instead.