aboutsummaryrefslogtreecommitdiff
path: root/framework/delibs/debase/deDefs.h
blob: 80b240a076aa4e74bae3f864c3bd1379c2142fd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#ifndef _DEDEFS_H
#define _DEDEFS_H
/*-------------------------------------------------------------------------
 * drawElements Base Portability Library
 * -------------------------------------
 *
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief Basic portability.
 *//*--------------------------------------------------------------------*/

/* Compilers. */
#define DE_COMPILER_VANILLA	0		/*!< Vanilla compiler. Used for disabling all platform-specific optimizations.	*/
#define DE_COMPILER_MSC		1		/*!< Microsoft Visual Studio.													*/
#define DE_COMPILER_GCC     2		/*!< Gnu C Compiler.															*/
#define DE_COMPILER_CLANG   3		/*!< LLVM Clang Compiler.														*/

/* Compiler detection. */
#if defined(_MSC_VER)
#	define DE_DETAIL_DETECTED_COMPILER DE_COMPILER_MSC
#elif defined(__clang__)
#	define DE_DETAIL_DETECTED_COMPILER DE_COMPILER_CLANG
#elif defined(__GNUC__)
#   define DE_DETAIL_DETECTED_COMPILER DE_COMPILER_GCC
#else
	/* DE_DETAIL_DETECTED_COMPILER not set */
#endif

/* Compiler setting. */
#if defined(DE_COMPILER)
	/* Allow definitions from outside, but fail early if it conflicts with our detection */
#	if defined(DE_DETAIL_DETECTED_COMPILER) && (DE_COMPILER != DE_DETAIL_DETECTED_COMPILER)
		/* conflict, print a nice error messages for the most common misconfigs,
		 * GCC and Clang, and a generic for other conflicts.
		 */
#		if (DE_DETAIL_DETECTED_COMPILER == DE_COMPILER_CLANG) && (DE_COMPILER == DE_COMPILER_GCC)
#			error Detected compiler is Clang, but got DE_COMPILER == DE_COMPILER_GCC
#		elif (DE_DETAIL_DETECTED_COMPILER == DE_COMPILER_GCC) && (DE_COMPILER == DE_COMPILER_CLANG)
#			error Detected compiler is GCC, but got DE_COMPILER == DE_COMPILER_CLANG
#		else
#			error Detected compiler does not match the supplied compiler.
#		endif
#	endif
	/* Clear autodetect vars. */
#	if defined(DE_DETAIL_DETECTED_COMPILER)
#		undef DE_DETAIL_DETECTED_COMPILER
#	endif
#else
	/* No definition given from outside, try to autodetect */
#	if defined(DE_DETAIL_DETECTED_COMPILER)
#		define DE_COMPILER DE_DETAIL_DETECTED_COMPILER /*!< Compiler identification (set to one of DE_COMPILER_*). */
#	else
#		error Unknown compiler.
#	endif
#endif

/* Operating systems. */
#define DE_OS_VANILLA	0			/*!< Vanilla OS.								*/
#define DE_OS_WIN32		1			/*!< Microsoft Windows desktop					*/
#define DE_OS_UNIX      2			/*!< Unix (or compatible)						*/
#define DE_OS_WINCE		3			/*!< Windows CE, Windows Mobile or Pocket PC	*/
#define DE_OS_OSX		4			/*!< Mac OS X									*/
#define DE_OS_ANDROID	5			/*!< Android									*/
#define DE_OS_SYMBIAN	6			/*!< Symbian OS									*/
#define DE_OS_IOS		7			/*!< iOS										*/
#define DE_OS_QNX       8           /*!< QNX                                        */

/* OS detection (set to one of DE_OS_*). */
#if defined(DE_OS)
	/* Allow definitions from outside. */
#elif defined(__ANDROID__)
#	define DE_OS DE_OS_ANDROID
#elif defined(_WIN32_WCE) || defined(UNDER_CE)
#	define DE_OS DE_OS_WINCE
#elif defined(_WIN32)
#	define DE_OS DE_OS_WIN32
#elif defined(__unix__) || defined(__linux) || defined(__linux__)
#   define DE_OS DE_OS_UNIX
#elif defined(__APPLE__)
#	define DE_OS DE_OS_OSX
#elif defined(__EPOC32__)
#	define DE_OS DE_OS_SYMBIAN
#elif defined(__QNX__)
#   define DE_OS DE_OS_QNX
#else
#	error Unknown operating system.
#endif

