How to read shared memory using the Haskell mmap library?

In C, I can create a shared memory object with:

int fd = shm_open("/object", O_RDWR | O_CREAT, 0777); 

I can also read this memory using mmap :

 int* addr = mmap(0, sizeof(*addr), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 

But how do I access this data from Haskell?

 import System.Posix.SharedMem import System.IO.MMap main = do fd <- shmOpen "/bolts" (ShmOpenFlags False False False False) 0777 -- Obviously doesn't make sense, mmapFileByteString -- requires a file path that I don't have! addr <- mmapFileByteString "/bolts" Nothing print addr 
+6
source share
1 answer

It seems that the mmap module you are using does not support this function. However, the good news is that what you want is not so difficult to implement. One approach is to create a raw FFI binding for mmap , call mmap yourself, and then use packCStringLen to convert the pointer to a ByteString .

 foreign import ccall "mmap" mmap :: Ptr () -> CSize -> CInt -> CInt-> CInt-> Int64 -> IO (Ptr ()) 

As another option, if you do not want to have your own FFI binding, the bindings-posix c'mmap package will provide you with this as c'mmap .

+2
source

Source: https://habr.com/ru/post/987881/


All Articles