1 | /* $Id: kDbgBase.h 3526 2007-08-19 22:46:14Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * kDbg - The Debug Info Reader, Base Definitions and Typedefs.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2006-2007 knut st. osmundsen <bird@anduin.net>
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This file is part of kDbg.
|
---|
10 | *
|
---|
11 | * kDbg 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 | * kDbg 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 kDbg; 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 ___kDbgBase_h___
|
---|
28 | #define ___kDbgBase_h___
|
---|
29 |
|
---|
30 |
|
---|
31 | /** @defgroup grp_kDbgBase kDbgBase - Base Definitions And Typedefs
|
---|
32 | * @{ */
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * kDbg depend on size_t, [u]intNN_t, [u]intptr_t and some related constants.
|
---|
36 | * If KDBG_ALREADY_INCLUDED_STD_TYPES or KCOMMON_ALREADY_INCLUDED_STD_TYPES
|
---|
37 | * is defined, these has already been defined.
|
---|
38 | */
|
---|
39 | #if !defined(KDBG_ALREADY_INCLUDED_STD_TYPES) && !defined(KCOMMON_ALREADY_INCLUDED_STD_TYPES)
|
---|
40 | # define KCOMMON_ALREADY_INCLUDED_STD_TYPES 1
|
---|
41 | # include <sys/types.h>
|
---|
42 | # include <stddef.h>
|
---|
43 | # ifdef _MSC_VER
|
---|
44 | typedef signed char int8_t;
|
---|
45 | typedef unsigned char uint8_t;
|
---|
46 | typedef signed short int16_t;
|
---|
47 | typedef unsigned short uint16_t;
|
---|
48 | typedef signed int int32_t;
|
---|
49 | typedef unsigned int uint32_t;
|
---|
50 | typedef signed __int64 int64_t;
|
---|
51 | typedef unsigned __int64 uint64_t;
|
---|
52 | typedef int64_t intmax_t;
|
---|
53 | typedef uint64_t uintmax_t;
|
---|
54 | # define UINT8_C(c) (c)
|
---|
55 | # define UINT16_C(c) (c)
|
---|
56 | # define UINT32_C(c) (c ## U)
|
---|
57 | # define UINT64_C(c) (c ## ULL)
|
---|
58 | # define INT8_C(c) (c)
|
---|
59 | # define INT16_C(c) (c)
|
---|
60 | # define INT32_C(c) (c)
|
---|
61 | # define INT64_C(c) (c ## LL)
|
---|
62 | # define INT8_MIN (INT8_C(-0x7f) - 1)
|
---|
63 | # define INT16_MIN (INT16_C(-0x7fff) - 1)
|
---|
64 | # define INT32_MIN (INT32_C(-0x7fffffff) - 1)
|
---|
65 | # define INT64_MIN (INT64_C(-0x7fffffffffffffff) - 1)
|
---|
66 | # define INT8_MAX INT8_C(0x7f)
|
---|
67 | # define INT16_MAX INT16_C(0x7fff)
|
---|
68 | # define INT32_MAX INT32_C(0x7fffffff)
|
---|
69 | # define INT64_MAX INT64_C(0x7fffffffffffffff)
|
---|
70 | # define UINT8_MAX UINT8_C(0xff)
|
---|
71 | # define UINT16_MAX UINT16_C(0xffff)
|
---|
72 | # define UINT32_MAX UINT32_C(0xffffffff)
|
---|
73 | # define UINT64_MAX UINT64_C(0xffffffffffffffff)
|
---|
74 | # else
|
---|
75 | # include <stdint.h>
|
---|
76 | # endif
|
---|
77 | #endif /* !KDBG_ALREADY_INCLUDED_STD_TYPES && !KCOMMON_ALREADY_INCLUDED_STD_TYPES */
|
---|
78 |
|
---|
79 |
|
---|
80 | /** Get the minimum of two values. */
|
---|
81 | #define KDBG_MIN(a, b) ((a) <= (b) ? (a) : (b))
|
---|
82 | /** Get the maximum of two values. */
|
---|
83 | #define KDBG_MAX(a, b) ((a) >= (b) ? (a) : (b))
|
---|
84 | /** Calculate the offset of a structure member. */
|
---|
85 | #define KDBG_OFFSETOF(strct, memb) ( (size_t)( &((strct *)0)->memb ) )
|
---|
86 | /** Align a size_t value. */
|
---|
87 | #define KDBG_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(size_t)((align) - 1) )
|
---|
88 | /** Align a void * value. */
|
---|
89 | #define KDBG_ALIGN_P(pv, align) ( (void *)( ((uintptr_t)(pv) + ((align) - 1)) & ~(uintptr_t)((align) - 1) ) )
|
---|
90 | /** Align a size_t value. */
|
---|
91 | #define KDBG_ALIGN_ADDR(val, align) ( ((val) + ((align) - 1)) & ~(KLDRADDR)((align) - 1) )
|
---|
92 | /** Number of elements in an array. */
|
---|
93 | #define KDBG_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) )
|
---|
94 |
|
---|
95 |
|
---|
96 | /** @def KDBG_LITTLE_ENDIAN
|
---|
97 | * The kDbg build is for a little endian target. */
|
---|
98 | /** @def KDBG_BIG_ENDIAN
|
---|
99 | * The kDbg build is for a big endian target. */
|
---|
100 | #if !defined(KDBG_LITTLE_ENDIAN) && !defined(KDBG_BIG_ENDIAN)
|
---|
101 | # define KDBG_LITTLE_ENDIAN
|
---|
102 | #endif
|
---|
103 | #ifdef __DOXYGEN__
|
---|
104 | # define KDBG_BIG_ENDIAN
|
---|
105 | #endif
|
---|
106 |
|
---|
107 |
|
---|
108 | /** @name Endian Conversion
|
---|
109 | * @{ */
|
---|
110 |
|
---|
111 | /** @def KDBG_E2E_U16
|
---|
112 | * Convert the endian of an unsigned 16-bit value. */
|
---|
113 | # define KDBG_E2E_U16(u16) ( (uint16_t) (((u16) >> 8) | ((u16) << 8)) )
|
---|
114 | /** @def KDBG_E2E_U32
|
---|
115 | * Convert the endian of an unsigned 32-bit value. */
|
---|
116 | # define KDBG_E2E_U32(u32) ( ( ((u32) & UINT32_C(0xff000000)) >> 24 ) \
|
---|
117 | | ( ((u32) & UINT32_C(0x00ff0000)) >> 8 ) \
|
---|
118 | | ( ((u32) & UINT32_C(0x0000ff00)) << 8 ) \
|
---|
119 | | ( ((u32) & UINT32_C(0x000000ff)) << 24 ) \
|
---|
120 | )
|
---|
121 | /** @def KDBG_E2E_U64
|
---|
122 | * Convert the endian of an unsigned 64-bit value. */
|
---|
123 | # define KDBG_E2E_U64(u64) ( ( ((u64) & UINT64_C(0xff00000000000000)) >> 56 ) \
|
---|
124 | | ( ((u64) & UINT64_C(0x00ff000000000000)) >> 40 ) \
|
---|
125 | | ( ((u64) & UINT64_C(0x0000ff0000000000)) >> 24 ) \
|
---|
126 | | ( ((u64) & UINT64_C(0x000000ff00000000)) >> 8 ) \
|
---|
127 | | ( ((u64) & UINT64_C(0x00000000ff000000)) << 8 ) \
|
---|
128 | | ( ((u64) & UINT64_C(0x0000000000ff0000)) << 24 ) \
|
---|
129 | | ( ((u64) & UINT64_C(0x000000000000ff00)) << 40 ) \
|
---|
130 | | ( ((u64) & UINT64_C(0x00000000000000ff)) << 56 ) \
|
---|
131 | )
|
---|
132 |
|
---|
133 | /** @def KDBG_LE2H_U16
|
---|
134 | * Unsigned 16-bit little-endian to host endian. */
|
---|
135 | /** @def KDBG_LE2H_U32
|
---|
136 | * Unsigned 32-bit little-endian to host endian. */
|
---|
137 | /** @def KDBG_LE2H_U64
|
---|
138 | * Unsigned 64-bit little-endian to host endian. */
|
---|
139 | /** @def KDBG_BE2H_U16
|
---|
140 | * Unsigned 16-bit big-endian to host endian. */
|
---|
141 | /** @def KDBG_BE2H_U32
|
---|
142 | * Unsigned 32-bit big-endian to host endian. */
|
---|
143 | /** @def KDBG_BE2H_U64
|
---|
144 | * Unsigned 64-bit big-endian to host endian. */
|
---|
145 | #ifdef KDBG_LITTLE_ENDIAN
|
---|
146 | # define KDBG_LE2H_U16(u16) ((uint16_t)(u16))
|
---|
147 | # define KDBG_LE2H_U32(u32) ((uint32_t)(u32))
|
---|
148 | # define KDBG_LE2H_U64(u64) ((uint32_t)(u32))
|
---|
149 | # define KDBG_BE2H_U16(u16) KDBG_E2E_U16(u16)
|
---|
150 | # define KDBG_BE2H_U32(u32) KDBG_E2E_U32(u32)
|
---|
151 | # define KDBG_BE2H_U64(u64) KDBG_E2E_U64(u64)
|
---|
152 | #elif defined(KDBG_BIG_ENDIAN)
|
---|
153 | # define KDBG_LE2H_U16(u16) KDBG_E2E_U16(u16)
|
---|
154 | # define KDBG_LE2H_U32(u32) KDBG_E2E_U32(u32)
|
---|
155 | # define KDBG_LE2H_U32(u64) KDBG_E2E_U64(u64)
|
---|
156 | # define KDBG_BE2H_U16(u16) ((uint16_t)(u16))
|
---|
157 | # define KDBG_BE2H_U32(u32) ((uint32_t)(u32))
|
---|
158 | # define KDBG_BE2H_U64(u64) ((uint32_t)(u32))
|
---|
159 | #else
|
---|
160 | # error "KDBG_BIG_ENDIAN or KDBG_LITTLE_ENDIAN is supposed to be defined."
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | /** @} */
|
---|
164 |
|
---|
165 | /** @} */
|
---|
166 |
|
---|
167 | #endif
|
---|
168 |
|
---|