From bcc8241833d90544199abfa1a6a90565f6040015 Mon Sep 17 00:00:00 2001 From: Alexander Couzens Date: Sat, 11 Jul 2026 23:35:06 +0200 Subject: [PATCH] firmware: Add talloc_report_buf() encode the allocation by bits First byte encodes the amount of blocks. The following bytes encode a block as a single bit. Change-Id: I3a8ddbe4100f342db2c59ad1fb9aa0014efc62b8 --- firmware/libcommon/include/talloc.h | 3 +++ firmware/libcommon/source/pseudo_talloc.c | 24 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/firmware/libcommon/include/talloc.h b/firmware/libcommon/include/talloc.h index 1e956038..8fa8cc8c 100644 --- a/firmware/libcommon/include/talloc.h +++ b/firmware/libcommon/include/talloc.h @@ -16,6 +16,8 @@ #include #include +#include + /* minimalistic emulation of core talloc API functions used by msgb.c */ #define __TALLOC_STRING_LINE1__(s) #s @@ -37,3 +39,4 @@ void talloc_set_name_const(const void *ptr, const char *name); char *talloc_strdup(const void *t, const char *p); void *talloc_pool(const void *context, size_t size); void talloc_report(const void *ptr, FILE *f); +unsigned int talloc_report_buf(uint8_t *buf, unsigned int buf_len); diff --git a/firmware/libcommon/source/pseudo_talloc.c b/firmware/libcommon/source/pseudo_talloc.c index 5a4d1caa..9a7fc93f 100644 --- a/firmware/libcommon/source/pseudo_talloc.c +++ b/firmware/libcommon/source/pseudo_talloc.c @@ -77,6 +77,30 @@ int _talloc_free(void *ptr, const char *location) return -1; } +/* Report the talloc usages and return of used bytes */ +unsigned int talloc_report_buf(uint8_t *buf, unsigned int buf_len) +{ + unsigned int i, bitoff = 0, bitpos; + + osmo_static_assert(ARRAY_SIZE(msgb_inuse) < 256, msgb_inuse_too_ig); + + memset(buf, 0, buf_len); + buf[bitoff] = ARRAY_SIZE(msgb_inuse); + bitoff++; + + for (i = 0, bitpos = 7; i < ARRAY_SIZE(msgb_inuse) && bitoff < buf_len; i++, bitpos--) { + if (msgb_inuse[i]) + buf[bitoff] |= (1 << bitpos); + + if (bitpos == 0) { + bitpos = 7; + bitoff++; + } + } + + return bitoff + 1; +} + void talloc_report(const void *ptr, FILE *f) { unsigned int i;