Subversion Repositories eduke32

Rev

Rev 5037 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5037 Rev 5038
1
// Compatibility declarations for things which might not be present in
1
// Compatibility declarations for things which might not be present in
2
// certain build environments. It also levels the playing field caused
2
// certain build environments. It also levels the playing field caused
3
// by different platforms.
3
// by different platforms.
4
4
5
#ifndef compat_h_
5
#ifndef compat_h_
6
#define compat_h_
6
#define compat_h_
7
7
8
#ifdef __GNUC__
8
#ifdef __GNUC__
9
# define EDUKE32_GCC_PREREQ(major, minor) (major < __GNUC__ || (major == __GNUC__ && minor <= __GNUC_MINOR__))
9
# define EDUKE32_GCC_PREREQ(major, minor) (major < __GNUC__ || (major == __GNUC__ && minor <= __GNUC_MINOR__))
10
#else
10
#else
11
# define EDUKE32_GCC_PREREQ(major, minor) 0
11
# define EDUKE32_GCC_PREREQ(major, minor) 0
12
#endif
12
#endif
13
13
14
#ifdef __clang__
14
#ifdef __clang__
15
# define EDUKE32_CLANG_PREREQ(major, minor) (major < __clang_major__ || (major == __clang_major__ && minor <= __clang_minor__))
15
# define EDUKE32_CLANG_PREREQ(major, minor) (major < __clang_major__ || (major == __clang_major__ && minor <= __clang_minor__))
16
#else
16
#else
17
# define EDUKE32_CLANG_PREREQ(major, minor) 0
17
# define EDUKE32_CLANG_PREREQ(major, minor) 0
18
#endif
18
#endif
19
#ifndef __has_builtin
19
#ifndef __has_builtin
20
# define __has_builtin(x) 0  // Compatibility with non-clang compilers.
20
# define __has_builtin(x) 0  // Compatibility with non-clang compilers.
21
#endif
21
#endif
22
#ifndef __has_feature
22
#ifndef __has_feature
23
# define __has_feature(x) 0  // Compatibility with non-clang compilers.
23
# define __has_feature(x) 0  // Compatibility with non-clang compilers.
24
#endif
24
#endif
25
#ifndef __has_extension
25
#ifndef __has_extension
26
# define __has_extension __has_feature // Compatibility with pre-3.0 compilers.
26
# define __has_extension __has_feature // Compatibility with pre-3.0 compilers.
27
#endif
27
#endif
28
28
29
#if !defined(_MSC_VER) || _MSC_FULL_VER < 180031101
29
#if !defined(_MSC_VER) || _MSC_FULL_VER < 180031101
30
#ifdef UNREFERENCED_PARAMETER
30
#ifdef UNREFERENCED_PARAMETER
31
#undef UNREFERENCED_PARAMETER
31
#undef UNREFERENCED_PARAMETER
32
#endif
32
#endif
33
33
34
#define UNREFERENCED_PARAMETER(x) x = x
34
#define UNREFERENCED_PARAMETER(x) x = x
35
#endif
35
#endif
36
36
37
#if defined __GNUC__ || defined __clang__
37
#if defined __GNUC__ || defined __clang__
38
# define ATTRIBUTE(attrlist) __attribute__(attrlist)
38
# define ATTRIBUTE(attrlist) __attribute__(attrlist)
39
#else
39
#else
40
# define ATTRIBUTE(attrlist)
40
# define ATTRIBUTE(attrlist)
41
#endif
41
#endif
42
42
43
#if !defined __clang__ && !defined USING_LTO
43
#if !defined __clang__ && !defined USING_LTO
44
# define ATTRIBUTE_OPTIMIZE(str) ATTRIBUTE((optimize(str)))
44
# define ATTRIBUTE_OPTIMIZE(str) ATTRIBUTE((optimize(str)))
45
#else
45
#else
46
# define ATTRIBUTE_OPTIMIZE(str)
46
# define ATTRIBUTE_OPTIMIZE(str)
47
#endif
47
#endif
48
48
49
#if defined __GNUC__ || __has_builtin(__builtin_expect)
49
#if defined __GNUC__ || __has_builtin(__builtin_expect)
50
#define EDUKE32_PREDICT_TRUE(x)       __builtin_expect(!!(x),1)
50
#define EDUKE32_PREDICT_TRUE(x)       __builtin_expect(!!(x),1)
51
#define EDUKE32_PREDICT_FALSE(x)     __builtin_expect(!!(x),0)
51
#define EDUKE32_PREDICT_FALSE(x)     __builtin_expect(!!(x),0)
52
#else
52
#else
53
#define EDUKE32_PREDICT_TRUE(x) (x)
53
#define EDUKE32_PREDICT_TRUE(x) (x)
54
#define EDUKE32_PREDICT_FALSE(x) (x)
54
#define EDUKE32_PREDICT_FALSE(x) (x)
55
#endif
55
#endif
56
56
57
#if EDUKE32_GCC_PREREQ(4,5)  || __has_builtin(__builtin_unreachable)
57
#if EDUKE32_GCC_PREREQ(4,5)  || __has_builtin(__builtin_unreachable)
58
#define EDUKE32_UNREACHABLE_SECTION(...)   __builtin_unreachable()
58
#define EDUKE32_UNREACHABLE_SECTION(...)   __builtin_unreachable()
59
#elif _MSC_VER
59
#elif _MSC_VER
60
#define EDUKE32_UNREACHABLE_SECTION(...)   __assume(0)
60
#define EDUKE32_UNREACHABLE_SECTION(...)   __assume(0)
61
#else
61
#else
62
#define EDUKE32_UNREACHABLE_SECTION(...) __VA_ARGS__
62
#define EDUKE32_UNREACHABLE_SECTION(...) __VA_ARGS__
63
#endif
63
#endif
64
64
65
#ifndef min
65
#ifndef min
66
#define min(x,y) ((x) < (y) ? (x) : (y))
66
#define min(x,y) ((x) < (y) ? (x) : (y))
67
#endif
67
#endif
68
#ifndef max
68
#ifndef max
69
#define max(x,y) ((x) > (y) ? (x) : (y))
69
#define max(x,y) ((x) > (y) ? (x) : (y))
70
#endif
70
#endif
71
71
-
 
72
#if defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __bsdi__ || defined __DragonFly__
-
 
73
# define EDUKE32_BSD
-
 
74
#endif
-
 
75
72
#if !defined __APPLE__ && (!defined EDUKE32_BSD || !__STDC__)
76
#if !defined __APPLE__ && (!defined EDUKE32_BSD || !__STDC__)
73
# include <malloc.h>
77
# include <malloc.h>
74
#endif
78
#endif
75
79
76
#ifdef _WIN32
80
#ifdef _WIN32
77
# define WIN32_LEAN_AND_MEAN
81
# define WIN32_LEAN_AND_MEAN
78
# include <windows.h>
82
# include <windows.h>
79
#endif
83
#endif
80
84
81
#ifdef __APPLE__
85
#ifdef __APPLE__
82
# include <TargetConditionals.h>
86
# include <TargetConditionals.h>
83
# if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
87
# if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
84
#  define EDUKE32_IOS
88
#  define EDUKE32_IOS
85
# else
89
# else
86
#  define EDUKE32_OSX
90
#  define EDUKE32_OSX
87
#  if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_3
91
#  if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_3
88
#   include <CoreFoundation/CoreFoundation.h>
92
#   include <CoreFoundation/CoreFoundation.h>
89
#  endif
93
#  endif
90
#  include <CoreServices/CoreServices.h>
94
#  include <CoreServices/CoreServices.h>
91
# endif
95
# endif
92
#endif
96
#endif
93
-
 
94
#if defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __bsdi__ || defined __DragonFly__
-
 
95
# define EDUKE32_BSD
-
 
96
#endif
-
 
