diff snes_spc/blargg_common.h @ 0:e38dacceb958

initial import
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 05:53:11 -0700
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/snes_spc/blargg_common.h	Fri Oct 21 05:53:11 2011 -0700
     1.3 @@ -0,0 +1,186 @@
     1.4 +// Sets up common environment for Shay Green's libraries.
     1.5 +// To change configuration options, modify blargg_config.h, not this file.
     1.6 +
     1.7 +// snes_spc 0.9.0
     1.8 +#ifndef BLARGG_COMMON_H
     1.9 +#define BLARGG_COMMON_H
    1.10 +
    1.11 +#include <stddef.h>
    1.12 +#include <stdlib.h>
    1.13 +#include <assert.h>
    1.14 +#include <limits.h>
    1.15 +
    1.16 +#undef BLARGG_COMMON_H
    1.17 +// allow blargg_config.h to #include blargg_common.h
    1.18 +#include "blargg_config.h"
    1.19 +#ifndef BLARGG_COMMON_H
    1.20 +#define BLARGG_COMMON_H
    1.21 +
    1.22 +// BLARGG_RESTRICT: equivalent to restrict, where supported
    1.23 +#if defined (__GNUC__) || _MSC_VER >= 1100
    1.24 +	#define BLARGG_RESTRICT __restrict
    1.25 +#else
    1.26 +	#define BLARGG_RESTRICT
    1.27 +#endif
    1.28 +
    1.29 +// STATIC_CAST(T,expr): Used in place of static_cast<T> (expr)
    1.30 +#ifndef STATIC_CAST
    1.31 +	#define STATIC_CAST(T,expr) ((T) (expr))
    1.32 +#endif
    1.33 +
    1.34 +// blargg_err_t (0 on success, otherwise error string)
    1.35 +#ifndef blargg_err_t
    1.36 +	typedef const char* blargg_err_t;
    1.37 +#endif
    1.38 +
    1.39 +// blargg_vector - very lightweight vector of POD types (no constructor/destructor)
    1.40 +template<class T>
    1.41 +class blargg_vector {
    1.42 +	T* begin_;
    1.43 +	size_t size_;
    1.44 +public:
    1.45 +	blargg_vector() : begin_( 0 ), size_( 0 ) { }
    1.46 +	~blargg_vector() { free( begin_ ); }
    1.47 +	size_t size() const { return size_; }
    1.48 +	T* begin() const { return begin_; }
    1.49 +	T* end() const { return begin_ + size_; }
    1.50 +	blargg_err_t resize( size_t n )
    1.51 +	{
    1.52 +		// TODO: blargg_common.cpp to hold this as an outline function, ugh
    1.53 +		void* p = realloc( begin_, n * sizeof (T) );
    1.54 +		if ( p )
    1.55 +			begin_ = (T*) p;
    1.56 +		else if ( n > size_ ) // realloc failure only a problem if expanding
    1.57 +			return "Out of memory";
    1.58 +		size_ = n;
    1.59 +		return 0;
    1.60 +	}
    1.61 +	void clear() { void* p = begin_; begin_ = 0; size_ = 0; free( p ); }
    1.62 +	T& operator [] ( size_t n ) const
    1.63 +	{
    1.64 +		assert( n <= size_ ); // <= to allow past-the-end value
    1.65 +		return begin_ [n];
    1.66 +	}
    1.67 +};
    1.68 +
    1.69 +#ifndef BLARGG_DISABLE_NOTHROW
    1.70 +	// throw spec mandatory in ISO C++ if operator new can return NULL
    1.71 +	#if __cplusplus >= 199711 || defined (__GNUC__)
    1.72 +		#define BLARGG_THROWS( spec ) throw spec
    1.73 +	#else
    1.74 +		#define BLARGG_THROWS( spec )
    1.75 +	#endif
    1.76 +	#define BLARGG_DISABLE_NOTHROW \
    1.77 +		void* operator new ( size_t s ) BLARGG_THROWS(()) { return malloc( s ); }\
    1.78 +		void operator delete ( void* p ) { free( p ); }
    1.79 +	#define BLARGG_NEW new
    1.80 +#else
    1.81 +	#include <new>
    1.82 +	#define BLARGG_NEW new (std::nothrow)
    1.83 +#endif
    1.84 +
    1.85 +// BLARGG_4CHAR('a','b','c','d') = 'abcd' (four character integer constant)
    1.86 +#define BLARGG_4CHAR( a, b, c, d ) \
    1.87 +	((a&0xFF)*0x1000000L + (b&0xFF)*0x10000L + (c&0xFF)*0x100L + (d&0xFF))
    1.88 +
    1.89 +// BOOST_STATIC_ASSERT( expr ): Generates compile error if expr is 0.
    1.90 +#ifndef BOOST_STATIC_ASSERT
    1.91 +	#ifdef _MSC_VER
    1.92 +		// MSVC6 (_MSC_VER < 1300) fails for use of __LINE__ when /Zl is specified
    1.93 +		#define BOOST_STATIC_ASSERT( expr ) \
    1.94 +			void blargg_failed_( int (*arg) [2 / (int) !!(expr) - 1] )
    1.95 +	#else
    1.96 +		// Some other compilers fail when declaring same function multiple times in class,
    1.97 +		// so differentiate them by line
    1.98 +		#define BOOST_STATIC_ASSERT( expr ) \
    1.99 +			void blargg_failed_( int (*arg) [2 / !!(expr) - 1] [__LINE__] )
   1.100 +	#endif
   1.101 +#endif
   1.102 +
   1.103 +// BLARGG_COMPILER_HAS_BOOL: If 0, provides bool support for old compiler. If 1,
   1.104 +// compiler is assumed to support bool. If undefined, availability is determined.
   1.105 +#ifndef BLARGG_COMPILER_HAS_BOOL
   1.106 +	#if defined (__MWERKS__)
   1.107 +		#if !__option(bool)
   1.108 +			#define BLARGG_COMPILER_HAS_BOOL 0
   1.109 +		#endif
   1.110 +	#elif defined (_MSC_VER)
   1.111 +		#if _MSC_VER < 1100
   1.112 +			#define BLARGG_COMPILER_HAS_BOOL 0
   1.113 +		#endif
   1.114 +	#elif defined (__GNUC__)
   1.115 +		// supports bool
   1.116 +	#elif __cplusplus < 199711
   1.117 +		#define BLARGG_COMPILER_HAS_BOOL 0
   1.118 +	#endif
   1.119 +#endif
   1.120 +#if defined (BLARGG_COMPILER_HAS_BOOL) && !BLARGG_COMPILER_HAS_BOOL
   1.121 +	// If you get errors here, modify your blargg_config.h file
   1.122 +	typedef int bool;
   1.123 +	const bool true  = 1;
   1.124 +	const bool false = 0;
   1.125 +#endif
   1.126 +
   1.127 +// blargg_long/blargg_ulong = at least 32 bits, int if it's big enough
   1.128 +
   1.129 +#if INT_MAX < 0x7FFFFFFF || LONG_MAX == 0x7FFFFFFF
   1.130 +	typedef long blargg_long;
   1.131 +#else
   1.132 +	typedef int blargg_long;
   1.133 +#endif
   1.134 +
   1.135 +#if UINT_MAX < 0xFFFFFFFF || ULONG_MAX == 0xFFFFFFFF
   1.136 +	typedef unsigned long blargg_ulong;
   1.137 +#else
   1.138 +	typedef unsigned blargg_ulong;
   1.139 +#endif
   1.140 +
   1.141 +// BOOST::int8_t etc.
   1.142 +
   1.143 +// HAVE_STDINT_H: If defined, use <stdint.h> for int8_t etc.
   1.144 +#if defined (HAVE_STDINT_H)
   1.145 +	#include <stdint.h>
   1.146 +	#define BOOST
   1.147 +
   1.148 +// HAVE_INTTYPES_H: If defined, use <stdint.h> for int8_t etc.
   1.149 +#elif defined (HAVE_INTTYPES_H)
   1.150 +	#include <inttypes.h>
   1.151 +	#define BOOST
   1.152 +
   1.153 +#else
   1.154 +	struct BOOST
   1.155 +	{
   1.156 +		#if UCHAR_MAX == 0xFF && SCHAR_MAX == 0x7F
   1.157 +			typedef signed char     int8_t;
   1.158 +			typedef unsigned char   uint8_t;
   1.159 +		#else
   1.160 +			// No suitable 8-bit type available
   1.161 +			typedef struct see_blargg_common_h int8_t;
   1.162 +			typedef struct see_blargg_common_h uint8_t;
   1.163 +		#endif
   1.164 +		
   1.165 +		#if USHRT_MAX == 0xFFFF
   1.166 +			typedef short           int16_t;
   1.167 +			typedef unsigned short  uint16_t;
   1.168 +		#else
   1.169 +			// No suitable 16-bit type available
   1.170 +			typedef struct see_blargg_common_h int16_t;
   1.171 +			typedef struct see_blargg_common_h uint16_t;
   1.172 +		#endif
   1.173 +		
   1.174 +		#if ULONG_MAX == 0xFFFFFFFF
   1.175 +			typedef long            int32_t;
   1.176 +			typedef unsigned long   uint32_t;
   1.177 +		#elif UINT_MAX == 0xFFFFFFFF
   1.178 +			typedef int             int32_t;
   1.179 +			typedef unsigned int    uint32_t;
   1.180 +		#else
   1.181 +			// No suitable 32-bit type available
   1.182 +			typedef struct see_blargg_common_h int32_t;
   1.183 +			typedef struct see_blargg_common_h uint32_t;
   1.184 +		#endif
   1.185 +	};
   1.186 +#endif
   1.187 +
   1.188 +#endif
   1.189 +#endif