From ac3060834095c1f124435422ea7797746b99759e Mon Sep 17 00:00:00 2001 From: Stevan Andjelkovic Date: Fri, 17 Dec 2021 15:21:44 +0100 Subject: [PATCH] feat(journal): add posix compatible way of getting the page size --- src/journal/src/Journal/Internal/ByteBuffer.hs | 3 ++- src/journal/src/Journal/Internal/Mmap.hs | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/journal/src/Journal/Internal/ByteBuffer.hs b/src/journal/src/Journal/Internal/ByteBuffer.hs index 35bc8109..05b4150a 100644 --- a/src/journal/src/Journal/Internal/ByteBuffer.hs +++ b/src/journal/src/Journal/Internal/ByteBuffer.hs @@ -108,7 +108,8 @@ allocateAligned capa align = do mmapped :: FilePath -> Int -> IO ByteBuffer mmapped fp capa = do fd <- openFd fp ReadWrite Nothing defaultFileFlags - bb <- allocateAligned capa 4096 + pageSize <- sysconfPageSize + bb <- allocateAligned capa pageSize ptr <- mmap (Just (bbPtr bb)) (fromIntegral capa) (PROT (READ :| WRITE)) MAP_SHARED (Just fd) 0 assert (ptr == bbPtr bb) (return ()) diff --git a/src/journal/src/Journal/Internal/Mmap.hs b/src/journal/src/Journal/Internal/Mmap.hs index c7c202a2..e0903b37 100644 --- a/src/journal/src/Journal/Internal/Mmap.hs +++ b/src/journal/src/Journal/Internal/Mmap.hs @@ -25,6 +25,9 @@ foreign import ccall unsafe "sys/mman.h msync" foreign import ccall unsafe "stdlib.h posix_memalign" c_posix_memalign :: Ptr (Ptr a) -> CSize -> CSize -> IO CInt +foreign import ccall unsafe "unistd.h sysconf" + c_sysconf :: CInt -> IO CLong + ------------------------------------------------------------------------ mmap :: Maybe (Ptr a) -> Int -> MemoryProtection -> MemoryVisibility -> Maybe Fd @@ -116,6 +119,9 @@ posixMemalign align size = do (c_posix_memalign memPtr (fromIntegral align) (fromIntegral size)) peek memPtr +sysconfPageSize :: IO Int +sysconfPageSize = fromIntegral <$> c_sysconf 30 + ------------------------------------------------------------------------ {-