How do I call mmap() in Haskell?

I've been wondering how to do mmap() in Haskell since I don't know how to get things to interact with the runtime system etc. Actually, similar questions apply to asynchronous IO. The foreign function interface is nice and all, but modelling the result with types and interoperating with the runtime system is the real question here. -- wli

On Thu, Dec 02, 2004 at 01:16:31AM -0800, William Lee Irwin III wrote:
I've been wondering how to do mmap() in Haskell since I don't know how to get things to interact with the runtime system etc.
Actually, similar questions apply to asynchronous IO.
The foreign function interface is nice and all, but modelling the result with types and interoperating with the runtime system is the real question here.
You could take a look at FastPackedString.hs in darcs, which calls mmap to read strings, and uses ForeignPtrs to hold their data, so you don't have to copy the memory over. -- David Roundy http://www.darcs.net

On Thu, 2004-12-02 at 01:16 -0800, William Lee Irwin III wrote:
I've been wondering how to do mmap() in Haskell since I don't know how to get things to interact with the runtime system etc.
Actually, similar questions apply to asynchronous IO.
The foreign function interface is nice and all, but modelling the result with types and interoperating with the runtime system is the real question here.
Yes, I've used mmap in a couple projects now just using the FFI. I think it would be nice to have a System.Posix.MMap module or something with a portable underlying implementation (see how many mmap variants there are in the ghc rts MBlock.c implementation to deal with different OSs). Of course we would have to agree some common interface(s), like do we get back a raw Ptr () or a managed ForeignPtr or what? We'd need current users to speak up and say what they're using it for and what interface they could live with. My initial guess is that we'd need a mmap_raw which gives back an unmanaged pointer and also a managed version that has a finaliser that calls munmap(). Then there is the range of flags that mmap supports on different OSs. Posix specifies the common ones, and then there is anonymous pages which can be implemented directly on some systems or emulated by opening /dev/null or some other similar trick. Both these interfaces should be implementable on win32 systems (via MapViewOfFile etc). Duncan

On Thu, Dec 02, 2004 at 05:35:53PM +0000, Duncan Coutts wrote:
Yes, I've used mmap in a couple projects now just using the FFI. I think it would be nice to have a System.Posix.MMap module or something with a portable underlying implementation (see how many mmap variants there are in the ghc rts MBlock.c implementation to deal with different OSs). Of course we would have to agree some common interface(s), like do we get back a raw Ptr () or a managed ForeignPtr or what? We'd need current users to speak up and say what they're using it for and what interface they could live with. My initial guess is that we'd need a mmap_raw which gives back an unmanaged pointer and also a managed version that has a finaliser that calls munmap(). Then there is the range of flags that mmap supports on different OSs. Posix specifies the common ones, and then there is anonymous pages which can be implemented directly on some systems or emulated by opening /dev/null or some other similar trick. Both these interfaces should be implementable on win32 systems (via MapViewOfFile etc).
Someone showed me an example of darcs using mmap(). So now I just have to figure out how to translate that to get it to work with Linux' remap_file_pages() and some aio interface that can interoperate with it. -- wli
participants (3)
-
David Roundy
-
Duncan Coutts
-
William Lee Irwin III