membrain-0.0.0.1: Type-safe memory units

Copyright(c) 2018-2020 Kowainik
LicenseMPL-2.0
MaintainerKowainik <xrom.xkov@gmail.com>
Safe HaskellNone
LanguageHaskell2010

Membrain.Base

Contents

Description

This modules introduces more type-safe interface of some standard function to work with memory from base package.

Synopsis

System.IO module

hFileSize :: Handle -> IO (Memory Byte) Source #

For a handle which attached to a physical file, the function returns the size of that file in Bytes.

Similar hFileSize from base but more type-safe: returns 'Memory Byte' instead of Integer.

hSetFileSize :: Handle -> Memory Byte -> IO () Source #

Truncates the physical file with the handle to the given size in Bytes.

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 Bits 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 Bytes. 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 Bytes instead of Int.

mallocBytes :: Memory Byte -> IO (Ptr a) Source #

Allocates a block of memory of the given number of Bytes. 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 Bytes instead of Int.

callocBytes :: Memory Byte -> IO (Ptr a) Source #

Llike mallocBytes but memory is filled with Bytes 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.