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 );
00014 
00015 #ifdef NDEBUG
00016 #define ASSERTING 0
00017 #else
00018 #define ASSERTING 1
00019 #endif
00020 
00021 /** printf() for assertions
00022  *
00023  * This function exists so that the assert() macro can expand to
00024  * printf() calls without dragging the printf() prototype into scope.
00025  *
00026  * As far as the compiler is concerned, assert_printf() and printf() are
00027  * completely unrelated calls; it's only at the assembly stage that
00028  * references to the assert_printf symbol are collapsed into references
00029  * to the printf symbol.
00030  */
00031 extern int __attribute__ (( format ( printf, 1, 2 ) )) 
00032 assert_printf ( const char *fmt, ... ) asm ( "printf" );
00033 
00034 /**
00035  * Assert a condition at run-time.
00036  *
00037  * If the condition is not true, a debug message will be printed.
00038  * Assertions only take effect in debug-enabled builds (see DBG()).
00039  *
00040  * @todo Make an assertion failure abort the program
00041  *
00042  */
00043 #define assert( condition )                                                  \
00044         do {                                                                 \
00045                 if ( ASSERTING && ! (condition) ) {                          \
00046                         assert_printf ( "assert(%s) failed at %s line %d\n", \
00047                                         #condition, __FILE__, __LINE__ );    \
00048                 }                                                            \
00049         } while ( 0 )
00050 
00051 /**
00052  * Assert a condition at link-time.
00053  *
00054  * If the condition is not true, the link will fail with an unresolved
00055  * symbol (error_symbol).
00056  *
00057  * This macro is gPXE-specific.  Do not use this macro in code
00058  * intended to be portable.
00059  *
00060  */
00061 #define linker_assert( condition, error_symbol )        \
00062         if ( ! (condition) ) {                          \
00063                 extern void error_symbol ( void );      \
00064                 error_symbol();                         \
00065         }
00066 
00067 #endif /* _ASSERT_H */

Generated on Tue Apr 6 20:01:06 2010 for gPXE by  doxygen 1.5.7.1