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