Re: building and analysing C structs with Storable

Stefan O'Rear <[email protected]> Sun, 14 Oct 2007 12:23:00 -0700
Newsgroups gmane.comp.lang.haskell.libraries,gmane.comp.lang.haskell.ffi
Message-ID <[email protected]>
--===============0748063348==
Content-Type: multipart/signed; micalg=pgp-sha1;
	protocol="application/pgp-signature"; boundary="dDRMvlgZJXvWKvBx"
Content-Disposition: inline


--dDRMvlgZJXvWKvBx
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sun, Oct 14, 2007 at 09:19:24PM +0200, Henning Thielemann wrote:
>
> On Sat, 13 Oct 2007, Adam Langley wrote:
>
>> On 10/13/07, Henning Thielemann <[email protected]> wrote:
>>> Are there analogons to ByteString's Put and Get Monads to construct and
>>> disect binary data in a C struct for exchange with a C program?
>>
>> The memory layout of structs depends on the ABI of system. You can
>> take a guess and write code in the Get and Put monads. Your guess will
>> probably work pretty well within a single architecture (e.g. x86 or
>> x86-64).
>>
>> However, the portable way to do this is to get the information from
>> the C compiler, which is the approach that c2hs[1] and friends use. I
>> would recommend this unless there's some good reason to think
>> otherwise. (see [2] for the exact hook)
>>
>> [1] http://www.cse.unsw.edu.au/~chak/haskell/c2hs/
>> [2] http://www.cse.unsw.edu.au/~chak/haskell/c2hs/docu/c2hs-2.html#ss2.8
>
> I do not quite see the need for an extra tool. I thought it must be=20
> possible to ship a Haskell compiler with modules that depend on the=20
> system's C compiler, just like the modules that are implemented different=
ly=20
> for Windows and Unix. Such modules could provide the functionality to=20
> create and inspect C structs for exchange with system libraries.

You say "the system's C compiler" as if there was only one.  It's quite
common for UNIXoid systems to have several C compilers installed
simultaneously; and if you use the module corresponding to the wrong
compiler, you get silent data loss.  I wouldn't risk it.

Stefan

--dDRMvlgZJXvWKvBx
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHEmyUFBz7OZ2P+dIRAukhAKDKgPNkg7VGVgCfM1lP0+hOstDTxACeOYBj
ocwzk4dgKD5Ap+o5tm38WJM=
=igOX
-----END PGP SIGNATURE-----

--dDRMvlgZJXvWKvBx--

--===============0748063348==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Libraries mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/libraries

--===============0748063348==--