Mercurial > octave-nkf
diff src/mex.cc @ 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 | f21fdff5c906 |
children | 57a59eae83cc |
line wrap: on
line diff
--- 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; }