how implement a UArray with a ForeignPtr?

David Roundy droundy@abridgegame.org
Sat, 19 Jul 2003 08:17:10 -0400


Hello.  I was wondering if there might be any relatively simple way to
implement a type equivalent to UArray, but with the memory stored in a
ForeignPtr? It seems like this would be a commonly useful situation, where
you have a C function that returns a pointer to an array of data, and it
would be nice to be able to access that data without making a copy of it.

In case you're wondering, the reason for not allocating the memory within
haskell itself and passing it to the C side is that I want to use mmap,
which really needs its own finalizer.

>From browsing the ghc base libraries, it looks like I'll pretty much just
need peekElemOff and unsafePerformIO (which should be safe as long as I
don't give a copy of the ForeignPtr to anyone else who might try to write
to it--and provided the file isn't opened for write while I'm trying to
read it).  Does this sound like the right idea, or is there some problem?
Might this actually turn out to be slow, which would defeat the whole
purpose?
-- 
David Roundy
http://www.abridgegame.org