iPXE
assert.h
Go to the documentation of this file.
00001 #ifndef _ASSERT_H
00002 #define _ASSERT_H
00003 
00004 /** @file
00005  *
00006  * Assertions
00007  *
00008  * This file provides two assertion macros: assert() (for run-time
00009  * assertions) and linker_assert() (for link-time assertions).
00010  *
00011  */
00012 
00013 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00014 
00015 #ifndef ASSERTING
00016 #ifdef NDEBUG
00017 #define ASSERTING 0
00018 #else
00019 #define ASSERTING 1
00020 #endif
00021 #endif
00022 
00023 extern unsigned int assertion_failures;
00024 
00025 #define ASSERTED ( ASSERTING && ( assertion_failures != 0 ) )
00026 
00027 /** printf() for assertions
00028  *
00029  * This function exists so that the assert() macro can expand to
00030  * printf() calls without dragging the printf() prototype into scope.
00031  *
00032  * As far as the compiler is concerned, assert_printf() and printf() are
00033  * completely unrelated calls; it's only at the assembly stage that
00034  * references to the assert_printf symbol are collapsed into references
00035  * to the printf symbol.
00036  */
00037 extern int __attribute__ (( format ( printf, 1, 2 ) )) 
00038 assert_printf ( const char *fmt, ... ) asm ( "printf" );
00039 
00040 /**
00041  * Assert a condition at run-time.
00042  *
00043  * If the condition is not true, a debug message will be printed.
00044  * Assertions only take effect in debug-enabled builds (see DBG()).
00045  *
00046  * @todo Make an assertion failure abort the program
00047  *
00048  */
00049 #define assert( condition )                                                  \
00050         do {                                                                 \
00051                 if ( ASSERTING && ! (condition) ) {                          \
00052                         assertion_failures++;                                \
00053                         assert_printf ( "assert(%s) failed at %s line %d\n", \
00054                                         #condition, __FILE__, __LINE__ );    \
00055                 }                                                            \
00056         } while ( 0 )
00057 
00058 /**
00059  * Assert a condition at link-time.
00060  *
00061  * If the condition is not true, the link will fail with an unresolved
00062  * symbol (error_symbol).
00063  *
00064  * This macro is iPXE-specific.  Do not use this macro in code
00065  * intended to be portable.
00066  *
00067  */
00068 #define linker_assert( condition, error_symbol )        \
00069         if ( ! (condition) ) {                          \
00070                 extern void error_symbol ( void );      \
00071                 error_symbol();                         \
00072         }
00073 
00074 #endif /* _ASSERT_H */