annotate snes_spc/blargg_common.h @ 9:477c36226481 tip

added old scripts for historical interest.
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 07:46:18 -0700
parents e38dacceb958
children
rev   line source
rlm@0 1 // Sets up common environment for Shay Green's libraries.
rlm@0 2 // To change configuration options, modify blargg_config.h, not this file.
rlm@0 3
rlm@0 4 // snes_spc 0.9.0
rlm@0 5 #ifndef BLARGG_COMMON_H
rlm@0 6 #define BLARGG_COMMON_H
rlm@0 7
rlm@0 8 #include <stddef.h>
rlm@0 9 #include <stdlib.h>
rlm@0 10 #include <assert.h>
rlm@0 11 #include <limits.h>
rlm@0 12
rlm@0 13 #undef BLARGG_COMMON_H
rlm@0 14 // allow blargg_config.h to #include blargg_common.h
rlm@0 15 #include "blargg_config.h"
rlm@0 16 #ifndef BLARGG_COMMON_H
rlm@0 17 #define BLARGG_COMMON_H
rlm@0 18
rlm@0 19 // BLARGG_RESTRICT: equivalent to restrict, where supported
rlm@0 20 #if defined (__GNUC__) || _MSC_VER >= 1100
rlm@0 21 #define BLARGG_RESTRICT __restrict
rlm@0 22 #else
rlm@0 23 #define BLARGG_RESTRICT
rlm@0 24 #endif
rlm@0 25
rlm@0 26 // STATIC_CAST(T,expr): Used in place of static_cast<T> (expr)
rlm@0 27 #ifndef STATIC_CAST
rlm@0 28 #define STATIC_CAST(T,expr) ((T) (expr))
rlm@0 29 #endif
rlm@0 30
rlm@0 31 // blargg_err_t (0 on success, otherwise error string)
rlm@0 32 #ifndef blargg_err_t
rlm@0 33 typedef const char* blargg_err_t;
rlm@0 34 #endif
rlm@0 35
rlm@0 36 // blargg_vector - very lightweight vector of POD types (no constructor/destructor)
rlm@0 37 template<class T>
rlm@0 38 class blargg_vector {
rlm@0 39 T* begin_;
rlm@0 40 size_t size_;
rlm@0 41 public:
rlm@0 42 blargg_vector() : begin_( 0 ), size_( 0 ) { }
rlm@0 43 ~blargg_vector() { free( begin_ ); }
rlm@0 44 size_t size() const { return size_; }
rlm@0 45 T* begin() const { return begin_; }
rlm@0 46 T* end() const { return begin_ + size_; }
rlm@0 47 blargg_err_t resize( size_t n )
rlm@0 48 {
rlm@0 49 // TODO: blargg_common.cpp to hold this as an outline function, ugh
rlm@0 50 void* p = realloc( begin_, n * sizeof (T) );
rlm@0 51 if ( p )
rlm@0 52 begin_ = (T*) p;
rlm@0 53 else if ( n > size_ ) // realloc failure only a problem if expanding
rlm@0 54 return "Out of memory";
rlm@0 55 size_ = n;
rlm@0 56 return 0;
rlm@0 57 }
rlm@0 58 void clear() { void* p = begin_; begin_ = 0; size_ = 0; free( p ); }
rlm@0 59 T& operator [] ( size_t n ) const
rlm@0 60 {
rlm@0 61 assert( n <= size_ ); // <= to allow past-the-end value
rlm@0 62 return begin_ [n];
rlm@0 63 }
rlm@0 64 };
rlm@0 65
rlm@0 66 #ifndef BLARGG_DISABLE_NOTHROW
rlm@0 67 // throw spec mandatory in ISO C++ if operator new can return NULL
rlm@0 68 #if __cplusplus >= 199711 || defined (__GNUC__)
rlm@0 69 #define BLARGG_THROWS( spec ) throw spec
rlm@0 70 #else
rlm@0 71 #define BLARGG_THROWS( spec )
rlm@0 72 #endif
rlm@0 73 #define BLARGG_DISABLE_NOTHROW \
rlm@0 74 void* operator new ( size_t s ) BLARGG_THROWS(()) { return malloc( s ); }\
rlm@0 75 void operator delete ( void* p ) { free( p ); }
rlm@0 76 #define BLARGG_NEW new
rlm@0 77 #else
rlm@0 78 #include <new>
rlm@0 79 #define BLARGG_NEW new (std::nothrow)
rlm@0 80 #endif
rlm@0 81
rlm@0 82 // BLARGG_4CHAR('a','b','c','d') = 'abcd' (four character integer constant)
rlm@0 83 #define BLARGG_4CHAR( a, b, c, d ) \
rlm@0 84 ((a&0xFF)*0x1000000L + (b&0xFF)*0x10000L + (c&0xFF)*0x100L + (d&0xFF))
rlm@0 85
rlm@0 86 // BOOST_STATIC_ASSERT( expr ): Generates compile error if expr is 0.
rlm@0 87 #ifndef BOOST_STATIC_ASSERT
rlm@0 88 #ifdef _MSC_VER
rlm@0 89 // MSVC6 (_MSC_VER < 1300) fails for use of __LINE__ when /Zl is specified
rlm@0 90 #define BOOST_STATIC_ASSERT( expr ) \
rlm@0 91 void blargg_failed_( int (*arg) [2 / (int) !!(expr) - 1] )
rlm@0 92 #else
rlm@0 93 // Some other compilers fail when declaring same function multiple times in class,
rlm@0 94 // so differentiate them by line
rlm@0 95 #define BOOST_STATIC_ASSERT( expr ) \
rlm@0 96 void blargg_failed_( int (*arg) [2 / !!(expr) - 1] [__LINE__] )
rlm@0 97 #endif
rlm@0 98 #endif
rlm@0 99
rlm@0 100 // BLARGG_COMPILER_HAS_BOOL: If 0, provides bool support for old compiler. If 1,
rlm@0 101 // compiler is assumed to support bool. If undefined, availability is determined.
rlm@0 102 #ifndef BLARGG_COMPILER_HAS_BOOL
rlm@0 103 #if defined (__MWERKS__)
rlm@0 104 #if !__option(bool)
rlm@0 105 #define BLARGG_COMPILER_HAS_BOOL 0
rlm@0 106 #endif
rlm@0 107 #elif defined (_MSC_VER)
rlm@0 108 #if _MSC_VER < 1100
rlm@0 109 #define BLARGG_COMPILER_HAS_BOOL 0
rlm@0 110 #endif
rlm@0 111 #elif defined (__GNUC__)
rlm@0 112 // supports bool
rlm@0 113 #elif __cplusplus < 199711
rlm@0 114 #define BLARGG_COMPILER_HAS_BOOL 0
rlm@0 115 #endif
rlm@0 116 #endif
rlm@0 117 #if defined (BLARGG_COMPILER_HAS_BOOL) && !BLARGG_COMPILER_HAS_BOOL
rlm@0 118 // If you get errors here, modify your blargg_config.h file
rlm@0 119 typedef int bool;
rlm@0 120 const bool true = 1;
rlm@0 121 const bool false = 0;
rlm@0 122 #endif
rlm@0 123
rlm@0 124 // blargg_long/blargg_ulong = at least 32 bits, int if it's big enough
rlm@0 125
rlm@0 126 #if INT_MAX < 0x7FFFFFFF || LONG_MAX == 0x7FFFFFFF
rlm@0 127 typedef long blargg_long;
rlm@0 128 #else
rlm@0 129 typedef int blargg_long;
rlm@0 130 #endif
rlm@0 131
rlm@0 132 #if UINT_MAX < 0xFFFFFFFF || ULONG_MAX == 0xFFFFFFFF
rlm@0 133 typedef unsigned long blargg_ulong;
rlm@0 134 #else
rlm@0 135 typedef unsigned blargg_ulong;
rlm@0 136 #endif
rlm@0 137
rlm@0 138 // BOOST::int8_t etc.
rlm@0 139
rlm@0 140 // HAVE_STDINT_H: If defined, use <stdint.h> for int8_t etc.
rlm@0 141 #if defined (HAVE_STDINT_H)
rlm@0 142 #include <stdint.h>
rlm@0 143 #define BOOST
rlm@0 144
rlm@0 145 // HAVE_INTTYPES_H: If defined, use <stdint.h> for int8_t etc.
rlm@0 146 #elif defined (HAVE_INTTYPES_H)
rlm@0 147 #include <inttypes.h>
rlm@0 148 #define BOOST
rlm@0 149
rlm@0 150 #else
rlm@0 151 struct BOOST
rlm@0 152 {
rlm@0 153 #if UCHAR_MAX == 0xFF && SCHAR_MAX == 0x7F
rlm@0 154 typedef signed char int8_t;
rlm@0 155 typedef unsigned char uint8_t;
rlm@0 156 #else
rlm@0 157 // No suitable 8-bit type available
rlm@0 158 typedef struct see_blargg_common_h int8_t;
rlm@0 159 typedef struct see_blargg_common_h uint8_t;
rlm@0 160 #endif
rlm@0 161
rlm@0 162 #if USHRT_MAX == 0xFFFF
rlm@0 163 typedef short int16_t;
rlm@0 164 typedef unsigned short uint16_t;
rlm@0 165 #else
rlm@0 166 // No suitable 16-bit type available
rlm@0 167 typedef struct see_blargg_common_h int16_t;
rlm@0 168 typedef struct see_blargg_common_h uint16_t;
rlm@0 169 #endif
rlm@0 170
rlm@0 171 #if ULONG_MAX == 0xFFFFFFFF
rlm@0 172 typedef long int32_t;
rlm@0 173 typedef unsigned long uint32_t;
rlm@0 174 #elif UINT_MAX == 0xFFFFFFFF
rlm@0 175 typedef int int32_t;
rlm@0 176 typedef unsigned int uint32_t;
rlm@0 177 #else
rlm@0 178 // No suitable 32-bit type available
rlm@0 179 typedef struct see_blargg_common_h int32_t;
rlm@0 180 typedef struct see_blargg_common_h uint32_t;
rlm@0 181 #endif
rlm@0 182 };
rlm@0 183 #endif
rlm@0 184
rlm@0 185 #endif
rlm@0 186 #endif