[Mesa-dev] [PATCH 3/3] ralloc: Add ralloc_calc_size to measure memory held by memory contexts

Danylo Piliaiev danylo.piliaiev at gmail.com
Tue Jul 10 08:51:46 UTC 2018


The function recursively calculates memory held by context's children.
It is aimed to easier debugging of excessive memory usage with ralloc.
It will work only if malloc_usable_size is available.

Signed-off-by: Danylo Piliaiev <danylo.piliaiev at globallogic.com>
---
 src/util/ralloc.c | 30 ++++++++++++++++++++++++++++++
 src/util/ralloc.h |  5 +++++
 2 files changed, 35 insertions(+)

diff --git a/src/util/ralloc.c b/src/util/ralloc.c
index 5d77f75ee8..c8d217f6a3 100644
--- a/src/util/ralloc.c
+++ b/src/util/ralloc.c
@@ -28,6 +28,10 @@
 #include <string.h>
 #include <stdint.h>
 
+#ifdef __GLIBC__
+#include <malloc.h>
+#endif
+
 /* Some versions of MinGW are missing _vscprintf's declaration, although they
  * still provide the symbol in the import library. */
 #ifdef __MINGW32__
@@ -239,6 +243,32 @@ ralloc_free(void *ptr)
    unsafe_free(info);
 }
 
+size_t
+ralloc_calc_size(void *ctx)
+{
+#ifdef __GLIBC__
+   ralloc_header *info;
+
+   if (ctx == NULL)
+      return 0;
+
+   info = get_header(ctx);
+
+   size_t total_size = malloc_usable_size(info);
+
+   /* Recursively calculate children size */
+   ralloc_header *temp = info->child;
+   while (temp != NULL) {
+      total_size += ralloc_calc_size(PTR_FROM_HEADER(temp));
+      temp = temp->next;
+   }
+
+   return total_size;
+#else
+   return 0;
+#endif
+}
+
 static void
 unlink_block(ralloc_header *info)
 {
diff --git a/src/util/ralloc.h b/src/util/ralloc.h
index 05ae8f8407..fecd31bdca 100644
--- a/src/util/ralloc.h
+++ b/src/util/ralloc.h
@@ -219,6 +219,11 @@ void *reralloc_array_size(const void *ctx, void *ptr, size_t size,
 			  unsigned count);
 /// @}
 
+/**
+ * Calculate total memory size held by the context
+ */
+size_t ralloc_calc_size(void *ctx);
+
 /**
  * Free a piece of ralloc-managed memory.
  *
-- 
2.17.1



More information about the mesa-dev mailing list