VirtualBox

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

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

Reverse allocation for Windows hosts: physical pages are allocated in the support driver and mapped into user space
VMM: Use locked memory for the MM pagepool structures.

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