[Haskell-cafe] How can I pass IOUArrays to FFI functions?

Stefan O'Rear stefanor at cox.net
Tue Aug 21 03:47:22 EDT 2007


On Tue, Aug 21, 2007 at 12:50:22AM -0700, Ryan Ingram wrote:
> Ah, sneaky.  That code is fine because it uses unsafeCoerce# on "memcpy",
> changing memcpy from whatever type it is, into
> MutableByteArray# s# -> MutableByteArray# s# -> Int# -> s# -> (# s#, () #)
> 
> So as long as the GC understands MutableByteArray# it's safe; it's relying
> on the C calling convention being handled properly.

Which still isn't quite correct, because the code for
base-2.1:Data.Array.Base.memcpy could still be perverse and trigger a
GC.  However, since base is version-locked to GHC, it can depend on as
much undocumented behaviour as it needs.  The worst that can happen is a
few more testsuite failures when someone tries to change the compiler.

> On 8/21/07, Bulat Ziganshin <bulat.ziganshin at gmail.com> wrote:
> >
> > Hello Stefan,
> >
> > Tuesday, August 21, 2007, 10:08:59 AM, you wrote:
> >
> > > Your code is broken in a most evil and insidious way.
> >
> > and this code, too? :)
> >
> > freezeSTUArray :: Ix i => STUArray s i e -> ST s (UArray i e)
> > freezeSTUArray (STUArray l u marr#) = ST $ \s1# ->
> >    case sizeofMutableByteArray# marr#  of { n# ->
> >    case newByteArray# n# s1#           of { (# s2#, marr'# #) ->
> >    case unsafeCoerce# memcpy marr'# marr# n# s2# of { (# s3#, () #) ->
> >    case unsafeFreezeByteArray# marr'# s3# of { (# s4#, arr# #) ->
> >    (# s4#, UArray l u arr# #) }}}}
> >
> > > Unreproducable bugs are rarely reported, but they do add to people's
> > > impression of how unstable a language/library is.

Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070821/f0cf5205/attachment-0001.bin


More information about the Haskell-Cafe mailing list