97
97
98
#if defined __ANDROID__ || defined EDUKE32_IOS
98
#if defined __ANDROID__ || defined EDUKE32_IOS
99
# define EDUKE32_TOUCH_DEVICES
99
# define EDUKE32_TOUCH_DEVICES
100
# define EDUKE32_GLES
100
# define EDUKE32_GLES
101
#endif
101
#endif
102
102
103
// This gives us access to 'intptr_t' and 'uintptr_t', which are
103
// This gives us access to 'intptr_t' and 'uintptr_t', which are
104
// abstractions to the size of a pointer on a given platform
104
// abstractions to the size of a pointer on a given platform
105
// (ie, they're guaranteed to be the same size as a pointer)
105
// (ie, they're guaranteed to be the same size as a pointer)
106
106
107
#undef __USE_MINGW_ANSI_STDIO // Workaround for MinGW-w64.
107
#undef __USE_MINGW_ANSI_STDIO // Workaround for MinGW-w64.
108
108
109
#ifndef __STDC_FORMAT_MACROS
109
#ifndef __STDC_FORMAT_MACROS
110
#define __STDC_FORMAT_MACROS
110
#define __STDC_FORMAT_MACROS
111
#endif
111
#endif
112
#ifndef __STDC_LIMIT_MACROS
112
#ifndef __STDC_LIMIT_MACROS
113
# define __STDC_LIMIT_MACROS
113
# define __STDC_LIMIT_MACROS
114
#endif
114
#endif
115
#if defined(HAVE_INTTYPES) || defined(__cplusplus)
115
#if defined(HAVE_INTTYPES) || defined(__cplusplus)
116
# include <stdint.h>
116
# include <stdint.h>
117
# include <inttypes.h>
117
# include <inttypes.h>
118
118
119
// Ghetto. Blame devkitPPC's faulty headers.
119
// Ghetto. Blame devkitPPC's faulty headers.
120
# ifdef GEKKO
120
# ifdef GEKKO
121
#  undef PRIdPTR
121
#  undef PRIdPTR
122
#  define PRIdPTR "d"
122
#  define PRIdPTR "d"
123
#  undef PRIxPTR
123
#  undef PRIxPTR
124
#  define PRIxPTR "x"
124
#  define PRIxPTR "x"
125
#  undef SCNx32
125
#  undef SCNx32
126
#  define SCNx32 "x"
126
#  define SCNx32 "x"
127
# endif
127
# endif
128
128
129
#elif defined(_MSC_VER)
129
#elif defined(_MSC_VER)
130
# include "msvc/inttypes.h" // from http://code.google.com/p/msinttypes/
130
# include "msvc/inttypes.h" // from http://code.google.com/p/msinttypes/
131
#endif
131
#endif
132
132
133
#ifndef _MSC_VER
133
#ifndef _MSC_VER
134
#  ifndef __fastcall
134
#  ifndef __fastcall
135
#    if defined(__GNUC__) && defined(__i386__)
135
#    if defined(__GNUC__) && defined(__i386__)
136
#      define __fastcall __attribute__((fastcall))
136
#      define __fastcall __attribute__((fastcall))
137
#    else
137
#    else
138
#      define __fastcall
138
#      define __fastcall
139
#    endif
139
#    endif
140
#  endif
140
#  endif
141
#endif
141
#endif
142
142
143
#ifndef TRUE
143
#ifndef TRUE
144
#  define TRUE 1
144
#  define TRUE 1
145
#endif
145
#endif
146
146
147
#ifndef FALSE
147
#ifndef FALSE
148
#  define FALSE 0
148
#  define FALSE 0
149
#endif
149
#endif
150
150
151
#define WITHKPLIB
151
#define WITHKPLIB
152
152
153
#include "libdivide.h"
153
#include "libdivide.h"
154
154
155
// Define this to rewrite all 'B' versions to library functions. This
155
// Define this to rewrite all 'B' versions to library functions. This
156
// is for platforms which give us a standard sort of C library so we
156
// is for platforms which give us a standard sort of C library so we
157
// link directly. Platforms like PalmOS which don't have a standard C
157
// link directly. Platforms like PalmOS which don't have a standard C
158
// library will need to wrap these functions with suitable emulations.
158
// library will need to wrap these functions with suitable emulations.
159
#define compat_h_macrodef__
159
#define compat_h_macrodef__
160
160
161
#include <stdarg.h>
161
#include <stdarg.h>
162
#include <stddef.h>
162
#include <stddef.h>
163
163
164
#ifdef compat_h_macrodef__
164
#ifdef compat_h_macrodef__
165
# include <stdio.h>
165
# include <stdio.h>
166
# include <string.h>
166
# include <string.h>
167
# include <stdlib.h>
167
# include <stdlib.h>
168
# include <time.h>
168
# include <time.h>
169
# include <fcntl.h>
169
# include <fcntl.h>
170
# include <ctype.h>
170
# include <ctype.h>
171
# include <sys/types.h>
171
# include <sys/types.h>
172
# include <sys/stat.h>
172
# include <sys/stat.h>
173
# include <errno.h>
173
# include <errno.h>
174
# if defined(_WIN32)
174
# if defined(_WIN32)
175
#  include <io.h>
175
#  include <io.h>
176
# else
176
# else
177
#  include <unistd.h>
177
#  include <unistd.h>
178
# endif
178
# endif
179
#endif
179
#endif
180
180
181
#include <assert.h>
181
#include <assert.h>
182
182
183
#ifdef EFENCE
183
#ifdef EFENCE
184
# include <efence.h>
184
# include <efence.h>
185
#elif defined DMALLOC
185
#elif defined DMALLOC
186
# include <dmalloc.h>
186
# include <dmalloc.h>
187
#endif
187
#endif
188
188
189
#if defined(_MSC_VER)
189
#if defined(_MSC_VER)
190
# include <direct.h>
190
# include <direct.h>
191
# define longlong(x) x##i64
191
# define longlong(x) x##i64
192
# if _MSC_VER < 1800
192
# if _MSC_VER < 1800
193
#  define inline __inline
193
#  define inline __inline
194
# endif
194
# endif
195
#else
195
#else
196
# define longlong(x) x##ll
196
# define longlong(x) x##ll
197
#endif
197
#endif
198
198
199
#if defined(__arm__)
199
#if defined(__arm__)
200
# define Bsqrt __builtin_sqrt
200
# define Bsqrt __builtin_sqrt
201
# define Bsqrtf __builtin_sqrtf
201
# define Bsqrtf __builtin_sqrtf
202
#else
202
#else
203
# define Bsqrt sqrt
203
# define Bsqrt sqrt
204
# define Bsqrtf sqrtf
204
# define Bsqrtf sqrtf
205
#endif
205
#endif
206
206
207
#ifndef NULL
207
#ifndef NULL
208
# define NULL ((void *)0)
208
# define NULL ((void *)0)
209
#endif
209
#endif
210
210
211
#if DEBUGGINGAIDS>=2
211
#if DEBUGGINGAIDS>=2
212
# define DEBUG_MAIN_ARRAYS
212
# define DEBUG_MAIN_ARRAYS
213
#endif
213
#endif
214
214
215
#ifndef DISABLE_INLINING
215
#ifndef DISABLE_INLINING
216
# define EXTERN_INLINE static inline
216
# define EXTERN_INLINE static inline
217
# define EXTERN_INLINE_HEADER static inline
217
# define EXTERN_INLINE_HEADER static inline
218
#else
218
#else
219
# define EXTERN_INLINE __fastcall
219
# define EXTERN_INLINE __fastcall
220
# define EXTERN_INLINE_HEADER extern __fastcall
220
# define EXTERN_INLINE_HEADER extern __fastcall
221
#endif
221
#endif
222
222
223
#ifndef FORCE_INLINE
223
#ifndef FORCE_INLINE
224
# ifdef _MSC_VER    // Visual Studio
224
# ifdef _MSC_VER    // Visual Studio
225
#  define FORCE_INLINE static __forceinline
225
#  define FORCE_INLINE static __forceinline
226
# else
226
# else
227
#  ifdef __GNUC__
227
#  ifdef __GNUC__
228
#    define FORCE_INLINE static inline __attribute__((always_inline))
228
#    define FORCE_INLINE static inline __attribute__((always_inline))
229
#  else
229
#  else
230
#    define FORCE_INLINE static inline
230
#    define FORCE_INLINE static inline
231
#  endif
231
#  endif
232
# endif
232
# endif
233
#endif
233
#endif
234
234
235
#if !defined DEBUG_MAIN_ARRAYS
235
#if !defined DEBUG_MAIN_ARRAYS
236
# define HAVE_CLIPSHAPE_FEATURE
236
# define HAVE_CLIPSHAPE_FEATURE
237
#endif
237
#endif
238
238
239
// redefined for apple/ppc, which chokes on stderr when linking...
239
// redefined for apple/ppc, which chokes on stderr when linking...
240
#define ERRprintf(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
240
#define ERRprintf(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
241
241
242
#if defined(__linux)
242
#if defined(__linux)
243
# include <endian.h>
243
# include <endian.h>
244
# if __BYTE_ORDER == __LITTLE_ENDIAN
244
# if __BYTE_ORDER == __LITTLE_ENDIAN
245
#  define B_LITTLE_ENDIAN 1
245
#  define B_LITTLE_ENDIAN 1
246
#  define B_BIG_ENDIAN    0
246
#  define B_BIG_ENDIAN    0
247
# elif __BYTE_ORDER == __BIG_ENDIAN
247
# elif __BYTE_ORDER == __BIG_ENDIAN
248
#  define B_LITTLE_ENDIAN 0
248
#  define B_LITTLE_ENDIAN 0
249
#  define B_BIG_ENDIAN    1
249
#  define B_BIG_ENDIAN    1
250
# endif
250
# endif
251
# define B_ENDIAN_C_INLINE 1
251
# define B_ENDIAN_C_INLINE 1
252
252
253
#elif defined(GEKKO) || defined(__ANDROID__)
253
#elif defined(GEKKO) || defined(__ANDROID__)
254
# define B_LITTLE_ENDIAN 0
254
# define B_LITTLE_ENDIAN 0
255
# define B_BIG_ENDIAN 1
255
# define B_BIG_ENDIAN 1
256
# define B_ENDIAN_C_INLINE 1
256
# define B_ENDIAN_C_INLINE 1
257
257
258
#elif defined(__OpenBSD__)
258
#elif defined(__OpenBSD__)
259
# include <machine/endian.h>
259
# include <machine/endian.h>
260
# if _BYTE_ORDER == _LITTLE_ENDIAN
260
# if _BYTE_ORDER == _LITTLE_ENDIAN
261
#  define B_LITTLE_ENDIAN 1
261
#  define B_LITTLE_ENDIAN 1
262
#  define B_BIG_ENDIAN    0
262
#  define B_BIG_ENDIAN    0
263
# elif _BYTE_ORDER == _BIG_ENDIAN
263
# elif _BYTE_ORDER == _BIG_ENDIAN
264
#  define B_LITTLE_ENDIAN 0
264
#  define B_LITTLE_ENDIAN 0
265
#  define B_BIG_ENDIAN    1
265
#  define B_BIG_ENDIAN    1
266
# endif
266
# endif
267
# define B_SWAP64(x) __swap64(x)
267
# define B_SWAP64(x) __swap64(x)
268
# define B_SWAP32(x) __swap32(x)
268
# define B_SWAP32(x) __swap32(x)
269
# define B_SWAP16(x) __swap16(x)
269
# define B_SWAP16(x) __swap16(x)
270
270
271
#elif defined EDUKE32_BSD
271
#elif defined EDUKE32_BSD
272
# include <sys/endian.h>
272
# include <sys/endian.h>
273
# if _BYTE_ORDER == _LITTLE_ENDIAN
273
# if _BYTE_ORDER == _LITTLE_ENDIAN
274
#  define B_LITTLE_ENDIAN 1
274
#  define B_LITTLE_ENDIAN 1
275
#  define B_BIG_ENDIAN    0
275
#  define B_BIG_ENDIAN    0
276
# elif _BYTE_ORDER == _BIG_ENDIAN
276
# elif _BYTE_ORDER == _BIG_ENDIAN
277
#  define B_LITTLE_ENDIAN 0
277
#  define B_LITTLE_ENDIAN 0
278
#  define B_BIG_ENDIAN    1
278
#  define B_BIG_ENDIAN    1
279
# endif
279
# endif
280
# define B_SWAP64(x) __bswap64(x)
280
# define B_SWAP64(x) __bswap64(x)
281
# define B_SWAP32(x) __bswap32(x)
281
# define B_SWAP32(x) __bswap32(x)
282
# define B_SWAP16(x) __bswap16(x)
282
# define B_SWAP16(x) __bswap16(x)
283
283
284
#elif defined(__APPLE__)
284
#elif defined(__APPLE__)
285
# if !defined __x86_64__ && defined __GNUC__
285
# if !defined __x86_64__ && defined __GNUC__
286
// PK 20110617: is*() crashes for me in x86 code compiled from 64-bit, and gives link errors on ppc
286
// PK 20110617: is*() crashes for me in x86 code compiled from 64-bit, and gives link errors on ppc
287
//              This hack patches all occurences.
287
//              This hack patches all occurences.
288
#  define isdigit(ch) ({ int32_t c__dontuse_=ch; c__dontuse_>='0' && c__dontuse_<='9'; })
288
#  define isdigit(ch) ({ int32_t c__dontuse_=ch; c__dontuse_>='0' && c__dontuse_<='9'; })
289
#  define isalpha(ch) ({ int32_t c__dontuse2_=ch; (c__dontuse2_>='A' && c__dontuse2_<='Z') || (c__dontuse2_>='a' && c__dontuse2_<='z'); })
289
#  define isalpha(ch) ({ int32_t c__dontuse2_=ch; (c__dontuse2_>='A' && c__dontuse2_<='Z') || (c__dontuse2_>='a' && c__dontuse2_<='z'); })
290
#  define isalnum(ch2)  ({ int32_t c2__dontuse_=ch2; isalpha(c2__dontuse_) || isdigit(c2__dontuse_); })
290
#  define isalnum(ch2)  ({ int32_t c2__dontuse_=ch2; isalpha(c2__dontuse_) || isdigit(c2__dontuse_); })
291
#  if defined __BIG_ENDIAN__
291
#  if defined __BIG_ENDIAN__
292
#    define isspace(ch)  ({ int32_t c__dontuse_=ch; (c__dontuse_==' ' || c__dontuse_=='\t' || c__dontuse_=='\n' || c__dontuse_=='\v' || c__dontuse_=='\f' || c__dontuse_=='\r'); })
292
#    define isspace(ch)  ({ int32_t c__dontuse_=ch; (c__dontuse_==' ' || c__dontuse_=='\t' || c__dontuse_=='\n' || c__dontuse_=='\v' || c__dontuse_=='\f' || c__dontuse_=='\r'); })
293
#    define isprint(ch)  ({ int32_t c__dontuse_=ch; (c__dontuse_>=0x20 && c__dontuse_<0x7f); })
293
#    define isprint(ch)  ({ int32_t c__dontuse_=ch; (c__dontuse_>=0x20 && c__dontuse_<0x7f); })
294
#    undef ERRprintf
294
#    undef ERRprintf
295
#    define ERRprintf(fmt, ...) printf(fmt, ## __VA_ARGS__)
295
#    define ERRprintf(fmt, ...) printf(fmt, ## __VA_ARGS__)
296
#  endif
296
#  endif
297
# endif
297
# endif
298
# if defined(__LITTLE_ENDIAN__)
298
# if defined(__LITTLE_ENDIAN__)
299
#  define B_LITTLE_ENDIAN 1
299
#  define B_LITTLE_ENDIAN 1
300
#  define B_BIG_ENDIAN    0
300
#  define B_BIG_ENDIAN    0
301
# elif defined(__BIG_ENDIAN__)
301
# elif defined(__BIG_ENDIAN__)
302
#  define B_LITTLE_ENDIAN 0
302
#  define B_LITTLE_ENDIAN 0
303
#  define B_BIG_ENDIAN    1
303
#  define B_BIG_ENDIAN    1
304
# endif
304
# endif
305
# include <libkern/OSByteOrder.h>
305
# include <libkern/OSByteOrder.h>
306
# define B_SWAP64(x) OSSwapConstInt64(x)
306
# define B_SWAP64(x) OSSwapConstInt64(x)
307
# define B_SWAP32(x) OSSwapConstInt32(x)
307
# define B_SWAP32(x) OSSwapConstInt32(x)
308
# define B_SWAP16(x) OSSwapConstInt16(x)
308
# define B_SWAP16(x) OSSwapConstInt16(x)
309
309
310
#elif defined(__BEOS__)
310
#elif defined(__BEOS__)
311
# include <posix/endian.h>
311
# include <posix/endian.h>
312
# if LITTLE_ENDIAN != 0
312
# if LITTLE_ENDIAN != 0
313
#  define B_LITTLE_ENDIAN 1
313
#  define B_LITTLE_ENDIAN 1
314
#  define B_BIG_ENDIAN    0
314
#  define B_BIG_ENDIAN    0
315
# elif BIG_ENDIAN != 0
315
# elif BIG_ENDIAN != 0
316
#  define B_LITTLE_ENDIAN 0
316
#  define B_LITTLE_ENDIAN 0
317
#  define B_BIG_ENDIAN    1
317
#  define B_BIG_ENDIAN    1
318
# endif
318
# endif
319
# define B_ENDIAN_C_INLINE 1
319
# define B_ENDIAN_C_INLINE 1
320
320
321
#elif defined(__QNX__)
321
#elif defined(__QNX__)
322
# if defined __LITTLEENDIAN__
322
# if defined __LITTLEENDIAN__
323
#  define B_LITTLE_ENDIAN 1
323
#  define B_LITTLE_ENDIAN 1
324
#  define B_BIG_ENDIAN    0
324
#  define B_BIG_ENDIAN    0
325
# elif defined __BIGENDIAN__
325
# elif defined __BIGENDIAN__
326
#  define B_LITTLE_ENDIAN 0
326
#  define B_LITTLE_ENDIAN 0
327
#  define B_BIG_ENDIAN    1
327
#  define B_BIG_ENDIAN    1
328
# endif
328
# endif
329
# define B_ENDIAN_C_INLINE 1
329
# define B_ENDIAN_C_INLINE 1
330
330
331
#elif defined(__sun)
331
#elif defined(__sun)
332
# if defined _LITTLE_ENDIAN
332
# if defined _LITTLE_ENDIAN
333
#  define B_LITTLE_ENDIAN 1
333
#  define B_LITTLE_ENDIAN 1
334
#  define B_BIG_ENDIAN    0
334
#  define B_BIG_ENDIAN    0
335
# elif defined _BIG_ENDIAN
335
# elif defined _BIG_ENDIAN
336
#  define B_LITTLE_ENDIAN 0
336
#  define B_LITTLE_ENDIAN 0
337
#  define B_BIG_ENDIAN    1
337
#  define B_BIG_ENDIAN    1
338
# endif
338
# endif
339
# define B_ENDIAN_C_INLINE 1
339
# define B_ENDIAN_C_INLINE 1
340
340
341
#elif defined(_WIN32) || defined(SKYOS) || defined(__SYLLABLE__)
341
#elif defined(_WIN32) || defined(SKYOS) || defined(__SYLLABLE__)
342
# define B_LITTLE_ENDIAN 1
342
# define B_LITTLE_ENDIAN 1
343
# define B_BIG_ENDIAN    0
343
# define B_BIG_ENDIAN    0
344
# define B_ENDIAN_C_INLINE 1
344
# define B_ENDIAN_C_INLINE 1
345
#endif
345
#endif
346
346
347
#if !defined(B_LITTLE_ENDIAN) || !defined(B_BIG_ENDIAN)
347
#if !defined(B_LITTLE_ENDIAN) || !defined(B_BIG_ENDIAN)
348
# error Unknown endianness
348
# error Unknown endianness
349
#endif
349
#endif
350
350
351
#if defined _LP64 || defined __LP64__ || defined __64BIT__ || _ADDR64 || defined _WIN64 || defined __arch64__ ||       \
351
#if defined _LP64 || defined __LP64__ || defined __64BIT__ || _ADDR64 || defined _WIN64 || defined __arch64__ ||       \
352
__WORDSIZE == 64 || (defined __sparc && defined __sparcv9) || defined __x86_64 || defined __amd64 ||                   \
352
__WORDSIZE == 64 || (defined __sparc && defined __sparcv9) || defined __x86_64 || defined __amd64 ||                   \
353
defined __x86_64__ || defined __amd64__ || defined _M_X64 || defined _M_IA64 || defined __ia64 || defined __IA64__
353
defined __x86_64__ || defined __amd64__ || defined _M_X64 || defined _M_IA64 || defined __ia64 || defined __IA64__
354
354
355
# define BITNESS64
355
# define BITNESS64
356
356
357
#endif
357
#endif
358
358
359
#ifdef __cplusplus
359
#ifdef __cplusplus
360
360
361
# ifndef SCREWED_UP_CPP
361
# ifndef SCREWED_UP_CPP
362
//   using namespace std;
362
//   using namespace std;
363
# endif
363
# endif
364
364
365
extern "C" {
365
extern "C" {
366
#endif
366
#endif
367
367
368
#if defined B_ENDIAN_X86_INLINE
368
#if defined B_ENDIAN_X86_INLINE
369
# if defined(_MSC_VER)
369
# if defined(_MSC_VER)
370
        // inline asm using bswap/xchg
370
        // inline asm using bswap/xchg
371
# elif defined(__GNUC__)
371
# elif defined(__GNUC__)
372
        // inline asm using bswap/xchg
372
        // inline asm using bswap/xchg
373
# endif
373
# endif
374
#elif defined B_ENDIAN_C_INLINE
374
#elif defined B_ENDIAN_C_INLINE
375
FORCE_INLINE uint16_t B_SWAP16(uint16_t s) { return (s >> 8) | (s << 8); }
375
FORCE_INLINE uint16_t B_SWAP16(uint16_t s) { return (s >> 8) | (s << 8); }
376
FORCE_INLINE uint32_t B_SWAP32(uint32_t l)
376
FORCE_INLINE uint32_t B_SWAP32(uint32_t l)
377
{
377
{
378
    return ((l >> 8) & 0xff00) | ((l & 0xff00) << 8) | (l << 24) | (l >> 24);
378
    return ((l >> 8) & 0xff00) | ((l & 0xff00) << 8) | (l << 24) | (l >> 24);
379
}
379
}
380
FORCE_INLINE uint64_t B_SWAP64(uint64_t l)
380
FORCE_INLINE uint64_t B_SWAP64(uint64_t l)
381
{
381
{
382
    return (l >> 56) | ((l >> 40) & 0xff00) | ((l >> 24) & 0xff0000) | ((l >> 8) & 0xff000000) |
382
    return (l >> 56) | ((l >> 40) & 0xff00) | ((l >> 24) & 0xff0000) | ((l >> 8) & 0xff000000) |
383
        ((l & 255) << 56) | ((l & 0xff00) << 40) | ((l & 0xff0000) << 24) | ((l & 0xff000000) << 8);
383
        ((l & 255) << 56) | ((l & 0xff00) << 40) | ((l & 0xff0000) << 24) | ((l & 0xff000000) << 8);
384
}
384
}
385
#endif
385
#endif
386
386
387
FORCE_INLINE void B_BUF16(uint8_t *buf, uint16_t x)
387
FORCE_INLINE void B_BUF16(uint8_t *buf, uint16_t x)
388
{
388
{
389
    buf[0] = (x & 0x00FF);
389
    buf[0] = (x & 0x00FF);
390
    buf[1] = (x & 0xFF00) >> 8;
390
    buf[1] = (x & 0xFF00) >> 8;
391
}
391
}
392
FORCE_INLINE void B_BUF32(uint8_t *buf, uint32_t x)
392
FORCE_INLINE void B_BUF32(uint8_t *buf, uint32_t x)
393
{
393
{
394
    buf[0] = (x & 0x000000FF);
394
    buf[0] = (x & 0x000000FF);
395
    buf[1] = (x & 0x0000FF00) >> 8;
395
    buf[1] = (x & 0x0000FF00) >> 8;
396
    buf[2] = (x & 0x00FF0000) >> 16;
396
    buf[2] = (x & 0x00FF0000) >> 16;
397
    buf[3] = (x & 0xFF000000) >> 24;
397
    buf[3] = (x & 0xFF000000) >> 24;
398
}
398
}
399
#if 0
399
#if 0
400
// i686-apple-darwin11-llvm-gcc-4.2 complains "integer constant is too large for 'long' type"
400
// i686-apple-darwin11-llvm-gcc-4.2 complains "integer constant is too large for 'long' type"
401
FORCE_INLINE void B_BUF64(uint8_t *buf, uint64_t x)
401
FORCE_INLINE void B_BUF64(uint8_t *buf, uint64_t x)
402
{
402
{
403
    buf[0] = (x & 0x00000000000000FF);
403
    buf[0] = (x & 0x00000000000000FF);
404
    buf[1] = (x & 0x000000000000FF00) >> 8;
404
    buf[1] = (x & 0x000000000000FF00) >> 8;
405
    buf[2] = (x & 0x0000000000FF0000) >> 16;
405
    buf[2] = (x & 0x0000000000FF0000) >> 16;
406
    buf[3] = (x & 0x00000000FF000000) >> 24;
406
    buf[3] = (x & 0x00000000FF000000) >> 24;
407
    buf[4] = (x & 0x000000FF00000000) >> 32;
407
    buf[4] = (x & 0x000000FF00000000) >> 32;
408
    buf[5] = (x & 0x0000FF0000000000) >> 40;
408
    buf[5] = (x & 0x0000FF0000000000) >> 40;
409
    buf[6] = (x & 0x00FF000000000000) >> 48;
409
    buf[6] = (x & 0x00FF000000000000) >> 48;
410
    buf[7] = (x & 0xFF00000000000000) >> 56;
410
    buf[7] = (x & 0xFF00000000000000) >> 56;
411
}
411
}
412
#endif
412
#endif
413
413
414
FORCE_INLINE uint16_t B_UNBUF16(const uint8_t *buf) { return (buf[1] << 8) | (buf[0]); }
414
FORCE_INLINE uint16_t B_UNBUF16(const uint8_t *buf) { return (buf[1] << 8) | (buf[0]); }
415
FORCE_INLINE uint32_t B_UNBUF32(const uint8_t *buf)
415
FORCE_INLINE uint32_t B_UNBUF32(const uint8_t *buf)
416
{
416
{
417
    return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | (buf[0]);
417
    return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | (buf[0]);
418
}
418
}
419
FORCE_INLINE uint64_t B_UNBUF64(const uint8_t *buf)
419
FORCE_INLINE uint64_t B_UNBUF64(const uint8_t *buf)
420
{
420
{
421
    return ((uint64_t)buf[7] << 56) | ((uint64_t)buf[6] << 48) | ((uint64_t)buf[5] << 40) |
421
    return ((uint64_t)buf[7] << 56) | ((uint64_t)buf[6] << 48) | ((uint64_t)buf[5] << 40) |
422
        ((uint64_t)buf[4] << 32) | (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | (buf[0]);
422
        ((uint64_t)buf[4] << 32) | (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | (buf[0]);
423
}
423
}
424
424
425
#if defined BITNESS64 && (defined __SSE2__ || defined _MSC_VER)
425
#if defined BITNESS64 && (defined __SSE2__ || defined _MSC_VER)
426
#include <emmintrin.h>
426
#include <emmintrin.h>
427
FORCE_INLINE int32_t Blrintf(const float x)
427
FORCE_INLINE int32_t Blrintf(const float x)
428
{
428
{
429
    __m128 xx = _mm_load_ss(&x);
429
    __m128 xx = _mm_load_ss(&x);
430
    return _mm_cvtss_si32(xx);
430
    return _mm_cvtss_si32(xx);
431
}
431
}
432
#elif defined (_MSC_VER)
432
#elif defined (_MSC_VER)
433
FORCE_INLINE int32_t Blrintf(const float x)
433
FORCE_INLINE int32_t Blrintf(const float x)
434
{
434
{
435
    int n;
435
    int n;
436
    __asm fld x;
436
    __asm fld x;
437
    __asm fistp n;
437
    __asm fistp n;
438
    return n;
438
    return n;
439
}
439
}
440
#else
440
#else
441
#include <math.h>
441
#include <math.h>
442
#define Blrintf lrintf
442
#define Blrintf lrintf
443
#endif
443
#endif
444
444
445
#if B_LITTLE_ENDIAN == 1
445
#if B_LITTLE_ENDIAN == 1
446
# define B_LITTLE64(x) (x)
446
# define B_LITTLE64(x) (x)
447
# define B_BIG64(x)    B_SWAP64(x)
447
# define B_BIG64(x)    B_SWAP64(x)
448
# define B_LITTLE32(x) (x)
448
# define B_LITTLE32(x) (x)
449
# define B_BIG32(x)    B_SWAP32(x)
449
# define B_BIG32(x)    B_SWAP32(x)
450
# define B_LITTLE16(x) (x)
450
# define B_LITTLE16(x) (x)
451
# define B_BIG16(x)    B_SWAP16(x)
451
# define B_BIG16(x)    B_SWAP16(x)
452
#elif B_BIG_ENDIAN == 1
452
#elif B_BIG_ENDIAN == 1
453
# define B_LITTLE64(x) B_SWAP64(x)
453
# define B_LITTLE64(x) B_SWAP64(x)
454
# define B_BIG64(x)    (x)
454
# define B_BIG64(x)    (x)
455
# define B_LITTLE32(x) B_SWAP32(x)
455
# define B_LITTLE32(x) B_SWAP32(x)
456
# define B_BIG32(x)    (x)
456
# define B_BIG32(x)    (x)
457
# define B_LITTLE16(x) B_SWAP16(x)
457
# define B_LITTLE16(x) B_SWAP16(x)
458
# define B_BIG16(x)    (x)
458
# define B_BIG16(x)    (x)
459
#endif
459
#endif
460
460
461
#ifndef FP_OFF
461
#ifndef FP_OFF
462
# define FP_OFF(__p) ((uintptr_t)(__p))
462
# define FP_OFF(__p) ((uintptr_t)(__p))
463
#endif
463
#endif
464
464
465
#ifdef compat_h_macrodef__
465
#ifdef compat_h_macrodef__
466
466
467
# ifndef O_BINARY
467
# ifndef O_BINARY
468
#  define O_BINARY 0
468
#  define O_BINARY 0
469
# endif
469
# endif
470
# ifndef O_TEXT
470
# ifndef O_TEXT
471
#  define O_TEXT 0
471
#  define O_TEXT 0
472
# endif
472
# endif
473
473
474
# ifndef F_OK
474
# ifndef F_OK
475
#  define F_OK 0
475
#  define F_OK 0
476
# endif
476
# endif
477
477
478
# define BO_BINARY O_BINARY
478
# define BO_BINARY O_BINARY
479
# define BO_TEXT   O_TEXT
479
# define BO_TEXT   O_TEXT
480
# define BO_RDONLY O_RDONLY
480
# define BO_RDONLY O_RDONLY
481
# define BO_WRONLY O_WRONLY
481
# define BO_WRONLY O_WRONLY
482
# define BO_RDWR   O_RDWR
482
# define BO_RDWR   O_RDWR
483
# define BO_APPEND O_APPEND
483
# define BO_APPEND O_APPEND
484
# define BO_CREAT  O_CREAT
484
# define BO_CREAT  O_CREAT
485
# define BO_TRUNC  O_TRUNC
485
# define BO_TRUNC  O_TRUNC
486
# define BS_IRGRP  S_IRGRP
486
# define BS_IRGRP  S_IRGRP
487
# define BS_IWGRP  S_IWGRP
487
# define BS_IWGRP  S_IWGRP
488
# define BS_IEXEC  S_IEXEC
488
# define BS_IEXEC  S_IEXEC
489
# ifdef __ANDROID__
489
# ifdef __ANDROID__
490
#  define BS_IWRITE S_IWUSR
490
#  define BS_IWRITE S_IWUSR
491
#  define BS_IREAD  S_IRUSR
491
#  define BS_IREAD  S_IRUSR
492
# else
492
# else
493
#  define BS_IWRITE S_IWRITE
493
#  define BS_IWRITE S_IWRITE
494
#  define BS_IREAD  S_IREAD
494
#  define BS_IREAD  S_IREAD
495
# endif
495
# endif
496
# define BS_IFIFO  S_IFIFO
496
# define BS_IFIFO  S_IFIFO
497
# define BS_IFCHR  S_IFCHR
497
# define BS_IFCHR  S_IFCHR
498
# define BS_IFBLK  S_IFBLK
498
# define BS_IFBLK  S_IFBLK
499
# define BS_IFDIR  S_IFDIR
499
# define BS_IFDIR  S_IFDIR
500
# define BS_IFREG  S_IFREG
500
# define BS_IFREG  S_IFREG
501
# define BSEEK_SET SEEK_SET
501
# define BSEEK_SET SEEK_SET
502
# define BSEEK_CUR SEEK_CUR
502
# define BSEEK_CUR SEEK_CUR
503
# define BSEEK_END SEEK_END
503
# define BSEEK_END SEEK_END
504
#else
504
#else
505
# define BO_BINARY 0
505
# define BO_BINARY 0
506
# define BO_TEXT   1
506
# define BO_TEXT   1
507
# define BO_RDONLY 2
507
# define BO_RDONLY 2
508
# define BO_WRONLY 4
508
# define BO_WRONLY 4
509
# define BO_RDWR   6
509
# define BO_RDWR   6
510
# define BO_APPEND 8
510
# define BO_APPEND 8
511
# define BO_CREAT  16
511
# define BO_CREAT  16
512
# define BO_TRUNC  32
512
# define BO_TRUNC  32
513
# define BS_IRGRP  0
513
# define BS_IRGRP  0
514
# define BS_IWGRP  0
514
# define BS_IWGRP  0
515
# define BS_IEXEC  1
515
# define BS_IEXEC  1
516
# define BS_IWRITE 2
516
# define BS_IWRITE 2
517
# define BS_IREAD  4
517
# define BS_IREAD  4
518
# define BS_IFIFO  0x1000
518
# define BS_IFIFO  0x1000
519
# define BS_IFCHR  0x2000
519
# define BS_IFCHR  0x2000
520
# define BS_IFBLK  0x3000
520
# define BS_IFBLK  0x3000
521
# define BS_IFDIR  0x4000
521
# define BS_IFDIR  0x4000
522
# define BS_IFREG  0x8000
522
# define BS_IFREG  0x8000
523
# define BSEEK_SET 0
523
# define BSEEK_SET 0
524
# define BSEEK_CUR 1
524
# define BSEEK_CUR 1
525
# define BSEEK_END 2
525
# define BSEEK_END 2
526
#endif
526
#endif
527
527
528
#ifdef UNDERSCORES
528
#ifdef UNDERSCORES
529
# define ASMSYM(x) "_" x
529
# define ASMSYM(x) "_" x
530
#else
530
#else
531
# define ASMSYM(x) x
531
# define ASMSYM(x) x
532
#endif
532
#endif
533
533
534
#ifndef min
534
#ifndef min
535
# define min(a, b) (((a) < (b)) ? (a) : (b))
535
# define min(a, b) (((a) < (b)) ? (a) : (b))
536
#endif
536
#endif
537
537
538
#ifndef max
538
#ifndef max
539
# define max(a, b) (((a) > (b)) ? (a) : (b))
539
# define max(a, b) (((a) > (b)) ? (a) : (b))
540
#endif
540
#endif
541
541
542
#if __GNUC__ >= 4
542
#if __GNUC__ >= 4
543
# define CLAMP_DECL FORCE_INLINE __attribute__((warn_unused_result))
543
# define CLAMP_DECL FORCE_INLINE __attribute__((warn_unused_result))
544
#else
544
#else
545
# define CLAMP_DECL FORCE_INLINE
545
# define CLAMP_DECL FORCE_INLINE
546
#endif
546
#endif
547
547
548
// Clamp <in> to [<min>..<max>]. The case in <= min is handled first.
548
// Clamp <in> to [<min>..<max>]. The case in <= min is handled first.
549
CLAMP_DECL int32_t clamp(int32_t in, int32_t min, int32_t max) { return in <= min ? min : (in >= max ? max : in); }
549
CLAMP_DECL int32_t clamp(int32_t in, int32_t min, int32_t max) { return in <= min ? min : (in >= max ? max : in); }
550
550
551
// Clamp <in> to [<min>..<max>]. The case in >= max is handled first.
551
// Clamp <in> to [<min>..<max>]. The case in >= max is handled first.
552
CLAMP_DECL int32_t clamp2(int32_t in, int32_t min, int32_t max) { return in >= max ? max : (in <= min ? min : in); }
552
CLAMP_DECL int32_t clamp2(int32_t in, int32_t min, int32_t max) { return in >= max ? max : (in <= min ? min : in); }
553
553
554
// Clamp <in> to [<min>..<max>]. The case in <= min is handled first.
554
// Clamp <in> to [<min>..<max>]. The case in <= min is handled first.
555
CLAMP_DECL float fclamp(float in, float min, float max) { return in <= min ? min : (in >= max ? max : in); }
555
CLAMP_DECL float fclamp(float in, float min, float max) { return in <= min ? min : (in >= max ? max : in); }
556
556
557
// Clamp <in> to [<min>..<max>]. The case in >= max is handled first.
557
// Clamp <in> to [<min>..<max>]. The case in >= max is handled first.
558
CLAMP_DECL float fclamp2(float in, float min, float max) { return in >= max ? max : (in <= min ? min : in); }
558
CLAMP_DECL float fclamp2(float in, float min, float max) { return in >= max ? max : (in <= min ? min : in); }
559
559
560
#define BMAX_PATH 256
560
#define BMAX_PATH 256
561
561
562
562
563
struct Bdirent
563
struct Bdirent
564
{
564
{
565
    uint16_t namlen;
565
    uint16_t namlen;
566
    char *name;
566
    char *name;
567
    uint32_t mode;
567
    uint32_t mode;
568
    uint32_t size;
568
    uint32_t size;
569
    uint32_t mtime;
569
    uint32_t mtime;
570
};
570
};
571
typedef void BDIR;
571
typedef void BDIR;
572
572
573
BDIR *Bopendir(const char *name);
573
BDIR *Bopendir(const char *name);
574
struct Bdirent *Breaddir(BDIR *dir);
574
struct Bdirent *Breaddir(BDIR *dir);
575
int32_t Bclosedir(BDIR *dir);
575
int32_t Bclosedir(BDIR *dir);
576
576
577
#ifdef _MSC_VER
577
#ifdef _MSC_VER
578
typedef intptr_t ssize_t;
578
typedef intptr_t ssize_t;
579
#endif
579
#endif
580
580
581
#ifdef compat_h_macrodef__
581
#ifdef compat_h_macrodef__
582
  typedef FILE BFILE;
582
  typedef FILE BFILE;
583
# define bsize_t size_t
583
# define bsize_t size_t
584
# define bssize_t ssize_t
584
# define bssize_t ssize_t
585
#else
585
#else
586
  typedef void          BFILE;
586
  typedef void          BFILE;
587
  typedef uint32_t bsize_t;
587
  typedef uint32_t bsize_t;
588
  typedef int32_t bssize_t;
588
  typedef int32_t bssize_t;
589
#endif
589
#endif
590
590
591
591
592
typedef struct {
592
typedef struct {
593
    int32_t x, y;
593
    int32_t x, y;
594
} vec2_t;
594
} vec2_t;
595
595
596
typedef struct {
596
typedef struct {
597
    int32_t x, y, z;
597
    int32_t x, y, z;
598
} vec3_t;
598
} vec3_t;
599
599
600
typedef struct {
600
typedef struct {
601
    float x, y;
601
    float x, y;
602
} vec2f_t;
602
} vec2f_t;
603
603
604
typedef struct {
604
typedef struct {
605
    float x, y, z;
605
    float x, y, z;
606
} vec3f_t;
606
} vec3f_t;
607
607
608
608
609
#if RAND_MAX == 32767
609
#if RAND_MAX == 32767
610
FORCE_INLINE uint16_t system_15bit_rand(void) { return (uint16_t)rand(); }
610
FORCE_INLINE uint16_t system_15bit_rand(void) { return (uint16_t)rand(); }
611
#else  // RAND_MAX > 32767, assumed to be of the form 2^k - 1
611
#else  // RAND_MAX > 32767, assumed to be of the form 2^k - 1
612
FORCE_INLINE uint16_t system_15bit_rand(void) { return ((uint16_t)rand())&0x7fff; }
612
FORCE_INLINE uint16_t system_15bit_rand(void) { return ((uint16_t)rand())&0x7fff; }
613
#endif
613
#endif
614
614
615
#if defined(_MSC_VER)
615
#if defined(_MSC_VER)
616
// XXX: non-__compat_h_macrodef__ version?
616
// XXX: non-__compat_h_macrodef__ version?
617
#define strtoll _strtoi64
617
#define strtoll _strtoi64
618
#endif
618
#endif
619
619
620
#ifdef compat_h_macrodef__
620
#ifdef compat_h_macrodef__
621
# define Bassert assert
621
# define Bassert assert
622
# define Brand rand
622
# define Brand rand
623
# define Balloca alloca
623
# define Balloca alloca
624
# define Bmalloc malloc
624
# define Bmalloc malloc
625
# define Bcalloc calloc
625
# define Bcalloc calloc
626
# define Brealloc realloc
626
# define Brealloc realloc
627
# define Bfree free
627
# define Bfree free
628
# if defined(__cplusplus) && defined(_MSC_VER)
628
# if defined(__cplusplus) && defined(_MSC_VER)
629
#  define Bstrdup _strdup
629
#  define Bstrdup _strdup
630
#  define Bchdir _chdir
630
#  define Bchdir _chdir
631
#  define Bgetcwd _getcwd
631
#  define Bgetcwd _getcwd
632
# else
632
# else
633
#  define Bstrdup strdup
633
#  define Bstrdup strdup
634
#  define Bchdir chdir
634
#  define Bchdir chdir
635
#  define Bgetcwd getcwd
635
#  define Bgetcwd getcwd
636
# endif
636
# endif
637
# define Bopen open
637
# define Bopen open
638
# define Bclose close
638
# define Bclose close
639
# define Bwrite write
639
# define Bwrite write
640
# define Bread read
640
# define Bread read
641
# define Blseek lseek
641
# define Blseek lseek
642
# if defined(__GNUC__)
642
# if defined(__GNUC__)
643
#  define Btell(h) lseek(h,0,SEEK_CUR)
643
#  define Btell(h) lseek(h,0,SEEK_CUR)
644
# else
644
# else
645
#  define Btell tell
645
#  define Btell tell
646
# endif
646
# endif
647
# ifdef _MSC_VER
647
# ifdef _MSC_VER
648
#  define Bstat stat
648
#  define Bstat stat
649
#  define Bfstat fstat
649
#  define Bfstat fstat
650
# else
650
# else
651
#  define Bstat stat
651
#  define Bstat stat
652
#  define Bfstat fstat
652
#  define Bfstat fstat
653
# endif
653
# endif
654
# define Bfileno fileno
654
# define Bfileno fileno
655
# define Bferror ferror
655
# define Bferror ferror
656
# define Bfopen fopen
656
# define Bfopen fopen
657
# define Bfclose fclose
657
# define Bfclose fclose
658
# define Bfflush fflush
658
# define Bfflush fflush
659
# define Bfeof feof
659
# define Bfeof feof
660
# define Bfgetc fgetc
660
# define Bfgetc fgetc
661
# define Brewind rewind
661
# define Brewind rewind
662
# define Bfgets fgets
662
# define Bfgets fgets
663
# define Bfputc fputc
663
# define Bfputc fputc
664
# define Bfputs fputs
664
# define Bfputs fputs
665
# define Bfread fread
665
# define Bfread fread
666
# define Bfwrite fwrite
666
# define Bfwrite fwrite
667
# define Bfprintf fprintf
667
# define Bfprintf fprintf
668
# define Bfscanf fscanf
668
# define Bfscanf fscanf
669
# define Bfseek fseek
669
# define Bfseek fseek
670
# define Bftell ftell
670
# define Bftell ftell
671
# define Bputs puts
671
# define Bputs puts
672
# define Bstrcpy strcpy
672
# define Bstrcpy strcpy
673
# define Bstrncpy strncpy
673
# define Bstrncpy strncpy
674
# define Bstrcmp strcmp
674
# define Bstrcmp strcmp
675
# define Bstrncmp strncmp
675
# define Bstrncmp strncmp
676
# if defined(_MSC_VER)
676
# if defined(_MSC_VER)
677
#  define Bstrcasecmp _stricmp
677
#  define Bstrcasecmp _stricmp
678
#  define Bstrncasecmp _strnicmp
678
#  define Bstrncasecmp _strnicmp
679
# elif defined(__QNX__)
679
# elif defined(__QNX__)
680
#  define Bstrcasecmp stricmp
680
#  define Bstrcasecmp stricmp
681
#  define Bstrncasecmp strnicmp
681
#  define Bstrncasecmp strnicmp
682
# else
682
# else
683
#  define Bstrcasecmp strcasecmp
683
#  define Bstrcasecmp strcasecmp
684
#  define Bstrncasecmp strncasecmp
684
#  define Bstrncasecmp strncasecmp
685
# endif
685
# endif
686
# if defined(_WIN32)
686
# if defined(_WIN32)
687
#  define Bstrlwr strlwr
687
#  define Bstrlwr strlwr
688
#  define Bstrupr strupr
688
#  define Bstrupr strupr
689
#  define Bmkdir(s,x) mkdir(s)
689
#  define Bmkdir(s,x) mkdir(s)
690
# else
690
# else
691
#  define Bmkdir mkdir
691
#  define Bmkdir mkdir
692
# endif
692
# endif
693
# define Bstrcat strcat
693
# define Bstrcat strcat
694
# define Bstrncat strncat
694
# define Bstrncat strncat
695
# define Bstrlen strlen
695
# define Bstrlen strlen
696
# define Bstrchr strchr
696
# define Bstrchr strchr
697
# define Bstrrchr strrchr
697
# define Bstrrchr strrchr
698
// XXX: different across 32- and 64-bit archs (e.g.
698
// XXX: different across 32- and 64-bit archs (e.g.
699
// parsing the decimal representation of 0xffffffff,
699
// parsing the decimal representation of 0xffffffff,
700
// 4294967295 -- long is signed, so strtol would
700
// 4294967295 -- long is signed, so strtol would
701
// return LONG_MAX (== 0x7fffffff on 32-bit archs))
701
// return LONG_MAX (== 0x7fffffff on 32-bit archs))
702
# define Batoi(str) ((int32_t)strtol(str, NULL, 10))
702
# define Batoi(str) ((int32_t)strtol(str, NULL, 10))
703
# define Batol(str) (strtol(str, NULL, 10))
703
# define Batol(str) (strtol(str, NULL, 10))
704
# define Batof(str) (strtod(str, NULL))
704
# define Batof(str) (strtod(str, NULL))
705
# define Bstrtol strtol
705
# define Bstrtol strtol
706
# define Bstrtoul strtoul
706
# define Bstrtoul strtoul
707
# define Bstrtod strtod
707
# define Bstrtod strtod
708
# define Bstrstr strstr
708
# define Bstrstr strstr
709
# define Bislower islower
709
# define Bislower islower
710
# define Bisupper isupper
710
# define Bisupper isupper
711
# define Bisdigit isdigit
711
# define Bisdigit isdigit
712
# define Btoupper toupper
712
# define Btoupper toupper
713
# define Btolower tolower
713
# define Btolower tolower
714
# define Bmemcpy memcpy
714
# define Bmemcpy memcpy
715
# define Bmemmove memmove
715
# define Bmemmove memmove
716
# define Bmemchr memchr
716
# define Bmemchr memchr
717
# define Bmemset memset
717
# define Bmemset memset
718
# define Bmemcmp memcmp
718
# define Bmemcmp memcmp
719
# define Bscanf scanf
719
# define Bscanf scanf
720
# define Bprintf printf
720
# define Bprintf printf
721
# define Bsscanf sscanf
721
# define Bsscanf sscanf
722
# define Bsprintf sprintf
722
# define Bsprintf sprintf
723
# ifdef _MSC_VER
723
# ifdef _MSC_VER
724
#  define Bsnprintf _snprintf
724
#  define Bsnprintf _snprintf
725
#  define Bvsnprintf _vsnprintf
725
#  define Bvsnprintf _vsnprintf
726
# else
726
# else
727
#  define Bsnprintf snprintf
727
#  define Bsnprintf snprintf
728
#  define Bvsnprintf vsnprintf
728
#  define Bvsnprintf vsnprintf
729
# endif
729
# endif
730
# define Bvfprintf vfprintf
730
# define Bvfprintf vfprintf
731
# define Bgetenv getenv
731
# define Bgetenv getenv
732
# define Btime() time(NULL)
732
# define Btime() time(NULL)
733
# define Butime utime
733
# define Butime utime
734
734
735
#else
735
#else
736
736
737
void Bassert(int);
737
void Bassert(int);
738
int32_t Brand(void);
738
int32_t Brand(void);
739
void *Bmalloc(bsize_t size);
739
void *Bmalloc(bsize_t size);
740
void Bfree(void *ptr);
740
void Bfree(void *ptr);
741
int32_t Bopen(const char *pathname, int32_t flags, unsigned mode);
741
int32_t Bopen(const char *pathname, int32_t flags, unsigned mode);
742
int32_t Bclose(int32_t fd);
742
int32_t Bclose(int32_t fd);
743
bssize_t Bwrite(int32_t fd, const void *buf, bsize_t count);
743
bssize_t Bwrite(int32_t fd, const void *buf, bsize_t count);
744
bssize_t Bread(int32_t fd, void *buf, bsize_t count);
744
bssize_t Bread(int32_t fd, void *buf, bsize_t count);
745
int32_t Blseek(int32_t fildes, int32_t offset, int32_t whence);
745
int32_t Blseek(int32_t fildes, int32_t offset, int32_t whence);
746
BFILE *Bfopen(const char *path, const char *mode);
746
BFILE *Bfopen(const char *path, const char *mode);
747
int32_t Bfclose(BFILE *stream);
747
int32_t Bfclose(BFILE *stream);
748
int32_t Bfeof(BFILE *stream);
748
int32_t Bfeof(BFILE *stream);
749
int32_t Bfgetc(BFILE *stream);
749
int32_t Bfgetc(BFILE *stream);
750
void Brewind(BFILE *stream);
750
void Brewind(BFILE *stream);
751
char *Bfgets(char *s, int32_t size, BFILE *stream);
751
char *Bfgets(char *s, int32_t size, BFILE *stream);
752
int32_t Bfputc(int32_t c, BFILE *stream);
752
int32_t Bfputc(int32_t c, BFILE *stream);
753
int32_t Bfputs(const char *s, BFILE *stream);
753
int32_t Bfputs(const char *s, BFILE *stream);
754
bsize_t Bfread(void *ptr, bsize_t size, bsize_t nmemb, BFILE *stream);
754
bsize_t Bfread(void *ptr, bsize_t size, bsize_t nmemb, BFILE *stream);
755
bsize_t Bfwrite(const void *ptr, bsize_t size, bsize_t nmemb, BFILE *stream);
755
bsize_t Bfwrite(const void *ptr, bsize_t size, bsize_t nmemb, BFILE *stream);
756
char *Bstrdup(const char *s);
756
char *Bstrdup(const char *s);
757
char *Bstrcpy(char *dest, const char *src);
757
char *Bstrcpy(char *dest, const char *src);
758
char *Bstrncpy(char *dest, const char *src, bsize_t n);
758
char *Bstrncpy(char *dest, const char *src, bsize_t n);
759
int32_t Bstrcmp(const char *s1, const char *s2);
759
int32_t Bstrcmp(const char *s1, const char *s2);
760
int32_t Bstrncmp(const char *s1, const char *s2, bsize_t n);
760
int32_t Bstrncmp(const char *s1, const char *s2, bsize_t n);
761
int32_t Bstrcasecmp(const char *s1, const char *s2);
761
int32_t Bstrcasecmp(const char *s1, const char *s2);
762
int32_t Bstrncasecmp(const char *s1, const char *s2, bsize_t n);
762
int32_t Bstrncasecmp(const char *s1, const char *s2, bsize_t n);
763
char *Bstrcat(char *dest, const char *src);
763
char *Bstrcat(char *dest, const char *src);
764
char *Bstrncat(char *dest, const char *src, bsize_t n);
764
char *Bstrncat(char *dest, const char *src, bsize_t n);
765
bsize_t Bstrlen(const char *s);
765
bsize_t Bstrlen(const char *s);
766
char *Bstrchr(const char *s, int32_t c);
766
char *Bstrchr(const char *s, int32_t c);
767
char *Bstrrchr(const char *s, int32_t c);
767
char *Bstrrchr(const char *s, int32_t c);
768
int32_t Batoi(const char *nptr);
768
int32_t Batoi(const char *nptr);
769
int32_t Batol(const char *nptr);
769
int32_t Batol(const char *nptr);
770
int32_t Bstrtol(const char *nptr, char **endptr, int32_t base);
770
int32_t Bstrtol(const char *nptr, char **endptr, int32_t base);
771
uint32_t Bstrtoul(const char *nptr, char **endptr, int32_t base);
771
uint32_t Bstrtoul(const char *nptr, char **endptr, int32_t base);
772
void *Bmemcpy(void *dest, const void *src, bsize_t n);
772
void *Bmemcpy(void *dest, const void *src, bsize_t n);
773
void *Bmemmove(void *dest, const void *src, bsize_t n);
773
void *Bmemmove(void *dest, const void *src, bsize_t n);
774
void *Bmemchr(const void *s, int32_t c, bsize_t n);
774
void *Bmemchr(const void *s, int32_t c, bsize_t n);
775
void *Bmemset(void *s, int32_t c, bsize_t n);
775
void *Bmemset(void *s, int32_t c, bsize_t n);
776
int32_t Bmemcmp(const void *s1, const void *s2, bsize_t n);
776
int32_t Bmemcmp(const void *s1, const void *s2, bsize_t n);
777
int32_t Bprintf(const char *format, ...) ATTRIBUTE((format(printf, 1, 2)));
777
int32_t Bprintf(const char *format, ...) ATTRIBUTE((format(printf, 1, 2)));
778
int32_t Bsprintf(char *str, const char *format, ...) ATTRIBUTE((format(printf, 2, 3)));
778
int32_t Bsprintf(char *str, const char *format, ...) ATTRIBUTE((format(printf, 2, 3)));
779
int32_t Bsnprintf(char *str, bsize_t size, const char *format, ...) ATTRIBUTE((format(printf, 3, 4)));
779
int32_t Bsnprintf(char *str, bsize_t size, const char *format, ...) ATTRIBUTE((format(printf, 3, 4)));
780
int32_t Bvsnprintf(char *str, bsize_t size, const char *format, va_list ap);
780
int32_t Bvsnprintf(char *str, bsize_t size, const char *format, va_list ap);
781
char *Bgetcwd(char *buf, bsize_t size);
781
char *Bgetcwd(char *buf, bsize_t size);
782
char *Bgetenv(const char *name);
782
char *Bgetenv(const char *name);
783
#endif
783
#endif
784
784
785
char *Bgethomedir(void);
785
char *Bgethomedir(void);
786
char *Bgetappdir(void);
786
char *Bgetappdir(void);
787
uint32_t Bgetsysmemsize(void);
787
uint32_t Bgetsysmemsize(void);
788
int32_t Bcorrectfilename(char *filename, int32_t removefn);
788
int32_t Bcorrectfilename(char *filename, int32_t removefn);
789
int32_t Bcanonicalisefilename(char *filename, int32_t removefn);
789
int32_t Bcanonicalisefilename(char *filename, int32_t removefn);
790
char *Bgetsystemdrives(void);
790
char *Bgetsystemdrives(void);
791
int32_t Bfilelength(int32_t fd);
791
int32_t Bfilelength(int32_t fd);
792
char *Bstrtoken(char *s, const char *delim, char **ptrptr, int chop);
792
char *Bstrtoken(char *s, const char *delim, char **ptrptr, int chop);
793
char *Bstrtolower(char *str);
793
char *Bstrtolower(char *str);
794
#define Bwildmatch wildmatch
794
#define Bwildmatch wildmatch
795
795
796
#if !defined(_WIN32)
796
#if !defined(_WIN32)
797
char *Bstrlwr(char *);
797
char *Bstrlwr(char *);
798
char *Bstrupr(char *);
798
char *Bstrupr(char *);
799
#endif
799
#endif
800
800
801
// Copy min(strlen(src)+1, n) characters into dst, always terminate with a NUL.
801
// Copy min(strlen(src)+1, n) characters into dst, always terminate with a NUL.
802
FORCE_INLINE char *Bstrncpyz(char *dst, const char *src, bsize_t n)
802
FORCE_INLINE char *Bstrncpyz(char *dst, const char *src, bsize_t n)
803
{
803
{
804
    Bstrncpy(dst, src, n);
804
    Bstrncpy(dst, src, n);
805
    dst[n-1] = 0;
805
    dst[n-1] = 0;
806
    return dst;
806
    return dst;
807
}
807
}
808
808
809
// Append extension when <outbuf> contains no dot.
809
// Append extension when <outbuf> contains no dot.
810
// <ext> can be like ".mhk" or like "_crash.map", no need to start with a dot.
810
// <ext> can be like ".mhk" or like "_crash.map", no need to start with a dot.
811
// The ugly name is deliberate: we should be checking the sizes of all buffers!
811
// The ugly name is deliberate: we should be checking the sizes of all buffers!
812
static inline void append_ext_UNSAFE(char *outbuf, const char *ext)
812
static inline void append_ext_UNSAFE(char *outbuf, const char *ext)
813
{
813
{
814
    char *p = Bstrrchr(outbuf,'.');
814
    char *p = Bstrrchr(outbuf,'.');
815
815
816
    if (!p)
816
    if (!p)
817
        Bstrcat(outbuf, ext);
817
        Bstrcat(outbuf, ext);
818
    else
818
    else
819
        Bstrcpy(p, ext);
819
        Bstrcpy(p, ext);
820
}
820
}
821
821
822
#ifdef DEBUGGINGAIDS
822
#ifdef DEBUGGINGAIDS
823
extern void xalloc_set_location(int32_t line, const char *file, const char *func);
823
extern void xalloc_set_location(int32_t line, const char *file, const char *func);
824
#endif
824
#endif
825
void set_memerr_handler(void (*handlerfunc)(int32_t, const char *, const char *));
825
void set_memerr_handler(void (*handlerfunc)(int32_t, const char *, const char *));
826
void handle_memerr(void);
826
void handle_memerr(void);
827
827
828
FORCE_INLINE char *xstrdup(const char *s)
828
FORCE_INLINE char *xstrdup(const char *s)
829
{
829
{
830
    char *ptr = Bstrdup(s);
830
    char *ptr = Bstrdup(s);
831
    if (ptr == NULL) handle_memerr();
831
    if (ptr == NULL) handle_memerr();
832
    return ptr;
832
    return ptr;
833
}
833
}
834
834
835
FORCE_INLINE void *xmalloc(const bsize_t size)
835
FORCE_INLINE void *xmalloc(const bsize_t size)
836
{
836
{
837
    void *ptr = Bmalloc(size);
837
    void *ptr = Bmalloc(size);
838
    if (ptr == NULL) handle_memerr();
838
    if (ptr == NULL) handle_memerr();
839
    return ptr;
839
    return ptr;
840
}
840
}
841
841
842
FORCE_INLINE void *xcalloc(const bsize_t nmemb, const bsize_t size)
842
FORCE_INLINE void *xcalloc(const bsize_t nmemb, const bsize_t size)
843
{
843
{
844
    void *ptr = Bcalloc(nmemb, size);
844
    void *ptr = Bcalloc(nmemb, size);
845
    if (ptr == NULL) handle_memerr();
845
    if (ptr == NULL) handle_memerr();
846
    return ptr;
846
    return ptr;
847
}
847
}
848
848
849
FORCE_INLINE void *xrealloc(void * const ptr, const bsize_t size)
849
FORCE_INLINE void *xrealloc(void * const ptr, const bsize_t size)
850
{
850
{
851
    void *newptr = Brealloc(ptr, size);
851
    void *newptr = Brealloc(ptr, size);
852
852
853
    // According to the C Standard,
853
    // According to the C Standard,
854
    //  - ptr == NULL makes realloc() behave like malloc()
854
    //  - ptr == NULL makes realloc() behave like malloc()
855
    //  - size == 0 make it behave like free() if ptr != NULL
855
    //  - size == 0 make it behave like free() if ptr != NULL
856
    // Since we want to catch an out-of-mem in the first case, this leaves:
856
    // Since we want to catch an out-of-mem in the first case, this leaves:
857
    if (newptr == NULL && size != 0)
857
    if (newptr == NULL && size != 0)
858
        handle_memerr();
858
        handle_memerr();
859
859
860
    return newptr;
860
    return newptr;
861
}
861
}
862
862
863
FORCE_INLINE void *xaligned_malloc(const bsize_t alignment, const bsize_t size)
863
FORCE_INLINE void *xaligned_malloc(const bsize_t alignment, const bsize_t size)
864
{
864
{
865
#ifdef _WIN32
865
#ifdef _WIN32
866
    void *ptr = _aligned_malloc(size, alignment);
866
    void *ptr = _aligned_malloc(size, alignment);
867
#elif defined __APPLE__ || defined EDUKE32_BSD
867
#elif defined __APPLE__ || defined EDUKE32_BSD
868
    void *ptr = NULL;
868
    void *ptr = NULL;
869
    posix_memalign(&ptr, alignment, size);
869
    posix_memalign(&ptr, alignment, size);
870
#else
870
#else
871
    void *ptr = memalign(alignment, size);
871
    void *ptr = memalign(alignment, size);
872
#endif
872
#endif
873
873
874
    if (ptr == NULL) handle_memerr();
874
    if (ptr == NULL) handle_memerr();
875
    return ptr;
875
    return ptr;
876
}
876
}
877
877
878
878
879
#ifdef __cplusplus
879
#ifdef __cplusplus
880
}
880
}
881
#endif
881
#endif
882
882
883
#define DO_FREE_AND_NULL(var) do { \
883
#define DO_FREE_AND_NULL(var) do { \
884
    if (var != NULL) { Bfree(var); var = NULL; } \
884
    if (var != NULL) { Bfree(var); var = NULL; } \
885
} while (0)
885
} while (0)
886
886
887
#define ALIGNED_FREE_AND_NULL(var) do { \
887
#define ALIGNED_FREE_AND_NULL(var) do { \
888
    if (var != NULL) { Baligned_free(var); var = NULL; } \
