[Haskell-cafe] State Variables

Matthew Bromberg mattcbro at earthlink.net
Mon May 29 14:54:36 EDT 2006

If possible I'd like to memory manage on the Haskell side.  All of the 
calls to BLAS and LAPACK that I'm aware of assume that
all arrays are allocated outside of the C or Fortran that implement the 
matrix algorithms.  They never return buffers to
newly allocated arrays. So what I'd like to do is something like 
allocate an array in Haskell, freeze it and extract a  pointer,
send it to the C code,  and then unfreeze it.  That way I benefit from 
Haskell's garbage collector.  Explicitly managing memory is a
huge bug generator in C and C++.  In fact I'd say it's number one on the 

So can I modify Foreign Array to achieve this, or do I just end up with 
essentially a Storable Array?
Maybe it would be just easier to use 6.5, though heaven knows what sorts 
of bugs may be lurking there.  I downloaded it already per your link.

Bulat Ziganshin wrote:
> Hello Matthew,
> Monday, May 29, 2006, 8:04:56 PM, you wrote:
>> What is the difference between the ForeignArray defined in this source
>> and the StorableArray?  The source code of both modules are very similar.
> the devil in details :) - StorableArray in 6.4 is slow because it uses
> ForeignPtr. there are two possible solutions - either speed up
> ForeignPtr (done in 6.5) or use Ptr instead of ForeignPtr (used in my
> module). of course in this case you lose all the benefits of
> ForeignPtr's and in particular you will need to make explicit 'free'
> if you create such array via 'newArray' or it's derivatives. if you
> create such array via 'unsafePtrToForeignArray', then you don't need
> to make any additional steps to free it's memory besides of that
> required for StorableArray (i.e. in most cases it's the problem of FFI
> library you are used)
> simply speaking, if you don't create ForeignArray on the Haskell side,
> it's as simple in use as StorableArray and as fast as IOUArray

More information about the Haskell-Cafe mailing list