[Haskell-cafe] Keeping a set of ForeignPtr's alive
felipe.lessa at gmail.com
Mon Jul 21 12:56:16 EDT 2008
-- Background: --
I have some foreign objects that are mapped to ForeignPtr's, say
newtype Obj = O (ForeignPtr Obj)
and a foreign "collection" where those objects are added. The adding
is something on the lines of
addObj, removeObj :: Collection -> Obj -> IO ()
id est, the C code of the collection maintains a reference to all
objects added. That means I have to keep all the ForeignPtr's added
alive and that I must not prevent garbage collection of those removed.
Of course the 'Collection' isn't just a simple collection, otherwise I
would be using normal Haskell counterparts.
-- Question: --
What is the best way to keep the added objects alive? Using a list
will keep the addition cheap and a low memory overhead, while a
Data.Set will make removeObj work in sublinear time (on the Haskell
side). Are there any other options? I'm currently heading towards
data Collection = C !(ForeignPtr Collection) !(IORef [Obj])
like ForeignPtr itself (although in the case of ForeignPtr's there is
no way to remove finalizers).
More information about the Haskell-Cafe