VirtualBox

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

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

RTGCPHYS is now 64 bits

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