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