How do I call mmap() in Haskell?

Duncan Coutts duncan.coutts at
Thu Dec 2 12:35:53 EST 2004

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


More information about the Glasgow-haskell-users mailing list