[3/3] ralloc: Add ralloc_calc_size to measure memory held by memory contexts

Submitted by Danylo Piliaiev on July 10, 2018, 8:51 a.m.

Details

Message ID 20180710085211.13506-4-danylo.piliaiev@globallogic.com
State New
Headers show
Series "Sweep NIR after linking phase to free held memory" ( rev: 2 1 ) in Mesa

Not browsing as part of any series.

Commit Message

Danylo Piliaiev July 10, 2018, 8:51 a.m.
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@globallogic.com>
---
 src/util/ralloc.c | 30 ++++++++++++++++++++++++++++++
 src/util/ralloc.h |  5 +++++
 2 files changed, 35 insertions(+)

Patch hide | download patch | download mbox

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.
  *