/* CPUs */
#define DE_CPU_VANILLA	0
#define DE_CPU_X86		1
#define DE_CPU_ARM		2
#define DE_CPU_X86_64	3
#define DE_CPU_ARM_64	4
#define DE_CPU_MIPS		5
#define DE_CPU_MIPS_64	6
#define DE_CPU_RISCV_32	7
#define DE_CPU_RISCV_64	8

/* CPU detection. */
#if defined(DE_CPU)
	/* Allow definitions from outside. */
#elif defined(__aarch64__)
#	define DE_CPU DE_CPU_ARM_64
#elif defined(__arm__) || defined(__ARM__) || defined(__ARM_NEON__) || defined(ARM_BUILD)
#	define DE_CPU DE_CPU_ARM
#elif defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
#	define DE_CPU DE_CPU_X86_64
#elif defined(__i386__) || defined(_M_X86) || defined(_M_IX86) || defined(X86_BUILD)
#	define DE_CPU DE_CPU_X86
#elif defined(__mips__) && ((__mips) == 32)
#	define DE_CPU DE_CPU_MIPS
#elif defined(__mips__) && ((__mips) == 64)
#	define DE_CPU DE_CPU_MIPS_64
#elif defined(__riscv) && ((__riscv_xlen) == 32)
#	define DE_CPU DE_CPU_RISCV_32
#elif defined(__riscv) && ((__riscv_xlen) == 64)
#	define DE_CPU DE_CPU_RISCV_64
#else
#	error Unknown CPU.
#endif

/* Endianness */
#define DE_BIG_ENDIAN		0
#define DE_LITTLE_ENDIAN	1

#if defined(DE_ENDIANNESS)
	/* Allow definitions from outside. */
#elif (DE_CPU == DE_CPU_X86) || (DE_CPU == DE_CPU_X86_64)
	/* "detect" x86(_64) endianness */
#	define DE_ENDIANNESS DE_LITTLE_ENDIAN
#elif ((DE_CPU == DE_CPU_MIPS) || (DE_CPU == DE_CPU_MIPS_64))
	/* detect mips endianness using platform specific macros */
#	if defined(__MIPSEB__) && !defined(__MIPSEL__)
#		define DE_ENDIANNESS DE_BIG_ENDIAN
#	elif !defined(__MIPSEB__) && defined(__MIPSEL__)
#		define DE_ENDIANNESS DE_LITTLE_ENDIAN
#	else
#		error Invalid MIPS endianness.
#	endif
#elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
#	define DE_ENDIANNESS DE_LITTLE_ENDIAN
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
#	define DE_ENDIANNESS DE_BIG_ENDIAN
#else
#	error Unknown endianness.
#endif

/* Sanity */
#if ((DE_CPU == DE_CPU_X86) || (DE_CPU == DE_CPU_X86_64)) && (DE_ENDIANNESS == DE_BIG_ENDIAN)
#	error Invalid x86(_64) endianness.
#endif

/* Sized data types. */
typedef signed char			deInt8;
typedef signed short		deInt16;
typedef signed int			deInt32;
typedef unsigned char		deUint8;
typedef unsigned short		deUint16;
typedef unsigned int		deUint32;

#if (DE_COMPILER == DE_COMPILER_MSC)
	typedef signed __int64		deInt64;
	typedef unsigned __int64	deUint64;

#	if (DE_OS == DE_OS_WINCE)
#		include <basetsd.h>
		typedef INT_PTR			deIntptr;
		typedef UINT_PTR		deUintptr;
#	elif (DE_OS == DE_OS_WIN32)
#		include <crtdefs.h>
		typedef intptr_t		deIntptr;
		typedef uintptr_t		deUintptr;
#	else
#		error Define intptr types.
#	endif

#elif (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
	/* \note stddef.h is needed for size_t definition. */
#	include <stddef.h>
#	include <stdint.h>
	typedef int64_t				deInt64;
	typedef uint64_t			deUint64;
	typedef intptr_t			deIntptr;
	typedef uintptr_t			deUintptr;
#else
#	error Define 64-bit and intptr types.
#endif

/** Boolean type. */
typedef int deBool;
#define DE_TRUE		1		/*!< True value for deBool.		*/
#define DE_FALSE	0		/*!< False value for deBool.	*/

/* Null pointer. */
#if defined(__cplusplus)
#	define DE_NULL 0
#else
#	define DE_NULL ((void*)0)		/*!< Null pointer.				*/
#endif

/* Function pointer type. */
typedef void (*deFunctionPtr) (void);

/* Use DE_PTR_TYPE(T) instead of T* in macros to avoid clang-tidy warning. */
#define DE_PTR_TYPE(T) T*  /* NOLINT(T) */

