Path: blob/main/misc/emulator/xnes/snes9x/apu/blargg_common.h
28798 views
// Sets up common environment for Shay Green's libraries.1// To change configuration options, modify blargg_config.h, not this file.23// snes_spc 0.9.04#ifndef BLARGG_COMMON_H5#define BLARGG_COMMON_H67#include <stddef.h>8#include <stdlib.h>9#include <assert.h>10#include <limits.h>1112#undef BLARGG_COMMON_H13// allow blargg_config.h to #include blargg_common.h14#include "blargg_config.h"15#ifndef BLARGG_COMMON_H16#define BLARGG_COMMON_H1718// BLARGG_RESTRICT: equivalent to restrict, where supported19#if defined (__GNUC__) || _MSC_VER >= 110020#define BLARGG_RESTRICT __restrict21#else22#define BLARGG_RESTRICT23#endif2425// STATIC_CAST(T,expr): Used in place of static_cast<T> (expr)26#ifndef STATIC_CAST27#define STATIC_CAST(T,expr) ((T) (expr))28#endif2930// blargg_err_t (0 on success, otherwise error string)31#ifndef blargg_err_t32typedef const char* blargg_err_t;33#endif3435// blargg_vector - very lightweight vector of POD types (no constructor/destructor)36template<class T>37class blargg_vector {38T* begin_;39size_t size_;40public:41blargg_vector() : begin_( 0 ), size_( 0 ) { }42~blargg_vector() { free( begin_ ); }43size_t size() const { return size_; }44T* begin() const { return begin_; }45T* end() const { return begin_ + size_; }46blargg_err_t resize( size_t n )47{48// TODO: blargg_common.cpp to hold this as an outline function, ugh49void* p = realloc( begin_, n * sizeof (T) );50if ( p )51begin_ = (T*) p;52else if ( n > size_ ) // realloc failure only a problem if expanding53return "Out of memory";54size_ = n;55return 0;56}57void clear() { void* p = begin_; begin_ = 0; size_ = 0; free( p ); }58T& operator [] ( size_t n ) const59{60assert( n <= size_ ); // <= to allow past-the-end value61return begin_ [n];62}63};6465#ifndef BLARGG_DISABLE_NOTHROW66// throw spec mandatory in ISO C++ if operator new can return NULL67#if __cplusplus >= 199711 || defined (__GNUC__)68#define BLARGG_THROWS( spec ) throw spec69#else70#define BLARGG_THROWS( spec )71#endif72#define BLARGG_DISABLE_NOTHROW \73void* operator new ( size_t s ) BLARGG_THROWS(()) { return malloc( s ); }\74void operator delete ( void* p ) { free( p ); }75#define BLARGG_NEW new76#else77#include <new>78#define BLARGG_NEW new (std::nothrow)79#endif8081// BLARGG_4CHAR('a','b','c','d') = 'abcd' (four character integer constant)82#define BLARGG_4CHAR( a, b, c, d ) \83((a&0xFF)*0x1000000L + (b&0xFF)*0x10000L + (c&0xFF)*0x100L + (d&0xFF))8485// BOOST_STATIC_ASSERT( expr ): Generates compile error if expr is 0.86#ifndef BOOST_STATIC_ASSERT87#ifdef _MSC_VER88// MSVC6 (_MSC_VER < 1300) fails for use of __LINE__ when /Zl is specified89#define BOOST_STATIC_ASSERT( expr ) \90void blargg_failed_( int (*arg) [2 / (int) !!(expr) - 1] )91#else92// Some other compilers fail when declaring same function multiple times in class,93// so differentiate them by line94#define BOOST_STATIC_ASSERT( expr ) \95void blargg_failed_( int (*arg) [2 / !!(expr) - 1] [__LINE__] )96#endif97#endif9899// BLARGG_COMPILER_HAS_BOOL: If 0, provides bool support for old compiler. If 1,100// compiler is assumed to support bool. If undefined, availability is determined.101#ifndef BLARGG_COMPILER_HAS_BOOL102#if defined (__MWERKS__)103#if !__option(bool)104#define BLARGG_COMPILER_HAS_BOOL 0105#endif106#elif defined (_MSC_VER)107#if _MSC_VER < 1100108#define BLARGG_COMPILER_HAS_BOOL 0109#endif110#elif defined (__GNUC__)111// supports bool112#elif __cplusplus < 199711113#define BLARGG_COMPILER_HAS_BOOL 0114#endif115#endif116#if defined (BLARGG_COMPILER_HAS_BOOL) && !BLARGG_COMPILER_HAS_BOOL117// If you get errors here, modify your blargg_config.h file118typedef int bool;119const bool true = 1;120const bool false = 0;121#endif122123// blargg_long/blargg_ulong = at least 32 bits, int if it's big enough124125#if INT_MAX < 0x7FFFFFFF || LONG_MAX == 0x7FFFFFFF126typedef long blargg_long;127#else128typedef int blargg_long;129#endif130131#if UINT_MAX < 0xFFFFFFFF || ULONG_MAX == 0xFFFFFFFF132typedef unsigned long blargg_ulong;133#else134typedef unsigned blargg_ulong;135#endif136137// BOOST::int8_t etc.138139// HAVE_STDINT_H: If defined, use <stdint.h> for int8_t etc.140#if defined (HAVE_STDINT_H)141#include <stdint.h>142#define BOOST143144// HAVE_INTTYPES_H: If defined, use <stdint.h> for int8_t etc.145#elif defined (HAVE_INTTYPES_H)146#include <inttypes.h>147#define BOOST148149#else150struct BOOST151{152#if UCHAR_MAX == 0xFF && SCHAR_MAX == 0x7F153typedef signed char int8_t;154typedef unsigned char uint8_t;155#else156// No suitable 8-bit type available157typedef struct see_blargg_common_h int8_t;158typedef struct see_blargg_common_h uint8_t;159#endif160161#if USHRT_MAX == 0xFFFF162typedef short int16_t;163typedef unsigned short uint16_t;164#else165// No suitable 16-bit type available166typedef struct see_blargg_common_h int16_t;167typedef struct see_blargg_common_h uint16_t;168#endif169170#if ULONG_MAX == 0xFFFFFFFF171typedef long int32_t;172typedef unsigned long uint32_t;173#elif UINT_MAX == 0xFFFFFFFF174typedef int int32_t;175typedef unsigned int uint32_t;176#else177// No suitable 32-bit type available178typedef struct see_blargg_common_h int32_t;179typedef struct see_blargg_common_h uint32_t;180#endif181};182#endif183184#endif185#endif186187188