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

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

K_OS_NT

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