1 /** 2 * Copyright (C) Mellanox Technologies Ltd. 2001-2018. ALL RIGHTS RESERVED. 3 * 4 * See file LICENSE for terms. 5 */ 6 7 #ifndef _UCS_ASSERT_H 8 #define _UCS_ASSERT_H 9 10 #ifdef HAVE_CONFIG_H 11 # include "config.h" 12 #endif 13 14 #include <ucs/sys/compiler_def.h> 15 16 17 BEGIN_C_DECLS 18 19 /** @file assert.h */ 20 21 /** 22 * Fail if _expression evaluates to 0 23 */ 24 #define ucs_assert_always(_expression) \ 25 do { \ 26 if (!ucs_likely(_expression)) { \ 27 ucs_fatal_error_format(__FILE__, __LINE__, __FUNCTION__, \ 28 "Assertion `%s' failed", #_expression); \ 29 } \ 30 } while (0) 31 32 33 /** 34 * Fail if _expression evaluates to 0 and print a formatted error message 35 */ 36 #define ucs_assertv_always(_expression, _fmt, ...) \ 37 do { \ 38 if (!ucs_likely(_expression)) { \ 39 ucs_fatal_error_format(__FILE__, __LINE__, __FUNCTION__, \ 40 "Assertion `%s' failed: " _fmt, \ 41 #_expression, ## __VA_ARGS__); \ 42 } \ 43 } while (0) 44 45 46 /** 47 * Generate a fatal error 48 */ 49 #define ucs_fatal(_fmt, ...) \ 50 ucs_fatal_error_format(__FILE__, __LINE__, __FUNCTION__, \ 51 "Fatal: " _fmt, ## __VA_ARGS__) 52 53 54 #if defined (ENABLE_ASSERT) || defined(__COVERITY__) || defined(__clang_analyzer__) 55 56 #define UCS_ENABLE_ASSERT 1 57 58 /** 59 * Generate a program bug report if assertions are enabled 60 */ 61 #define ucs_bug(_fmt, ...) \ 62 ucs_fatal_error_format(__FILE__, __LINE__, __FUNCTION__, \ 63 "Bug: " _fmt, ## __VA_ARGS__) 64 65 #define ucs_assert(...) ucs_assert_always(__VA_ARGS__) 66 #define ucs_assertv(...) ucs_assertv_always(__VA_ARGS__) 67 68 #else 69 70 #define UCS_ENABLE_ASSERT 0 71 72 #define ucs_bug(...) 73 #define ucs_assert(...) 74 #define ucs_assertv(...) 75 76 #endif 77 78 79 /** 80 * Generate a fatal error and stop the program. 81 * 82 * @param [in] file Source file name 83 * @param [in] line Source line number 84 * @param [in] function Calling function name 85 * @param [in] format Error message format string. Multi-line message is 86 * supported. 87 */ 88 void ucs_fatal_error_format(const char *file, unsigned line, 89 const char *function, const char *format, ...) 90 UCS_F_NORETURN UCS_F_PRINTF(4, 5); 91 92 93 /** 94 * Generate a fatal error and stop the program. 95 * 96 * @param [in] file Source file name 97 * @param [in] line Source line number 98 * @param [in] function Calling function name 99 * @param [in] message_buf Error message buffer. Multi-line message is 100 * supported. 101 * 102 * IMPORTANT NOTE: message_buf could be overridden by this function 103 */ 104 void ucs_fatal_error_message(const char *file, unsigned line, 105 const char *function, char *message_buf) 106 UCS_F_NORETURN; 107 108 109 END_C_DECLS 110 111 #endif 112