VirtualBox

source: vbox/trunk/include/iprt/cdefs.h@ 36564

Last change on this file since 36564 was 36555, checked in by vboxsync, 14 years ago

Use DECLHIDDEN, especially in IPRT.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 74.2 KB
Line 
1/** @file
2 * IPRT - Common C and C++ definitions.
3 */
4
5/*
6 * Copyright (C) 2006-2010 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_cdefs_h
27#define ___iprt_cdefs_h
28
29
30/** @defgroup grp_rt_cdefs IPRT Common Definitions and Macros
31 * @{
32 */
33
34/*
35 * Include sys/cdefs.h if present, if not define the stuff we need.
36 */
37#ifdef HAVE_SYS_CDEFS_H
38# if defined(RT_ARCH_LINUX) && defined(__KERNEL__)
39# error "oops"
40# endif
41# include <sys/cdefs.h>
42#else
43
44/** @def RT_C_DECLS_BEGIN
45 * Used to start a block of function declarations which are shared
46 * between C and C++ program.
47 */
48
49/** @def RT_C_DECLS_END
50 * Used to end a block of function declarations which are shared
51 * between C and C++ program.
52 */
53
54# if defined(__cplusplus)
55# define RT_C_DECLS_BEGIN extern "C" {
56# define RT_C_DECLS_END }
57# else
58# define RT_C_DECLS_BEGIN
59# define RT_C_DECLS_END
60# endif
61
62#endif
63
64
65/*
66 * Shut up DOXYGEN warnings and guide it properly thru the code.
67 */
68#ifdef DOXYGEN_RUNNING
69# define __AMD64__
70# define __X86__
71# define RT_ARCH_AMD64
72# define RT_ARCH_X86
73# define IN_RING0
74# define IN_RING3
75# define IN_RC
76# define IN_RC
77# define IN_RT_RC
78# define IN_RT_R0
79# define IN_RT_R3
80# define IN_RT_STATIC
81# define RT_STRICT
82# define RT_LOCK_STRICT
83# define RT_LOCK_NO_STRICT
84# define RT_LOCK_STRICT_ORDER
85# define RT_LOCK_NO_STRICT_ORDER
86# define Breakpoint
87# define RT_NO_DEPRECATED_MACROS
88# define RT_EXCEPTIONS_ENABLED
89# define RT_BIG_ENDIAN
90# define RT_LITTLE_ENDIAN
91# define RT_COMPILER_GROKS_64BIT_BITFIELDS
92# define RT_COMPILER_WITH_80BIT_LONG_DOUBLE
93# define RT_NO_VISIBILITY_HIDDEN
94#endif /* DOXYGEN_RUNNING */
95
96/** @def RT_ARCH_X86
97 * Indicates that we're compiling for the X86 architecture.
98 */
99
100/** @def RT_ARCH_AMD64
101 * Indicates that we're compiling for the AMD64 architecture.
102 */
103
104/** @def RT_ARCH_SPARC
105 * Indicates that we're compiling for the SPARC V8 architecture (32-bit).
106 */
107
108/** @def RT_ARCH_SPARC64
109 * Indicates that we're compiling for the SPARC V9 architecture (64-bit).
110 */
111#if !defined(RT_ARCH_X86) && !defined(RT_ARCH_AMD64) && !defined(RT_ARCH_SPARC) && !defined(RT_ARCH_SPARC64)
112# if defined(__amd64__) || defined(__x86_64__) || defined(_M_X64) || defined(__AMD64__)
113# define RT_ARCH_AMD64
114# elif defined(__i386__) || defined(_M_IX86) || defined(__X86__)
115# define RT_ARCH_X86
116# elif defined(__sparcv9)
117# define RT_ARCH_SPARC64
118# elif defined(__sparc__)
119# define RT_ARCH_SPARC
120# else /* PORTME: append test for new archs. */
121# error "Check what predefined macros your compiler uses to indicate architecture."
122# endif
123/* PORTME: append new archs checks. */
124#elif defined(RT_ARCH_X86) && defined(RT_ARCH_AMD64)
125# error "Both RT_ARCH_X86 and RT_ARCH_AMD64 cannot be defined at the same time!"
126#elif defined(RT_ARCH_X86) && defined(RT_ARCH_SPARC)
127# error "Both RT_ARCH_X86 and RT_ARCH_SPARC cannot be defined at the same time!"
128#elif defined(RT_ARCH_X86) && defined(RT_ARCH_SPARC64)
129# error "Both RT_ARCH_X86 and RT_ARCH_SPARC64 cannot be defined at the same time!"
130#elif defined(RT_ARCH_AMD64) && defined(RT_ARCH_SPARC)
131# error "Both RT_ARCH_AMD64 and RT_ARCH_SPARC cannot be defined at the same time!"
132#elif defined(RT_ARCH_AMD64) && defined(RT_ARCH_SPARC64)
133# error "Both RT_ARCH_AMD64 and RT_ARCH_SPARC64 cannot be defined at the same time!"
134#elif defined(RT_ARCH_SPARC) && defined(RT_ARCH_SPARC64)
135# error "Both RT_ARCH_SPARC and RT_ARCH_SPARC64 cannot be defined at the same time!"
136#endif
137
138
139/** @def __X86__
140 * Indicates that we're compiling for the X86 architecture.
141 * @deprecated
142 */
143
144/** @def __AMD64__
145 * Indicates that we're compiling for the AMD64 architecture.
146 * @deprecated
147 */
148#if !defined(__X86__) && !defined(__AMD64__) && !defined(RT_ARCH_SPARC) && !defined(RT_ARCH_SPARC64)
149# if defined(RT_ARCH_AMD64)
150# define __AMD64__
151# elif defined(RT_ARCH_X86)
152# define __X86__
153# else
154# error "Check what predefined macros your compiler uses to indicate architecture."
155# endif
156#elif defined(__X86__) && defined(__AMD64__)
157# error "Both __X86__ and __AMD64__ cannot be defined at the same time!"
158#elif defined(__X86__) && !defined(RT_ARCH_X86)
159# error "Both __X86__ without RT_ARCH_X86!"
160#elif defined(__AMD64__) && !defined(RT_ARCH_AMD64)
161# error "Both __AMD64__ without RT_ARCH_AMD64!"
162#endif
163
164/** @def RT_BIG_ENDIAN
165 * Defined if the architecture is big endian. */
166/** @def RT_LITTLE_ENDIAN
167 * Defined if the architecture is little endian. */
168#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
169# define RT_LITTLE_ENDIAN
170#elif defined(RT_ARCH_SPARC) || defined(RT_ARCH_SPARC64)
171# define RT_BIG_ENDIAN
172#else
173# error "PORTME: architecture endianess"
174#endif
175#if defined(RT_BIG_ENDIAN) && defined(RT_LITTLE_ENDIAN)
176# error "Both RT_BIG_ENDIAN and RT_LITTLE_ENDIAN are defined"
177#endif
178
179
180/** @def IN_RING0
181 * Used to indicate that we're compiling code which is running
182 * in Ring-0 Host Context.
183 */
184
185/** @def IN_RING3
186 * Used to indicate that we're compiling code which is running
187 * in Ring-3 Host Context.
188 */
189
190/** @def IN_RC
191 * Used to indicate that we're compiling code which is running
192 * in the Raw-mode Context (implies R0).
193 */
194#if !defined(IN_RING3) && !defined(IN_RING0) && !defined(IN_RC) && !defined(IN_RC)
195# error "You must define which context the compiled code should run in; IN_RING3, IN_RING0 or IN_RC"
196#endif
197#if (defined(IN_RING3) && (defined(IN_RING0) || defined(IN_RC)) ) \
198 || (defined(IN_RING0) && (defined(IN_RING3) || defined(IN_RC)) ) \
199 || (defined(IN_RC) && (defined(IN_RING3) || defined(IN_RING0)) )
200# error "Only one of the IN_RING3, IN_RING0, IN_RC defines should be defined."
201#endif
202
203
204/** @def ARCH_BITS
205 * Defines the bit count of the current context.
206 */
207#if !defined(ARCH_BITS) || defined(DOXYGEN_RUNNING)
208# if defined(RT_ARCH_AMD64) || defined(RT_ARCH_SPARC64)
209# define ARCH_BITS 64
210# else
211# define ARCH_BITS 32
212# endif
213#endif
214
215/** @def HC_ARCH_BITS
216 * Defines the host architecture bit count.
217 */
218#if !defined(HC_ARCH_BITS) || defined(DOXYGEN_RUNNING)
219# ifndef IN_RC
220# define HC_ARCH_BITS ARCH_BITS
221# else
222# define HC_ARCH_BITS 32
223# endif
224#endif
225
226/** @def GC_ARCH_BITS
227 * Defines the guest architecture bit count.
228 */
229#if !defined(GC_ARCH_BITS) && !defined(DOXYGEN_RUNNING)
230# ifdef VBOX_WITH_64_BITS_GUESTS
231# define GC_ARCH_BITS 64
232# else
233# define GC_ARCH_BITS 32
234# endif
235#endif
236
237/** @def R3_ARCH_BITS
238 * Defines the host ring-3 architecture bit count.
239 */
240#if !defined(R3_ARCH_BITS) || defined(DOXYGEN_RUNNING)
241# ifdef IN_RING3
242# define R3_ARCH_BITS ARCH_BITS
243# else
244# define R3_ARCH_BITS HC_ARCH_BITS
245# endif
246#endif
247
248/** @def R0_ARCH_BITS
249 * Defines the host ring-0 architecture bit count.
250 */
251#if !defined(R0_ARCH_BITS) || defined(DOXYGEN_RUNNING)
252# ifdef IN_RING0
253# define R0_ARCH_BITS ARCH_BITS
254# else
255# define R0_ARCH_BITS HC_ARCH_BITS
256# endif
257#endif
258
259/** @def GC_ARCH_BITS
260 * Defines the guest architecture bit count.
261 */
262#if !defined(GC_ARCH_BITS) || defined(DOXYGEN_RUNNING)
263# ifdef IN_RC
264# define GC_ARCH_BITS ARCH_BITS
265# else
266# define GC_ARCH_BITS 32
267# endif
268#endif
269
270
271/** @def CTXTYPE
272 * Declare a type differently in GC, R3 and R0.
273 *
274 * @param GCType The GC type.
275 * @param R3Type The R3 type.
276 * @param R0Type The R0 type.
277 * @remark For pointers used only in one context use RCPTRTYPE(), R3R0PTRTYPE(), R3PTRTYPE() or R0PTRTYPE().
278 */
279#ifdef IN_RC
280# define CTXTYPE(GCType, R3Type, R0Type) GCType
281#elif defined(IN_RING3)
282# define CTXTYPE(GCType, R3Type, R0Type) R3Type
283#else
284# define CTXTYPE(GCType, R3Type, R0Type) R0Type
285#endif
286
287/** @def RCPTRTYPE
288 * Declare a pointer which is used in the raw mode context but appears in structure(s) used by
289 * both HC and RC. The main purpose is to make sure structures have the same
290 * size when built for different architectures.
291 *
292 * @param RCType The RC type.
293 */
294#define RCPTRTYPE(RCType) CTXTYPE(RCType, RTRCPTR, RTRCPTR)
295
296/** @def R3R0PTRTYPE
297 * Declare a pointer which is used in HC, is explicitly valid in ring 3 and 0,
298 * but appears in structure(s) used by both HC and GC. The main purpose is to
299 * make sure structures have the same size when built for different architectures.
300 *
301 * @param R3R0Type The R3R0 type.
302 * @remarks This used to be called HCPTRTYPE.
303 */
304#define R3R0PTRTYPE(R3R0Type) CTXTYPE(RTHCPTR, R3R0Type, R3R0Type)
305
306/** @def R3PTRTYPE
307 * Declare a pointer which is used in R3 but appears in structure(s) used by
308 * both HC and GC. The main purpose is to make sure structures have the same
309 * size when built for different architectures.
310 *
311 * @param R3Type The R3 type.
312 */
313#define R3PTRTYPE(R3Type) CTXTYPE(RTHCUINTPTR, R3Type, RTHCUINTPTR)
314
315/** @def R0PTRTYPE
316 * Declare a pointer which is used in R0 but appears in structure(s) used by
317 * both HC and GC. The main purpose is to make sure structures have the same
318 * size when built for different architectures.
319 *
320 * @param R0Type The R0 type.
321 */
322#define R0PTRTYPE(R0Type) CTXTYPE(RTHCUINTPTR, RTHCUINTPTR, R0Type)
323
324/** @def CTXSUFF
325 * Adds the suffix of the current context to the passed in
326 * identifier name. The suffix is HC or GC.
327 *
328 * This is macro should only be used in shared code to avoid a forest of ifdefs.
329 * @param var Identifier name.
330 * @deprecated Use CTX_SUFF. Do NOT use this for new code.
331 */
332/** @def OTHERCTXSUFF
333 * Adds the suffix of the other context to the passed in
334 * identifier name. The suffix is HC or GC.
335 *
336 * This is macro should only be used in shared code to avoid a forest of ifdefs.
337 * @param var Identifier name.
338 * @deprecated Use CTX_SUFF. Do NOT use this for new code.
339 */
340#ifdef IN_RC
341# define CTXSUFF(var) var##GC
342# define OTHERCTXSUFF(var) var##HC
343#else
344# define CTXSUFF(var) var##HC
345# define OTHERCTXSUFF(var) var##GC
346#endif
347
348/** @def CTXALLSUFF
349 * Adds the suffix of the current context to the passed in
350 * identifier name. The suffix is R3, R0 or GC.
351 *
352 * This is macro should only be used in shared code to avoid a forest of ifdefs.
353 * @param var Identifier name.
354 * @deprecated Use CTX_SUFF. Do NOT use this for new code.
355 */
356#ifdef IN_RC
357# define CTXALLSUFF(var) var##GC
358#elif defined(IN_RING0)
359# define CTXALLSUFF(var) var##R0
360#else
361# define CTXALLSUFF(var) var##R3
362#endif
363
364/** @def CTX_SUFF
365 * Adds the suffix of the current context to the passed in
366 * identifier name. The suffix is R3, R0 or RC.
367 *
368 * This is macro should only be used in shared code to avoid a forest of ifdefs.
369 * @param var Identifier name.
370 *
371 * @remark This will replace CTXALLSUFF and CTXSUFF before long.
372 */
373#ifdef IN_RC
374# define CTX_SUFF(var) var##RC
375#elif defined(IN_RING0)
376# define CTX_SUFF(var) var##R0
377#else
378# define CTX_SUFF(var) var##R3
379#endif
380
381/** @def CTX_SUFF_Z
382 * Adds the suffix of the current context to the passed in
383 * identifier name, combining RC and R0 into RZ.
384 * The suffix thus is R3 or RZ.
385 *
386 * This is macro should only be used in shared code to avoid a forest of ifdefs.
387 * @param var Identifier name.
388 *
389 * @remark This will replace CTXALLSUFF and CTXSUFF before long.
390 */
391#ifdef IN_RING3
392# define CTX_SUFF_Z(var) var##R3
393#else
394# define CTX_SUFF_Z(var) var##RZ
395#endif
396
397
398/** @def CTXMID
399 * Adds the current context as a middle name of an identifier name
400 * The middle name is HC or GC.
401 *
402 * This is macro should only be used in shared code to avoid a forest of ifdefs.
403 * @param first First name.
404 * @param last Surname.
405 */
406/** @def OTHERCTXMID
407 * Adds the other context as a middle name of an identifier name
408 * The middle name is HC or GC.
409 *
410 * This is macro should only be used in shared code to avoid a forest of ifdefs.
411 * @param first First name.
412 * @param last Surname.
413 * @deprecated use CTX_MID or CTX_MID_Z
414 */
415#ifdef IN_RC
416# define CTXMID(first, last) first##GC##last
417# define OTHERCTXMID(first, last) first##HC##last
418#else
419# define CTXMID(first, last) first##HC##last
420# define OTHERCTXMID(first, last) first##GC##last
421#endif
422
423/** @def CTXALLMID
424 * Adds the current context as a middle name of an identifier name.
425 * The middle name is R3, R0 or GC.
426 *
427 * This is macro should only be used in shared code to avoid a forest of ifdefs.
428 * @param first First name.
429 * @param last Surname.
430 * @deprecated use CTX_MID or CTX_MID_Z
431 */
432#ifdef IN_RC
433# define CTXALLMID(first, last) first##GC##last
434#elif defined(IN_RING0)
435# define CTXALLMID(first, last) first##R0##last
436#else
437# define CTXALLMID(first, last) first##R3##last
438#endif
439
440/** @def CTX_MID
441 * Adds the current context as a middle name of an identifier name.
442 * The middle name is R3, R0 or RC.
443 *
444 * This is macro should only be used in shared code to avoid a forest of ifdefs.
445 * @param first First name.
446 * @param last Surname.
447 */
448#ifdef IN_RC
449# define CTX_MID(first, last) first##RC##last
450#elif defined(IN_RING0)
451# define CTX_MID(first, last) first##R0##last
452#else
453# define CTX_MID(first, last) first##R3##last
454#endif
455
456/** @def CTX_MID_Z
457 * Adds the current context as a middle name of an identifier name, combining RC
458 * and R0 into RZ.
459 * The middle name thus is either R3 or RZ.
460 *
461 * This is macro should only be used in shared code to avoid a forest of ifdefs.
462 * @param first First name.
463 * @param last Surname.
464 */
465#ifdef IN_RING3
466# define CTX_MID_Z(first, last) first##R3##last
467#else
468# define CTX_MID_Z(first, last) first##RZ##last
469#endif
470
471
472/** @def R3STRING
473 * A macro which in GC and R0 will return a dummy string while in R3 it will return
474 * the parameter.
475 *
476 * This is typically used to wrap description strings in structures shared
477 * between R3, R0 and/or GC. The intention is to avoid the \#ifdef IN_RING3 mess.
478 *
479 * @param pR3String The R3 string. Only referenced in R3.
480 * @see R0STRING and GCSTRING
481 */
482#ifdef IN_RING3
483# define R3STRING(pR3String) (pR3String)
484#else
485# define R3STRING(pR3String) ("<R3_STRING>")
486#endif
487
488/** @def R0STRING
489 * A macro which in GC and R3 will return a dummy string while in R0 it will return
490 * the parameter.
491 *
492 * This is typically used to wrap description strings in structures shared
493 * between R3, R0 and/or GC. The intention is to avoid the \#ifdef IN_RING0 mess.
494 *
495 * @param pR0String The R0 string. Only referenced in R0.
496 * @see R3STRING and GCSTRING
497 */
498#ifdef IN_RING0
499# define R0STRING(pR0String) (pR0String)
500#else
501# define R0STRING(pR0String) ("<R0_STRING>")
502#endif
503
504/** @def RCSTRING
505 * A macro which in R3 and R0 will return a dummy string while in RC it will return
506 * the parameter.
507 *
508 * This is typically used to wrap description strings in structures shared
509 * between R3, R0 and/or RC. The intention is to avoid the \#ifdef IN_RC mess.
510 *
511 * @param pRCString The RC string. Only referenced in RC.
512 * @see R3STRING, R0STRING
513 */
514#ifdef IN_RC
515# define RCSTRING(pRCString) (pRCString)
516#else
517# define RCSTRING(pRCString) ("<RC_STRING>")
518#endif
519
520
521/** @def RT_NOTHING
522 * A macro that expands to nothing.
523 * This is primarily intended as a dummy argument for macros to avoid the
524 * undefined behavior passing empty arguments to an macro (ISO C90 and C++98,
525 * gcc v4.4 warns about it).
526 */
527#define RT_NOTHING
528
529/** @def RT_GCC_EXTENSION
530 * Macro for shutting up GCC warnings about using language extensions. */
531#ifdef __GNUC__
532# define RT_GCC_EXTENSION __extension__
533#else
534# define RT_GCC_EXTENSION
535#endif
536
537/** @def RT_COMPILER_GROKS_64BIT_BITFIELDS
538 * Macro that is defined if the compiler understands 64-bit bitfields. */
539#if !defined(RT_OS_OS2) || (!defined(__IBMC__) && !defined(__IBMCPP__))
540# define RT_COMPILER_GROKS_64BIT_BITFIELDS
541#endif
542
543/** @def RT_COMPILER_WITH_80BIT_LONG_DOUBLE
544 * Macro that is defined if the compiler implements long double as the
545 * IEEE extended precision floating. */
546#if (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)) && !defined(RT_OS_WINDOWS)
547# define RT_COMPILER_WITH_80BIT_LONG_DOUBLE
548#endif
549
550
551/** @def RT_EXCEPTIONS_ENABLED
552 * Defined when C++ exceptions are enabled.
553 */
554#if !defined(RT_EXCEPTIONS_ENABLED) \
555 && defined(__cplusplus) \
556 && ( (defined(_MSC_VER) && defined(_CPPUNWIND)) \
557 || (defined(__GNUC__) && defined(__EXCEPTIONS)))
558# define RT_EXCEPTIONS_ENABLED
559#endif
560
561/** @def RT_NO_THROW
562 * How to express that a function doesn't throw C++ exceptions
563 * and the compiler can thus save itself the bother of trying
564 * to catch any of them. Put this between the closing parenthesis
565 * and the semicolon in function prototypes (and implementation if C++).
566 */
567#ifdef RT_EXCEPTIONS_ENABLED
568# define RT_NO_THROW throw()
569#else
570# define RT_NO_THROW
571#endif
572
573/** @def RT_THROW
574 * How to express that a method or function throws a type of exceptions. Some
575 * compilers does not want this kind of information and will warning about it.
576 *
577 * @param type The type exception.
578 *
579 * @remarks If the actual throwing is done from the header, enclose it by
580 * \#ifdef RT_EXCEPTIONS_ENABLED ... \#else ... \#endif so the header
581 * compiles cleanly without exceptions enabled.
582 *
583 * Do NOT use this for the actual throwing of exceptions!
584 */
585#ifdef RT_EXCEPTIONS_ENABLED
586# ifdef _MSC_VER
587# if _MSC_VER >= 1400
588# define RT_THROW(type)
589# else
590# define RT_THROW(type) throw(type)
591# endif
592# else
593# define RT_THROW(type) throw(type)
594# endif
595#else
596# define RT_THROW(type)
597#endif
598
599/** @def RT_GCC_SUPPORTS_VISIBILITY_HIDDEN
600 * Indicates that the "hidden" visibility attribute can be used (GCC) */
601#if defined(__GNUC__)
602# if __GNUC__ >= 4 && !defined(RT_OS_OS2) && !defined(RT_OS_WINDOWS)
603# define RT_GCC_SUPPORTS_VISIBILITY_HIDDEN
604# endif
605#endif
606
607/** @def RTCALL
608 * The standard calling convention for the Runtime interfaces.
609 */
610#ifdef _MSC_VER
611# define RTCALL __cdecl
612#elif defined(RT_OS_OS2)
613# define RTCALL __cdecl
614#elif defined(__GNUC__) && defined(IN_RING0) \
615 && !(defined(RT_ARCH_AMD64) || defined(RT_ARCH_SPARC) || defined(RT_ARCH_SPARC64)) /* the latter is kernel/gcc */
616# define RTCALL __attribute__((cdecl,regparm(0)))
617#else
618# define RTCALL
619#endif
620
621/** @def DECLEXPORT
622 * How to declare an exported function.
623 * @param type The return type of the function declaration.
624 */
625#if defined(_MSC_VER) || defined(RT_OS_OS2)
626# define DECLEXPORT(type) __declspec(dllexport) type
627#elif defined(RT_USE_VISIBILITY_DEFAULT)
628# define DECLEXPORT(type) __attribute__((visibility("default"))) type
629#else
630# define DECLEXPORT(type) type
631#endif
632
633/** @def DECLIMPORT
634 * How to declare an imported function.
635 * @param type The return type of the function declaration.
636 */
637#if defined(_MSC_VER) || (defined(RT_OS_OS2) && !defined(__IBMC__) && !defined(__IBMCPP__))
638# define DECLIMPORT(type) __declspec(dllimport) type
639#else
640# define DECLIMPORT(type) type
641#endif
642
643/** @def DECLHIDDEN
644 * How to declare a non-exported function or variable.
645 * @param type The return type of the function or the data type of the variable.
646 */
647#if !defined(RT_GCC_SUPPORTS_VISIBILITY_HIDDEN) || defined(RT_NO_VISIBILITY_HIDDEN)
648# define DECLHIDDEN(type) type
649#else
650# define DECLHIDDEN(type) __attribute__((visibility("hidden"))) type
651#endif
652
653/** @def DECL_HIDDEN_CONST
654 * Workaround for g++ warnings when applying the hidden attribute to a const
655 * definition. Use DECLHIDDEN for the declaration.
656 * @param a_Type The return type of the function or the data type of
657 * the variable.
658 */
659#if defined(__cplusplus) && defined(__GNUC__)
660# define DECL_HIDDEN_CONST(a_Type) a_Type
661#else
662# define DECL_HIDDEN_CONST(a_Type) DECLHIDDEN(a_Type)
663#endif
664
665/** @def DECL_INVALID
666 * How to declare a function not available for linking in the current context.
667 * The purpose is to create compile or like time errors when used. This isn't
668 * possible on all platforms.
669 * @param type The return type of the function.
670 */
671#if defined(_MSC_VER)
672# define DECL_INVALID(type) __declspec(dllimport) type __stdcall
673#elif defined(__GNUC__) && defined(__cplusplus)
674# define DECL_INVALID(type) extern "C++" type
675#else
676# define DECL_INVALID(type) type
677#endif
678
679/** @def DECLASM
680 * How to declare an internal assembly function.
681 * @param type The return type of the function declaration.
682 */
683#ifdef __cplusplus
684# ifdef _MSC_VER
685# define DECLASM(type) extern "C" type __cdecl
686# else
687# define DECLASM(type) extern "C" type
688# endif
689#else
690# ifdef _MSC_VER
691# define DECLASM(type) type __cdecl
692# else
693# define DECLASM(type) type
694# endif
695#endif
696
697/** @def DECLASMTYPE
698 * How to declare an internal assembly function type.
699 * @param type The return type of the function.
700 */
701#ifdef _MSC_VER
702# define DECLASMTYPE(type) type __cdecl
703#else
704# define DECLASMTYPE(type) type
705#endif
706
707/** @def DECLNORETURN
708 * How to declare a function which does not return.
709 * @note: This macro can be combined with other macros, for example
710 * @code
711 * EMR3DECL(DECLNORETURN(void)) foo(void);
712 * @endcode
713 */
714#ifdef _MSC_VER
715# define DECLNORETURN(type) __declspec(noreturn) type
716#elif defined(__GNUC__)
717# define DECLNORETURN(type) __attribute__((noreturn)) type
718#else
719# define DECLNORETURN(type) type
720#endif
721
722/** @def DECLCALLBACK
723 * How to declare an call back function type.
724 * @param type The return type of the function declaration.
725 */
726#define DECLCALLBACK(type) type RTCALL
727
728/** @def DECLCALLBACKPTR
729 * How to declare an call back function pointer.
730 * @param type The return type of the function declaration.
731 * @param name The name of the variable member.
732 */
733#define DECLCALLBACKPTR(type, name) type (RTCALL * name)
734
735/** @def DECLCALLBACKMEMBER
736 * How to declare an call back function pointer member.
737 * @param type The return type of the function declaration.
738 * @param name The name of the struct/union/class member.
739 */
740#define DECLCALLBACKMEMBER(type, name) type (RTCALL * name)
741
742/** @def DECLR3CALLBACKMEMBER
743 * How to declare an call back function pointer member - R3 Ptr.
744 * @param type The return type of the function declaration.
745 * @param name The name of the struct/union/class member.
746 * @param args The argument list enclosed in parentheses.
747 */
748#ifdef IN_RING3
749# define DECLR3CALLBACKMEMBER(type, name, args) type (RTCALL * name) args
750#else
751# define DECLR3CALLBACKMEMBER(type, name, args) RTR3PTR name
752#endif
753
754/** @def DECLRCCALLBACKMEMBER
755 * How to declare an call back function pointer member - RC Ptr.
756 * @param type The return type of the function declaration.
757 * @param name The name of the struct/union/class member.
758 * @param args The argument list enclosed in parentheses.
759 */
760#ifdef IN_RC
761# define DECLRCCALLBACKMEMBER(type, name, args) type (RTCALL * name) args
762#else
763# define DECLRCCALLBACKMEMBER(type, name, args) RTRCPTR name
764#endif
765
766/** @def DECLR0CALLBACKMEMBER
767 * How to declare an call back function pointer member - R0 Ptr.
768 * @param type The return type of the function declaration.
769 * @param name The name of the struct/union/class member.
770 * @param args The argument list enclosed in parentheses.
771 */
772#ifdef IN_RING0
773# define DECLR0CALLBACKMEMBER(type, name, args) type (RTCALL * name) args
774#else
775# define DECLR0CALLBACKMEMBER(type, name, args) RTR0PTR name
776#endif
777
778/** @def DECLINLINE
779 * How to declare a function as inline.
780 * @param type The return type of the function declaration.
781 * @remarks Don't use this macro on C++ methods.
782 */
783#ifdef __GNUC__
784# define DECLINLINE(type) static __inline__ type
785#elif defined(__cplusplus)
786# define DECLINLINE(type) inline type
787#elif defined(_MSC_VER)
788# define DECLINLINE(type) _inline type
789#elif defined(__IBMC__)
790# define DECLINLINE(type) _Inline type
791#else
792# define DECLINLINE(type) inline type
793#endif
794
795
796/** @def DECL_FORCE_INLINE
797 * How to declare a function as inline and try convince the compiler to always
798 * inline it regardless of optimization switches.
799 * @param type The return type of the function declaration.
800 * @remarks Use sparsely and with care. Don't use this macro on C++ methods.
801 */
802#ifdef __GNUC__
803# define DECL_FORCE_INLINE(type) __attribute__((__always_inline__)) DECLINLINE(type)
804#elif defined(_MSC_VER)
805# define DECL_FORCE_INLINE(type) __forceinline type
806#else
807# define DECL_FORCE_INLINE(type) DECLINLINE(type)
808#endif
809
810
811/** @def DECL_NO_INLINE
812 * How to declare a function telling the compiler not to inline it.
813 * @param scope The function scope, static or RT_NOTHING.
814 * @param type The return type of the function declaration.
815 * @remarks Don't use this macro on C++ methods.
816 */
817#ifdef __GNUC__
818# define DECL_NO_INLINE(scope,type) __attribute__((noinline)) scope type
819#elif defined(_MSC_VER)
820# define DECL_NO_INLINE(scope,type) __declspec(noline) scope type
821#else
822# define DECL_NO_INLINE(scope,type) scope type
823#endif
824
825
826/** @def IN_RT_STATIC
827 * Used to indicate whether we're linking against a static IPRT
828 * or not. The IPRT symbols will be declared as hidden (if
829 * supported). Note that this define has no effect without setting
830 * IN_RT_R0, IN_RT_R3 or IN_RT_RC indicators are set first.
831 */
832
833/** @def IN_RT_R0
834 * Used to indicate whether we're inside the same link module as
835 * the HC Ring-0 Runtime Library.
836 */
837/** @def RTR0DECL(type)
838 * Runtime Library HC Ring-0 export or import declaration.
839 * @param type The return type of the function declaration.
840 */
841#ifdef IN_RT_R0
842# ifdef IN_RT_STATIC
843# define RTR0DECL(type) DECLHIDDEN(type) RTCALL
844# else
845# define RTR0DECL(type) DECLEXPORT(type) RTCALL
846# endif
847#else
848# define RTR0DECL(type) DECLIMPORT(type) RTCALL
849#endif
850
851/** @def IN_RT_R3
852 * Used to indicate whether we're inside the same link module as
853 * the HC Ring-3 Runtime Library.
854 */
855/** @def RTR3DECL(type)
856 * Runtime Library HC Ring-3 export or import declaration.
857 * @param type The return type of the function declaration.
858 */
859#ifdef IN_RT_R3
860# ifdef IN_RT_STATIC
861# define RTR3DECL(type) DECLHIDDEN(type) RTCALL
862# else
863# define RTR3DECL(type) DECLEXPORT(type) RTCALL
864# endif
865#else
866# define RTR3DECL(type) DECLIMPORT(type) RTCALL
867#endif
868
869/** @def IN_RT_RC
870 * Used to indicate whether we're inside the same link module as the raw-mode
871 * context (RC) runtime library.
872 */
873/** @def RTRCDECL(type)
874 * Runtime Library raw-mode context export or import declaration.
875 * @param type The return type of the function declaration.
876 */
877#ifdef IN_RT_RC
878# ifdef IN_RT_STATIC
879# define RTRCDECL(type) DECLHIDDEN(type) RTCALL
880# else
881# define RTRCDECL(type) DECLEXPORT(type) RTCALL
882# endif
883#else
884# define RTRCDECL(type) DECLIMPORT(type) RTCALL
885#endif
886
887/** @def RTDECL(type)
888 * Runtime Library export or import declaration.
889 * Functions declared using this macro exists in all contexts.
890 * @param type The return type of the function declaration.
891 */
892#if defined(IN_RT_R3) || defined(IN_RT_RC) || defined(IN_RT_R0)
893# ifdef IN_RT_STATIC
894# define RTDECL(type) DECLHIDDEN(type) RTCALL
895# else
896# define RTDECL(type) DECLEXPORT(type) RTCALL
897# endif
898#else
899# define RTDECL(type) DECLIMPORT(type) RTCALL
900#endif
901
902/** @def RTDATADECL(type)
903 * Runtime Library export or import declaration.
904 * Data declared using this macro exists in all contexts.
905 * @param type The return type of the function declaration.
906 */
907#if defined(IN_RT_R3) || defined(IN_RT_RC) || defined(IN_RT_R0)
908# ifdef IN_RT_STATIC
909# define RTDATADECL(type) DECLHIDDEN(type)
910# else
911# define RTDATADECL(type) DECLEXPORT(type)
912# endif
913#else
914# define RTDATADECL(type) DECLIMPORT(type)
915#endif
916
917/** @def RT_DECL_CLASS
918 * Declares an class living in the runtime.
919 */
920#if defined(IN_RT_R3) || defined(IN_RT_RC) || defined(IN_RT_R0)
921# ifdef IN_RT_STATIC
922# define RT_DECL_CLASS
923# else
924# define RT_DECL_CLASS DECLEXPORT_CLASS
925# endif
926#else
927# define RT_DECL_CLASS DECLIMPORT_CLASS
928#endif
929
930
931/** @def RT_NOCRT
932 * Symbol name wrapper for the No-CRT bits.
933 *
934 * In order to coexist in the same process as other CRTs, we need to
935 * decorate the symbols such that they don't conflict the ones in the
936 * other CRTs. The result of such conflicts / duplicate symbols can
937 * confuse the dynamic loader on Unix like systems.
938 *
939 * Define RT_WITHOUT_NOCRT_WRAPPERS to drop the wrapping.
940 * Define RT_WITHOUT_NOCRT_WRAPPER_ALIASES to drop the aliases to the
941 * wrapped names.
942 */
943/** @def RT_NOCRT_STR
944 * Same as RT_NOCRT only it'll return a double quoted string of the result.
945 */
946#ifndef RT_WITHOUT_NOCRT_WRAPPERS
947# define RT_NOCRT(name) nocrt_ ## name
948# define RT_NOCRT_STR(name) "nocrt_" # name
949#else
950# define RT_NOCRT(name) name
951# define RT_NOCRT_STR(name) #name
952#endif
953
954
955
956/** @def RT_LIKELY
957 * Give the compiler a hint that an expression is very likely to hold true.
958 *
959 * Some compilers support explicit branch prediction so that the CPU backend
960 * can hint the processor and also so that code blocks can be reordered such
961 * that the predicted path sees a more linear flow, thus improving cache
962 * behaviour, etc.
963 *
964 * IPRT provides the macros RT_LIKELY() and RT_UNLIKELY() as a way to utilize
965 * this compiler feature when present.
966 *
967 * A few notes about the usage:
968 *
969 * - Generally, use RT_UNLIKELY() with error condition checks (unless you
970 * have some _strong_ reason to do otherwise, in which case document it),
971 * and/or RT_LIKELY() with success condition checks, assuming you want
972 * to optimize for the success path.
973 *
974 * - Other than that, if you don't know the likelihood of a test succeeding
975 * from empirical or other 'hard' evidence, don't make predictions unless
976 * you happen to be a Dirk Gently.
977 *
978 * - These macros are meant to be used in places that get executed a lot. It
979 * is wasteful to make predictions in code that is executed rarely (e.g.
980 * at subsystem initialization time) as the basic block reordering that this
981 * affects can often generate larger code.
982 *
983 * - Note that RT_SUCCESS() and RT_FAILURE() already makes use of RT_LIKELY()
984 * and RT_UNLIKELY(). Should you wish for prediction free status checks,
985 * use the RT_SUCCESS_NP() and RT_FAILURE_NP() macros instead.
986 *
987 *
988 * @returns the boolean result of the expression.
989 * @param expr The expression that's very likely to be true.
990 * @see RT_UNLIKELY
991 */
992/** @def RT_UNLIKELY
993 * Give the compiler a hint that an expression is highly unlikely to hold true.
994 *
995 * See the usage instructions give in the RT_LIKELY() docs.
996 *
997 * @returns the boolean result of the expression.
998 * @param expr The expression that's very unlikely to be true.
999 * @see RT_LIKELY
1000 */
1001#if defined(__GNUC__)
1002# if __GNUC__ >= 3 && !defined(FORTIFY_RUNNING)
1003# define RT_LIKELY(expr) __builtin_expect(!!(expr), 1)
1004# define RT_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
1005# else
1006# define RT_LIKELY(expr) (expr)
1007# define RT_UNLIKELY(expr) (expr)
1008# endif
1009#else
1010# define RT_LIKELY(expr) (expr)
1011# define RT_UNLIKELY(expr) (expr)
1012#endif
1013
1014
1015/** @def RT_STR
1016 * Returns the argument as a string constant.
1017 * @param str Argument to stringify. */
1018#define RT_STR(str) #str
1019/** @def RT_XSTR
1020 * Returns the expanded argument as a string.
1021 * @param str Argument to expand and stringy. */
1022#define RT_XSTR(str) RT_STR(str)
1023
1024
1025/** @def RT_BIT
1026 * Convert a bit number into an integer bitmask (unsigned).
1027 * @param bit The bit number.
1028 */
1029#define RT_BIT(bit) ( 1U << (bit) )
1030
1031/** @def RT_BIT_32
1032 * Convert a bit number into a 32-bit bitmask (unsigned).
1033 * @param bit The bit number.
1034 */
1035#define RT_BIT_32(bit) ( UINT32_C(1) << (bit) )
1036
1037/** @def RT_BIT_64
1038 * Convert a bit number into a 64-bit bitmask (unsigned).
1039 * @param bit The bit number.
1040 */
1041#define RT_BIT_64(bit) ( UINT64_C(1) << (bit) )
1042
1043/** @def RT_ALIGN
1044 * Align macro.
1045 * @param u Value to align.
1046 * @param uAlignment The alignment. Power of two!
1047 *
1048 * @remark Be extremely careful when using this macro with type which sizeof != sizeof int.
1049 * When possible use any of the other RT_ALIGN_* macros. And when that's not
1050 * possible, make 101% sure that uAlignment is specified with a right sized type.
1051 *
1052 * Specifying an unsigned 32-bit alignment constant with a 64-bit value will give
1053 * you a 32-bit return value!
1054 *
1055 * In short: Don't use this macro. Use RT_ALIGN_T() instead.
1056 */
1057#define RT_ALIGN(u, uAlignment) ( ((u) + ((uAlignment) - 1)) & ~((uAlignment) - 1) )
1058
1059/** @def RT_ALIGN_T
1060 * Align macro.
1061 * @param u Value to align.
1062 * @param uAlignment The alignment. Power of two!
1063 * @param type Integer type to use while aligning.
1064 * @remark This macro is the preferred alignment macro, it doesn't have any of the pitfalls RT_ALIGN has.
1065 */
1066#define RT_ALIGN_T(u, uAlignment, type) ( ((type)(u) + ((uAlignment) - 1)) & ~(type)((uAlignment) - 1) )
1067
1068/** @def RT_ALIGN_32
1069 * Align macro for a 32-bit value.
1070 * @param u32 Value to align.
1071 * @param uAlignment The alignment. Power of two!
1072 */
1073#define RT_ALIGN_32(u32, uAlignment) RT_ALIGN_T(u32, uAlignment, uint32_t)
1074
1075/** @def RT_ALIGN_64
1076 * Align macro for a 64-bit value.
1077 * @param u64 Value to align.
1078 * @param uAlignment The alignment. Power of two!
1079 */
1080#define RT_ALIGN_64(u64, uAlignment) RT_ALIGN_T(u64, uAlignment, uint64_t)
1081
1082/** @def RT_ALIGN_Z
1083 * Align macro for size_t.
1084 * @param cb Value to align.
1085 * @param uAlignment The alignment. Power of two!
1086 */
1087#define RT_ALIGN_Z(cb, uAlignment) RT_ALIGN_T(cb, uAlignment, size_t)
1088
1089/** @def RT_ALIGN_P
1090 * Align macro for pointers.
1091 * @param pv Value to align.
1092 * @param uAlignment The alignment. Power of two!
1093 */
1094#define RT_ALIGN_P(pv, uAlignment) RT_ALIGN_PT(pv, uAlignment, void *)
1095
1096/** @def RT_ALIGN_PT
1097 * Align macro for pointers with type cast.
1098 * @param u Value to align.
1099 * @param uAlignment The alignment. Power of two!
1100 * @param CastType The type to cast the result to.
1101 */
1102#define RT_ALIGN_PT(u, uAlignment, CastType) ( (CastType)RT_ALIGN_T(u, uAlignment, uintptr_t) )
1103
1104/** @def RT_ALIGN_R3PT
1105 * Align macro for ring-3 pointers with type cast.
1106 * @param u Value to align.
1107 * @param uAlignment The alignment. Power of two!
1108 * @param CastType The type to cast the result to.
1109 */
1110#define RT_ALIGN_R3PT(u, uAlignment, CastType) ( (CastType)RT_ALIGN_T(u, uAlignment, RTR3UINTPTR) )
1111
1112/** @def RT_ALIGN_R0PT
1113 * Align macro for ring-0 pointers with type cast.
1114 * @param u Value to align.
1115 * @param uAlignment The alignment. Power of two!
1116 * @param CastType The type to cast the result to.
1117 */
1118#define RT_ALIGN_R0PT(u, uAlignment, CastType) ( (CastType)RT_ALIGN_T(u, uAlignment, RTR0UINTPTR) )
1119
1120/** @def RT_ALIGN_GCPT
1121 * Align macro for GC pointers with type cast.
1122 * @param u Value to align.
1123 * @param uAlignment The alignment. Power of two!
1124 * @param CastType The type to cast the result to.
1125 */
1126#define RT_ALIGN_GCPT(u, uAlignment, CastType) ( (CastType)RT_ALIGN_T(u, uAlignment, RTGCUINTPTR) )
1127
1128
1129/** @def RT_OFFSETOF
1130 * Our own special offsetof() variant, returns a signed result.
1131 *
1132 * This differs from the usual offsetof() in that it's not relying on builtin
1133 * compiler stuff and thus can use variables in arrays the structure may
1134 * contain. This is useful to determine the sizes of structures ending
1135 * with a variable length field.
1136 *
1137 * @returns offset into the structure of the specified member. signed.
1138 * @param type Structure type.
1139 * @param member Member.
1140 */
1141#define RT_OFFSETOF(type, member) ( (int)(uintptr_t)&( ((type *)(void *)0)->member) )
1142
1143/** @def RT_UOFFSETOF
1144 * Our own special offsetof() variant, returns an unsigned result.
1145 *
1146 * This differs from the usual offsetof() in that it's not relying on builtin
1147 * compiler stuff and thus can use variables in arrays the structure may
1148 * contain. This is useful to determine the sizes of structures ending
1149 * with a variable length field.
1150 *
1151 * @returns offset into the structure of the specified member. unsigned.
1152 * @param type Structure type.
1153 * @param member Member.
1154 */
1155#define RT_UOFFSETOF(type, member) ( (uintptr_t)&( ((type *)(void *)0)->member) )
1156
1157/** @def RT_OFFSETOF_ADD
1158 * RT_OFFSETOF with an addend.
1159 *
1160 * @returns offset into the structure of the specified member. signed.
1161 * @param type Structure type.
1162 * @param member Member.
1163 * @param addend The addend to add to the offset.
1164 */
1165#define RT_OFFSETOF_ADD(type, member, addend) ( (int)RT_UOFFSETOF_ADD(type, member, addend) )
1166
1167/** @def RT_UOFFSETOF_ADD
1168 * RT_UOFFSETOF with an addend.
1169 *
1170 * @returns offset into the structure of the specified member. signed.
1171 * @param type Structure type.
1172 * @param member Member.
1173 * @param addend The addend to add to the offset.
1174 */
1175#define RT_UOFFSETOF_ADD(type, member, addend) ( (uintptr_t)&( ((type *)(void *)(uintptr_t)(addend))->member) )
1176
1177/** @def RT_SIZEOFMEMB
1178 * Get the size of a structure member.
1179 *
1180 * @returns size of the structure member.
1181 * @param type Structure type.
1182 * @param member Member.
1183 */
1184#define RT_SIZEOFMEMB(type, member) ( sizeof(((type *)(void *)0)->member) )
1185
1186/** @def RT_FROM_MEMBER
1187 * Convert a pointer to a structure member into a pointer to the structure.
1188 *
1189 * @returns pointer to the structure.
1190 * @param pMem Pointer to the member.
1191 * @param Type Structure type.
1192 * @param Member Member name.
1193 */
1194#define RT_FROM_MEMBER(pMem, Type, Member) ( (Type *) ((uint8_t *)(void *)(pMem) - RT_UOFFSETOF(Type, Member)) )
1195
1196/** @def RT_FROM_CPP_MEMBER
1197 * Same as RT_FROM_MEMBER except it avoids the annoying g++ warnings about
1198 * invalid access to non-static data member of NULL object.
1199 *
1200 * @returns pointer to the structure.
1201 * @param pMem Pointer to the member.
1202 * @param Type Structure type.
1203 * @param Member Member name.
1204 *
1205 * @remarks Using the __builtin_offsetof does not shut up the compiler.
1206 */
1207#if defined(__GNUC__) && defined(__cplusplus)
1208# define RT_FROM_CPP_MEMBER(pMem, Type, Member) \
1209 ( (Type *) ((uintptr_t)(pMem) - (uintptr_t)&((Type *)0x1000)->Member + 0x1000U) )
1210#else
1211# define RT_FROM_CPP_MEMBER(pMem, Type, Member) RT_FROM_MEMBER(pMem, Type, Member)
1212#endif
1213
1214/** @def RT_ELEMENTS
1215 * Calculates the number of elements in a statically sized array.
1216 * @returns Element count.
1217 * @param aArray Array in question.
1218 */
1219#define RT_ELEMENTS(aArray) ( sizeof(aArray) / sizeof((aArray)[0]) )
1220
1221/**
1222 * Checks if the value is a power of two.
1223 *
1224 * @returns true if power of two, false if not.
1225 * @param uVal The value to test.
1226 * @remarks 0 is a power of two.
1227 * @see VERR_NOT_POWER_OF_TWO
1228 */
1229#define RT_IS_POWER_OF_TWO(uVal) ( ((uVal) & ((uVal) - 1)) == 0)
1230
1231#ifdef RT_OS_OS2
1232/* Undefine RT_MAX since there is an unfortunate clash with the max
1233 resource type define in os2.h. */
1234# undef RT_MAX
1235#endif
1236
1237/** @def RT_MAX
1238 * Finds the maximum value.
1239 * @returns The higher of the two.
1240 * @param Value1 Value 1
1241 * @param Value2 Value 2
1242 */
1243#define RT_MAX(Value1, Value2) ( (Value1) >= (Value2) ? (Value1) : (Value2) )
1244
1245/** @def RT_MIN
1246 * Finds the minimum value.
1247 * @returns The lower of the two.
1248 * @param Value1 Value 1
1249 * @param Value2 Value 2
1250 */
1251#define RT_MIN(Value1, Value2) ( (Value1) <= (Value2) ? (Value1) : (Value2) )
1252
1253/** @def RT_CLAMP
1254 * Clamps the value to minimum and maximum values.
1255 * @returns The clamped value.
1256 * @param Value The value to check.
1257 * @param Min Minimum value.
1258 * @param Max Maximum value.
1259 */
1260#define RT_CLAMP(Value, Min, Max) ( (Value) > (Max) ? (Max) : (Value) < (Min) ? (Min) : (Value) )
1261
1262/** @def RT_ABS
1263 * Get the absolute (non-negative) value.
1264 * @returns The absolute value of Value.
1265 * @param Value The value.
1266 */
1267#define RT_ABS(Value) ( (Value) >= 0 ? (Value) : -(Value) )
1268
1269/** @def RT_BOOL
1270 * Turn non-zero/zero into true/false
1271 * @returns The resulting boolean value.
1272 * @param Value The value.
1273 */
1274#define RT_BOOL(Value) ( !!(Value) )
1275
1276/** @def RT_LODWORD
1277 * Gets the low dword (=uint32_t) of something. */
1278#define RT_LODWORD(a) ( (uint32_t)(a) )
1279
1280/** @def RT_HIDWORD
1281 * Gets the high dword (=uint32_t) of a 64-bit of something. */
1282#define RT_HIDWORD(a) ( (uint32_t)((a) >> 32) )
1283
1284/** @def RT_LOWORD
1285 * Gets the low word (=uint16_t) of something. */
1286#define RT_LOWORD(a) ( (a) & 0xffff )
1287
1288/** @def RT_HIWORD
1289 * Gets the high word (=uint16_t) of a 32-bit something. */
1290#define RT_HIWORD(a) ( (a) >> 16 )
1291
1292/** @def RT_LOBYTE
1293 * Gets the low byte of something. */
1294#define RT_LOBYTE(a) ( (a) & 0xff )
1295
1296/** @def RT_HIBYTE
1297 * Gets the low byte of a 16-bit something. */
1298#define RT_HIBYTE(a) ( (a) >> 8 )
1299
1300/** @def RT_BYTE1
1301 * Gets the first byte of something. */
1302#define RT_BYTE1(a) ( (a) & 0xff )
1303
1304/** @def RT_BYTE2
1305 * Gets the second byte of something. */
1306#define RT_BYTE2(a) ( ((a) >> 8) & 0xff )
1307
1308/** @def RT_BYTE3
1309 * Gets the second byte of something. */
1310#define RT_BYTE3(a) ( ((a) >> 16) & 0xff )
1311
1312/** @def RT_BYTE4
1313 * Gets the fourth byte of something. */
1314#define RT_BYTE4(a) ( ((a) >> 24) & 0xff )
1315
1316/** @def RT_BYTE5
1317 * Gets the fifth byte of something. */
1318#define RT_BYTE5(a) (((a) >> 32) & 0xff)
1319
1320/** @def RT_BYTE6
1321 * Gets the sixth byte of something. */
1322#define RT_BYTE6(a) (((a) >> 40) & 0xff)
1323
1324/** @def RT_BYTE7
1325 * Gets the seventh byte of something. */
1326#define RT_BYTE7(a) (((a) >> 48) & 0xff)
1327
1328/** @def RT_BYTE8
1329 * Gets the eight byte of something. */
1330#define RT_BYTE8(a) (((a) >> 56) & 0xff)
1331
1332
1333/** @def RT_MAKE_U64
1334 * Constructs a uint64_t value from two uint32_t values.
1335 */
1336#define RT_MAKE_U64(Lo, Hi) ( (uint64_t)((uint32_t)(Hi)) << 32 | (uint32_t)(Lo) )
1337
1338/** @def RT_MAKE_U64_FROM_U16
1339 * Constructs a uint64_t value from four uint16_t values.
1340 */
1341#define RT_MAKE_U64_FROM_U16(w0, w1, w2, w3) \
1342 ((uint64_t)( (uint64_t)((uint16_t)(w3)) << 48 \
1343 | (uint64_t)((uint16_t)(w2)) << 32 \
1344 | (uint32_t)((uint16_t)(w1)) << 16 \
1345 | (uint16_t)(w0) ))
1346
1347/** @def RT_MAKE_U64_FROM_U8
1348 * Constructs a uint64_t value from eight uint8_t values.
1349 */
1350#define RT_MAKE_U64_FROM_U8(b0, b1, b2, b3, b4, b5, b6, b7) \
1351 ((uint64_t)( (uint64_t)((uint8_t)(b7)) << 56 \
1352 | (uint64_t)((uint8_t)(b6)) << 48 \
1353 | (uint64_t)((uint8_t)(b5)) << 40 \
1354 | (uint64_t)((uint8_t)(b4)) << 32 \
1355 | (uint32_t)((uint8_t)(b3)) << 24 \
1356 | (uint32_t)((uint8_t)(b2)) << 16 \
1357 | (uint16_t)((uint8_t)(b1)) << 8 \
1358 | (uint8_t)(b0) ))
1359
1360/** @def RT_MAKE_U32
1361 * Constructs a uint32_t value from two uint16_t values.
1362 */
1363#define RT_MAKE_U32(Lo, Hi) \
1364 ((uint32_t)( (uint32_t)((uint16_t)(Hi)) << 16 \
1365 | (uint16_t)(Lo) ))
1366
1367/** @def RT_MAKE_U32_FROM_U8
1368 * Constructs a uint32_t value from four uint8_t values.
1369 */
1370#define RT_MAKE_U32_FROM_U8(b0, b1, b2, b3) \
1371 ((uint32_t)( (uint32_t)((uint8_t)(b3)) << 24 \
1372 | (uint32_t)((uint8_t)(b2)) << 16 \
1373 | (uint16_t)((uint8_t)(b1)) << 8 \
1374 | (uint8_t)(b0) ))
1375
1376/** @def RT_MAKE_U16
1377 * Constructs a uint16_t value from two uint8_t values.
1378 */
1379#define RT_MAKE_U16(Lo, Hi) \
1380 ((uint16_t)( (uint16_t)((uint8_t)(Hi)) << 8 \
1381 | (uint8_t)(Lo) ))
1382
1383
1384/** @def RT_BSWAP_U64
1385 * Reverses the byte order of an uint64_t value. */
1386#if 0
1387# define RT_BSWAP_U64(u64) RT_BSWAP_U64_C(u64)
1388#elif defined(__GNUC__)
1389# define RT_BSWAP_U64(u64) (__builtin_constant_p((u64)) \
1390 ? RT_BSWAP_U64_C(u64) : ASMByteSwapU64(u64))
1391#else
1392# define RT_BSWAP_U64(u64) ASMByteSwapU64(u64)
1393#endif
1394
1395/** @def RT_BSWAP_U32
1396 * Reverses the byte order of an uint32_t value. */
1397#if 0
1398# define RT_BSWAP_U32(u32) RT_BSWAP_U32_C(u32)
1399#elif defined(__GNUC__)
1400# define RT_BSWAP_U32(u32) (__builtin_constant_p((u32)) \
1401 ? RT_BSWAP_U32_C(u32) : ASMByteSwapU32(u32))
1402#else
1403# define RT_BSWAP_U32(u32) ASMByteSwapU32(u32)
1404#endif
1405
1406/** @def RT_BSWAP_U16
1407 * Reverses the byte order of an uint16_t value. */
1408#if 0
1409# define RT_BSWAP_U16(u16) RT_BSWAP_U16_C(u16)
1410#elif defined(__GNUC__)
1411# define RT_BSWAP_U16(u16) (__builtin_constant_p((u16)) \
1412 ? RT_BSWAP_U16_C(u16) : ASMByteSwapU16(u16))
1413#else
1414# define RT_BSWAP_U16(u16) ASMByteSwapU16(u16)
1415#endif
1416
1417
1418/** @def RT_BSWAP_U64_C
1419 * Reverses the byte order of an uint64_t constant. */
1420#define RT_BSWAP_U64_C(u64) RT_MAKE_U64(RT_BSWAP_U32_C((u64) >> 32), RT_BSWAP_U32_C((u64) & 0xffffffff))
1421
1422/** @def RT_BSWAP_U32_C
1423 * Reverses the byte order of an uint32_t constant. */
1424#define RT_BSWAP_U32_C(u32) RT_MAKE_U32_FROM_U8(RT_BYTE4(u32), RT_BYTE3(u32), RT_BYTE2(u32), RT_BYTE1(u32))
1425
1426/** @def RT_BSWAP_U16_C
1427 * Reverses the byte order of an uint16_t constant. */
1428#define RT_BSWAP_U16_C(u16) RT_MAKE_U16(RT_HIBYTE(u16), RT_LOBYTE(u16))
1429
1430
1431/** @def RT_H2LE_U64
1432 * Converts an uint64_t value from host to little endian byte order. */
1433#ifdef RT_BIG_ENDIAN
1434# define RT_H2LE_U64(u64) RT_BSWAP_U64(u64)
1435#else
1436# define RT_H2LE_U64(u64) (u64)
1437#endif
1438
1439/** @def RT_H2LE_U64_C
1440 * Converts an uint64_t constant from host to little endian byte order. */
1441#ifdef RT_BIG_ENDIAN
1442# define RT_H2LE_U64_C(u64) RT_BSWAP_U64_C(u64)
1443#else
1444# define RT_H2LE_U64_C(u64) (u64)
1445#endif
1446
1447/** @def RT_H2LE_U32
1448 * Converts an uint32_t value from host to little endian byte order. */
1449#ifdef RT_BIG_ENDIAN
1450# define RT_H2LE_U32(u32) RT_BSWAP_U32(u32)
1451#else
1452# define RT_H2LE_U32(u32) (u32)
1453#endif
1454
1455/** @def RT_H2LE_U32_C
1456 * Converts an uint32_t constant from host to little endian byte order. */
1457#ifdef RT_BIG_ENDIAN
1458# define RT_H2LE_U32_C(u32) RT_BSWAP_U32_C(u32)
1459#else
1460# define RT_H2LE_U32_C(u32) (u32)
1461#endif
1462
1463/** @def RT_H2LE_U16
1464 * Converts an uint16_t value from host to little endian byte order. */
1465#ifdef RT_BIG_ENDIAN
1466# define RT_H2LE_U16(u16) RT_BSWAP_U16(u16)
1467#else
1468# define RT_H2LE_U16(u16) (u16)
1469#endif
1470
1471/** @def RT_H2LE_U16_C
1472 * Converts an uint16_t constant from host to little endian byte order. */
1473#ifdef RT_BIG_ENDIAN
1474# define RT_H2LE_U16_C(u16) RT_BSWAP_U16_C(u16)
1475#else
1476# define RT_H2LE_U16_C(u16) (u16)
1477#endif
1478
1479
1480/** @def RT_LE2H_U64
1481 * Converts an uint64_t value from little endian to host byte order. */
1482#ifdef RT_BIG_ENDIAN
1483# define RT_LE2H_U64(u64) RT_BSWAP_U64(u64)
1484#else
1485# define RT_LE2H_U64(u64) (u64)
1486#endif
1487
1488/** @def RT_LE2H_U64_C
1489 * Converts an uint64_t constant from little endian to host byte order. */
1490#ifdef RT_BIG_ENDIAN
1491# define RT_LE2H_U64_C(u64) RT_BSWAP_U64_C(u64)
1492#else
1493# define RT_LE2H_U64_C(u64) (u64)
1494#endif
1495
1496/** @def RT_LE2H_U32
1497 * Converts an uint32_t value from little endian to host byte order. */
1498#ifdef RT_BIG_ENDIAN
1499# define RT_LE2H_U32(u32) RT_BSWAP_U32(u32)
1500#else
1501# define RT_LE2H_U32(u32) (u32)
1502#endif
1503
1504/** @def RT_LE2H_U32_C
1505 * Converts an uint32_t constant from little endian to host byte order. */
1506#ifdef RT_BIG_ENDIAN
1507# define RT_LE2H_U32_C(u32) RT_BSWAP_U32_C(u32)
1508#else
1509# define RT_LE2H_U32_C(u32) (u32)
1510#endif
1511
1512/** @def RT_LE2H_U16
1513 * Converts an uint16_t value from little endian to host byte order. */
1514#ifdef RT_BIG_ENDIAN
1515# define RT_LE2H_U16(u16) RT_BSWAP_U16(u16)
1516#else
1517# define RT_LE2H_U16(u16) (u16)
1518#endif
1519
1520/** @def RT_LE2H_U16_C
1521 * Converts an uint16_t constant from little endian to host byte order. */
1522#ifdef RT_BIG_ENDIAN
1523# define RT_LE2H_U16_C(u16) RT_BSWAP_U16_C(u16)
1524#else
1525# define RT_LE2H_U16_C(u16) (u16)
1526#endif
1527
1528
1529/** @def RT_H2BE_U64
1530 * Converts an uint64_t value from host to big endian byte order. */
1531#ifdef RT_BIG_ENDIAN
1532# define RT_H2BE_U64(u64) (u64)
1533#else
1534# define RT_H2BE_U64(u64) RT_BSWAP_U64(u64)
1535#endif
1536
1537/** @def RT_H2BE_U64_C
1538 * Converts an uint64_t constant from host to big endian byte order. */
1539#ifdef RT_BIG_ENDIAN
1540# define RT_H2BE_U64_C(u64) (u64)
1541#else
1542# define RT_H2BE_U64_C(u64) RT_BSWAP_U64_C(u64)
1543#endif
1544
1545/** @def RT_H2BE_U32
1546 * Converts an uint32_t value from host to big endian byte order. */
1547#ifdef RT_BIG_ENDIAN
1548# define RT_H2BE_U32(u32) (u32)
1549#else
1550# define RT_H2BE_U32(u32) RT_BSWAP_U32(u32)
1551#endif
1552
1553/** @def RT_H2BE_U32_C
1554 * Converts an uint32_t constant from host to big endian byte order. */
1555#ifdef RT_BIG_ENDIAN
1556# define RT_H2BE_U32_C(u32) (u32)
1557#else
1558# define RT_H2BE_U32_C(u32) RT_BSWAP_U32_C(u32)
1559#endif
1560
1561/** @def RT_H2BE_U16
1562 * Converts an uint16_t value from host to big endian byte order. */
1563#ifdef RT_BIG_ENDIAN
1564# define RT_H2BE_U16(u16) (u16)
1565#else
1566# define RT_H2BE_U16(u16) RT_BSWAP_U16(u16)
1567#endif
1568
1569/** @def RT_H2BE_U16_C
1570 * Converts an uint16_t constant from host to big endian byte order. */
1571#ifdef RT_BIG_ENDIAN
1572# define RT_H2BE_U16_C(u16) (u16)
1573#else
1574# define RT_H2BE_U16_C(u16) RT_BSWAP_U16_C(u16)
1575#endif
1576
1577/** @def RT_BE2H_U64
1578 * Converts an uint64_t value from big endian to host byte order. */
1579#ifdef RT_BIG_ENDIAN
1580# define RT_BE2H_U64(u64) (u64)
1581#else
1582# define RT_BE2H_U64(u64) RT_BSWAP_U64(u64)
1583#endif
1584
1585/** @def RT_BE2H_U64
1586 * Converts an uint64_t constant from big endian to host byte order. */
1587#ifdef RT_BIG_ENDIAN
1588# define RT_BE2H_U64_C(u64) (u64)
1589#else
1590# define RT_BE2H_U64_C(u64) RT_BSWAP_U64_C(u64)
1591#endif
1592
1593/** @def RT_BE2H_U32
1594 * Converts an uint32_t value from big endian to host byte order. */
1595#ifdef RT_BIG_ENDIAN
1596# define RT_BE2H_U32(u32) (u32)
1597#else
1598# define RT_BE2H_U32(u32) RT_BSWAP_U32(u32)
1599#endif
1600
1601/** @def RT_BE2H_U32_C
1602 * Converts an uint32_t value from big endian to host byte order. */
1603#ifdef RT_BIG_ENDIAN
1604# define RT_BE2H_U32_C(u32) (u32)
1605#else
1606# define RT_BE2H_U32_C(u32) RT_BSWAP_U32_C(u32)
1607#endif
1608
1609/** @def RT_BE2H_U16
1610 * Converts an uint16_t value from big endian to host byte order. */
1611#ifdef RT_BIG_ENDIAN
1612# define RT_BE2H_U16(u16) (u16)
1613#else
1614# define RT_BE2H_U16(u16) RT_BSWAP_U16(u16)
1615#endif
1616
1617/** @def RT_BE2H_U16_C
1618 * Converts an uint16_t constant from big endian to host byte order. */
1619#ifdef RT_BIG_ENDIAN
1620# define RT_BE2H_U16_C(u16) (u16)
1621#else
1622# define RT_BE2H_U16_C(u16) RT_BSWAP_U16_C(u16)
1623#endif
1624
1625
1626/** @def RT_H2N_U64
1627 * Converts an uint64_t value from host to network byte order. */
1628#define RT_H2N_U64(u64) RT_H2BE_U64(u64)
1629
1630/** @def RT_H2N_U64_C
1631 * Converts an uint64_t constant from host to network byte order. */
1632#define RT_H2N_U64_C(u64) RT_H2BE_U64_C(u64)
1633
1634/** @def RT_H2N_U32
1635 * Converts an uint32_t value from host to network byte order. */
1636#define RT_H2N_U32(u32) RT_H2BE_U32(u32)
1637
1638/** @def RT_H2N_U32_C
1639 * Converts an uint32_t constant from host to network byte order. */
1640#define RT_H2N_U32_C(u32) RT_H2BE_U32_C(u32)
1641
1642/** @def RT_H2N_U16
1643 * Converts an uint16_t value from host to network byte order. */
1644#define RT_H2N_U16(u16) RT_H2BE_U16(u16)
1645
1646/** @def RT_H2N_U16_C
1647 * Converts an uint16_t constant from host to network byte order. */
1648#define RT_H2N_U16_C(u16) RT_H2BE_U16_C(u16)
1649
1650/** @def RT_N2H_U64
1651 * Converts an uint64_t value from network to host byte order. */
1652#define RT_N2H_U64(u64) RT_BE2H_U64(u64)
1653
1654/** @def RT_N2H_U64_C
1655 * Converts an uint64_t constant from network to host byte order. */
1656#define RT_N2H_U64_C(u64) RT_BE2H_U64_C(u64)
1657
1658/** @def RT_N2H_U32
1659 * Converts an uint32_t value from network to host byte order. */
1660#define RT_N2H_U32(u32) RT_BE2H_U32(u32)
1661
1662/** @def RT_N2H_U32_C
1663 * Converts an uint32_t constant from network to host byte order. */
1664#define RT_N2H_U32_C(u32) RT_BE2H_U32_C(u32)
1665
1666/** @def RT_N2H_U16
1667 * Converts an uint16_t value from network to host byte order. */
1668#define RT_N2H_U16(u16) RT_BE2H_U16(u16)
1669
1670/** @def RT_N2H_U16_C
1671 * Converts an uint16_t value from network to host byte order. */
1672#define RT_N2H_U16_C(u16) RT_BE2H_U16_C(u16)
1673
1674
1675/*
1676 * The BSD sys/param.h + machine/param.h file is a major source of
1677 * namespace pollution. Kill off some of the worse ones unless we're
1678 * compiling kernel code.
1679 */
1680#if defined(RT_OS_DARWIN) \
1681 && !defined(KERNEL) \
1682 && !defined(RT_NO_BSD_PARAM_H_UNDEFING) \
1683 && ( defined(_SYS_PARAM_H_) || defined(_I386_PARAM_H_) )
1684/* sys/param.h: */
1685# undef PSWP
1686# undef PVM
1687# undef PINOD
1688# undef PRIBO
1689# undef PVFS
1690# undef PZERO
1691# undef PSOCK
1692# undef PWAIT
1693# undef PLOCK
1694# undef PPAUSE
1695# undef PUSER
1696# undef PRIMASK
1697# undef MINBUCKET
1698# undef MAXALLOCSAVE
1699# undef FSHIFT
1700# undef FSCALE
1701
1702/* i386/machine.h: */
1703# undef ALIGN
1704# undef ALIGNBYTES
1705# undef DELAY
1706# undef STATUS_WORD
1707# undef USERMODE
1708# undef BASEPRI
1709# undef MSIZE
1710# undef CLSIZE
1711# undef CLSIZELOG2
1712#endif
1713
1714
1715/** @def NULL
1716 * NULL pointer.
1717 */
1718#ifndef NULL
1719# ifdef __cplusplus
1720# define NULL 0
1721# else
1722# define NULL ((void*)0)
1723# endif
1724#endif
1725
1726/** @def NIL_OFFSET
1727 * NIL offset.
1728 * Whenever we use offsets instead of pointers to save space and relocation effort
1729 * NIL_OFFSET shall be used as the equivalent to NULL.
1730 */
1731#define NIL_OFFSET (~0U)
1732
1733/** @def NOREF
1734 * Keeps the compiler from bitching about an unused parameter.
1735 */
1736#define NOREF(var) (void)(var)
1737
1738/** @def RT_BREAKPOINT
1739 * Emit a debug breakpoint instruction.
1740 *
1741 * @remarks In the x86/amd64 gnu world we add a nop instruction after the int3
1742 * to force gdb to remain at the int3 source line.
1743 * @remarks The L4 kernel will try make sense of the breakpoint, thus the jmp on
1744 * x86/amd64.
1745 */
1746#ifdef __GNUC__
1747# if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
1748# if !defined(__L4ENV__)
1749# define RT_BREAKPOINT() __asm__ __volatile__("int $3\n\tnop\n\t")
1750# else
1751# define RT_BREAKPOINT() __asm__ __volatile__("int3; jmp 1f; 1:\n\t")
1752# endif
1753# elif defined(RT_ARCH_SPARC64)
1754# define RT_BREAKPOINT() __asm__ __volatile__("illtrap 0\n\t") /** @todo Sparc64: this is just a wild guess. */
1755# elif defined(RT_ARCH_SPARC)
1756# define RT_BREAKPOINT() __asm__ __volatile__("unimp 0\n\t") /** @todo Sparc: this is just a wild guess (same as Sparc64, just different name). */
1757# endif
1758#endif
1759#ifdef _MSC_VER
1760# define RT_BREAKPOINT() __debugbreak()
1761#endif
1762#if defined(__IBMC__) || defined(__IBMCPP__)
1763# define RT_BREAKPOINT() __interrupt(3)
1764#endif
1765#ifndef RT_BREAKPOINT
1766# error "This compiler/arch is not supported!"
1767#endif
1768
1769
1770/** @defgroup grp_rt_cdefs_size Size Constants
1771 * (Of course, these are binary computer terms, not SI.)
1772 * @{
1773 */
1774/** 1 K (Kilo) (1 024). */
1775#define _1K 0x00000400
1776/** 4 K (Kilo) (4 096). */
1777#define _4K 0x00001000
1778/** 32 K (Kilo) (32 678). */
1779#define _32K 0x00008000
1780/** 64 K (Kilo) (65 536). */
1781#define _64K 0x00010000
1782/** 128 K (Kilo) (131 072). */
1783#define _128K 0x00020000
1784/** 256 K (Kilo) (262 144). */
1785#define _256K 0x00040000
1786/** 512 K (Kilo) (524 288). */
1787#define _512K 0x00080000
1788/** 1 M (Mega) (1 048 576). */
1789#define _1M 0x00100000
1790/** 2 M (Mega) (2 097 152). */
1791#define _2M 0x00200000
1792/** 4 M (Mega) (4 194 304). */
1793#define _4M 0x00400000
1794/** 1 G (Giga) (1 073 741 824). (32-bit) */
1795#define _1G 0x40000000
1796/** 1 G (Giga) (1 073 741 824). (64-bit) */
1797#define _1G64 0x40000000LL
1798/** 2 G (Giga) (2 147 483 648). (32-bit) */
1799#define _2G32 0x80000000U
1800/** 2 G (Giga) (2 147 483 648). (64-bit) */
1801#define _2G 0x0000000080000000LL
1802/** 4 G (Giga) (4 294 967 296). */
1803#define _4G 0x0000000100000000LL
1804/** 1 T (Tera) (1 099 511 627 776). */
1805#define _1T 0x0000010000000000LL
1806/** 1 P (Peta) (1 125 899 906 842 624). */
1807#define _1P 0x0004000000000000LL
1808/** 1 E (Exa) (1 152 921 504 606 846 976). */
1809#define _1E 0x1000000000000000LL
1810/** 2 E (Exa) (2 305 843 009 213 693 952). */
1811#define _2E 0x2000000000000000ULL
1812/** @} */
1813
1814
1815/** @defgroup grp_rt_cdefs_time Time Constants
1816 * @{
1817 */
1818/** 1 hour expressed in nanoseconds (64-bit). */
1819#define RT_NS_1HOUR UINT64_C(3600000000000)
1820/** 1 minute expressed in nanoseconds (64-bit). */
1821#define RT_NS_1MIN UINT64_C(60000000000)
1822/** 1 second expressed in nanoseconds. */
1823#define RT_NS_1SEC UINT32_C(1000000000)
1824/** 100 millsecond expressed in nanoseconds. */
1825#define RT_NS_100MS UINT32_C(100000000)
1826/** 10 millsecond expressed in nanoseconds. */
1827#define RT_NS_10MS UINT32_C(10000000)
1828/** 1 millsecond expressed in nanoseconds. */
1829#define RT_NS_1MS UINT32_C(1000000)
1830/** 100 microseconds expressed in nanoseconds. */
1831#define RT_NS_100US UINT32_C(100000)
1832/** 10 microseconds expressed in nanoseconds. */
1833#define RT_NS_10US UINT32_C(10000)
1834/** 1 microsecond expressed in nanoseconds. */
1835#define RT_NS_1US UINT32_C(1000)
1836
1837/** 1 second expressed in nanoseconds - 64-bit type. */
1838#define RT_NS_1SEC_64 UINT64_C(1000000000)
1839/** 100 millsecond expressed in nanoseconds - 64-bit type. */
1840#define RT_NS_100MS_64 UINT64_C(100000000)
1841/** 10 millsecond expressed in nanoseconds - 64-bit type. */
1842#define RT_NS_10MS_64 UINT64_C(10000000)
1843/** 1 millsecond expressed in nanoseconds - 64-bit type. */
1844#define RT_NS_1MS_64 UINT64_C(1000000)
1845/** 100 microseconds expressed in nanoseconds - 64-bit type. */
1846#define RT_NS_100US_64 UINT64_C(100000)
1847/** 10 microseconds expressed in nanoseconds - 64-bit type. */
1848#define RT_NS_10US_64 UINT64_C(10000)
1849/** 1 microsecond expressed in nanoseconds - 64-bit type. */
1850#define RT_NS_1US_64 UINT64_C(1000)
1851
1852/** 1 hour expressed in microseconds. */
1853#define RT_US_1HOUR UINT32_C(3600000000)
1854/** 1 minute expressed in microseconds. */
1855#define RT_US_1MIN UINT32_C(60000000)
1856/** 1 second expressed in microseconds. */
1857#define RT_US_1SEC UINT32_C(1000000)
1858/** 100 millsecond expressed in microseconds. */
1859#define RT_US_100MS UINT32_C(100000)
1860/** 10 millsecond expressed in microseconds. */
1861#define RT_US_10MS UINT32_C(10000)
1862/** 1 millsecond expressed in microseconds. */
1863#define RT_US_1MS UINT32_C(1000)
1864
1865/** 1 hour expressed in microseconds - 64-bit type. */
1866#define RT_US_1HOUR_64 UINT64_C(3600000000)
1867/** 1 minute expressed in microseconds - 64-bit type. */
1868#define RT_US_1MIN_64 UINT64_C(60000000)
1869/** 1 second expressed in microseconds - 64-bit type. */
1870#define RT_US_1SEC_64 UINT64_C(1000000)
1871/** 100 millsecond expressed in microseconds - 64-bit type. */
1872#define RT_US_100MS_64 UINT64_C(100000)
1873/** 10 millsecond expressed in microseconds - 64-bit type. */
1874#define RT_US_10MS_64 UINT64_C(10000)
1875/** 1 millsecond expressed in microseconds - 64-bit type. */
1876#define RT_US_1MS_64 UINT64_C(1000)
1877
1878/** 1 hour expressed in milliseconds. */
1879#define RT_MS_1HOUR UINT32_C(3600000)
1880/** 1 minute expressed in milliseconds. */
1881#define RT_MS_1MIN UINT32_C(60000)
1882/** 1 second expressed in milliseconds. */
1883#define RT_MS_1SEC UINT32_C(1000)
1884
1885/** 1 hour expressed in milliseconds - 64-bit type. */
1886#define RT_MS_1HOUR_64 UINT64_C(3600000)
1887/** 1 minute expressed in milliseconds - 64-bit type. */
1888#define RT_MS_1MIN_64 UINT64_C(60000)
1889/** 1 second expressed in milliseconds - 64-bit type. */
1890#define RT_MS_1SEC_64 UINT64_C(1000)
1891
1892/** The number of seconds per week. */
1893#define RT_SEC_1WEEK UINT32_C(604800)
1894/** The number of seconds per day. */
1895#define RT_SEC_1DAY UINT32_C(86400)
1896/** The number of seconds per hour. */
1897#define RT_SEC_1HOUR UINT32_C(3600)
1898
1899/** The number of seconds per week - 64-bit type. */
1900#define RT_SEC_1WEEK_64 UINT64_C(604800)
1901/** The number of seconds per day - 64-bit type. */
1902#define RT_SEC_1DAY_64 UINT64_C(86400)
1903/** The number of seconds per hour - 64-bit type. */
1904#define RT_SEC_1HOUR_64 UINT64_C(3600)
1905/** @} */
1906
1907
1908/** @defgroup grp_rt_cdefs_dbgtype Debug Info Types
1909 * @{ */
1910/** Other format. */
1911#define RT_DBGTYPE_OTHER RT_BIT_32(0)
1912/** Stabs. */
1913#define RT_DBGTYPE_STABS RT_BIT_32(1)
1914/** Debug With Arbitrary Record Format (DWARF). */
1915#define RT_DBGTYPE_DWARF RT_BIT_32(2)
1916/** Microsoft Codeview debug info. */
1917#define RT_DBGTYPE_CODEVIEW RT_BIT_32(3)
1918/** Watcom debug info. */
1919#define RT_DBGTYPE_WATCOM RT_BIT_32(4)
1920/** IBM High Level Language debug info. */
1921#define RT_DBGTYPE_HLL RT_BIT_32(5)
1922/** Old OS/2 and Windows symbol file. */
1923#define RT_DBGTYPE_SYM RT_BIT_32(6)
1924/** Map file. */
1925#define RT_DBGTYPE_MAP RT_BIT_32(7)
1926/** @} */
1927
1928
1929/** @defgroup grp_rt_cdefs_exetype Executable Image Types
1930 * @{ */
1931/** Some other format. */
1932#define RT_EXETYPE_OTHER RT_BIT_32(0)
1933/** Portable Executable. */
1934#define RT_EXETYPE_PE RT_BIT_32(1)
1935/** Linear eXecutable. */
1936#define RT_EXETYPE_LX RT_BIT_32(2)
1937/** Linear Executable. */
1938#define RT_EXETYPE_LE RT_BIT_32(3)
1939/** New Executable. */
1940#define RT_EXETYPE_NE RT_BIT_32(4)
1941/** DOS Executable (Mark Zbikowski). */
1942#define RT_EXETYPE_MZ RT_BIT_32(5)
1943/** COM Executable. */
1944#define RT_EXETYPE_COM RT_BIT_32(6)
1945/** a.out Executable. */
1946#define RT_EXETYPE_AOUT RT_BIT_32(7)
1947/** Executable and Linkable Format. */
1948#define RT_EXETYPE_ELF RT_BIT_32(8)
1949/** Mach-O Executable (including FAT ones). */
1950#define RT_EXETYPE_MACHO RT_BIT_32(9)
1951/** TE from UEFI. */
1952#define RT_EXETYPE_TE RT_BIT_32(9)
1953/** @} */
1954
1955
1956/** @def VALID_PTR
1957 * Pointer validation macro.
1958 * @param ptr The pointer.
1959 */
1960#if defined(RT_ARCH_AMD64)
1961# ifdef IN_RING3
1962# if defined(RT_OS_DARWIN) /* first 4GB is reserved for legacy kernel. */
1963# define RT_VALID_PTR(ptr) ( (uintptr_t)(ptr) >= _4G \
1964 && !((uintptr_t)(ptr) & 0xffff800000000000ULL) )
1965# elif defined(RT_OS_SOLARIS) /* The kernel only used the top 2TB, but keep it simple. */
1966# define RT_VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U \
1967 && ( ((uintptr_t)(ptr) & 0xffff800000000000ULL) == 0xffff800000000000ULL \
1968 || ((uintptr_t)(ptr) & 0xffff800000000000ULL) == 0) )
1969# else
1970# define RT_VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U \
1971 && !((uintptr_t)(ptr) & 0xffff800000000000ULL) )
1972# endif
1973# else /* !IN_RING3 */
1974# define RT_VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U \
1975 && ( ((uintptr_t)(ptr) & 0xffff800000000000ULL) == 0xffff800000000000ULL \
1976 || ((uintptr_t)(ptr) & 0xffff800000000000ULL) == 0) )
1977# endif /* !IN_RING3 */
1978
1979#elif defined(RT_ARCH_X86)
1980# define RT_VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U )
1981
1982#elif defined(RT_ARCH_SPARC64)
1983# ifdef IN_RING3
1984# if defined(RT_OS_SOLARIS)
1985/** Sparc64 user mode: According to Figure 9.4 in solaris internals */
1986/** @todo # define RT_VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x80004000U >= 0x80004000U + 0x100000000ULL ) - figure this. */
1987# define RT_VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x80000000U >= 0x80000000U + 0x100000000ULL )
1988# else
1989# error "Port me"
1990# endif
1991# else /* !IN_RING3 */
1992# if defined(RT_OS_SOLARIS)
1993/** @todo Sparc64 kernel mode: This is according to Figure 11.1 in solaris
1994 * internals. Verify in sources. */
1995# define RT_VALID_PTR(ptr) ( (uintptr_t)(ptr) >= 0x01000000U )
1996# else
1997# error "Port me"
1998# endif
1999# endif /* !IN_RING3 */
2000
2001#elif defined(RT_ARCH_SPARC)
2002# ifdef IN_RING3
2003# ifdef RT_OS_SOLARIS
2004/** Sparc user mode: According to
2005 * http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/sun4/os/startup.c#510 */
2006# define RT_VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x400000U >= 0x400000U + 0x2000U )
2007
2008# else
2009# error "Port me"
2010# endif
2011# else /* !IN_RING3 */
2012# ifdef RT_OS_SOLARIS
2013/** @todo Sparc kernel mode: Check the sources! */
2014# define RT_VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U )
2015# else
2016# error "Port me"
2017# endif
2018# endif /* !IN_RING3 */
2019
2020#else
2021# error "Architecture identifier missing / not implemented."
2022#endif
2023
2024/** Old name for RT_VALID_PTR. */
2025#define VALID_PTR(ptr) RT_VALID_PTR(ptr)
2026
2027/** @def RT_VALID_ALIGNED_PTR
2028 * Pointer validation macro that also checks the alignment.
2029 * @param ptr The pointer.
2030 * @param align The alignment, must be a power of two.
2031 */
2032#define RT_VALID_ALIGNED_PTR(ptr, align) \
2033 ( !((uintptr_t)(ptr) & (uintptr_t)((align) - 1)) \
2034 && VALID_PTR(ptr) )
2035
2036
2037/** @def VALID_PHYS32
2038 * 32 bits physical address validation macro.
2039 * @param Phys The RTGCPHYS address.
2040 */
2041#define VALID_PHYS32(Phys) ( (uint64_t)(Phys) < (uint64_t)_4G )
2042
2043/** @def N_
2044 * The \#define N_ is used to mark a string for translation. This is usable in
2045 * any part of the code, as it is only used by the tools that create message
2046 * catalogs. This macro is a no-op as far as the compiler and code generation
2047 * is concerned.
2048 *
2049 * If you want to both mark a string for translation and translate it, use _().
2050 */
2051#define N_(s) (s)
2052
2053/** @def _
2054 * The \#define _ is used to mark a string for translation and to translate it
2055 * in one step.
2056 *
2057 * If you want to only mark a string for translation, use N_().
2058 */
2059#define _(s) gettext(s)
2060
2061
2062/** @def __PRETTY_FUNCTION__
2063 * With GNU C we'd like to use the builtin __PRETTY_FUNCTION__, so define that
2064 * for the other compilers.
2065 */
2066#if !defined(__GNUC__) && !defined(__PRETTY_FUNCTION__)
2067# define __PRETTY_FUNCTION__ __FUNCTION__
2068#endif
2069
2070
2071/** @def RT_STRICT
2072 * The \#define RT_STRICT controls whether or not assertions and other runtime
2073 * checks should be compiled in or not.
2074 *
2075 * If you want assertions which are not subject to compile time options use
2076 * the AssertRelease*() flavors.
2077 */
2078#if !defined(RT_STRICT) && defined(DEBUG)
2079# define RT_STRICT
2080#endif
2081
2082/** @todo remove this: */
2083#if !defined(RT_LOCK_STRICT) && !defined(DEBUG_bird)
2084# define RT_LOCK_NO_STRICT
2085#endif
2086#if !defined(RT_LOCK_STRICT_ORDER) && !defined(DEBUG_bird)
2087# define RT_LOCK_NO_STRICT_ORDER
2088#endif
2089
2090/** @def RT_LOCK_STRICT
2091 * The \#define RT_LOCK_STRICT controls whether deadlock detection and related
2092 * checks are done in the lock and semaphore code. It is by default enabled in
2093 * RT_STRICT builds, but this behavior can be overridden by defining
2094 * RT_LOCK_NO_STRICT. */
2095#if !defined(RT_LOCK_STRICT) && !defined(RT_LOCK_NO_STRICT) && defined(RT_STRICT)
2096# define RT_LOCK_STRICT
2097#endif
2098/** @def RT_LOCK_NO_STRICT
2099 * The \#define RT_LOCK_NO_STRICT disables RT_LOCK_STRICT. */
2100#if defined(RT_LOCK_NO_STRICT) && defined(RT_LOCK_STRICT)
2101# undef RT_LOCK_STRICT
2102#endif
2103
2104/** @def RT_LOCK_STRICT_ORDER
2105 * The \#define RT_LOCK_STRICT_ORDER controls whether locking order is checked
2106 * by the lock and semaphore code. It is by default enabled in RT_STRICT
2107 * builds, but this behavior can be overridden by defining
2108 * RT_LOCK_NO_STRICT_ORDER. */
2109#if !defined(RT_LOCK_STRICT_ORDER) && !defined(RT_LOCK_NO_STRICT_ORDER) && defined(RT_STRICT)
2110# define RT_LOCK_STRICT_ORDER
2111#endif
2112/** @def RT_LOCK_NO_STRICT_ORDER
2113 * The \#define RT_LOCK_NO_STRICT_ORDER disables RT_LOCK_STRICT_ORDER. */
2114#if defined(RT_LOCK_NO_STRICT_ORDER) && defined(RT_LOCK_STRICT_ORDER)
2115# undef RT_LOCK_STRICT_ORDER
2116#endif
2117
2118
2119/** Source position. */
2120#define RT_SRC_POS __FILE__, __LINE__, __PRETTY_FUNCTION__
2121
2122/** Source position declaration. */
2123#define RT_SRC_POS_DECL const char *pszFile, unsigned iLine, const char *pszFunction
2124
2125/** Source position arguments. */
2126#define RT_SRC_POS_ARGS pszFile, iLine, pszFunction
2127
2128/** Applies NOREF() to the source position arguments. */
2129#define RT_SRC_POS_NOREF() do { NOREF(pszFile); NOREF(iLine); NOREF(pszFunction); } while (0)
2130
2131
2132/** @def RT_INLINE_ASM_EXTERNAL
2133 * Defined as 1 if the compiler does not support inline assembly.
2134 * The ASM* functions will then be implemented in external .asm files.
2135 */
2136#if (defined(_MSC_VER) && defined(RT_ARCH_AMD64)) \
2137 || (!defined(RT_ARCH_AMD64) && !defined(RT_ARCH_X86))
2138# define RT_INLINE_ASM_EXTERNAL 1
2139#else
2140# define RT_INLINE_ASM_EXTERNAL 0
2141#endif
2142
2143/** @def RT_INLINE_ASM_GNU_STYLE
2144 * Defined as 1 if the compiler understands GNU style inline assembly.
2145 */
2146#if defined(_MSC_VER)
2147# define RT_INLINE_ASM_GNU_STYLE 0
2148#else
2149# define RT_INLINE_ASM_GNU_STYLE 1
2150#endif
2151
2152/** @def RT_INLINE_ASM_USES_INTRIN
2153 * Defined as 1 if the compiler have and uses intrin.h. Otherwise it is 0. */
2154#ifdef _MSC_VER
2155# if _MSC_VER >= 1400
2156# define RT_INLINE_ASM_USES_INTRIN 1
2157# endif
2158#endif
2159#ifndef RT_INLINE_ASM_USES_INTRIN
2160# define RT_INLINE_ASM_USES_INTRIN 0
2161#endif
2162
2163/** @} */
2164
2165
2166/** @defgroup grp_rt_cdefs_cpp Special Macros for C++
2167 * @ingroup grp_rt_cdefs
2168 * @{
2169 */
2170
2171#ifdef __cplusplus
2172
2173/** @def DECLEXPORT_CLASS
2174 * How to declare an exported class. Place this macro after the 'class'
2175 * keyword in the declaration of every class you want to export.
2176 *
2177 * @note It is necessary to use this macro even for inner classes declared
2178 * inside the already exported classes. This is a GCC specific requirement,
2179 * but it seems not to harm other compilers.
2180 */
2181#if defined(_MSC_VER) || defined(RT_OS_OS2)
2182# define DECLEXPORT_CLASS __declspec(dllexport)
2183#elif defined(RT_USE_VISIBILITY_DEFAULT)
2184# define DECLEXPORT_CLASS __attribute__((visibility("default")))
2185#else
2186# define DECLEXPORT_CLASS
2187#endif
2188
2189/** @def DECLIMPORT_CLASS
2190 * How to declare an imported class Place this macro after the 'class'
2191 * keyword in the declaration of every class you want to export.
2192 *
2193 * @note It is necessary to use this macro even for inner classes declared
2194 * inside the already exported classes. This is a GCC specific requirement,
2195 * but it seems not to harm other compilers.
2196 */
2197#if defined(_MSC_VER) || (defined(RT_OS_OS2) && !defined(__IBMC__) && !defined(__IBMCPP__))
2198# define DECLIMPORT_CLASS __declspec(dllimport)
2199#elif defined(RT_USE_VISIBILITY_DEFAULT)
2200# define DECLIMPORT_CLASS __attribute__((visibility("default")))
2201#else
2202# define DECLIMPORT_CLASS
2203#endif
2204
2205/** @def WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP
2206 * Macro to work around error C2593 of the not-so-smart MSVC 7.x ambiguity
2207 * resolver. The following snippet clearly demonstrates the code causing this
2208 * error:
2209 * @code
2210 * class A
2211 * {
2212 * public:
2213 * operator bool() const { return false; }
2214 * operator int*() const { return NULL; }
2215 * };
2216 * int main()
2217 * {
2218 * A a;
2219 * if (!a);
2220 * if (a && 0);
2221 * return 0;
2222 * }
2223 * @endcode
2224 * The code itself seems pretty valid to me and GCC thinks the same.
2225 *
2226 * This macro fixes the compiler error by explicitly overloading implicit
2227 * global operators !, && and || that take the given class instance as one of
2228 * their arguments.
2229 *
2230 * The best is to use this macro right after the class declaration.
2231 *
2232 * @note The macro expands to nothing for compilers other than MSVC.
2233 *
2234 * @param Cls Class to apply the workaround to
2235 */
2236#if defined(_MSC_VER)
2237# define WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP(Cls) \
2238 inline bool operator! (const Cls &that) { return !bool (that); } \
2239 inline bool operator&& (const Cls &that, bool b) { return bool (that) && b; } \
2240 inline bool operator|| (const Cls &that, bool b) { return bool (that) || b; } \
2241 inline bool operator&& (bool b, const Cls &that) { return b && bool (that); } \
2242 inline bool operator|| (bool b, const Cls &that) { return b || bool (that); }
2243#else
2244# define WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP(Cls)
2245#endif
2246
2247/** @def WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP_TPL
2248 * Version of WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP for template classes.
2249 *
2250 * @param Tpl Name of the template class to apply the workaround to
2251 * @param ArgsDecl arguments of the template, as declared in |<>| after the
2252 * |template| keyword, including |<>|
2253 * @param Args arguments of the template, as specified in |<>| after the
2254 * template class name when using the, including |<>|
2255 *
2256 * Example:
2257 * @code
2258 * // template class declaration
2259 * template <class C>
2260 * class Foo { ... };
2261 * // applied workaround
2262 * WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP_TPL (Foo, <class C>, <C>)
2263 * @endcode
2264 */
2265#if defined(_MSC_VER)
2266# define WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP_TPL(Tpl, ArgsDecl, Args) \
2267 template ArgsDecl \
2268 inline bool operator! (const Tpl Args &that) { return !bool (that); } \
2269 template ArgsDecl \
2270 inline bool operator&& (const Tpl Args &that, bool b) { return bool (that) && b; } \
2271 template ArgsDecl \
2272 inline bool operator|| (const Tpl Args &that, bool b) { return bool (that) || b; } \
2273 template ArgsDecl \
2274 inline bool operator&& (bool b, const Tpl Args &that) { return b && bool (that); } \
2275 template ArgsDecl \
2276 inline bool operator|| (bool b, const Tpl Args &that) { return b || bool (that); }
2277#else
2278# define WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP_TPL(Tpl, ArgsDecl, Args)
2279#endif
2280
2281
2282/** @def DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP
2283 * Declares the copy constructor and the assignment operation as inlined no-ops
2284 * (non-existent functions) for the given class. Use this macro inside the
2285 * private section if you want to effectively disable these operations for your
2286 * class.
2287 *
2288 * @param Cls class name to declare for
2289 */
2290
2291#define DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(Cls) \
2292 inline Cls (const Cls &); \
2293 inline Cls &operator= (const Cls &);
2294
2295
2296/** @def DECLARE_CLS_NEW_DELETE_NOOP
2297 * Declares the new and delete operations as no-ops (non-existent functions)
2298 * for the given class. Use this macro inside the private section if you want
2299 * to effectively limit creating class instances on the stack only.
2300 *
2301 * @note The destructor of the given class must not be virtual, otherwise a
2302 * compile time error will occur. Note that this is not a drawback: having
2303 * the virtual destructor for a stack-based class is absolutely useless
2304 * (the real class of the stack-based instance is always known to the compiler
2305 * at compile time, so it will always call the correct destructor).
2306 *
2307 * @param Cls class name to declare for
2308 */
2309#define DECLARE_CLS_NEW_DELETE_NOOP(Cls) \
2310 inline static void *operator new (size_t); \
2311 inline static void operator delete (void *);
2312
2313#endif /* __cplusplus */
2314
2315/** @} */
2316
2317#endif
2318
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette