source: trunk/include/k/kDefs.h@ 9

Last change on this file since 9 was 4, checked in by bird, 18 years ago

PARISC and S390 architecture defines with dection. Detect arm, mips and sparc. (Patch from Torsten w/ some modification.)

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