/* Debug macro. */
#if defined(DE_DEBUG)
	/* Already defined from outside. */
#else
#	if (DE_COMPILER != DE_COMPILER_GCC)
#		if defined(_DEBUG)
#			define DE_DEBUG					/*!< Debug build enabled? Usage: #if defined(DE_DEBUG).	*/
#		endif
#	elif (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
#		if !defined(NDEBUG)
#			define DE_DEBUG
#		endif
#	endif
#endif

/* Debug code macro. */
#if defined(DE_DEBUG)
#	define DE_DEBUG_CODE(X) X
#else
#	define DE_DEBUG_CODE(X)
#endif

/* Inline. */
#if (DE_COMPILER == DE_COMPILER_MSC)
#	define DE_INLINE __forceinline
#elif (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
#   define DE_INLINE static __inline__
#else
#	define DE_INLINE inline			/*!< Function inline.		*/
#endif

/* DE_DEV_BUILD -- only define when building on a development machine. */
#if !defined(DE_DEV_BUILD)
#	if (DE_COMPILER == DE_COMPILER_MSC)
#		define DE_DEV_BUILD
#	endif
#endif

/* DE_VALGRIND_BUILD -- define this in makefile if support for Valgrind is wanted. */
/*#define DE_VALGRIND_BUILD*/

/** Length of array. C++ version does compile time check that passed value is an array reference. */
#if defined(__cplusplus)
	// deArraySizeHelper is a function that receives a reference to an array of N elements of type T and returns a reference to an
	// array of N chars. This forces the compiler to check the argument is an actual array and not some other type implementing
	// operator[]. The actual function is never defined anywhere, but taking the sizeof() of the result is allowed and equal to N.
	template <typename T, size_t N> char (&deArraySizeHelper(T (&array)[N]))[N];
#	define DE_LENGTH_OF_ARRAY(ARRAY) ((int)(sizeof(deArraySizeHelper(ARRAY))))
#else
#	define DE_LENGTH_OF_ARRAY(ARRAY) ((int)(sizeof(ARRAY) / sizeof((ARRAY)[0])))
#endif

