RISC Machine Data Structure Word Alignment Problems?

Desikan Venkatrangan venkat at matrix.UUCP
Fri Feb 2 04:20:32 AEST 1990


In article <1990Jan21.224826.1699 at esegue.segue.boston.ma.us> johnl at esegue.segue.boston.ma.us (John R. Levine) writes:
>From article <111 at melpar.UUCP>, by toppin at melpar.UUCP (Doug Toppin   X2075):
>> We are using the SUN 4/260 which is a RISC architecture machine.
>> We are having trouble with data alignment in our data structures.

and suggests:

>              You need to write something like this:
>
>read_foo_structure(struct foo *p)
>{
>	p->a = read_long();
>	p->b = read_short();
>	p->c = read_long();
>}
>
>long read_long(void)
>{
>	long v;
>
>	/* read in big endian order */
>	v = getc(f) << 24;	/* should do some error checking */
>	v |= getc(f) << 16;
>	v |= getc(f) << 8;
>	v |= getc(f);
>	return v;
>}
>
>This may seem like more work, but in my experience you write a few of these
>things and use them all over the place.  Then your code is really portable.
>-- 
For complete portablility and ease of maintenance, try to pattern such 
routines along the External Data Representation (XDR) as SUN has done, 
for support of the RPC mechanism.  You will be writing

bool_t
xdr_foo(xdrs, objp)
register XDR *xdrs;
register objtype *opbjp;
{
	return (xdr_long(xdrs, &objp->a) &&
		xdr_short(xdrs, &objp->b) &&
		xdr_long(xdrs, &obj->c));
}

This way, both read and write operations can be done using the same routines;
(with proper setting of XDR_ENCODE/XDR_DECODE in xdrs.)  Also, the read/write
can be from memory or a file.

The xdr routines for the premitive types are provided by SUN.  But they have
chosen to represent 'shorts' as 4-byte quantities externally.  If you wish to
avoid this and prefer little-endian representation, you should write similar
routines yourself.

The utility rpcgen may be useful as well.



More information about the Comp.lang.c mailing list