From owner-freebsd-hackers@FreeBSD.ORG Mon Nov 24 01:59:54 2003 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 603F416A4CE for ; Mon, 24 Nov 2003 01:59:54 -0800 (PST) Received: from milla.ask33.net (milla.ask33.net [217.197.166.60]) by mx1.FreeBSD.org (Postfix) with ESMTP id 5219043FB1 for ; Mon, 24 Nov 2003 01:59:53 -0800 (PST) (envelope-from nick@milla.ask33.net) Received: by milla.ask33.net (Postfix, from userid 1001) id 591733ABB2D; Mon, 24 Nov 2003 10:58:52 +0100 (CET) Date: Mon, 24 Nov 2003 10:58:52 +0100 From: Pawel Jakub Dawidek To: freebsd-hackers@freebsd.org Message-ID: <20031124095852.GZ511@garage.freebsd.pl> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-md5; protocol="application/pgp-signature"; boundary="jk/5KjyVsvxWSMIA" Content-Disposition: inline X-PGP-Key-URL: http://garage.freebsd.pl/jules.asc X-OS: FreeBSD 4.8-RELEASE-p13 i386 X-URL: http://garage.freebsd.pl User-Agent: Mutt/1.5.1i Subject: Size-independent byte order swapping functions. X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 24 Nov 2003 09:59:54 -0000 --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--