[Concept,03/33] malloc: Report mcheck-registry entry count

Message ID 20260416023021.626949-4-sjg@u-boot.org
State New
Headers
Series Fix memory leaks and test pollution in sandbox tests |

Commit Message

Simon Glass April 16, 2026, 2:29 a.m. UTC
  From: Simon Glass <sjg@chromium.org>

When tracking down heap-registry overflows it is useful to see how
many entries are currently tracked, so the limit can be compared at
a glance.

Expose malloc_mcheck_count() as a public accessor over the existing
mcheck_chunk_count variable, and print its value with the 'malloc info'
command when CONFIG_MCHECK_HEAP_PROTECTION is enabled.

Extend cmd_test_malloc_info() to check the extra line when the feature
is on.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 cmd/malloc.c      | 2 ++
 common/dlmalloc.c | 5 +++++
 include/malloc.h  | 2 ++
 test/cmd/malloc.c | 2 ++
 4 files changed, 11 insertions(+)
  

Patch

diff --git a/cmd/malloc.c b/cmd/malloc.c
index 6b019f4c056..7de421c0ae8 100644
--- a/cmd/malloc.c
+++ b/cmd/malloc.c
@@ -27,6 +27,8 @@  static int do_malloc_info(struct cmd_tbl *cmdtp, int flag, int argc,
 	printf("malloc count  = %lu\n", info.malloc_count);
 	printf("free count    = %lu\n", info.free_count);
 	printf("realloc count = %lu\n", info.realloc_count);
+	if (IS_ENABLED(CONFIG_MCHECK_HEAP_PROTECTION))
+		printf("mcheck count  = %zu\n", malloc_mcheck_count());
 
 	return 0;
 }
diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index ae1e6d27d08..895aa215228 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -6015,6 +6015,11 @@  bool malloc_mcheck_overflow(void)
 	return mcheck_registry_full;
 }
 
+size_t malloc_mcheck_count(void)
+{
+	return mcheck_chunk_count;
+}
+
 static const char *mcheck_caller(void)
 {
 #if CONFIG_IS_ENABLED(MCHECK_BACKTRACE)
diff --git a/include/malloc.h b/include/malloc.h
index 0a780fc03c3..787ec999f5e 100644
--- a/include/malloc.h
+++ b/include/malloc.h
@@ -875,8 +875,10 @@  static inline bool malloc_backtrace_is_active(bool *skipp, bool *busyp)
  */
 #if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION)
 bool malloc_mcheck_overflow(void);
+size_t malloc_mcheck_count(void);
 #else
 static inline bool malloc_mcheck_overflow(void) { return false; }
+static inline size_t malloc_mcheck_count(void) { return 0; }
 #endif
 
 /**
diff --git a/test/cmd/malloc.c b/test/cmd/malloc.c
index db4350afe68..812f7530b79 100644
--- a/test/cmd/malloc.c
+++ b/test/cmd/malloc.c
@@ -28,6 +28,8 @@  static int cmd_test_malloc_info(struct unit_test_state *uts)
 	ut_assert_nextlinen("malloc count  = ");
 	ut_assert_nextlinen("free count    = ");
 	ut_assert_nextlinen("realloc count = ");
+	if (IS_ENABLED(CONFIG_MCHECK_HEAP_PROTECTION))
+		ut_assert_nextlinen("mcheck count  = ");
 	ut_assert_console_end();
 
 	return 0;