Minor tweaks to ffi addendum

Alastair Reid reid at cs.utah.edu
Mon Jun 3 11:20:10 EDT 2002

> We have to specify the constraint that the C finalizer can't
> directly or indirectly invoke any Haskell code (is this checkable at
> run time?).

It's the same constraint we place on 'unsafe' foreign imports.

I picture a typical usage being something like this:

  foreign import ccall unsafe "static & free" free :: Ptr a -> IO ()

  alloc x = do
    object <- malloc x
    newForeignPtr' object free 

It's easy to check correct use of the unsafe flag at runtime if we
want: set a flag when you make an unsafe call and test the flag in the
foreign export wrapper code.

We could imagine getting the Haskell typesystem to check that
newForeignPtr' is only used with unsafe free functions.  It'd be a bit
verbose but we could probably do it.  There's not much point though since
C can't do a similar job on its side.

Alastair Reid        reid at cs.utah.edu        http://www.cs.utah.edu/~reid/

More information about the FFI mailing list