changeset 22459:6af82fc041a4

Implement Matlab-compatible MEX functions mxAssert and mxAssertS. * mexproto.h: Define macros mxAssert and mxAssertS which call mxErrMsgIdAndTxt. Macros are protected by #if defined (MEX_DEBUG) so that there is a performance penalty only for debug versions of code (-g). * mkoctfile.in.cc: When compiling a mex file with -g debug option, define MEX_DEBUG so that mxAssert and mxAssertS are available.
author Rik <rik@octave.org>
date Fri, 09 Sep 2016 19:40:22 -0700
parents 30002d6568c4
children b0caa61934fe
files libinterp/corefcn/mexproto.h src/mkoctfile.in.cc
diffstat 2 files changed, 30 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/mexproto.h	Fri Sep 09 16:04:01 2016 -0700
+++ b/libinterp/corefcn/mexproto.h	Fri Sep 09 19:40:22 2016 -0700
@@ -282,14 +282,26 @@
 extern OCTINTERP_API char *mxArrayToString (const mxArray *ptr);
 
 /* Miscellaneous.  */
-#if defined (NDEBUG)
+extern OCTINTERP_API mwIndex
+mxCalcSingleSubscript (const mxArray *ptr, mwSize nsubs, mwIndex *subs);
+
+extern OCTINTERP_API size_t mxGetElementSize (const mxArray *ptr);
+
+#if defined (MEX_DEBUG)
+
 #  define mxAssert(expr, msg)                                           \
   do                                                                    \
     {                                                                   \
       if (! expr)                                                       \
         {                                                               \
-          mexPrintf ("Assertion failed: %s, at line %d of file \"%s\".\n%s\n", \
-                     #expr, __LINE__, __FILE__, msg);                   \
+          if (msg != NULL && msg[0] != '\0')                            \
+            mexErrMsgIdAndTxt ("Octave:MEX",                            \
+                               "Assertion failed: %s, at line %d of file \"%s\".\n%s\n", \
+                               #expr, __LINE__, __FILE__, msg);         \
+          else                                                          \
+            mexErrMsgIdAndTxt ("Octave:MEX",                            \
+                               "Assertion failed: %s, at line %d of file \"%s\".\n", \
+                               #expr, __LINE__, __FILE__);              \
         }                                                               \
     }                                                                   \
   while (0)
@@ -299,22 +311,23 @@
     {                                                                   \
       if (! expr)                                                       \
         {                                                               \
-          mexPrintf ("Assertion failed at line %d of file \"%s\".\n%s\n", \
-                     __LINE__, __FILE__, msg);                          \
-          abort ();                                                     \
+          if (msg != NULL && msg[0] != '\0')                            \
+            mexErrMsgIdAndTxt ("Octave:MEX",                            \
+                               "Assertion failed at line %d of file \"%s\".\n%s\n", \
+                               __LINE__, __FILE__, msg);                \
+          else                                                          \
+            mexErrMsgIdAndTxt ("Octave:MEX",                            \
+                               "Assertion failed at line %d of file \"%s\".\n", \
+                               __LINE__, __FILE__);                     \
         }                                                               \
     }                                                                   \
   while (0)
+
 #else
 #  define mxAssert(expr, msg)
 #  define mxAssertS(expr, msg)
 #endif
 
-extern OCTINTERP_API mwIndex
-mxCalcSingleSubscript (const mxArray *ptr, mwSize nsubs, mwIndex *subs);
-
-extern OCTINTERP_API size_t mxGetElementSize (const mxArray *ptr);
-
 #if defined (__cplusplus)
 }
 #endif
--- a/src/mkoctfile.in.cc	Fri Sep 09 16:04:01 2016 -0700
+++ b/src/mkoctfile.in.cc	Fri Sep 09 19:40:22 2016 -0700
@@ -583,6 +583,12 @@
         octfile = file;
     }
 
+  if (output_ext ==  ".mex"
+      && vars["ALL_CFLAGS"].find ("-g") != std::string::npos) 
+    {
+      defs += " -DMEX_DEBUG";
+    }
+
   if (link_stand_alone)
     {
       if (! outputfile.empty ())