888
    if (var != NULL) { Baligned_free(var); var = NULL; } \
889
} while (0)
889
} while (0)
890
890
891
#define MAYBE_FCLOSE_AND_NULL(fileptr) do { \
891
#define MAYBE_FCLOSE_AND_NULL(fileptr) do { \
892
    if (fileptr) { Bfclose(fileptr); fileptr=NULL; } \
892
    if (fileptr) { Bfclose(fileptr); fileptr=NULL; } \
893
} while (0)
893
} while (0)
894
894
895
/* Static assertions, based on source found in LuaJIT's src/lj_def.h. */
895
/* Static assertions, based on source found in LuaJIT's src/lj_def.h. */
896
#define EDUKE32_ASSERT_NAME2(name, line) name ## line
896
#define EDUKE32_ASSERT_NAME2(name, line) name ## line
897
#define EDUKE32_ASSERT_NAME(line) EDUKE32_ASSERT_NAME2(eduke32_assert_, line)
897
#define EDUKE32_ASSERT_NAME(line) EDUKE32_ASSERT_NAME2(eduke32_assert_, line)
898
#ifdef __COUNTER__
898
#ifdef __COUNTER__
899
# define EDUKE32_STATIC_ASSERT(cond) \
899
# define EDUKE32_STATIC_ASSERT(cond) \
900
    extern void EDUKE32_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
