source: trunk/include/k/kDefs.h

Last change on this file was 123, checked in by bird, 10 months ago

kDefs.h: Detect arm64 on windows

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 20.6 KB
Line 
1/* $Id: kDefs.h 123 2024-11-02 23:41:24Z bird $ */
2/** @file
3 * kTypes - Defines and Macros.
4 */
5
6/*
7 * Copyright (c) 2006-2017 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31#ifndef ___k_kDefs_h___
32#define ___k_kDefs_h___
33
34/** @defgroup grp_kDefs kDefs - Defines and Macros
35 * @{ */
36
37/** @name Operative System Identifiers.
38 * These are the value that the K_OS \#define can take.
39 * @{
40 */
41/** Unknown OS. */
42#define K_OS_UNKNOWN 0
43/** Darwin - aka Mac OS X. */
44#define K_OS_DARWIN 1
45/** DragonFly BSD. */
46#define K_OS_DRAGONFLY 2
47/** FreeBSD. */
48#define K_OS_FREEBSD 3
49/** GNU/Hurd. */
50#define K_OS_GNU_HURD 4
51/** GNU/kFreeBSD. */
52#define K_OS_GNU_KFBSD 5
53/** GNU/kNetBSD or GNU/NetBSD or whatever the decide to call it. */
54#define K_OS_GNU_KNBSD 6
55/** Haiku. */
56#define K_OS_HAIKU 7
57/** Linux. */
58#define K_OS_LINUX 8
59/** NetBSD. */
60#define K_OS_NETBSD 9
61/** NT (native). */
62#define K_OS_NT 10
63/** OpenBSD*/
64#define K_OS_OPENBSD 11
65/** OS/2 */
66#define K_OS_OS2 12
67/** Solaris */
68#define K_OS_SOLARIS 13
69/** Windows. */
70#define K_OS_WINDOWS 14
71/** The max K_OS_* value (exclusive). */
72#define K_OS_MAX 15
73/** @} */
74
75/** @def K_OS
76 * Indicates which OS we're targetting. It's a \#define with is
77 * assigned one of the K_OS_* defines above.
78 *
79 * So to test if we're on FreeBSD do the following:
80 * @code
81 * #if K_OS == K_OS_FREEBSD
82 * some_funky_freebsd_specific_stuff();
83 * #endif
84 * @endcode
85 */
86#ifndef K_OS
87# if defined(__APPLE__)
88# define K_OS K_OS_DARWIN
89# elif defined(__DragonFly__)
90# define K_OS K_OS_DRAGONFLY
91# elif defined(__FreeBSD__)
92# define K_OS K_OS_FREEBSD
93# elif defined(__FreeBSD_kernel__)
94# define K_OS K_OS_GNU_KFBSD
95# elif defined(__gnu_hurd__)
96# define K_OS K_OS_GNU_HURD
97# elif defined(__gnu_linux__)
98# define K_OS K_OS_LINUX
99# elif defined(__NetBSD__) /*??*/
100# define K_OS K_OS_NETBSD
101# elif defined(__NetBSD_kernel__)
102# define K_OS K_OS_GNU_KNBSD
103# elif defined(__OpenBSD__) /*??*/
104# define K_OS K_OS_OPENBSD
105# elif defined(__OS2__)
106# define K_OS K_OS_OS2
107# elif defined(__sun__) || defined(__SunOS__) || defined(__sun) || defined(__SunOS)
108# define K_OS K_OS_SOLARIS
109# elif defined(_WIN32) || defined(_WIN64)
110# define K_OS K_OS_WINDOWS
111# elif defined(__haiku__) || defined(__HAIKU__)
112# define K_OS K_OS_HAIKU
113# else
114# error "Port Me"
115# endif
116#endif
117#if K_OS < K_OS_UNKNOWN || K_OS >= K_OS_MAX
118# error "Invalid K_OS value."
119#endif
120
121
122
123/** @name Architecture bit width.
124 * @{ */
125#define K_ARCH_BIT_8 0x0100 /**< 8-bit */
126#define K_ARCH_BIT_16 0x0200 /**< 16-bit */
127#define K_ARCH_BIT_32 0x0400 /**< 32-bit */
128#define K_ARCH_BIT_64 0x0800 /**< 64-bit */
129#define K_ARCH_BIT_128 0x1000 /**< 128-bit */
130#define K_ARCH_BIT_MASK 0x1f00 /**< The bit mask. */
131#define K_ARCH_BIT_SHIFT 5 /**< Shift count for producing the width in bits. */
132#define K_ARCH_BYTE_SHIFT 8 /**< Shift count for producing the width in bytes. */
133/** @} */
134
135/** @name Architecture Endianness.
136 * @{ */
137#define K_ARCH_END_LITTLE 0x2000 /**< Little-endian. */
138#define K_ARCH_END_BIG 0x4000 /**< Big-endian. */
139#define K_ARCH_END_BI 0x6000 /**< Bi-endian, can be switched. */
140#define K_ARCH_END_MASK 0x6000 /**< The endian mask. */
141#define K_ARCH_END_SHIFT 13 /**< Shift count for converting between this K_ENDIAN_*. */
142/** @} */
143
144/** @name Architecture Identifiers.
145 * These are the value that the K_ARCH \#define can take.
146 *@{ */
147/** Unknown CPU architecture. */
148#define K_ARCH_UNKNOWN ( 0 )
149/** Clone or Intel 16-bit x86. */
150#define K_ARCH_X86_16 ( 1 | K_ARCH_BIT_16 | K_ARCH_END_LITTLE)
151/** Clone or Intel 32-bit x86. */
152#define K_ARCH_X86_32 ( 1 | K_ARCH_BIT_32 | K_ARCH_END_LITTLE)
153/** AMD64 (including clones). */
154#define K_ARCH_AMD64 ( 2 | K_ARCH_BIT_64 | K_ARCH_END_LITTLE)
155/** Itanic (64-bit). */
156#define K_ARCH_IA64 ( 3 | K_ARCH_BIT_64 | K_ARCH_END_BI)
157/** ALPHA (64-bit). */
158#define K_ARCH_ALPHA ( 4 | K_ARCH_BIT_64 | K_ARCH_END_BI)
159/** ALPHA limited to 32-bit. */
160#define K_ARCH_ALPHA_32 ( 4 | K_ARCH_BIT_32 | K_ARCH_END_BI)
161/** 32-bit ARM. */
162#define K_ARCH_ARM_32 ( 5 | K_ARCH_BIT_32 | K_ARCH_END_BI)
163/** 64-bit ARM. */
164#define K_ARCH_ARM_64 ( 5 | K_ARCH_BIT_64 | K_ARCH_END_BI)
165/** Motorola 68000 (32-bit). */
166#define K_ARCH_M68K ( 6 | K_ARCH_BIT_32 | K_ARCH_END_BIG)
167/** 32-bit MIPS. */
168#define K_ARCH_MIPS_32 ( 7 | K_ARCH_BIT_32 | K_ARCH_END_BI)
169/** 64-bit MIPS. */
170#define K_ARCH_MIPS_64 ( 7 | K_ARCH_BIT_64 | K_ARCH_END_BI)
171/** 32-bit PA-RISC. */
172#define K_ARCH_PARISC_32 ( 8 | K_ARCH_BIT_32 | K_ARCH_END_BI)
173/** 64-bit PA-RISC. */
174#define K_ARCH_PARISC_64 ( 8 | K_ARCH_BIT_64 | K_ARCH_END_BI)
175/** 32-bit PowerPC. */
176#define K_ARCH_POWERPC_32 ( 9 | K_ARCH_BIT_32 | K_ARCH_END_BI)
177/** 64-bit PowerPC. */
178#define K_ARCH_POWERPC_64 ( 9 | K_ARCH_BIT_64 | K_ARCH_END_BI)
179/** 32(31)-bit S390. */
180#define K_ARCH_S390_32 (10 | K_ARCH_BIT_32 | K_ARCH_END_BIG)
181/** 64-bit S390. */
182#define K_ARCH_S390_64 (10 | K_ARCH_BIT_64 | K_ARCH_END_BIG)
183/** 32-bit SuperH. */
184#define K_ARCH_SH_32 (11 | K_ARCH_BIT_32 | K_ARCH_END_BI)
185/** 64-bit SuperH. */
186#define K_ARCH_SH_64 (11 | K_ARCH_BIT_64 | K_ARCH_END_BI)
187/** 32-bit SPARC. */
188#define K_ARCH_SPARC_32 (12 | K_ARCH_BIT_32 | K_ARCH_END_BIG)
189/** 64-bit SPARC. */
190#define K_ARCH_SPARC_64 (12 | K_ARCH_BIT_64 | K_ARCH_END_BI)
191/** 32-bit RISC-V, little endian. */
192#define K_ARCH_RISCV_32 (13 | K_ARCH_BIT_32 | K_ARCH_END_LITTLE)
193/** 32-bit RISC-V, big endian. */
194#define K_ARCH_RISCV_32_BE (13 | K_ARCH_BIT_32 | K_ARCH_END_BIG)
195/** 64-bit RISC-V, little endian. */
196#define K_ARCH_RISCV_64 (13 | K_ARCH_BIT_64 | K_ARCH_END_LITTLE)
197/** 64-bit RISC-V, big endian. */
198#define K_ARCH_RISCV_64_BE (13 | K_ARCH_BIT_64 | K_ARCH_END_BIG)
199/** 64-bit LoongArch. */
200#define K_ARCH_LOONGARCH_64 (14 | K_ARCH_BIT_64 | K_ARCH_END_LITTLE)
201/** The end of the valid architecture values (exclusive). */
202#define K_ARCH_MAX (14+1)
203/** @} */
204
205
206/** @def K_ARCH
207 * The value of this \#define indicates which architecture we're targetting.
208 */
209#ifndef K_ARCH
210 /* detection based on compiler defines. */
211# if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || defined(_M_X64) || defined(__amd64)
212# define K_ARCH K_ARCH_AMD64
213# elif defined(__i386__) || defined(__x86__) || defined(__X86__) || defined(_M_IX86) || defined(__i386)
214# define K_ARCH K_ARCH_X86_32
215# elif defined(__ia64__) || defined(__IA64__) || defined(_M_IA64)
216# define K_ARCH K_ARCH_IA64
217# elif defined(__alpha__)
218# define K_ARCH K_ARCH_ALPHA
219# elif defined(__arm__) || defined(__arm32__) || defined(_M_ARM)
220# define K_ARCH K_ARCH_ARM_32
221# elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
222# define K_ARCH K_ARCH_ARM_64
223# elif defined(__hppa__) && defined(__LP64__)
224# define K_ARCH K_ARCH_PARISC_64
225# elif defined(__hppa__)
226# define K_ARCH K_ARCH_PARISC_32
227# elif defined(__m68k__)
228# define K_ARCH K_ARCH_M68K
229# elif defined(__loongarch64)
230# define K_ARCH K_ARCH_LOONGARCH_64
231# elif defined(__mips64)
232# define K_ARCH K_ARCH_MIPS_64
233# elif defined(__mips__)
234# define K_ARCH K_ARCH_MIPS_32
235# elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__)
236# define K_ARCH K_ARCH_POWERPC_64
237# elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)
238# define K_ARCH K_ARCH_POWERPC_32
239# elif defined(__riscv)
240# if __BYTE_ORDER__ == WORDS_BIG_ENDIAN
241# if defined(__riscv32) || __riscv_xlen+0 == 32
242# define K_ARCH K_ARCH_RISCV_32_BE
243# else
244# define K_ARCH K_ARCH_RISCV_64_BE
245# endif
246# elif defined(__riscv32) || __riscv_xlen+0 == 32
247# define K_ARCH K_ARCH_RISCV_32
248# else
249# define K_ARCH K_ARCH_RISCV_64
250# endif
251# elif defined(__sparcv9__) || defined(__sparcv9)
252# define K_ARCH K_ARCH_SPARC_64
253# elif defined(__sparc__) || defined(__sparc)
254# define K_ARCH K_ARCH_SPARC_32
255# elif defined(__s390x__)
256# define K_ARCH K_ARCH_S390_64
257# elif defined(__s390__)
258# define K_ARCH K_ARCH_S390_32
259# elif defined(__sh__)
260# if !defined(__SH5__)
261# define K_ARCH K_ARCH_SH_32
262# else
263# if __SH5__ == 64
264# define K_ARCH K_ARCH_SH_64
265# else
266# define K_ARCH K_ARCH_SH_32
267# endif
268# endif
269# else
270# error "Port Me"
271# endif
272#else
273 /* validate the user specified value. */
274# if (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_8 \
275 && (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_16 \
276 && (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_32 \
277 && (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_64 \
278 && (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_128
279# error "Invalid K_ARCH value (bit)"
280# endif
281# if (K_ARCH & K_ARCH_END_MASK) != K_ARCH_END_LITTLE \
282 && (K_ARCH & K_ARCH_END_MASK) != K_ARCH_END_BIG \
283 && (K_ARCH & K_ARCH_END_MASK) != K_ARCH_END_BI
284# error "Invalid K_ARCH value (endian)"
285# endif
286# if (K_ARCH & ~(K_ARCH_BIT_MASK | K_ARCH_BIT_END_MASK)) < K_ARCH_UNKNOWN \
287 || (K_ARCH & ~(K_ARCH_BIT_MASK | K_ARCH_BIT_END_MASK)) >= K_ARCH_MAX
288# error "Invalid K_ARCH value"
289# endif
290#endif
291
292/** @def K_ARCH_IS_VALID
293 * Check if the architecture identifier is valid.
294 * @param arch The K_ARCH_* define to examin.
295 */
296#define K_ARCH_IS_VALID(arch) ( ( ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_8 \
297 || ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_16 \
298 || ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_32 \
299 || ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_64 \
300 || ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_128) \
301 && \
302 ( ((arch) & K_ARCH_END_MASK) == K_ARCH_END_LITTLE \
303 || ((arch) & K_ARCH_END_MASK) == K_ARCH_END_BIG \
304 || ((arch) & K_ARCH_END_MASK) == K_ARCH_END_BI) \
305 && \
306 ( ((arch) & ~(K_ARCH_BIT_MASK | K_ARCH_END_MASK)) >= K_ARCH_UNKNOWN \
307 && ((arch) & ~(K_ARCH_BIT_MASK | K_ARCH_END_MASK)) < K_ARCH_MAX) \
308 )
309
310/** @def K_ARCH_BITS_EX
311 * Determin the architure byte width of the specified architecture.
312 * @param arch The K_ARCH_* define to examin.
313 */
314#define K_ARCH_BITS_EX(arch) ( ((arch) & K_ARCH_BIT_MASK) >> K_ARCH_BIT_SHIFT )
315
316/** @def K_ARCH_BYTES_EX
317 * Determin the architure byte width of the specified architecture.
318 * @param arch The K_ARCH_* define to examin.
319 */
320#define K_ARCH_BYTES_EX(arch) ( ((arch) & K_ARCH_BIT_MASK) >> K_ARCH_BYTE_SHIFT )
321
322/** @def K_ARCH_ENDIAN_EX
323 * Determin the K_ENDIAN value for the specified architecture.
324 * @param arch The K_ARCH_* define to examin.
325 */
326#define K_ARCH_ENDIAN_EX(arch) ( ((arch) & K_ARCH_END_MASK) >> K_ARCH_END_SHIFT )
327
328/** @def K_ARCH_BITS
329 * Determin the target architure bit width.
330 */
331#define K_ARCH_BITS K_ARCH_BITS_EX(K_ARCH)
332
333/** @def K_ARCH_BYTES
334 * Determin the target architure byte width.
335 */
336#define K_ARCH_BYTES K_ARCH_BYTES_EX(K_ARCH)
337
338/** @def K_ARCH_ENDIAN
339 * Determin the target K_ENDIAN value.
340 */
341#define K_ARCH_ENDIAN K_ARCH_ENDIAN_EX(K_ARCH)
342
343
344
345/** @name Endianness Identifiers.
346 * These are the value that the K_ENDIAN \#define can take.
347 * @{ */
348#define K_ENDIAN_LITTLE 1 /**< Little-endian. */
349#define K_ENDIAN_BIG 2 /**< Big-endian. */
350#define K_ENDIAN_BI 3 /**< Bi-endian, can be switched. Only used with K_ARCH. */
351/** @} */
352
353/** @def K_ENDIAN
354 * The value of this \#define indicates the target endianness.
355 *
356 * @remark It's necessary to define this (or add the necessary deduction here)
357 * on bi-endian architectures.
358 */
359#ifndef K_ENDIAN
360 /* use K_ARCH if possible. */
361# if K_ARCH_ENDIAN != K_ENDIAN_BI
362# define K_ENDIAN K_ARCH_ENDIAN
363# elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__)
364# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
365# define K_ENDIAN K_ENDIAN_LITTLE
366# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
367# define K_ENDIAN K_ENDIAN_BIG
368# else
369# error "Port Me or define K_ENDIAN."
370# endif
371# elif defined(_MSC_VER) /* fix when encounter situation where this isn't the case... */
372# define K_ENDIAN K_ENDIAN_LITTLE
373# else
374# error "Port Me or define K_ENDIAN."
375# endif
376#else
377 /* validate the user defined value. */
378# if K_ENDIAN != K_ENDIAN_LITTLE \
379 && K_ENDIAN != K_ENDIAN_BIG
380# error "K_ENDIAN must either be defined as K_ENDIAN_LITTLE or as K_ENDIAN_BIG."
381# endif
382#endif
383
384/** @name Endian Conversion
385 * @{ */
386
387/** @def K_E2E_U16
388 * Convert the endian of an unsigned 16-bit value. */
389# define K_E2E_U16(u16) ( (KU16) (((u16) >> 8) | ((u16) << 8)) )
390/** @def K_E2E_U32
391 * Convert the endian of an unsigned 32-bit value. */
392# define K_E2E_U32(u32) ( ( ((u32) & KU32_C(0xff000000)) >> 24 ) \
393 | ( ((u32) & KU32_C(0x00ff0000)) >> 8 ) \
394 | ( ((u32) & KU32_C(0x0000ff00)) << 8 ) \
395 | ( ((u32) & KU32_C(0x000000ff)) << 24 ) \
396 )
397/** @def K_E2E_U64
398 * Convert the endian of an unsigned 64-bit value. */
399# define K_E2E_U64(u64) ( ( ((u64) & KU64_C(0xff00000000000000)) >> 56 ) \
400 | ( ((u64) & KU64_C(0x00ff000000000000)) >> 40 ) \
401 | ( ((u64) & KU64_C(0x0000ff0000000000)) >> 24 ) \
402 | ( ((u64) & KU64_C(0x000000ff00000000)) >> 8 ) \
403 | ( ((u64) & KU64_C(0x00000000ff000000)) << 8 ) \
404 | ( ((u64) & KU64_C(0x0000000000ff0000)) << 24 ) \
405 | ( ((u64) & KU64_C(0x000000000000ff00)) << 40 ) \
406 | ( ((u64) & KU64_C(0x00000000000000ff)) << 56 ) \
407 )
408
409/** @def K_LE2H_U16
410 * Unsigned 16-bit little-endian to host endian. */
411/** @def K_LE2H_U32
412 * Unsigned 32-bit little-endian to host endian. */
413/** @def K_LE2H_U64
414 * Unsigned 64-bit little-endian to host endian. */
415/** @def K_BE2H_U16
416 * Unsigned 16-bit big-endian to host endian. */
417/** @def K_BE2H_U32
418 * Unsigned 32-bit big-endian to host endian. */
419/** @def K_BE2H_U64
420 * Unsigned 64-bit big-endian to host endian. */
421#if K_ENDIAN == K_ENDIAN_LITTLE
422# define K_LE2H_U16(u16) ((KU16)(u16))
423# define K_LE2H_U32(u32) ((KU32)(u32))
424# define K_LE2H_U64(u64) ((KU64)(u32))
425# define K_BE2H_U16(u16) K_E2E_U16(u16)
426# define K_BE2H_U32(u32) K_E2E_U32(u32)
427# define K_BE2H_U64(u64) K_E2E_U64(u64)
428#else
429# define K_LE2H_U16(u16) K_E2E_U16(u16)
430# define K_LE2H_U32(u32) K_E2E_U32(u32)
431# define K_LE2H_U64(u64) K_E2E_U64(u64)
432# define K_BE2H_U16(u16) ((KU16)(u16))
433# define K_BE2H_U32(u32) ((KU32)(u32))
434# define K_BE2H_U64(u64) ((KU64)(u32))
435#endif
436
437
438
439/** @def K_INLINE
440 * How to say 'inline' in both C and C++ dialects.
441 * @param type The return type.
442 */
443#ifdef __cplusplus
444# if defined(__GNUC__)
445# define K_INLINE static inline
446# else
447# define K_INLINE inline
448# endif
449#else
450# if defined(__GNUC__)
451# define K_INLINE static __inline__
452# elif defined(_MSC_VER)
453# define K_INLINE static __inline
454# else
455# error "Port Me"
456# endif
457#endif
458
459/** @def K_EXPORT
460 * What to put in front of an exported function.
461 */
462#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
463# define K_EXPORT __declspec(dllexport)
464#else
465# define K_EXPORT
466#endif
467
468/** @def K_IMPORT
469 * What to put in front of an imported function.
470 */
471#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
472# define K_IMPORT __declspec(dllimport)
473#else
474# define K_IMPORT extern
475#endif
476
477/** @def K_DECL_EXPORT
478 * Declare an exported function.
479 * @param type The return type.
480 */
481#define K_DECL_EXPORT(type) K_EXPORT type
482
483/** @def K_DECL_IMPORT
484 * Declare an import function.
485 * @param type The return type.
486 */
487#define K_DECL_IMPORT(type) K_IMPORT type
488
489/** @def K_DECL_INLINE
490 * Declare an inline function.
491 * @param type The return type.
492 * @remark Don't use on (class) methods.
493 */
494#define K_DECL_INLINE(type) K_INLINE type
495
496
497/** Get the minimum of two values. */
498#define K_MIN(a, b) ( (a) <= (b) ? (a) : (b) )
499/** Get the maximum of two values. */
500#define K_MAX(a, b) ( (a) >= (b) ? (a) : (b) )
501/** Calculate the offset of a structure member. */
502#define K_OFFSETOF(strct, memb) ( (KSIZE)( &((strct *)0)->memb ) )
503/** Align a size_t value. */
504#define K_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(KSIZE)((align) - 1) )
505/** Align a void * value. */
506#define K_ALIGN_P(pv, align) ( (void *)( ((KUPTR)(pv) + ((align) - 1)) & ~(KUPTR)((align) - 1) ) )
507/** Number of elements in an array. */
508#define K_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) )
509/** Checks if the specified pointer is a valid address or not. */
510#define K_VALID_PTR(ptr) ( (KUPTR)(ptr) + 0x1000U >= 0x2000U )
511/** Makes a 32-bit bit mask. */
512#define K_BIT32(bit) ( KU32_C(1) << (bit))
513/** Makes a 64-bit bit mask. */
514#define K_BIT64(bit) ( KU64_C(1) << (bit))
515/** Shuts up unused parameter and unused variable warnings. */
516#define K_NOREF(var) ( (void)(var) )
517
518
519/** @name Parameter validation macros
520 * @{ */
521
522/** Return/Crash validation of a string argument. */
523#define K_VALIDATE_STRING(str) \
524 do { \
525 if (!K_VALID_PTR(str)) \
526 return KERR_INVALID_POINTER; \
527 kHlpStrLen(str); \
528 } while (0)
529
530/** Return/Crash validation of an optional string argument. */
531#define K_VALIDATE_OPTIONAL_STRING(str) \
532 do { \
533 if (str) \
534 K_VALIDATE_STRING(str); \
535 } while (0)
536
537/** Return/Crash validation of an output buffer. */
538#define K_VALIDATE_BUFFER(buf, cb) \
539 do { \
540 if (!K_VALID_PTR(buf)) \
541 return KERR_INVALID_POINTER; \
542 if ((cb) != 0) \
543 { \
544 KU8 __b; \
545 KU8 volatile *__pb = (KU8 volatile *)(buf); \
546 KSIZE __cbPage1 = 0x1000 - ((KUPTR)(__pb) & 0xfff); /* ASSUMES page size! */ \
547 __b = *__pb; *__pb = 0xff; *__pb = __b; \
548 if ((cb) > __cbPage1) \
549 { \
550 KSIZE __cb = (cb) - __cbPage1; \
551 __pb -= __cbPage1; \
552 for (;;) \
553 { \
554 __b = *__pb; *__pb = 0xff; *__pb = __b; \
555 if (__cb < 0x1000) \
556 break; \
557 __pb += 0x1000; \
558 __cb -= 0x1000; \
559 } \
560 } \
561 } \
562 else \
563 return KERR_INVALID_PARAMETER; \
564 } while (0)
565
566/** Return/Crash validation of an optional output buffer. */
567#define K_VALIDATE_OPTIONAL_BUFFER(buf, cb) \
568 do { \
569 if ((buf) && (cb) != 0) \
570 K_VALIDATE_BUFFER(buf, cb); \
571 } while (0)
572
573/** Return validation of an enum argument. */
574#define K_VALIDATE_ENUM(arg, enumname) \
575 do { \
576 if ((arg) <= enumname##_INVALID || (arg) >= enumname##_END) \
577 return KERR_INVALID_PARAMETER; \
578 } while (0)
579
580/** Return validation of a flags argument. */
581#define K_VALIDATE_FLAGS(arg, AllowedMask) \
582 do { \
583 if ((arg) & ~(AllowedMask)) \
584 return KERR_INVALID_PARAMETER; \
585 } while (0)
586
587/** @} */
588
589/** @def NULL
590 * The nil pointer value. */
591#ifndef NULL
592# ifdef __cplusplus
593# define NULL 0
594# else
595# define NULL ((void *)0)
596# endif
597#endif
598
599/** @} */
600
601#endif
602
Note: See TracBrowser for help on using the repository browser.