VirtualBox

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

Last change on this file since 6356 was 6216, checked in by vboxsync, 17 years ago

added note to DECLNORETURN

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