VirtualBox

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

Last change on this file since 647 was 647, checked in by vboxsync, 18 years ago

VALID_PTR update: when running 32-bit in compatability mode, there might be close to 4GB of memory available.

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