[PATCH 14/19] test: Add functions to find the amount of allocated memory

Simon Glass sjg at chromium.org
Mon Dec 30 05:19:23 CET 2019


The malloc() implementations provides a way of finding out the approximate
amount of memory that is allocated. Add helper functions to make it easier
to access this and see changes over time. This is useful for tests that
want to check if memory has been allocated or freed.

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

 include/test/ut.h | 16 ++++++++++++++++
 test/ut.c         | 14 ++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/include/test/ut.h b/include/test/ut.h
index fbfde10719..f616c202f3 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -149,4 +149,20 @@ void ut_failf(struct unit_test_state *uts, const char *fname, int line,
 /* Assert that an operation succeeds (returns 0) */
 #define ut_assertok(cond)	ut_asserteq(0, cond)
 
+/**
+ * ut_check_free() - Return the number of bytes free in the malloc() pool
+ *
+ * @return bytes free
+ */
+ulong ut_check_free(void);
+
+/**
+ * ut_check_delta() - Return the number of bytes allocated/freed
+ *
+ * @last: Last value from ut_check_free
+ * @return free memory delta from @last; positive means more memory has been
+ *	allocated, negative means less has been allocated (i.e. some is freed)
+ */
+long ut_check_delta(ulong last);
+
 #endif
diff --git a/test/ut.c b/test/ut.c
index 55798041ba..265da4a0d8 100644
--- a/test/ut.c
+++ b/test/ut.c
@@ -6,6 +6,7 @@
  */
 
 #include <common.h>
+#include <malloc.h>
 #include <test/test.h>
 #include <test/ut.h>
 
@@ -32,3 +33,16 @@ void ut_failf(struct unit_test_state *uts, const char *fname, int line,
 	putc('\n');
 	uts->fail_count++;
 }
+
+ulong ut_check_free(void)
+{
+	struct mallinfo info = mallinfo();
+
+	return info.uordblks;
+}
+
+long ut_check_delta(ulong last)
+{
+	return ut_check_free() - last;
+}
+
-- 
2.24.1.735.g03f4e72817-goog



More information about the U-Boot mailing list