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

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

Helpers and macros.

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