module Membrain.Base
(
hFileSize
, hSetFileSize
, bitSizeMaybe
, finiteBitSize
, sizeOf
, allocaBytes
, mallocBytes
, callocBytes
, reallocBytes
, mallocForeignPtrBytes
) where
import Data.Bits (FiniteBits)
import Foreign.ForeignPtr (ForeignPtr)
import Foreign.Ptr (Ptr)
import Foreign.Storable (Storable)
import Prelude hiding (floor)
import System.IO (Handle)
import Membrain.Constructors (bit, byte)
import Membrain.Memory (Memory, floor)
import Membrain.Units (Bit, Byte)
import qualified Data.Bits as Base (Bits, bitSizeMaybe, finiteBitSize)
import qualified Foreign.ForeignPtr as Base (mallocForeignPtrBytes)
import qualified Foreign.Marshal.Alloc as Base (allocaBytes, callocBytes, mallocBytes, reallocBytes)
import qualified Foreign.Storable as Base
import qualified System.IO as Base (hFileSize, hSetFileSize)
hFileSize :: Handle -> IO (Memory Byte)
hFileSize :: Handle -> IO (Memory Byte)
hFileSize h :: Handle
h = Natural -> Memory Byte
byte (Natural -> Memory Byte)
-> (Integer -> Natural) -> Integer -> Memory Byte
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Natural
forall a. Num a => Integer -> a
fromInteger (Integer -> Memory Byte) -> IO Integer -> IO (Memory Byte)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Handle -> IO Integer
Base.hFileSize Handle
h
{-# INLINE hFileSize #-}
hSetFileSize :: Handle -> Memory Byte -> IO ()
hSetFileSize :: Handle -> Memory Byte -> IO ()
hSetFileSize h :: Handle
h = Handle -> Integer -> IO ()
Base.hSetFileSize Handle
h (Integer -> IO ())
-> (Memory Byte -> Integer) -> Memory Byte -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Memory Byte -> Integer
forall n (mem :: Nat).
(Integral n, KnownNat mem) =>
Memory mem -> n
floor
{-# INLINE hSetFileSize #-}
bitSizeMaybe :: Base.Bits a => a -> Maybe (Memory Bit)
bitSizeMaybe :: a -> Maybe (Memory Bit)
bitSizeMaybe x :: a
x = Natural -> Memory Bit
bit (Natural -> Memory Bit) -> (Int -> Natural) -> Int -> Memory Bit
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Natural
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Memory Bit) -> Maybe Int -> Maybe (Memory Bit)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> Maybe Int
forall a. Bits a => a -> Maybe Int
Base.bitSizeMaybe a
x
{-# INLINE bitSizeMaybe #-}
finiteBitSize :: FiniteBits b => b -> Memory Bit
finiteBitSize :: b -> Memory Bit
finiteBitSize = Natural -> Memory Bit
bit (Natural -> Memory Bit) -> (b -> Natural) -> b -> Memory Bit
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Natural
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Natural) -> (b -> Int) -> b -> Natural
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Int
forall b. FiniteBits b => b -> Int
Base.finiteBitSize
{-# INLINE finiteBitSize #-}
sizeOf :: Storable a => a -> Memory Byte
sizeOf :: a -> Memory Byte
sizeOf = Natural -> Memory Byte
byte (Natural -> Memory Byte) -> (a -> Natural) -> a -> Memory Byte
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Natural
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Natural) -> (a -> Int) -> a -> Natural
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Int
forall a. Storable a => a -> Int
Base.sizeOf
{-# INLINE sizeOf #-}
allocaBytes :: Memory Byte -> (Ptr a -> IO b) -> IO b
allocaBytes :: Memory Byte -> (Ptr a -> IO b) -> IO b
allocaBytes bs :: Memory Byte
bs = Int -> (Ptr a -> IO b) -> IO b
forall a b. Int -> (Ptr a -> IO b) -> IO b
Base.allocaBytes (Memory Byte -> Int
forall n (mem :: Nat).
(Integral n, KnownNat mem) =>
Memory mem -> n
floor Memory Byte
bs)
{-# INLINE allocaBytes #-}
mallocBytes :: Memory Byte -> IO (Ptr a)
mallocBytes :: Memory Byte -> IO (Ptr a)
mallocBytes = Int -> IO (Ptr a)
forall a. Int -> IO (Ptr a)
Base.mallocBytes (Int -> IO (Ptr a))
-> (Memory Byte -> Int) -> Memory Byte -> IO (Ptr a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Memory Byte -> Int
forall n (mem :: Nat).
(Integral n, KnownNat mem) =>
Memory mem -> n
floor
{-# INLINE mallocBytes #-}
callocBytes :: Memory Byte -> IO (Ptr a)
callocBytes :: Memory Byte -> IO (Ptr a)
callocBytes = Int -> IO (Ptr a)
forall a. Int -> IO (Ptr a)
Base.callocBytes (Int -> IO (Ptr a))
-> (Memory Byte -> Int) -> Memory Byte -> IO (Ptr a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Memory Byte -> Int
forall n (mem :: Nat).
(Integral n, KnownNat mem) =>
Memory mem -> n
floor
{-# INLINE callocBytes #-}
reallocBytes :: Ptr a -> Memory Byte -> IO (Ptr a)
reallocBytes :: Ptr a -> Memory Byte -> IO (Ptr a)
reallocBytes p :: Ptr a
p = Ptr a -> Int -> IO (Ptr a)
forall a. Ptr a -> Int -> IO (Ptr a)
Base.reallocBytes Ptr a
p (Int -> IO (Ptr a))
-> (Memory Byte -> Int) -> Memory Byte -> IO (Ptr a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Memory Byte -> Int
forall n (mem :: Nat).
(Integral n, KnownNat mem) =>
Memory mem -> n
floor
{-# INLINE reallocBytes #-}
mallocForeignPtrBytes :: Memory Byte -> IO (ForeignPtr a)
mallocForeignPtrBytes :: Memory Byte -> IO (ForeignPtr a)
mallocForeignPtrBytes = Int -> IO (ForeignPtr a)
forall a. Int -> IO (ForeignPtr a)
Base.mallocForeignPtrBytes (Int -> IO (ForeignPtr a))
-> (Memory Byte -> Int) -> Memory Byte -> IO (ForeignPtr a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Memory Byte -> Int
forall n (mem :: Nat).
(Integral n, KnownNat mem) =>
Memory mem -> n
floor
{-# INLINE mallocForeignPtrBytes #-}