github.com/256dpi/max-go@v0.7.0/lib/max/ext_prefix.h (about) 1 #include "max_types.h" // safety check to insure C74_X64 is defined appropriately 2 3 #ifndef _EXT_PREFIX_H_ 4 #define _EXT_PREFIX_H_ 5 6 //////////////////////////////////////////////////////////////////////////////// 7 // macros 8 9 #ifdef __cplusplus 10 /** 11 Ensure that any definitions following this macro use a C-linkage, not a C++ linkage. 12 The Max API uses C-linkage. 13 This is important for objects written in C++ or that use a C++ compiler. 14 This macro must be balanced with the #END_USING_C_LINKAGE macro. 15 @ingroup misc 16 */ 17 #define BEGIN_USING_C_LINKAGE \ 18 extern "C" { 19 #else 20 /** 21 Ensure that any definitions following this macro use a C-linkage, not a C++ linkage. 22 The Max API uses C-linkage. 23 This is important for objects written in C++ or that use a C++ compiler. 24 This macro must be balanced with the #END_USING_C_LINKAGE macro. 25 @ingroup misc 26 */ 27 #define BEGIN_USING_C_LINKAGE 28 #endif // __cplusplus 29 30 #ifdef __cplusplus 31 /** 32 Close a definition section that was opened using #BEGIN_USING_C_LINKAGE. 33 @ingroup misc 34 */ 35 #define END_USING_C_LINKAGE \ 36 } 37 #else 38 /** 39 Close a definition section that was opened using #BEGIN_USING_C_LINKAGE. 40 @ingroup misc 41 */ 42 #define END_USING_C_LINKAGE 43 #endif // __cplusplus 44 45 //////////////////////////////////////////////////////////////////////////////// 46 // Mac Target 47 48 #ifdef MAC_VERSION 49 50 // the C74_PRAGMA_STRUCT_PACK* macros are used to ensure that 51 // Win32 builds of Max externals use a 2 byte struct packing 52 // for all Max structs in projects that have a default struct 53 // packing other than 2 (this is typically 4). On Macintosh 54 // the default byte packing is 4, and it is with this packing 55 // that all Max structs are packed on Macintosh. If for some 56 // reason you are using a struct byte packing of something other 57 // than 4 on macintosh, there may be problems. Please let us 58 // know, and we can address this and the other Max header files to 59 // ensure 4 byte struct packing on Macintosh in such a setting. 60 #define C74_PRAGMA_STRUCT_PACKPUSH 0 61 #define C74_PRAGMA_STRUCT_PACK 0 62 #define C74_STRUCT_PACK_SIZE 4 63 64 #endif // MAC_VERSION 65 66 67 //////////////////////////////////////////////////////////////////////////////// 68 // Linux Target 69 70 #ifdef LINUX_VERSION 71 72 #define C74_PRAGMA_STRUCT_PACKPUSH 0 73 #define C74_PRAGMA_STRUCT_PACK 0 74 #define C74_STRUCT_PACK_SIZE 4 75 76 #endif // LINUX_VERSION 77 78 //////////////////////////////////////////////////////////////////////////////// 79 // Win32 Target 80 81 #ifdef WIN_VERSION 82 83 #ifndef C74_X64 84 85 // the C74_PRAGMA_STRUCT_PACK* macros are used to ensure that 86 // Win32 builds of Max externals use a 2 byte struct packing 87 // for all Max structs in projects that have a default struct 88 // packing other than 2 (this is typically 4). On Macintosh 89 // the default byte packing is 4, and it is with this packing 90 // that all Max structs are packed on Macintosh. If for some 91 // reason you are using a struct byte packing of something other 92 // than 4 on macintosh, there may be problems. Please let us 93 // know, and we can address this and the other Max header files to 94 // ensure 4 byte struct packing on Macintosh in such a setting. 95 #if ((defined(_MSC_VER) && !defined(__MWERKS__)) || (defined(__MWERKS__)&&(__MWERKS__ >= 0x0900))) 96 // Visual C++ or Metrowerks >=0x0900 support pack(push,n) 97 // this may not accomodate building for a windows target 98 // using Metrowerks on a macintosh, which might need to use 99 // the pragma align statement. 100 #define C74_PRAGMA_STRUCT_PACKPUSH 1 101 #else 102 #define C74_PRAGMA_STRUCT_PACKPUSH 0 103 #endif 104 105 #define C74_PRAGMA_STRUCT_PACK 1 106 #define C74_STRUCT_PACK_SIZE 2 107 108 #else // #if !C74_X64 109 #define C74_PRAGMA_STRUCT_PACKPUSH 0 // don't specify packing for x64 target, use default 110 #define C74_PRAGMA_STRUCT_PACKPUSH 0 111 #endif 112 113 // promote this warning to an error because 114 // it helps catch using long* when we really need t_atom_long* for x64 115 #pragma warning ( error : 4133 ) // incompatible types 116 117 #if defined(DISABLE_A_BUNCH_OF_WINDOWS_WARNINGS) 118 #pragma warning( disable : 4005 ) // macro redefinition 119 #pragma warning( disable : 4101 ) // unreferenced local 120 #pragma warning( disable : 4800 ) // forcing value to bool 'true' or 'false' 121 #pragma warning( disable : 4805 ) // unsafe mix of type 'BOOL' and type 'bool' 122 #pragma warning( disable : 4087 ) // 'function' declared with 'void' parameter list 123 #pragma warning( disable : 4068 ) // unknown pragma 124 #pragma warning( disable : 4244 ) // implicit larger to smaller type conversion (int + float) 125 #pragma warning( disable : 4245 ) // implicit unsigned/signed type conversion 126 #pragma warning( disable : 4305 ) // truncation from 'type1' to 'type2' (e.g. double->float) 127 #endif 128 129 #endif // WIN_VERSION 130 131 #include "ext_prefix_includes.h" 132 133 #ifdef calcoffset 134 // The ifdefs for this macro have been set up like this so that Doxygen can document this macro on a Mac [TAP] 135 #else 136 /** 137 Find byte offset of a named member of a struct, relative to the beginning of that struct. 138 @ingroup misc 139 @param x The name of the struct 140 @param y The name of the member 141 @return A pointer-sized integer representing the number of bytes into the struct where the member begins. 142 */ 143 #define calcoffset(x,y) offsetof(x, y) 144 #endif 145 146 #ifdef structmembersize 147 // The ifdefs for this macro have been set up like this so that Doxygen can document this macro on a Mac [TAP] 148 #else 149 /** 150 Find size of a named member of a struct. 151 @ingroup misc 152 @param structname The name of the struct 153 @param membername The name of the member 154 @return The size of the member of the struct. 155 */ 156 #define structmembersize(structname, membername) (sizeof(((structname*)0)->membername)) 157 #endif 158 159 // debug support 160 #if defined (__GNUC__) && defined(C74_X64) 161 #ifndef C74DebugBreak 162 #ifdef __x86_64__ 163 #define C74DebugBreak asm("int3") 164 #elif defined(__aarch64__) 165 #define C74DebugBreak __asm__ volatile(".inst 0xd4200000") 166 #else 167 #warning implement me 168 #endif 169 #endif 170 #endif 171 172 #ifdef WIN_VERSION 173 #define C74DebugBreak DebugBreak() 174 #endif 175 176 #ifndef C74_STR 177 #define C74_STR_HELPER(x) #x 178 #define C74_STR(x) C74_STR_HELPER(x) 179 #endif 180 181 #ifndef C74_ASSERT 182 #if defined(_DEBUG) || defined(DEBUG) 183 #define C74_ASSERT(condition) \ 184 if (!(condition)) { cpost("ASSERTION - failed condition: %s\n", C74_STR(condition)); C74DebugBreak; } 185 #else 186 #define C74_ASSERT(condition) 187 #endif 188 #endif 189 190 // the following are convenient before casting to a smaller size 191 // so the debug build will let you know if you are truncating unexpectedly 192 #define C74_ASSERT_FITS_CHAR(x) C74_ASSERT(((char)(x)) == (long long) (x)) 193 #define C74_ASSERT_FITS_UCHAR(x) C74_ASSERT(((unsigned char)(x)) == (unsigned long long) (x)) 194 #define C74_ASSERT_FITS_SHORT(x) C74_ASSERT(((short)(x)) == (long long) (x)) 195 #define C74_ASSERT_FITS_USHORT(x) C74_ASSERT(((unsigned short)(x)) == (unsigned long long) (x)) 196 #define C74_ASSERT_FITS_LONG(x) C74_ASSERT(((long)(x)) == (long long) (x)) 197 #define C74_ASSERT_FITS_ULONG(x) C74_ASSERT(((unsigned long)(x)) == (unsigned long long)(x)) 198 #define C74_ASSERT_FITS_INT32(x) C74_ASSERT(((t_int32)(x)) == (long long) (x)) 199 #define C74_ASSERT_FITS_UINT32(x) C74_ASSERT(((t_uint32)(x)) == (unsigned long long) (x)) 200 #define C74_ASSERT_FITS_PTR_INT(x) C74_ASSERT(((t_ptr_int)(x)) == (long long) (x)) 201 #define C74_ASSERT_FITS_PTR_UINT(x) C74_ASSERT(((t_ptr_uint)(x)) == (unsigned long long) (x)) 202 203 // C74_STATIC_ASSERT: generates a compile error if expression e is false 204 // Can be disabled by defining C74_ENABLE_STATIC_ASSERT to be 0 before including max headers 205 #if defined(_DEBUG) && !defined(C74_ENABLE_STATIC_ASSERT) 206 #define C74_ENABLE_STATIC_ASSERT 1 207 #endif 208 #if C74_ENABLE_STATIC_ASSERT 209 #if (__cplusplus >= 201103L) 210 #define C74_STATIC_ASSERT(e, m) static_assert(e, m) 211 #else 212 #define C74_STATIC_ASSERT(e, m) extern char __C74_STATIC_ASSERT__[(e)?1:-1]; 213 #endif 214 #else 215 #define C74_STATIC_ASSERT(e, m) 216 #endif 217 218 #if defined(C74_X64) && defined(WIN_VERSION) 219 // It used to be required to set two-byte packing of structures on windows. 220 // For x64 it is important to set packing to default. 221 typedef struct _testpackingstruct 222 { 223 t_uint8 firstmember; 224 t_int32 secondmember; // 225 } t_testpackingstruct; 226 227 // if external is still configured for two byte alignment this will cause a compile error 228 // in which case you should change the structure alignment to the compiler default 229 C74_STATIC_ASSERT( (calcoffset(t_testpackingstruct, secondmember) - calcoffset(t_testpackingstruct, firstmember)) > 2, "struct alignment must be set to default!" ); 230 231 #endif 232 233 #define C74_EXTERNAL_NOT_ON_X64(name) void ext_main(void *r) { error("%s: not supported on x64", name); } 234 #define C74_EXTERNAL_NOT_ON_X64_QUIET void ext_main(void *r) { return; } 235 236 #ifdef C74_POISON_FLAVOUR_MACROS 237 238 #ifndef WIN_VERSION 239 #pragma GCC poison RT PLUG MFL 240 #endif 241 242 #endif // #ifdef C74_POISON_FLAVOUR_MACROS 243 244 #endif // _EXT_PREFIX_H_