Create and use __CAST(type, value) in headers so that modern c++ with

-Wold-style-casts does not bitch.
This commit is contained in:
christos 2009-08-08 21:23:15 +00:00
parent 065a21e041
commit ff2fe63754
4 changed files with 77 additions and 67 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: bswap.h,v 1.15 2009/07/20 18:45:27 joerg Exp $ */
/* $NetBSD: bswap.h,v 1.16 2009/08/08 21:23:15 christos Exp $ */
/* Written by Manuel Bouyer. Public domain */
@ -39,7 +39,7 @@ __END_DECLS
#endif
#define __byte_swap_u64_constant(x) \
((uint64_t) \
(__CAST(uint64_t, \
((((x) & 0xff00000000000000ull) >> 56) | \
(((x) & 0x00ff000000000000ull) >> 40) | \
(((x) & 0x0000ff0000000000ull) >> 24) | \
@ -47,19 +47,19 @@ __END_DECLS
(((x) & 0x00000000ff000000ull) << 8) | \
(((x) & 0x0000000000ff0000ull) << 24) | \
(((x) & 0x000000000000ff00ull) << 40) | \
(((x) & 0x00000000000000ffull) << 56)))
(((x) & 0x00000000000000ffull) << 56))))
#define __byte_swap_u32_constant(x) \
((uint32_t) \
(__CAST(uint32_t, \
((((x) & 0xff000000) >> 24) | \
(((x) & 0x00ff0000) >> 8) | \
(((x) & 0x0000ff00) << 8) | \
(((x) & 0x000000ff) << 24)))
(((x) & 0x000000ff) << 24))))
#define __byte_swap_u16_constant(x) \
((uint16_t) \
(__CAST(uint16_t, \
((((x) & 0xff00) >> 8) | \
(((x) & 0x00ff) << 8)))
(((x) & 0x00ff) << 8))))
#define bswap64(x) \
(__builtin_constant_p((x)) ? \

View File

@ -1,4 +1,4 @@
/* $NetBSD: cdefs.h,v 1.76 2009/07/20 17:46:04 joerg Exp $ */
/* $NetBSD: cdefs.h,v 1.77 2009/08/08 21:23:15 christos Exp $ */
/*
* Copyright (c) 1991, 1993
@ -434,4 +434,14 @@
#define __SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))
#define __SHIFTOUT_MASK(__mask) __SHIFTOUT((__mask), (__mask))
/*
* Only to be used in other headers that are included from both c or c++
* NOT to be used in code.
*/
#ifdef __cplusplus
#define __CAST(__dt, __st) static_cast<__dt>(__st)
#else
#define __CAST(__dt, __st) ((__dt)(__st))
#endif
#endif /* !_SYS_CDEFS_H_ */

View File

@ -1,4 +1,4 @@
/* $NetBSD: endian.h,v 1.27 2009/07/20 18:45:27 joerg Exp $ */
/* $NetBSD: endian.h,v 1.28 2009/08/08 21:23:15 christos Exp $ */
/*
* Copyright (c) 1987, 1991, 1993
@ -122,15 +122,15 @@ __END_DECLS
#else /* LITTLE_ENDIAN || !defined(__lint__) */
#define ntohl(x) bswap32((uint32_t)(x))
#define ntohs(x) bswap16((uint16_t)(x))
#define htonl(x) bswap32((uint32_t)(x))
#define htons(x) bswap16((uint16_t)(x))
#define ntohl(x) bswap32(__CAST(uint32_t, (x)))
#define ntohs(x) bswap16(__CAST(uint16_t, (x)))
#define htonl(x) bswap32(__CAST(uint32_t, (x)))
#define htons(x) bswap16(__CAST(uint16_t, (x)))
#define NTOHL(x) (x) = ntohl((uint32_t)(x))
#define NTOHS(x) (x) = ntohs((uint16_t)(x))
#define HTONL(x) (x) = htonl((uint32_t)(x))
#define HTONS(x) (x) = htons((uint16_t)(x))
#define NTOHL(x) (x) = ntohl(__CAST(uint32_t, (x)))
#define NTOHS(x) (x) = ntohs(__CAST(uint16_t, (x)))
#define HTONL(x) (x) = htonl(__CAST(uint32_t, (x)))
#define HTONS(x) (x) = htons(__CAST(uint16_t, (x)))
#endif /* LITTLE_ENDIAN || !defined(__lint__) */
/*
@ -142,32 +142,32 @@ __END_DECLS
#define htobe16(x) (x)
#define htobe32(x) (x)
#define htobe64(x) (x)
#define htole16(x) bswap16((uint16_t)(x))
#define htole32(x) bswap32((uint32_t)(x))
#define htole64(x) bswap64((uint64_t)(x))
#define htole16(x) bswap16(__CAST(uint16_t, (x)))
#define htole32(x) bswap32(__CAST(uint32_t, (x)))
#define htole64(x) bswap64(__CAST(uint64_t, (x)))
#define HTOBE16(x) (void) (x)
#define HTOBE32(x) (void) (x)
#define HTOBE64(x) (void) (x)
#define HTOLE16(x) (x) = bswap16((uint16_t)(x))
#define HTOLE32(x) (x) = bswap32((uint32_t)(x))
#define HTOLE64(x) (x) = bswap64((uint64_t)(x))
#define HTOBE16(x) __CAST(void, (x))
#define HTOBE32(x) __CAST(void, (x))
#define HTOBE64(x) __CAST(void, (x))
#define HTOLE16(x) (x) = bswap16(__CAST(uint16_t, (x)))
#define HTOLE32(x) (x) = bswap32(__CAST(uint32_t, (x)))
#define HTOLE64(x) (x) = bswap64(__CAST(uint64_t, (x)))
#else /* LITTLE_ENDIAN */
#define htobe16(x) bswap16((uint16_t)(x))
#define htobe32(x) bswap32((uint32_t)(x))
#define htobe64(x) bswap64((uint64_t)(x))
#define htobe16(x) bswap16(__CAST(uint16_t, (x)))
#define htobe32(x) bswap32(__CAST(uint32_t, (x)))
#define htobe64(x) bswap64(__CAST(uint64_t, (x)))
#define htole16(x) (x)
#define htole32(x) (x)
#define htole64(x) (x)
#define HTOBE16(x) (x) = bswap16((uint16_t)(x))
#define HTOBE32(x) (x) = bswap32((uint32_t)(x))
#define HTOBE64(x) (x) = bswap64((uint64_t)(x))
#define HTOLE16(x) (void) (x)
#define HTOLE32(x) (void) (x)
#define HTOLE64(x) (void) (x)
#define HTOBE16(x) (x) = bswap16(__CAST(uint16_t, (x)))
#define HTOBE32(x) (x) = bswap32(__CAST(uint32_t, (x)))
#define HTOBE64(x) (x) = bswap64(__CAST(uint64_t, (x)))
#define HTOLE16(x) __CAST(void, (x))
#define HTOLE32(x) __CAST(void, (x))
#define HTOLE64(x) __CAST(void, (x))
#endif /* LITTLE_ENDIAN */
@ -230,63 +230,63 @@ __GEN_ENDIAN_DEC(64, le)
static __inline void __unused
be16enc(void *buf, uint16_t u)
{
uint8_t *p = (uint8_t *)buf;
uint8_t *p = __CAST(uint8_t *, buf);
p[0] = (uint8_t)(((unsigned)u >> 8) & 0xff);
p[1] = (uint8_t)(u & 0xff);
p[0] = __CAST(uint8_t, ((__CAST(unsigned, u) >> 8) & 0xff));
p[1] = __CAST(uint8_t, (u & 0xff));
}
static __inline void __unused
le16enc(void *buf, uint16_t u)
{
uint8_t *p = (uint8_t *)buf;
uint8_t *p = __CAST(uint8_t *, buf);
p[0] = (uint8_t)(u & 0xff);
p[1] = (uint8_t)(((unsigned)u >> 8) & 0xff);
p[0] = __CAST(uint8_t, (u & 0xff));
p[1] = __CAST(uint8_t, ((__CAST(unsigned, u) >> 8) & 0xff));
}
static __inline uint16_t __unused
be16dec(const void *buf)
{
const uint8_t *p = (const uint8_t *)buf;
const uint8_t *p = __CAST(const uint8_t *, buf);
return (uint16_t)((p[0] << 8) | p[1]);
return __CAST(uint16_t, ((p[0] << 8) | p[1]));
}
static __inline uint16_t __unused
le16dec(const void *buf)
{
const uint8_t *p = (const uint8_t *)buf;
const uint8_t *p = __CAST(const uint8_t *, buf);
return (uint16_t)((p[1] << 8) | p[0]);
return __CAST(uint16_t, ((p[1] << 8) | p[0]));
}
static __inline void __unused
be32enc(void *buf, uint32_t u)
{
uint8_t *p = (uint8_t *)buf;
uint8_t *p = __CAST(uint8_t *, buf);
p[0] = (uint8_t)((u >> 24) & 0xff);
p[1] = (uint8_t)((u >> 16) & 0xff);
p[2] = (uint8_t)((u >> 8) & 0xff);
p[3] = (uint8_t)(u & 0xff);
p[0] = __CAST(uint8_t, ((u >> 24) & 0xff));
p[1] = __CAST(uint8_t, ((u >> 16) & 0xff));
p[2] = __CAST(uint8_t, ((u >> 8) & 0xff));
p[3] = __CAST(uint8_t, (u & 0xff));
}
static __inline void __unused
le32enc(void *buf, uint32_t u)
{
uint8_t *p = (uint8_t *)buf;
uint8_t *p = __CAST(uint8_t *, buf);
p[0] = (uint8_t)(u & 0xff);
p[1] = (uint8_t)((u >> 8) & 0xff);
p[2] = (uint8_t)((u >> 16) & 0xff);
p[3] = (uint8_t)((u >> 24) & 0xff);
p[0] = __CAST(uint8_t, (u & 0xff));
p[1] = __CAST(uint8_t, ((u >> 8) & 0xff));
p[2] = __CAST(uint8_t, ((u >> 16) & 0xff));
p[3] = __CAST(uint8_t, ((u >> 24) & 0xff));
}
static __inline uint32_t __unused
be32dec(const void *buf)
{
const uint8_t *p = (const uint8_t *)buf;
const uint8_t *p = __CAST(const uint8_t *, buf);
return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
}
@ -294,7 +294,7 @@ be32dec(const void *buf)
static __inline uint32_t __unused
le32dec(const void *buf)
{
const uint8_t *p = (const uint8_t *)buf;
const uint8_t *p = __CAST(const uint8_t *, buf);
return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
}
@ -302,19 +302,19 @@ le32dec(const void *buf)
static __inline void __unused
be64enc(void *buf, uint64_t u)
{
uint8_t *p = (uint8_t *)buf;
uint8_t *p = __CAST(uint8_t *, buf);
be32enc(p, (uint32_t)(u >> 32));
be32enc(p + 4, (uint32_t)(u & 0xffffffffULL));
be32enc(p, __CAST(uint32_t, (u >> 32)));
be32enc(p + 4, __CAST(uint32_t, (u & 0xffffffffULL)));
}
static __inline void __unused
le64enc(void *buf, uint64_t u)
{
uint8_t *p = (uint8_t *)buf;
uint8_t *p = __CAST(uint8_t *, buf);
le32enc(p, (uint32_t)(u & 0xffffffffULL));
le32enc(p + 4, (uint32_t)(u >> 32));
le32enc(p, __CAST(uint32_t, (u & 0xffffffffULL)));
le32enc(p + 4, __CAST(uint32_t, (u >> 32)));
}
static __inline uint64_t __unused
@ -322,7 +322,7 @@ be64dec(const void *buf)
{
const uint8_t *p = (const uint8_t *)buf;
return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
return ((__CAST(uint64_t, be32dec(p)) << 32) | be32dec(p + 4));
}
static __inline uint64_t __unused
@ -330,7 +330,7 @@ le64dec(const void *buf)
{
const uint8_t *p = (const uint8_t *)buf;
return (le32dec(p) | ((uint64_t)le32dec(p + 4) << 32));
return (le32dec(p) | (__CAST(uint64_t, le32dec(p + 4)) << 32));
}
#endif /* GCC >= 2.95 */

View File

@ -1,4 +1,4 @@
/* $NetBSD: termios.h,v 1.29 2005/12/11 12:25:21 christos Exp $ */
/* $NetBSD: termios.h,v 1.30 2009/08/08 21:23:15 christos Exp $ */
/*
* Copyright (c) 1988, 1989, 1993, 1994
@ -78,7 +78,7 @@
#endif
#define NCCS 20
#define _POSIX_VDISABLE ((unsigned char)'\377')
#define _POSIX_VDISABLE __CAST(unsigned char, '\377')
#if defined(_NETBSD_SOURCE)
#define CCEQ(val, c) (c == val ? val != _POSIX_VDISABLE : 0)