tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: type behavior of sys/endian.h



On Sun, Jul 12, 2009 at 04:54:11PM +0000, David Holland wrote:
> Or maybe they should be made inline functions instead so they don't
> fail silently if passed pointers.

Any objections to the attached patch?

Joerg
Index: endian.h
===================================================================
RCS file: /home/joerg/repo/netbsd/src/sys/sys/endian.h,v
retrieving revision 1.27
diff -u -p -r1.27 endian.h
--- endian.h    20 Jul 2009 18:45:27 -0000      1.27
+++ endian.h    21 Jul 2009 19:41:16 -0000
@@ -106,14 +106,32 @@ __END_DECLS
 
 #include <machine/bswap.h>
 
+static __inline __unused __constfunc uint16_t
+__ident16(uint16_t __v)
+{
+       return __v;
+}
+
+static __inline __unused __constfunc uint32_t
+__ident32(uint32_t __v)
+{
+       return __v;
+}
+
+static __inline __unused __constfunc uint64_t
+__ident64(uint64_t __v)
+{
+       return __v;
+}
+
 /*
  * Macros for network/external number representation conversion.
  */
 #if BYTE_ORDER == BIG_ENDIAN && !defined(__lint__)
-#define        ntohl(x)        (x)
-#define        ntohs(x)        (x)
-#define        htonl(x)        (x)
-#define        htons(x)        (x)
+#define        ntohl(x)        __ident32(x)
+#define        ntohs(x)        __ident16(x)
+#define        htonl(x)        __ident32(x)
+#define        htons(x)        __ident16(x)
 
 #define        NTOHL(x)        (void) (x)
 #define        NTOHS(x)        (void) (x)
@@ -139,9 +157,9 @@ __END_DECLS
 
 #if BYTE_ORDER == BIG_ENDIAN
 
-#define htobe16(x)     (x)
-#define htobe32(x)     (x)
-#define htobe64(x)     (x)
+#define htobe16(x)     __ident16(x)
+#define htobe32(x)     __ident32(x)
+#define htobe64(x)     __ident64(x)
 #define htole16(x)     bswap16((uint16_t)(x))
 #define htole32(x)     bswap32((uint32_t)(x))
 #define htole64(x)     bswap64((uint64_t)(x))
@@ -158,9 +176,9 @@ __END_DECLS
 #define htobe16(x)     bswap16((uint16_t)(x))
 #define htobe32(x)     bswap32((uint32_t)(x))
 #define htobe64(x)     bswap64((uint64_t)(x))
-#define htole16(x)     (x)
-#define htole32(x)     (x)
-#define htole64(x)     (x)
+#define htole16(x)     __ident16(x)
+#define htole32(x)     __ident32(x)
+#define htole64(x)     __ident64(x)
 
 #define HTOBE16(x)     (x) = bswap16((uint16_t)(x))
 #define HTOBE32(x)     (x) = bswap32((uint32_t)(x))


Home | Main Index | Thread Index | Old Index