900
    extern void EDUKE32_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
901
#else
901
#else
902
# define EDUKE32_STATIC_ASSERT(cond) \
902
# define EDUKE32_STATIC_ASSERT(cond) \
903
    extern void EDUKE32_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
903
    extern void EDUKE32_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
904
#endif
904
#endif
905
905
906
#define ARRAY_SIZE(Ar) (sizeof(Ar)/sizeof((Ar)[0]))
906
#define ARRAY_SIZE(Ar) (sizeof(Ar)/sizeof((Ar)[0]))
907
#define ARRAY_SSIZE(Ar) (bssize_t)ARRAY_SIZE(Ar)
907
#define ARRAY_SSIZE(Ar) (bssize_t)ARRAY_SIZE(Ar)
908
908
909
////////// PANICKING ALLOCATION MACROS (wrapping the functions) //////////
909
////////// PANICKING ALLOCATION MACROS (wrapping the functions) //////////
910
#ifdef DEBUGGINGAIDS
910
#ifdef DEBUGGINGAIDS
911
// Detection of __func__ or equivalent functionality, found in SDL_assert.h
911
// Detection of __func__ or equivalent functionality, found in SDL_assert.h
912
# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */
912
# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */
913
#  define EDUKE32_FUNCTION __func__
913
#  define EDUKE32_FUNCTION __func__
914
# elif ((__GNUC__ >= 2) || defined(_MSC_VER))
914
# elif ((__GNUC__ >= 2) || defined(_MSC_VER))
915
#  define EDUKE32_FUNCTION __FUNCTION__
915
#  define EDUKE32_FUNCTION __FUNCTION__
916
# else
916
# else
917
#  define EDUKE32_FUNCTION "???"
917
#  define EDUKE32_FUNCTION "???"
918
# endif
918
# endif
919
919
920
# define EDUKE32_PRE_XALLLOC xalloc_set_location(__LINE__, __FILE__, EDUKE32_FUNCTION)
920
# define EDUKE32_PRE_XALLLOC xalloc_set_location(__LINE__, __FILE__, EDUKE32_FUNCTION)
921
# define Xstrdup(s) (EDUKE32_PRE_XALLLOC, xstrdup(s))
921
# define Xstrdup(s) (EDUKE32_PRE_XALLLOC, xstrdup(s))
922
# define Xmalloc(size) (EDUKE32_PRE_XALLLOC, xmalloc(size))
922
# define Xmalloc(size) (EDUKE32_PRE_XALLLOC, xmalloc(size))
923
# define Xcalloc(nmemb, size) (EDUKE32_PRE_XALLLOC, xcalloc(nmemb, size))
923
# define Xcalloc(nmemb, size) (EDUKE32_PRE_XALLLOC, xcalloc(nmemb, size))
924
# define Xrealloc(ptr, size) (EDUKE32_PRE_XALLLOC, xrealloc(ptr, size))
924
# define Xrealloc(ptr, size) (EDUKE32_PRE_XALLLOC, xrealloc(ptr, size))
925
# define Xaligned_alloc(size, alignment) (EDUKE32_PRE_XALLLOC, xaligned_malloc(size, alignment))
925
# define Xaligned_alloc(size, alignment) (EDUKE32_PRE_XALLLOC, xaligned_malloc(size, alignment))
926
# define Bexit(status) do { initprintf("exit(%d) at %s:%d in %s()\n", status, __FILE__, __LINE__, EDUKE32_FUNCTION); exit(status); } while (0)
926
# define Bexit(status) do { initprintf("exit(%d) at %s:%d in %s()\n", status, __FILE__, __LINE__, EDUKE32_FUNCTION); exit(status); } while (0)
927
#else
927
#else
928
# define Xstrdup xstrdup
928
# define Xstrdup xstrdup
929
# define Xmalloc xmalloc
929
# define Xmalloc xmalloc
930
# define Xcalloc xcalloc
930
# define Xcalloc xcalloc
931
# define Xrealloc xrealloc
931
# define Xrealloc xrealloc
932
# define Xaligned_alloc xaligned_malloc
932
# define Xaligned_alloc xaligned_malloc
933
# define Bexit exit
933
# define Bexit exit
934
#endif
934
#endif
935
935
936
#ifdef _WIN32
936
#ifdef _WIN32
937
# define Baligned_free(ptr) _aligned_free(ptr)
937
# define Baligned_free(ptr) _aligned_free(ptr)
938
#else
938
#else
939
# define Baligned_free(ptr) Bfree(ptr)
939
# define Baligned_free(ptr) Bfree(ptr)
940
#endif
940
#endif
941
941
942
//////////
942
//////////
943
943
944
#endif // compat_h_
944
#endif // compat_h_
945
 
945