Let's get this finished
Simon Marlow
simonmar at microsoft.com
Mon Jan 15 08:22:21 EST 2001
> On Mon, 15 Jan 2001, Simon Marlow wrote:
>
> > data UnsafeCString
> > withUnsafeCString :: String -> (UnsafeCString -> IO a) -> IO a
> >
> > where an UnsafeCString is valid only in an argument position of an
> > unsafe foreign import.
>
> Much of the conversion stuff would have to be done in a different way.
> An additional internal interface to conversions implemented in C
> (with ByteArrays instead of Ptrs), pointer arithmetic moved from
> Haskell to C, and in future hard to mix with Handle I/O done on
> Ptrs.
Hmm. You're right, unfortunately :( This is depressing.
> > I still can't think of a good way to do this in general. Perhaps
> > enhancing the garbage collector so that it could "pin" objects - but
> > you've still got the problem of keeping the lifetime of the
> ByteArray
> > in sync with the Ptr.
>
> Lifetime is not a problem:
> withByteArray :: ByteArray -> (Ptr a -> IO b) -> IO b
>
> (And now we know that the ByteArray type should be parametrized!)
Well, the reason I said this was a problem was because I was trying to
avoid the overhead of the exception handler or continuation in
withByteArray.
Actually we can do this right now: you allocate a large ByteArray (>= 4k
ish) to make sure it gets allocated in immovable space, and then use
touch# to keep it alive. You get through a lot of memory this way, but
it should be faster than malloc/free. I'll do some experiments.
Cheers,
Simon
More information about the FFI
mailing list