Subversion Repositories eduke32

Rev

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

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