Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 3 Feb 2004 02:48:44 -0800
From:      Luigi Rizzo <rizzo@icir.org>
To:        Pawel Jakub Dawidek <pjd@freebsd.org>
Cc:        freebsd-arch@freebsd.org
Subject:   Re: Size-independent byte order swapping functions.
Message-ID:  <20040203024844.A59792@xorpc.icir.org>
In-Reply-To: <20040203103804.GP4200@garage.freebsd.pl>; from pjd@freebsd.org on Tue, Feb 03, 2004 at 11:38:04AM %2B0100
References:  <20040203083444.GM4200@garage.freebsd.pl> <6923.1075802515@critter.freebsd.dk> <20040203103804.GP4200@garage.freebsd.pl>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, Feb 03, 2004 at 11:38:04AM +0100, Pawel Jakub Dawidek wrote:
...
> +> I have a hard time seeing a sensible use for these.
> +> 
> +> Endianess conversion is almost exclusively used in communications
> +> (even if the "transmission media" is a disk), and I can't possibly
> +> see how it can make sense to be lax about wordsize but strict about
> +> byteordering.

in fact, i'd rather have some types that prevent you from
making mistakes and carelessly copy values between incompatible types.
I am exploring ways to do this -- e.g. at the moment i am using this:

	#define N(x)    ((x).__x)
	#define H16(x)  (ntohs(N(x)))
	#define H32(x)  (ntohl(N(x)))
	#define N16(x)  (htons(x))
	#define N32(x)  (htonl(x))
	 
	struct _n32_t {
		uint32_t __x;
	};

	struct _n16_t {
		uint16_t __x;
	};
	typedef struct _n32_t   n32_t;  /* network format */
	typedef struct _n16_t   n16_t;  /* network format */

so that the compiler prevents me from assigning between
host and network representations.

	uint32_t	a, b;
	n32_t		c, d;

	c = d;		/* ok */
	a = b;		/* ok */
	c = b;		/* compiler complains */
	N(c) = N32(b);	/* ok */
	b = H32(c);	/* ok */
	c == b;		/* unfortunately does not work */
	N(c) == N(b);	/* ok, saves conversion */
	H32(c) == H32(b);	/* ok, requires conversion */

of course, the use of custom macros probably makes the code less
readable once one is used to the ntoh*() stuff...

	cheers
	luigi



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040203024844.A59792>