ia64/xen-unstable

view tools/ioemu/bswap.h @ 7238:971e7c7411b3

Raise an exception if an error appears on the pipes to our children, and make
sure that the child's pipes are closed even under that exception. Move the
handling of POLLHUP to the end of the loop, so that we guarantee to read any
remaining data from the child if POLLHUP and POLLIN appear at the same time.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@ewan
date Thu Oct 06 10:13:11 2005 +0100 (2005-10-06)
parents 8e5fc5fe636c
children 04cc0e22a20a
line source
1 #ifndef BSWAP_H
2 #define BSWAP_H
4 #include "config-host.h"
6 #include <inttypes.h>
8 #ifdef HAVE_BYTESWAP_H
9 #include <byteswap.h>
10 #else
12 #define bswap_16(x) \
13 ({ \
14 uint16_t __x = (x); \
15 ((uint16_t)( \
16 (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
17 (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
18 })
20 #define bswap_32(x) \
21 ({ \
22 uint32_t __x = (x); \
23 ((uint32_t)( \
24 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
25 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
26 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
27 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
28 })
30 #define bswap_64(x) \
31 ({ \
32 uint64_t __x = (x); \
33 ((uint64_t)( \
34 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \
35 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
36 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
37 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
38 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
39 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
40 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
41 (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
42 })
44 #endif /* !HAVE_BYTESWAP_H */
46 static inline uint16_t bswap16(uint16_t x)
47 {
48 return bswap_16(x);
49 }
51 static inline uint32_t bswap32(uint32_t x)
52 {
53 return bswap_32(x);
54 }
56 static inline uint64_t bswap64(uint64_t x)
57 {
58 return bswap_64(x);
59 }
61 static inline void bswap16s(uint16_t *s)
62 {
63 *s = bswap16(*s);
64 }
66 static inline void bswap32s(uint32_t *s)
67 {
68 *s = bswap32(*s);
69 }
71 static inline void bswap64s(uint64_t *s)
72 {
73 *s = bswap64(*s);
74 }
76 #if defined(WORDS_BIGENDIAN)
77 #define be_bswap(v, size) (v)
78 #define le_bswap(v, size) bswap ## size(v)
79 #define be_bswaps(v, size)
80 #define le_bswaps(p, size) *p = bswap ## size(*p);
81 #else
82 #define le_bswap(v, size) (v)
83 #define be_bswap(v, size) bswap ## size(v)
84 #define le_bswaps(v, size)
85 #define be_bswaps(p, size) *p = bswap ## size(*p);
86 #endif
88 #define CPU_CONVERT(endian, size, type)\
89 static inline type endian ## size ## _to_cpu(type v)\
90 {\
91 return endian ## _bswap(v, size);\
92 }\
93 \
94 static inline type cpu_to_ ## endian ## size(type v)\
95 {\
96 return endian ## _bswap(v, size);\
97 }\
98 \
99 static inline void endian ## size ## _to_cpus(type *p)\
100 {\
101 endian ## _bswaps(p, size)\
102 }\
103 \
104 static inline void cpu_to_ ## endian ## size ## s(type *p)\
105 {\
106 endian ## _bswaps(p, size)\
107 }\
108 \
109 static inline type endian ## size ## _to_cpup(const type *p)\
110 {\
111 return endian ## size ## _to_cpu(*p);\
112 }\
113 \
114 static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
115 {\
116 *p = cpu_to_ ## endian ## size(v);\
117 }
119 CPU_CONVERT(be, 16, uint16_t)
120 CPU_CONVERT(be, 32, uint32_t)
121 CPU_CONVERT(be, 64, uint64_t)
123 CPU_CONVERT(le, 16, uint16_t)
124 CPU_CONVERT(le, 32, uint32_t)
125 CPU_CONVERT(le, 64, uint64_t)
127 /* unaligned versions (optimized for frequent unaligned accesses)*/
129 #if defined(__i386__) || defined(__powerpc__)
131 #define cpu_to_le16wu(p, v) cpu_to_le16w(p, v)
132 #define cpu_to_le32wu(p, v) cpu_to_le32w(p, v)
133 #define le16_to_cpupu(p) le16_to_cpup(p)
134 #define le32_to_cpupu(p) le32_to_cpup(p)
136 #define cpu_to_be16wu(p, v) cpu_to_be16w(p, v)
137 #define cpu_to_be32wu(p, v) cpu_to_be32w(p, v)
139 #else
141 static inline void cpu_to_le16wu(uint16_t *p, uint16_t v)
142 {
143 uint8_t *p1 = (uint8_t *)p;
145 p1[0] = v;
146 p1[1] = v >> 8;
147 }
149 static inline void cpu_to_le32wu(uint32_t *p, uint32_t v)
150 {
151 uint8_t *p1 = (uint8_t *)p;
153 p1[0] = v;
154 p1[1] = v >> 8;
155 p1[2] = v >> 16;
156 p1[3] = v >> 24;
157 }
159 static inline uint16_t le16_to_cpupu(const uint16_t *p)
160 {
161 const uint8_t *p1 = (const uint8_t *)p;
162 return p1[0] | (p1[1] << 8);
163 }
165 static inline uint32_t le32_to_cpupu(const uint32_t *p)
166 {
167 const uint8_t *p1 = (const uint8_t *)p;
168 return p1[0] | (p1[1] << 8) | (p1[2] << 16) | (p1[3] << 24);
169 }
171 static inline void cpu_to_be16wu(uint16_t *p, uint16_t v)
172 {
173 uint8_t *p1 = (uint8_t *)p;
175 p1[0] = v >> 8;
176 p1[1] = v;
177 }
179 static inline void cpu_to_be32wu(uint32_t *p, uint32_t v)
180 {
181 uint8_t *p1 = (uint8_t *)p;
183 p1[0] = v >> 24;
184 p1[1] = v >> 16;
185 p1[2] = v >> 8;
186 p1[3] = v;
187 }
189 #endif
191 #ifdef WORDS_BIGENDIAN
192 #define cpu_to_32wu cpu_to_be32wu
193 #else
194 #define cpu_to_32wu cpu_to_le32wu
195 #endif
197 #undef le_bswap
198 #undef be_bswap
199 #undef le_bswaps
200 #undef be_bswaps
202 #endif /* BSWAP_H */