Date: Mon, 24 Nov 2003 10:58:52 +0100 From: Pawel Jakub Dawidek <nick@garage.freebsd.pl> To: freebsd-hackers@freebsd.org Subject: Size-independent byte order swapping functions. Message-ID: <20031124095852.GZ511@garage.freebsd.pl>
next in thread | raw e-mail | index | archive | help
--jk/5KjyVsvxWSMIA Content-Type: multipart/mixed; boundary="McOwgVabxjWRBDh+" Content-Disposition: inline --McOwgVabxjWRBDh+ Content-Type: text/plain; charset=iso-8859-2 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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. --=20 Pawel Jakub Dawidek pawel@dawidek.net UNIX Systems Programmer/Administrator http://garage.freebsd.pl Am I Evil? Yes, I Am! http://cerber.sourceforge.net --McOwgVabxjWRBDh+ Content-Type: text/plain; charset=iso-8859-2 Content-Disposition: attachment; filename="endian.h.patch" Content-Transfer-Encoding: quoted-printable --- endian.h.orig Sat Nov 22 13:15:40 2003 +++ endian.h Mon Nov 24 10:57:02 2003 @@ -49,6 +49,46 @@ #endif =20 /* + * 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) =3D=3D 1 ? (x) =3D (x) : \ + (sizeof(x) =3D=3D 2 ? bswap16(x) : \ + (sizeof(x) =3D=3D 4 ? bswap32(x) : \ + (sizeof(x) =3D=3D 8 ? bswap64(x) : \ + epanic("out of range in bswap()"))))) +#define htobe(x) (sizeof(x) =3D=3D 1 ? (x) =3D (x) : \ + (sizeof(x) =3D=3D 2 ? htobe16(x) : \ + (sizeof(x) =3D=3D 4 ? htobe32(x) : \ + (sizeof(x) =3D=3D 8 ? htobe64(x) : \ + epanic("out of range in htobe()"))))) +#define htole(x) (sizeof(x) =3D=3D 1 ? (x) =3D (x) : \ + (sizeof(x) =3D=3D 2 ? htole16(x) : \ + (sizeof(x) =3D=3D 4 ? htole32(x) : \ + (sizeof(x) =3D=3D 8 ? htole64(x) : \ + epanic("out of range in htole()"))))) +#define betoh(x) (sizeof(x) =3D=3D 1 ? (x) =3D (x) : \ + (sizeof(x) =3D=3D 2 ? betoh16(x) : \ + (sizeof(x) =3D=3D 4 ? betoh32(x) : \ + (sizeof(x) =3D=3D 8 ? betoh64(x) : \ + epanic("out of range in betoh()"))))) +#define letoh(x) (sizeof(x) =3D=3D 1 ? (x) =3D (x) : \ + (sizeof(x) =3D=3D 2 ? letoh16(x) : \ + (sizeof(x) =3D=3D 4 ? letoh32(x) : \ + (sizeof(x) =3D=3D 8 ? letoh64(x) : \ + epanic("out of range in letoh()"))))) +#endif + +/* * General byte order swapping functions. */ #define bswap16(x) __bswap16(x) --McOwgVabxjWRBDh+-- --jk/5KjyVsvxWSMIA Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.7 (FreeBSD) iQCVAwUBP8HWXD/PhmMH/Mf1AQHNVQP9E2e8VNOXaxznkW3flpMHRkwoQFgSPde1 Hc+xdhY2Vv2AIqyMEnaS4x2ocUk9npt/+/B+C7d6rqXrN6s89P7faKtjHzoWwmJC HOXn1n/3lUvXXXwf5Vl42DxA555RL7YbYI3+9hoOqFHwlHQvDDa4wh+YFf9CC+jx lRoQKVhhBxQ= =kSg1 -----END PGP SIGNATURE----- --jk/5KjyVsvxWSMIA--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20031124095852.GZ511>