Pawel Jakub Dawidek | 3 Feb 2004 11:38
Picon
Favicon

Re: Size-independent byte order swapping functions.

On Tue, Feb 03, 2004 at 11:01:55AM +0100, Poul-Henning Kamp wrote:
+> >I'm planning to commit this patch:
+> >
+> >	http://garage.freebsd.pl/patches/endian.h.patch
+> 
+> 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.
+> 
+> Could you please tell us what you need these for and why you could
+> not use the explicitly sized families of endian functions ?

I found them very useful while doing many such translations.
It protect from problems when you need to manage many such transformations.

For example, you have some structure:

struct mystruct {
	uint16_t	ms_foo;
	uint32_t	ms_bar;
	uint64_t	ms_foobar;
};

and many places where you translate those fields.
Suddenly, you need to change size of one of those fields.
If you were using size-independent functions you don't need to change
anything else, in other case diff will be much bigger with much
(Continue reading)

Luigi Rizzo | 3 Feb 2004 11:48

Re: Size-independent byte order swapping functions.

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
(Continue reading)


Gmane