source: trunk/kLdr/kLdrBase.h@ 3525

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

made the format headers usable externally.

  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1/* $Id: kLdrBase.h 3525 2007-08-19 22:14:55Z 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
31/** @defgroup grp_kLdrBase kLdrBase - Base Definitions And Typedefs
32 * @{ */
33
34/*
35 * kLdr depend on size_t, [u]intNN_t, [u]intptr_t and some related constants.
36 * If KLDR_ALREADY_INCLUDED_STD_TYPES is defined, these has already been defined.
37 */
38#ifndef KLDR_ALREADY_INCLUDED_STD_TYPES
39# include <sys/types.h>
40# include <stddef.h>
41# ifdef _MSC_VER
42 typedef signed char int8_t;
43 typedef unsigned char uint8_t;
44 typedef signed short int16_t;
45 typedef unsigned short uint16_t;
46 typedef signed int int32_t;
47 typedef unsigned int uint32_t;
48 typedef signed __int64 int64_t;
49 typedef unsigned __int64 uint64_t;
50 typedef int64_t intmax_t;
51 typedef uint64_t uintmax_t;
52# define UINT8_C(c) (c)
53# define UINT16_C(c) (c)
54# define UINT32_C(c) (c ## U)
55# define UINT64_C(c) (c ## ULL)
56# define INT8_C(c) (c)
57# define INT16_C(c) (c)
58# define INT32_C(c) (c)
59# define INT64_C(c) (c ## LL)
60# define INT8_MIN (INT8_C(-0x7f) - 1)
61# define INT16_MIN (INT16_C(-0x7fff) - 1)
62# define INT32_MIN (INT32_C(-0x7fffffff) - 1)
63# define INT64_MIN (INT64_C(-0x7fffffffffffffff) - 1)
64# define INT8_MAX INT8_C(0x7f)
65# define INT16_MAX INT16_C(0x7fff)
66# define INT32_MAX INT32_C(0x7fffffff)
67# define INT64_MAX INT64_C(0x7fffffffffffffff)
68# define UINT8_MAX UINT8_C(0xff)
69# define UINT16_MAX UINT16_C(0xffff)
70# define UINT32_MAX UINT32_C(0xffffffff)
71# define UINT64_MAX UINT64_C(0xffffffffffffffff)
72# else
73# include <stdint.h>
74# endif
75#endif /* !KLDR_ALREADY_INCLUDED_STD_TYPES */
76
77
78/** Get the minimum of two values. */
79#define KLDR_MIN(a, b) ((a) <= (b) ? (a) : (b))
80/** Get the maximum of two values. */
81#define KLDR_MAX(a, b) ((a) >= (b) ? (a) : (b))
82/** Calculate the offset of a structure member. */
83#define KLDR_OFFSETOF(strct, memb) ( (size_t)( &((strct *)0)->memb ) )
84/** Align a size_t value. */
85#define KLDR_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(size_t)((align) - 1) )
86/** Align a void * value. */
87#define KLDR_ALIGN_P(pv, align) ( (void *)( ((uintptr_t)(pv) + ((align) - 1)) & ~(uintptr_t)((align) - 1) ) )
88/** Align a size_t value. */
89#define KLDR_ALIGN_ADDR(val, align) ( ((val) + ((align) - 1)) & ~(KLDRADDR)((align) - 1) )
90/** Number of elements in an array. */
91#define KLDR_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) )
92
93
94/** @def KLDR_LITTLE_ENDIAN
95 * The kLdr build is for a little endian target. */
96/** @def KLDR_BIG_ENDIAN
97 * The kLdr build is for a big endian target. */
98#if !defined(KLDR_LITTLE_ENDIAN) && !defined(KLDR_BIG_ENDIAN)
99# define KLDR_LITTLE_ENDIAN
100#endif
101#ifdef __DOXYGEN__
102# define KLDR_BIG_ENDIAN
103#endif
104
105
106/** @name Endian Conversion
107 * @{ */
108
109/** @def KLDR_E2E_U16
110 * Convert the endian of an unsigned 16-bit value. */
111# define KLDR_E2E_U16(u16) ( (uint16_t) (((u16) >> 8) | ((u16) << 8)) )
112/** @def KLDR_E2E_U32
113 * Convert the endian of an unsigned 32-bit value. */
114# define KLDR_E2E_U32(u32) ( ( ((u32) & UINT32_C(0xff000000)) >> 24 ) \
115 | ( ((u32) & UINT32_C(0x00ff0000)) >> 8 ) \
116 | ( ((u32) & UINT32_C(0x0000ff00)) << 8 ) \
117 | ( ((u32) & UINT32_C(0x000000ff)) << 24 ) \
118 )
119/** @def KLDR_E2E_U64
120 * Convert the endian of an unsigned 64-bit value. */
121# define KLDR_E2E_U64(u64) ( ( ((u64) & UINT64_C(0xff00000000000000)) >> 56 ) \
122 | ( ((u64) & UINT64_C(0x00ff000000000000)) >> 40 ) \
123 | ( ((u64) & UINT64_C(0x0000ff0000000000)) >> 24 ) \
124 | ( ((u64) & UINT64_C(0x000000ff00000000)) >> 8 ) \
125 | ( ((u64) & UINT64_C(0x00000000ff000000)) << 8 ) \
126 | ( ((u64) & UINT64_C(0x0000000000ff0000)) << 24 ) \
127 | ( ((u64) & UINT64_C(0x000000000000ff00)) << 40 ) \
128 | ( ((u64) & UINT64_C(0x00000000000000ff)) << 56 ) \
129 )
130
131/** @def KLDR_LE2H_U16
132 * Unsigned 16-bit little-endian to host endian. */
133/** @def KLDR_LE2H_U32
134 * Unsigned 32-bit little-endian to host endian. */
135/** @def KLDR_LE2H_U64
136 * Unsigned 64-bit little-endian to host endian. */
137/** @def KLDR_BE2H_U16
138 * Unsigned 16-bit big-endian to host endian. */
139/** @def KLDR_BE2H_U32
140 * Unsigned 32-bit big-endian to host endian. */
141/** @def KLDR_BE2H_U64
142 * Unsigned 64-bit big-endian to host endian. */
143#ifdef KLDR_LITTLE_ENDIAN
144# define KLDR_LE2H_U16(u16) ((uint16_t)(u16))
145# define KLDR_LE2H_U32(u32) ((uint32_t)(u32))
146# define KLDR_LE2H_U64(u64) ((uint32_t)(u32))
147# define KLDR_BE2H_U16(u16) KLDR_E2E_U16(u16)
148# define KLDR_BE2H_U32(u32) KLDR_E2E_U32(u32)
149# define KLDR_BE2H_U64(u64) KLDR_E2E_U64(u64)
150#elif defined(KLDR_BIG_ENDIAN)
151# define KLDR_LE2H_U16(u16) KLDR_E2E_U16(u16)
152# define KLDR_LE2H_U32(u32) KLDR_E2E_U32(u32)
153# define KLDR_LE2H_U32(u64) KLDR_E2E_U64(u64)
154# define KLDR_BE2H_U16(u16) ((uint16_t)(u16))
155# define KLDR_BE2H_U32(u32) ((uint32_t)(u32))
156# define KLDR_BE2H_U64(u64) ((uint32_t)(u32))
157#else
158# error "KLDR_BIG_ENDIAN or KLDR_LITTLE_ENDIAN is supposed to be defined."
159#endif
160
161/** @} */
162
163/** @} */
164
165#endif
166
Note: See TracBrowser for help on using the repository browser.