Subversion Repositories eduke32

Rev

Rev 5013 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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