diff --git a/src/util/ralloc.c b/src/util/ralloc.c index 04bb6cc1d71..659288922f3 100644 --- a/src/util/ralloc.c +++ b/src/util/ralloc.c @@ -264,6 +264,31 @@ ralloc_free(void *ptr) unsafe_free(info); } +#ifndef NDEBUG +static size_t +ralloc_total_size_internal(const ralloc_header *info) +{ + /* Count the block itself. This requires NDEBUG for the statistic. */ + unsigned sum = align64(info->size + sizeof(ralloc_header), + alignof(ralloc_header)); + + /* Recursively count children */ + ralloc_header *it = info->child; + while (it != NULL) { + sum += ralloc_total_size_internal(it); + it = it->next; + } + + return sum; +} + +size_t +ralloc_total_size(const void *ptr) +{ + return ralloc_total_size_internal(get_header(ptr)); +} +#endif + static void unlink_block(ralloc_header *info) { diff --git a/src/util/ralloc.h b/src/util/ralloc.h index f12606381f9..95e932fbab1 100644 --- a/src/util/ralloc.h +++ b/src/util/ralloc.h @@ -483,6 +483,16 @@ bool ralloc_asprintf_append (char **str, const char *fmt, ...) bool ralloc_vasprintf_append(char **str, const char *fmt, va_list args); /// @} +/** + * Estimate the memory usage in bytes of a ralloc context, recursively including + * all of its child counts. This is only available in debug builds as release + * builds do not track size information. It is providing as a aid for debugging + * memory bloat. + */ +#ifndef NDEBUG +size_t ralloc_total_size(const void *ptr); +#endif + typedef struct gc_ctx gc_ctx; /**