VirtualBox

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

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

added DECLNORETURN()

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