Re: Proposed change to ForeignPtr
Alastair wrote [snip]
I propose the following changes:
1) Add these functions:
makeForeignPtr :: Ptr a -> FunPtr (Ptr a -> IO ()) -> IO ForeignObj attachForeignPtrFinalizer :: ForeignPtr a -> FunPtr (Ptr a -> IO ()) -> IO ()
2) Remove newForeignPtr and addForeignPtrFinalizer [snip] I don't understand this proposal. What is a ForeignObj?
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?
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@reid-consulting-uk.ltd.uk Reid Consulting (UK) Limited http://www.reid-consulting-uk.ltd.uk/alastair/
participants (2)
-
Alastair Reid -
George Russell