net-snmp 5.7
snmp_assert.h
00001 #ifndef SNMP_ASSERT_H
00002 #define SNMP_ASSERT_H
00003 
00004 #ifdef NETSNMP_USE_ASSERT
00005 #   include <assert.h>
00006 #else
00007 #   include <net-snmp/library/snmp_logging.h>
00008 #endif
00009 
00010 
00011 /*
00012  * MACROs don't need extern "C"
00013  */
00014 
00015 /*
00016  * define __STRING for systems (*cough* sun *cough*) that don't have it
00017  */
00018 #ifndef __STRING
00019 #  if defined(__STDC__) || defined(_MSC_VER)
00020 #    define __STRING(x) #x
00021 #  else
00022 #    define __STRING(x) "x"
00023 #  endif /* __STDC__ */
00024 #endif /* __STRING */
00025 
00026 /*
00027  * always use assert if requested
00028  */
00029 #ifdef NETSNMP_USE_ASSERT
00030 /*   void netsnmp_assert( int );*/
00031 #   define netsnmp_assert(x)  assert( x )
00032 #   define netsnmp_assert_or_return(x, y)  assert( x )
00033 #   define netsnmp_assert_or_msgreturn(x, y, z)  assert( x )
00034 #else
00035 /*
00036  *  if asserts weren't requested, just log, unless NETSNMP_NO_DEBUGGING specified
00037  */
00038 #   ifndef NETSNMP_NO_DEBUGGING
00039 #      ifdef  NETSNMP_FUNCTION
00040 #          define NETSNMP_FUNC_FMT " %s()\n"
00041 #          define NETSNMP_FUNC_PARAM NETSNMP_FUNCTION
00042 #      else
00043 #          define NETSNMP_FUNC_FMT "%c"
00044 #          define NETSNMP_FUNC_PARAM '\n'
00045 #      endif
00046 #
00047 #      define netsnmp_assert(x)  do { \
00048               if ( x ) \
00049                  ; \
00050               else \
00051                  snmp_log(LOG_ERR, \
00052                           "netsnmp_assert %s failed %s:%d" NETSNMP_FUNC_FMT, \
00053                           __STRING(x),__FILE__,__LINE__, \
00054                           NETSNMP_FUNC_PARAM); \
00055            }while(0)
00056 #      define netsnmp_assert_or_return(x, y)  do {        \
00057               if ( x ) \
00058                  ; \
00059               else { \
00060                  snmp_log(LOG_ERR, \
00061                           "netsnmp_assert %s failed %s:%d" NETSNMP_FUNC_FMT, \
00062                           __STRING(x),__FILE__,__LINE__, \
00063                           NETSNMP_FUNC_PARAM); \
00064                  return y; \
00065               } \
00066            }while(0)
00067 #      define netsnmp_assert_or_msgreturn(x, y, z)  do {       \
00068               if ( x ) \
00069                  ; \
00070               else { \
00071                  snmp_log(LOG_ERR, \
00072                           "netsnmp_assert %s failed %s:%d" NETSNMP_FUNC_FMT, \
00073                           __STRING(x),__FILE__,__LINE__, \
00074                           NETSNMP_FUNC_PARAM); \
00075                  snmp_log(LOG_ERR, y); \
00076                  return z; \
00077               } \
00078            }while(0)
00079 #   else /* NO DEBUGGING */
00080 #      define netsnmp_assert(x)
00081 #      define netsnmp_assert_or_return(x, y)  do {        \
00082                  if ( x ) \
00083                     ; \
00084                  else { \
00085                     return y; \
00086                  } \
00087               }while(0)
00088 #      define netsnmp_assert_or_msgreturn(x, y, z)  do {       \
00089                  if ( x ) \
00090                     ; \
00091                  else { \
00092                     return z; \
00093                  } \
00094               }while(0)
00095 #   endif /* NO DEBUGGING */
00096 #endif /* not NETSNMP_USE_ASSERT */
00097 
00098 
00099 #define netsnmp_static_assert(x) \
00100     do { switch(0) { case (x): case 0: ; } } while(0)
00101 
00102 
00103 /*
00104  *  EXPERIMENTAL macros. May be removed without warning in future
00105  * releases. Use at your own risk
00106  *
00107  * The series of uppercase letters at or near the end of these macros give
00108  * an indication of what they do. The letters used are:
00109  *
00110  *   L  : log a message
00111  *   RN : return NULL
00112  *   RE : return a specific hardcoded error appropriate for the condition
00113  *   RV : return user specified value
00114  *
00115  */
00116 #define netsnmp_malloc_check_LRN(ptr)           \
00117     netsnmp_assert_or_return( (ptr) != NULL, NULL)
00118 #define netsnmp_malloc_check_LRE(ptr)           \
00119     netsnmp_assert_or_return( (ptr) != NULL, SNMPERR_MALLOC)
00120 #define netsnmp_malloc_check_LRV(ptr, val)                          \
00121     netsnmp_assert_or_return( (ptr) != NULL, val)
00122 
00123 #define netsnmp_require_ptr_LRV( ptr, val ) \
00124     netsnmp_assert_or_return( (ptr) != NULL, val)
00125 
00126 
00127 #endif /* SNMP_ASSERT_H */