source: trunk/kStuff/include/k/kDefs.h@ 3585

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

Cpu / architecture cleanup...

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