VirtualBox

source: kBuild/trunk/src/lib/k/kDefs.h@ 1881

Last change on this file since 1881 was 1601, checked in by bird, 17 years ago

Added K_OS_DRAGONFLY w/ detection.

  • Property svn:eol-style set to native
File size: 17.8 KB
Line 
1/* $Id: kDefs.h 15 2008-05-05 22:14:33Z bird $ */
2/** @file
3 *
4 * kTypes - Defines and Macros.
5 *
6 * Copyright (c) 2007-2008 knut st. osmundsen <[email protected]>
7 *
8 *
9 * This file is part of k*.
10 *
11 * k* is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * k* is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with k*; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#ifndef ___k_kDefs_h___
28#define ___k_kDefs_h___
29
30/** @defgroup grp_kDefs kDefs - Defines and Macros
31 * @{ */
32
33/** @name Operative System Identifiers.
34 * These are the value that the K_OS \#define can take.
35 * @{
36 */
37/** Unknown OS. */
38#define K_OS_UNKNOWN 0
39/** Darwin - aka Mac OS X. */
40#define K_OS_DARWIN 1
41/** DragonFly BSD. */
42#define K_OS_DRAGONFLY 2
43/** FreeBSD. */
44#define K_OS_FREEBSD 3
45/** Linux. */
46#define K_OS_LINUX 4
47/** NetBSD. */
48#define K_OS_NETBSD 5
49/** NT (native). */
50#define K_OS_NT 6
51/** OpenBSD*/
52#define K_OS_OPENBSD 7
53/** OS/2 */
54#define K_OS_OS2 8
55/** Solaris */
56#define K_OS_SOLARIS 9
57/** Windows. */
58#define K_OS_WINDOWS 10
59/** The max K_OS_* value (exclusive). */
60#define K_OS_MAX 11
61/** @} */
62
63/** @def K_OS
64 * Indicates which OS we're targetting. It's a \#define with is
65 * assigned one of the K_OS_* defines above.
66 *
67 * So to test if we're on FreeBSD do the following:
68 * @code
69 * #if K_OS == K_OS_FREEBSD
70 * some_funky_freebsd_specific_stuff();
71 * #endif
72 * @endcode
73 */
74#ifndef K_OS
75# if defined(__APPLE__)
76# define K_OS K_OS_DARWIN
77# elif defined(__DragonFly__)
78# define K_OS K_OS_DRAGONFLY
79# elif defined(__FreeBSD__) /*??*/
80# define K_OS K_OS_FREEBSD
81# elif defined(__gnu_linux__)
82# define K_OS K_OS_LINUX
83# elif defined(__NetBSD__) /*??*/
84# define K_OS K_OS_NETBSD
85# elif defined(__OpenBSD__) /*??*/
86# define K_OS K_OS_OPENBSD
87# elif defined(__OS2__)
88# define K_OS K_OS_OS2
89# elif defined(__sun__) || defined(__SunOS__) || defined(__sun) || defined(__SunOS)
90# define K_OS K_OS_SOLARIS
91# elif defined(_WIN32) || defined(_WIN64)
92# define K_OS K_OS_WINDOWS
93# else
94# error "Port Me"
95# endif
96#endif
97#if K_OS < K_OS_UNKNOWN || K_OS >= K_OS_MAX
98# error "Invalid K_OS value."
99#endif
100
101
102
103/** @name Architecture bit width.
104 * @{ */
105#define K_ARCH_BIT_8 0x0100 /**< 8-bit */
106#define K_ARCH_BIT_16 0x0200 /**< 16-bit */
107#define K_ARCH_BIT_32 0x0400 /**< 32-bit */
108#define K_ARCH_BIT_64 0x0800 /**< 64-bit */
109#define K_ARCH_BIT_128 0x1000 /**< 128-bit */
110#define K_ARCH_BIT_MASK 0x1f00 /**< The bit mask. */
111#define K_ARCH_BIT_SHIFT 5 /**< Shift count for producing the width in bits. */
112#define K_ARCH_BYTE_SHIFT 8 /**< Shift count for producing the width in bytes. */
113/** @} */
114
115/** @name Architecture Endianness.
116 * @{ */
117#define K_ARCH_END_LITTLE 0x2000 /**< Little-endian. */
118#define K_ARCH_END_BIG 0x4000 /**< Big-endian. */
119#define K_ARCH_END_BI 0x6000 /**< Bi-endian, can be switched. */
120#define K_ARCH_END_MASK 0x6000 /**< The endian mask. */
121#define K_ARCH_END_SHIFT 13 /**< Shift count for converting between this K_ENDIAN_*. */
122/** @} */
123
124/** @name Architecture Identifiers.
125 * These are the value that the K_ARCH \#define can take.
126 *@{ */
127/** Unknown CPU architecture. */
128#define K_ARCH_UNKNOWN ( 0 )
129/** Clone or Intel 16-bit x86. */
130#define K_ARCH_X86_16 ( 1 | K_ARCH_BIT_16 | K_ARCH_END_LITTLE)
131/** Clone or Intel 32-bit x86. */
132#define K_ARCH_X86_32 ( 2 | K_ARCH_BIT_32 | K_ARCH_END_LITTLE)
133/** AMD64 (including clones). */
134#define K_ARCH_AMD64 ( 3 | K_ARCH_BIT_64 | K_ARCH_END_LITTLE)
135/** Itanic (64-bit). */
136#define K_ARCH_IA64 ( 4 | K_ARCH_BIT_64 | K_ARCH_END_BI)
137/** ALPHA (64-bit). */
138#define K_ARCH_ALPHA ( 5 | K_ARCH_BIT_64 | K_ARCH_END_BI)
139/** ALPHA limited to 32-bit. */
140#define K_ARCH_ALPHA_32 ( 6 | K_ARCH_BIT_32 | K_ARCH_END_BI)
141/** 32-bit ARM. */
142#define K_ARCH_ARM_32 ( 7 | K_ARCH_BIT_32 | K_ARCH_END_BI)
143/** 64-bit ARM. */
144#define K_ARCH_ARM_64 ( 8 | K_ARCH_BIT_64 | K_ARCH_END_BI)
145/** 32-bit MIPS. */
146#define K_ARCH_MIPS_32 ( 9 | K_ARCH_BIT_32 | K_ARCH_END_BI)
147/** 64-bit MIPS. */
148#define K_ARCH_MIPS_64 (10 | K_ARCH_BIT_64 | K_ARCH_END_BI)
149/** 32-bit PA-RISC. */
150#define K_ARCH_PARISC_32 (11 | K_ARCH_BIT_32 | K_ARCH_END_BI)
151/** 64-bit PA-RISC. */
152#define K_ARCH_PARISC_64 (12 | K_ARCH_BIT_64 | K_ARCH_END_BI)
153/** 32-bit PowerPC. */
154#define K_ARCH_POWERPC_32 (13 | K_ARCH_BIT_32 | K_ARCH_END_BI)
155/** 64-bit PowerPC. */
156#define K_ARCH_POWERPC_64 (14 | K_ARCH_BIT_64 | K_ARCH_END_BI)
157/** 32(31)-bit S390. */
158#define K_ARCH_S390_32 (15 | K_ARCH_BIT_32 | K_ARCH_END_BIG)
159/** 64-bit S390. */
160#define K_ARCH_S390_64 (16 | K_ARCH_BIT_64 | K_ARCH_END_BIG)
161/** 32-bit SPARC. */
162#define K_ARCH_SPARC_32 (17 | K_ARCH_BIT_32 | K_ARCH_END_BIG)
163/** 64-bit SPARC. */
164#define K_ARCH_SPARC_64 (18 | K_ARCH_BIT_64 | K_ARCH_END_BI)
165/** The end of the valid architecture values (exclusive). */
166#define K_ARCH_MAX (19)
167/** @} */
168
169
170/** @def K_ARCH
171 * The value of this \#define indicates which architecture we're targetting.
172 */
173#ifndef K_ARCH
174 /* detection based on compiler defines. */
175# if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || defined(_M_X64) || defined(__amd64)
176# define K_ARCH K_ARCH_AMD64
177# elif defined(__i386__) || defined(__x86__) || defined(__X86__) || defined(_M_IX86) || defined(__i386)
178# define K_ARCH K_ARCH_X86_32
179# elif defined(__ia64__) || defined(__IA64__) || defined(_M_IA64)
180# define K_ARCH K_ARCH_IA64
181# elif defined(__alpha__)
182# define K_ARCH K_ARCH_ALPHA
183# elif defined(__arm__) || defined(__arm32__)
184# define K_ARCH K_ARCH_ARM_32
185# elif defined(__hppa__) && defined(__LP64__)
186# define K_ARCH K_ARCH_PARISC_64
187# elif defined(__hppa__)
188# define K_ARCH K_ARCH_PARISC_32
189# elif defined(__mips64)
190# define K_ARCH K_ARCH_MIPS_64
191# elif defined(__mips__)
192# define K_ARCH K_ARCH_MIPS_32
193# elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__)
194# define K_ARCH K_ARCH_POWERPC_64
195# elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)
196# define K_ARCH K_ARCH_POWERPC_32
197# elif defined(__sparcv9__) || defined(__sparcv9)
198# define K_ARCH K_ARCH_SPARC_64
199# elif defined(__sparc__) || defined(__sparc)
200# define K_ARCH K_ARCH_SPARC_32
201# elif defined(__s390x__)
202# define K_ARCH K_ARCH_S390_64
203# elif defined(__s390__)
204# define K_ARCH K_ARCH_S390_32
205# else
206# error "Port Me"
207# endif
208#else
209 /* validate the user specified value. */
210# if (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_8 \
211 && (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_16 \
212 && (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_32 \
213 && (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_64 \
214 && (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_128
215# error "Invalid K_ARCH value (bit)"
216# endif
217# if (K_ARCH & K_ARCH_END_MASK) != K_ARCH_END_LITTLE \
218 && (K_ARCH & K_ARCH_END_MASK) != K_ARCH_END_BIG \
219 && (K_ARCH & K_ARCH_END_MASK) != K_ARCH_END_BI
220# error "Invalid K_ARCH value (endian)"
221# endif
222# if (K_ARCH & ~(K_ARCH_BIT_MASK | K_ARCH_BIT_END_MASK)) < K_ARCH_UNKNOWN \
223 || (K_ARCH & ~(K_ARCH_BIT_MASK | K_ARCH_BIT_END_MASK)) >= K_ARCH_MAX
224# error "Invalid K_ARCH value"
225# endif
226#endif
227
228/** @def K_ARCH_IS_VALID
229 * Check if the architecture identifier is valid.
230 * @param arch The K_ARCH_* define to examin.
231 */
232#define K_ARCH_IS_VALID(arch) ( ( ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_8 \
233 || ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_16 \
234 || ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_32 \
235 || ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_64 \
236 || ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_128) \
237 && \
238 ( ((arch) & K_ARCH_END_MASK) == K_ARCH_END_LITTLE \
239 || ((arch) & K_ARCH_END_MASK) == K_ARCH_END_BIG \
240 || ((arch) & K_ARCH_END_MASK) == K_ARCH_END_BI) \
241 && \
242 ( ((arch) & ~(K_ARCH_BIT_MASK | K_ARCH_END_MASK)) >= K_ARCH_UNKNOWN \
243 && ((arch) & ~(K_ARCH_BIT_MASK | K_ARCH_END_MASK)) < K_ARCH_MAX) \
244 )
245
246/** @def K_ARCH_BITS_EX
247 * Determin the architure byte width of the specified architecture.
248 * @param arch The K_ARCH_* define to examin.
249 */
250#define K_ARCH_BITS_EX(arch) ( ((arch) & K_ARCH_BIT_MASK) >> K_ARCH_BIT_SHIFT )
251
252/** @def K_ARCH_BYTES_EX
253 * Determin the architure byte width of the specified architecture.
254 * @param arch The K_ARCH_* define to examin.
255 */
256#define K_ARCH_BYTES_EX(arch) ( ((arch) & K_ARCH_BIT_MASK) >> K_ARCH_BYTE_SHIFT )
257
258/** @def K_ARCH_ENDIAN_EX
259 * Determin the K_ENDIAN value for the specified architecture.
260 * @param arch The K_ARCH_* define to examin.
261 */
262#define K_ARCH_ENDIAN_EX(arch) ( ((arch) & K_ARCH_END_MASK) >> K_ARCH_END_SHIFT )
263
264/** @def K_ARCH_BITS
265 * Determin the target architure bit width.
266 */
267#define K_ARCH_BITS K_ARCH_BITS_EX(K_ARCH)
268
269/** @def K_ARCH_BYTES
270 * Determin the target architure byte width.
271 */
272#define K_ARCH_BYTES K_ARCH_BYTES_EX(K_ARCH)
273
274/** @def K_ARCH_ENDIAN
275 * Determin the target K_ENDIAN value.
276 */
277#define K_ARCH_ENDIAN K_ARCH_ENDIAN_EX(K_ARCH)
278
279
280
281/** @name Endianness Identifiers.
282 * These are the value that the K_ENDIAN \#define can take.
283 * @{ */
284#define K_ENDIAN_LITTLE 1 /**< Little-endian. */
285#define K_ENDIAN_BIG 2 /**< Big-endian. */
286#define K_ENDIAN_BI 3 /**< Bi-endian, can be switched. Only used with K_ARCH. */
287/** @} */
288
289/** @def K_ENDIAN
290 * The value of this \#define indicates the target endianness.
291 *
292 * @remark It's necessary to define this (or add the necessary dection here)
293 * on bi-endian architectures.
294 */
295#ifndef K_ENDIAN
296 /* use K_ARCH if possible. */
297# if K_ARCH_END != K_ENDIAN_BI
298# define K_ENDIAN K_ARCH_ENDIAN
299# else
300# error "Port Me or define K_ENDIAN."
301# endif
302#else
303 /* validate the user defined value. */
304# if K_ENDIAN != K_ENDIAN_LITTLE
305 && K_ENDIAN != K_ENDIAN_BIG
306# error "K_ENDIAN must either be defined as K_ENDIAN_LITTLE or as K_ENDIAN_BIG."
307# endif
308#endif
309
310/** @name Endian Conversion
311 * @{ */
312
313/** @def K_E2E_U16
314 * Convert the endian of an unsigned 16-bit value. */
315# define K_E2E_U16(u16) ( (KU16) (((u16) >> 8) | ((u16) << 8)) )
316/** @def K_E2E_U32
317 * Convert the endian of an unsigned 32-bit value. */
318# define K_E2E_U32(u32) ( ( ((u32) & KU32_C(0xff000000)) >> 24 ) \
319 | ( ((u32) & KU32_C(0x00ff0000)) >> 8 ) \
320 | ( ((u32) & KU32_C(0x0000ff00)) << 8 ) \
321 | ( ((u32) & KU32_C(0x000000ff)) << 24 ) \
322 )
323/** @def K_E2E_U64
324 * Convert the endian of an unsigned 64-bit value. */
325# define K_E2E_U64(u64) ( ( ((u64) & KU64_C(0xff00000000000000)) >> 56 ) \
326 | ( ((u64) & KU64_C(0x00ff000000000000)) >> 40 ) \
327 | ( ((u64) & KU64_C(0x0000ff0000000000)) >> 24 ) \
328 | ( ((u64) & KU64_C(0x000000ff00000000)) >> 8 ) \
329 | ( ((u64) & KU64_C(0x00000000ff000000)) << 8 ) \
330 | ( ((u64) & KU64_C(0x0000000000ff0000)) << 24 ) \
331 | ( ((u64) & KU64_C(0x000000000000ff00)) << 40 ) \
332 | ( ((u64) & KU64_C(0x00000000000000ff)) << 56 ) \
333 )
334
335/** @def K_LE2H_U16
336 * Unsigned 16-bit little-endian to host endian. */
337/** @def K_LE2H_U32
338 * Unsigned 32-bit little-endian to host endian. */
339/** @def K_LE2H_U64
340 * Unsigned 64-bit little-endian to host endian. */
341/** @def K_BE2H_U16
342 * Unsigned 16-bit big-endian to host endian. */
343/** @def K_BE2H_U32
344 * Unsigned 32-bit big-endian to host endian. */
345/** @def K_BE2H_U64
346 * Unsigned 64-bit big-endian to host endian. */
347#if K_ENDIAN == K_ENDIAN_LITTLE
348# define K_LE2H_U16(u16) ((KU16)(u16))
349# define K_LE2H_U32(u32) ((KU32)(u32))
350# define K_LE2H_U64(u64) ((KU64)(u32))
351# define K_BE2H_U16(u16) K_E2E_U16(u16)
352# define K_BE2H_U32(u32) K_E2E_U32(u32)
353# define K_BE2H_U64(u64) K_E2E_U64(u64)
354#else
355# define K_LE2H_U16(u16) K_E2E_U16(u16)
356# define K_LE2H_U32(u32) K_E2E_U32(u32)
357# define K_LE2H_U32(u64) K_E2E_U64(u64)
358# define K_BE2H_U16(u16) ((KU16)(u16))
359# define K_BE2H_U32(u32) ((KU32)(u32))
360# define K_BE2H_U64(u64) ((KU64)(u32))
361#endif
362
363
364
365/** @def K_INLINE
366 * How to say 'inline' in both C and C++ dialects.
367 * @param type The return type.
368 */
369#ifdef __cplusplus
370# if defined(__GNUC__)
371# define K_INLINE static inline
372# else
373# define K_INLINE inline
374# endif
375#else
376# if defined(__GNUC__)
377# define K_INLINE static __inline__
378# elif defined(_MSC_VER)
379# define K_INLINE static _Inline
380# else
381# error "Port Me"
382# endif
383#endif
384
385/** @def K_EXPORT
386 * What to put in front of an exported function.
387 */
388#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
389# define K_EXPORT __declspec(dllexport)
390#else
391# define K_EXPORT
392#endif
393
394/** @def K_IMPORT
395 * What to put in front of an imported function.
396 */
397#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
398# define K_IMPORT __declspec(dllimport)
399#else
400# define K_IMPORT extern
401#endif
402
403/** @def K_DECL_EXPORT
404 * Declare an exported function.
405 * @param type The return type.
406 */
407#define K_DECL_EXPORT(type) K_EXPORT type
408
409/** @def K_DECL_IMPORT
410 * Declare an import function.
411 * @param type The return type.
412 */
413#define K_DECL_IMPORT(type) K_IMPORT type
414
415/** @def K_DECL_INLINE
416 * Declare an inline function.
417 * @param type The return type.
418 * @remark Don't use on (class) methods.
419 */
420#define K_DECL_INLINE(type) K_INLINE type
421
422
423/** Get the minimum of two values. */
424#define K_MIN(a, b) ( (a) <= (b) ? (a) : (b) )
425/** Get the maximum of two values. */
426#define K_MAX(a, b) ( (a) >= (b) ? (a) : (b) )
427/** Calculate the offset of a structure member. */
428#define K_OFFSETOF(strct, memb) ( (KSIZE)( &((strct *)0)->memb ) )
429/** Align a size_t value. */
430#define K_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(KSIZE)((align) - 1) )
431/** Align a void * value. */
432#define K_ALIGN_P(pv, align) ( (void *)( ((KUPTR)(pv) + ((align) - 1)) & ~(KUPTR)((align) - 1) ) )
433/** Number of elements in an array. */
434#define K_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) )
435/** Checks if the specified pointer is a valid address or not. */
436#define K_VALID_PTR(ptr) ( (KUPTR)(ptr) + 0x1000U >= 0x2000U )
437/** Makes a 32-bit bit mask. */
438#define K_BIT32(bit) ( KU32_C(1) << (bit))
439/** Makes a 64-bit bit mask. */
440#define K_BIT64(bit) ( KU64_C(1) << (bit))
441/** Shuts up unused parameter and unused variable warnings. */
442#define K_NOREF(var) ( (void)(var) )
443
444
445/** @name Parameter validation macros
446 * @{ */
447
448/** Return/Crash validation of a string argument. */
449#define K_VALIDATE_STRING(str) \
450 do { \
451 if (!K_VALID_PTR(str)) \
452 return KERR_INVALID_POINTER; \
453 kHlpStrLen(str); \
454 } while (0)
455
456/** Return/Crash validation of an optional string argument. */
457#define K_VALIDATE_OPTIONAL_STRING(str) \
458 do { \
459 if (str) \
460 K_VALIDATE_STRING(str); \
461 } while (0)
462
463/** Return/Crash validation of an output buffer. */
464#define K_VALIDATE_BUFFER(buf, cb) \
465 do { \
466 if (!K_VALID_PTR(buf)) \
467 return KERR_INVALID_POINTER; \
468 if ((cb) != 0) \
469 { \
470 KU8 __b; \
471 KU8 volatile *__pb = (KU8 volatile *)(buf); \
472 KSIZE __cbPage1 = 0x1000 - ((KUPTR)(__pb) & 0xfff); /* ASSUMES page size! */ \
473 __b = *__pb; *__pb = 0xff; *__pb = __b; \
474 if ((cb) > __cbPage1) \
475 { \
476 KSIZE __cb = (cb) - __cbPage1; \
477 __pb -= __cbPage1; \
478 for (;;) \
479 { \
480 __b = *__pb; *__pb = 0xff; *__pb = __b; \
481 if (__cb < 0x1000) \
482 break; \
483 __pb += 0x1000; \
484 __cb -= 0x1000; \
485 } \
486 } \
487 } \
488 else \
489 return KERR_INVALID_PARAMETER; \
490 } while (0)
491
492/** Return/Crash validation of an optional output buffer. */
493#define K_VALIDATE_OPTIONAL_BUFFER(buf, cb) \
494 do { \
495 if ((buf) && (cb) != 0) \
496 K_VALIDATE_BUFFER(buf, cb); \
497 } while (0)
498
499/** Return validation of an enum argument. */
500#define K_VALIDATE_ENUM(arg, enumname) \
501 do { \
502 if ((arg) <= enumname##_INVALID || (arg) >= enumname##_END) \
503 return KERR_INVALID_PARAMETER; \
504 } while (0)
505
506/** Return validation of a flags argument. */
507#define K_VALIDATE_FLAGS(arg, AllowedMask) \
508 do { \
509 if ((arg) & ~(AllowedMask)) \
510 return KERR_INVALID_PARAMETER; \
511 } while (0)
512
513/** @} */
514
515/** @def NULL
516 * The nil pointer value. */
517#ifndef NULL
518# ifdef __cplusplus
519# define NULL 0
520# else
521# define NULL ((void *)0)
522# endif
523#endif
524
525/** @} */
526
527#endif
528
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