VirtualBox

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

Last change on this file since 8165 was 8163, checked in by vboxsync, 17 years ago

rebrand IPRT

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