From owner-freebsd-arch@FreeBSD.ORG Tue Feb 3 02:48:47 2004 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 14CCA16A4CE; Tue, 3 Feb 2004 02:48:47 -0800 (PST) Received: from xorpc.icir.org (xorpc.icir.org [192.150.187.68]) by mx1.FreeBSD.org (Postfix) with ESMTP id 443FB43D46; Tue, 3 Feb 2004 02:48:45 -0800 (PST) (envelope-from rizzo@icir.org) Received: from xorpc.icir.org (localhost [127.0.0.1]) by xorpc.icir.org (8.12.9p1/8.12.8) with ESMTP id i13AmiAF059915; Tue, 3 Feb 2004 02:48:44 -0800 (PST) (envelope-from rizzo@xorpc.icir.org) Received: (from rizzo@localhost) by xorpc.icir.org (8.12.9p1/8.12.3/Submit) id i13AmiS6059914; Tue, 3 Feb 2004 02:48:44 -0800 (PST) (envelope-from rizzo) Date: Tue, 3 Feb 2004 02:48:44 -0800 From: Luigi Rizzo To: Pawel Jakub Dawidek Message-ID: <20040203024844.A59792@xorpc.icir.org> References: <20040203083444.GM4200@garage.freebsd.pl> <6923.1075802515@critter.freebsd.dk> <20040203103804.GP4200@garage.freebsd.pl> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5.1i In-Reply-To: <20040203103804.GP4200@garage.freebsd.pl>; from pjd@freebsd.org on Tue, Feb 03, 2004 at 11:38:04AM +0100 cc: Poul-Henning Kamp cc: freebsd-arch@freebsd.org Subject: Re: Size-independent byte order swapping functions. X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 03 Feb 2004 10:48:47 -0000 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