On portable C programming (was: libraw1394 struct layouts...)

VirginSnow at vfemail.net VirginSnow at vfemail.net
Thu Jan 8 11:25:53 EST 2009


> From: Jim Kuzdrall <gnhlug at intrel.com>
> Organization: Intrel
> Date: Thu, 8 Jan 2009 08:20:12 -0500

> On Thursday 08 January 2009 00:06, Ben Scott wrote:
> > On Wed, Jan 7, 2009 at 11:31 PM,  <VirginSnow at vfemail.net> wrote:
> > > So what's the recommended way to do this?
> >
> >   I dunno that there really is any really good way.
> 
>     There is a solution to the problem, I believe, if the structs are 
> data structures that you wish to be common to several environments and 
> processors.

Sure there is.  Just write every program in Java. ;^)

>     The solution starts with a program for which the structs are 
> working.  These structs are then read, one element at a time, by a 
> program that converts them to a "universal" format, creating a 
> universal struct.  These functions are readily compiled on any system 
> without any adjustments, as it turns out.
> 
>     Once created, the structs go into a "universal" struct library which 
> can be used various ways, depending on your needs.  Your programs must 
> explicitely access all structs via a pointer array, constructed each 
> time the program loads.  The program which uses the struct first 
> translates the universal struct to its native format.  Then it replaces 
> the pointer the compiler put in the array with the location of the 
> translated struct.

While this could work for userland code, you might run into trouble in
kernel code.  How would you access the "struct library" from the
kernel?  And (more importantly!) how would you convince the kernel
developers to adopt such a practice?

Perhaps, if the universal<->native translation was done by a
preprocessor, you could have say scsi_ioctl.uni -> scsi_ioctl.h, which
could then be #included in the source.  That might be a little less
cumbersome.

I don't think having *.uni->*.h would solve endian issues, but endian
issues generally aren't a problem in the kernel.  (With some notable
exceptions: struct udphdr, struct nbd_request, etc.)


More information about the gnhlug-discuss mailing list