Proposed change to ForeignPtr
Alastair Reid
alastair at reid-consulting-uk.ltd.uk
Fri Aug 9 14:39:36 EDT 2002
Alastair wrote [snip]
> makeForeignPtr :: Ptr a -> FunPtr (Ptr a -> IO ()) -> IO ForeignObj
> [snip] I don't understand this proposal. What is a ForeignObj?
Sorry, that was a typo. The result type should be
IO (ForeignPtr a)
> I call a C function, which gives me a cString :: Ptr CChar, and it's
> my responsibility to free it when I've finished with it. So I
> convert it to a ForeignPtr:
> foreignPtr <- mkForeignPtr cString
> and then always refer to the pointer via foreignPtr. When
> foreignPtr is garbage collected the space is freed. So how do I do
> this with your proposal?
With the existing spec, you would write:
> foreign import free :: Ptr CChar -> IO ()
>
> foo = do
> ...
> foreignPtr <- newForeignPtr cString (free cString)
> ...
With my proposal, you would write:
> foreign import "&" free :: FunPtr (Ptr CChar -> IO ())
>
> foo = do
> ...
> foreignPtr <- newForeignPtr cString free
> ...
All the rest of your code to manipulate ForeignPtrs remains the same.
(Well, there's a corresponding change in addForeignFinalizer if you
happen to use that.)
--
Alastair Reid alastair at reid-consulting-uk.ltd.uk
Reid Consulting (UK) Limited http://www.reid-consulting-uk.ltd.uk/alastair/
More information about the FFI
mailing list