changeset 10225:477d05b0a739

mxRealloc: Allocate new memory on NULL argument
author David Grundberg <davidg@cs.umu.se>
date Fri, 29 Jan 2010 13:34:42 -0500
parents f6e0404421f4
children 2884758e265b
files src/ChangeLog src/mex.cc
diffstat 2 files changed, 30 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Fri Jan 29 12:52:32 2010 -0500
+++ b/src/ChangeLog	Fri Jan 29 13:34:42 2010 -0500
@@ -1,3 +1,8 @@
+2010-01-29  David Grundberg  <davidg@cs.umu.se>
+
+	* mex.cc (mex::realloc): Allocate new memory if the argument is
+	NULL.
+
 2010-01-29  Ryan Rusaw  <rrusaw@gmail.com>
 
 	* pt-eval.h, pt-eval.cc (tree_evaluator::do_keyboard):
--- a/src/mex.cc	Fri Jan 29 12:52:32 2010 -0500
+++ b/src/mex.cc	Fri Jan 29 13:34:42 2010 -0500
@@ -2154,27 +2154,35 @@
     return ptr;
   }
 
-  // Reallocate a pointer obtained from malloc or calloc.  We don't
-  // need an "unmarked" version of this.
+  // Reallocate a pointer obtained from malloc or calloc. If the
+  // pointer is NULL, allocate using malloc.  We don't need an
+  // "unmarked" version of this.
   void *realloc (void *ptr, size_t n)
   {
-    void *v = ::realloc (ptr, n);
-
-    std::set<void *>::iterator p = memlist.find (ptr);
-
-    if (v && p != memlist.end ())
+    void *v;
+
+    if (ptr)
       {
-	memlist.erase (p);
-	memlist.insert (v);
+	v = ::realloc (ptr, n);
+	
+	std::set<void *>::iterator p = memlist.find (ptr);
+	
+	if (v && p != memlist.end ())
+	  {
+	    memlist.erase (p);
+	    memlist.insert (v);
+	  }
+	
+	p = global_memlist.find (ptr);
+	
+	if (v && p != global_memlist.end ())
+	  {
+	    global_memlist.erase (p);
+	    global_memlist.insert (v);
+	  }
       }
-
-    p = global_memlist.find (ptr);
-
-    if (v && p != global_memlist.end ())
-      {
-	global_memlist.erase (p);
-	global_memlist.insert (v);
-      }
+    else
+      v = malloc (n);
 
     return v;
   }