Copyright | (c) 2018-2020 Kowainik |
---|---|
License | MPL-2.0 |
Maintainer | Kowainik <xrom.xkov@gmail.com> |
Safe Haskell | None |
Language | Haskell2010 |
This modules introduces more type-safe interface of some standard function to
work with memory from base
package.
Synopsis
- hFileSize :: Handle -> IO (Memory Byte)
- hSetFileSize :: Handle -> Memory Byte -> IO ()
- bitSizeMaybe :: Bits a => a -> Maybe (Memory Bit)
- finiteBitSize :: FiniteBits b => b -> Memory Bit
- sizeOf :: Storable a => a -> Memory Byte
- allocaBytes :: Memory Byte -> (Ptr a -> IO b) -> IO b
- mallocBytes :: Memory Byte -> IO (Ptr a)
- callocBytes :: Memory Byte -> IO (Ptr a)
- reallocBytes :: Ptr a -> Memory Byte -> IO (Ptr a)
- mallocForeignPtrBytes :: Memory Byte -> IO (ForeignPtr a)
System.IO module
hSetFileSize :: Handle -> Memory Byte -> IO () Source #
Truncates the physical file with the handle to the given size in Byte
s.
Similar to hSetFileSize
from base
but more type-safe: it works with
given 'Memory Byte' instead of Integer
.
NOTE: This function makes floor
under the hood, so be careful when working
with the smaller units.
Data.Bits module
bitSizeMaybe :: Bits a => a -> Maybe (Memory Bit) Source #
Returns the number of Bit
s in the type of the argument. The actual value of
the argument is ignored. Returns Nothing
for types that do not have a fixed
bitsize, like Integer
.
Type safe version of the bitSizeMaybe
function.
>>>
bitSizeMaybe (0 :: Int)
Just (Memory {unMemory = 64})>>>
bitSizeMaybe (0 :: Integer)
Nothing
finiteBitSize :: FiniteBits b => b -> Memory Bit Source #
Return the number of bits in the type of the argument. The actual value of the argument is ignored.
Type safe version of the finiteBitSize
function.
>>>
finiteBitSize False
Memory {unMemory = 1}>>>
finiteBitSize (0 :: Int)
Memory {unMemory = 64}
Foreign module
sizeOf :: Storable a => a -> Memory Byte Source #
Like sizeOf
but returns type-safe 'Memory Byte' instead of the Int
which represents the amount of bytes.
Computes the storage requirements (in bytes) of the argument. The value of the argument is not used.
>>>
sizeOf True
Memory {unMemory = 32}>>>
sizeOf 'x'
Memory {unMemory = 32}>>>
sizeOf (0 :: Int)
Memory {unMemory = 64}>>>
sizeOf (0.0 :: Double)
Memory {unMemory = 64}
allocaBytes :: Memory Byte -> (Ptr a -> IO b) -> IO b Source #
Executes the computation, passing as argument a pointer to a temporarily
allocated block of memory of n
Byte
s. The block of memory is sufficiently
aligned for any of the basic foreign types that fits into a memory block of the
allocated size.
The memory is freed when the computation terminates (either normally or via an exception), so the passed pointer must not be used after this.
Similar to allocaBytes
but receives Byte
s instead of Int
.
mallocBytes :: Memory Byte -> IO (Ptr a) Source #
Allocates a block of memory of the given number of Byte
s.
The block of memory is sufficiently aligned for any of the basic foreign types
that fits into a memory block of the allocated size.
Similar to mallocBytes
but receives Byte
s instead of Int
.
callocBytes :: Memory Byte -> IO (Ptr a) Source #
Llike mallocBytes
but memory is filled with Byte
s of value zero.
reallocBytes :: Ptr a -> Memory Byte -> IO (Ptr a) Source #
Type safe version of the reallocBytes
function.
mallocForeignPtrBytes :: Memory Byte -> IO (ForeignPtr a) Source #
Type safe version of the mallocForeignPtrBytes
function.