rlm@0: /* Included at the beginning of library source files, after all other #include lines. rlm@0: Sets up helpful macros and services used in my source code. They don't need rlm@0: module an annoying module prefix on their names since they are defined after rlm@0: all other #include lines. */ rlm@0: rlm@0: // snes_spc 0.9.0 rlm@0: #ifndef BLARGG_SOURCE_H rlm@0: #define BLARGG_SOURCE_H rlm@0: rlm@0: // If debugging is enabled, abort program if expr is false. Meant for checking rlm@0: // internal state and consistency. A failed assertion indicates a bug in the module. rlm@0: // void assert( bool expr ); rlm@0: #include rlm@0: rlm@0: // If debugging is enabled and expr is false, abort program. Meant for checking rlm@0: // caller-supplied parameters and operations that are outside the control of the rlm@0: // module. A failed requirement indicates a bug outside the module. rlm@0: // void require( bool expr ); rlm@0: #undef require rlm@0: #define require( expr ) assert( expr ) rlm@0: rlm@0: // Like printf() except output goes to debug log file. Might be defined to do rlm@0: // nothing (not even evaluate its arguments). rlm@0: // void dprintf( const char* format, ... ); rlm@0: static inline void blargg_dprintf_( const char*, ... ) { } rlm@0: #undef dprintf rlm@0: #define dprintf (1) ? (void) 0 : blargg_dprintf_ rlm@0: rlm@0: // If enabled, evaluate expr and if false, make debug log entry with source file rlm@0: // and line. Meant for finding situations that should be examined further, but that rlm@0: // don't indicate a problem. In all cases, execution continues normally. rlm@0: #undef check rlm@0: #define check( expr ) ((void) 0) rlm@0: rlm@0: // If expr yields error string, return it from current function, otherwise continue. rlm@0: #undef RETURN_ERR rlm@0: #define RETURN_ERR( expr ) do { \ rlm@0: blargg_err_t blargg_return_err_ = (expr); \ rlm@0: if ( blargg_return_err_ ) return blargg_return_err_; \ rlm@0: } while ( 0 ) rlm@0: rlm@0: // If ptr is 0, return out of memory error string. rlm@0: #undef CHECK_ALLOC rlm@0: #define CHECK_ALLOC( ptr ) do { if ( (ptr) == 0 ) return "Out of memory"; } while ( 0 ) rlm@0: rlm@0: // Avoid any macros which evaluate their arguments multiple times rlm@0: #undef min rlm@0: #undef max rlm@0: rlm@0: #define DEF_MIN_MAX( type ) \ rlm@0: static inline type min( type x, type y ) { if ( x < y ) return x; return y; }\ rlm@0: static inline type max( type x, type y ) { if ( y < x ) return x; return y; } rlm@0: rlm@0: DEF_MIN_MAX( int ) rlm@0: DEF_MIN_MAX( unsigned ) rlm@0: DEF_MIN_MAX( long ) rlm@0: DEF_MIN_MAX( unsigned long ) rlm@0: DEF_MIN_MAX( float ) rlm@0: DEF_MIN_MAX( double ) rlm@0: rlm@0: #undef DEF_MIN_MAX rlm@0: rlm@0: /* rlm@0: // using const references generates crappy code, and I am currenly only using these rlm@0: // for built-in types, so they take arguments by value rlm@0: rlm@0: // TODO: remove rlm@0: inline int min( int x, int y ) rlm@0: template rlm@0: inline T min( T x, T y ) rlm@0: { rlm@0: if ( x < y ) rlm@0: return x; rlm@0: return y; rlm@0: } rlm@0: rlm@0: template rlm@0: inline T max( T x, T y ) rlm@0: { rlm@0: if ( x < y ) rlm@0: return y; rlm@0: return x; rlm@0: } rlm@0: */ rlm@0: rlm@0: // TODO: good idea? bad idea? rlm@0: #undef byte rlm@0: #define byte byte_ rlm@0: typedef unsigned char byte; rlm@0: rlm@0: // deprecated rlm@0: #define BLARGG_CHECK_ALLOC CHECK_ALLOC rlm@0: #define BLARGG_RETURN_ERR RETURN_ERR rlm@0: rlm@0: // BLARGG_SOURCE_BEGIN: If defined, #included, allowing redefition of dprintf and check rlm@0: #ifdef BLARGG_SOURCE_BEGIN rlm@0: #include BLARGG_SOURCE_BEGIN rlm@0: #endif rlm@0: rlm@0: #endif