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

Last change on this file since 122 was 122, checked in by bird, 11 months ago

kDefs: Fixed LoongArch typo

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 20.5 KB
Line 
1/* $Id: kDefs.h 122 2024-09-30 09:50:11Z 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__)
220# define K_ARCH K_ARCH_ARM_32
221# elif defined(__aarch64__) || defined(__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# else
372# error "Port Me or define K_ENDIAN."
373# endif
374#else
375 /* validate the user defined value. */
376# if K_ENDIAN != K_ENDIAN_LITTLE \
377 && K_ENDIAN != K_ENDIAN_BIG
378# error "K_ENDIAN must either be defined as K_ENDIAN_LITTLE or as K_ENDIAN_BIG."
379# endif
380#endif
381
382/** @name Endian Conversion
383 * @{ */
384
385/** @def K_E2E_U16
386 * Convert the endian of an unsigned 16-bit value. */
387# define K_E2E_U16(u16) ( (KU16) (((u16) >> 8) | ((u16) << 8)) )
388/** @def K_E2E_U32
389 * Convert the endian of an unsigned 32-bit value. */
390# define K_E2E_U32(u32) ( ( ((u32) & KU32_C(0xff000000)) >> 24 ) \
391 | ( ((u32) & KU32_C(0x00ff0000)) >> 8 ) \
392 | ( ((u32) & KU32_C(0x0000ff00)) << 8 ) \
393 | ( ((u32) & KU32_C(0x000000ff)) << 24 ) \
394 )
395/** @def K_E2E_U64
396 * Convert the endian of an unsigned 64-bit value. */
397# define K_E2E_U64(u64) ( ( ((u64) & KU64_C(0xff00000000000000)) >> 56 ) \
398 | ( ((u64) & KU64_C(0x00ff000000000000)) >> 40 ) \
399 | ( ((u64) & KU64_C(0x0000ff0000000000)) >> 24 ) \
400 | ( ((u64) & KU64_C(0x000000ff00000000)) >> 8 ) \
401 | ( ((u64) & KU64_C(0x00000000ff000000)) << 8 ) \
402 | ( ((u64) & KU64_C(0x0000000000ff0000)) << 24 ) \
403 | ( ((u64) & KU64_C(0x000000000000ff00)) << 40 ) \
404 | ( ((u64) & KU64_C(0x00000000000000ff)) << 56 ) \
405 )
406
407/** @def K_LE2H_U16
408 * Unsigned 16-bit little-endian to host endian. */
409/** @def K_LE2H_U32
410 * Unsigned 32-bit little-endian to host endian. */
411/** @def K_LE2H_U64
412 * Unsigned 64-bit little-endian to host endian. */
413/** @def K_BE2H_U16
414 * Unsigned 16-bit big-endian to host endian. */
415/** @def K_BE2H_U32
416 * Unsigned 32-bit big-endian to host endian. */
417/** @def K_BE2H_U64
418 * Unsigned 64-bit big-endian to host endian. */
419#if K_ENDIAN == K_ENDIAN_LITTLE
420# define K_LE2H_U16(u16) ((KU16)(u16))
421# define K_LE2H_U32(u32) ((KU32)(u32))
422# define K_LE2H_U64(u64) ((KU64)(u32))
423# define K_BE2H_U16(u16) K_E2E_U16(u16)
424# define K_BE2H_U32(u32) K_E2E_U32(u32)
425# define K_BE2H_U64(u64) K_E2E_U64(u64)
426#else
427# define K_LE2H_U16(u16) K_E2E_U16(u16)
428# define K_LE2H_U32(u32) K_E2E_U32(u32)
429# define K_LE2H_U64(u64) K_E2E_U64(u64)
430# define K_BE2H_U16(u16) ((KU16)(u16))
431# define K_BE2H_U32(u32) ((KU32)(u32))
432# define K_BE2H_U64(u64) ((KU64)(u32))
433#endif
434
435
436
437/** @def K_INLINE
438 * How to say 'inline' in both C and C++ dialects.
439 * @param type The return type.
440 */
441#ifdef __cplusplus
442# if defined(__GNUC__)
443# define K_INLINE static inline
444# else
445# define K_INLINE inline
446# endif
447#else
448# if defined(__GNUC__)
449# define K_INLINE static __inline__
450# elif defined(_MSC_VER)
451# define K_INLINE static __inline
452# else
453# error "Port Me"
454# endif
455#endif
456
457/** @def K_EXPORT
458 * What to put in front of an exported function.
459 */
460#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
461# define K_EXPORT __declspec(dllexport)
462#else
463# define K_EXPORT
464#endif
465
466/** @def K_IMPORT
467 * What to put in front of an imported function.
468 */
469#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
470# define K_IMPORT __declspec(dllimport)
471#else
472# define K_IMPORT extern
473#endif
474
475/** @def K_DECL_EXPORT
476 * Declare an exported function.
477 * @param type The return type.
478 */
479#define K_DECL_EXPORT(type) K_EXPORT type
480
481/** @def K_DECL_IMPORT
482 * Declare an import function.
483 * @param type The return type.
484 */
485#define K_DECL_IMPORT(type) K_IMPORT type
486
487/** @def K_DECL_INLINE
488 * Declare an inline function.
489 * @param type The return type.
490 * @remark Don't use on (class) methods.
491 */
492#define K_DECL_INLINE(type) K_INLINE type
493
494
495/** Get the minimum of two values. */
496#define K_MIN(a, b) ( (a) <= (b) ? (a) : (b) )
497/** Get the maximum of two values. */
498#define K_MAX(a, b) ( (a) >= (b) ? (a) : (b) )
499/** Calculate the offset of a structure member. */
500#define K_OFFSETOF(strct, memb) ( (KSIZE)( &((strct *)0)->memb ) )
501/** Align a size_t value. */
502#define K_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(KSIZE)((align) - 1) )
503/** Align a void * value. */
504#define K_ALIGN_P(pv, align) ( (void *)( ((KUPTR)(pv) + ((align) - 1)) & ~(KUPTR)((align) - 1) ) )
505/** Number of elements in an array. */
506#define K_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) )
507/** Checks if the specified pointer is a valid address or not. */
508#define K_VALID_PTR(ptr) ( (KUPTR)(ptr) + 0x1000U >= 0x2000U )
509/** Makes a 32-bit bit mask. */
510#define K_BIT32(bit) ( KU32_C(1) << (bit))
511/** Makes a 64-bit bit mask. */
512#define K_BIT64(bit) ( KU64_C(1) << (bit))
513/** Shuts up unused parameter and unused variable warnings. */
514#define K_NOREF(var) ( (void)(var) )
515
516
517/** @name Parameter validation macros
518 * @{ */
519
520/** Return/Crash validation of a string argument. */
521#define K_VALIDATE_STRING(str) \
522 do { \
523 if (!K_VALID_PTR(str)) \
524 return KERR_INVALID_POINTER; \
525 kHlpStrLen(str); \
526 } while (0)
527
528/** Return/Crash validation of an optional string argument. */
529#define K_VALIDATE_OPTIONAL_STRING(str) \
530 do { \
531 if (str) \
532 K_VALIDATE_STRING(str); \
533 } while (0)
534
535/** Return/Crash validation of an output buffer. */
536#define K_VALIDATE_BUFFER(buf, cb) \
537 do { \
538 if (!K_VALID_PTR(buf)) \
539 return KERR_INVALID_POINTER; \
540 if ((cb) != 0) \
541 { \
542 KU8 __b; \
543 KU8 volatile *__pb = (KU8 volatile *)(buf); \
544 KSIZE __cbPage1 = 0x1000 - ((KUPTR)(__pb) & 0xfff); /* ASSUMES page size! */ \
545 __b = *__pb; *__pb = 0xff; *__pb = __b; \
546 if ((cb) > __cbPage1) \
547 { \
548 KSIZE __cb = (cb) - __cbPage1; \
549 __pb -= __cbPage1; \
550 for (;;) \
551 { \
552 __b = *__pb; *__pb = 0xff; *__pb = __b; \
553 if (__cb < 0x1000) \
554 break; \
555 __pb += 0x1000; \
556 __cb -= 0x1000; \
557 } \
558 } \
559 } \
560 else \
561 return KERR_INVALID_PARAMETER; \
562 } while (0)
563
564/** Return/Crash validation of an optional output buffer. */
565#define K_VALIDATE_OPTIONAL_BUFFER(buf, cb) \
566 do { \
567 if ((buf) && (cb) != 0) \
568 K_VALIDATE_BUFFER(buf, cb); \
569 } while (0)
570
571/** Return validation of an enum argument. */
572#define K_VALIDATE_ENUM(arg, enumname) \
573 do { \
574 if ((arg) <= enumname##_INVALID || (arg) >= enumname##_END) \
575 return KERR_INVALID_PARAMETER; \
576 } while (0)
577
578/** Return validation of a flags argument. */
579#define K_VALIDATE_FLAGS(arg, AllowedMask) \
580 do { \
581 if ((arg) & ~(AllowedMask)) \
582 return KERR_INVALID_PARAMETER; \
583 } while (0)
584
585/** @} */
586
587/** @def NULL
588 * The nil pointer value. */
589#ifndef NULL
590# ifdef __cplusplus
591# define NULL 0
592# else
593# define NULL ((void *)0)
594# endif
595#endif
596
597/** @} */
598
599#endif
600
Note: See TracBrowser for help on using the repository browser.