#ifdef __cplusplus
extern "C" {
#endif

/* Assertion macro family. */
void deAssertFail(const char* reason, const char* file, int line);

/* Assertion failure callback. Requires DE_ASSERT_FAILURE_CALLBACK to be defined or otherwise has no effect. */
typedef void (*deAssertFailureCallbackFunc) (const char* reason, const char* file, int line);
void deSetAssertFailureCallback (deAssertFailureCallbackFunc callback);

DE_INLINE deBool deGetFalse (void) { return DE_FALSE; }
DE_INLINE deBool deGetTrue (void) { return DE_TRUE; }

/* Assertion macro. */
#if defined(DE_DEBUG) && !defined(DE_COVERAGE_BUILD)
#	define DE_ASSERT(X) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) deAssertFail(#X, __FILE__, __LINE__); } while(deGetFalse())
#else
#	define DE_ASSERT(X) /*@ -noeffect*/ ((void)0)	/*!< Assertion macro. */
#endif

/* Verify macro. Behaves like assert in debug build, but executes statement in release build. */
#if defined(DE_DEBUG)
#	define DE_VERIFY(X) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) deAssertFail(#X, __FILE__, __LINE__); } while(deGetFalse())
#else
#	define DE_VERIFY(X) X
#endif

/* Fatal macro. */
#if defined(DE_DEBUG) && !defined(DE_COVERAGE_BUILD)
#	define DE_FATAL(MSG) do { deAssertFail("" /* force to string literal */ MSG, __FILE__, __LINE__); } while(deGetFalse())
#else
#	define DE_FATAL(MSG) /*@ -noeffect*/ ((void)0)	/*!< Fatal macro. */
#endif

/** Test assert macro for use in testers (same as DE_ASSERT, but always enabled). */
#define DE_TEST_ASSERT(X) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) deAssertFail(#X, __FILE__, __LINE__); } while(deGetFalse())

#if (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
	/* GCC 4.8 and newer warns about unused typedefs. */
#	define DE_UNUSED_ATTR __attribute__((unused))
#else
#	define DE_UNUSED_ATTR
#endif

/** Compile-time assertion macro. */
#define DE_STATIC_ASSERT(X)						typedef char DE_UNIQUE_NAME[(X) ? 1 : -1] DE_UNUSED_ATTR
#define DE_HEADER_STATIC_ASSERT(HEADERTOKEN, X)	typedef char DE_HEADER_UNIQUE_NAME(HEADERTOKEN)[(X) ? 1 : -1] DE_UNUSED_ATTR

#define DE_UNIQUE_NAME						DE_MAKE_NAME(__LINE__, hoax)
#define DE_HEADER_UNIQUE_NAME(HEADERTOKEN)	DE_MAKE_NAME(__LINE__, HEADERTOKEN)
#define DE_MAKE_NAME(line, token) DE_MAKE_NAME2(line, token)
#define DE_MAKE_NAME2(line, token) _static_assert_##line##_##token

/** Software breakpoint. */
#if (DE_CPU == DE_CPU_X86) && (DE_COMPILER == DE_COMPILER_MSC)
#	define DE_BREAKPOINT() do { printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); __asm { int 3 } } while (deGetFalse())
#elif (DE_CPU == DE_CPU_X86_64) && (DE_COMPILER == DE_COMPILER_MSC)
#	define DE_BREAKPOINT() do { printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); __debugbreak(); } while (deGetFalse())
#elif (DE_CPU == DE_CPU_ARM) && (DE_COMPILER == DE_COMPILER_GCC)
#	define DE_BREAKPOINT() do { printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); __asm__ __volatile__ ( "bkpt #3" ); } while (deGetFalse())
#elif (DE_CPU == DE_CPU_ARM_64) && (DE_COMPILER == DE_COMPILER_GCC)
#	define DE_BREAKPOINT() do { printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); __asm__ __volatile__ ( "brk #3" ); } while (deGetFalse())
#elif ((DE_CPU == DE_CPU_ARM) || (DE_CPU == DE_CPU_ARM_64)) && (DE_COMPILER == DE_COMPILER_MSC)
#	define DE_BREAKPOINT() do { printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); DebugBreak(); } while (deGetFalse())
#else
#	define DE_BREAKPOINT() DE_FATAL("Software breakpoint encountered!")
#endif

/** Swap two values. */
#define DE_SWAP(TYPE, A, B) do { TYPE _tmp_ = (A); (A) = (B); (B) = _tmp_; } while(deGetFalse())

/** Offset of a struct member. */
#define DE_OFFSET_OF(STRUCT, MEMBER) ((deUint32)(deUintptr)(deUint8*)&(((STRUCT*)0)->MEMBER))

/** Used in enum to easify declarations for struct serialization. Declares 'NAME'_OFFSET, 'NAME'_SIZE, and offsets counter for next enum value by SIZE. */
#define DE_SERIALIZED_FIELD(NAME, SIZE) NAME ## _OFFSET, NAME ## _SIZE = (SIZE), _DE_TMP_ ## NAME = NAME ## _OFFSET + (SIZE) - 1

/* Pointer size. */
#if defined(DE_PTR_SIZE)
	/* nada */
#elif defined(_M_X64) || defined(__x86_64__) || defined(__amd64__) || defined(__aarch64__) || (defined(__mips) && ((__mips) == 64)) || defined(_LP64) || defined(__LP64__)
#	define DE_PTR_SIZE 8
#else
#	define DE_PTR_SIZE 4	/* default to 32-bit */
#endif

/** Unreferenced variable silencing. */
#define DE_UNREF(VAR) ((void)(VAR))

/** DE_BEGIN_EXTERN_C and DE_END_EXTERN_C. */
#if defined(__cplusplus)
#	define DE_BEGIN_EXTERN_C extern "C" {
#	define DE_END_EXTERN_C }
#else
#	define DE_BEGIN_EXTERN_C
#	define DE_END_EXTERN_C
#endif

/** DE_NULL_STATEMENT */
#if defined(DE_DEBUG)
#	define DE_NULL_STATEMENT do {} while (deGetFalse())
#else
#	define DE_NULL_STATEMENT (void)0
#endif

/** GCC format string attributes */
#if (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
#	define DE_PRINTF_FUNC_ATTR(FORMAT_STRING, FIRST_ARG) __attribute__ ((format(printf, FORMAT_STRING, FIRST_ARG)))
#else
#	define DE_PRINTF_FUNC_ATTR(FORMAT_STRING, FIRST_ARG)
#endif

/** Potentially unused func attribute to silence warnings from C templates. */
#if (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
#	define DE_UNUSED_FUNCTION __attribute__((unused))
#else
#	define DE_UNUSED_FUNCTION
#endif

DE_INLINE const char* deFatalStr (const char* reason) { DE_ASSERT(0); return reason; }

#ifdef __cplusplus
}
#endif

#endif /* _DEDEFS_H */