Hello hackers...

Macros in attached patch are designed for doing life a little easier.

If one is using strictly defined types as uint8_t, uint16_t, int32_t, etc.
those macros are helpful IMHO, because futher value size changes does not
affects code for byte order managing. This also does not hit perfromance,
because this should be resolved at compile-time.

I'm not sure if dedicated epanic() is the best way to implement out-of-range
errors prevention - the more handy solution should cause compile error.

-- 
Pawel Jakub Dawidek                       [EMAIL PROTECTED]
UNIX Systems Programmer/Administrator     http://garage.freebsd.pl
Am I Evil? Yes, I Am!                     http://cerber.sourceforge.net
--- endian.h.orig       Sat Nov 22 13:15:40 2003
+++ endian.h    Mon Nov 24 10:57:02 2003
@@ -49,6 +49,46 @@
 #endif
  
 /*
+ * Size-independent byte order swapping functions.
+ */
+#ifdef _KERNEL
+#define        epanic(msg)     _epanic(msg, __FILE__, __LINE__)
+/* New function, because panic(9) type is void and this is not enough. */
+static __inline int
+_epanic(const char *msg, const char *file, unsigned line)
+{
+
+       panic("%s:%u: %s", file, line, msg);
+       /* NOTREACHED */
+}
+#define        bswap(x)        (sizeof(x) == 1 ? (x) = (x) :                   \
+                       (sizeof(x) == 2 ? bswap16(x) :                  \
+                       (sizeof(x) == 4 ? bswap32(x) :                  \
+                       (sizeof(x) == 8 ? bswap64(x) :                  \
+                       epanic("out of range in bswap()")))))
+#define        htobe(x)        (sizeof(x) == 1 ? (x) = (x) :                   \
+                       (sizeof(x) == 2 ? htobe16(x) :                  \
+                       (sizeof(x) == 4 ? htobe32(x) :                  \
+                       (sizeof(x) == 8 ? htobe64(x) :                  \
+                       epanic("out of range in htobe()")))))
+#define        htole(x)        (sizeof(x) == 1 ? (x) = (x) :                   \
+                       (sizeof(x) == 2 ? htole16(x) :                  \
+                       (sizeof(x) == 4 ? htole32(x) :                  \
+                       (sizeof(x) == 8 ? htole64(x) :                  \
+                       epanic("out of range in htole()")))))
+#define        betoh(x)        (sizeof(x) == 1 ? (x) = (x) :                   \
+                       (sizeof(x) == 2 ? betoh16(x) :                  \
+                       (sizeof(x) == 4 ? betoh32(x) :                  \
+                       (sizeof(x) == 8 ? betoh64(x) :                  \
+                       epanic("out of range in betoh()")))))
+#define        letoh(x)        (sizeof(x) == 1 ? (x) = (x) :                   \
+                       (sizeof(x) == 2 ? letoh16(x) :                  \
+                       (sizeof(x) == 4 ? letoh32(x) :                  \
+                       (sizeof(x) == 8 ? letoh64(x) :                  \
+                       epanic("out of range in letoh()")))))
+#endif
+
+/*
  * General byte order swapping functions.
  */
 #define        bswap16(x)      __bswap16(x)

Attachment: pgp00000.pgp
Description: PGP signature

Reply via email to