Ptr and ForeignPtr Questions

Ashley Yakeley ashley@semantic.org
Wed, 19 Sep 2001 18:37:46 -0700


The documentation for FFI in the GHC user's guide seems to be out of date 
with regard to passing Ptrs across.

1. My reference is 
<http://www.haskell.org/ghc/docs/latest/set/ffi.html>
(from <http://www.haskell.org/ghc/docs/latest/set/book-users-guide.html>)
Is this the latest documentation available?

2. My understanding is that you can use any instance of 'Storable a => 
Ptr a' as an FFI argument and return type for both imported and exported 
functions? Is this correct? What if the type is polymorphic (e.g. 
declared as 'Storable a => Ptr a' rather than something like 'Ptr Word8')?

3. What about ForeignPtr? Can instances of 'Storable a => ForeignPtr a' 
be used in FFI?

4. Does newForeignPtr work safely with null pointers, and will the 
finalizer get called? For instance:

     fp <- newForeignPtr nullPtr finalFunc;
     let {isNull = (foreignPtrToPtr fp) == nullPtr};
     r <- withForeign fp (\p -> foo p);

Will foo be passed nullPtr? Will finalFunc ever get called? Is my use, 
above, of foreignPtrToPtr safe, and will isNull be True?


-- 
Ashley Yakeley, Seattle WA