VirtualBox

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

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

fixed typo.

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