Size-independent byte order swapping functions.
Pawel Jakub Dawidek
nick at garage.freebsd.pl
Mon Nov 24 01:59:54 PST 2003
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 pawel at dawidek.net
UNIX Systems Programmer/Administrator http://garage.freebsd.pl
Am I Evil? Yes, I Am! http://cerber.sourceforge.net
-------------- next part --------------
--- 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)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 305 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20031124/da4edf7f/attachment.bin
More information about the freebsd-hackers
mailing list