Fix proper support of no stdio

and add macro M_RAISE_FATAL to raise a fatal error
with the default is using stdio.
This commit is contained in:
Patrick Pelissier committed 2022-10-01 09:50:09 +02:00
1 parent bd49e31f3b
commit 1a5b428bfb
1 file changed
+27 -12
+27 -12
View File
@@ -41,10 +41,6 @@
#endif #endif
#if M_USE_STDIO #if M_USE_STDIO
# include <stdio.h> # include <stdio.h>
#else
#if !defined(M_MEMORY_FULL) || !defined(M_ASSERT_INIT)
# error Without stdio.h, definitions for macros M_MEMORY_FULL, M_ASSERT_INIT are mandatory.
#endif
#endif #endif
/* By default, always use stdarg. Can be turned off in specific environment if needed /* By default, always use stdarg. Can be turned off in specific environment if needed
@@ -286,15 +282,13 @@ M_BEGIN_PROTECTED_CODE
#endif #endif
/* This macro is called on memory allocation failure. /* This macro is called on memory allocation failure.
* By default, it aborts the program execution. * By default, it raises a fatal error.
* NOTE: Can be overloaded by user code. * NOTE: Can be overloaded by user code.
*/ */
#ifndef M_MEMORY_FULL #ifndef M_MEMORY_FULL
#define M_MEMORY_FULL(size) do { \ #define M_MEMORY_FULL(size) \
fprintf(stderr, "ERROR(M*LIB): Cannot allocate %zu bytes of memory at (%s:%s:%d).\n", \ M_RAISE_FATAL("Cannot allocate %zu bytes of memory at (%s:%s:%d).\n", \
(size_t) (size), __FILE__, __func__, __LINE__); \ (size_t) (size), __FILE__, __func__, __LINE__)
abort(); \
} while (0)
#endif #endif
@@ -302,6 +296,28 @@ M_BEGIN_PROTECTED_CODE
/********************* ERROR handling **********************/ /********************* ERROR handling **********************/
/************************************************************/ /************************************************************/
/* Raise a fatal error and terminate the current execution flow.
Default behavior performs the following actions:
* format and print the error message on stderr
* terminate the program.
Can be overloaded by the user code.
Usage:
M_RAISE_FATAL(message, ...)
with
message, a printf formatted message associated to the error
*/
#ifndef M_RAISE_FATAL
#if M_USE_STDIO == 1
#define M_RAISE_FATAL(...) do { \
fprintf(stderr, "ERROR(M*LIB): " __VA_ARGS__); \
abort(); \
} while (0)
#else
# error Without stdio.h, definitions for macro M_RAISE_FATAL is mandatory.
#endif
#endif
/* Define the default assertion macro used by M*LIB. /* Define the default assertion macro used by M*LIB.
* By default, it is an encapsulation of CLIB assert. * By default, it is an encapsulation of CLIB assert.
* NOTE: Can be overiden by user if it needs to keep finer access * NOTE: Can be overiden by user if it needs to keep finer access
@@ -333,9 +349,8 @@ M_BEGIN_PROTECTED_CODE
#ifndef M_ASSERT_INIT #ifndef M_ASSERT_INIT
#define M_ASSERT_INIT(expr, object) { \ #define M_ASSERT_INIT(expr, object) { \
if (!(expr)) { \ if (!(expr)) { \
fprintf(stderr, "ERROR(M*LIB): Cannot initialize %s at (%s:%s:%d): %s\n", \ M_RAISE_FATAL("Cannot initialize %s at (%s:%s:%d): %s\n", \
(object), __FILE__, __func__, __LINE__, #expr); \ (object), __FILE__, __func__, __LINE__, #expr); \
abort(); \
} } while (0) } } while (0)
#endif #endif