| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Haskus.Memory.Buffer
Contents
Description
A buffer in memory
Synopsis
- data Buffer (mut :: Mutability) (pin :: Pinning) (fin :: Finalization) (heap :: Heap) where
- Buffer :: !ByteArray# -> BufferI
- BufferP :: !ByteArray# -> BufferP
- BufferM :: !(MutableByteArray# RealWorld) -> BufferM
- BufferMP :: !(MutableByteArray# RealWorld) -> BufferMP
- BufferME :: Addr# -> !Word -> BufferME
- BufferE :: Addr# -> !Word -> BufferE
- BufferF :: !ByteArray# -> !Finalizers -> BufferF
- BufferPF :: !ByteArray# -> !Finalizers -> BufferPF
- BufferMF :: !(MutableByteArray# RealWorld) -> !Finalizers -> BufferMF
- BufferMPF :: !(MutableByteArray# RealWorld) -> !Finalizers -> BufferMPF
- BufferMEF :: Addr# -> !Word -> !Finalizers -> BufferMEF
- BufferEF :: Addr# -> !Word -> !Finalizers -> BufferEF
- newtype AnyBuffer = AnyBuffer (forall mut pin fin heap. Buffer mut pin fin heap)
- data Pinning
- data Finalization
- data Mutability
- data Heap
- type BufferI = Buffer Immutable NotPinned Collected Internal
- type BufferP = Buffer Immutable Pinned Collected Internal
- type BufferM = Buffer Mutable NotPinned Collected Internal
- type BufferMP = Buffer Mutable Pinned Collected Internal
- type BufferME = Buffer Mutable Pinned NotFinalized External
- type BufferE = Buffer Immutable Pinned NotFinalized External
- type BufferF = Buffer Immutable NotPinned Finalized Internal
- type BufferPF = Buffer Immutable Pinned Finalized Internal
- type BufferMF = Buffer Mutable NotPinned Finalized Internal
- type BufferMPF = Buffer Mutable Pinned Finalized Internal
- type BufferMEF = Buffer Mutable Pinned Finalized External
- type BufferEF = Buffer Immutable Pinned Finalized External
- newBuffer :: MonadIO m => Word -> m BufferM
- newPinnedBuffer :: MonadIO m => Word -> m BufferMP
- newAlignedPinnedBuffer :: MonadIO m => Word -> Word -> m BufferMP
- bufferSizeIO :: MonadIO m => Buffer mut pin fin heap -> m Word
- class BufferSize a where
- bufferSize :: a -> Word
- class Freezable a b | a -> b where
- unsafeBufferFreeze :: MonadIO m => a -> m b
- class Thawable a b | a -> b where
- unsafeBufferThaw :: MonadIO m => a -> m b
- bufferIsDynamicallyPinned :: Buffer mut pin fin heap -> Bool
- bufferDynamicallyPinned :: Buffer mut pin fin heap -> Either (Buffer mut NotPinned fin heap) (Buffer mut Pinned fin heap)
- withBufferAddr# :: MonadIO m => Buffer Mutable Pinned fin heap -> (Addr# -> m a) -> m a
- withBufferPtr :: MonadIO m => Buffer Mutable Pinned fin heap -> (Ptr b -> m a) -> m a
- unsafeWithBufferAddr# :: MonadIO m => Buffer mut Pinned fin heap -> (Addr# -> m a) -> m a
- unsafeWithBufferPtr :: MonadIO m => Buffer mut Pinned fin heap -> (Ptr b -> m a) -> m a
- bufferReadWord8IO :: MonadIO m => Buffer mut pin fin heap -> Word -> m Word8
- bufferReadWord8 :: Buffer Immutable pin fin heap -> Word -> Word8
- bufferReadWord16IO :: MonadIO m => Buffer mut pin fin heap -> Word -> m Word16
- bufferReadWord16 :: Buffer Immutable pin fin heap -> Word -> Word16
- bufferReadWord32IO :: MonadIO m => Buffer mut pin fin heap -> Word -> m Word32
- bufferReadWord32 :: Buffer Immutable pin fin heap -> Word -> Word32
- bufferReadWord64IO :: MonadIO m => Buffer mut pin fin heap -> Word -> m Word64
- bufferReadWord64 :: Buffer Immutable pin fin heap -> Word -> Word64
- bufferWriteWord8IO :: MonadIO m => Buffer Mutable pin fin heap -> Word -> Word8 -> m ()
- bufferWriteWord16IO :: MonadIO m => Buffer Mutable pin fin heap -> Word -> Word16 -> m ()
- bufferWriteWord32IO :: MonadIO m => Buffer Mutable pin fin heap -> Word -> Word32 -> m ()
- bufferWriteWord64IO :: MonadIO m => Buffer Mutable pin fin heap -> Word -> Word64 -> m ()
- copyBuffer :: forall m mut pin0 fin0 heap0 pin1 fin1 heap1. MonadIO m => Buffer mut pin0 fin0 heap0 -> Word -> Buffer Mutable pin1 fin1 heap1 -> Word -> Word -> m ()
- data Finalizers
- addFinalizer :: MonadIO m => Buffer mut pin Finalized heap -> IO () -> m ()
- makeFinalizable :: MonadIO m => Buffer mut pin f heap -> m (Buffer mut pin Finalized heap)
- touchBuffer :: MonadIO m => Buffer mut pin fin heap -> m ()
- touch :: MonadIO m => a -> m ()
- bufferToListIO :: MonadIO m => Buffer mut pin fin heap -> m [Word8]
- class BufferToList a where
- bufferToList :: a -> [Word8]
Documentation
data Buffer (mut :: Mutability) (pin :: Pinning) (fin :: Finalization) (heap :: Heap) where Source #
A memory buffer
Constructors
| Buffer :: !ByteArray# -> BufferI | |
| BufferP :: !ByteArray# -> BufferP | |
| BufferM :: !(MutableByteArray# RealWorld) -> BufferM | |
| BufferMP :: !(MutableByteArray# RealWorld) -> BufferMP | |
| BufferME :: Addr# -> !Word -> BufferME | |
| BufferE :: Addr# -> !Word -> BufferE | |
| BufferF :: !ByteArray# -> !Finalizers -> BufferF | |
| BufferPF :: !ByteArray# -> !Finalizers -> BufferPF | |
| BufferMF :: !(MutableByteArray# RealWorld) -> !Finalizers -> BufferMF | |
| BufferMPF :: !(MutableByteArray# RealWorld) -> !Finalizers -> BufferMPF | |
| BufferMEF :: Addr# -> !Word -> !Finalizers -> BufferMEF | |
| BufferEF :: Addr# -> !Word -> !Finalizers -> BufferEF |
Instances
Wrapper containing any kind of buffer
Buffer taxonomy
Is the buffer pinned into memory?
Constructors
| Pinned | The buffer has a fixed associated memory address |
| NotPinned | The buffer contents can be freely moved to another address |
data Finalization Source #
Is the memory automatically garbage collected?
Constructors
| Collected | Automatically collected by the garbage-collector |
| Finalized | Finalizers are run just before the garbage collector collects the referencing entity (buffer, pointer...). The memory used by the entity may be collected too (Internal heap), explicitly freed by a finalizer or not freed at all. |
| NotFinalized | The memory is not automatically freed and we can't attach finalizers to the buffer. |
Instances
| Eq Finalization Source # | |
Defined in Haskus.Memory.Property | |
| Show Finalization Source # | |
Defined in Haskus.Memory.Property Methods showsPrec :: Int -> Finalization -> ShowS # show :: Finalization -> String # showList :: [Finalization] -> ShowS # | |
data Mutability Source #
Is the memory mutable or not?
Instances
| Eq Mutability Source # | |
Defined in Haskus.Memory.Property | |
| Show Mutability Source # | |
Defined in Haskus.Memory.Property Methods showsPrec :: Int -> Mutability -> ShowS # show :: Mutability -> String # showList :: [Mutability] -> ShowS # | |
GHC allocator
newBuffer :: MonadIO m => Word -> m BufferM Source #
Allocate a buffer (mutable, unpinned)
>>>b <- newBuffer 1024
newAlignedPinnedBuffer :: MonadIO m => Word -> Word -> m BufferMP Source #
Allocate an aligned buffer (mutable, pinned)
Buffer size
class BufferSize a where Source #
Instances
| BufferSize BufferEF Source # | |
Defined in Haskus.Memory.Buffer Methods bufferSize :: BufferEF -> Word Source # | |
| BufferSize BufferMEF Source # | |
Defined in Haskus.Memory.Buffer Methods bufferSize :: BufferMEF -> Word Source # | |
| BufferSize BufferPF Source # | |
Defined in Haskus.Memory.Buffer Methods bufferSize :: BufferPF -> Word Source # | |
| BufferSize BufferF Source # | |
Defined in Haskus.Memory.Buffer Methods bufferSize :: BufferF -> Word Source # | |
| BufferSize BufferE Source # | |
Defined in Haskus.Memory.Buffer Methods bufferSize :: BufferE -> Word Source # | |
| BufferSize BufferME Source # | |
Defined in Haskus.Memory.Buffer Methods bufferSize :: BufferME -> Word Source # | |
| BufferSize BufferP Source # | |
Defined in Haskus.Memory.Buffer Methods bufferSize :: BufferP -> Word Source # | |
| BufferSize BufferI Source # | |
Defined in Haskus.Memory.Buffer Methods bufferSize :: BufferI -> Word Source # | |
Buffer freeze/thaw
class Freezable a b | a -> b where Source #
Buffer that can be frozen (converted from mutable to immutable)
Methods
unsafeBufferFreeze :: MonadIO m => a -> m b Source #
Convert a mutable buffer to an immutable one without copying. The buffer should not be modified after the conversion.
class Thawable a b | a -> b where Source #
Buffer that can be thawed (converted from immutable to mutable)
Methods
unsafeBufferThaw :: MonadIO m => a -> m b Source #
Convert an immutable buffer to a mutable one without copying. The original buffer should not be used after the conversion.
Instances
| Thawable (Buffer Immutable pin NotFinalized heap) (Buffer Mutable pin NotFinalized heap) Source # | |
Defined in Haskus.Memory.Buffer Methods unsafeBufferThaw :: MonadIO m => Buffer Immutable pin NotFinalized heap -> m (Buffer Mutable pin NotFinalized heap) Source # | |
| Thawable (Buffer Immutable pin Collected heap) (Buffer Mutable pin Collected heap) Source # | |
Buffer address
bufferIsDynamicallyPinned :: Buffer mut pin fin heap -> Bool Source #
Some buffers managed by GHC can be pinned as an optimization. This function reports this.
bufferDynamicallyPinned :: Buffer mut pin fin heap -> Either (Buffer mut NotPinned fin heap) (Buffer mut Pinned fin heap) Source #
Transform type-level NotPinned buffers into type-level Pinned if the buffer
is dynamically pinned (see bufferIsDynamicallyPinned).
withBufferAddr# :: MonadIO m => Buffer Mutable Pinned fin heap -> (Addr# -> m a) -> m a Source #
Do something with a buffer address
withBufferPtr :: MonadIO m => Buffer Mutable Pinned fin heap -> (Ptr b -> m a) -> m a Source #
Do something with a buffer pointer
unsafeWithBufferAddr# :: MonadIO m => Buffer mut Pinned fin heap -> (Addr# -> m a) -> m a Source #
Do something with a buffer address
Note: don't write into immutable buffer as it would break referential consistency
unsafeWithBufferPtr :: MonadIO m => Buffer mut Pinned fin heap -> (Ptr b -> m a) -> m a Source #
Do something with a buffer pointer
Note: don't write into immutable buffer as it would break referential consistency
Buffer read
bufferReadWord8IO :: MonadIO m => Buffer mut pin fin heap -> Word -> m Word8 Source #
Read a Word8, offset in bytes
We don't check that the offset is valid
>>>let b = [25,26,27,28] :: BufferI>>>bufferReadWord8IO b 227
bufferReadWord8 :: Buffer Immutable pin fin heap -> Word -> Word8 Source #
Read a Word8 in an immutable buffer, offset in bytes
We don't check that the offset is valid
>>>let b = [25,26,27,28] :: BufferI>>>putStrLn $ "Word8 at offset 2 is " ++ show (bufferReadWord8 b 2)Word8 at offset 2 is 27
bufferReadWord16IO :: MonadIO m => Buffer mut pin fin heap -> Word -> m Word16 Source #
Read a Word16, offset in bytes
We don't check that the offset is valid
>>>let b = [0x12,0x34,0x56,0x78] :: BufferI>>>x <- bufferReadWord16IO b 0>>>(x == 0x1234) || (x == 0x3412)True
bufferReadWord16 :: Buffer Immutable pin fin heap -> Word -> Word16 Source #
Read a Word16 in an immutable buffer, offset in bytes
We don't check that the offset is valid
bufferReadWord32IO :: MonadIO m => Buffer mut pin fin heap -> Word -> m Word32 Source #
Read a Word32, offset in bytes
We don't check that the offset is valid
>>>let b = [0x12,0x34,0x56,0x78] :: BufferI>>>x <- bufferReadWord32IO b 0>>>(x == 0x12345678) || (x == 0x78563412)True
bufferReadWord32 :: Buffer Immutable pin fin heap -> Word -> Word32 Source #
Read a Word32 in an immutable buffer, offset in bytes
We don't check that the offset is valid
bufferReadWord64IO :: MonadIO m => Buffer mut pin fin heap -> Word -> m Word64 Source #
Read a Word64, offset in bytes
We don't check that the offset is valid
>>>let b = [0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0] :: BufferI>>>x <- bufferReadWord64IO b 0>>>(x == 0x123456789ABCDEF0) || (x == 0xF0DEBC9A78563412)True
bufferReadWord64 :: Buffer Immutable pin fin heap -> Word -> Word64 Source #
Read a Word64 in an immutable buffer, offset in bytes
We don't check that the offset is valid
Buffer write and copy
bufferWriteWord8IO :: MonadIO m => Buffer Mutable pin fin heap -> Word -> Word8 -> m () Source #
Write a Word8, offset in bytes
We don't check that the offset is valid
>>>b <- newBuffer 10>>>bufferWriteWord8IO b 1 123>>>bufferReadWord8IO b 1123
bufferWriteWord16IO :: MonadIO m => Buffer Mutable pin fin heap -> Word -> Word16 -> m () Source #
Write a Word16, offset in bytes
We don't check that the offset is valid
>>>b <- newBuffer 10>>>let v = 1234 :: Word16>>>bufferWriteWord16IO b 1 v>>>bufferReadWord16IO b 11234
>>>(x :: Word16) <- fromIntegral <$> bufferReadWord8IO b 1>>>(y :: Word16) <- fromIntegral <$> bufferReadWord8IO b 2>>>(((x `shiftL` 8) .|. y) == v) || (((y `shiftL` 8) .|. x) == v)True
bufferWriteWord32IO :: MonadIO m => Buffer Mutable pin fin heap -> Word -> Word32 -> m () Source #
Write a Word32, offset in bytes
We don't check that the offset is valid
>>>b <- newBuffer 10>>>let v = 1234 :: Word32>>>bufferWriteWord32IO b 1 v>>>bufferReadWord32IO b 11234
bufferWriteWord64IO :: MonadIO m => Buffer Mutable pin fin heap -> Word -> Word64 -> m () Source #
Write a Word64, offset in bytes
We don't check that the offset is valid
>>>b <- newBuffer 10>>>let v = 1234 :: Word64>>>bufferWriteWord64IO b 1 v>>>bufferReadWord64IO b 11234
Arguments
| :: MonadIO m | |
| => Buffer mut pin0 fin0 heap0 | Source buffer |
| -> Word | Offset in source buffer |
| -> Buffer Mutable pin1 fin1 heap1 | Target buffer |
| -> Word | Offset in target buffer |
| -> Word | Number of Word8 to copy |
| -> m () |
Copy a buffer into another from/to the given offsets
We don't check buffer limits.
>>>let b = [0,1,2,3,4,5,6,7,8] :: BufferI>>>b2 <- newBuffer 8>>>copyBuffer b 4 b2 0 4>>>copyBuffer b 0 b2 4 4>>>forM [0..7] (bufferReadWord8IO b2)[4,5,6,7,0,1,2,3]
Finalizers
data Finalizers Source #
addFinalizer :: MonadIO m => Buffer mut pin Finalized heap -> IO () -> m () Source #
Add a finalizer.
The latest added finalizers are executed first. Finalizers are not guaranteed to run (e.g. if the program exits before the buffer is collected).
makeFinalizable :: MonadIO m => Buffer mut pin f heap -> m (Buffer mut pin Finalized heap) Source #
Make a buffer finalizable
The new buffer liveness is used to trigger finalizers.
touchBuffer :: MonadIO m => Buffer mut pin fin heap -> m () Source #
Touch a buffer
Conversions
bufferToListIO :: MonadIO m => Buffer mut pin fin heap -> m [Word8] Source #
Get contents as a list of bytes
class BufferToList a where Source #
Instances
| BufferToList BufferPF Source # | |
Defined in Haskus.Memory.Buffer Methods bufferToList :: BufferPF -> [Word8] Source # | |
| BufferToList BufferF Source # | |
Defined in Haskus.Memory.Buffer Methods bufferToList :: BufferF -> [Word8] Source # | |
| BufferToList BufferP Source # | |
Defined in Haskus.Memory.Buffer Methods bufferToList :: BufferP -> [Word8] Source # | |
| BufferToList BufferI Source # | |
Defined in Haskus.Memory.Buffer Methods bufferToList :: BufferI -> [Word8] Source # | |