How can I pass IOUArrays to FFI functions?

I have a C function of type void f ( HsWord32* p0, HsWord32* p1, HsWord32 size ); along with the FFI declaration: foreign import ccall unsafe f :: Ptr Word32 -> Ptr Word32 -> Word32 -> IO () In my Haskell code I have an unboxed IO array of Word32; IOUArray Int Word32. I want to pass the pointer to this array to f(). How can I get the pointer out of the array? Or, is there a better way to declare f() to do this? I'm open to using GHC hackery; using v6.6.1 right now. -- ryan

ryani.spam:
I have a C function of type
void f ( HsWord32* p0, HsWord32* p1, HsWord32 size );
along with the FFI declaration:
foreign import ccall unsafe f :: Ptr Word32 -> Ptr Word32 -> Word32 -> IO ()
In my Haskell code I have an unboxed IO array of Word32; IOUArray Int Word32.
I want to pass the pointer to this array to f(). How can I get the pointer out of the array? Or, is there a better way to declare f() to do this?
I'm open to using GHC hackery; using v6.6.1 right now.
Perhaps you want to use Foreign.Array instead (they are unboxed, and can be used as a Ptr over to C land)? -- Don

On Monday 20 August 2007 07:27:04 Ryan Ingram wrote:
I have a C function of type void f ( HsWord32* p0, HsWord32* p1, HsWord32 size );
along with the FFI declaration: foreign import ccall unsafe f :: Ptr Word32 -> Ptr Word32 -> Word32 -> IO ()
In my Haskell code I have an unboxed IO array of Word32; IOUArray Int Word32. I want to pass the pointer to this array to f(). How can I get the pointer out of the array? Or, is there a better way to declare f() to do this?
I'm open to using GHC hackery; using v6.6.1 right now.
-- ryan
Perhaps you'd like to use Data.Array.Storable? It supports the MArray interface, and has the additional operation: withStorableArray :: StorableArray i e -> (Ptr e -> IO a) -> IO a Cheers, Spencer Janssen

On Mon, Aug 20, 2007 at 05:27:04AM -0700, Ryan Ingram wrote:
I have a C function of type void f ( HsWord32* p0, HsWord32* p1, HsWord32 size );
along with the FFI declaration: foreign import ccall unsafe f :: Ptr Word32 -> Ptr Word32 -> Word32 -> IO ()
In my Haskell code I have an unboxed IO array of Word32; IOUArray Int Word32. I want to pass the pointer to this array to f(). How can I get the pointer out of the array? Or, is there a better way to declare f() to do this?
Short answer: You can't. Longer: GHC uses a copying/compacting garbage collector, so most objects don't have stable addresses. In particular MutableByteArray# is movable by default. So, you need an array structure built on pinned memory. If you want MArray operations, that leaves you with Data.Array.Storable, as already mentioned. Stefan
participants (4)
-
dons@cse.unsw.edu.au
-
Ryan Ingram
-
Spencer Janssen
-
Stefan O'Rear