source: trunk/kStuff/kLdr/kLdrBase.h@ 3567

Last change on this file since 3567 was 3567, checked in by bird, 18 years ago

Use the new type system.

  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/* $Id: kLdrBase.h 3567 2007-08-27 19:54:05Z bird $ */
2/** @file
3 *
4 * kLdr - The Dynamic Loader, Base Definitions and Typedefs.
5 *
6 * Copyright (c) 2006-2007 knut st. osmundsen <bird@anduin.net>
7 *
8 *
9 * This file is part of kLdr.
10 *
11 * kLdr is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kLdr 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 General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kLdr; 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 ___kLdrBase_h___
28#define ___kLdrBase_h___
29
30#include <k/kDefs.h>
31#include <k/kTypes.h>
32
33
34/** @defgroup grp_kLdrBase kLdrBase - Base Definitions And Typedefs
35 * @{ */
36
37/** Get the minimum of two values. */
38#define KLDR_MIN(a, b) ((a) <= (b) ? (a) : (b))
39/** Get the maximum of two values. */
40#define KLDR_MAX(a, b) ((a) >= (b) ? (a) : (b))
41/** Calculate the offset of a structure member. */
42#define KLDR_OFFSETOF(strct, memb) ( (KSIZE)( &((strct *)0)->memb ) )
43/** Align a KSIZE value. */
44#define KLDR_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(KSIZE)((align) - 1) )
45/** Align a void * value. */
46#define KLDR_ALIGN_P(pv, align) ( (void *)( ((KUPTR)(pv) + ((align) - 1)) & ~(KUPTR)((align) - 1) ) )
47/** Align a KSIZE value. */
48#define KLDR_ALIGN_ADDR(val, align) ( ((val) + ((align) - 1)) & ~(KLDRADDR)((align) - 1) )
49/** Number of elements in an array. */
50#define KLDR_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) )
51
52
53/** @def KLDR_LITTLE_ENDIAN
54 * The kLdr build is for a little endian target. */
55/** @def KLDR_BIG_ENDIAN
56 * The kLdr build is for a big endian target. */
57#if !defined(KLDR_LITTLE_ENDIAN) && !defined(KLDR_BIG_ENDIAN)
58# define KLDR_LITTLE_ENDIAN
59#endif
60#ifdef __DOXYGEN__
61# define KLDR_BIG_ENDIAN
62#endif
63
64
65/** @name Endian Conversion
66 * @{ */
67
68/** @def KLDR_E2E_U16
69 * Convert the endian of an unsigned 16-bit value. */
70# define KLDR_E2E_U16(u16) ( (KU16) (((u16) >> 8) | ((u16) << 8)) )
71/** @def KLDR_E2E_U32
72 * Convert the endian of an unsigned 32-bit value. */
73# define KLDR_E2E_U32(u32) ( ( ((u32) & KU32_C(0xff000000)) >> 24 ) \
74 | ( ((u32) & KU32_C(0x00ff0000)) >> 8 ) \
75 | ( ((u32) & KU32_C(0x0000ff00)) << 8 ) \
76 | ( ((u32) & KU32_C(0x000000ff)) << 24 ) \
77 )
78/** @def KLDR_E2E_U64
79 * Convert the endian of an unsigned 64-bit value. */
80# define KLDR_E2E_U64(u64) ( ( ((u64) & KU64_C(0xff00000000000000)) >> 56 ) \
81 | ( ((u64) & KU64_C(0x00ff000000000000)) >> 40 ) \
82 | ( ((u64) & KU64_C(0x0000ff0000000000)) >> 24 ) \
83 | ( ((u64) & KU64_C(0x000000ff00000000)) >> 8 ) \
84 | ( ((u64) & KU64_C(0x00000000ff000000)) << 8 ) \
85 | ( ((u64) & KU64_C(0x0000000000ff0000)) << 24 ) \
86 | ( ((u64) & KU64_C(0x000000000000ff00)) << 40 ) \
87 | ( ((u64) & KU64_C(0x00000000000000ff)) << 56 ) \
88 )
89
90/** @def KLDR_LE2H_U16
91 * Unsigned 16-bit little-endian to host endian. */
92/** @def KLDR_LE2H_U32
93 * Unsigned 32-bit little-endian to host endian. */
94/** @def KLDR_LE2H_U64
95 * Unsigned 64-bit little-endian to host endian. */
96/** @def KLDR_BE2H_U16
97 * Unsigned 16-bit big-endian to host endian. */
98/** @def KLDR_BE2H_U32
99 * Unsigned 32-bit big-endian to host endian. */
100/** @def KLDR_BE2H_U64
101 * Unsigned 64-bit big-endian to host endian. */
102#ifdef KLDR_LITTLE_ENDIAN
103# define KLDR_LE2H_U16(u16) ((KU16)(u16))
104# define KLDR_LE2H_U32(u32) ((KU32)(u32))
105# define KLDR_LE2H_U64(u64) ((KU32)(u32))
106# define KLDR_BE2H_U16(u16) KLDR_E2E_U16(u16)
107# define KLDR_BE2H_U32(u32) KLDR_E2E_U32(u32)
108# define KLDR_BE2H_U64(u64) KLDR_E2E_U64(u64)
109#elif defined(KLDR_BIG_ENDIAN)
110# define KLDR_LE2H_U16(u16) KLDR_E2E_U16(u16)
111# define KLDR_LE2H_U32(u32) KLDR_E2E_U32(u32)
112# define KLDR_LE2H_U32(u64) KLDR_E2E_U64(u64)
113# define KLDR_BE2H_U16(u16) ((KU16)(u16))
114# define KLDR_BE2H_U32(u32) ((KU32)(u32))
115# define KLDR_BE2H_U64(u64) ((KU32)(u32))
116#else
117# error "KLDR_BIG_ENDIAN or KLDR_LITTLE_ENDIAN is supposed to be defined."
118#endif
119
120/** @} */
121
122/** @} */
123
124#endif
125
Note: See TracBrowser for help on using the repository browser.