| 1 | /* $Id: kDefs.h 3554 2007-08-26 10:42:15Z 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 | /** Shuts up unused parameter and unused variable warnings. */
|
|---|
| 381 | #define K_NOREF(var) ( (void)(var) )
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 | /** @name Parameter validation macros
|
|---|
| 385 | * @{ */
|
|---|
| 386 |
|
|---|
| 387 | /** Return/Crash validation of a string argument. */
|
|---|
| 388 | #define K_VALIDATE_STRING(str) \
|
|---|
| 389 | do { \
|
|---|
| 390 | if (!K_VALID_PTR(str)) \
|
|---|
| 391 | return KERR_INVALID_POINTER; \
|
|---|
| 392 | kHlpStrLen(str); \
|
|---|
| 393 | } while (0)
|
|---|
| 394 |
|
|---|
| 395 | /** Return/Crash validation of an optional string argument. */
|
|---|
| 396 | #define K_VALIDATE_OPTIONAL_STRING(str) \
|
|---|
| 397 | do { \
|
|---|
| 398 | if (str) \
|
|---|
| 399 | K_VALIDATE_STRING(str); \
|
|---|
| 400 | } while (0)
|
|---|
| 401 |
|
|---|
| 402 | /** Return/Crash validation of an output buffer. */
|
|---|
| 403 | #define K_VALIDATE_BUFFER(buf, cb) \
|
|---|
| 404 | do { \
|
|---|
| 405 | if (!K_VALID_PTR(buf)) \
|
|---|
| 406 | return KERR_INVALID_POINTER; \
|
|---|
| 407 | if ((cb) != 0) \
|
|---|
| 408 | { \
|
|---|
| 409 | KU8 __b; \
|
|---|
| 410 | KU8 volatile *__pb = (KU8 volatile *)(buf); \
|
|---|
| 411 | KSIZE __cbPage1 = 0x1000 - ((KUPTR)(__pb) & 0xfff); /* ASSUMES page size! */ \
|
|---|
| 412 | __b = *__pb; *__pb = 0xff; *__pb = __b; \
|
|---|
| 413 | if ((cb) > __cbPage1) \
|
|---|
| 414 | { \
|
|---|
| 415 | KSIZE __cb = (cb) - __cbPage1; \
|
|---|
| 416 | __pb -= __cbPage1; \
|
|---|
| 417 | for (;;) \
|
|---|
| 418 | { \
|
|---|
| 419 | __b = *__pb; *__pb = 0xff; *__pb = __b; \
|
|---|
| 420 | if (__cb < 0x1000) \
|
|---|
| 421 | break; \
|
|---|
| 422 | __pb += 0x1000; \
|
|---|
| 423 | __cb -= 0x1000; \
|
|---|
| 424 | } \
|
|---|
| 425 | } \
|
|---|
| 426 | } \
|
|---|
| 427 | else \
|
|---|
| 428 | return KERR_INVALID_PARAMETER; \
|
|---|
| 429 | } while (0)
|
|---|
| 430 |
|
|---|
| 431 | /** Return/Crash validation of an optional output buffer. */
|
|---|
| 432 | #define K_VALIDATE_OPTIONAL_BUFFER(buf, cb) \
|
|---|
| 433 | do { \
|
|---|
| 434 | if ((buf) && (cb) != 0) \
|
|---|
| 435 | K_VALIDATE_BUFFER(buf, cb); \
|
|---|
| 436 | } while (0)
|
|---|
| 437 |
|
|---|
| 438 | /** Return validation of an enum argument. */
|
|---|
| 439 | #define K_VALIDATE_ENUM(arg, enumname) \
|
|---|
| 440 | do { \
|
|---|
| 441 | if ((arg) <= enumname##_INVALID || (arg) >= enumname##_END) \
|
|---|
| 442 | return KERR_INVALID_PARAMETER; \
|
|---|
| 443 | } while (0)
|
|---|
| 444 |
|
|---|
| 445 | /** Return validation of a flags argument. */
|
|---|
| 446 | #define K_VALIDATE_FLAGS(arg, AllowedMask) \
|
|---|
| 447 | do { \
|
|---|
| 448 | if ((arg) & ~(AllowedMask)) \
|
|---|
| 449 | return KERR_INVALID_PARAMETER; \
|
|---|
| 450 | } while (0)
|
|---|
| 451 |
|
|---|
| 452 | /** @} */
|
|---|
| 453 |
|
|---|
| 454 | /** @def NULL
|
|---|
| 455 | * The nil pointer value. */
|
|---|
| 456 | #ifndef NULL
|
|---|
| 457 | # ifdef __cplusplus
|
|---|
| 458 | # define NULL 0
|
|---|
| 459 | # else
|
|---|
| 460 | # define NULL ((void *)0)
|
|---|
| 461 | # endif
|
|---|
| 462 | #endif
|
|---|
| 463 |
|
|---|
| 464 | /** @} */
|
|---|
| 465 |
|
|---|
| 466 | #endif
|
|---|
| 467 |
|
|---|