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

Last change on this file since 120 was 120, checked in by bird, 4 years ago

kDefs.h: Added risc-v to the K_ARCH_XXX defines.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 20.3 KB
Line 
1/* $Id: kDefs.h 120 2022-02-18 02:00:53Z 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/** The end of the valid architecture values (exclusive). */
200#define K_ARCH_MAX (12+1)
201/** @} */
202
203
204/** @def K_ARCH
205 * The value of this \#define indicates which architecture we're targetting.
206 */
207#ifndef K_ARCH
208 /* detection based on compiler defines. */
209# if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || defined(_M_X64) || defined(__amd64)
210# define K_ARCH K_ARCH_AMD64
211# elif defined(__i386__) || defined(__x86__) || defined(__X86__) || defined(_M_IX86) || defined(__i386)
212# define K_ARCH K_ARCH_X86_32
213# elif defined(__ia64__) || defined(__IA64__) || defined(_M_IA64)
214# define K_ARCH K_ARCH_IA64
215# elif defined(__alpha__)
216# define K_ARCH K_ARCH_ALPHA
217# elif defined(__arm__) || defined(__arm32__)
218# define K_ARCH K_ARCH_ARM_32
219# elif defined(__aarch64__) || defined(__arm64__)
220# define K_ARCH K_ARCH_ARM_64
221# elif defined(__hppa__) && defined(__LP64__)
222# define K_ARCH K_ARCH_PARISC_64
223# elif defined(__hppa__)
224# define K_ARCH K_ARCH_PARISC_32
225# elif defined(__m68k__)
226# define K_ARCH K_ARCH_M68K
227# elif defined(__mips64)
228# define K_ARCH K_ARCH_MIPS_64
229# elif defined(__mips__)
230# define K_ARCH K_ARCH_MIPS_32
231# elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__)
232# define K_ARCH K_ARCH_POWERPC_64
233# elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)
234# define K_ARCH K_ARCH_POWERPC_32
235# elif defined(__riscv)
236# if __BYTE_ORDER__ == WORDS_BIG_ENDIAN
237# if defined(__riscv32) || __riscv_xlen+0 == 32
238# define K_ARCH K_ARCH_RISCV_32_BE
239# else
240# define K_ARCH K_ARCH_RISCV_64_BE
241# endif
242# elif defined(__riscv32) || __riscv_xlen+0 == 32
243# define K_ARCH K_ARCH_RISCV_32
244# else
245# define K_ARCH K_ARCH_RISCV_64
246# endif
247# elif defined(__sparcv9__) || defined(__sparcv9)
248# define K_ARCH K_ARCH_SPARC_64
249# elif defined(__sparc__) || defined(__sparc)
250# define K_ARCH K_ARCH_SPARC_32
251# elif defined(__s390x__)
252# define K_ARCH K_ARCH_S390_64
253# elif defined(__s390__)
254# define K_ARCH K_ARCH_S390_32
255# elif defined(__sh__)
256# if !defined(__SH5__)
257# define K_ARCH K_ARCH_SH_32
258# else
259# if __SH5__ == 64
260# define K_ARCH K_ARCH_SH_64
261# else
262# define K_ARCH K_ARCH_SH_32
263# endif
264# endif
265# else
266# error "Port Me"
267# endif
268#else
269 /* validate the user specified value. */
270# if (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_8 \
271 && (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_16 \
272 && (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_32 \
273 && (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_64 \
274 && (K_ARCH & K_ARCH_BIT_MASK) != K_ARCH_BIT_128
275# error "Invalid K_ARCH value (bit)"
276# endif
277# if (K_ARCH & K_ARCH_END_MASK) != K_ARCH_END_LITTLE \
278 && (K_ARCH & K_ARCH_END_MASK) != K_ARCH_END_BIG \
279 && (K_ARCH & K_ARCH_END_MASK) != K_ARCH_END_BI
280# error "Invalid K_ARCH value (endian)"
281# endif
282# if (K_ARCH & ~(K_ARCH_BIT_MASK | K_ARCH_BIT_END_MASK)) < K_ARCH_UNKNOWN \
283 || (K_ARCH & ~(K_ARCH_BIT_MASK | K_ARCH_BIT_END_MASK)) >= K_ARCH_MAX
284# error "Invalid K_ARCH value"
285# endif
286#endif
287
288/** @def K_ARCH_IS_VALID
289 * Check if the architecture identifier is valid.
290 * @param arch The K_ARCH_* define to examin.
291 */
292#define K_ARCH_IS_VALID(arch) ( ( ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_8 \
293 || ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_16 \
294 || ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_32 \
295 || ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_64 \
296 || ((arch) & K_ARCH_BIT_MASK) == K_ARCH_BIT_128) \
297 && \
298 ( ((arch) & K_ARCH_END_MASK) == K_ARCH_END_LITTLE \
299 || ((arch) & K_ARCH_END_MASK) == K_ARCH_END_BIG \
300 || ((arch) & K_ARCH_END_MASK) == K_ARCH_END_BI) \
301 && \
302 ( ((arch) & ~(K_ARCH_BIT_MASK | K_ARCH_END_MASK)) >= K_ARCH_UNKNOWN \
303 && ((arch) & ~(K_ARCH_BIT_MASK | K_ARCH_END_MASK)) < K_ARCH_MAX) \
304 )
305
306/** @def K_ARCH_BITS_EX
307 * Determin the architure byte width of the specified architecture.
308 * @param arch The K_ARCH_* define to examin.
309 */
310#define K_ARCH_BITS_EX(arch) ( ((arch) & K_ARCH_BIT_MASK) >> K_ARCH_BIT_SHIFT )
311
312/** @def K_ARCH_BYTES_EX
313 * Determin the architure byte width of the specified architecture.
314 * @param arch The K_ARCH_* define to examin.
315 */
316#define K_ARCH_BYTES_EX(arch) ( ((arch) & K_ARCH_BIT_MASK) >> K_ARCH_BYTE_SHIFT )
317
318/** @def K_ARCH_ENDIAN_EX
319 * Determin the K_ENDIAN value for the specified architecture.
320 * @param arch The K_ARCH_* define to examin.
321 */
322#define K_ARCH_ENDIAN_EX(arch) ( ((arch) & K_ARCH_END_MASK) >> K_ARCH_END_SHIFT )
323
324/** @def K_ARCH_BITS
325 * Determin the target architure bit width.
326 */
327#define K_ARCH_BITS K_ARCH_BITS_EX(K_ARCH)
328
329/** @def K_ARCH_BYTES
330 * Determin the target architure byte width.
331 */
332#define K_ARCH_BYTES K_ARCH_BYTES_EX(K_ARCH)
333
334/** @def K_ARCH_ENDIAN
335 * Determin the target K_ENDIAN value.
336 */
337#define K_ARCH_ENDIAN K_ARCH_ENDIAN_EX(K_ARCH)
338
339
340
341/** @name Endianness Identifiers.
342 * These are the value that the K_ENDIAN \#define can take.
343 * @{ */
344#define K_ENDIAN_LITTLE 1 /**< Little-endian. */
345#define K_ENDIAN_BIG 2 /**< Big-endian. */
346#define K_ENDIAN_BI 3 /**< Bi-endian, can be switched. Only used with K_ARCH. */
347/** @} */
348
349/** @def K_ENDIAN
350 * The value of this \#define indicates the target endianness.
351 *
352 * @remark It's necessary to define this (or add the necessary deduction here)
353 * on bi-endian architectures.
354 */
355#ifndef K_ENDIAN
356 /* use K_ARCH if possible. */
357# if K_ARCH_ENDIAN != K_ENDIAN_BI
358# define K_ENDIAN K_ARCH_ENDIAN
359# elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__)
360# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
361# define K_ENDIAN K_ENDIAN_LITTLE
362# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
363# define K_ENDIAN K_ENDIAN_BIG
364# else
365# error "Port Me or define K_ENDIAN."
366# endif
367# else
368# error "Port Me or define K_ENDIAN."
369# endif
370#else
371 /* validate the user defined value. */
372# if K_ENDIAN != K_ENDIAN_LITTLE \
373 && K_ENDIAN != K_ENDIAN_BIG
374# error "K_ENDIAN must either be defined as K_ENDIAN_LITTLE or as K_ENDIAN_BIG."
375# endif
376#endif
377
378/** @name Endian Conversion
379 * @{ */
380
381/** @def K_E2E_U16
382 * Convert the endian of an unsigned 16-bit value. */
383# define K_E2E_U16(u16) ( (KU16) (((u16) >> 8) | ((u16) << 8)) )
384/** @def K_E2E_U32
385 * Convert the endian of an unsigned 32-bit value. */
386# define K_E2E_U32(u32) ( ( ((u32) & KU32_C(0xff000000)) >> 24 ) \
387 | ( ((u32) & KU32_C(0x00ff0000)) >> 8 ) \
388 | ( ((u32) & KU32_C(0x0000ff00)) << 8 ) \
389 | ( ((u32) & KU32_C(0x000000ff)) << 24 ) \
390 )
391/** @def K_E2E_U64
392 * Convert the endian of an unsigned 64-bit value. */
393# define K_E2E_U64(u64) ( ( ((u64) & KU64_C(0xff00000000000000)) >> 56 ) \
394 | ( ((u64) & KU64_C(0x00ff000000000000)) >> 40 ) \
395 | ( ((u64) & KU64_C(0x0000ff0000000000)) >> 24 ) \
396 | ( ((u64) & KU64_C(0x000000ff00000000)) >> 8 ) \
397 | ( ((u64) & KU64_C(0x00000000ff000000)) << 8 ) \
398 | ( ((u64) & KU64_C(0x0000000000ff0000)) << 24 ) \
399 | ( ((u64) & KU64_C(0x000000000000ff00)) << 40 ) \
400 | ( ((u64) & KU64_C(0x00000000000000ff)) << 56 ) \
401 )
402
403/** @def K_LE2H_U16
404 * Unsigned 16-bit little-endian to host endian. */
405/** @def K_LE2H_U32
406 * Unsigned 32-bit little-endian to host endian. */
407/** @def K_LE2H_U64
408 * Unsigned 64-bit little-endian to host endian. */
409/** @def K_BE2H_U16
410 * Unsigned 16-bit big-endian to host endian. */
411/** @def K_BE2H_U32
412 * Unsigned 32-bit big-endian to host endian. */
413/** @def K_BE2H_U64
414 * Unsigned 64-bit big-endian to host endian. */
415#if K_ENDIAN == K_ENDIAN_LITTLE
416# define K_LE2H_U16(u16) ((KU16)(u16))
417# define K_LE2H_U32(u32) ((KU32)(u32))
418# define K_LE2H_U64(u64) ((KU64)(u32))
419# define K_BE2H_U16(u16) K_E2E_U16(u16)
420# define K_BE2H_U32(u32) K_E2E_U32(u32)
421# define K_BE2H_U64(u64) K_E2E_U64(u64)
422#else
423# define K_LE2H_U16(u16) K_E2E_U16(u16)
424# define K_LE2H_U32(u32) K_E2E_U32(u32)
425# define K_LE2H_U64(u64) K_E2E_U64(u64)
426# define K_BE2H_U16(u16) ((KU16)(u16))
427# define K_BE2H_U32(u32) ((KU32)(u32))
428# define K_BE2H_U64(u64) ((KU64)(u32))
429#endif
430
431
432
433/** @def K_INLINE
434 * How to say 'inline' in both C and C++ dialects.
435 * @param type The return type.
436 */
437#ifdef __cplusplus
438# if defined(__GNUC__)
439# define K_INLINE static inline
440# else
441# define K_INLINE inline
442# endif
443#else
444# if defined(__GNUC__)
445# define K_INLINE static __inline__
446# elif defined(_MSC_VER)
447# define K_INLINE static __inline
448# else
449# error "Port Me"
450# endif
451#endif
452
453/** @def K_EXPORT
454 * What to put in front of an exported function.
455 */
456#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
457# define K_EXPORT __declspec(dllexport)
458#else
459# define K_EXPORT
460#endif
461
462/** @def K_IMPORT
463 * What to put in front of an imported function.
464 */
465#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
466# define K_IMPORT __declspec(dllimport)
467#else
468# define K_IMPORT extern
469#endif
470
471/** @def K_DECL_EXPORT
472 * Declare an exported function.
473 * @param type The return type.
474 */
475#define K_DECL_EXPORT(type) K_EXPORT type
476
477/** @def K_DECL_IMPORT
478 * Declare an import function.
479 * @param type The return type.
480 */
481#define K_DECL_IMPORT(type) K_IMPORT type
482
483/** @def K_DECL_INLINE
484 * Declare an inline function.
485 * @param type The return type.
486 * @remark Don't use on (class) methods.
487 */
488#define K_DECL_INLINE(type) K_INLINE type
489
490
491/** Get the minimum of two values. */
492#define K_MIN(a, b) ( (a) <= (b) ? (a) : (b) )
493/** Get the maximum of two values. */
494#define K_MAX(a, b) ( (a) >= (b) ? (a) : (b) )
495/** Calculate the offset of a structure member. */
496#define K_OFFSETOF(strct, memb) ( (KSIZE)( &((strct *)0)->memb ) )
497/** Align a size_t value. */
498#define K_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(KSIZE)((align) - 1) )
499/** Align a void * value. */
500#define K_ALIGN_P(pv, align) ( (void *)( ((KUPTR)(pv) + ((align) - 1)) & ~(KUPTR)((align) - 1) ) )
501/** Number of elements in an array. */
502#define K_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) )
503/** Checks if the specified pointer is a valid address or not. */
504#define K_VALID_PTR(ptr) ( (KUPTR)(ptr) + 0x1000U >= 0x2000U )
505/** Makes a 32-bit bit mask. */
506#define K_BIT32(bit) ( KU32_C(1) << (bit))
507/** Makes a 64-bit bit mask. */
508#define K_BIT64(bit) ( KU64_C(1) << (bit))
509/** Shuts up unused parameter and unused variable warnings. */
510#define K_NOREF(var) ( (void)(var) )
511
512
513/** @name Parameter validation macros
514 * @{ */
515
516/** Return/Crash validation of a string argument. */
517#define K_VALIDATE_STRING(str) \
518 do { \
519 if (!K_VALID_PTR(str)) \
520 return KERR_INVALID_POINTER; \
521 kHlpStrLen(str); \
522 } while (0)
523
524/** Return/Crash validation of an optional string argument. */
525#define K_VALIDATE_OPTIONAL_STRING(str) \
526 do { \
527 if (str) \
528 K_VALIDATE_STRING(str); \
529 } while (0)
530
531/** Return/Crash validation of an output buffer. */
532#define K_VALIDATE_BUFFER(buf, cb) \
533 do { \
534 if (!K_VALID_PTR(buf)) \
535 return KERR_INVALID_POINTER; \
536 if ((cb) != 0) \
537 { \
538 KU8 __b; \
539 KU8 volatile *__pb = (KU8 volatile *)(buf); \
540 KSIZE __cbPage1 = 0x1000 - ((KUPTR)(__pb) & 0xfff); /* ASSUMES page size! */ \
541 __b = *__pb; *__pb = 0xff; *__pb = __b; \
542 if ((cb) > __cbPage1) \
543 { \
544 KSIZE __cb = (cb) - __cbPage1; \
545 __pb -= __cbPage1; \
546 for (;;) \
547 { \
548 __b = *__pb; *__pb = 0xff; *__pb = __b; \
549 if (__cb < 0x1000) \
550 break; \
551 __pb += 0x1000; \
552 __cb -= 0x1000; \
553 } \
554 } \
555 } \
556 else \
557 return KERR_INVALID_PARAMETER; \
558 } while (0)
559
560/** Return/Crash validation of an optional output buffer. */
561#define K_VALIDATE_OPTIONAL_BUFFER(buf, cb) \
562 do { \
563 if ((buf) && (cb) != 0) \
564 K_VALIDATE_BUFFER(buf, cb); \
565 } while (0)
566
567/** Return validation of an enum argument. */
568#define K_VALIDATE_ENUM(arg, enumname) \
569 do { \
570 if ((arg) <= enumname##_INVALID || (arg) >= enumname##_END) \
571 return KERR_INVALID_PARAMETER; \
572 } while (0)
573
574/** Return validation of a flags argument. */
575#define K_VALIDATE_FLAGS(arg, AllowedMask) \
576 do { \
577 if ((arg) & ~(AllowedMask)) \
578 return KERR_INVALID_PARAMETER; \
579 } while (0)
580
581/** @} */
582
583/** @def NULL
584 * The nil pointer value. */
585#ifndef NULL
586# ifdef __cplusplus
587# define NULL 0
588# else
589# define NULL ((void *)0)
590# endif
591#endif
592
593/** @} */
594
595#endif
596
Note: See TracBrowser for help on using the repository browser.