You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

assert.h 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #ifndef _ASSERT_H
  2. #define _ASSERT_H
  3. /** @file
  4. *
  5. * Assertions
  6. *
  7. * This file provides two assertion macros: assert() (for run-time
  8. * assertions) and linker_assert() (for link-time assertions).
  9. *
  10. */
  11. #ifdef NDEBUG
  12. #define ASSERTING 0
  13. #else
  14. #define ASSERTING 1
  15. #endif
  16. /** printf() for assertions
  17. *
  18. * This function exists so that the assert() macro can expand to
  19. * printf() calls without dragging the printf() prototype into scope.
  20. *
  21. * As far as the compiler is concerned, assert_printf() and printf() are
  22. * completely unrelated calls; it's only at the assembly stage that
  23. * references to the assert_printf symbol are collapsed into references
  24. * to the printf symbol.
  25. */
  26. extern int __attribute__ (( format ( printf, 1, 2 ) ))
  27. assert_printf ( const char *fmt, ... ) asm ( "printf" );
  28. /**
  29. * Assert a condition at run-time.
  30. *
  31. * If the condition is not true, a debug message will be printed.
  32. * Assertions only take effect in debug-enabled builds (see DBG()).
  33. *
  34. * @todo Make an assertion failure abort the program
  35. *
  36. */
  37. #define assert( condition ) \
  38. do { \
  39. if ( ASSERTING && ! (condition) ) { \
  40. assert_printf ( "assert(%s) failed at %s line %d\n", \
  41. #condition, __FILE__, __LINE__ ); \
  42. } \
  43. } while ( 0 )
  44. /**
  45. * Assert a condition at link-time.
  46. *
  47. * If the condition is not true, the link will fail with an unresolved
  48. * symbol (error_symbol).
  49. *
  50. * This macro is gPXE-specific. Do not use this macro in code
  51. * intended to be portable.
  52. *
  53. */
  54. #define linker_assert( condition, error_symbol ) \
  55. if ( ! (condition) ) { \
  56. extern void error_symbol ( void ); \
  57. error_symbol(); \
  58. }
  59. #endif /* _ASSERT_H */