snmp_assert.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef SNMP_ASSERT_H
  2. #define SNMP_ASSERT_H
  3. #ifdef NETSNMP_USE_ASSERT
  4. # include <assert.h>
  5. #else
  6. # include <net-snmp/library/snmp_logging.h>
  7. #endif
  8. /*
  9. * MACROs don't need extern "C"
  10. */
  11. /*
  12. * define __STRING for systems (*cough* sun *cough*) that don't have it
  13. */
  14. #ifndef __STRING
  15. # if defined(__STDC__) || defined(_MSC_VER)
  16. # define __STRING(x) #x
  17. # else
  18. # define __STRING(x) "x"
  19. # endif /* __STDC__ */
  20. #endif /* __STRING */
  21. /*
  22. * always use assert if requested
  23. */
  24. #ifdef NETSNMP_USE_ASSERT
  25. /* void netsnmp_assert( int );*/
  26. # define netsnmp_assert(x) assert( x )
  27. # define netsnmp_assert_or_return(x, y) assert( x )
  28. # define netsnmp_assert_or_msgreturn(x, y, z) assert( x )
  29. #else
  30. /*
  31. * if asserts weren't requested, just log, unless NETSNMP_NO_DEBUGGING specified
  32. */
  33. # ifndef NETSNMP_NO_DEBUGGING
  34. # ifdef NETSNMP_FUNCTION
  35. # define NETSNMP_FUNC_FMT " %s()\n"
  36. # define NETSNMP_FUNC_PARAM NETSNMP_FUNCTION
  37. # else
  38. # define NETSNMP_FUNC_FMT "%c"
  39. # define NETSNMP_FUNC_PARAM '\n'
  40. # endif
  41. #
  42. # define netsnmp_assert(x) do { \
  43. if ( x ) \
  44. ; \
  45. else \
  46. snmp_log(LOG_ERR, \
  47. "netsnmp_assert %s failed %s:%d" NETSNMP_FUNC_FMT, \
  48. __STRING(x),__FILE__,__LINE__, \
  49. NETSNMP_FUNC_PARAM); \
  50. }while(0)
  51. # define netsnmp_assert_or_return(x, y) do { \
  52. if ( x ) \
  53. ; \
  54. else { \
  55. snmp_log(LOG_ERR, \
  56. "netsnmp_assert %s failed %s:%d" NETSNMP_FUNC_FMT, \
  57. __STRING(x),__FILE__,__LINE__, \
  58. NETSNMP_FUNC_PARAM); \
  59. return y; \
  60. } \
  61. }while(0)
  62. # define netsnmp_assert_or_msgreturn(x, y, z) do { \
  63. if ( x ) \
  64. ; \
  65. else { \
  66. snmp_log(LOG_ERR, \
  67. "netsnmp_assert %s failed %s:%d" NETSNMP_FUNC_FMT, \
  68. __STRING(x),__FILE__,__LINE__, \
  69. NETSNMP_FUNC_PARAM); \
  70. snmp_log(LOG_ERR, y); \
  71. return z; \
  72. } \
  73. }while(0)
  74. # else /* NO DEBUGGING */
  75. # define netsnmp_assert(x)
  76. # define netsnmp_assert_or_return(x, y) do { \
  77. if ( x ) \
  78. ; \
  79. else { \
  80. return y; \
  81. } \
  82. }while(0)
  83. # define netsnmp_assert_or_msgreturn(x, y, z) do { \
  84. if ( x ) \
  85. ; \
  86. else { \
  87. return z; \
  88. } \
  89. }while(0)
  90. # endif /* NO DEBUGGING */
  91. #endif /* not NETSNMP_USE_ASSERT */
  92. #define netsnmp_static_assert(x) \
  93. do { switch(0) { case (x): case 0: ; } } while(0)
  94. /*
  95. * EXPERIMENTAL macros. May be removed without warning in future
  96. * releases. Use at your own risk
  97. *
  98. * The series of uppercase letters at or near the end of these macros give
  99. * an indication of what they do. The letters used are:
  100. *
  101. * L : log a message
  102. * RN : return NULL
  103. * RE : return a specific hardcoded error appropriate for the condition
  104. * RV : return user specified value
  105. *
  106. */
  107. #define netsnmp_malloc_check_LRN(ptr) \
  108. netsnmp_assert_or_return( (ptr) != NULL, NULL)
  109. #define netsnmp_malloc_check_LRE(ptr) \
  110. netsnmp_assert_or_return( (ptr) != NULL, SNMPERR_MALLOC)
  111. #define netsnmp_malloc_check_LRV(ptr, val) \
  112. netsnmp_assert_or_return( (ptr) != NULL, val)
  113. #define netsnmp_require_ptr_LRV( ptr, val ) \
  114. netsnmp_assert_or_return( (ptr) != NULL, val)
  115. #endif /* SNMP_ASSERT_H */