Effect of large binaries on garbage collection

Simon Marlow simonmar@microsoft.com
Mon, 10 Mar 2003 12:11:04 -0000

> > Thanks, looks like it's option (1) then. Could you tell me what
> > Haskell type I have to use be able to pass a pointer to this binary
> > to C land (genuine address, not StablePtr). I don't think
> > the standard FFI allows this at all but, IIRC, the old ghc libraries
> > allowed you to do this with a mutable byte array.
> Does anybody know the answer to this? please.. pretty please..:-)
> Sorry if this is a case of me failing to RTFM, but I don't
> think it is. Paragraph 8.1.1 of the users guide says..
>  "The types ByteArray and MutableByteArray may be used as basic
>   foreign types (see FFI Addendum, Section 3.2). In C land,
>   they map to (char *)."

This is still true for 5.04.x: the ByteArray and MutableArray libs in
hslibs (package lang) provide these types, but they are deprecated and
will be removed at some point.  The functionality which allows ByteArray
and MutableByteArrays to be passed to FFI functions has already been
removed in CVS.

In the current CVS GHC, undoubtedly the right thing to use is
Foreign.mallocForeignPtr.  Internally these are implemented as
MutableByteArray#, so you get fast allocation and GC, but from the
programmer's point of view it's a normal ForeignPtr.

If you don't mind the not-so-great performance, you can use ForeignPtr
with malloc/free in 5.04.x and convert to mallocForeignPtr when the next
major release of GHC comes along.  Or you can use
ByteArray/MutableByteArray for the time being (note that you can only
pass one of these to an "unsafe" FFI function, BTW).