assert.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. * assert.h
  3. *
  4. * Define a common interface for assertions that builds for both the PAL
  5. * and libOS.
  6. *
  7. */
  8. #ifndef ASSERT_H
  9. #define ASSERT_H
  10. #include <stdnoreturn.h>
  11. #define COMPILE_TIME_ASSERT(pred) \
  12. switch (0) { \
  13. case 0: \
  14. case (pred):; \
  15. }
  16. /* All environments should implement warn, which prints a non-optional debug
  17. * message. All environments should also implement __abort, which
  18. * terminates the process.
  19. */
  20. void warn(const char* format, ...) __attribute__((format(printf, 1, 2)));
  21. noreturn void __abort(void);
  22. #define assert(test) \
  23. ({ \
  24. long _val = (long)(test); \
  25. (!(_val)) ? ({ \
  26. warn("assert failed " __FILE__ ":%d %s (value:%lx)\n", __LINE__, #test, _val); \
  27. __abort(); \
  28. }) \
  29. : (void)0; \
  30. })
  31. #endif