VirtualBox

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

Last change on this file since 9666 was 9411, checked in by vboxsync, 17 years ago

Use a union for esp & rsp, so they are in-sync.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 45.0 KB
Line 
1/** @file
2 * IPRT - Common C and C++ definitions.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___iprt_cdefs_h
31#define ___iprt_cdefs_h
32
33
34/** @defgroup grp_rt_cdefs IPRT Common Definitions and Macros
35 * @{
36 */
37
38/*
39 * Include sys/cdefs.h if present, if not define the stuff we need.
40 */
41#ifdef HAVE_SYS_CDEFS_H
42# if defined(RT_ARCH_LINUX) && defined(__KERNEL__)
43# error "oops"
44# endif
45# include <sys/cdefs.h>
46#else
47
48 /** @def __BEGIN_DECLS
49 * Used to start a block of function declarations which are shared
50 * between C and C++ program.
51 */
52
53 /** @def __END_DECLS
54 * Used to end a block of function declarations which are shared
55 * between C and C++ program.
56 */
57
58 #if defined(__cplusplus)
59 # define __BEGIN_DECLS extern "C" {
60 # define __END_DECLS }
61 #else
62 # define __BEGIN_DECLS
63 # define __END_DECLS
64 #endif
65
66#endif
67
68
69/*
70 * Shut up DOXYGEN warnings and guide it properly thru the code.
71 */
72#ifdef DOXYGEN_RUNNING
73#define __AMD64__
74#define __X86__
75#define RT_ARCH_AMD64
76#define RT_ARCH_X86
77#define IN_RING0
78#define IN_RING3
79#define IN_GC
80#define IN_RT_GC
81#define IN_RT_R0
82#define IN_RT_R3
83#define RT_STRICT
84#define Breakpoint
85#define RT_NO_DEPRECATED_MACROS
86#endif /* DOXYGEN_RUNNING */
87
88/** @def RT_ARCH_X86
89 * Indicates that we're compiling for the X86 architecture.
90 */
91
92/** @def RT_ARCH_AMD64
93 * Indicates that we're compiling for the AMD64 architecture.
94 */
95#if !defined(RT_ARCH_X86) && !defined(RT_ARCH_AMD64)
96# if defined(__amd64__) || defined(__x86_64__) || defined(_M_X64) || defined(__AMD64__)
97# define RT_ARCH_AMD64
98# elif defined(__i386__) || defined(_M_IX86) || defined(__X86__)
99# define RT_ARCH_X86
100# else /* PORTME: append test for new archs. */
101# error "Check what predefined stuff your compiler uses to indicate architecture."
102# endif
103#elif defined(RT_ARCH_X86) && defined(RT_ARCH_AMD64) /* PORTME: append new archs. */
104# error "Both RT_ARCH_X86 and RT_ARCH_AMD64 cannot be defined at the same time!"
105#endif
106
107
108/** @def __X86__
109 * Indicates that we're compiling for the X86 architecture.
110 * @deprecated
111 */
112
113/** @def __AMD64__
114 * Indicates that we're compiling for the AMD64 architecture.
115 * @deprecated
116 */
117#if !defined(__X86__) && !defined(__AMD64__)
118# if defined(RT_ARCH_AMD64)
119# define __AMD64__
120# elif defined(RT_ARCH_X86)
121# define __X86__
122# else
123# error "Check what predefined stuff your compiler uses to indicate architecture."
124# endif
125#elif defined(__X86__) && defined(__AMD64__)
126# error "Both __X86__ and __AMD64__ cannot be defined at the same time!"
127#elif defined(__X86__) && !defined(RT_ARCH_X86)
128# error "Both __X86__ without RT_ARCH_X86!"
129#elif defined(__AMD64__) && !defined(RT_ARCH_AMD64)
130# error "Both __AMD64__ without RT_ARCH_AMD64!"
131#endif
132
133/** @def IN_RING0
134 * Used to indicate that we're compiling code which is running
135 * in Ring-0 Host Context.
136 */
137
138/** @def IN_RING3
139 * Used to indicate that we're compiling code which is running
140 * in Ring-3 Host Context.
141 */
142
143/** @def IN_GC
144 * Used to indicate that we're compiling code which is running
145 * in Guest Context (implies R0).
146 */
147#if !defined(IN_RING3) && !defined(IN_RING0) && !defined(IN_GC)
148# error "You must defined which context the compiled code should run in; IN_RING3, IN_RING0 or IN_GC"
149#endif
150#if (defined(IN_RING3) && (defined(IN_RING0) || defined(IN_GC)) ) \
151 || (defined(IN_RING0) && (defined(IN_RING3) || defined(IN_GC)) ) \
152 || (defined(IN_GC) && (defined(IN_RING3) || defined(IN_RING0)) )
153# error "Only one of the IN_RING3, IN_RING0, IN_GC defines should be defined."
154#endif
155
156
157/** @def ARCH_BITS
158 * Defines the bit count of the current context.
159 */
160#if !defined(ARCH_BITS) || defined(DOXYGEN_RUNNING)
161# if defined(RT_ARCH_AMD64)
162# define ARCH_BITS 64
163# else
164# define ARCH_BITS 32
165# endif
166#endif
167
168/** @def HC_ARCH_BITS
169 * Defines the host architecture bit count.
170 */
171#if !defined(HC_ARCH_BITS) || defined(DOXYGEN_RUNNING)
172# ifndef IN_GC
173# define HC_ARCH_BITS ARCH_BITS
174# else
175# define HC_ARCH_BITS 32
176# endif
177#endif
178
179/** @def GC_ARCH_BITS
180 * Defines the guest architecture bit count.
181 */
182#if !defined(GC_ARCH_BITS) && !defined(DOXYGEN_RUNNING)
183# ifdef VBOX_WITH_64_BITS_GUESTS
184# define GC_ARCH_BITS 64
185# else
186# define GC_ARCH_BITS 32
187# endif
188#endif
189
190/** @def R3_ARCH_BITS
191 * Defines the host ring-3 architecture bit count.
192 */
193#if !defined(R3_ARCH_BITS) || defined(DOXYGEN_RUNNING)
194# ifdef IN_RING3
195# define R3_ARCH_BITS ARCH_BITS
196# else
197# define R3_ARCH_BITS HC_ARCH_BITS
198# endif
199#endif
200
201/** @def R0_ARCH_BITS
202 * Defines the host ring-0 architecture bit count.
203 */
204#if !defined(R0_ARCH_BITS) || defined(DOXYGEN_RUNNING)
205# ifdef IN_RING0
206# define R0_ARCH_BITS ARCH_BITS
207# else
208# define R0_ARCH_BITS HC_ARCH_BITS
209# endif
210#endif
211
212/** @def GC_ARCH_BITS
213 * Defines the guest architecture bit count.
214 */
215#if !defined(GC_ARCH_BITS) || defined(DOXYGEN_RUNNING)
216# ifdef IN_GC
217# define GC_ARCH_BITS ARCH_BITS
218# else
219# define GC_ARCH_BITS 32
220# endif
221#endif
222
223
224/** @def CTXTYPE
225 * Declare a type differently in GC, R3 and R0.
226 *
227 * @param GCType The GC type.
228 * @param R3Type The R3 type.
229 * @param R0Type The R0 type.
230 * @remark For pointers used only in one context use RCPTRTYPE(), R3R0PTRTYPE(), R3PTRTYPE() or R0PTRTYPE().
231 */
232#ifdef IN_GC
233# define CTXTYPE(GCType, R3Type, R0Type) GCType
234#elif defined(IN_RING3)
235# define CTXTYPE(GCType, R3Type, R0Type) R3Type
236#else
237# define CTXTYPE(GCType, R3Type, R0Type) R0Type
238#endif
239
240/** @def GCTYPE
241 * Declare a type differently in GC and HC.
242 *
243 * @param GCType The GC type.
244 * @param HCType The HC type.
245 * @remark For pointers used only in one context use RCPTRTYPE(), R3R0PTRTYPE(), R3PTRTYPE() or R0PTRTYPE().
246 */
247#define GCTYPE(GCType, HCType) CTXTYPE(GCType, HCType, HCType)
248
249/** @def RCPTRTYPE
250 * Declare a pointer which is used in the raw mode context but appears in structure(s) used by
251 * both HC and RC. The main purpose is to make sure structures have the same
252 * size when built for different architectures.
253 *
254 * @param RCType The RC type.
255 */
256#define RCPTRTYPE(RCType) CTXTYPE(RCType, RTRCPTR, RTRCPTR)
257
258/** @def R3R0PTRTYPE
259 * Declare a pointer which is used in HC, is explicitely valid in ring 3 and 0,
260 * but appears in structure(s) used by both HC and GC. The main purpose is to
261 * make sure structures have the same size when built for different architectures.
262 *
263 * @param R3R0Type The R3R0 type.
264 * @remarks This used to be called HCPTRTYPE.
265 */
266#define R3R0PTRTYPE(R3R0Type) CTXTYPE(RTHCPTR, R3R0Type, R3R0Type)
267
268/** @def R3PTRTYPE
269 * Declare a pointer which is used in R3 but appears in structure(s) used by
270 * both HC and GC. The main purpose is to make sure structures have the same
271 * size when built for different architectures.
272 *
273 * @param R3Type The R3 type.
274 */
275#define R3PTRTYPE(R3Type) CTXTYPE(RTHCUINTPTR, R3Type, RTHCUINTPTR)
276
277/** @def R0PTRTYPE
278 * Declare a pointer which is used in R0 but appears in structure(s) used by
279 * both HC and GC. The main purpose is to make sure structures have the same
280 * size when built for different architectures.
281 *
282 * @param R0Type The R0 type.
283 */
284#define R0PTRTYPE(R0Type) CTXTYPE(RTHCUINTPTR, RTHCUINTPTR, R0Type)
285
286/** @def CTXSUFF
287 * Adds the suffix of the current context to the passed in
288 * identifier name. The suffix is HC or GC.
289 *
290 * This is macro should only be used in shared code to avoid a forrest of ifdefs.
291 * @param var Identifier name.
292 */
293/** @def OTHERCTXSUFF
294 * Adds the suffix of the other context to the passed in
295 * identifier name. The suffix is HC or GC.
296 *
297 * This is macro should only be used in shared code to avoid a forrest of ifdefs.
298 * @param var Identifier name.
299 */
300#ifdef IN_GC
301# define CTXSUFF(var) var##GC
302# define OTHERCTXSUFF(var) var##HC
303#else
304# define CTXSUFF(var) var##HC
305# define OTHERCTXSUFF(var) var##GC
306#endif
307
308/** @def CTXALLSUFF
309 * Adds the suffix of the current context to the passed in
310 * identifier name. The suffix is R3, R0 or GC.
311 *
312 * This is macro should only be used in shared code to avoid a forrest of ifdefs.
313 * @param var Identifier name.
314 */
315#ifdef IN_GC
316# define CTXALLSUFF(var) var##GC
317#elif defined(IN_RING0)
318# define CTXALLSUFF(var) var##R0
319#else
320# define CTXALLSUFF(var) var##R3
321#endif
322
323/** @def CTXMID
324 * Adds the current context as a middle name of an identifier name
325 * The middle name is HC or GC.
326 *
327 * This is macro should only be used in shared code to avoid a forrest of ifdefs.
328 * @param first First name.
329 * @param last Surname.
330 */
331/** @def OTHERCTXMID
332 * Adds the other context as a middle name of an identifier name
333 * The middle name is HC or GC.
334 *
335 * This is macro should only be used in shared code to avoid a forrest of ifdefs.
336 * @param first First name.
337 * @param last Surname.
338 */
339#ifdef IN_GC
340# define CTXMID(first, last) first##GC##last
341# define OTHERCTXMID(first, last) first##HC##last
342#else
343# define CTXMID(first, last) first##HC##last
344# define OTHERCTXMID(first, last) first##GC##last
345#endif
346
347/** @def CTXALLMID
348 * Adds the current context as a middle name of an identifier name
349 * The middle name is R3, R0 or GC.
350 *
351 * This is macro should only be used in shared code to avoid a forrest of ifdefs.
352 * @param first First name.
353 * @param last Surname.
354 */
355#ifdef IN_GC
356# define CTXALLMID(first, last) first##GC##last
357#elif defined(IN_RING0)
358# define CTXALLMID(first, last) first##R0##last
359#else
360# define CTXALLMID(first, last) first##R3##last
361#endif
362
363
364/** @def R3STRING
365 * A macro which in GC and R0 will return a dummy string while in R3 it will return
366 * the parameter.
367 *
368 * This is typically used to wrap description strings in structures shared
369 * between R3, R0 and/or GC. The intention is to avoid the \#ifdef IN_RING3 mess.
370 *
371 * @param pR3String The R3 string. Only referenced in R3.
372 * @see R0STRING and GCSTRING
373 */
374#ifdef IN_RING3
375# define R3STRING(pR3String) (pR3String)
376#else
377# define R3STRING(pR3String) ("<R3_STRING>")
378#endif
379
380/** @def R0STRING
381 * A macro which in GC and R3 will return a dummy string while in R0 it will return
382 * the parameter.
383 *
384 * This is typically used to wrap description strings in structures shared
385 * between R3, R0 and/or GC. The intention is to avoid the \#ifdef IN_RING0 mess.
386 *
387 * @param pR0String The R0 string. Only referenced in R0.
388 * @see R3STRING and GCSTRING
389 */
390#ifdef IN_RING0
391# define R0STRING(pR0String) (pR0String)
392#else
393# define R0STRING(pR0String) ("<R0_STRING>")
394#endif
395
396/** @def GCSTRING
397 * A macro which in R3 and R0 will return a dummy string while in GC it will return
398 * the parameter.
399 *
400 * This is typically used to wrap description strings in structures shared
401 * between R3, R0 and/or GC. The intention is to avoid the \#ifdef IN_GC mess.
402 *
403 * @param pR0String The GC string. Only referenced in GC.
404 * @see R3STRING, R0STRING
405 */
406#ifdef IN_GC
407# define GCSTRING(pR0String) (pGCString)
408#else
409# define GCSTRING(pR0String) ("<GC_STRING>")
410#endif
411
412/** @def HCSTRING
413 * Macro which in GC will return a dummy string while in HC will return
414 * the parameter.
415 *
416 * This is typically used to wrap description strings in structures shared
417 * between HC and GC. The intention is to avoid the \#ifdef IN_GC kludge.
418 *
419 * @param pHCString The HC string. Only referenced in HC.
420 * @deprecated Use R3STRING or R0STRING instead.
421 */
422#ifdef IN_GC
423# define HCSTRING(pHCString) ("<HC_STRING>")
424#else
425# define HCSTRING(pHCString) (pHCString)
426#endif
427
428
429/** @def RTCALL
430 * The standard calling convention for the Runtime interfaces.
431 */
432#ifdef _MSC_VER
433# define RTCALL __cdecl
434#elif defined(__GNUC__) && defined(IN_RING0) && !(defined(RT_OS_OS2) || defined(RT_ARCH_AMD64)) /* the latter is kernel/gcc */
435# define RTCALL __attribute__((cdecl,regparm(0)))
436#else
437# define RTCALL
438#endif
439
440/** @def DECLEXPORT
441 * How to declare an exported function.
442 * @param type The return type of the function declaration.
443 */
444#if defined(_MSC_VER) || defined(RT_OS_OS2)
445# define DECLEXPORT(type) __declspec(dllexport) type
446#else
447# ifdef VBOX_HAVE_VISIBILITY_HIDDEN
448# define DECLEXPORT(type) __attribute__((visibility("default"))) type
449# else
450# define DECLEXPORT(type) type
451# endif
452#endif
453
454/** @def DECLIMPORT
455 * How to declare an imported function.
456 * @param type The return type of the function declaration.
457 */
458#if defined(_MSC_VER) || (defined(RT_OS_OS2) && !defined(__IBMC__) && !defined(__IBMCPP__))
459# define DECLIMPORT(type) __declspec(dllimport) type
460#else
461# define DECLIMPORT(type) type
462#endif
463
464/** @def DECLASM
465 * How to declare an internal assembly function.
466 * @param type The return type of the function declaration.
467 */
468#ifdef __cplusplus
469# ifdef _MSC_VER
470# define DECLASM(type) extern "C" type __cdecl
471# else
472# define DECLASM(type) extern "C" type
473# endif
474#else
475# ifdef _MSC_VER
476# define DECLASM(type) type __cdecl
477# else
478# define DECLASM(type) type
479# endif
480#endif
481
482/** @def DECLASMTYPE
483 * How to declare an internal assembly function type.
484 * @param type The return type of the function.
485 */
486#ifdef _MSC_VER
487# define DECLASMTYPE(type) type __cdecl
488#else
489# define DECLASMTYPE(type) type
490#endif
491
492/** @def DECLNORETURN
493 * How to declare a function which does not return.
494 * @note: This macro can be combined with other macros, for example
495 * @code
496 * EMR3DECL(DECLNORETURN(void)) foo(void);
497 * @endcode
498 */
499#ifdef _MSC_VER
500# define DECLNORETURN(type) __declspec(noreturn) type
501#elif defined(__GNUC__)
502# define DECLNORETURN(type) __attribute__((noreturn)) type
503#else
504# define DECLNORETURN(type) type
505#endif
506
507/** @def DECLCALLBACK
508 * How to declare an call back function type.
509 * @param type The return type of the function declaration.
510 */
511#define DECLCALLBACK(type) type RTCALL
512
513/** @def DECLCALLBACKPTR
514 * How to declare an call back function pointer.
515 * @param type The return type of the function declaration.
516 * @param name The name of the variable member.
517 */
518#define DECLCALLBACKPTR(type, name) type (RTCALL * name)
519
520/** @def DECLCALLBACKMEMBER
521 * How to declare an call back function pointer member.
522 * @param type The return type of the function declaration.
523 * @param name The name of the struct/union/class member.
524 */
525#define DECLCALLBACKMEMBER(type, name) type (RTCALL * name)
526
527/** @def DECLR3CALLBACKMEMBER
528 * How to declare an call back function pointer member - R3 Ptr.
529 * @param type The return type of the function declaration.
530 * @param name The name of the struct/union/class member.
531 * @param args The argument list enclosed in parentheses.
532 */
533#ifdef IN_RING3
534# define DECLR3CALLBACKMEMBER(type, name, args) type (RTCALL * name) args
535#else
536# define DECLR3CALLBACKMEMBER(type, name, args) RTR3PTR name
537#endif
538
539/** @def DECLGCCALLBACKMEMBER
540 * How to declare an call back function pointer member - GC Ptr.
541 * @param type The return type of the function declaration.
542 * @param name The name of the struct/union/class member.
543 * @param args The argument list enclosed in parentheses.
544 */
545#ifdef IN_GC
546# define DECLGCCALLBACKMEMBER(type, name, args) type (RTCALL * name) args
547#else
548# define DECLGCCALLBACKMEMBER(type, name, args) RTRCPTR name
549#endif
550
551/** @def DECLR0CALLBACKMEMBER
552 * How to declare an call back function pointer member - R0 Ptr.
553 * @param type The return type of the function declaration.
554 * @param name The name of the struct/union/class member.
555 * @param args The argument list enclosed in parentheses.
556 */
557#ifdef IN_RING0
558# define DECLR0CALLBACKMEMBER(type, name, args) type (RTCALL * name) args
559#else
560# define DECLR0CALLBACKMEMBER(type, name, args) RTR0PTR name
561#endif
562
563/** @def DECLINLINE
564 * How to declare a function as inline.
565 * @param type The return type of the function declaration.
566 * @remarks Don't use this macro on C++ methods.
567 */
568#ifdef __GNUC__
569# define DECLINLINE(type) static __inline__ type
570#elif defined(__cplusplus)
571# define DECLINLINE(type) inline type
572#elif defined(_MSC_VER)
573# define DECLINLINE(type) _inline type
574#elif defined(__IBMC__)
575# define DECLINLINE(type) _Inline type
576#else
577# define DECLINLINE(type) inline type
578#endif
579
580
581/** @def IN_RT_R0
582 * Used to indicate whether we're inside the same link module as
583 * the HC Ring-0 Runtime Library.
584 */
585/** @def RTR0DECL(type)
586 * Runtime Library HC Ring-0 export or import declaration.
587 * @param type The return type of the function declaration.
588 */
589#ifdef IN_RT_R0
590# define RTR0DECL(type) DECLEXPORT(type) RTCALL
591#else
592# define RTR0DECL(type) DECLIMPORT(type) RTCALL
593#endif
594
595/** @def IN_RT_R3
596 * Used to indicate whether we're inside the same link module as
597 * the HC Ring-3 Runtime Library.
598 */
599/** @def RTR3DECL(type)
600 * Runtime Library HC Ring-3 export or import declaration.
601 * @param type The return type of the function declaration.
602 */
603#ifdef IN_RT_R3
604# define RTR3DECL(type) DECLEXPORT(type) RTCALL
605#else
606# define RTR3DECL(type) DECLIMPORT(type) RTCALL
607#endif
608
609/** @def IN_RT_GC
610 * Used to indicate whether we're inside the same link module as
611 * the GC Runtime Library.
612 */
613/** @def RTGCDECL(type)
614 * Runtime Library HC Ring-3 export or import declaration.
615 * @param type The return type of the function declaration.
616 */
617#ifdef IN_RT_GC
618# define RTGCDECL(type) DECLEXPORT(type) RTCALL
619#else
620# define RTGCDECL(type) DECLIMPORT(type) RTCALL
621#endif
622
623/** @def RTDECL(type)
624 * Runtime Library export or import declaration.
625 * Functions declared using this macro exists in all contexts.
626 * @param type The return type of the function declaration.
627 */
628#if defined(IN_RT_R3) || defined(IN_RT_GC) || defined(IN_RT_R0)
629# define RTDECL(type) DECLEXPORT(type) RTCALL
630#else
631# define RTDECL(type) DECLIMPORT(type) RTCALL
632#endif
633
634/** @def RTDATADECL(type)
635 * Runtime Library export or import declaration.
636 * Data declared using this macro exists in all contexts.
637 * @param type The return type of the function declaration.
638 */
639#if defined(IN_RT_R3) || defined(IN_RT_GC) || defined(IN_RT_R0)
640# define RTDATADECL(type) DECLEXPORT(type)
641#else
642# define RTDATADECL(type) DECLIMPORT(type)
643#endif
644
645
646/** @def RT_NOCRT
647 * Symbol name wrapper for the No-CRT bits.
648 *
649 * In order to coexist in the same process as other CRTs, we need to
650 * decorate the symbols such that they don't conflict the ones in the
651 * other CRTs. The result of such conflicts / duplicate symbols can
652 * confuse the dynamic loader on unix like systems.
653 *
654 * Define RT_WITHOUT_NOCRT_WRAPPERS to drop the wrapping.
655 * Define RT_WITHOUT_NOCRT_WRAPPER_ALIASES to drop the aliases to the
656 * wrapped names.
657 */
658/** @def RT_NOCRT_STR
659 * Same as RT_NOCRT only it'll return a double quoted string of the result.
660 */
661#ifndef RT_WITHOUT_NOCRT_WRAPPERS
662# define RT_NOCRT(name) nocrt_ ## name
663# define RT_NOCRT_STR(name) "nocrt_" # name
664#else
665# define RT_NOCRT(name) name
666# define RT_NOCRT_STR(name) #name
667#endif
668
669
670
671/** @def RT_LIKELY
672 * Give the compiler a hint that an expression is very likely to hold true.
673 *
674 * Some compilers support explicit branch prediction so that the CPU backend
675 * can hint the processor and also so that code blocks can be reordered such
676 * that the predicted path sees a more linear flow, thus improving cache
677 * behaviour, etc.
678 *
679 * IPRT provides the macros RT_LIKELY() and RT_UNLIKELY() as a way to utilize
680 * this compiler feature when present.
681 *
682 * A few notes about the usage:
683 *
684 * - Generally, use RT_UNLIKELY() with error condition checks (unless you
685 * have some _strong_ reason to do otherwise, in which case document it),
686 * and/or RT_LIKELY() with success condition checks, assuming you want
687 * to optimize for the success path.
688 *
689 * - Other than that, if you don't know the likelihood of a test succeeding
690 * from empirical or other 'hard' evidence, don't make predictions unless
691 * you happen to be a Dirk Gently.
692 *
693 * - These macros are meant to be used in places that get executed a lot. It
694 * is wasteful to make predictions in code that is executed seldomly (e.g.
695 * at subsystem initialization time) as the basic block reording that this
696 * affecs can often generate larger code.
697 *
698 * - Note that RT_SUCCESS() and RT_FAILURE() already makes use of RT_LIKELY()
699 * and RT_UNLIKELY(). Should you wish for prediction free status checks,
700 * use the RT_SUCCESS_NP() and RT_FAILURE_NP() macros instead.
701 *
702 *
703 * @returns the boolean result of the expression.
704 * @param expr The expression that's very likely to be true.
705 * @see RT_UNLIKELY
706 */
707/** @def RT_UNLIKELY
708 * Give the compiler a hint that an expression is highly unlikely hold true.
709 *
710 * See the usage instructions give in the RT_LIKELY() docs.
711 *
712 * @returns the boolean result of the expression.
713 * @param expr The expression that's very unlikely to be true.
714 * @see RT_LIKELY
715 */
716#if defined(__GNUC__)
717# if __GNUC__ >= 3
718# define RT_LIKELY(expr) __builtin_expect(!!(expr), 1)
719# define RT_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
720# else
721# define RT_LIKELY(expr) (expr)
722# define RT_UNLIKELY(expr) (expr)
723# endif
724#else
725# define RT_LIKELY(expr) (expr)
726# define RT_UNLIKELY(expr) (expr)
727#endif
728
729
730/** @def RT_BIT
731 * Make a bitmask for one integer sized bit.
732 * @param bit Bit number.
733 */
734#define RT_BIT(bit) (1U << (bit))
735
736/** @def RT_BIT_32
737 * Make a 32-bit bitmask for one bit.
738 * @param bit Bit number.
739 */
740#define RT_BIT_32(bit) (UINT32_C(1) << (bit))
741
742/** @def RT_BIT_64
743 * Make a 64-bit bitmask for one bit.
744 * @param bit Bit number.
745 */
746#define RT_BIT_64(bit) (UINT64_C(1) << (bit))
747
748/** @def RT_ALIGN
749 * Align macro.
750 * @param u Value to align.
751 * @param uAlignment The alignment. Power of two!
752 *
753 * @remark Be extremely careful when using this macro with type which sizeof != sizeof int.
754 * When possible use any of the other RT_ALIGN_* macros. And when that's not
755 * possible, make 101% sure that uAlignment is specified with a right sized type.
756 *
757 * Specifying an unsigned 32-bit alignment constant with a 64-bit value will give
758 * you a 32-bit return value!
759 *
760 * In short: Don't use this macro. Use RT_ALIGN_T() instead.
761 */
762#define RT_ALIGN(u, uAlignment) ( ((u) + ((uAlignment) - 1)) & ~((uAlignment) - 1) )
763
764/** @def RT_ALIGN_T
765 * Align macro.
766 * @param u Value to align.
767 * @param uAlignment The alignment. Power of two!
768 * @param type Integer type to use while aligning.
769 * @remark This macro is the prefered alignment macro, it doesn't have any of the pitfalls RT_ALIGN has.
770 */
771#define RT_ALIGN_T(u, uAlignment, type) ( ((type)(u) + ((uAlignment) - 1)) & ~(type)((uAlignment) - 1) )
772
773/** @def RT_ALIGN_32
774 * Align macro for a 32-bit value.
775 * @param u32 Value to align.
776 * @param uAlignment The alignment. Power of two!
777 */
778#define RT_ALIGN_32(u32, uAlignment) RT_ALIGN_T(u32, uAlignment, uint32_t)
779
780/** @def RT_ALIGN_64
781 * Align macro for a 64-bit value.
782 * @param u64 Value to align.
783 * @param uAlignment The alignment. Power of two!
784 */
785#define RT_ALIGN_64(u64, uAlignment) RT_ALIGN_T(u64, uAlignment, uint64_t)
786
787/** @def RT_ALIGN_Z
788 * Align macro for size_t.
789 * @param cb Value to align.
790 * @param uAlignment The alignment. Power of two!
791 */
792#define RT_ALIGN_Z(cb, uAlignment) RT_ALIGN_T(cb, uAlignment, size_t)
793
794/** @def RT_ALIGN_P
795 * Align macro for pointers.
796 * @param pv Value to align.
797 * @param uAlignment The alignment. Power of two!
798 */
799#define RT_ALIGN_P(pv, uAlignment) RT_ALIGN_PT(pv, uAlignment, void *)
800
801/** @def RT_ALIGN_PT
802 * Align macro for pointers with type cast.
803 * @param u Value to align.
804 * @param uAlignment The alignment. Power of two!
805 * @param CastType The type to cast the result to.
806 */
807#define RT_ALIGN_PT(u, uAlignment, CastType) ((CastType)RT_ALIGN_T(u, uAlignment, uintptr_t))
808
809/** @def RT_ALIGN_R3PT
810 * Align macro for ring-3 pointers with type cast.
811 * @param u Value to align.
812 * @param uAlignment The alignment. Power of two!
813 * @param CastType The type to cast the result to.
814 */
815#define RT_ALIGN_R3PT(u, uAlignment, CastType) ((CastType)RT_ALIGN_T(u, uAlignment, RTR3UINTPTR))
816
817/** @def RT_ALIGN_R0PT
818 * Align macro for ring-0 pointers with type cast.
819 * @param u Value to align.
820 * @param uAlignment The alignment. Power of two!
821 * @param CastType The type to cast the result to.
822 */
823#define RT_ALIGN_R0PT(u, uAlignment, CastType) ((CastType)RT_ALIGN_T(u, uAlignment, RTR0UINTPTR))
824
825/** @def RT_ALIGN_GCPT
826 * Align macro for GC pointers with type cast.
827 * @param u Value to align.
828 * @param uAlignment The alignment. Power of two!
829 * @param CastType The type to cast the result to.
830 */
831#define RT_ALIGN_GCPT(u, uAlignment, CastType) ((CastType)RT_ALIGN_T(u, uAlignment, RTGCUINTPTR))
832
833
834/** @def RT_OFFSETOF
835 * Our own special offsetof() variant, returns a signed result.
836 *
837 * This differs from the usual offsetof() in that it's not relying on builtin
838 * compiler stuff and thus can use variables in arrays the structure may
839 * contain. If in this usful to determin the sizes of structures ending
840 * with a variable length field.
841 *
842 * @returns offset into the structure of the specified member. signed.
843 * @param type Structure type.
844 * @param member Member.
845 */
846#define RT_OFFSETOF(type, member) ( (int)(uintptr_t)&( ((type *)(void *)0)->member) )
847
848/** @def RT_UOFFSETOF
849 * Our own special offsetof() variant, returns an unsigned result.
850 *
851 * This differs from the usual offsetof() in that it's not relying on builtin
852 * compiler stuff and thus can use variables in arrays the structure may
853 * contain. If in this usful to determin the sizes of structures ending
854 * with a variable length field.
855 *
856 * @returns offset into the structure of the specified member. unsigned.
857 * @param type Structure type.
858 * @param member Member.
859 */
860#define RT_UOFFSETOF(type, member) ( (uintptr_t)&( ((type *)(void *)0)->member) )
861
862/** @def RT_SIZEOFMEMB
863 * Get the size of a structure member.
864 *
865 * @returns size of the structure member.
866 * @param type Structure type.
867 * @param member Member.
868 */
869#define RT_SIZEOFMEMB(type, member) ( sizeof(((type *)(void *)0)->member) )
870
871/** @def RT_ELEMENTS
872 * Calcs the number of elements in an array.
873 * @returns Element count.
874 * @param aArray Array in question.
875 */
876#define RT_ELEMENTS(aArray) ( sizeof(aArray) / sizeof((aArray)[0]) )
877
878#ifdef RT_OS_OS2
879/* Undefine RT_MAX since there is an unfortunate clash with the max
880 resource type define in os2.h. */
881# undef RT_MAX
882#endif
883
884/** @def RT_MAX
885 * Finds the maximum value.
886 * @returns The higher of the two.
887 * @param Value1 Value 1
888 * @param Value2 Value 2
889 */
890#define RT_MAX(Value1, Value2) ((Value1) >= (Value2) ? (Value1) : (Value2))
891
892/** @def RT_MIN
893 * Finds the minimum value.
894 * @returns The lower of the two.
895 * @param Value1 Value 1
896 * @param Value2 Value 2
897 */
898#define RT_MIN(Value1, Value2) ((Value1) <= (Value2) ? (Value1) : (Value2))
899
900/** @def RT_ABS
901 * Get the absolute (non-negative) value.
902 * @returns The absolute value of Value.
903 * @param Value The value.
904 */
905#define RT_ABS(Value) ((Value) >= 0 ? (Value) : -(Value))
906
907/** @def RT_LOWORD
908 * Gets the low word (=uint16_t) of something. */
909#define RT_LOWORD(a) ((a) & 0xffff)
910
911/** @def RT_HIWORD
912 * Gets the high word (=uint16_t) of a 32 bit something. */
913#define RT_HIWORD(a) ((a) >> 16)
914
915/** @def RT_LOBYTE
916 * Gets the low byte of something. */
917#define RT_LOBYTE(a) ((a) & 0xff)
918
919/** @def RT_HIBYTE
920 * Gets the low byte of a 16 bit something. */
921#define RT_HIBYTE(a) ((a) >> 8)
922
923/** @def RT_BYTE1
924 * Gets first byte of something. */
925#define RT_BYTE1(a) ((a) & 0xff)
926
927/** @def RT_BYTE2
928 * Gets second byte of something. */
929#define RT_BYTE2(a) (((a) >> 8) & 0xff)
930
931/** @def RT_BYTE3
932 * Gets second byte of something. */
933#define RT_BYTE3(a) (((a) >> 16) & 0xff)
934
935/** @def RT_BYTE4
936 * Gets fourth byte of something. */
937#define RT_BYTE4(a) (((a) >> 24) & 0xff)
938
939
940/** @def RT_MAKE_U64
941 * Constructs a uint64_t value from two uint32_t values.
942 */
943#define RT_MAKE_U64(Lo, Hi) ( (uint64_t)((uint32_t)(Hi)) << 32 | (uint32_t)(Lo) )
944
945/** @def RT_MAKE_U64_FROM_U16
946 * Constructs a uint64_t value from four uint16_t values.
947 */
948#define RT_MAKE_U64_FROM_U16(w0, w1, w2, w3) \
949 ( (uint64_t)((uint16_t)(w3)) << 48 \
950 | (uint64_t)((uint16_t)(w2)) << 32 \
951 | (uint32_t)((uint16_t)(w1)) << 16 \
952 | (uint16_t)(w0) )
953
954/** @def RT_MAKE_U64_FROM_U8
955 * Constructs a uint64_t value from eight uint8_t values.
956 */
957#define RT_MAKE_U64_FROM_U8(b0, b1, b2, b3, b4, b5, b6, b7) \
958 ( (uint64_t)((uint8_t)(b7)) << 56 \
959 | (uint64_t)((uint8_t)(b6)) << 48 \
960 | (uint64_t)((uint8_t)(b5)) << 40 \
961 | (uint64_t)((uint8_t)(b4)) << 32 \
962 | (uint32_t)((uint8_t)(b3)) << 24 \
963 | (uint32_t)((uint8_t)(b2)) << 16 \
964 | (uint16_t)((uint8_t)(b1)) << 8 \
965 | (uint8_t)(b0) )
966
967/** @def RT_MAKE_U32
968 * Constructs a uint32_t value from two uint16_t values.
969 */
970#define RT_MAKE_U32(Lo, Hi) ( (uint32_t)((uint16_t)(Hi)) << 16 | (uint16_t)(Lo) )
971
972/** @def RT_MAKE_U32_FROM_U8
973 * Constructs a uint32_t value from four uint8_t values.
974 */
975#define RT_MAKE_U32_FROM_U8(b0, b1, b2, b3) \
976 ( (uint32_t)((uint8_t)(b3)) << 24 \
977 | (uint32_t)((uint8_t)(b2)) << 16 \
978 | (uint16_t)((uint8_t)(b1)) << 8 \
979 | (uint8_t)(b0) )
980
981/** @def RT_MAKE_U16
982 * Constructs a uint32_t value from two uint16_t values.
983 */
984#define RT_MAKE_U16(Lo, Hi) ( (uint16_t)((uint8_t)(Hi)) << 8 | (uint8_t)(Lo) )
985
986
987/** @def RT_H2LE_U64
988 * Converts uint64_t value from host to little endian byte order. */
989#define RT_H2LE_U64(u64) (u64)
990
991/** @def RT_H2LE_U32
992 * Converts uint32_t value from host to little endian byte order. */
993#define RT_H2LE_U32(u32) (u32)
994
995/** @def RT_H2LE_U16
996 * Converts uint16_t value from host to little endian byte order. */
997#define RT_H2LE_U16(u16) (u16)
998
999/** @def RT_LE2H_U64
1000 * Converts uint64_t value from little endian to host byte order. */
1001#define RT_LE2H_U64(u64) (u64)
1002
1003/** @def RT_LE2H_U32
1004 * Converts uint32_t value from little endian to host byte order. */
1005#define RT_LE2H_U32(u32) (u32)
1006
1007/** @def RT_LE2H_U16
1008 * Converts uint16_t value from little endian to host byte order. */
1009#define RT_LE2H_U16(u16) (u16)
1010
1011
1012/** @def RT_H2BE_U64
1013 * Converts uint64_t value from host to big endian byte order. */
1014#define RT_H2BE_U64(u64) RT_MAKE_U64(RT_H2BE_U32((u64) >> 32), RT_H2BE_U32((u64) & 0xffffffff))
1015
1016/** @def RT_H2BE_U32
1017 * Converts uint32_t value from host to big endian byte order. */
1018#define RT_H2BE_U32(u32) (RT_BYTE4(u32) | (RT_BYTE3(u32) << 8) | (RT_BYTE2(u32) << 16) | (RT_BYTE1(u32) << 24))
1019
1020/** @def RT_H2BE_U16
1021 * Converts uint16_t value from host to big endian byte order. */
1022#define RT_H2BE_U16(u16) (RT_HIBYTE(u16) | (RT_LOBYTE(u16) << 8))
1023
1024/** @def RT_BE2H_U64
1025 * Converts uint64_t value from big endian to host byte order. */
1026#define RT_BE2H_U64(u64) RT_MAKE_U64(RT_H2BE_U32((u64) >> 32), RT_H2BE_U32((u64) & 0xffffffff))
1027
1028/** @def RT_BE2H_U32
1029 * Converts uint32_t value from big endian to host byte order. */
1030#define RT_BE2H_U32(u32) (RT_BYTE4(u32) | (RT_BYTE3(u32) << 8) | (RT_BYTE2(u32) << 16) | (RT_BYTE1(u32) << 24))
1031
1032/** @def RT_BE2H_U16
1033 * Converts uint16_t value from big endian to host byte order. */
1034#define RT_BE2H_U16(u16) (RT_HIBYTE(u16) | (RT_LOBYTE(u16) << 8))
1035
1036
1037/** @def RT_H2N_U32
1038 * Converts uint32_t value from host to network byte order. */
1039#define RT_H2N_U32(u32) RT_H2BE_U32(u32)
1040
1041/** @def RT_H2N_U16
1042 * Converts uint16_t value from host to network byte order. */
1043#define RT_H2N_U16(u16) RT_H2BE_U16(u16)
1044
1045/** @def RT_N2H_U32
1046 * Converts uint32_t value from network to host byte order. */
1047#define RT_N2H_U32(u32) RT_BE2H_U32(u32)
1048
1049/** @def RT_N2H_U16
1050 * Converts uint16_t value from network to host byte order. */
1051#define RT_N2H_U16(u16) RT_BE2H_U16(u16)
1052
1053
1054/** @def RT_NO_DEPRECATED_MACROS
1055 * Define RT_NO_DEPRECATED_MACROS to not define deprecated macros.
1056 */
1057#ifndef RT_NO_DEPRECATED_MACROS
1058/** @copydoc RT_ELEMENTS
1059 * @deprecated use RT_ELEMENTS. */
1060# define ELEMENTS(aArray) RT_ELEMENTS(aArray)
1061#endif
1062
1063
1064/*
1065 * The BSD sys/param.h + machine/param.h file is a major source of
1066 * namespace pollution. Kill off some of the worse ones unless we're
1067 * compiling kernel code.
1068 */
1069#if defined(RT_OS_DARWIN) \
1070 && !defined(KERNEL) \
1071 && !defined(RT_NO_BSD_PARAM_H_UNDEFING) \
1072 && ( defined(_SYS_PARAM_H_) || defined(_I386_PARAM_H_) )
1073/* sys/param.h: */
1074# undef PSWP
1075# undef PVM
1076# undef PINOD
1077# undef PRIBO
1078# undef PVFS
1079# undef PZERO
1080# undef PSOCK
1081# undef PWAIT
1082# undef PLOCK
1083# undef PPAUSE
1084# undef PUSER
1085# undef PRIMASK
1086# undef MINBUCKET
1087# undef MAXALLOCSAVE
1088# undef FSHIFT
1089# undef FSCALE
1090
1091/* i386/machine.h: */
1092# undef ALIGN
1093# undef ALIGNBYTES
1094# undef DELAY
1095# undef STATUS_WORD
1096# undef USERMODE
1097# undef BASEPRI
1098# undef MSIZE
1099# undef CLSIZE
1100# undef CLSIZELOG2
1101#endif
1102
1103
1104/** @def NULL
1105 * NULL pointer.
1106 */
1107#ifndef NULL
1108# ifdef __cplusplus
1109# define NULL 0
1110# else
1111# define NULL ((void*)0)
1112# endif
1113#endif
1114
1115/** @def NIL_OFFSET
1116 * NIL offset.
1117 * Whenever we use offsets instead of pointers to save space and relocation effort
1118 * NIL_OFFSET shall be used as the equivalent to NULL.
1119 */
1120#define NIL_OFFSET (~0U)
1121
1122/** @def NOREF
1123 * Keeps the compiler from bitching about an unused parameters.
1124 */
1125#define NOREF(var) (void)(var)
1126
1127/** @def Breakpoint
1128 * Emit a debug breakpoint instruction.
1129 *
1130 * Use this for instrumenting a debugging session only!
1131 * No comitted code shall use Breakpoint().
1132 */
1133#ifdef __GNUC__
1134# define Breakpoint() __asm__ __volatile__("int $3\n\t")
1135#endif
1136#ifdef _MSC_VER
1137# define Breakpoint() __asm int 3
1138#endif
1139#if defined(__IBMC__) || defined(__IBMCPP__)
1140# define Breakpoint() __interrupt(3)
1141#endif
1142#ifndef Breakpoint
1143# error "This compiler is not supported!"
1144#endif
1145
1146
1147/** Size Constants
1148 * (Of course, these are binary computer terms, not SI.)
1149 * @{
1150 */
1151/** 1 K (Kilo) (1 024). */
1152#define _1K 0x00000400
1153/** 4 K (Kilo) (4 096). */
1154#define _4K 0x00001000
1155/** 32 K (Kilo) (32 678). */
1156#define _32K 0x00008000
1157/** 64 K (Kilo) (65 536). */
1158#define _64K 0x00010000
1159/** 128 K (Kilo) (131 072). */
1160#define _128K 0x00020000
1161/** 256 K (Kilo) (262 144). */
1162#define _256K 0x00040000
1163/** 512 K (Kilo) (524 288). */
1164#define _512K 0x00080000
1165/** 1 M (Mega) (1 048 576). */
1166#define _1M 0x00100000
1167/** 2 M (Mega) (2 097 152). */
1168#define _2M 0x00200000
1169/** 4 M (Mega) (4 194 304). */
1170#define _4M 0x00400000
1171/** 1 G (Giga) (1 073 741 824). */
1172#define _1G 0x40000000
1173/** 2 G (Giga) (2 147 483 648). (32-bit) */
1174#define _2G32 0x80000000U
1175/** 2 G (Giga) (2 147 483 648). (64-bit) */
1176#define _2G 0x0000000080000000LL
1177/** 4 G (Giga) (4 294 967 296). */
1178#define _4G 0x0000000100000000LL
1179/** 1 T (Tera) (1 099 511 627 776). */
1180#define _1T 0x0000010000000000LL
1181/** 1 P (Peta) (1 125 899 906 842 624). */
1182#define _1P 0x0004000000000000LL
1183/** 1 E (Exa) (1 152 921 504 606 846 976). */
1184#define _1E 0x1000000000000000LL
1185/** 2 E (Exa) (2 305 843 009 213 693 952). */
1186#define _2E 0x2000000000000000ULL
1187/** @} */
1188
1189/** @def VALID_PTR
1190 * Pointer validation macro.
1191 * @param ptr
1192 */
1193#if defined(RT_ARCH_AMD64)
1194# ifdef IN_RING3
1195# if defined(RT_OS_DARWIN) /* first 4GB is reserved for legacy kernel. */
1196# define VALID_PTR(ptr) ( (uintptr_t)(ptr) >= _4G \
1197 && !((uintptr_t)(ptr) & 0xffff800000000000ULL) )
1198# elif defined(RT_OS_SOLARIS) /* The kernel only used the top 2TB, but keep it simple. */
1199# define VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U \
1200 && ( ((uintptr_t)(ptr) & 0xffff800000000000ULL) == 0xffff800000000000ULL \
1201 || ((uintptr_t)(ptr) & 0xffff800000000000ULL) == 0) )
1202# else
1203# define VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U \
1204 && !((uintptr_t)(ptr) & 0xffff800000000000ULL) )
1205# endif
1206# else /* !IN_RING3 */
1207# define VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U \
1208 && ( ((uintptr_t)(ptr) & 0xffff800000000000ULL) == 0xffff800000000000ULL \
1209 || ((uintptr_t)(ptr) & 0xffff800000000000ULL) == 0) )
1210# endif /* !IN_RING3 */
1211#elif defined(RT_ARCH_X86)
1212# define VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U )
1213#else
1214# error "Architecture identifier missing / not implemented."
1215#endif
1216
1217
1218/** @def VALID_PHYS32
1219 * 32 bits physical address validation macro.
1220 * @param Phys The RTGCPHYS address.
1221 */
1222#define VALID_PHYS32(Phys) ( (uint64_t)(Phys) < (uint64_t)_4G )
1223
1224/** @def N_
1225 * The \#define N_ is used mark a string for translation. This is usable in
1226 * any part of the code, as it is only used by the tools that create message
1227 * catalogs. This macro is a no-op as far as the compiler and code generation
1228 * is concerned.
1229 *
1230 * If you want to both mark a string for translation and translate it, use _.
1231 */
1232#define N_(s) (s)
1233
1234/** @def _
1235 * The \#define _ is used mark a string for translation and to translate it in
1236 * one step.
1237 *
1238 * If you want to only mark a string for translation, use N_.
1239 */
1240#define _(s) gettext(s)
1241
1242
1243/** @def __PRETTY_FUNCTION__
1244 * With GNU C we'd like to use the builtin __PRETTY_FUNCTION__, so define that for the other compilers.
1245 */
1246#if !defined(__GNUC__) && !defined(__PRETTY_FUNCTION__)
1247# define __PRETTY_FUNCTION__ __FUNCTION__
1248#endif
1249
1250
1251/** @def RT_STRICT
1252 * The \#define RT_STRICT controls whether or not assertions and other runtime checks
1253 * should be compiled in or not.
1254 *
1255 * If you want assertions which are not a subject to compile time options use
1256 * the AssertRelease*() flavors.
1257 */
1258#if !defined(RT_STRICT) && defined(DEBUG)
1259# define RT_STRICT
1260#endif
1261
1262/** Source position. */
1263#define RT_SRC_POS __FILE__, __LINE__, __PRETTY_FUNCTION__
1264
1265/** Source position declaration. */
1266#define RT_SRC_POS_DECL const char *pszFile, unsigned iLine, const char *pszFunction
1267
1268/** Source position arguments. */
1269#define RT_SRC_POS_ARGS pszFile, iLine, pszFunction
1270
1271/** @} */
1272
1273
1274/** @defgroup grp_rt_cdefs_cpp Special Macros for C++
1275 * @ingroup grp_rt_cdefs
1276 * @{
1277 */
1278
1279#ifdef __cplusplus
1280
1281/** @def DECLEXPORT_CLASS
1282 * How to declare an exported class. Place this macro after the 'class'
1283 * keyword in the declaration of every class you want to export.
1284 *
1285 * @note It is necessary to use this macro even for inner classes declared
1286 * inside the already exported classes. This is a GCC specific requirement,
1287 * but it seems not to harm other compilers.
1288 */
1289#if defined(_MSC_VER) || defined(RT_OS_OS2)
1290# define DECLEXPORT_CLASS __declspec(dllexport)
1291#else
1292# ifdef VBOX_HAVE_VISIBILITY_HIDDEN
1293# define DECLEXPORT_CLASS __attribute__((visibility("default")))
1294# else
1295# define DECLEXPORT_CLASS
1296# endif
1297#endif
1298
1299/** @def DECLIMPORT_CLASS
1300 * How to declare an imported class Place this macro after the 'class'
1301 * keyword in the declaration of every class you want to export.
1302 *
1303 * @note It is necessary to use this macro even for inner classes declared
1304 * inside the already exported classes. This is a GCC specific requirement,
1305 * but it seems not to harm other compilers.
1306 */
1307#if defined(_MSC_VER) || (defined(RT_OS_OS2) && !defined(__IBMC__) && !defined(__IBMCPP__))
1308# define DECLIMPORT_CLASS __declspec(dllimport)
1309#else
1310# ifdef VBOX_HAVE_VISIBILITY_HIDDEN
1311# define DECLIMPORT_CLASS __attribute__((visibility("default")))
1312# else
1313# define DECLIMPORT_CLASS
1314# endif
1315#endif
1316
1317/** @def WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP
1318 * Macro to work around error C2593 of the not-so-smart MSVC 7.x ambiguity
1319 * resolver. The following snippet clearly demonstrates the code causing this
1320 * error:
1321 * @code
1322 * class A
1323 * {
1324 * public:
1325 * operator bool() const { return false; }
1326 * operator int*() const { return NULL; }
1327 * };
1328 * int main()
1329 * {
1330 * A a;
1331 * if (!a);
1332 * if (a && 0);
1333 * return 0;
1334 * }
1335 * @endcode
1336 * The code itself seems pretty valid to me and GCC thinks the same.
1337 *
1338 * This macro fixes the compiler error by explicitly overloading implicit
1339 * global operators !, && and || that take the given class instance as one of
1340 * their arguments.
1341 *
1342 * The best is to use this macro right after the class declaration.
1343 *
1344 * @note The macro expands to nothing for compilers other than MSVC.
1345 *
1346 * @param Cls Class to apply the workaround to
1347 */
1348#if defined(_MSC_VER)
1349# define WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP(Cls) \
1350 inline bool operator! (const Cls &that) { return !bool (that); } \
1351 inline bool operator&& (const Cls &that, bool b) { return bool (that) && b; } \
1352 inline bool operator|| (const Cls &that, bool b) { return bool (that) || b; } \
1353 inline bool operator&& (bool b, const Cls &that) { return b && bool (that); } \
1354 inline bool operator|| (bool b, const Cls &that) { return b || bool (that); }
1355#else
1356# define WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP(Cls)
1357#endif
1358
1359/** @def WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP_TPL
1360 * Version of WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP for template classes.
1361 *
1362 * @param Tpl Name of the template class to apply the workaround to
1363 * @param ArgsDecl arguments of the template, as declared in |<>| after the
1364 * |template| keyword, including |<>|
1365 * @param Args arguments of the template, as specified in |<>| after the
1366 * template class name when using the, including |<>|
1367 *
1368 * Example:
1369 * @code
1370 * // template class declaration
1371 * template <class C>
1372 * class Foo { ... };
1373 * // applied workaround
1374 * WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP_TPL (Foo, <class C>, <C>)
1375 * @endcode
1376 */
1377#if defined(_MSC_VER)
1378# define WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP_TPL(Tpl, ArgsDecl, Args) \
1379 template ArgsDecl \
1380 inline bool operator! (const Tpl Args &that) { return !bool (that); } \
1381 template ArgsDecl \
1382 inline bool operator&& (const Tpl Args &that, bool b) { return bool (that) && b; } \
1383 template ArgsDecl \
1384 inline bool operator|| (const Tpl Args &that, bool b) { return bool (that) || b; } \
1385 template ArgsDecl \
1386 inline bool operator&& (bool b, const Tpl Args &that) { return b && bool (that); } \
1387 template ArgsDecl \
1388 inline bool operator|| (bool b, const Tpl Args &that) { return b || bool (that); }
1389#else
1390# define WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP_TPL(Tpl, ArgsDecl, Args)
1391#endif
1392
1393
1394/** @def DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP
1395 * Declares the copy constructor and the assignment operation as inlined no-ops
1396 * (non-existent functions) for the given class. Use this macro inside the
1397 * private section if you want to effectively disable these operations for your
1398 * class.
1399 *
1400 * @param Cls class name to declare for
1401 */
1402
1403#define DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(Cls) \
1404 inline Cls (const Cls &); \
1405 inline Cls &operator= (const Cls &);
1406
1407
1408/** @def DECLARE_CLS_NEW_DELETE_NOOP
1409 * Declares the new and delete operations as no-ops (non-existent functions)
1410 * for the given class. Use this macro inside the private section if you want
1411 * to effectively limit creating class instances on the stack only.
1412 *
1413 * @note The destructor of the given class must not be virtual, otherwise a
1414 * compile time error will occur. Note that this is not a drawback: having
1415 * the virtual destructor for a stack-based class is absolutely useless
1416 * (the real class of the stack-based instance is always known to the compiler
1417 * at compile time, so it will always call the correct destructor).
1418 *
1419 * @param Cls class name to declare for
1420 */
1421#define DECLARE_CLS_NEW_DELETE_NOOP(Cls) \
1422 inline static void *operator new (size_t); \
1423 inline static void operator delete (void *);
1424
1425#endif /* defined(__cplusplus) */
1426
1427/** @} */
1428
1429#endif
1430
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