ForeignPtr query

Peter Gammie peteg at cse.unsw.EDU.AU
Tue Dec 3 01:53:21 EST 2002

g'day everyone,

as Manuel said before, i've been trying to give a BDD library a pretty
Haskell interface (give me a shout if this is of interest to you). i have
a few queries:

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.

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).

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

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

IMHO, as many common usage patterns requiring touchForeignPtr as possible
should be stuck in the library... what form this might take remains to be


More information about the FFI mailing list