ForeignPtr query

Simon Marlow simonmar at
Wed Dec 4 10:43:12 EST 2002

> 1. part of this BDD library uses ints (not pointers) to identify
> garbage-collectable objects. there's nothing in the FFI to 
> cope with this AFAICS. i could:
> - hack the library (but would prefer not to).
> - say the functions in question deal with Ptr CInt, and hope 
> sizeof(int)
> == sizeof(int *) on all platforms i need to run this on (yuk).
> - plead for a general "stick this arbitrary (small) C object in an
> optimiser-impervious box"  generalisation of ForeignPtr.

There was a proposal for such a thing kicking around recently, and I
think there's no real problems with it.  Although you're the first
person to come along with a real application, to my knowledge.

Please feel free to mail ffi at and make a case, I'd say
there's a pretty good chance it'll get adopted.

> 2. how do i import a function pointer under ghc5.02? in 5.04 i say:
> foreign import ccall "&bdd_reorder_stable_window3"
>   bdd_reorder_stable_window3 :: FunPtr (BDDManager -> IO ())
> and that works (i.e. i can treat it like any other Haskell function).

5.02 had something called 'foreign label', which does the same thing.
Check the docs for details.

> 3. i'd like to see some array marshalling code that copes 
> with lists of
> ForeignPtrs, such as:
> -- | Marshall a list of ForeignPtrs. Same structure as withArray0.
> withForeignArray0 :: Ptr a
> 		  -> [ForeignPtr a]
> 		  -> (Ptr (Ptr a) -> IO b)
> 		  -> IO b
> withForeignArray0 marker vals f =
>   allocaArray0 len
>     (\ptr -> do pokeArray0 marker ptr $ map foreignPtrToPtr vals
>                 res <- f ptr
>                 mapM touchForeignPtr vals
>                 return res)
>   where len = length vals


  withForeignArray0 fps io =
     withMany withForeignPtr fps $ \ps ->
	 withArray0 nullPtr ps io

> the problem is it isn't as usefully polymorphic as the corresponding
> withArray0 function (e.g. consider [(ForeignPtr a, ForeignPtr 
> a)]). ideas?

What type do you want for it?


More information about the FFI mailing list