ForeignPtr's - why can't they be passed directly to foreign functions?

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Wed Mar 15 12:16:37 EST 2006


On Wed, 2006-03-15 at 17:01 +0000, Brian Hulley wrote:
> Simon Marlow wrote:

> > This is the way it used to be in GHC before the FFI.  In the FFI we
> > moved to withForeignPtr instead.  IIRC, the motivation was something
> > along these lines:
> >
> >  - allowing ForeignPtr to be passed directly to a foreign function
> >    implies some magic that happens at the point of the foreign call
> >    to convert the ForeignPtr to a Ptr.
> >
> >  - there also has to be some magic to ensure that the ForeignPtr
> >    couldn't be finalized until the call returns.  This amounted to
> >    adding a touch# primitive to keep the ForeignPtr alive over the
> >    call. So internally the compiler was doing something like
> >    withForeignPtr anyway.  This behaviour is quite hard to explain in
> >    the spec, withForeignPtr is much simpler.
> 
> 
> The above two things were what I was expecting the compiler to do for me 
> instead of me having to manually write a wrapper function in Haskell using 
> withForeignPtr for each wrapper function for each foreign function...

This is exactly the sort of thing that FFI tools automate. Indeed they
can do this and much more.

Duncan



More information about the Glasgow-haskell-users mailing list