changeset 30428:2ac26917bdec stable

mex.cc: allow for tracing malloc/realloc/free calls mex.cc (xmalloc, xrealloc): New wrapper functions. (xmalloc, xrealloc, xfree): Display debugging info if DEBUG is defined.
author John W. Eaton <jwe@octave.org>
date Sat, 04 Dec 2021 08:34:05 -0500
parents 8d303ace1aff
children e38202d3628d
files libinterp/corefcn/mex.cc
diffstat 1 files changed, 40 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/mex.cc	Sat Dec 04 16:36:59 2021 +0100
+++ b/libinterp/corefcn/mex.cc	Sat Dec 04 08:34:05 2021 -0500
@@ -255,10 +255,43 @@
 
 // #define DEBUG 1
 
+#if DEBUG
+#  include <iostream>
+#endif
+
+static void *
+xmalloc (size_t n)
+{
+  void *ptr = std::malloc (n);
+
+#if DEBUG
+  std::cerr << "xmalloc (" << n << ") = " << ptr << std::endl;
+#endif
+
+  return ptr;
+}
+
+static void *
+xrealloc (void *ptr, size_t n)
+{
+  void *newptr = std::realloc (ptr, n);
+
+#if DEBUG
+  std::cerr << "xrealloc (" << ptr << ", " << n << ") = " << newptr
+            << std::endl;
+#endif
+
+  return newptr;
+}
+
 static void
 xfree (void *ptr)
 {
-  ::free (ptr);
+#if DEBUG
+  std::cerr << "xfree (" << ptr << ")" << std::endl;
+#endif
+
+  std::free (ptr);
 }
 
 static mwSize
@@ -3273,7 +3306,7 @@
   // Allocate memory.
   void * malloc_unmarked (std::size_t n)
   {
-    void *ptr = std::malloc (n);
+    void *ptr = xmalloc (n);
 
     if (! ptr)
       {
@@ -3329,7 +3362,7 @@
         auto p_local = m_memlist.find (ptr);
         auto p_global = s_global_memlist.find (ptr);
 
-        v = std::realloc (ptr, n);
+        v = xrealloc (ptr, n);
 
         if (v)
           {
@@ -3539,7 +3572,7 @@
 void *
 mxArray::malloc (std::size_t n)
 {
-  return mex_context ? mex_context->malloc_unmarked (n) : std::malloc (n);
+  return mex_context ? mex_context->malloc_unmarked (n) : xmalloc (n);
 }
 
 void *
@@ -3659,14 +3692,14 @@
 void *
 mxMalloc (std::size_t n)
 {
-  return mex_context ? mex_context->malloc (n) : std::malloc (n);
+  return mex_context ? mex_context->malloc (n) : xmalloc (n);
 }
 
 void *
 mxRealloc (void *ptr, std::size_t size)
 {
-  return mex_context ? mex_context->realloc (ptr, size)
-                     : std::realloc (ptr, size);
+  return (mex_context
+          ? mex_context->realloc (ptr, size) : xrealloc (ptr, size));
 }
 
 void