{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_HADDOCK hide, not-home #-}
-- |
-- Module      : Data.Prim.Memory.Internal
-- Copyright   : (c) Alexey Kuleshevich 2020
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <alexey@kuleshevi.ch>
-- Stability   : experimental
-- Portability : non-portable
--
module Data.Prim.Memory.Internal
  ( module Data.Prim.Memory.Internal
  , module Data.Prim.Memory.Bytes.Internal
  ) where

import Control.Prim.Exception
import Control.Prim.Monad.Unsafe
import qualified Data.ByteString as BS
import Data.Foldable as Foldable
import Data.Kind
import Data.List as List
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.Monoid as Monoid
import Data.Prim
import Data.Prim.Array
import Data.Prim.Memory.Bytes.Internal
import Data.Prim.Memory.ByteString
import Data.Prim.Memory.ForeignPtr
import Data.Prim.Memory.Ptr
import qualified Data.Prim.Memory.Text as T
import qualified Data.Semigroup as Semigroup
import Foreign.Prim
import Numeric (showHex)

-- | Type class that can be implemented for an immutable data type that provides
-- read-only direct access to memory
class MemRead mr where

  -- | Check if two read only regions refer to the exact same region of memory
  --
  -- @since 0.3.0
  isSameMem :: mr -> mr -> Bool

  -- | Number of bytes allocated by the data type available for reading.
  --
  -- ====__Example__
  --
  -- >>> :set -XDataKinds
  -- >>> import Data.Prim.Memory
  -- >>> byteCountMem (fromByteListMem [1,2,3] :: Bytes 'Inc)
  -- Count {unCount = 3}
  --
  -- @since 0.1.0
  byteCountMem :: mr -> Count Word8

  -- | Read an element with an offset in number of elements, rather than bytes as is the
  -- case with `indexByteOffMem`.
  --
  -- [Unsafe] Bounds are not checked. When precondition for @off@ argument is violated the
  -- result is either unpredictable output or failure with a segfault.
  --
  -- @since 0.1.0
  indexOffMem :: Prim e
    => mr -- ^ /memRead/ - Memory to read an element from
    -> Off e
    -- ^ /off/ - Offset in number of elements from the beginning of @memRead@
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= off
    --
    -- > unOffBytes off <= unCount (byteCountMem memRead - byteCountType @e)
    --
    -> e
  indexOffMem mr
mr Off e
off = mr -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem mr
mr (Off e -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff Off e
off)
  {-# INLINE indexOffMem #-}

  -- | Read an element with an offset in number of bytes. Bounds are not checked.
  --
  -- [Unsafe] When precondition for @off@ argument is violated the result is either
  -- unpredictable output or failure with a segfault.
  --
  -- @since 0.1.0
  indexByteOffMem :: Prim e
    => mr -- ^ /memRead/ - Memory to read an element from
    -> Off Word8
    -- ^ /off/ - Offset in number of elements from the beginning of @memRead@
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= unOff off
    --
    -- > unOff off <= unCount (byteCountMem memRead - byteCountType @e)
    --
    -> e

  -- | Copy contiguous chunk of memory from the read only memory into the target mutable
  -- `MBytes`. Source and target /must not/ refer to the same memory region, otherwise
  -- that would imply that the source is not immutable which would be a violation of some
  -- other invariant elsewhere in the code.
  --
  -- [Unsafe] When a precondition for either of the offsets @memSourceOff@, @memTargetOff@
  -- or the element count @memCount@ is violated the result is either unpredictable output or
  -- failure with a segfault.
  --
  -- @since 0.1.0
  copyByteOffToMBytesMem ::
       (MonadPrim s m, Prim e)
    => mr -- ^ /memSourceRead/ - Source from where to copy
    -> Off Word8
    -- ^ /memSourceOff/ - Offset into source memory in number of bytes
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memSourceOff
    --
    -- > unOff memSourceOff <= unCount (byteCountMem memSourceRead - byteCountType @e)
    -> MBytes p s -- ^ /memTargetWrite/ - Target mutable memory
    -> Off Word8
    -- ^ /memTargetOff/ -  Offset into target memory in number of bytes
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memTargetOff
    --
    -- > unOff memTargetOff <= unCount (byteCountMem memTargetWrite - byteCountType @e)
    -> Count e
    -- ^ /memCount/ - Number of elements of type __@e@__ to copy
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memCount
    --
    -- > unCountBytes memCount + unOff memSourceOff <= unCount (byteCountMem memSourceRead - byteCountType @e)
    --
    -- > unCountBytes memCount + unOff memTargetOff <= unCount (byteCountMem memTargetRead - byteCountType @e)
    -> m ()

  -- | Copy contiguous chunk of memory from the read only memory into the target mutable
  -- `Ptr`. Source and target /must not/ refer to the same memory region, otherwise that
  -- would imply that the source is not immutable which would be a violation of some other
  -- invariant elsewhere in the code.
  --
  -- [Unsafe] When any precondition for one of the offsets @memSourceOff@, @memTargetOff@
  -- or the element count @memCount@ is violated a call to this function can result in:
  -- copy of data that doesn't belong to @memSourceRead@, heap corruption or failure with
  -- a segfault.
  --
  --
  -- @since 0.1.0
  copyByteOffToPtrMem ::
       (MonadPrim s m, Prim e)
    => mr -- ^ /memSourceRead/ - Source from where to copy
    -> Off Word8
    -- ^ /memSourceOff/ - Offset into source memory in number of bytes
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memSourceOff
    --
    -- > unOff memSourceOff <= unCount (byteCountMem memSourceRead - byteCountType @e)
    -> Ptr e
    -- ^ /memTargetWrite/ - Pointer to the target mutable memory
    --
    -- /__Preconditions:__/
    --
    -- Once the pointer is advanced by @memTargetOff@ the next @unCountBytes memCount@ bytes must
    -- still belong to the same region of memory @memTargetWrite@
    -> Off Word8
    -- ^ /memTargetOff/ - Number of bytes to advance the pointer @memTargetWrite@ forward
    --
    -- /__Precondition:__/
    --
    -- Once the pointer is advanced by @memTargetOff@ it must still refer to the same
    -- memory region @memTargetWrite@
    -> Count e
    -- ^ /memCount/ - Number of elements of type __@e@__ to copy
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memCount
    --
    -- > unCountBytes memCount + unOff memSourceOff <= unCount (byteCountMem memSourceRead - byteCountType @e)
    -> m ()

  -- | Same as `compareByteOffMem`, but compare the read-only
  -- memory region to a region addressed by a `Ptr` inside of a `MonadPrim`.
  --
  -- [Unsafe] When any precondition for either of the offsets @memOff1@, @memOff2@, the
  -- pointer @memRead2@ or the element count @memCount@ is violated the result is either
  -- unpredictable output or failure with a segfault.
  --
  -- @since 0.1.0
  compareByteOffToPtrMem ::
       (MonadPrim s m, Prim e)
    => mr -- ^ /memRead1/ - First memory region
    -> Off Word8
    -- ^ /memOff1/ - Offset for @memRead1@ in number of bytes
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memOff1
    --
    -- > unOff memOff1 <= unCount (byteCountMem memRead1 - byteCountType @e)
    -> Ptr e
    -- ^ /memRead2/- Second memory region that can be accessed by a pointer
    --
    -- /__Preconditions__/
    --
    -- Once the pointer is advanced by @memOff2@ the next @unCountBytes memCount@ bytes must
    -- still belong to the same region of memory @memRead2@
    -> Off Word8
    -- ^ /memOff2/ - Number of bytes to advance the pointer @memRead2@ forward
    --
    -- /__Precondition:__/
    --
    -- Once the pointer is advanced by @memOff2@ it must still refer to the same memory
    -- region @memRead2@
    -> Count e -- ^ /memCount/ - Number of elements of type __@e@__ to compare as binary
    -- ^ /memCount/ - Number of elements of type __@e@__ to compare as binary
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memCount
    --
    -- > unCountBytes memCount + unOff memOff1 <= unCount (byteCountMem memRead1 - byteCountType @e)
    -> m Ordering

  -- | Same as `compareByteOffMem`, but compare the read-only memory region to `Bytes`.
  --
  -- [Unsafe] When any precondition for either of the offsets @memOff1@, @memOff2@ or the
  -- element count @memCount@ is violated the result is either unpredictable output or
  -- failure with a segfault.
  --
  -- @since 0.1.0
  compareByteOffToBytesMem ::
       Prim e
    => mr -- ^ /memRead1/ - First memory region
    -> Off Word8
    -- ^ /memOff1/ - Offset for @memRead1@ in number of bytes
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memOff1
    --
    -- > unOff memOff1 <= unCount (byteCountMem memRead1 - byteCountType @e)
    -> Bytes p -- ^ /memRead2/- Second memory region that is backed by `Bytes`
    -> Off Word8
    -- ^ /memOff2/ - Offset for @memRead2@ in number of bytes
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memOff2
    --
    -- > unOff memOff2 <= unCount (byteCountMem memRead2 - byteCountType @e)
    -> Count e
    -- ^ /memCount/ - Number of elements of type __@e@__ to compare as binary
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memCount
    --
    -- > unCountBytes memCount + unOff memOff1 <= unCount (byteCountMem memRead1 - byteCountType @e)
    --
    -- > unCountBytes memCount + unOff memOff2 <= unCount (byteCountMem memRead2 - byteCountType @e)
    -> Ordering

  -- | Compare two read-only regions of memory byte-by-byte. The very first mismatched
  -- byte will cause this function to produce `LT` if the byte in @memRead1@ is smaller
  -- than the one in @memRead2@ and `GT` if it is bigger. It is not a requirement to
  -- short-circuit on the first mismatch, but it is a good optimization to have for
  -- non-sensitive data. Memory regions that store security critical data may choose to
  -- implement this function to work in constant time.
  --
  -- This function is usually implemented by either one of `compareByteOffToPtrMem` or
  -- `compareByteOffToBytesMem`, depending on the nature of @mr@ type. However it differs
  -- from the aforementioned functions with a fact that it is pure non-monadic
  -- computation.
  --
  -- [Unsafe] When any precondition for either of the offsets @memOff1@, @memOff2@ or the
  -- element count @memCount@ is violated the result is either unpredictable output or
  -- failure with a segfault.
  --
  -- @since 0.1.0
  compareByteOffMem ::
       (MemRead mr', Prim e)
    => mr' -- ^ /memRead1/ - First memory region
    -> Off Word8
    -- ^ /memOff1/ - Offset for @memRead1@ in number of bytes
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memOff1
    --
    -- > unOff memOff1 <= unCount (byteCountMem memRead1 - byteCountType @e)
    -> mr -- ^ /memRead2/ - Second memory region
    -> Off Word8
    -- ^ /memOff2/ - Offset for @memRead2@ in number of bytes
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memOff2
    --
    -- > unOff memOff2 <= unCount (byteCountMem memRead2 - byteCountType @e)
    -> Count e
    -- ^ /memCount/ - Number of elements of type __@e@__ to compare as binary
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memCount
    --
    -- > unCountBytes memCount + unOff memOff1 <= unCount (byteCountMem memRead1 - byteCountType @e)
    --
    -- > unCountBytes memCount + unOff memOff2 <= unCount (byteCountMem memRead2 - byteCountType @e)
    -> Ordering

-- | Type class that can be implemented for a mutable data type that provides direct read
-- and write access to memory
class MemWrite mw where

  -- | Check if two mutable regions refer to the exact same region of memory
  --
  -- @since 0.3.0
  isSameMutMem :: mw s -> mw s -> Bool

  -- | Read an element with an offset in number of elements, rather than bytes as it is
  -- the case with `readByteOffMutMem`.
  --
  -- [Unsafe] Bounds are not checked. When precondition for @off@ argument is violated the
  -- result is either unpredictable output or failure with a segfault.
  --
  -- @since 0.3.0
  readOffMutMem :: (MonadPrim s m, Prim e)
    => mw s -- ^ /memRead/ - Memory region to read an element from
    -> Off e
    -- ^ /off/ - Offset in number of elements from the beginning of @memRead@
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= off
    --
    -- With offset applied it should still refer to the same memory region. For types that
    -- also implement `MemAlloc` this can be described as:
    --
    -- > count <- getByteCountMutMem memRead
    -- > unOff (toByteOff off) <= unCount (count - byteCountType @e)
    --
    -> m e
  readOffMutMem mw s
mw Off e
off = mw s -> Off Word8 -> m e
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> m e
readByteOffMutMem mw s
mw (Off e -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff Off e
off)
  {-# INLINE readOffMutMem #-}

  -- | Read an element with an offset in number of bytes.
  --
  -- [Unsafe] Bounds are not checked. When precondition for @off@ argument is violated the
  -- result is either unpredictable output or failure with a segfault.
  --
  -- @since 0.3.0
  readByteOffMutMem :: (MonadPrim s m, Prim e)
    => mw s -- ^ /memRead/ - Memory region to read an element from
    -> Off Word8
    -- ^ /off/ - Offset in number of elements from the beginning of @memRead@
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= off
    --
    -- With offset applied it should still refer to the same memory region. For types that
    -- also implement `MemAlloc` this can be described as:
    --
    -- > count <- getByteCountMutMem memRead
    -- > unOff (toByteOff off) <= unCount (count - byteCountType @e)
    --
    -> m e

  -- | Write an element with an offset in number of elements, rather than bytes as it is
  -- the case with `writeByteOffMutMem`.
  --
  -- [Unsafe] Bounds are not checked. When precondition for @off@ argument is violated the
  -- outcome is either heap corruption or failure with a segfault.
  --
  -- @since 0.3.0
  writeOffMutMem :: (MonadPrim s m, Prim e)
    => mw s -- ^ /memWrite/ - Memory region to write an element into
    -> Off e
    -- ^ /off/ - Offset in number of elements from the beginning of @memWrite@
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= off
    --
    -- With offset applied it should still refer to the same memory region. For types that
    -- also implement `MemAlloc` this can be described as:
    --
    -- > count <- getByteCountMutMem memWrite
    -- > unOff (toByteOff off) <= unCount (count - byteCountType @e)
    --
    -> e -- ^ /elt/ - Element to write
    -> m ()
  writeOffMutMem mw s
mw Off e
off = mw s -> Off Word8 -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> e -> m ()
writeByteOffMutMem mw s
mw (Off e -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff Off e
off)
  {-# INLINE writeOffMutMem #-}

  -- | Write an element with an offset in number of bytes.
  --
  -- [Unsafe] Bounds are not checked. When precondition for @off@ argument is violated the
  -- outcome is either heap corruption or failure with a segfault.
  --
  -- @since 0.3.0
  writeByteOffMutMem :: (MonadPrim s m, Prim e)
    => mw s -- ^ /memWrite/ - Memory region to write an element into
    -> Off Word8
    -- ^ /off/ - Offset in number of elements from the beginning of @memWrite@
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= off
    --
    -- With offset applied it should still refer to the same memory region. For types that
    -- also implement `MemAlloc` this can be described as:
    --
    -- > count <- getByteCountMutMem memWrite
    -- > unOff (toByteOff off) <= unCount (count - byteCountType @e)
    --
    -> e -> m ()

  -- | Copy contiguous chunk of memory from the source mutable memory into the target
  -- mutable `MBytes`. Source and target /may/ refer to overlapping memory regions.
  --
  -- [Unsafe] When any precondition for one of the offsets @memSourceOff@, @memTargetOff@
  -- or the element count @memCount@ is violated a call to this function can result in:
  -- copy of data that doesn't belong to @memSource@, heap corruption or failure with
  -- a segfault.
  --
  -- @since 0.3.0
  moveByteOffToMBytesMutMem ::
    (MonadPrim s m, Prim e)
    => mw s -- ^ /memSource/ - Source memory from where to copy
    -> Off Word8
    -- ^ /memSourceOff/ - Offset in number of bytes into source memory
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memSourceOff
    --
    -- With offset applied it should still refer to the same memory region. For types that
    -- also implement `MemAlloc` this can be described as:
    --
    -- > sourceByteCount <- getByteCountMutMem memSource
    -- > unOff (toByteOff memSourceOff) <= unCount (sourceByteCount - byteCountType @e)
    -> MBytes p s -- ^ /memTarget/ - Target memory into where to copy
    -> Off Word8
    -- ^ /memTargetOff/ - Offset in number of bytes into target memory where writing will start
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memTargetOff
    --
    -- With offset applied it should still refer to the same memory region. For types that
    -- also implement `MemAlloc` this can be described as:
    --
    -- > targetByteCount <- getByteCountMutMem memTarget
    -- > unOffBytes memTargetOff <= unCount (targetByteCount - byteCountType @e)
    -> Count e
    -- ^ /memCount/ - Number of elements of type __@e@__ to copy
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memCount
    --
    -- Both source and target memory regions must have enough memory to perform a copy
    -- of @memCount@ elements starting at their respective offsets. For types that also
    -- implement `MemAlloc` this can be described as:
    --
    -- > sourceByteCount <- getByteCountMutMem memSource
    -- > unOff memSourceOff + unCountBytes memCount <= unCount (sourceByteCount - byteCountType @e)
    --
    -- > targetByteCount <- getByteCountMutMem memTarget
    -- > unOff memTargetOff + unCountBytes memCount <= unCount (targetByteCount - byteCountType @e)
    -> m ()

  -- | Copy contiguous chunk of memory from the source mutable memory into the target
  -- `Ptr`. Source and target /may/ refer to overlapping memory regions.
  --
  -- [Unsafe] When any precondition for one of the offsets @memSourceOff@ or
  -- @memTargetOff@, a target pointer @memTarget@ or the element count @memCount@ is
  -- violated a call to this function can result in: copy of data that doesn't belong to
  -- @memSource@, heap corruption or failure with a segfault.
  --
  -- @since 0.3.0
  moveByteOffToPtrMutMem ::
    (MonadPrim s m, Prim e)
    => mw s -- ^ /memSource/ - Source memory from where to copy
    -> Off Word8
    -- ^ /memSourceOff/ - Offset in number of bytes into source memory
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memSourceOff
    --
    -- With offset applied it should still refer to the same memory region. For types that
    -- also implement `MemAlloc` this can be described as:
    --
    -- > sourceByteCount <- getByteCountMutMem memSource
    -- > unOff (toByteOff memSourceOff) <= unCount (sourceByteCount - byteCountType @e)
    -> Ptr e
    -- ^ /memTarget/ - Target memory into where to copy
    --
    -- /__Precondition:__/
    --
    -- Once the pointer is advanced by @memTargetOff@ the next @unCountBytes memCount@ bytes must
    -- still belong to the same region of memory @memTargetWrite@
    -> Off Word8
    -- ^ /memTargetOff/ - Offset in number of bytes into target memory where writing will start
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memTargetOff
    --
    -- Once the pointer is advanced by @memTargetOff@ it must still refer to the same
    -- memory region @memTarget@
    -> Count e
    -- ^ /memCount/ - Number of elements of type __@e@__ to copy
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memCount
    --
    -- Both source and target memory regions must have enough memory to perform a copy
    -- of @memCount@ elements starting at their respective offsets. For /memSource/ that also
    -- implements `MemAlloc` this can be described as:
    --
    -- > sourceByteCount <- getByteCountMutMem memSource
    -- > unOff memSourceOff + unCountBytes memCount <= unCount (sourceByteCount - byteCountType @e)
    -> m ()

  -- | Copy contiguous chunk of memory from the read only memory region into the target
  -- mutable memory region. Source and target /must not/ refer to the same memory region,
  -- otherwise that would imply that the source is not immutable which would be a
  -- violation of some other invariant elsewhere in the code.
  --
  -- [Unsafe] When any precondition for one of the offsets @memSourceOff@, @memTargetOff@
  -- or the element count @memCount@ is violated a call to this function can result in:
  -- copy of data that doesn't belong to @memSourceRead@, heap corruption or failure with
  -- a segfault.
  --
  -- @since 0.1.0
  copyByteOffMem :: (MonadPrim s m, MemRead mr, Prim e)
    => mr -- ^ /memSourceRead/ - Read-only source memory region from where to copy
    -> Off Word8
    -- ^ /memSourceOff/ - Offset into source memory in number of bytes
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memSourceOff
    --
    -- > unOff memSourceOff <= unCount (byteCountMem memSourceRead - byteCountType @e)
    -> mw s -- ^ /memTargetWrite/ - Target mutable memory
    -> Off Word8
    -- ^ /memTargetOff/ -  Offset into target memory in number of bytes
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memTargetOff
    --
    -- With offset applied it should still refer to the same memory region. For types that
    -- also implement `MemAlloc` this can be described as:
    --
    -- > targetByteCount <- getByteCountMutMem memTargetWrite
    -- > unOffBytes memTargetOff <= unCount (targetByteCount - byteCountType @e)
    -> Count e
    -- ^ /memCount/ - Number of elements of type __@e@__ to copy
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memCount
    --
    -- Both source and target memory regions must have enough memory to perform a copy
    -- of @memCount@ elements starting at their respective offsets. For @memSourceRead@:
    --
    -- > unOff memSourceOff + unCountBytes memCount <= unCount (byteCountMem memSourceRead - byteCountType @e)
    --
    -- and for @memTargetWrite@ that also implements `MemAlloc` this can be described as:
    --
    -- > targetByteCount <- getByteCountMutMem memTargetWrite
    -- > unOff memTargetOff + unCountBytes memCount <= unCount (targetByteCount - byteCountType @e)
    -> m ()

  -- | Copy contiguous chunk of memory from a mutable memory region into the target
  -- mutable memory region. Source and target /may/ refer to the same memory region.
  --
  -- [Unsafe] When any precondition for one of the offsets @memSourceOff@, @memTargetOff@
  -- or the element count @memCount@ is violated a call to this function can result in:
  -- copy of data that doesn't belong to @memSourceRead@, heap corruption or failure with
  -- a segfault.
  --
  -- @since 0.3.0
  moveByteOffMutMem :: (MonadPrim s m, MemWrite mw', Prim e)
    => mw' s -- ^ /memSource/ - Source memory from where to copy
    -> Off Word8
    -- ^ /memSourceOff/ - Offset in number of bytes into source memory
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memSourceOff
    --
    -- With offset applied it should still refer to the same memory region. For types that
    -- also implement `MemAlloc` this can be described as:
    --
    -- > sourceByteCount <- getByteCountMutMem memSource
    -- > unOffBytes memSourceOff <= unCount (sourceByteCount - byteCountType @e)
    -> mw s -- ^ /memTarget/ - Target memory into where to copy
    -> Off Word8
    -- ^ /memTargetOff/ -  Offset into target memory in number of bytes
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memTargetOff
    --
    -- With offset applied it should still refer to the same memory region. For types that
    -- also implement `MemAlloc` this can be described as:
    --
    -- > targetByteCount <- getByteCountMutMem memTarget
    -- > unOffBytes (toByteOff memTargetOff) <= unCount (targetByteCount - byteCountType @e)
    -> Count e
    -- ^ /memCount/ - Number of elements of type __@e@__ to copy
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memCount
    --
    -- Both source and target memory regions must have enough memory to perform a copy
    -- of @memCount@ elements starting at their respective offsets. For types that also
    -- implement `MemAlloc` this can be described as:
    --
    -- > sourceByteCount <- getByteCountMutMem memSource
    -- > unOff memSourceOff + unCountBytes memCount <= unCount (sourceByteCount - byteCountType @e)
    --
    -- > targetByteCount <- getByteCountMutMem memTarget
    -- > unOff memTargetOff + unCountBytes memCount <= unCount (targetByteCount - byteCountType @e)
    -> m ()

  -- TODO: Potential feature for the future implementation. Will require extra function in `Prim`.
  --setByteOffMutMem :: (MonadPrim s m, Prim e) => w s -> Off Word8 -> Count e -> e -> m ()

  -- | Write the same value @memCount@ times into each cell of @memTarget@ starting at an
  -- offset @memTargetOff@.
  --
  -- [Unsafe] Bounds are not checked. When precondition for @memTargetOff@ argument is
  -- violated the outcome is either heap corruption or failure with a segfault.
  --
  -- @since 0.3.0
  setMutMem
    :: (MonadPrim s m, Prim e)
    => mw s -- ^ /memTarget/ - Target memory into where to write the element
    -> Off e
    -- ^ /memTargetOff/ - Offset into target memory in number of elements at which element
    -- setting should start.
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memTargetOff
    --
    -- With offset applied it should still refer to the same memory region. For types that
    -- also implement `MemAlloc` this can be described as:
    --
    -- > targetByteCount <- getByteCountMutMem memTarget
    -- > unOffBytes memTargetOff <= unCount (targetByteCount - byteCountType @e)
    -> Count e
    -- ^ /memCount/ - Number of times the element @elt@ should be written
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memCount
    --
    -- Target memory region should have enough memory to perform a set operation of the
    -- supplied element @memCount@ number of times starting at the supplied offset. For
    -- types that also implement `MemAlloc` this can be described as:
    --
    -- > targetByteCount <- getByteCountMutMem memTarget
    -- > unCountBytes memCount + unOff memTargetOff <= unCount (targetByteCount - byteCountType @e)
    -> e
    -- ^ /elt/ - Element to write into memory cells. This function is strict with
    -- respect to element, which means that the even @memCount = 0@ it might be still
    -- fully evaluated.
    -> m ()

-- | Generalized memory allocation and pure/mutable state conversion.
class (MemRead (FrozenMem ma), MemWrite ma) => MemAlloc ma where
  -- | Memory region in the immutable state. Types for frozen and thawed states of
  -- memory region are in one-to-one correspondence, therefore @ma <-> FrozeMem ma@ will
  -- always uniquely identify each other, which is an extremely useful property when it
  -- comes to type inference.
  type FrozenMem ma = (fm :: Type) | fm -> ma

  -- | Extract the number of bytes a mutable memory region can hold, i.e. what is the
  -- total allocated size for this region. The size of a region can be changes and in some
  -- circuimstances even in place without copy, see `reallocMutMem` for more info.
  --
  -- ====__Examples__
  --
  -- >>> m <- allocMutMem (10 :: Count Int64) :: IO (MBytes 'Pin RW)
  -- >>> getByteCountMutMem m
  -- Count {unCount = 80}
  --
  -- @since 0.3.0
  getByteCountMutMem :: MonadPrim s m => ma s -> m (Count Word8)

  -- | Allocate a mutable memory region for specified number of elements. Memory is not
  -- reset and will likely hold some garbage data, therefore prefer to use `allocZeroMutMem`,
  -- unless it is guaranteed that all of allocated memory will be overwritten.
  --
  -- [Unsafe] When any of preconditions for @memCount@ argument is violated the outcome is
  -- unpredictable. One possible outcome is termination with
  -- `Control.Exception.HeapOverflow` async exception. In a pure setting, such as when
  -- executed within `runST`, if allocated memory is not fully overwritten it can lead to
  -- violation of referential transparency, because contents of newly allocated region is
  -- non-determinstic.
  --
  -- @since 0.3.0
  allocMutMem :: (Prim e, MonadPrim s m)
    => Count e
    -- ^ /memCount/ - Amount of memory to allocate for the region in number of elements of
    -- type __@e@__
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memCount
    --
    -- Possibility of overflow:
    --
    -- > memCount <= fromByteCount maxBound
    --
    -- When converted to bytes the value should be less then available physical memory
    -> m (ma s)

  -- | Convert the state of an immutable memory region to the mutable one. This is a no
  -- copy operation, as such it is fast, but dangerous. See `thawCloneMutMem` for a safe
  -- alternative.
  --
  -- [Unsafe] This function makes it possible to break referential transparency, because
  -- any subsequent destructive operation to the mutable region of memory will also be
  -- reflected in the frozen immutable type as well.
  --
  -- @since 0.1.0
  thawMem :: MonadPrim s m => FrozenMem ma -> m (ma s)

  -- | Convert the state of a mutable memory region to the immutable one. This is a no
  -- copy operation, as such it is fast, but dangerous. See `freezeCopyMem` for a safe alternative.
  --
  -- [Unsafe] It makes it possible to break referential transparency, because any
  -- subsequent destructive operation to the mutable region of memory will also be
  -- reflected in the frozen immutable type as well.
  --
  -- @since 0.3.0
  freezeMutMem :: MonadPrim s m => ma s -> m (FrozenMem ma)

  -- | Either grow or shrink currently allocated mutable region of memory. For some
  -- implementations it might be possible to change the size of the allocated region
  -- in-place, i.e. without copy. However in all implementations there is a good chance
  -- that the memory region has to be allocated anew, in which case all of the contents
  -- up to the minimum of new and old sizes will get copied over. After the resize
  -- operation is complete the supplied @memSource@ region must not be used
  -- anymore. Moreover, no reference to the old one should be kept in order to allow
  -- garbage collection of the original in case a new one had to be allocated.
  --
  -- Default implementation is `defaultReallocMutMem`
  --
  -- [Unsafe] Undefined behavior when @memSource@ is used afterwards. The same unsafety
  -- notice from `allocMutMem` with regards to @memCount@ is applicable here as well.
  --
  -- @since 0.3.0
  reallocMutMem :: (MonadPrim s m, Prim e)
    => ma s
    -- ^ /memSource/ - Source memory region to resize
    -> Count e
    -- ^ /memCount/ - Number of elements for the reallocated memory region
    --
    -- /__Preconditions:__/
    --
    -- > 0 <= memCount
    --
    -- Should be less then available physical memory
    -> m (ma s)
  reallocMutMem = ma s -> Count e -> m (ma s)
forall e (ma :: * -> *) s (m :: * -> *).
(Prim e, MemAlloc ma, MonadPrim s m) =>
ma s -> Count e -> m (ma s)
defaultReallocMutMem
  {-# INLINE reallocMutMem #-}

instance MemRead (UArray e) where
  isSameMem :: UArray e -> UArray e -> Bool
isSameMem = UArray e -> UArray e -> Bool
forall a b. UArray a -> UArray b -> Bool
isSameUArray
  {-# INLINE isSameMem #-}
  byteCountMem :: UArray e -> Count Word8
byteCountMem = Bytes 'Inc -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem (Bytes 'Inc -> Count Word8)
-> (UArray e -> Bytes 'Inc) -> UArray e -> Count Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes
  {-# INLINE byteCountMem #-}
  indexOffMem :: UArray e -> Off e -> e
indexOffMem UArray e
a = Bytes 'Inc -> Off e -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off e -> e
indexOffMem (UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes UArray e
a)
  {-# INLINE indexOffMem #-}
  indexByteOffMem :: UArray e -> Off Word8 -> e
indexByteOffMem UArray e
a = Bytes 'Inc -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem (UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes UArray e
a)
  {-# INLINE indexByteOffMem #-}
  copyByteOffToMBytesMem :: UArray e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem UArray e
a = Bytes 'Inc
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e (p :: Pinned).
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem (UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes UArray e
a)
  {-# INLINE copyByteOffToMBytesMem #-}
  copyByteOffToPtrMem :: UArray e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem UArray e
a = Bytes 'Inc -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem (UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes UArray e
a)
  {-# INLINE copyByteOffToPtrMem #-}
  compareByteOffToPtrMem :: UArray e
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem UArray e
a = Bytes 'Inc
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem (UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes UArray e
a)
  {-# INLINE compareByteOffToPtrMem #-}
  compareByteOffToBytesMem :: UArray e
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem UArray e
a = Bytes 'Inc
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall mr e (p :: Pinned).
(MemRead mr, Prim e) =>
mr -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem (UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes UArray e
a)
  {-# INLINE compareByteOffToBytesMem #-}
  compareByteOffMem :: mr' -> Off Word8 -> UArray e -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem1 Off Word8
off1 UArray e
a = mr' -> Off Word8 -> Bytes 'Inc -> Off Word8 -> Count e -> Ordering
forall mr mr' e.
(MemRead mr, MemRead mr', Prim e) =>
mr' -> Off Word8 -> mr -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem1 Off Word8
off1 (UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes UArray e
a)
  {-# INLINE compareByteOffMem #-}

instance MemWrite (UMArray e) where
  isSameMutMem :: UMArray e s -> UMArray e s -> Bool
isSameMutMem = UMArray e s -> UMArray e s -> Bool
forall a b s. UMArray a s -> UMArray b s -> Bool
isSameUMArray
  {-# INLINE isSameMutMem #-}
  readOffMutMem :: UMArray e s -> Off e -> m e
readOffMutMem UMArray e s
ma = MBytes 'Inc s -> Off e -> m e
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> m e
readOffMutMem (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
  {-# INLINE readOffMutMem #-}
  readByteOffMutMem :: UMArray e s -> Off Word8 -> m e
readByteOffMutMem UMArray e s
ma = MBytes 'Inc s -> Off Word8 -> m e
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> m e
readByteOffMutMem (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
  {-# INLINE readByteOffMutMem #-}
  writeOffMutMem :: UMArray e s -> Off e -> e -> m ()
writeOffMutMem UMArray e s
ma = MBytes 'Inc s -> Off e -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> e -> m ()
writeOffMutMem (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
  {-# INLINE writeOffMutMem #-}
  writeByteOffMutMem :: UMArray e s -> Off Word8 -> e -> m ()
writeByteOffMutMem UMArray e s
ma = MBytes 'Inc s -> Off Word8 -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> e -> m ()
writeByteOffMutMem (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
  {-# INLINE writeByteOffMutMem #-}
  moveByteOffToPtrMutMem :: UMArray e s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem UMArray e s
ma = MBytes 'Inc s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
  {-# INLINE moveByteOffToPtrMutMem #-}
  moveByteOffToMBytesMutMem :: UMArray e s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem UMArray e s
ma = MBytes 'Inc s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e (p :: Pinned).
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
  {-# INLINE moveByteOffToMBytesMutMem #-}
  copyByteOffMem :: mr -> Off Word8 -> UMArray e s -> Off Word8 -> Count e -> m ()
copyByteOffMem mr
src Off Word8
srcOff UMArray e s
ma = mr -> Off Word8 -> MBytes 'Inc s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) mr e.
(MemWrite mw, MonadPrim s m, MemRead mr, Prim e) =>
mr -> Off Word8 -> mw s -> Off Word8 -> Count e -> m ()
copyByteOffMem mr
src Off Word8
srcOff (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
  {-# INLINE copyByteOffMem #-}
  moveByteOffMutMem :: mw' s -> Off Word8 -> UMArray e s -> Off Word8 -> Count e -> m ()
moveByteOffMutMem mw' s
src Off Word8
srcOff UMArray e s
ma = mw' s -> Off Word8 -> MBytes 'Inc s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) (mw' :: * -> *) e.
(MemWrite mw, MonadPrim s m, MemWrite mw', Prim e) =>
mw' s -> Off Word8 -> mw s -> Off Word8 -> Count e -> m ()
moveByteOffMutMem mw' s
src Off Word8
srcOff (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
  {-# INLINE moveByteOffMutMem #-}
  setMutMem :: UMArray e s -> Off e -> Count e -> e -> m ()
setMutMem UMArray e s
ma = MBytes 'Inc s -> Off e -> Count e -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> Count e -> e -> m ()
setMutMem (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
  {-# INLINE setMutMem #-}

instance MemAlloc (UMArray e) where
  type FrozenMem (UMArray e) = UArray e
  getByteCountMutMem :: UMArray e s -> m (Count Word8)
getByteCountMutMem = MBytes 'Inc s -> m (Count Word8)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (Count Word8)
getByteCountMutMem (MBytes 'Inc s -> m (Count Word8))
-> (UMArray e s -> MBytes 'Inc s) -> UMArray e s -> m (Count Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes
  {-# INLINE getByteCountMutMem #-}
  allocMutMem :: Count e -> m (UMArray e s)
allocMutMem = (MBytes 'Inc s -> UMArray e s)
-> m (MBytes 'Inc s) -> m (UMArray e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes 'Inc s -> UMArray e s
forall (p :: Pinned) s e. MBytes p s -> UMArray e s
toUMArrayMBytes (m (MBytes 'Inc s) -> m (UMArray e s))
-> (Count e -> m (MBytes 'Inc s)) -> Count e -> m (UMArray e s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Count e -> m (MBytes 'Inc s)
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Count e -> m (MBytes 'Inc s)
allocUnpinnedMBytes
  {-# INLINE allocMutMem #-}
  thawMem :: FrozenMem (UMArray e) -> m (UMArray e s)
thawMem = (MBytes 'Inc s -> UMArray e s)
-> m (MBytes 'Inc s) -> m (UMArray e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes 'Inc s -> UMArray e s
forall (p :: Pinned) s e. MBytes p s -> UMArray e s
toUMArrayMBytes (m (MBytes 'Inc s) -> m (UMArray e s))
-> (UArray e -> m (MBytes 'Inc s)) -> UArray e -> m (UMArray e s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bytes 'Inc -> m (MBytes 'Inc s)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
Bytes p -> m (MBytes p s)
thawBytes (Bytes 'Inc -> m (MBytes 'Inc s))
-> (UArray e -> Bytes 'Inc) -> UArray e -> m (MBytes 'Inc s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes
  {-# INLINE thawMem #-}
  freezeMutMem :: UMArray e s -> m (FrozenMem (UMArray e))
freezeMutMem = (Bytes 'Inc -> UArray e) -> m (Bytes 'Inc) -> m (UArray e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bytes 'Inc -> UArray e
forall (p :: Pinned) e. Bytes p -> UArray e
toUArrayBytes (m (Bytes 'Inc) -> m (UArray e))
-> (UMArray e s -> m (Bytes 'Inc)) -> UMArray e s -> m (UArray e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MBytes 'Inc s -> m (Bytes 'Inc)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes (MBytes 'Inc s -> m (Bytes 'Inc))
-> (UMArray e s -> MBytes 'Inc s) -> UMArray e s -> m (Bytes 'Inc)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes
  {-# INLINE freezeMutMem #-}
  reallocMutMem :: UMArray e s -> Count e -> m (UMArray e s)
reallocMutMem UMArray e s
ma = (MBytes 'Inc s -> UMArray e s)
-> m (MBytes 'Inc s) -> m (UMArray e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes 'Inc s -> UMArray e s
forall (p :: Pinned) s e. MBytes p s -> UMArray e s
toUMArrayMBytes (m (MBytes 'Inc s) -> m (UMArray e s))
-> (Count e -> m (MBytes 'Inc s)) -> Count e -> m (UMArray e s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MBytes 'Inc s -> Count e -> m (MBytes 'Inc s)
forall e (p :: Pinned) (m :: * -> *) s.
(MonadPrim s m, Typeable p, Prim e) =>
MBytes p s -> Count e -> m (MBytes p s)
reallocMBytes (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
  {-# INLINE reallocMutMem #-}


instance MemRead ByteString where
  isSameMem :: ByteString -> ByteString -> Bool
isSameMem ByteString
bs1 ByteString
bs2 =
    IO Bool -> Bool
forall a. IO a -> a
unsafeInlineIO (IO Bool -> Bool) -> IO Bool -> Bool
forall a b. (a -> b) -> a -> b
$
    ByteString -> (Ptr Word8 -> IO Bool) -> IO Bool
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs1 ((Ptr Word8 -> IO Bool) -> IO Bool)
-> (Ptr Word8 -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr1 ->
      ByteString -> (Ptr Word8 -> IO Bool) -> IO Bool
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs2 ((Ptr Word8 -> IO Bool) -> IO Bool)
-> (Ptr Word8 -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr2 -> -- Can refer to the same memory but sliced differently:
        Bool -> IO Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ptr Word8
ptr1 Ptr Word8 -> Ptr Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== (Ptr Word8
ptr2 :: Ptr Word8) Bool -> Bool -> Bool
&& ByteString -> Int
BS.length ByteString
bs1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> Int
BS.length ByteString
bs2)
  {-# INLINE isSameMem #-}
  byteCountMem :: ByteString -> Count Word8
byteCountMem = Int -> Count Word8
forall e. Int -> Count e
Count (Int -> Count Word8)
-> (ByteString -> Int) -> ByteString -> Count Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
BS.length
  {-# INLINE byteCountMem #-}
  indexOffMem :: ByteString -> Off e -> e
indexOffMem ByteString
bs Off e
i = IO e -> e
forall a. IO a -> a
unsafeInlineIO (IO e -> e) -> IO e -> e
forall a b. (a -> b) -> a -> b
$ ByteString -> (Ptr e -> IO e) -> IO e
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs (Ptr e -> Off e -> IO e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> m e
`readOffPtr` Off e
i)
  {-# INLINE indexOffMem #-}
  indexByteOffMem :: ByteString -> Off Word8 -> e
indexByteOffMem ByteString
bs Off Word8
i = IO e -> e
forall a. IO a -> a
unsafeInlineIO (IO e -> e) -> IO e -> e
forall a b. (a -> b) -> a -> b
$ ByteString -> (Ptr e -> IO e) -> IO e
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs (Ptr e -> Off Word8 -> IO e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> m e
`readByteOffPtr` Off Word8
i)
  {-# INLINE indexByteOffMem #-}
  copyByteOffToMBytesMem :: ByteString
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem ByteString
bs Off Word8
srcOff MBytes p s
mb Off Word8
dstOff Count e
c =
    ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
srcPtr ->
      Ptr e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffPtrToMBytes Ptr e
srcPtr Off Word8
srcOff MBytes p s
mb Off Word8
dstOff Count e
c
  {-# INLINE copyByteOffToMBytesMem #-}
  copyByteOffToPtrMem :: ByteString -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem ByteString
bs Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c =
    ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
srcPtr ->
      Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffPtrToPtr Ptr e
srcPtr Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c
  {-# INLINE copyByteOffToPtrMem #-}
  compareByteOffToPtrMem :: ByteString
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem ByteString
bs Off Word8
off1 Ptr e
ptr2 Off Word8
off2 Count e
c =
    ByteString -> (Ptr e -> m Ordering) -> m Ordering
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs ((Ptr e -> m Ordering) -> m Ordering)
-> (Ptr e -> m Ordering) -> m Ordering
forall a b. (a -> b) -> a -> b
$ \Ptr e
ptr1 ->
      Ordering -> m Ordering
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ordering -> m Ordering) -> Ordering -> m Ordering
forall a b. (a -> b) -> a -> b
$! Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> Ordering
forall e.
Prim e =>
Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> Ordering
compareByteOffPtrToPtr Ptr e
ptr1 Off Word8
off1 Ptr e
ptr2 Off Word8
off2 Count e
c
  {-# INLINE compareByteOffToPtrMem #-}
  compareByteOffToBytesMem :: ByteString
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem ByteString
bs Off Word8
off1 Bytes p
bytes Off Word8
off2 Count e
c =
    IO Ordering -> Ordering
forall a. IO a -> a
unsafeInlineIO (IO Ordering -> Ordering) -> IO Ordering -> Ordering
forall a b. (a -> b) -> a -> b
$
    ByteString -> (Ptr e -> IO Ordering) -> IO Ordering
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs ((Ptr e -> IO Ordering) -> IO Ordering)
-> (Ptr e -> IO Ordering) -> IO Ordering
forall a b. (a -> b) -> a -> b
$ \Ptr e
ptr1 ->
      Ordering -> IO Ordering
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ordering -> IO Ordering) -> Ordering -> IO Ordering
forall a b. (a -> b) -> a -> b
$! Ptr e -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall e (p :: Pinned).
Prim e =>
Ptr e -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffPtrToBytes Ptr e
ptr1 Off Word8
off1 Bytes p
bytes Off Word8
off2 Count e
c
  {-# INLINE compareByteOffToBytesMem #-}
  compareByteOffMem :: mr' -> Off Word8 -> ByteString -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem1 Off Word8
off1 ByteString
bs Off Word8
off2 Count e
c =
    IO Ordering -> Ordering
forall a. IO a -> a
unsafeInlineIO (IO Ordering -> Ordering) -> IO Ordering -> Ordering
forall a b. (a -> b) -> a -> b
$
    ByteString -> (Ptr e -> IO Ordering) -> IO Ordering
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs ((Ptr e -> IO Ordering) -> IO Ordering)
-> (Ptr e -> IO Ordering) -> IO Ordering
forall a b. (a -> b) -> a -> b
$ \Ptr e
ptr2 -> mr' -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> IO Ordering
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem mr'
mem1 Off Word8
off1 Ptr e
ptr2 Off Word8
off2 Count e
c
  {-# INLINE compareByteOffMem #-}

instance MemWrite MByteString where
  isSameMutMem :: MByteString s -> MByteString s -> Bool
isSameMutMem (MByteString ByteString
bs1) (MByteString ByteString
bs2) = ByteString -> ByteString -> Bool
forall mr. MemRead mr => mr -> mr -> Bool
isSameMem ByteString
bs1 ByteString
bs2
  {-# INLINE isSameMutMem #-}
  readOffMutMem :: MByteString s -> Off e -> m e
readOffMutMem (MByteString ByteString
mbs) Off e
i = ByteString -> (Ptr e -> m e) -> m e
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
mbs (Ptr e -> Off e -> m e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> m e
`readOffPtr` Off e
i)
  {-# INLINE readOffMutMem #-}
  readByteOffMutMem :: MByteString s -> Off Word8 -> m e
readByteOffMutMem (MByteString ByteString
mbs) Off Word8
i = ByteString -> (Ptr e -> m e) -> m e
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
mbs (Ptr e -> Off Word8 -> m e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> m e
`readByteOffPtr` Off Word8
i)
  {-# INLINE readByteOffMutMem #-}
  writeOffMutMem :: MByteString s -> Off e -> e -> m ()
writeOffMutMem (MByteString ByteString
mbs) Off e
i e
a = ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
mbs ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
ptr -> Ptr e -> Off e -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> e -> m ()
writeOffPtr Ptr e
ptr Off e
i e
a
  {-# INLINE writeOffMutMem #-}
  writeByteOffMutMem :: MByteString s -> Off Word8 -> e -> m ()
writeByteOffMutMem (MByteString ByteString
mbs) Off Word8
i e
a = ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
mbs ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
ptr -> Ptr e -> Off Word8 -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> e -> m ()
writeByteOffPtr Ptr e
ptr Off Word8
i e
a
  {-# INLINE writeByteOffMutMem #-}
  moveByteOffToPtrMutMem :: MByteString s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem (MByteString ByteString
fsrc) Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c =
    ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
fsrc ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
srcPtr -> Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffPtrToPtr Ptr e
srcPtr Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c
  {-# INLINE moveByteOffToPtrMutMem #-}
  moveByteOffToMBytesMutMem :: MByteString s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem (MByteString ByteString
fsrc) Off Word8
srcOff MBytes p s
dst Off Word8
dstOff Count e
c =
    ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
fsrc ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
srcPtr -> Ptr e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffPtrToMBytes Ptr e
srcPtr Off Word8
srcOff MBytes p s
dst Off Word8
dstOff Count e
c
  {-# INLINE moveByteOffToMBytesMutMem #-}
  copyByteOffMem :: mr -> Off Word8 -> MByteString s -> Off Word8 -> Count e -> m ()
copyByteOffMem mr
src Off Word8
srcOff (MByteString ByteString
fdst) Off Word8
dstOff Count e
c =
    ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
fdst ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
dstPtr -> mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem mr
src Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c
  {-# INLINE copyByteOffMem #-}
  moveByteOffMutMem :: mw' s -> Off Word8 -> MByteString s -> Off Word8 -> Count e -> m ()
moveByteOffMutMem mw' s
src Off Word8
srcOff (MByteString ByteString
fdst) Off Word8
dstOff Count e
c =
    ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
fdst ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
dstPtr -> mw' s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem mw' s
src Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c
  {-# INLINE moveByteOffMutMem #-}
  setMutMem :: MByteString s -> Off e -> Count e -> e -> m ()
setMutMem (MByteString ByteString
mbs) Off e
off Count e
c e
a = ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
mbs ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
ptr -> Ptr e -> Off e -> Count e -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> Count e -> e -> m ()
setOffPtr Ptr e
ptr Off e
off Count e
c e
a
  {-# INLINE setMutMem #-}

instance MemAlloc MByteString where
  type FrozenMem MByteString = ByteString
  getByteCountMutMem :: MByteString s -> m (Count Word8)
getByteCountMutMem (MByteString ByteString
bs) = Count Word8 -> m (Count Word8)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Count Word8 -> m (Count Word8)) -> Count Word8 -> m (Count Word8)
forall a b. (a -> b) -> a -> b
$! Int -> Count Word8
forall e. Int -> Count e
Count (ByteString -> Int
BS.length ByteString
bs)
  {-# INLINE getByteCountMutMem #-}
  allocMutMem :: Count e -> m (MByteString s)
allocMutMem Count e
c = do
    let cb :: Count Word8
cb = Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
c
    ForeignPtr Word8
fp <- Count Word8 -> m (ForeignPtr Word8)
forall s (m :: * -> *) e.
MonadPrim s m =>
Count Word8 -> m (ForeignPtr e)
mallocByteCountPlainForeignPtr Count Word8
cb
    MByteString s -> m (MByteString s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MByteString s -> m (MByteString s))
-> MByteString s -> m (MByteString s)
forall a b. (a -> b) -> a -> b
$ ByteString -> MByteString s
forall s. ByteString -> MByteString s
MByteString (ForeignPtr Word8 -> Int -> Int -> ByteString
PS ForeignPtr Word8
fp Int
0 (Count Word8 -> Int
coerce Count Word8
cb))
  {-# INLINE allocMutMem #-}
  thawMem :: FrozenMem MByteString -> m (MByteString s)
thawMem FrozenMem MByteString
bs = MByteString s -> m (MByteString s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MByteString s -> m (MByteString s))
-> MByteString s -> m (MByteString s)
forall a b. (a -> b) -> a -> b
$ ByteString -> MByteString s
forall s. ByteString -> MByteString s
MByteString ByteString
FrozenMem MByteString
bs
  {-# INLINE thawMem #-}
  freezeMutMem :: MByteString s -> m (FrozenMem MByteString)
freezeMutMem (MByteString ByteString
bs) = ByteString -> m ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
bs
  {-# INLINE freezeMutMem #-}
  reallocMutMem :: MByteString s -> Count e -> m (MByteString s)
reallocMutMem bsm :: MByteString s
bsm@(MByteString (PS ForeignPtr Word8
fp Int
o Int
n)) Count e
newc
    | Int
newn Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n = MByteString s -> Count e -> m (MByteString s)
forall e (ma :: * -> *) s (m :: * -> *).
(Prim e, MemAlloc ma, MonadPrim s m) =>
ma s -> Count e -> m (ma s)
defaultReallocMutMem MByteString s
bsm Count e
newc
    | Bool
otherwise = MByteString s -> m (MByteString s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MByteString s -> m (MByteString s))
-> MByteString s -> m (MByteString s)
forall a b. (a -> b) -> a -> b
$ ByteString -> MByteString s
forall s. ByteString -> MByteString s
MByteString (ForeignPtr Word8 -> Int -> Int -> ByteString
PS ForeignPtr Word8
fp Int
o Int
newn)
    where -- constant time slice if we need to reduce the size
      Count Int
newn = Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
newc
  {-# INLINE reallocMutMem #-}


instance MemRead T.Array where
  isSameMem :: Array -> Array -> Bool
isSameMem Array
a1 Array
a2 = Bytes 'Inc -> Bytes 'Inc -> Bool
forall mr. MemRead mr => mr -> mr -> Bool
isSameMem (Array -> Bytes 'Inc
T.fromArrayBytes Array
a1) (Array -> Bytes 'Inc
T.fromArrayBytes Array
a2)
  {-# INLINE isSameMem #-}
  byteCountMem :: Array -> Count Word8
byteCountMem = Bytes 'Inc -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem (Bytes 'Inc -> Count Word8)
-> (Array -> Bytes 'Inc) -> Array -> Count Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array -> Bytes 'Inc
T.fromArrayBytes
  {-# INLINE byteCountMem #-}
  indexOffMem :: Array -> Off e -> e
indexOffMem Array
a = Bytes 'Inc -> Off e -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off e -> e
indexOffMem (Array -> Bytes 'Inc
T.fromArrayBytes Array
a)
  {-# INLINE indexOffMem #-}
  indexByteOffMem :: Array -> Off Word8 -> e
indexByteOffMem Array
a = Bytes 'Inc -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem (Array -> Bytes 'Inc
T.fromArrayBytes Array
a)
  {-# INLINE indexByteOffMem #-}
  copyByteOffToMBytesMem :: Array -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem Array
a = Bytes 'Inc
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e (p :: Pinned).
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem (Array -> Bytes 'Inc
T.fromArrayBytes Array
a)
  {-# INLINE copyByteOffToMBytesMem #-}
  copyByteOffToPtrMem :: Array -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem Array
a = Bytes 'Inc -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem (Array -> Bytes 'Inc
T.fromArrayBytes Array
a)
  {-# INLINE copyByteOffToPtrMem #-}
  compareByteOffToPtrMem :: Array -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem Array
a = Bytes 'Inc
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem (Array -> Bytes 'Inc
T.fromArrayBytes Array
a)
  {-# INLINE compareByteOffToPtrMem #-}
  compareByteOffToBytesMem :: Array -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem Array
a = Bytes 'Inc
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall mr e (p :: Pinned).
(MemRead mr, Prim e) =>
mr -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem (Array -> Bytes 'Inc
T.fromArrayBytes Array
a)
  {-# INLINE compareByteOffToBytesMem #-}
  compareByteOffMem :: mr' -> Off Word8 -> Array -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem Off Word8
off1 Array
a = mr' -> Off Word8 -> Bytes 'Inc -> Off Word8 -> Count e -> Ordering
forall mr mr' e.
(MemRead mr, MemRead mr', Prim e) =>
mr' -> Off Word8 -> mr -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem Off Word8
off1 (Array -> Bytes 'Inc
T.fromArrayBytes Array
a)
  {-# INLINE compareByteOffMem #-}

instance MemAlloc T.MArray where
  type FrozenMem T.MArray = T.Array
  getByteCountMutMem :: MArray s -> m (Count Word8)
getByteCountMutMem = MBytes 'Inc s -> m (Count Word8)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Count Word8)
getByteCountMBytes (MBytes 'Inc s -> m (Count Word8))
-> (MArray s -> MBytes 'Inc s) -> MArray s -> m (Count Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes
  {-# INLINE getByteCountMutMem #-}
  allocMutMem :: Count e -> m (MArray s)
allocMutMem = (MBytes 'Inc s -> MArray s) -> m (MBytes 'Inc s) -> m (MArray s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes 'Inc s -> MArray s
forall (p :: Pinned) s. MBytes p s -> MArray s
T.toMArrayMBytes (m (MBytes 'Inc s) -> m (MArray s))
-> (Count e -> m (MBytes 'Inc s)) -> Count e -> m (MArray s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Count e -> m (MBytes 'Inc s)
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Count e -> m (MBytes 'Inc s)
allocUnpinnedMBytes
  {-# INLINE allocMutMem #-}
  thawMem :: FrozenMem MArray -> m (MArray s)
thawMem = (MBytes 'Inc s -> MArray s) -> m (MBytes 'Inc s) -> m (MArray s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes 'Inc s -> MArray s
forall (p :: Pinned) s. MBytes p s -> MArray s
T.toMArrayMBytes (m (MBytes 'Inc s) -> m (MArray s))
-> (Array -> m (MBytes 'Inc s)) -> Array -> m (MArray s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bytes 'Inc -> m (MBytes 'Inc s)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
Bytes p -> m (MBytes p s)
thawBytes (Bytes 'Inc -> m (MBytes 'Inc s))
-> (Array -> Bytes 'Inc) -> Array -> m (MBytes 'Inc s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array -> Bytes 'Inc
T.fromArrayBytes
  {-# INLINE thawMem #-}
  freezeMutMem :: MArray s -> m (FrozenMem MArray)
freezeMutMem = (Bytes 'Inc -> Array) -> m (Bytes 'Inc) -> m Array
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bytes 'Inc -> Array
forall (p :: Pinned). Bytes p -> Array
T.toArrayBytes (m (Bytes 'Inc) -> m Array)
-> (MArray s -> m (Bytes 'Inc)) -> MArray s -> m Array
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MBytes 'Inc s -> m (Bytes 'Inc)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes (MBytes 'Inc s -> m (Bytes 'Inc))
-> (MArray s -> MBytes 'Inc s) -> MArray s -> m (Bytes 'Inc)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes
  {-# INLINE freezeMutMem #-}
  reallocMutMem :: MArray s -> Count e -> m (MArray s)
reallocMutMem MArray s
m = (MBytes 'Inc s -> MArray s) -> m (MBytes 'Inc s) -> m (MArray s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes 'Inc s -> MArray s
forall (p :: Pinned) s. MBytes p s -> MArray s
T.toMArrayMBytes (m (MBytes 'Inc s) -> m (MArray s))
-> (Count e -> m (MBytes 'Inc s)) -> Count e -> m (MArray s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MBytes 'Inc s -> Count e -> m (MBytes 'Inc s)
forall e (p :: Pinned) (m :: * -> *) s.
(MonadPrim s m, Typeable p, Prim e) =>
MBytes p s -> Count e -> m (MBytes p s)
reallocMBytes (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
  {-# INLINE reallocMutMem #-}

instance MemWrite T.MArray where
  isSameMutMem :: MArray s -> MArray s -> Bool
isSameMutMem MArray s
ma1 MArray s
ma2 = MBytes 'Inc s -> MBytes 'Inc s -> Bool
forall (mw :: * -> *) s. MemWrite mw => mw s -> mw s -> Bool
isSameMutMem (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
ma1) (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
ma2)
  {-# INLINE isSameMutMem #-}
  readOffMutMem :: MArray s -> Off e -> m e
readOffMutMem MArray s
m = MBytes 'Inc s -> Off e -> m e
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off e -> m e
readOffMBytes (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
  {-# INLINE readOffMutMem #-}
  readByteOffMutMem :: MArray s -> Off Word8 -> m e
readByteOffMutMem MArray s
m = MBytes 'Inc s -> Off Word8 -> m e
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off Word8 -> m e
readByteOffMBytes (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
  {-# INLINE readByteOffMutMem #-}
  writeOffMutMem :: MArray s -> Off e -> e -> m ()
writeOffMutMem MArray s
m = MBytes 'Inc s -> Off e -> e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off e -> e -> m ()
writeOffMBytes (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
  {-# INLINE writeOffMutMem #-}
  writeByteOffMutMem :: MArray s -> Off Word8 -> e -> m ()
writeByteOffMutMem MArray s
m = MBytes 'Inc s -> Off Word8 -> e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off Word8 -> e -> m ()
writeByteOffMBytes (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
  {-# INLINE writeByteOffMutMem #-}
  moveByteOffToPtrMutMem :: MArray s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem MArray s
m = MBytes 'Inc s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffMBytesToPtr (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
  {-# INLINE moveByteOffToPtrMutMem #-}
  moveByteOffToMBytesMutMem :: MArray s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem MArray s
m = MBytes 'Inc s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (ps :: Pinned) (pd :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes ps s
-> Off Word8 -> MBytes pd s -> Off Word8 -> Count e -> m ()
moveByteOffMBytesToMBytes (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
  {-# INLINE moveByteOffToMBytesMutMem #-}
  moveByteOffMutMem :: mw' s -> Off Word8 -> MArray s -> Off Word8 -> Count e -> m ()
moveByteOffMutMem mw' s
src Off Word8
srcOff MArray s
m = mw' s -> Off Word8 -> MBytes 'Inc s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e (p :: Pinned).
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem mw' s
src Off Word8
srcOff (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
  {-# INLINE moveByteOffMutMem #-}
  copyByteOffMem :: mr -> Off Word8 -> MArray s -> Off Word8 -> Count e -> m ()
copyByteOffMem mr
src Off Word8
srcOff MArray s
m = mr -> Off Word8 -> MBytes 'Inc s -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e (p :: Pinned).
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem mr
src Off Word8
srcOff (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
  {-# INLINE copyByteOffMem #-}
  setMutMem :: MArray s -> Off e -> Count e -> e -> m ()
setMutMem MArray s
m = MBytes 'Inc s -> Off e -> Count e -> e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off e -> Count e -> e -> m ()
setMBytes (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
  {-# INLINE setMutMem #-}

instance MemRead T.Text where
  isSameMem :: Text -> Text -> Bool
isSameMem (T.Text Array
a1 Int
o1 Int
n1) (T.Text Array
a2 Int
o2 Int
n2) = Array -> Array -> Bool
forall mr. MemRead mr => mr -> mr -> Bool
isSameMem Array
a1 Array
a2 Bool -> Bool -> Bool
&& Int
o1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
o2 Bool -> Bool -> Bool
&& Int
n1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n2
  {-# INLINE isSameMem #-}
  byteCountMem :: Text -> Count Word8
byteCountMem (T.Text Array
_ Int
_ Int
n) = Count Word16 -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount (Int -> Count Word16
forall e. Int -> Count e
Count Int
n :: Count Word16)
  {-# INLINE byteCountMem #-}
  indexByteOffMem :: Text -> Off Word8 -> e
indexByteOffMem (T.Text Array
a Int
o Int
_) Off Word8
i = Array -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem Array
a (Off Word16 -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff (Int -> Off Word16
forall e. Int -> Off e
Off Int
o :: Off Word16) Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
i)
  {-# INLINE indexByteOffMem #-}
  copyByteOffToMBytesMem :: Text -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem (T.Text Array
a Int
o Int
_) Off Word8
i =
    Array -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e (p :: Pinned).
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem Array
a (Off Word16 -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff (Int -> Off Word16
forall e. Int -> Off e
Off Int
o :: Off Word16) Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
i)
  {-# INLINE copyByteOffToMBytesMem #-}
  copyByteOffToPtrMem :: Text -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem (T.Text Array
a Int
o Int
_) Off Word8
i =
    Array -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem Array
a (Off Word16 -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff (Int -> Off Word16
forall e. Int -> Off e
Off Int
o :: Off Word16) Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
i)
  {-# INLINE copyByteOffToPtrMem #-}
  compareByteOffToPtrMem :: Text -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem (T.Text Array
a Int
o Int
_) Off Word8
off =
    Array -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem Array
a (Off Word16 -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff (Int -> Off Word16
forall e. Int -> Off e
Off Int
o :: Off Word16) Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
off)
  {-# INLINE compareByteOffToPtrMem #-}
  compareByteOffToBytesMem :: Text -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem (T.Text Array
a Int
o Int
_) Off Word8
off =
    Array -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall mr e (p :: Pinned).
(MemRead mr, Prim e) =>
mr -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem Array
a (Off Word16 -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff (Int -> Off Word16
forall e. Int -> Off e
Off Int
o :: Off Word16) Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
off)
  {-# INLINE compareByteOffToBytesMem #-}
  compareByteOffMem :: mr' -> Off Word8 -> Text -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem Off Word8
off1 (T.Text Array
a Int
o Int
_) Off Word8
off2 =
    mr' -> Off Word8 -> Array -> Off Word8 -> Count e -> Ordering
forall mr mr' e.
(MemRead mr, MemRead mr', Prim e) =>
mr' -> Off Word8 -> mr -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem Off Word8
off1 Array
a (Off Word16 -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff (Int -> Off Word16
forall e. Int -> Off e
Off Int
o :: Off Word16) Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
off2)
  {-# INLINE compareByteOffMem #-}


instance MemRead ShortByteString where
  isSameMem :: ShortByteString -> ShortByteString -> Bool
isSameMem ShortByteString
sbs1 ShortByteString
sbs2 = Bytes 'Inc -> Bytes 'Inc -> Bool
forall mr. MemRead mr => mr -> mr -> Bool
isSameMem (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs1) (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs2)
  {-# INLINE isSameMem #-}
  byteCountMem :: ShortByteString -> Count Word8
byteCountMem = Bytes 'Inc -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem (Bytes 'Inc -> Count Word8)
-> (ShortByteString -> Bytes 'Inc)
-> ShortByteString
-> Count Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> Bytes 'Inc
fromShortByteStringBytes
  {-# INLINE byteCountMem #-}
  indexOffMem :: ShortByteString -> Off e -> e
indexOffMem ShortByteString
sbs = Bytes 'Inc -> Off e -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off e -> e
indexOffMem (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs)
  {-# INLINE indexOffMem #-}
  indexByteOffMem :: ShortByteString -> Off Word8 -> e
indexByteOffMem ShortByteString
sbs = Bytes 'Inc -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs)
  {-# INLINE indexByteOffMem #-}
  copyByteOffToMBytesMem :: ShortByteString
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem ShortByteString
sbs = Bytes 'Inc
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e (p :: Pinned).
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs)
  {-# INLINE copyByteOffToMBytesMem #-}
  copyByteOffToPtrMem :: ShortByteString
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem ShortByteString
sbs = Bytes 'Inc -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs)
  {-# INLINE copyByteOffToPtrMem #-}
  compareByteOffToPtrMem :: ShortByteString
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem ShortByteString
sbs = Bytes 'Inc
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs)
  {-# INLINE compareByteOffToPtrMem #-}
  compareByteOffToBytesMem :: ShortByteString
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem ShortByteString
sbs = Bytes 'Inc
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall mr e (p :: Pinned).
(MemRead mr, Prim e) =>
mr -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs)
  {-# INLINE compareByteOffToBytesMem #-}
  compareByteOffMem :: mr'
-> Off Word8 -> ShortByteString -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem Off Word8
off1 ShortByteString
sbs = mr' -> Off Word8 -> Bytes 'Inc -> Off Word8 -> Count e -> Ordering
forall mr mr' e.
(MemRead mr, MemRead mr', Prim e) =>
mr' -> Off Word8 -> mr -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem Off Word8
off1 (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs)
  {-# INLINE compareByteOffMem #-}

-- | A wrapper that adds a phantom state token. It can be used with types that either
-- don't have such state token or are designed to work in `IO` and therefore restricted
-- to `RealWorld`. Using this wrapper is very much unsafe, so make sure you know what you are
-- doing.
newtype MemState a s = MemState { MemState a s -> a
unMemState :: a }

instance MemWrite (MemState (ForeignPtr a)) where
  isSameMutMem :: MemState (ForeignPtr a) s -> MemState (ForeignPtr a) s -> Bool
isSameMutMem (MemState ForeignPtr a
fptr1) (MemState ForeignPtr a
fptr2) =
    IO Bool -> Bool
forall a. IO a -> a
unsafeInlineIO (IO Bool -> Bool) -> IO Bool -> Bool
forall a b. (a -> b) -> a -> b
$
    ForeignPtr a -> (Ptr Word8 -> IO Bool) -> IO Bool
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ForeignPtr a
fptr1 ((Ptr Word8 -> IO Bool) -> IO Bool)
-> (Ptr Word8 -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr1 ->
      ForeignPtr a -> (Ptr Word8 -> IO Bool) -> IO Bool
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ForeignPtr a
fptr2 ((Ptr Word8 -> IO Bool) -> IO Bool)
-> (Ptr Word8 -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr2 ->
        Bool -> IO Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ptr Word8
ptr1 Ptr Word8 -> Ptr Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== (Ptr Word8
ptr2 :: Ptr Word8))
  {-# INLINE isSameMutMem #-}
  readOffMutMem :: MemState (ForeignPtr a) s -> Off e -> m e
readOffMutMem (MemState ForeignPtr a
fptr) Off e
i = ForeignPtr a -> (Ptr a -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fptr ((Ptr a -> m e) -> m e) -> (Ptr a -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> Ptr e -> Off e -> m e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> m e
readOffPtr (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Off e
i
  {-# INLINE readOffMutMem #-}
  readByteOffMutMem :: MemState (ForeignPtr a) s -> Off Word8 -> m e
readByteOffMutMem (MemState ForeignPtr a
fptr) Off Word8
i =
    ForeignPtr a -> (Ptr a -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fptr ((Ptr a -> m e) -> m e) -> (Ptr a -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> Ptr e -> Off Word8 -> m e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> m e
readByteOffPtr (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Off Word8
i
  {-# INLINE readByteOffMutMem #-}
  writeOffMutMem :: MemState (ForeignPtr a) s -> Off e -> e -> m ()
writeOffMutMem (MemState ForeignPtr a
fptr) Off e
i e
a = ForeignPtr a -> (Ptr a -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fptr ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> Ptr e -> Off e -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> e -> m ()
writeOffPtr (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Off e
i e
a
  {-# INLINE writeOffMutMem #-}
  writeByteOffMutMem :: MemState (ForeignPtr a) s -> Off Word8 -> e -> m ()
writeByteOffMutMem (MemState ForeignPtr a
fptr) Off Word8
i e
a =
    ForeignPtr a -> (Ptr a -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fptr ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> Ptr e -> Off Word8 -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> e -> m ()
writeByteOffPtr (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Off Word8
i e
a
  {-# INLINE writeByteOffMutMem #-}
  moveByteOffToPtrMutMem :: MemState (ForeignPtr a) s
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem (MemState ForeignPtr a
fsrc) Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c =
    ForeignPtr a -> (Ptr a -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fsrc ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
srcPtr -> Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffPtrToPtr (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
srcPtr) Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c
  {-# INLINE moveByteOffToPtrMutMem #-}
  moveByteOffToMBytesMutMem :: MemState (ForeignPtr a) s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem (MemState ForeignPtr a
fsrc) Off Word8
srcOff MBytes p s
dst Off Word8
dstOff Count e
c =
    ForeignPtr a -> (Ptr a -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fsrc ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
srcPtr -> Ptr e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffPtrToMBytes (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
srcPtr) Off Word8
srcOff MBytes p s
dst Off Word8
dstOff Count e
c
  {-# INLINE moveByteOffToMBytesMutMem #-}
  copyByteOffMem :: mr
-> Off Word8
-> MemState (ForeignPtr a) s
-> Off Word8
-> Count e
-> m ()
copyByteOffMem mr
src Off Word8
srcOff (MemState ForeignPtr a
fdst) Off Word8
dstOff Count e
c =
    ForeignPtr a -> (Ptr a -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fdst ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
dstPtr ->
       mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem mr
src Off Word8
srcOff (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
dstPtr) Off Word8
dstOff Count e
c
  {-# INLINE copyByteOffMem #-}
  moveByteOffMutMem :: mw' s
-> Off Word8
-> MemState (ForeignPtr a) s
-> Off Word8
-> Count e
-> m ()
moveByteOffMutMem mw' s
src Off Word8
srcOff (MemState ForeignPtr a
fdst) Off Word8
dstOff Count e
c =
    ForeignPtr a -> (Ptr a -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fdst ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
dstPtr ->
       mw' s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem mw' s
src Off Word8
srcOff (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
dstPtr) Off Word8
dstOff Count e
c
  {-# INLINE moveByteOffMutMem #-}
  setMutMem :: MemState (ForeignPtr a) s -> Off e -> Count e -> e -> m ()
setMutMem (MemState ForeignPtr a
fptr) Off e
off Count e
c e
a = ForeignPtr a -> (Ptr a -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fptr ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> Ptr e -> Off e -> Count e -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> Count e -> e -> m ()
setOffPtr (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Off e
off Count e
c e
a
  {-# INLINE setMutMem #-}

--
-- @since 0.3.0
modifyFetchOldMutMem ::
     (MemWrite mw, MonadPrim s m, Prim e) => mw s -> Off e -> (e -> e) -> m e
modifyFetchOldMutMem :: mw s -> Off e -> (e -> e) -> m e
modifyFetchOldMutMem mw s
mem Off e
o e -> e
f = mw s -> Off e -> (e -> m e) -> m e
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> (e -> m e) -> m e
modifyFetchOldMutMemM mw s
mem Off e
o (e -> m e
forall (f :: * -> *) a. Applicative f => a -> f a
pure (e -> m e) -> (e -> e) -> e -> m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> e
f)
{-# INLINE modifyFetchOldMutMem #-}


--
-- @since 0.3.0
modifyFetchNewMutMem ::
     (MemWrite mw, MonadPrim s m, Prim e) => mw s -> Off e -> (e -> e) -> m e
modifyFetchNewMutMem :: mw s -> Off e -> (e -> e) -> m e
modifyFetchNewMutMem mw s
mem Off e
o e -> e
f = mw s -> Off e -> (e -> m e) -> m e
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> (e -> m e) -> m e
modifyFetchNewMutMemM mw s
mem Off e
o (e -> m e
forall (f :: * -> *) a. Applicative f => a -> f a
pure (e -> m e) -> (e -> e) -> e -> m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> e
f)
{-# INLINE modifyFetchNewMutMem #-}


--
-- @since 0.3.0
modifyFetchOldMutMemM ::
     (MemWrite mw, MonadPrim s m, Prim e) => mw s -> Off e -> (e -> m e) -> m e
modifyFetchOldMutMemM :: mw s -> Off e -> (e -> m e) -> m e
modifyFetchOldMutMemM mw s
mem Off e
o e -> m e
f = do
  e
a <- mw s -> Off e -> m e
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> m e
readOffMutMem mw s
mem Off e
o
  e
a e -> m () -> m e
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (mw s -> Off e -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> e -> m ()
writeOffMutMem mw s
mem Off e
o (e -> m ()) -> m e -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< e -> m e
f e
a)
{-# INLINE modifyFetchOldMutMemM #-}


--
-- @since 0.3.0
modifyFetchNewMutMemM ::
     (MemWrite mw, MonadPrim s m, Prim e) => mw s -> Off e -> (e -> m e) -> m e
modifyFetchNewMutMemM :: mw s -> Off e -> (e -> m e) -> m e
modifyFetchNewMutMemM mw s
mem Off e
o e -> m e
f = do
  e
a <- mw s -> Off e -> m e
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> m e
readOffMutMem mw s
mem Off e
o
  e
a' <- e -> m e
f e
a
  e
a' e -> m () -> m e
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ mw s -> Off e -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> e -> m ()
writeOffMutMem mw s
mem Off e
o e
a'
{-# INLINE modifyFetchNewMutMemM #-}

-- | An action that can be used as a default implementation for `reallocMutMem`. Whenever
-- current memory region byte count matches the supplied new size exactly then such memory
-- region is simply returned back and this function is a noop. Otherwise a new memory
-- region is allocated and all the data that can fit into the new region will be copied
-- over.
--
-- [Unsafe] Same unsafety notice as in `reallocMutMem`
--
-- @since 0.3.0
defaultReallocMutMem ::
     (Prim e, MemAlloc ma, MonadPrim s m) => ma s -> Count e -> m (ma s)
defaultReallocMutMem :: ma s -> Count e -> m (ma s)
defaultReallocMutMem ma s
mem Count e
c = do
  let newByteCount :: Count Word8
newByteCount = Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
c
  Count Word8
oldByteCount <- ma s -> m (Count Word8)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (Count Word8)
getByteCountMutMem ma s
mem
  if Count Word8
oldByteCount Count Word8 -> Count Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Count Word8
newByteCount
    then ma s -> m (ma s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ma s
mem
    else do
      ma s
newMem <- Count Word8 -> m (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count Word8
newByteCount
      FrozenMem ma
oldMem <- ma s -> m (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
mem
      ma s
newMem ma s -> m () -> m (ma s)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ FrozenMem ma
-> Off Word8 -> ma s -> Off Word8 -> Count Word8 -> m ()
forall s (m :: * -> *) mr (mw :: * -> *) e.
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e) =>
mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem FrozenMem ma
oldMem Off Word8
0 ma s
newMem Off Word8
0 (Count Word8 -> Count Word8 -> Count Word8
forall a. Ord a => a -> a -> a
min Count Word8
oldByteCount Count Word8
newByteCount)
{-# INLINE defaultReallocMutMem #-}


-- | Place @n@ copies of supplied region of memory one after another in a newly allocated
-- contiguous chunk of memory. Similar to `stimes`, but the source memory @memRead@ does
-- not have to match the type of `FrozenMem` ma.
--
-- ====__Example__
--
-- >>> :set -XTypeApplications
-- >>> :set -XDataKinds
-- >>> import Data.Prim.Memory
-- >>> let b = fromListMem @Word8 @(MBytes 'Inc) [0xde, 0xad, 0xbe, 0xef]
-- >>> cycleMemN @(MBytes 'Inc) 2 b
-- [0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef]
--
-- @since 0.1.0
cycleMemN ::
     forall ma mr. (MemAlloc ma, MemRead mr)
  => Int
  -> mr
  -> FrozenMem ma
cycleMemN :: Int -> mr -> FrozenMem ma
cycleMemN Int
n mr
r
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = FrozenMem ma
forall (ma :: * -> *). MemAlloc ma => FrozenMem ma
emptyMem
  | Bool
otherwise =
    (forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (FrozenMem ma)) -> FrozenMem ma)
-> (forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a b. (a -> b) -> a -> b
$ do
      let bc :: Count Word8
bc@(Count Int
chunk) = mr -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr
r
          c :: Count Word8
c@(Count Int
c8) = Int -> Count Word8
forall e. Int -> Count e
Count Int
n Count Word8 -> Count Word8 -> Count Word8
forall a. Num a => a -> a -> a
* Count Word8
bc
      ma s
mem <- Count Word8 -> ST s (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count Word8
c
      let go :: Int -> ST s ()
go Int
i = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
c8) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ mr -> Off Word8 -> ma s -> Off Word8 -> Count Word8 -> ST s ()
forall (mw :: * -> *) s (m :: * -> *) mr e.
(MemWrite mw, MonadPrim s m, MemRead mr, Prim e) =>
mr -> Off Word8 -> mw s -> Off Word8 -> Count e -> m ()
copyByteOffMem mr
r Off Word8
0 ma s
mem (Int -> Off Word8
forall e. Int -> Off e
Off Int
i) Count Word8
bc ST s () -> ST s () -> ST s ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> ST s ()
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
chunk)
      Int -> ST s ()
go Int
0
      ma s -> ST s (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
mem
{-# INLINE cycleMemN #-}


-- | Construct an immutable memory region that can't hold any data. Same as @`mempty` ::
-- `FrozenMem` ma@
--
-- ====__Example__
--
-- >>> :set -XTypeApplications
-- >>> :set -XDataKinds
-- >>> import Data.Prim.Memory
-- >>> toListMem (emptyMem @(MBytes 'Inc)) :: [Int]
-- []
--
-- @since 0.1.0
emptyMem ::
     forall ma. MemAlloc ma
  => FrozenMem ma
emptyMem :: FrozenMem ma
emptyMem = Count Word8 -> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall (ma :: * -> *) e b.
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createMemST_ (Count Word8
0 :: Count Word8) (\ma s
_ -> () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
{-# INLINE emptyMem #-}

-- | Allocate a region of immutable memory that holds a single element.
--
-- ====__Example__
--
-- >>> :set -XTypeApplications
-- >>> :set -XDataKinds
-- >>> import Data.Prim.Memory
-- >>> toListMem (singletonMem @Word16 @(MBytes 'Inc) 0xffff) :: [Word8]
-- [255,255]
--
-- @since 0.1.0
singletonMem ::
     forall e ma. (MemAlloc ma, Prim e)
  => e -- ^ The single element that will be stored in the newly allocated region of memory
  -> FrozenMem ma
singletonMem :: e -> FrozenMem ma
singletonMem e
a = Count e -> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall (ma :: * -> *) e b.
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createMemST_ (Count e
1 :: Count e) ((forall s. ma s -> ST s ()) -> FrozenMem ma)
-> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall a b. (a -> b) -> a -> b
$ \ma s
mem -> ma s -> Off e -> e -> ST s ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> e -> m ()
writeOffMutMem ma s
mem Off e
0 e
a
{-# INLINE singletonMem #-}

-- | Same as `allocMutMem`, but also use `setMutMem` to reset all of newly allocated memory to
-- zeros.
--
-- [Unsafe] When precondition for @memCount@ argument is violated the outcome is
-- unpredictable. One possible outcome is termination with `Control.Exception.HeapOverflow`
-- async exception.
--
-- ====__Example__
--
-- >>> :set -XTypeApplications
-- >>> :set -XDataKinds
-- >>> import Data.Prim.Memory
-- >>> mb <- allocZeroMutMem @Int @(MBytes 'Inc) 10
-- >>> b <- freezeMutMem mb
-- >>> toListMem b :: [Int]
-- [0,0,0,0,0,0,0,0,0,0]
--
-- @since 0.3.0
allocZeroMutMem ::
     forall e ma m s. (MemAlloc ma, MonadPrim s m, Prim e)
  => Count e
  -- ^ /memCount/ - Amount of memory to allocate for the region in number of elements of
  -- type __@e@__
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memCount
  --
  -- Possibility of overflow:
  --
  -- > memCount <= fromByteCount maxBound
  --
  -- When converted to bytes the value should be less then available physical memory
  -> m (ma s)
allocZeroMutMem :: Count e -> m (ma s)
allocZeroMutMem Count e
n = do
  ma s
m <- Count e -> m (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count e
n
  ma s
m ma s -> m () -> m (ma s)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ma s -> Off Word8 -> Count Word8 -> Word8 -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> Count e -> e -> m ()
setMutMem ma s
m Off Word8
0 (Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
n) (Word8
0 :: Word8)
{-# INLINE allocZeroMutMem #-}

-- | Allocate a mutable region of memory and fill it with the supplied `ST` action. Besides
-- the newly filled frozen memory this function also returns the result produced by the
-- filling action. See `createMemST_` for the version that discards it. Also see
-- `createZeroMemST` for a safer alternative.
--
-- [Unsafe] Same caviats as in `allocMutMem`
--
-- @since 0.1.0
createMemST ::
     forall e b ma. (MemAlloc ma, Prim e)
  => Count e
  -- ^ /memCount/ - Amount of memory to allocate for the region in number of elements of
  -- type __@e@__
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memCount
  --
  -- Possibility of overflow:
  --
  -- > memCount <= fromByteCount maxBound
  --
  -- When converted to bytes the value should be less then available physical memory
  -> (forall s. ma s -> ST s b)
  -- ^ /memFillAction/ - This action will be used to modify the contents of newly
  -- allocated memory. Make sure to overwrite all of it, otherwise it might lead to
  -- breaking referential transparency.
  -> (b, FrozenMem ma)
createMemST :: Count e -> (forall s. ma s -> ST s b) -> (b, FrozenMem ma)
createMemST Count e
n forall s. ma s -> ST s b
f = (forall s. ST s (b, FrozenMem ma)) -> (b, FrozenMem ma)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (b, FrozenMem ma)) -> (b, FrozenMem ma))
-> (forall s. ST s (b, FrozenMem ma)) -> (b, FrozenMem ma)
forall a b. (a -> b) -> a -> b
$ Count e -> ST s (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count e
n ST s (ma s)
-> (ma s -> ST s (b, FrozenMem ma)) -> ST s (b, FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ma s
m -> (,) (b -> FrozenMem ma -> (b, FrozenMem ma))
-> ST s b -> ST s (FrozenMem ma -> (b, FrozenMem ma))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ma s -> ST s b
forall s. ma s -> ST s b
f ma s
m ST s (FrozenMem ma -> (b, FrozenMem ma))
-> ST s (FrozenMem ma) -> ST s (b, FrozenMem ma)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ma s -> ST s (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
m
{-# INLINE createMemST #-}


createMemST_ ::
     (MemAlloc ma, Prim e)
  => Count e
  -- ^ /memCount/ - Amount of memory to allocate for the region in number of elements of
  -- type __@e@__
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memCount
  --
  -- Possibility of overflow:
  --
  -- > memCount <= fromByteCount maxBound
  --
  -- When converted to bytes the value should be less then available physical memory
  -> (forall s. ma s -> ST s b)
  -- ^ /memFillAction/ - This action will be used to modify the contents of newly
  -- allocated memory. Make sure to overwrite all of it, otherwise it might lead to
  -- breaking referential transparency.
  -> FrozenMem ma
createMemST_ :: Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createMemST_ Count e
n forall s. ma s -> ST s b
f = (forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a. (forall s. ST s a) -> a
runST (Count e -> ST s (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count e
n ST s (ma s) -> (ma s -> ST s (FrozenMem ma)) -> ST s (FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ma s
m -> ma s -> ST s b
forall s. ma s -> ST s b
f ma s
m ST s b -> ST s (FrozenMem ma) -> ST s (FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ma s -> ST s (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
m)
{-# INLINE createMemST_ #-}

-- | Same as `createMemST`, except it in ensures that the memory is reset to zeros right
-- after allocation
--
-- [Unsafe] Same caviats as in `allocZeroMutMem`: violation of precondition for @memCount@ may
-- result in undefined behavior or `Control.Exception.HeapOverflow` async exception.
--
-- @since 0.1.0
createZeroMemST ::
     forall e ma b. (MemAlloc ma, Prim e)
  => Count e
  -- ^ /memCount/ - Amount of memory to allocate for the region in number of elements of
  -- type __@e@__
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memCount
  --
  -- Possibility of overflow:
  --
  -- > memCount <= fromByteCount maxBound
  --
  -- When converted to bytes the value should be less then available physical memory
  -> (forall s. ma s -> ST s b)
  -- ^ /fillAction/ -- Action that will be used to modify contents of newly allocated
  -- memory. It is not required to overwrite the full region, since the whole thing will
  -- be reset to zeros before applying this action.
  -> (b, FrozenMem ma)
createZeroMemST :: Count e -> (forall s. ma s -> ST s b) -> (b, FrozenMem ma)
createZeroMemST Count e
n forall s. ma s -> ST s b
f = (forall s. ST s (b, FrozenMem ma)) -> (b, FrozenMem ma)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (b, FrozenMem ma)) -> (b, FrozenMem ma))
-> (forall s. ST s (b, FrozenMem ma)) -> (b, FrozenMem ma)
forall a b. (a -> b) -> a -> b
$ Count e -> ST s (ma s)
forall e (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m, Prim e) =>
Count e -> m (ma s)
allocZeroMutMem Count e
n ST s (ma s)
-> (ma s -> ST s (b, FrozenMem ma)) -> ST s (b, FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ma s
m -> (,) (b -> FrozenMem ma -> (b, FrozenMem ma))
-> ST s b -> ST s (FrozenMem ma -> (b, FrozenMem ma))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ma s -> ST s b
forall s. ma s -> ST s b
f ma s
m ST s (FrozenMem ma -> (b, FrozenMem ma))
-> ST s (FrozenMem ma) -> ST s (b, FrozenMem ma)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ma s -> ST s (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
m
{-# INLINE createZeroMemST #-}

-- | Same as `createMemST_`, except it ensures that the memory gets reset with zeros right
-- after allocation and prior applying the @ST@ filling action @fillAction@.
--
-- [Unsafe] Same reasons as `allocZeroMutMem`: violation of precondition for @memCount@ may
-- result in undefined behavior or `Control.Exception.HeapOverflow` async exception.
--
-- ====__Example__
--
-- Note that this example will work correctly only on little-endian machines:
--
-- >>> :set -XTypeApplications
-- >>> import Data.Prim
-- >>> import Control.Monad
-- >>> let ibs = zip [0, 4 ..] [0x48,0x61,0x73,0x6b,0x65,0x6c,0x6c] :: [(Off Word8, Word8)]
-- >>> let c = Count (length ibs) :: Count Char
-- >>> let bc = createZeroMemST_ @_ @(MBytes 'Inc) c $ \m -> forM_ ibs $ \(i, b) -> writeByteOffMutMem m i b
-- >>> toListMem bc :: String
-- "Haskell"
--
-- @since 0.1.0
createZeroMemST_ ::
     forall e ma b. (MemAlloc ma, Prim e)
  => Count e
  -- ^ /memCount/ - Amount of memory to allocate for the region in number of elements of
  -- type __@e@__
  --
  -- /__Precoditions:__/
  --
  -- Size should be non-negative, but smaller than amount of available memory. Note that the
  -- second condition simply describes overflow.
  --
  -- > 0 <= memCount
  --
  -- Possibility of overflow:
  --
  -- > memCount <= fromByteCount maxBound
  --
  -> (forall s. ma s -> ST s b)
  -- ^ /fillAction/ -- Action that will be used to modify contents of newly allocated
  -- memory. It is not required to overwrite the full region, since the whole thing will
  -- be reset to zeros before applying this action.
  -> FrozenMem ma
createZeroMemST_ :: Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createZeroMemST_ Count e
n forall s. ma s -> ST s b
f = (forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a. (forall s. ST s a) -> a
runST (Count e -> ST s (ma s)
forall e (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m, Prim e) =>
Count e -> m (ma s)
allocZeroMutMem Count e
n ST s (ma s) -> (ma s -> ST s (FrozenMem ma)) -> ST s (FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ma s
m -> ma s -> ST s b
forall s. ma s -> ST s b
f ma s
m ST s b -> ST s (FrozenMem ma) -> ST s (FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ma s -> ST s (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
m)
{-# INLINE createZeroMemST_ #-}

-- | Copy all of the data from the source into a newly allocate memory region of identical
-- size.
--
-- ====__Examples__
--
-- >>> :set -XDataKinds
-- >>> import Data.Prim.Memory
-- >>> import Data.Prim.Memory.Bytes
-- >>> let xs = fromByteListMem @(MBytes 'Pin) [0..15] :: Bytes 'Pin
-- >>> let ys = cloneMem xs
-- >>> let report bEq pEq = print $ "Bytes equal: " ++ show bEq ++ ", their pointers equal: " ++ show pEq
-- >>> withPtrBytes xs $ \ xsPtr -> withPtrBytes ys $ \ ysPtr -> report (xs == ys) (xsPtr == ysPtr)
-- "Bytes equal: True, their pointers equal: False"
-- >>> report (eqByteMem xs ys) (isSameBytes xs ys)
-- "Bytes equal: True, their pointers equal: False"
--
-- @since 0.2.0
cloneMem ::
     forall ma. MemAlloc ma
  => FrozenMem ma -- ^ /memSource/ - immutable source memory.
  -> FrozenMem ma
cloneMem :: FrozenMem ma -> FrozenMem ma
cloneMem FrozenMem ma
fm =
  (forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (FrozenMem ma)) -> FrozenMem ma)
-> (forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a b. (a -> b) -> a -> b
$ do
    let n :: Count Word8
n = FrozenMem ma -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem FrozenMem ma
fm
    ma s
mm <- Count Word8 -> ST s (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count Word8
n
    FrozenMem ma
-> Off Word8 -> ma s -> Off Word8 -> Count Word8 -> ST s ()
forall s (m :: * -> *) mr (mw :: * -> *) e.
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e) =>
mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem FrozenMem ma
fm Off Word8
0 ma s
mm Off Word8
0 Count Word8
n
    ma s -> ST s (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
mm
{-# INLINE cloneMem #-}

-- | Similar to `copyByteOffMem`, but supply offsets in number of elements instead of
-- bytes. Copy contiguous chunk of memory from the read only memory region into the target
-- mutable memory region. Source and target /must not/ refer to the same memory region,
-- otherwise that would imply that the source is not immutable which would be a violation
-- of some other invariant elsewhere in the code.
--
-- [Unsafe] When any precondition for one of the offsets @memSourceOff@, @memTargetOff@
-- or the element count @memCount@ is violated a call to this function can result in:
-- copy of data that doesn't belong to @memSourceRead@, heap corruption or failure with
-- a segfault.
--
-- @since 0.1.0
copyMem ::
     (MonadPrim s m, MemRead mr, MemWrite mw, Prim e)
  => mr -- ^ /memSourceRead/ - Read-only source memory region from which the data will
        -- copied
  -> Off e
  -- ^ /memSourceOff/ - Offset into source memory in number of elements of type __@e@__
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memSourceOff
  --
  -- > unOff memSourceOff < unCount (countMem memSourceRead)
  -> mw s -- ^ /memTargetWrite/ - Target mutable memory
  -> Off e
  -- ^ /memTargetOff/ -  Offset into target memory in number of elements
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memTargetOff
  --
  -- With offset applied it should still refer to the same memory region. For types that
  -- also implement `MemAlloc` this can be described as:
  --
  -- > targetCount <- getCountMutMem memTargetWrite
  -- > unOff memTargetOff < unCount targetCount
  -> Count e
  -- ^ /memCount/ - Number of elements of type __@e@__ to copy
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memCount
  --
  -- Both source and target memory regions must have enough memory to perform a copy
  -- of @memCount@ elements starting at their respective offsets. For @memSourceRead@:
  --
  -- > unOff memSourceOff + unCount memCount < unCount (countMem memSourceRead)
  --
  -- and for @memTargetWrite@ that also implements `MemAlloc` this can be described as:
  --
  -- > targetCount <- getCountMutMem memTargetWrite
  -- > unOff memTargetOff + unCount memCount < unCount targetCount
  -> m ()
copyMem :: mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem mr
src Off e
srcOff mw s
dst Off e
dstOff = mr -> Off Word8 -> mw s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) mr e.
(MemWrite mw, MonadPrim s m, MemRead mr, Prim e) =>
mr -> Off Word8 -> mw s -> Off Word8 -> Count e -> m ()
copyByteOffMem mr
src (Off e -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff Off e
srcOff) mw s
dst (Off e -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff Off e
dstOff)
{-# INLINE copyMem #-}


--
-- @since 0.3.0
moveMutMem ::
     (MonadPrim s m, MemWrite mw1, MemWrite mw2, Prim e)
  => mw1 s -- ^ Source memory region
  -> Off e -- ^ Offset into the source in number of elements
  -> mw2 s -- ^ Destination memory region
  -> Off e -- ^ Offset into destination in number of elements
  -> Count e -- ^ Number of elements to copy over
  -> m ()
moveMutMem :: mw1 s -> Off e -> mw2 s -> Off e -> Count e -> m ()
moveMutMem mw1 s
src Off e
srcOff mw2 s
dst Off e
dstOff = mw1 s -> Off Word8 -> mw2 s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) (mw' :: * -> *) e.
(MemWrite mw, MonadPrim s m, MemWrite mw', Prim e) =>
mw' s -> Off Word8 -> mw s -> Off Word8 -> Count e -> m ()
moveByteOffMutMem mw1 s
src (Off e -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff Off e
srcOff) mw2 s
dst (Off e -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff Off e
dstOff)
{-# INLINE moveMutMem #-}


appendMem ::
     forall mr1 mr2 ma. (MemRead mr1, MemRead mr2, MemAlloc ma)
  => mr1
  -> mr2
  -> FrozenMem ma
appendMem :: mr1 -> mr2 -> FrozenMem ma
appendMem mr1
r1 mr2
r2 =
  Count Word8 -> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall (ma :: * -> *) e b.
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createMemST_ (Count Word8
n1 Count Word8 -> Count Word8 -> Count Word8
forall a. Num a => a -> a -> a
+ Count Word8
n2) ((forall s. ma s -> ST s ()) -> FrozenMem ma)
-> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall a b. (a -> b) -> a -> b
$ \ma s
mem -> do
    mr1 -> Off Word8 -> ma s -> Off Word8 -> Count Word8 -> ST s ()
forall s (m :: * -> *) mr (mw :: * -> *) e.
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e) =>
mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem mr1
r1 Off Word8
0 ma s
mem Off Word8
0 Count Word8
n1
    mr2 -> Off Word8 -> ma s -> Off Word8 -> Count Word8 -> ST s ()
forall s (m :: * -> *) mr (mw :: * -> *) e.
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e) =>
mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem mr2
r2 (Count Word8 -> Off Word8
coerce Count Word8
n1) ma s
mem (Count Word8 -> Off Word8
coerce Count Word8
n1) Count Word8
n2
  where
    n1 :: Count Word8
n1 = mr1 -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr1
r1
    n2 :: Count Word8
n2 = mr2 -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr2
r2
{-# INLINABLE appendMem #-}

concatMem ::
     forall mr ma. (MemRead mr, MemAlloc ma)
  => [mr]
  -> FrozenMem ma
concatMem :: [mr] -> FrozenMem ma
concatMem [mr]
xs = do
  let c :: Count Word8
c = (Count Word8 -> mr -> Count Word8)
-> Count Word8 -> [mr] -> Count Word8
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' (\ !Count Word8
acc mr
b -> Count Word8
acc Count Word8 -> Count Word8 -> Count Word8
forall a. Num a => a -> a -> a
+ mr -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr
b) Count Word8
0 [mr]
xs
  Count Word8 -> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall (ma :: * -> *) e b.
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createMemST_ Count Word8
c ((forall s. ma s -> ST s ()) -> FrozenMem ma)
-> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall a b. (a -> b) -> a -> b
$ \ma s
mb -> do
    let load :: Off Word8 -> mr -> ST s (Off Word8)
load Off Word8
i mr
b = do
          let cb :: Count Word8
cb@(Count Int
n) = mr -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr
b :: Count Word8
          (Off Word8
i Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Int -> Off Word8
forall e. Int -> Off e
Off Int
n) Off Word8 -> ST s () -> ST s (Off Word8)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ mr -> Off Word8 -> ma s -> Off Word8 -> Count Word8 -> ST s ()
forall s (m :: * -> *) mr (mw :: * -> *) e.
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e) =>
mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem mr
b Off Word8
0 ma s
mb Off Word8
i Count Word8
cb
    (Off Word8 -> mr -> ST s (Off Word8))
-> Off Word8 -> [mr] -> ST s ()
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m ()
foldM_ Off Word8 -> mr -> ST s (Off Word8)
load Off Word8
0 [mr]
xs
{-# INLINABLE concatMem #-}

-- | This is a safe version of `thawMem`. It first makes an exact copy of the supplied
-- memory region and only then thaws it, thus yielding a mutable region of memory. This
-- means any mutation, will only affect the newly allocated region that was returned and
-- not the source region.
--
-- ====__Examples__
--
-- >>> :set -XTypeApplications
-- >>> :set -XDataKinds
-- >>> import Data.Prim.Memory
-- >>> let fm = fromListMem @Word8 @(MBytes 'Inc) [1,2,3,4]
-- >>> mm <- thawCloneMem fm
-- >>> writeOffMutMem mm 1 (0xadde :: Word16)
-- >>> freezeMutMem mm
-- [0x01,0x02,0xde,0xad]
-- >>> fm
-- [0x01,0x02,0x03,0x04]
--
-- @since 0.1.0
thawCloneMem ::
     forall ma m s. (MemAlloc ma, MonadPrim s m)
  => FrozenMem ma
  -> m (ma s)
thawCloneMem :: FrozenMem ma -> m (ma s)
thawCloneMem FrozenMem ma
a = FrozenMem ma -> Off Word8 -> Count Word8 -> m (ma s)
forall e (ma :: * -> *) (m :: * -> *) s.
(Prim e, MemAlloc ma, MonadPrim s m) =>
FrozenMem ma -> Off e -> Count e -> m (ma s)
thawCopyMem FrozenMem ma
a Off Word8
0 (FrozenMem ma -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem FrozenMem ma
a)
{-# INLINE thawCloneMem #-}


-- | Similar to `thawCloneMem`, except it is possible to specify which portion of the
-- frozen region will be copied over and thawed.
--
-- [Unsafe] When any precondition for eihter an offset @memSourceOff@ or the element count
-- @memCount@ is violated a call to this function can result in: copy of data that doesn't
-- belong to @memSource@ or failure with a segfault.
--
-- ====__Examples__
--
-- >>> :set -XTypeApplications
-- >>> :set -XDataKinds
-- >>> import Data.Prim.Memory
-- >>> let fm = fromListMem @Word8 @(MBytes 'Inc) [1,2,3,4,5]
-- >>> mm <- thawCopyMem fm 1 (3 :: Count Word8)
-- >>> writeOffMutMem mm 1 (0 :: Word8)
-- >>> freezeMutMem mm
-- [0x02,0x00,0x04]
-- >>> fm
-- [0x01,0x02,0x03,0x04,0x05]
--
-- @since 0.1.0
thawCopyMem ::
     forall e ma m s. (Prim e, MemAlloc ma, MonadPrim s m)
  => FrozenMem ma -- ^ /memSource/ - Read-only source memory region from which the data
                  -- will copied and thawed
  -> Off e
  -- ^ /memSourceOff/ - Offset into source memory in number of elements of type __@e@__
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memSourceOff
  --
  -- > unOff memSourceOff < unCount (countMem memSource)
  -> Count e
  -- ^ /memCount/ - Number of elements of type __@e@__ to copy
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memCount
  --
  -- > unOff memSourceOff + unCount memCount < unCount (countMem memSource)
  -> m (ma s)
thawCopyMem :: FrozenMem ma -> Off e -> Count e -> m (ma s)
thawCopyMem FrozenMem ma
a Off e
off Count e
c = do
  ma s
mem <- Count e -> m (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count e
c
  ma s
mem ma s -> m () -> m (ma s)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ FrozenMem ma -> Off e -> ma s -> Off e -> Count e -> m ()
forall s (m :: * -> *) mr (mw :: * -> *) e.
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e) =>
mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem FrozenMem ma
a Off e
off ma s
mem Off e
0 Count e
c
{-# INLINE thawCopyMem #-}



--
-- @since 0.3.0
freezeCopyMutMem ::
     forall e ma m s. (Prim e, MemAlloc ma, MonadPrim s m)
  => ma s
  -> Off e
  -> Count e
  -> m (FrozenMem ma)
freezeCopyMutMem :: ma s -> Off e -> Count e -> m (FrozenMem ma)
freezeCopyMutMem ma s
mem Off e
off Count e
c = ma s -> m (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
mem m (FrozenMem ma)
-> (FrozenMem ma -> m (FrozenMem ma)) -> m (FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \FrozenMem ma
r -> FrozenMem ma -> Off e -> Count e -> m (ma s)
forall e (ma :: * -> *) (m :: * -> *) s.
(Prim e, MemAlloc ma, MonadPrim s m) =>
FrozenMem ma -> Off e -> Count e -> m (ma s)
thawCopyMem FrozenMem ma
r Off e
off Count e
c m (ma s) -> (ma s -> m (FrozenMem ma)) -> m (FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ma s -> m (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem
{-# INLINE freezeCopyMutMem #-}

-- | Safe version of `freezeMutMem`. Yields an immutable copy of the supplied mutable
-- memory region. Further mutation of the source memory region will not affect the
-- produced copy.
---
-- ====__Example__
--
-- >>> :set -XTypeApplications
-- >>> :set -XDataKinds
-- >>> import Data.Prim.Memory
-- >>> mb <- allocZeroMutMem @Word8 @(MBytes 'Pin) 4
-- >>> writeOffMutMem mb 2 (0xff :: Word8)
-- >>> b <- freezeCloneMutMem mb
-- >>> writeOffMutMem mb 1 (0xab :: Word8)
-- >>> b
-- [0x00,0x00,0xff,0x00]
-- >>> freezeMutMem mb
-- [0x00,0xab,0xff,0x00]
--
-- @since 0.3.0
freezeCloneMutMem ::
     forall ma m s. (MemAlloc ma, MonadPrim s m)
  => ma s
  -> m (FrozenMem ma)
freezeCloneMutMem :: ma s -> m (FrozenMem ma)
freezeCloneMutMem = ma s -> m (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem (ma s -> m (FrozenMem ma))
-> (FrozenMem ma -> m (FrozenMem ma)) -> ma s -> m (FrozenMem ma)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> FrozenMem ma -> m (ma s)
forall (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m) =>
FrozenMem ma -> m (ma s)
thawCloneMem (FrozenMem ma -> m (ma s))
-> (ma s -> m (FrozenMem ma)) -> FrozenMem ma -> m (FrozenMem ma)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> ma s -> m (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem
{-# INLINE freezeCloneMutMem #-}

-- | /O(n)/ - Convert a read-only memory region into a newly allocated other type of
-- memory region
--
-- >>> import Data.ByteString (pack)
-- >>> let bs = pack [0x10 .. 0x20]
-- >>> bs
-- "\DLE\DC1\DC2\DC3\DC4\NAK\SYN\ETB\CAN\EM\SUB\ESC\FS\GS\RS\US "
-- >>> convertMem bs :: Bytes 'Inc
-- [0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20]
--
-- @since 0.1.0
convertMem :: (MemRead mr, MemAlloc ma) => mr -> FrozenMem ma
convertMem :: mr -> FrozenMem ma
convertMem mr
m =
  let c :: Count Word8
c = mr -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr
m
   in Count Word8 -> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall (ma :: * -> *) e b.
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createMemST_ Count Word8
c (\ma s
mm -> mr -> Off Word8 -> ma s -> Off Word8 -> Count Word8 -> ST s ()
forall s (m :: * -> *) mr (mw :: * -> *) e.
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e) =>
mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem mr
m Off Word8
0 ma s
mm Off Word8
0 Count Word8
c)
{-# INLINE convertMem #-}

-- | Figure out how many elements fits into the immutable region of memory. It is
-- possible that there is a remainder of bytes left, see `countRemMem` for getting that
-- too.
--
-- ====__Examples__
--
-- >>> let b = fromListMem [0 .. 5 :: Word8] :: Bytes 'Pin
-- >>> b
-- [0x00,0x01,0x02,0x03,0x04,0x05]
-- >>> countMem b :: Count Word16
-- Count {unCount = 3}
-- >>> countMem b :: Count Word32
-- Count {unCount = 1}
-- >>> countMem b :: Count Word64
-- Count {unCount = 0}
--
-- @since 0.1.0
countMem ::
     forall e mr. (MemRead mr, Prim e)
  => mr
  -> Count e
countMem :: mr -> Count e
countMem = Count Word8 -> Count e
forall e. Prim e => Count Word8 -> Count e
fromByteCount (Count Word8 -> Count e) -> (mr -> Count Word8) -> mr -> Count e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. mr -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem
{-# INLINE countMem #-}

-- | Figure out how many elements and a byte size remainder can fit into the immutable
-- region of memory.
--
-- ====__Examples__
--
-- >>> let b = fromListMem [0 .. 5 :: Word8] :: Bytes 'Pin
-- >>> b
-- [0x00,0x01,0x02,0x03,0x04,0x05]
-- >>> countRemMem @Word16 b
-- (Count {unCount = 3},Count {unCount = 0})
-- >>> countRemMem @Word32 b
-- (Count {unCount = 1},Count {unCount = 2})
-- >>> countRemMem @Word64 b
-- (Count {unCount = 0},Count {unCount = 6})
--
-- @since 0.1.0
countRemMem :: forall e mr. (MemRead mr, Prim e) => mr -> (Count e, Count Word8)
countRemMem :: mr -> (Count e, Count Word8)
countRemMem = Count Word8 -> (Count e, Count Word8)
forall e. Prim e => Count Word8 -> (Count e, Count Word8)
fromByteCountRem (Count Word8 -> (Count e, Count Word8))
-> (mr -> Count Word8) -> mr -> (Count e, Count Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. mr -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem
{-# INLINE countRemMem #-}

-- | Figure out how many elements fits into the mutable region of memory. Similar to
-- `countMem`, except that it is not a pure funciton, since the size of mutable memory can
-- change throuhout its lifetime. It is possible that there is a remainder of bytes left,
-- see `getCountRemMem` for getting that too.
--
-- ====__Examples__
--
-- >>> mb <- thawMem (fromListMem [0 .. 5 :: Word8] :: Bytes 'Pin)
-- >>> getCountMutMem mb :: IO (Count Word16)
-- Count {unCount = 3}
-- >>> getCountMutMem mb :: IO (Count Word32)
-- Count {unCount = 1}
-- >>> getCountMutMem mb :: IO (Count Word64)
-- Count {unCount = 0}
-- >>> mb' <- reallocMutMem mb (6 :: Count Word64)
-- >>> getCountMutMem mb' :: IO (Count Word32)
-- Count {unCount = 12}
--
-- @since 0.3.0
getCountMutMem :: forall e ma m s. (MemAlloc ma, MonadPrim s m, Prim e) => ma s -> m (Count e)
getCountMutMem :: ma s -> m (Count e)
getCountMutMem = (Count Word8 -> Count e) -> m (Count Word8) -> m (Count e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Count Word8 -> Count e
forall e. Prim e => Count Word8 -> Count e
fromByteCount (Count Word8 -> Count e)
-> (Count Word8 -> Count Word8) -> Count Word8 -> Count e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Count Word8 -> Count Word8
coerce) (m (Count Word8) -> m (Count e))
-> (ma s -> m (Count Word8)) -> ma s -> m (Count e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ma s -> m (Count Word8)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (Count Word8)
getByteCountMutMem
{-# INLINE getCountMutMem #-}


-- | Figure out how many elements and a byte size remainder can fit into the mutable
-- region of memory. Similar to `countRemMem`, except it is a monadic action for mutable
-- regions instead of a pure function for immutable memory. See `getCountMutMem` for getting
-- the element count only.
--
-- ====__Examples__
--
-- >>> b <- thawMem (fromListMem [0 .. 5 :: Word8] :: Bytes 'Pin)
-- >>> getCountRemMutMem @Word16 b
-- (Count {unCount = 3},Count {unCount = 0})
-- >>> getCountRemMutMem @Word32 b
-- (Count {unCount = 1},Count {unCount = 2})
-- >>> getCountRemMutMem @Word64 b
-- (Count {unCount = 0},Count {unCount = 6})
--
-- @since 0.3.0
getCountRemMutMem ::
     forall e ma m s. (MemAlloc ma, MonadPrim s m, Prim e)
  => ma s
  -> m (Count e, Count Word8)
getCountRemMutMem :: ma s -> m (Count e, Count Word8)
getCountRemMutMem = (Count Word8 -> (Count e, Count Word8))
-> m (Count Word8) -> m (Count e, Count Word8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Count Word8 -> (Count e, Count Word8)
forall e. Prim e => Count Word8 -> (Count e, Count Word8)
fromByteCountRem (Count Word8 -> (Count e, Count Word8))
-> (Count Word8 -> Count Word8)
-> Count Word8
-> (Count e, Count Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Count Word8 -> Count Word8
coerce) (m (Count Word8) -> m (Count e, Count Word8))
-> (ma s -> m (Count Word8)) -> ma s -> m (Count e, Count Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ma s -> m (Count Word8)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (Count Word8)
getByteCountMutMem
{-# INLINE getCountRemMutMem #-}

-- | Allocate the same amount of memory as the source memory region and copy all of its
-- data over.
--
-- @since 0.3.0
cloneMutMem ::
     forall ma m s. (MemAlloc ma, MonadPrim s m)
  => ma s
  -> m (ma s)
cloneMutMem :: ma s -> m (ma s)
cloneMutMem = ma s -> m (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem (ma s -> m (FrozenMem ma))
-> (FrozenMem ma -> m (ma s)) -> ma s -> m (ma s)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> FrozenMem ma -> m (ma s)
forall (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m) =>
FrozenMem ma -> m (ma s)
thawCloneMem
{-# INLINE cloneMutMem #-}

-- | Compare two memory regions byte-by-byte. Computation may be short-circuited on the
-- first mismatch, but it is `MemRead` implementation specific.
--
-- @since 0.3.0
eqByteOffMem ::
     (MemRead mr1, MemRead mr2)
  => mr1 -- ^ /memRead1/ - First region of memory
  -> Off Word8
  -- ^ /memOff1/ - Offset for @memRead1@ in number of bytes
  --
  -- /__Precondition:__/
  --
  -- > 0 <= memOff1
  -> mr2 -- ^ /memRead2/ - Second region of memory
  -> Off Word8
  -- ^ /memOff2/ - Offset for @memRead1@ in number of bytes
  --
  -- /__Precondition:__/
  --
  -- > 0 <= memOff2
  -> Count Word8
  -- ^ /memCount/ - Number of bytes compare
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memCount
  --
  -- > offToCount memOff1 + memCount < countMem memRead1
  --
  -- > offToCount memOff2 + memCount < countMem memRead2
  -> Bool
eqByteOffMem :: mr1 -> Off Word8 -> mr2 -> Off Word8 -> Count Word8 -> Bool
eqByteOffMem mr1
b1 Off Word8
off1 mr2
b2 Off Word8
off2 Count Word8
n = mr1 -> Off Word8 -> mr2 -> Off Word8 -> Count Word8 -> Ordering
forall mr mr' e.
(MemRead mr, MemRead mr', Prim e) =>
mr' -> Off Word8 -> mr -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr1
b1 Off Word8
off1 mr2
b2 Off Word8
off2 Count Word8
n Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
EQ
{-# INLINE eqByteOffMem #-}

-- | Compare two memory regions for equality byte-by-byte. `False` is returned immediately
-- when sizes reported by `byteCountMem` do not match.
--
-- @since 0.3.0
eqByteMem :: (MemRead mr1, MemRead mr2) => mr1 -> mr2 -> Bool
eqByteMem :: mr1 -> mr2 -> Bool
eqByteMem mr1
b1 mr2
b2 = Count Word8
n Count Word8 -> Count Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== mr2 -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr2
b2 Bool -> Bool -> Bool
&& mr1 -> Off Word8 -> mr2 -> Off Word8 -> Count Word8 -> Bool
forall mr1 mr2.
(MemRead mr1, MemRead mr2) =>
mr1 -> Off Word8 -> mr2 -> Off Word8 -> Count Word8 -> Bool
eqByteOffMem mr1
b1 Off Word8
0 mr2
b2 Off Word8
0 Count Word8
n
  where
    n :: Count Word8
n = mr1 -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr1
b1
{-# INLINE eqByteMem #-}


-- | Compare two memory regions byte-by-byte.
--
-- @since 0.3.0
compareByteMem :: (MemRead mr1, MemRead mr2) => mr1 -> mr2 -> Ordering
compareByteMem :: mr1 -> mr2 -> Ordering
compareByteMem mr1
b1 mr2
b2 = Count Word8 -> Count Word8 -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Count Word8
n (mr2 -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr2
b2) Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> mr1 -> Off Word8 -> mr2 -> Off Word8 -> Count Word8 -> Ordering
forall mr mr' e.
(MemRead mr, MemRead mr', Prim e) =>
mr' -> Off Word8 -> mr -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr1
b1 Off Word8
0 mr2
b2 Off Word8
0 Count Word8
n
  where
    n :: Count Word8
n = mr1 -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr1
b1
{-# INLINE compareByteMem #-}

-- =============== --
-- List conversion --
-- =============== --

-------------
-- To List --
-------------

-- | Convert an immutable memory region to a list. Whenever memory byte count is not
-- exactly divisible by the size of the element there will be some slack left unaccounted
-- for. In order to get a hold of this slack use `toListSlackMem` instead.
--
-- ====__Examples__
--
-- >>> import Data.Prim.Memory
-- >>> import Numeric (showHex)
-- >>> let b = fromByteListMem [0x48,0x61,0x73,0x6b,0x65,0x6c,0x6c] :: Bytes 'Inc
-- >>> toListMem b :: [Int8]
-- [72,97,115,107,101,108,108]
-- >>> let xs = toListMem b :: [Word32]
-- >>> xs
-- [1802723656]
-- >>> showHex (head xs) ""
-- "6b736148"
--
-- @since 0.1.0
toListMem :: forall e mr. (MemRead mr, Prim e) => mr -> [e]
toListMem :: mr -> [e]
toListMem mr
ba = (forall b. (e -> b -> b) -> b -> b) -> [e]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\ e -> b -> b
c b
n -> Count e -> (e -> b -> b) -> b -> mr -> b
forall e b mr.
(MemRead mr, Prim e) =>
Count e -> (e -> b -> b) -> b -> mr -> b
foldrCountMem (mr -> Count e
forall e mr. (MemRead mr, Prim e) => mr -> Count e
countMem mr
ba) e -> b -> b
c b
n mr
ba)
{-# INLINE toListMem #-}
{-# SPECIALIZE toListMem :: Prim e => Bytes p -> [e] #-}

-- | Same as `toListMem`, except when there is some slack towards the end of the memory
-- region that didn't fit into a list it will be returned as a list of bytes.
--
-- ====__Examples__
--
-- >>> import Data.Word
-- >>> :set -XDataKinds
-- >>> let a = fromListMem [0 .. 10 :: Word8] :: Bytes 'Pin
-- >>> a
-- [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a]
-- >>> toListSlackMem a :: ([Word8], [Word8])
-- ([0,1,2,3,4,5,6,7,8,9,10],[])
-- >>> toListSlackMem a :: ([Word16], [Word8])
-- ([256,770,1284,1798,2312],[10])
-- >>> toListSlackMem a :: ([Word32], [Word8])
-- ([50462976,117835012],[8,9,10])
-- >>> toListSlackMem a :: ([Word64], [Word8])
-- ([506097522914230528],[8,9,10])
--
-- @since 0.1.0
toListSlackMem ::
     forall e mr. (MemRead mr, Prim e)
  => mr
  -> ([e], [Word8])
toListSlackMem :: mr -> ([e], [Word8])
toListSlackMem mr
mem =
  ((forall b. (e -> b -> b) -> b -> b) -> [e]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\e -> b -> b
c b
n -> Count e -> (e -> b -> b) -> b -> mr -> b
forall e b mr.
(MemRead mr, Prim e) =>
Count e -> (e -> b -> b) -> b -> mr -> b
foldrCountMem Count e
k e -> b -> b
c b
n mr
mem), Int -> [Word8] -> [Word8]
getSlack (Int
k8 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
r8) [])
  where
    (Count e
k, Count Int
r8) = mr -> (Count e, Count Word8)
forall e mr. (MemRead mr, Prim e) => mr -> (Count e, Count Word8)
countRemMem mr
mem
    Count Int
k8 = Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
k
    getSlack :: Int -> [Word8] -> [Word8]
getSlack Int
i ![Word8]
acc
      | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k8 = [Word8]
acc
      | Bool
otherwise =
        let i' :: Int
i' = Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
         in Int -> [Word8] -> [Word8]
getSlack Int
i' (mr -> Off Word8 -> Word8
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem mr
mem (Int -> Off Word8
forall e. Int -> Off e
Off Int
i') Word8 -> [Word8] -> [Word8]
forall a. a -> [a] -> [a]
: [Word8]
acc)
{-# INLINABLE toListSlackMem #-}

-- | Right fold that is useful for converting to a list while tapping into list fusion.
--
-- [Unsafe] Supplying Count larger than memory holds will result in reading out of bounds
-- and a potential segfault.
--
-- @since 0.1.0
foldrCountMem :: forall e b mr. (MemRead mr, Prim e) => Count e -> (e -> b -> b) -> b -> mr -> b
foldrCountMem :: Count e -> (e -> b -> b) -> b -> mr -> b
foldrCountMem (Count Int
k) e -> b -> b
c b
nil mr
bs = Int -> b
go Int
0
  where
    go :: Int -> b
go Int
i
      | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k = b
nil
      | Bool
otherwise =
        let !v :: e
v = mr -> Off e -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off e -> e
indexOffMem mr
bs (Int -> Off e
forall e. Int -> Off e
Off Int
i)
         in e
v e -> b -> b
`c` Int -> b
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
{-# INLINE[0] foldrCountMem #-}

---------------
-- From List --
---------------

-- Pure immutable conversion --

-- | Just like `fromListMemN`, except it ensures safety by using the length of the
-- list for allocation. Because it has to figure out the length of the list first it
-- will be just a little bit slower, but that much safer.
--
-- ====__Examples__
--
-- >>> import Data.Prim.Memory
-- >>> :set -XDataKinds
-- >>> fromListMem "Hi" :: Bytes 'Inc
-- [0x48,0x00,0x00,0x00,0x69,0x00,0x00,0x00]
--
-- @since 0.1.0
fromListMem ::
     forall e ma. (Prim e, MemAlloc ma)
  => [e]
  -> FrozenMem ma
fromListMem :: [e] -> FrozenMem ma
fromListMem [e]
xs =
  let count :: Count e
count = Int -> Count e
coerce ([e] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [e]
xs) Count e -> [e] -> Count e
forall e (proxy :: * -> *). Count e -> proxy e -> Count e
`countForProxyTypeOf` [e]
xs
   in Count e -> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall (ma :: * -> *) e b.
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createMemST_ Count e
count (Count e -> [e] -> ma s -> ST s ()
forall e (mw :: * -> *) (m :: * -> *) s.
(Prim e, MemWrite mw, MonadPrim s m) =>
Count e -> [e] -> mw s -> m ()
loadListMutMemN_ Count e
count [e]
xs)
{-# INLINE fromListMem #-}


-- | Same as `fromListMem` but restricted to a list of `Word8`. Load a list of bytes into
-- a newly allocated memory region. Equivalent to `Data.ByteString.pack` for
-- `Data.ByteString.ByteString`
--
-- ====__Examples__
--
-- >>> fromByteListMem [0..10] :: Bytes 'Pin
-- [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a]
--
-- @since 0.1.0
fromByteListMem ::
     forall ma. MemAlloc ma
  => [Word8]
  -> FrozenMem ma
fromByteListMem :: [Word8] -> FrozenMem ma
fromByteListMem = [Word8] -> FrozenMem ma
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
[e] -> FrozenMem ma
fromListMem
{-# INLINE fromByteListMem #-}


-- | Similarly to `fromListMem` load a list into a newly allocated memory region, but
-- unlike the aforementioned function it also accepts a hint of how many elements is
-- expected to be in the list. Because the number of expected an actual elements might
-- not match we return not only the frozen memory region, but also:
--
-- * either a list with leftover elements from the input @list@, if it did not fully fit
--   into the allocated region. An empty list would indicate that it did fit exactly.
--
--     @
--     unCount memCount <= length list
--     @
--
-- * or an exact count of how many elements have been loaded when there was no
--   enough elements in the list
--
--
-- In the latter case a zero value would indicate that the list did fit into the newly
-- allocated memory region exactly, which is perfectly fine. But a positive value would
-- mean that the tail of the memory region is still unset and might contain garbage
-- data. Make sure to overwrite the surplus memory yourself or use the safe version
-- `fromListZeroMemN` that fills the surplus with zeros.
--
-- [Unsafe] Whenever @memCount@ precodition is violated, because on each call with the
-- same input it can produce different output therefore it will break referential
-- transparency.
--
-- ====__Examples__
--
-- >>> :set -XTypeApplications
-- >>> fromListMemN @Char @(MBytes 'Inc) 3 "Hello"
-- (Left "lo",[0x48,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x6c,0x00,0x00,0x00])
-- >>> fromListMemN @Char @(MBytes 'Inc) 2 "Hi"
-- (Left "",[0x48,0x00,0x00,0x00,0x69,0x00,0x00,0x00])
-- >>> fst $ fromListMemN @Char @(MBytes 'Inc) 5 "Hi"
-- Right (Count {unCount = 2})
--
-- @since 0.2.0
fromListMemN ::
     forall e ma. (Prim e, MemAlloc ma)
  => Count e
  -- ^ /memCount/ - Expected number of elements in the list, which exactly how much
  -- memory will be allocated.
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memCount
  -- > unCount memCount <= length list
  -> [e]
  -- ^ /list/ - A list of elements to load into the newly allocated memory region.
  -> (Either [e] (Count e), FrozenMem ma)
fromListMemN :: Count e -> [e] -> (Either [e] (Count e), FrozenMem ma)
fromListMemN Count e
count [e]
xs =
  Count e
-> (forall s. ma s -> ST s (Either [e] (Count e)))
-> (Either [e] (Count e), FrozenMem ma)
forall e b (ma :: * -> *).
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> (b, FrozenMem ma)
createMemST Count e
count ((forall s. ma s -> ST s (Either [e] (Count e)))
 -> (Either [e] (Count e), FrozenMem ma))
-> (forall s. ma s -> ST s (Either [e] (Count e)))
-> (Either [e] (Count e), FrozenMem ma)
forall a b. (a -> b) -> a -> b
$ \ma s
mm -> do
    ([e]
ys, Count e
loadedCount) <- Count e -> [e] -> ma s -> Off e -> ST s ([e], Count e)
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
Count e -> [e] -> mw s -> Off e -> m ([e], Count e)
loadListOffMutMemN Count e
count [e]
xs ma s
mm Off e
0
    Either [e] (Count e) -> ST s (Either [e] (Count e))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either [e] (Count e) -> ST s (Either [e] (Count e)))
-> Either [e] (Count e) -> ST s (Either [e] (Count e))
forall a b. (a -> b) -> a -> b
$
      if Count e
loadedCount Count e -> Count e -> Bool
forall a. Eq a => a -> a -> Bool
/= Count e
count Bool -> Bool -> Bool
&& [e] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [e]
ys
        then Count e -> Either [e] (Count e)
forall a b. b -> Either a b
Right Count e
loadedCount
        else [e] -> Either [e] (Count e)
forall a b. a -> Either a b
Left [e]
ys
{-# INLINE fromListMemN #-}


-- | Just like `fromListMemN`, except it ensures safety by filling tail with zeros,
-- whenever the list is not long enough.
--
-- ====__Examples__
--
-- >>> import Data.Prim.Memory
-- >>> :set -XTypeApplications
-- >>> fromListZeroMemN @Char @(MBytes 'Inc) 3 "Hi"
-- (Right (Count {unCount = 2}),[0x48,0x00,0x00,0x00,0x69,0x00,0x00,0x00,0x00,0x00,0x00,0x00])
--
-- @since 0.2.0
fromListZeroMemN ::
     forall e ma. (Prim e, MemAlloc ma)
  => Count e -- ^ /memCount/ - Number of elements to load from the list.
  -> [e]
  -> (Either [e] (Count e), FrozenMem ma)
fromListZeroMemN :: Count e -> [e] -> (Either [e] (Count e), FrozenMem ma)
fromListZeroMemN Count e
count [e]
xs =
  Count e
-> (forall s. ma s -> ST s (Either [e] (Count e)))
-> (Either [e] (Count e), FrozenMem ma)
forall e b (ma :: * -> *).
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> (b, FrozenMem ma)
createMemST (Count e -> Count e -> Count e
forall a. Ord a => a -> a -> a
max Count e
0 Count e
count) ((forall s. ma s -> ST s (Either [e] (Count e)))
 -> (Either [e] (Count e), FrozenMem ma))
-> (forall s. ma s -> ST s (Either [e] (Count e)))
-> (Either [e] (Count e), FrozenMem ma)
forall a b. (a -> b) -> a -> b
$ \ma s
mm -> do
    ([e]
ys, Count e
loadedCount) <- Count e -> [e] -> ma s -> Off e -> ST s ([e], Count e)
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
Count e -> [e] -> mw s -> Off e -> m ([e], Count e)
loadListOffMutMemN Count e
count [e]
xs ma s
mm Off e
0
    let loadedByteCount :: Count Word8
loadedByteCount = Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
loadedCount
        surplusByteCount :: Count Word8
surplusByteCount = Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
count Count Word8 -> Count Word8 -> Count Word8
forall a. Num a => a -> a -> a
- Count Word8
loadedByteCount
    Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Count Word8
surplusByteCount Count Word8 -> Count Word8 -> Bool
forall a. Ord a => a -> a -> Bool
> Count Word8
0) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ ma s -> Off Word8 -> Count Word8 -> Word8 -> ST s ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> Count e -> e -> m ()
setMutMem ma s
mm (Count Word8 -> Off Word8
forall e. Count e -> Off e
countToOff Count Word8
loadedByteCount) Count Word8
surplusByteCount Word8
0
    Either [e] (Count e) -> ST s (Either [e] (Count e))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either [e] (Count e) -> ST s (Either [e] (Count e)))
-> Either [e] (Count e) -> ST s (Either [e] (Count e))
forall a b. (a -> b) -> a -> b
$
      if Count e
loadedCount Count e -> Count e -> Bool
forall a. Eq a => a -> a -> Bool
/= Count e
count Bool -> Bool -> Bool
&& [e] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [e]
ys
        then Count e -> Either [e] (Count e)
forall a b. b -> Either a b
Right Count e
loadedCount
        else [e] -> Either [e] (Count e)
forall a b. a -> Either a b
Left [e]
ys
{-# INLINE fromListZeroMemN #-}

-- | Same as `fromListZeroMemN`, but ignore the extra information about how the loading went.
--
-- ====__Examples__
--
-- >>> import Data.Prim.Memory
-- >>> fromListZeroMemN_ 3 "Hi" :: Bytes 'Inc
-- [0x48,0x00,0x00,0x00,0x69,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
--
-- @since 0.2.0
fromListZeroMemN_ ::
     forall e ma. (Prim e, MemAlloc ma)
  => Count e
  -> [e]
  -> FrozenMem ma
fromListZeroMemN_ :: Count e -> [e] -> FrozenMem ma
fromListZeroMemN_ !Count e
n = (Either [e] (Count e), FrozenMem ma) -> FrozenMem ma
forall a b. (a, b) -> b
snd ((Either [e] (Count e), FrozenMem ma) -> FrozenMem ma)
-> ([e] -> (Either [e] (Count e), FrozenMem ma))
-> [e]
-> FrozenMem ma
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Count e -> [e] -> (Either [e] (Count e), FrozenMem ma)
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
Count e -> [e] -> (Either [e] (Count e), FrozenMem ma)
fromListZeroMemN Count e
n
{-# INLINE fromListZeroMemN_ #-}



-- Mutable loading --


loadListByteOffHelper ::
     (MemWrite mw, MonadPrim s m, Prim e)
  => [e]
  -> mw s
  -> Off Word8 -- ^ Offset
  -> Off Word8 -- ^ Upper bound
  -> Off Word8 -- ^ Element size
  -> m ([e], Count e)
loadListByteOffHelper :: [e]
-> mw s -> Off Word8 -> Off Word8 -> Off Word8 -> m ([e], Count e)
loadListByteOffHelper [e]
ys mw s
mw Off Word8
byteOff Off Word8
k Off Word8
step =
  let go :: [e] -> Off Word8 -> m ([e], Count e)
go []       Off Word8
i = ([e], Count e) -> m ([e], Count e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], Off Word8 -> Count e
toLoadedCount Off Word8
i)
      go a :: [e]
a@(e
x:[e]
xs) Off Word8
i
        | Off Word8
i Off Word8 -> Off Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Off Word8
k = mw s -> Off Word8 -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> e -> m ()
writeByteOffMutMem mw s
mw Off Word8
i e
x m () -> m ([e], Count e) -> m ([e], Count e)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [e] -> Off Word8 -> m ([e], Count e)
go [e]
xs (Off Word8
i Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
step)
        | Bool
otherwise = ([e], Count e) -> m ([e], Count e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([e]
a, Off Word8 -> Count e
toLoadedCount Off Word8
i)
      toLoadedCount :: Off Word8 -> Count e
toLoadedCount Off Word8
i = Count Word8 -> Count e
forall e. Prim e => Count Word8 -> Count e
fromByteCount (Off Word8 -> Count Word8
forall e. Off e -> Count e
offToCount (Off Word8
i Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
- Off Word8
byteOff))
   in [e] -> Off Word8 -> m ([e], Count e)
go [e]
ys Off Word8
byteOff
{-# INLINE loadListByteOffHelper #-}


-- | Load elements from the supplied list into a mutable memory region. Loading will
-- start at the supplied offset in number of bytes and will stop when either supplied
-- @elemCount@ number is reached or there are no more elements left in the list to
-- load. This action returns a list of elements that did not get loaded and the count of
-- how many elements did get loaded.
--
-- [Unsafe] When any precondition for either the offset @memTargetOff@ or the element
-- count @memCount@ is violated then a call to this function can result in heap corruption
-- or failure with a segfault.
--
-- ====__Examples__
--
-- For example load the @"Hell"@ somewhere in the middle of `MBytes`:
--
-- >>> ma <- allocZeroMutMem (6 :: Count Char) :: IO (MBytes 'Inc RW)
-- >>> loadListByteOffMutMemN 4 "Hello!" ma (toByteOff (1 :: Off Char))
-- ("o!",Count {unCount = 4})
-- >>> freezeMutMem ma
-- [0x00,0x00,0x00,0x00,0x48,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x6c,0x00,0x00,0x00,0x6c,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
--
-- Or something more useful like loading prefixes from nested lists:
--
-- >>> import Control.Monad
-- >>> foldM_ (\o xs -> (+ o) . countToByteOff . snd <$> loadListByteOffMutMemN 4 xs ma o) 2 [[x..] | x <- [1..5] :: [Word8]]
-- >>> freezeMutMem ma
-- [0x00,0x00,0x01,0x02,0x03,0x04,0x02,0x03,0x04,0x05,0x03,0x04,0x05,0x06,0x04,0x05,0x06,0x07,0x05,0x06,0x07,0x08,0x00,0x00]
--
-- @since 0.2.0
loadListByteOffMutMemN ::
     (MemWrite mw, MonadPrim s m, Prim e)
  => Count e
  -- ^ /elemCount/ - Maximum number of elements to load from list into the memory region
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memCount
  --
  -- Target memory region must have enough memory to perform loading of @elemCount@
  -- elements starting at the @memTargetOff@ offset. For types that also implement
  -- `MemAlloc` this can be described as:
  --
  -- > targetByteCount <- getByteCountMutMem memTarget
  -- > unOff memTargetOff + unCountBytes elemCount <= unCount (targetByteCount - byteCountType @e)
  -> [e] -- ^ /listSource/ - List with elements that should be loaded
  -> mw s -- ^ /memTarget/ - Memory region where to load the elements into
  -> Off Word8
  -- ^ /memTargetOff/ - Offset in number of bytes into target memory where writing will start
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memTargetOff
  --
  -- Once the pointer is advanced by @memTargetOff@ it must still refer to the same memory
  -- region @memTarget@. For types that also implement `MemAlloc` this can be described
  -- as:
  --
  -- > targetByteCount <- getByteCountMutMem memTarget
  -- > unOff memTargetOff <= unCount (targetByteCount - byteCountType @e)
  -> m ([e], Count e)
  -- ^ Leftover part of the @listSource@ if any and the exact count of elements that have been loaded.
loadListByteOffMutMemN :: Count e -> [e] -> mw s -> Off Word8 -> m ([e], Count e)
loadListByteOffMutMemN Count e
count [e]
ys mw s
mw Off Word8
byteOff = [e]
-> mw s -> Off Word8 -> Off Word8 -> Off Word8 -> m ([e], Count e)
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
[e]
-> mw s -> Off Word8 -> Off Word8 -> Off Word8 -> m ([e], Count e)
loadListByteOffHelper [e]
ys mw s
mw Off Word8
byteOff Off Word8
k Off Word8
step
  where
    k :: Off Word8
k = Off Word8
byteOff Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Count Word8 -> Off Word8
forall e. Count e -> Off e
countToOff (Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
count)
    step :: Off Word8
step = Count Word8 -> Off Word8
forall e. Count e -> Off e
countToOff (Count Word8 -> Off Word8) -> Count Word8 -> Off Word8
forall a b. (a -> b) -> a -> b
$ [e] -> Count Word8
forall (proxy :: * -> *) e. Prim e => proxy e -> Count Word8
byteCountProxy [e]
ys
{-# INLINABLE loadListByteOffMutMemN #-}

-- | Same as `loadListByteOffMutMemN`, but infer the count from number of bytes that is
-- available in the target memory region.
--
-- [Unsafe] When a precondition for the element count @memCount@ is violated then a call
-- to this function can result in heap corruption or failure with a segfault.
--
-- ====__Examples__
--
-- >>> :set -XDataKinds
-- >>> import Data.Prim.Memory
-- >>> ma <- allocZeroMutMem (5 :: Count Char) :: IO (MBytes 'Inc RW)
-- >>> freezeMutMem ma
-- [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
-- >>> loadListByteOffMutMem "Hello World" ma 0
-- (" World",Count {unCount = 5})
-- >>> freezeMutMem ma
-- [0x48,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x6c,0x00,0x00,0x00,0x6c,0x00,0x00,0x00,0x6f,0x00,0x00,0x00]
-- >>> loadListByteOffMutMem ([0xff,0xff,0xff] :: [Word8]) ma 1
-- ([],Count {unCount = 3})
-- >>> freezeMutMem ma
-- [0x48,0xff,0xff,0xff,0x65,0x00,0x00,0x00,0x6c,0x00,0x00,0x00,0x6c,0x00,0x00,0x00,0x6f,0x00,0x00,0x00]
--
-- @since 0.3.0
loadListByteOffMutMem ::
     (MemAlloc ma, MonadPrim s m, Prim e)
  => [e] -- ^ /listSource/ - List with elements that should be loaded
  -> ma s -- ^ /memTarget/ - Memory region where to load the elements into
  -> Off Word8
  -- ^ /memTargetOff/ - Offset in number of bytes into target memory where writing will start
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memTargetOff
  --
  -- Once the pointer is advanced by @memTargetOff@ it must still refer to the same memory
  -- region @memTarget@. For types that also implement `MemAlloc` this can be described
  -- as:
  --
  -- > targetByteCount <- getByteCountMutMem memTarget
  -- > unOff memTargetOff <= unCount (targetByteCount - byteCountType @e)
  -> m ([e], Count e)
  -- ^ Leftover part of the @listSource@ if any and the exact count of elements that have been loaded.
loadListByteOffMutMem :: [e] -> ma s -> Off Word8 -> m ([e], Count e)
loadListByteOffMutMem [e]
ys ma s
ma Off Word8
byteOff = do
  Count Word8
bCount <- ma s -> m (Count Word8)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (Count Word8)
getByteCountMutMem ma s
ma
  let k :: Off Word8
k = Count Word8 -> Off Word8
forall e. Count e -> Off e
countToOff Count Word8
bCount Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
- Off Word8
byteOff
      step :: Off Word8
step = Count Word8 -> Off Word8
forall e. Count e -> Off e
countToOff (Count Word8 -> Off Word8) -> Count Word8 -> Off Word8
forall a b. (a -> b) -> a -> b
$ [e] -> Count Word8
forall (proxy :: * -> *) e. Prim e => proxy e -> Count Word8
byteCountProxy [e]
ys
  [e]
-> ma s -> Off Word8 -> Off Word8 -> Off Word8 -> m ([e], Count e)
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
[e]
-> mw s -> Off Word8 -> Off Word8 -> Off Word8 -> m ([e], Count e)
loadListByteOffHelper [e]
ys ma s
ma Off Word8
byteOff Off Word8
k Off Word8
step
{-# INLINABLE loadListByteOffMutMem #-}

-- | Same as `loadListByteOffMutMemN`, but works with offset in number of elements instead of
-- bytes.
--
-- [Unsafe] When preconditions for either the offset @memTargetOff@ or the element count
-- @memCount@ is violated then a call to this function can result in heap corruption or
-- failure with a segfault.
--
-- @since 0.2.0
loadListOffMutMemN ::
     (MemWrite mw, MonadPrim s m, Prim e)
  => Count e
  -- ^ /elemCount/ - Maximum number of elements to load from list into the memory region
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memCount
  --
  -- Target memory region must have enough memory to perform loading of @elemCount@
  -- elements starting at the @memTargetOff@ offset. For types that also implement
  -- `MemAlloc` this can be described as:
  --
  -- > targetCount <- getCountMutMem memTarget
  -- > unOff memTargetOff + unCount elemCount < unCount targetCount
  -> [e] -- ^ /listSource/ - List with elements that should be loaded
  -> mw s -- ^ /memTarget/ - Memory region where to load the elements into
  -> Off e
  -- ^ /memTargetOff/ - Offset in number of elements into target memory where writing will start
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memTargetOff
  --
  -- Once the pointer is advanced by @memTargetOff@ it must still refer to the same memory
  -- region @memTarget@. For types that also implement `MemAlloc` this can be described
  -- as:
  --
  -- > targetCount <- getByteCountMutMem memTarget
  -- > unOff memTargetOff < unCount targetCount
  -> m ([e], Count e)
  -- ^ Leftover part of the @listSource@ if any and the exact count of elements that have been loaded.
loadListOffMutMemN :: Count e -> [e] -> mw s -> Off e -> m ([e], Count e)
loadListOffMutMemN Count e
count [e]
ys mw s
mw Off e
off =
  let go :: [e] -> Off e -> m ([e], Count e)
go []       Off e
i = ([e], Count e) -> m ([e], Count e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], Off e -> Count e
toLoadedCount Off e
i)
      go a :: [e]
a@(e
x:[e]
xs) Off e
i
        | Off e
i Off e -> Off e -> Bool
forall a. Ord a => a -> a -> Bool
< Off e
k = mw s -> Off e -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> e -> m ()
writeOffMutMem mw s
mw Off e
i e
x m () -> m ([e], Count e) -> m ([e], Count e)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [e] -> Off e -> m ([e], Count e)
go [e]
xs (Off e
i Off e -> Off e -> Off e
forall a. Num a => a -> a -> a
+ Off e
1)
        | Bool
otherwise = ([e], Count e) -> m ([e], Count e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([e]
a, Off e -> Count e
toLoadedCount Off e
i)
      k :: Off e
k = Off e
off Off e -> Off e -> Off e
forall a. Num a => a -> a -> a
+ Count e -> Off e
forall e. Count e -> Off e
countToOff Count e
count
      toLoadedCount :: Off e -> Count e
toLoadedCount Off e
i = Off e -> Count e
forall e. Off e -> Count e
offToCount (Off e
i Off e -> Off e -> Off e
forall a. Num a => a -> a -> a
- Off e
off)
  in [e] -> Off e -> m ([e], Count e)
go [e]
ys Off e
off
{-# INLINABLE loadListOffMutMemN #-}


-- | Same as `loadListOffMutMemN`, but start loading at @0@ offset.
--
-- [Unsafe] When any precondition for the element count @memCount@ is violated then a call to
-- this function can result in heap corruption or failure with a segfault.
--
-- @since 0.2.0
loadListMutMemN ::
     forall e mw m s. (MemWrite mw, MonadPrim s m, Prim e)
  => Count e
  -- ^ /elemCount/ - Maximum number of elements to load from list into the memory region
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memCount
  --
  -- Target memory region must have enough memory to perform loading of @elemCount@
  -- elements. For types that also implement `MemAlloc` this can be described as:
  --
  -- > targetCount <- getCountMutMem memTarget
  -- > elemCount <= targetCount
  -> [e] -- ^ /listSource/ - List with elements that should be loaded
  -> mw s -- ^ /memTarget/ - Memory region where to load the elements into
  -> m ([e], Count e)
  -- ^ Leftover part of the @listSource@ if any and the exact count of elements that have been loaded.
loadListMutMemN :: Count e -> [e] -> mw s -> m ([e], Count e)
loadListMutMemN Count e
count [e]
xs mw s
mw = Count e -> [e] -> mw s -> Off e -> m ([e], Count e)
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
Count e -> [e] -> mw s -> Off e -> m ([e], Count e)
loadListOffMutMemN Count e
count [e]
xs mw s
mw Off e
0
{-# INLINABLE loadListMutMemN #-}



-- | Same as `loadListMutMemN`, but ignores the result.
--
-- [Unsafe] When any precondition for the element count @memCount@ is violated then a call
-- to this function can result in heap corruption or failure with a segfault.
--
-- @since 0.2.0
loadListMutMemN_ ::
     forall e mw m s. (Prim e, MemWrite mw, MonadPrim s m)
  => Count e
  -- ^ /elemCount/ - Maximum number of elements to load from list into the memory region
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memCount
  --
  -- Target memory region must have enough memory to perform loading of @elemCount@
  -- elements. For types that also implement `MemAlloc` this can be described as:
  --
  -- > targetCount <- getCountMutMem memTarget
  -- > elemCount <= targetCount
  -> [e] -- ^ /listSource/ - List with elements that should be loaded
  -> mw s -- ^ /memTarget/ - Memory region where to load the elements into
  -> m ()
loadListMutMemN_ :: Count e -> [e] -> mw s -> m ()
loadListMutMemN_ (Count Int
n) [e]
ys mw s
mb =
  let go :: [e] -> Int -> m ()
go []     Int
_ = () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      go (e
x:[e]
xs) Int
i = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ mw s -> Off e -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> e -> m ()
writeOffMutMem mw s
mb (Int -> Off e
forall e. Int -> Off e
Off Int
i) e
x m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [e] -> Int -> m ()
go [e]
xs (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
   in [e] -> Int -> m ()
go [e]
ys Int
0
{-# INLINABLE loadListMutMemN_ #-}




-- | Same as `loadListOffMutMemN`, but infer the count from number of bytes that is available
-- in the target memory region.
--
-- [Unsafe] When a precondition for the element count @memCount@ is violated then a call
-- to this function can result in heap corruption or failure with a segfault.
--
-- @since 0.3.0
loadListOffMutMem ::
     forall e ma m s. (Prim e, MemAlloc ma, MonadPrim s m)
  => [e] -- ^ /listSource/ - List with elements that should be loaded
  -> ma s -- ^ /memTarget/ - Memory region where to load the elements into
  -> Off e
  -- ^ /memTargetOff/ - Offset in number of elements into target memory where writing will
  -- start
  --
  -- /__Preconditions:__/
  --
  -- > 0 <= memTargetOff
  --
  -- Once the pointer is advanced by @memTargetOff@ it must still refer to the same memory
  -- region @memTarget@. For types that also implement `MemAlloc` this can be described
  -- as:
  --
  -- > targetCount <- getCountMutMem memTarget
  -- > unOff memTargetOff < unCount targetCount
  -> m ([e], Count e)
  -- ^ Leftover part of the @listSource@ if any and the exact count of elements that have been loaded.
loadListOffMutMem :: [e] -> ma s -> Off e -> m ([e], Count e)
loadListOffMutMem [e]
ys ma s
ma Off e
off = ma s -> m (Count e)
forall e (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m, Prim e) =>
ma s -> m (Count e)
getCountMutMem ma s
ma m (Count e) -> (Count e -> m ([e], Count e)) -> m ([e], Count e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Count e
c -> Count e -> [e] -> ma s -> Off e -> m ([e], Count e)
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
Count e -> [e] -> mw s -> Off e -> m ([e], Count e)
loadListOffMutMemN (Count e
c Count e -> Count e -> Count e
forall a. Num a => a -> a -> a
- Off e -> Count e
forall e. Off e -> Count e
offToCount Off e
off) [e]
ys ma s
ma Off e
off
{-# INLINE loadListOffMutMem #-}


-- | Same as `loadListMutMemN`, but tries to fit as many elements as possible into the mutable
-- memory region starting at the beginning. This operation is always safe.
--
-- ====__Examples__
--
-- >>> import Data.Prim.Memory
-- >>> ma <- allocMutMem (5 :: Count Char) :: IO (MBytes 'Inc RW)
-- >>> loadListMutMem "HelloWorld" ma
-- ("World",Count {unCount = 5})
-- >>> freezeMutMem ma
-- [0x48,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x6c,0x00,0x00,0x00,0x6c,0x00,0x00,0x00,0x6f,0x00,0x00,0x00]
-- >>> loadListMutMem (replicate 6 (0xff :: Word8)) ma
-- ([],Count {unCount = 6})
-- >>> freezeMutMem ma
-- [0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x6c,0x00,0x00,0x00,0x6c,0x00,0x00,0x00,0x6f,0x00,0x00,0x00]
--
-- @since 0.3.0
loadListMutMem ::
     forall e ma m s. (Prim e, MemAlloc ma, MonadPrim s m)
  => [e] -- ^ /listSource/ - List with elements to load
  -> ma s -- ^ /memTarget/ - Mutable region where to load elements from the list
  -> m ([e], Count e)
  -- ^ Leftover part of the @listSource@ if any and the exact count of elements that have been loaded.
loadListMutMem :: [e] -> ma s -> m ([e], Count e)
loadListMutMem [e]
ys ma s
ma = ma s -> m (Count e)
forall e (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m, Prim e) =>
ma s -> m (Count e)
getCountMutMem ma s
ma m (Count e) -> (Count e -> m ([e], Count e)) -> m ([e], Count e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Count e
c -> Count e -> [e] -> ma s -> Off e -> m ([e], Count e)
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
Count e -> [e] -> mw s -> Off e -> m ([e], Count e)
loadListOffMutMemN (Count e
c Count e -> [e] -> Count e
forall e (proxy :: * -> *). Count e -> proxy e -> Count e
`countForProxyTypeOf` [e]
ys) [e]
ys ma s
ma Off e
0
{-# INLINE loadListMutMem #-}

-- | Same as `loadListMutMem`, but ignores the result. Equivalence as property:
--
-- prop> let c = fromInteger (abs i) :: Count Int in (createZeroMemST_ c (loadListMutMem_ (xs :: [Int])) :: Bytes 'Inc) == createZeroMemST_ c (void . loadListMutMem xs)
--
-- @since 0.2.0
loadListMutMem_ ::
     forall e ma m s. (Prim e, MemAlloc ma, MonadPrim s m)
  => [e] -- ^ /listSource/ - List with elements to load
  -> ma s -- ^ /memTarget/ - Mutable region where to load elements from the list
  -> m ()
loadListMutMem_ :: [e] -> ma s -> m ()
loadListMutMem_ [e]
ys ma s
mb = ma s -> m (Count e)
forall e (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m, Prim e) =>
ma s -> m (Count e)
getCountMutMem ma s
mb m (Count e) -> (Count e -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Count e
c -> Count e -> [e] -> ma s -> m ()
forall e (mw :: * -> *) (m :: * -> *) s.
(Prim e, MemWrite mw, MonadPrim s m) =>
Count e -> [e] -> mw s -> m ()
loadListMutMemN_ (Count e
c Count e -> [e] -> Count e
forall e (proxy :: * -> *). Count e -> proxy e -> Count e
`countForProxyTypeOf` [e]
ys) [e]
ys ma s
mb
{-# INLINE loadListMutMem_ #-}


-- | Convert a memory region to a list of bytes. Equivalent to `Data.ByteString.unpack`
-- for `Data.ByteString.ByteString`
--
-- ====__Example__
--
-- >>> toByteListMem (fromByteListMem [0..10] :: Bytes 'Pin)
-- [0,1,2,3,4,5,6,7,8,9,10]
--
-- @since 0.1.0
toByteListMem ::
     forall ma. MemAlloc ma
  => FrozenMem ma
  -> [Word8]
toByteListMem :: FrozenMem ma -> [Word8]
toByteListMem = FrozenMem ma -> [Word8]
forall e mr. (MemRead mr, Prim e) => mr -> [e]
toListMem
{-# INLINE toByteListMem #-}

-- mapMem ::
--      forall e e' mr ma. (MemRead mr, MemAlloc ma, Prim e, Prim e')
--   => (e -> e')
--   -> mr
--   -> (FrozenMem ma, [Word8])
-- mapMem f = undefined


mapByteMem ::
     forall e mr ma. (MemRead mr, MemAlloc ma, Prim e)
  => (Word8 -> e)
  -> mr
  -> FrozenMem ma
mapByteMem :: (Word8 -> e) -> mr -> FrozenMem ma
mapByteMem Word8 -> e
f = (Off Word8 -> Word8 -> e) -> mr -> FrozenMem ma
forall mr (ma :: * -> *) e.
(MemRead mr, MemAlloc ma, Prim e) =>
(Off Word8 -> Word8 -> e) -> mr -> FrozenMem ma
imapByteOffMem ((Word8 -> e) -> Off Word8 -> Word8 -> e
forall a b. a -> b -> a
const Word8 -> e
f)

-- Map an index aware function over memory region
--
-- >>> import Data.Prim.Memory
-- >>> a = fromListMem [1 .. 10 :: Word8] :: Bytes 'Inc
-- >>> a
-- [0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a]
-- >>> imapByteOffMem (\i e -> (fromIntegral i :: Int8, e + 0xf0)) a :: Bytes 'Pin
-- [0x00,0xf1,0x01,0xf2,0x02,0xf3,0x03,0xf4,0x04,0xf5,0x05,0xf6,0x06,0xf7,0x07,0xf8,0x08,0xf9,0x09,0xfa]
--
-- @since 0.1.0
imapByteOffMem ::
     (MemRead mr, MemAlloc ma, Prim e) => (Off Word8 -> Word8 -> e) -> mr -> FrozenMem ma
imapByteOffMem :: (Off Word8 -> Word8 -> e) -> mr -> FrozenMem ma
imapByteOffMem Off Word8 -> Word8 -> e
f mr
r = (forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (FrozenMem ma)) -> FrozenMem ma)
-> (forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a b. (a -> b) -> a -> b
$ (Off Word8 -> Word8 -> ST s e) -> mr -> ST s (FrozenMem ma)
forall e mr (ma :: * -> *) (m :: * -> *) s.
(MemRead mr, MemAlloc ma, MonadPrim s m, Prim e) =>
(Off Word8 -> Word8 -> m e) -> mr -> m (FrozenMem ma)
mapByteOffMemM (\Off Word8
i -> e -> ST s e
forall (f :: * -> *) a. Applicative f => a -> f a
pure (e -> ST s e) -> (Word8 -> e) -> Word8 -> ST s e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Off Word8 -> Word8 -> e
f Off Word8
i) mr
r

-- @since 0.1.0
mapByteMemM ::
     (MemRead mr, MemAlloc ma, MonadPrim s m, Prim e)
  => (Word8 -> m e)
  -> mr
  -> m (FrozenMem ma)
mapByteMemM :: (Word8 -> m e) -> mr -> m (FrozenMem ma)
mapByteMemM Word8 -> m e
f = (Off Word8 -> Word8 -> m e) -> mr -> m (FrozenMem ma)
forall e mr (ma :: * -> *) (m :: * -> *) s.
(MemRead mr, MemAlloc ma, MonadPrim s m, Prim e) =>
(Off Word8 -> Word8 -> m e) -> mr -> m (FrozenMem ma)
mapByteOffMemM ((Word8 -> m e) -> Off Word8 -> Word8 -> m e
forall a b. a -> b -> a
const Word8 -> m e
f)


-- @since 0.1.0
mapByteOffMemM ::
     forall e mr ma m s. (MemRead mr, MemAlloc ma, MonadPrim s m, Prim e)
  => (Off Word8 -> Word8 -> m e)
  -> mr
  -> m (FrozenMem ma)
mapByteOffMemM :: (Off Word8 -> Word8 -> m e) -> mr -> m (FrozenMem ma)
mapByteOffMemM Off Word8 -> Word8 -> m e
f mr
r = do
  let bc :: Count Word8
bc@(Count Int
n) = mr -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr
r
      c :: Count e
c = Int -> Count e
forall e. Int -> Count e
Count Int
n Count e -> m e -> Count e
forall e (proxy :: * -> *). Count e -> proxy e -> Count e
`countForProxyTypeOf` Off Word8 -> Word8 -> m e
f Off Word8
0 Word8
0
  ma s
mem <- Count e -> m (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count e
c
  Off Word8
_ <- mr
-> Off Word8
-> Count Word8
-> (Off Word8 -> Word8 -> m e)
-> m (Off Word8)
forall mr s (m :: * -> *) e b.
(MemRead mr, MonadPrim s m, Prim e) =>
mr
-> Off Word8 -> Count e -> (Off Word8 -> e -> m b) -> m (Off Word8)
forByteOffMemM_ mr
r Off Word8
0 Count Word8
bc Off Word8 -> Word8 -> m e
f
  -- let go i =
  --       when (i < n) $ do
  --         f i (indexByteOffMem r (Off i)) >>=
  --           writeOffMem mem (offAsProxy c (Off i))
  --         go (i + 1)
  -- go 0
  ma s -> m (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
mem


-- | Iterate over a region of memory
forByteOffMemM_ ::
     (MemRead mr, MonadPrim s m, Prim e)
  => mr
  -> Off Word8
  -> Count e
  -> (Off Word8 -> e -> m b)
  -> m (Off Word8)
forByteOffMemM_ :: mr
-> Off Word8 -> Count e -> (Off Word8 -> e -> m b) -> m (Off Word8)
forByteOffMemM_ mr
r (Off Int
byteOff) Count e
c Off Word8 -> e -> m b
f =
  let n :: Int
n = Count Word8 -> Int
coerce (Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
c) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
byteOff
      Count Int
k = Count e -> Count Word8
forall (proxy :: * -> *) e. Prim e => proxy e -> Count Word8
byteCountProxy Count e
c
      go :: Int -> m (Off Word8)
go Int
i
        | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n = Off Word8 -> e -> m b
f (Int -> Off Word8
forall e. Int -> Off e
Off Int
i) (mr -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem mr
r (Int -> Off Word8
forall e. Int -> Off e
Off Int
i)) m b -> m (Off Word8) -> m (Off Word8)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> m (Off Word8)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
k)
        | Bool
otherwise = Off Word8 -> m (Off Word8)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Off Word8 -> m (Off Word8)) -> Off Word8 -> m (Off Word8)
forall a b. (a -> b) -> a -> b
$ Int -> Off Word8
forall e. Int -> Off e
Off Int
i
   in Int -> m (Off Word8)
go Int
byteOff

{-

loopShort :: Int -> (Int -> a -> Bool) -> (Int -> Int) -> a -> (Int -> a -> a) -> a
loopShort !startAt condition increment !initAcc f = go startAt initAcc
  where
    go !step !acc =
      if condition step acc
        then go (increment step) (f step acc)
        else acc
{-# INLINE loopShort #-}

loopShortM :: Monad m => Int -> (Int -> a -> m Bool) -> (Int -> Int) -> a -> (Int -> a -> m a) -> m a
loopShortM !startAt condition increment !initAcc f = go startAt initAcc
  where
    go !step !acc = do
      shouldContinue <- condition step acc
      if shouldContinue
        then f step acc >>= go (increment step)
        else return acc
{-# INLINE loopShortM #-}



ifoldlShortBytes ::
     (Prim e)
  => Off e
  -- ^ Initial offset to start at
  -> Count e
  -- ^ Total number of elements to iterate through
  -> (a -> Bool)
  -- ^ Continuation condition applied to an accumulator. When `False` it will terminate
  -- early
  -> (a -> Off e -> e -> a)
  -- ^ Folding function
  -> a
  -- ^ Initial accumulator
  -> Bytes p
  -- ^ Memory region to iterate over
  -> a
ifoldlShortBytes off count g f initAcc mem =
  loopShort (coerce off) cond (+ 1) initAcc inner
  where
    inner !i !acc =
      let ioff = coerce i
       in f acc ioff $! indexOffBytes mem ioff
    {-# INLINE inner #-}
    cond i a = g a && i < k
    {-# INLINE cond #-}
    k = coerce count
{-# INLINE ifoldlShortBytes #-}
-}







{-




ifoldlShortMem ::
     (Prim e, MemRead mr)
  => Off e
  -- ^ Initial offset to start at
  -> Count e
  -- ^ Total number of elements to iterate through
  -> (a -> Bool)
  -- ^ Continuation condition applied to an accumulator. When `False` it will terminate
  -- early
  -> (a -> Off e -> e -> a)
  -- ^ Folding function
  -> a
  -- ^ Initial accumulator
  -> mr
  -- ^ Memory region to iterate over
  -> a
ifoldlShortMem off count g f initAcc mem = loop initAcc off
  where
    k = countToOff count + off
    loop !acc !i
      | g acc && i < k = loop (f acc i (indexOffMem mem i)) (i + 1)
      | otherwise = acc

  -- loopShort (coerce off) (\i a -> g a && i < k) (+ 1) initAcc $ \ !i !acc ->
  --   let ioff = coerce i
  --    in f acc ioff (indexOffMem mem ioff)
  -- where
  --   k = coerce count
{-# INLINE ifoldlShortMem #-}

ifoldlShortMemM ::
     (Prim e, MemRead mr, Monad m)
  => Off e
  -- ^ Initial offset to start at
  -> Count e
  -- ^ Total number of elements to iterate through
  -> (a -> m Bool)
  -- ^ Continuation condition applied to an accumulator. When `False` it will terminate
  -- early
  -> (a -> Off e -> e -> m a)
  -- ^ Folding function
  -> a
  -- ^ Initial accumulator
  -> mr
  -- ^ Memory region to iterate over
  -> m a
ifoldlShortMemM off count g f initAcc mem =
  loopShortM (coerce off) (\i a -> (\p -> p && i < k) <$> g a) (+ 1) initAcc $ \ !i !acc ->
    let ioff = coerce i
     in f acc ioff (indexOffMem mem ioff)
  where
    k = coerce count
{-# INLINE ifoldlShortMemM #-}


foldlShortMem ::
     (Prim e, MemRead mr)
  => Off e
  -- ^ Initial offset to start at
  -> Count e
  -- ^ Total number of elements to iterate through
  -> (a -> Bool)
  -- ^ Continuation condition applied to an accumulator. When `False` it will terminate
  -- early
  -> (a -> e -> a)
  -- ^ Folding function
  -> a
  -- ^ Initial accumulator
  -> mr
  -- ^ Memory region to iterate over
  -> a
foldlShortMem off count g f = ifoldlShortMem off count g (\a _ -> f a)
{-# INLINE foldlShortMem #-}
-}




-- -- | Iterate over a region of memory
-- loopMemM_ ::
--      (MemRead mr, MonadPrim s m, Prim e)
--   => r
--   -> Off Word8
--   -> Count e
--   -> (Count Word8 -> a -> Bool)
--   -> (Off Word8 -> e -> m b)
--   -> m (Off Word8)
-- foldlByteOffMemM_ r (Off byteOff) c f =
--   loopShortM byteOff (\i -> f (coerce i))
--   let n = coerce (toByteCount c) + byteOff
--       Count k = byteCountProxy c
--       go i
--         | i < n = f (Off i) (indexByteOffMem r (Off i)) >> go (i + k)
--         | otherwise = pure $ Off i
--    in go byteOff


data MemView a = MemView
  { MemView a -> Off Word8
mvOffset :: {-# UNPACK #-} !(Off Word8)
  , MemView a -> Count Word8
mvCount  :: {-# UNPACK #-} !(Count Word8)
  , MemView a -> a
mvMem    :: !a
  }

data MMemView a s = MMemView
  { MMemView a s -> Off Word8
mmvOffset :: {-# UNPACK #-} !(Off Word8)
  , MMemView a s -> Count Word8
mmvCount  :: {-# UNPACK #-} !(Count Word8)
  , MMemView a s -> a s
mmvMem    :: !(a s)
  }

izipWithByteOffMemM_ ::
     (MemRead mr1, MemRead mr2, MonadPrim s m, Prim e)
  => mr1
  -> Off Word8
  -> mr2
  -> Off Word8
  -> Count e
  -> (Off Word8 -> e -> Off Word8 -> e -> m b)
  -> m (Off Word8)
izipWithByteOffMemM_ :: mr1
-> Off Word8
-> mr2
-> Off Word8
-> Count e
-> (Off Word8 -> e -> Off Word8 -> e -> m b)
-> m (Off Word8)
izipWithByteOffMemM_ mr1
r1 (Off Int
byteOff1) mr2
r2 Off Word8
off2 Count e
c Off Word8 -> e -> Off Word8 -> e -> m b
f =
  let n :: Int
n = Count Word8 -> Int
coerce (Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
c) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
byteOff1
      Count Int
k = Count e -> Count Word8
forall (proxy :: * -> *) e. Prim e => proxy e -> Count Word8
byteCountProxy Count e
c
      go :: Int -> m (Off Word8)
go Int
i
        | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n =
          let o1 :: Off Word8
o1 = Int -> Off Word8
forall e. Int -> Off e
Off Int
i
              o2 :: Off Word8
o2 = Int -> Off Word8
forall e. Int -> Off e
Off Int
i Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
off2
           in Off Word8 -> e -> Off Word8 -> e -> m b
f Off Word8
o1 (mr1 -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem mr1
r1 Off Word8
o1) Off Word8
o2 (mr2 -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem mr2
r2 Off Word8
o2) m b -> m (Off Word8) -> m (Off Word8)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
              Int -> m (Off Word8)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
k)
        | Bool
otherwise = Off Word8 -> m (Off Word8)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Off Word8 -> m (Off Word8)) -> Off Word8 -> m (Off Word8)
forall a b. (a -> b) -> a -> b
$ Int -> Off Word8
forall e. Int -> Off e
Off Int
i
   in Int -> m (Off Word8)
go Int
byteOff1


izipWithOffMemM_ ::
     (MemRead mr1, MemRead mr2, MonadPrim s m, Prim e1, Prim e2)
  => mr1
  -> Off e1
  -> mr2
  -> Off e2
  -> Int
  -> (Off e1 -> e1 -> Off e2 -> e2 -> m b)
  -> m ()
izipWithOffMemM_ :: mr1
-> Off e1
-> mr2
-> Off e2
-> Int
-> (Off e1 -> e1 -> Off e2 -> e2 -> m b)
-> m ()
izipWithOffMemM_ mr1
r1 Off e1
off1 mr2
r2 Off e2
off2 Int
nc Off e1 -> e1 -> Off e2 -> e2 -> m b
f =
  let n :: Int
n = Int
nc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Off e1 -> Int
coerce Off e1
off1
      go :: Off e1 -> Off e2 -> m ()
go o1 :: Off e1
o1@(Off Int
i) Off e2
o2 =
        Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
        Off e1 -> e1 -> Off e2 -> e2 -> m b
f Off e1
o1 (mr1 -> Off e1 -> e1
forall mr e. (MemRead mr, Prim e) => mr -> Off e -> e
indexOffMem mr1
r1 Off e1
o1) Off e2
o2 (mr2 -> Off e2 -> e2
forall mr e. (MemRead mr, Prim e) => mr -> Off e -> e
indexOffMem mr2
r2 Off e2
o2) m b -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Off e1 -> Off e2 -> m ()
go (Off e1
o1 Off e1 -> Off e1 -> Off e1
forall a. Num a => a -> a -> a
+ Off e1
1) (Off e2
o2 Off e2 -> Off e2 -> Off e2
forall a. Num a => a -> a -> a
+ Off e2
1)
   in Off e1 -> Off e2 -> m ()
go Off e1
off1 Off e2
off2


-- class Mut f => MFunctor f where
--   mmap :: (Elt f a, Elt f b, MonadPrim s m) => (a -> b) -> f a s -> m (f b s)

-- class Mut f => MTraverse f where
--   mmapM :: (Elt f a, Elt f b, MonadPrim s m) => (a -> m b) -> f a s -> m (f b s)

-- class MFunctor f => MApplicative f where
--   pureMut :: (Elt f a, MonadPrim s m) => a -> m (f a s)
--   liftMut ::
--     (Elt f a, Elt f b, Elt f c, MonadPrim s m) => (a -> b -> m c) -> f a s -> f b s -> m (f c s)

-- class MApplicative f => MMonad f where
--   bindMut ::
--     (Elt f a, Elt f b, MonadPrim s m) => f a s -> (a -> m b) -> m (f b s)

-- instance MFunctor MAddr where
--   mmap f maddr = do
--     Count n <- getCountMAddr maddr
--     maddr' <- allocMAddr (Count n)
--     let go i =
--           when (i < n) $ do
--             writeOffMAddr maddr' (Off i) . f =<< readOffMAddr maddr (Off i)
--             go (i + 1)
--     maddr' <$ go 0

-- instance MTraverse MAddr where
--   mmapM f maddr = do
--     Count n <- getCountMAddr maddr
--     maddr' <- allocMAddr (Count n)
--     let go i =
--           when (i < n) $ do
--             writeOffMAddr maddr' (Off i) =<< f =<< readOffMAddr maddr (Off i)
--             go (i + 1)
--     maddr' <$ go 0


---------------------
-- Bytes instances --
---------------------

instance MemRead (Bytes p) where
  isSameMem :: Bytes p -> Bytes p -> Bool
isSameMem = Bytes p -> Bytes p -> Bool
forall (p1 :: Pinned) (p2 :: Pinned). Bytes p1 -> Bytes p2 -> Bool
isSameBytes
  {-# INLINE isSameMem #-}
  byteCountMem :: Bytes p -> Count Word8
byteCountMem = Bytes p -> Count Word8
forall (p :: Pinned). Bytes p -> Count Word8
byteCountBytes
  {-# INLINE byteCountMem #-}
  indexOffMem :: Bytes p -> Off e -> e
indexOffMem = Bytes p -> Off e -> e
forall e (p :: Pinned). Prim e => Bytes p -> Off e -> e
indexOffBytes
  {-# INLINE indexOffMem #-}
  indexByteOffMem :: Bytes p -> Off Word8 -> e
indexByteOffMem = Bytes p -> Off Word8 -> e
forall e (p :: Pinned). Prim e => Bytes p -> Off Word8 -> e
indexByteOffBytes
  {-# INLINE indexByteOffMem #-}
  copyByteOffToMBytesMem :: Bytes p -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem = Bytes p -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (ps :: Pinned) (pd :: Pinned).
(MonadPrim s m, Prim e) =>
Bytes ps
-> Off Word8 -> MBytes pd s -> Off Word8 -> Count e -> m ()
copyByteOffBytesToMBytes
  {-# INLINE copyByteOffToMBytesMem #-}
  copyByteOffToPtrMem :: Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem = Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffBytesToPtr
  {-# INLINE copyByteOffToPtrMem #-}
  compareByteOffToPtrMem :: Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem Bytes p
bytes1 Off Word8
off1 Ptr e
ptr2 Off Word8
off2 Count e
c =
    Ordering -> m Ordering
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ordering -> m Ordering) -> Ordering -> m Ordering
forall a b. (a -> b) -> a -> b
$! Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> Ordering
forall e (p :: Pinned).
Prim e =>
Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> Ordering
compareByteOffBytesToPtr Bytes p
bytes1 Off Word8
off1 Ptr e
ptr2 Off Word8
off2 Count e
c
  {-# INLINE compareByteOffToPtrMem #-}
  compareByteOffToBytesMem :: Bytes p -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem Bytes p
bytes1 Off Word8
off1 Bytes p
bytes2 Off Word8
off2 Count e
c =
    Bytes p -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall e (p1 :: Pinned) (p2 :: Pinned).
Prim e =>
Bytes p1
-> Off Word8 -> Bytes p2 -> Off Word8 -> Count e -> Ordering
compareByteOffBytes Bytes p
bytes1 Off Word8
off1 Bytes p
bytes2 Off Word8
off2 Count e
c
  {-# INLINE compareByteOffToBytesMem #-}
  compareByteOffMem :: mr' -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem1 Off Word8
off1 Bytes p
bs Off Word8
off2 Count e
c =
    mr' -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall mr e (p :: Pinned).
(MemRead mr, Prim e) =>
mr -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem mr'
mem1 Off Word8
off1 Bytes p
bs Off Word8
off2 Count e
c
  {-# INLINE compareByteOffMem #-}

instance Typeable p => MemAlloc (MBytes p) where
  type FrozenMem (MBytes p) = Bytes p
  getByteCountMutMem :: MBytes p s -> m (Count Word8)
getByteCountMutMem = MBytes p s -> m (Count Word8)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Count Word8)
getByteCountMBytes
  {-# INLINE getByteCountMutMem #-}
  allocMutMem :: Count e -> m (MBytes p s)
allocMutMem = Count e -> m (MBytes p s)
forall (p :: Pinned) e s (m :: * -> *).
(Typeable p, Prim e, MonadPrim s m) =>
Count e -> m (MBytes p s)
allocMBytes
  {-# INLINE allocMutMem #-}
  thawMem :: FrozenMem (MBytes p) -> m (MBytes p s)
thawMem = FrozenMem (MBytes p) -> m (MBytes p s)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
Bytes p -> m (MBytes p s)
thawBytes
  {-# INLINE thawMem #-}
  freezeMutMem :: MBytes p s -> m (FrozenMem (MBytes p))
freezeMutMem = MBytes p s -> m (FrozenMem (MBytes p))
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes
  {-# INLINE freezeMutMem #-}
  reallocMutMem :: MBytes p s -> Count e -> m (MBytes p s)
reallocMutMem = MBytes p s -> Count e -> m (MBytes p s)
forall e (p :: Pinned) (m :: * -> *) s.
(MonadPrim s m, Typeable p, Prim e) =>
MBytes p s -> Count e -> m (MBytes p s)
reallocMBytes
  {-# INLINE reallocMutMem #-}

instance MemWrite (MBytes p) where
  isSameMutMem :: MBytes p s -> MBytes p s -> Bool
isSameMutMem = MBytes p s -> MBytes p s -> Bool
forall (p1 :: Pinned) s (p2 :: Pinned).
MBytes p1 s -> MBytes p2 s -> Bool
isSameMBytes
  {-# INLINE isSameMutMem #-}
  readOffMutMem :: MBytes p s -> Off e -> m e
readOffMutMem = MBytes p s -> Off e -> m e
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off e -> m e
readOffMBytes
  {-# INLINE readOffMutMem #-}
  readByteOffMutMem :: MBytes p s -> Off Word8 -> m e
readByteOffMutMem = MBytes p s -> Off Word8 -> m e
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off Word8 -> m e
readByteOffMBytes
  {-# INLINE readByteOffMutMem #-}
  writeOffMutMem :: MBytes p s -> Off e -> e -> m ()
writeOffMutMem = MBytes p s -> Off e -> e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off e -> e -> m ()
writeOffMBytes
  {-# INLINE writeOffMutMem #-}
  writeByteOffMutMem :: MBytes p s -> Off Word8 -> e -> m ()
writeByteOffMutMem = MBytes p s -> Off Word8 -> e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off Word8 -> e -> m ()
writeByteOffMBytes
  {-# INLINE writeByteOffMutMem #-}
  moveByteOffToPtrMutMem :: MBytes p s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem = MBytes p s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffMBytesToPtr
  {-# INLINE moveByteOffToPtrMutMem #-}
  moveByteOffToMBytesMutMem :: MBytes p s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem = MBytes p s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (ps :: Pinned) (pd :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes ps s
-> Off Word8 -> MBytes pd s -> Off Word8 -> Count e -> m ()
moveByteOffMBytesToMBytes
  {-# INLINE moveByteOffToMBytesMutMem #-}
  moveByteOffMutMem :: mw' s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffMutMem = mw' s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e (p :: Pinned).
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem
  {-# INLINE moveByteOffMutMem #-}
  copyByteOffMem :: mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffMem = mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e (p :: Pinned).
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem
  {-# INLINE copyByteOffMem #-}
  setMutMem :: MBytes p s -> Off e -> Count e -> e -> m ()
setMutMem = MBytes p s -> Off e -> Count e -> e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off e -> Count e -> e -> m ()
setMBytes
  {-# INLINE setMutMem #-}


instance Show (Bytes p) where
  show :: Bytes p -> String
show Bytes p
b =
    (ShowS -> ShowS) -> String -> [ShowS] -> String
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr' ShowS -> ShowS
forall a b. (a -> b) -> a -> b
($) String
"]" ([ShowS] -> String) -> [ShowS] -> String
forall a b. (a -> b) -> a -> b
$
    (Char
'[' Char -> ShowS
forall a. a -> [a] -> [a]
:) ShowS -> [ShowS] -> [ShowS]
forall a. a -> [a] -> [a]
: ShowS -> [ShowS] -> [ShowS]
forall a. a -> [a] -> [a]
List.intersperse (Char
',' Char -> ShowS
forall a. a -> [a] -> [a]
:) ((ShowS -> ShowS) -> [ShowS] -> [ShowS]
forall a b. (a -> b) -> [a] -> [b]
map ((String
"0x" String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) (Bytes p -> [ShowS]
forall mr. MemRead mr => mr -> [ShowS]
showsHexMem Bytes p
b))

instance Typeable p => IsList (Bytes p) where
  type Item (Bytes p) = Word8
  fromList :: [Item (Bytes p)] -> Bytes p
fromList = [Item (Bytes p)] -> Bytes p
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
[e] -> FrozenMem ma
fromListMem
  {-# INLINE fromList #-}
  fromListN :: Int -> [Item (Bytes p)] -> Bytes p
fromListN Int
n = Count Word8 -> [Word8] -> FrozenMem (MBytes p)
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
Count e -> [e] -> FrozenMem ma
fromListZeroMemN_ (Int -> Count Word8
forall e. Int -> Count e
Count Int
n)
  {-# INLINE fromListN #-}
  toList :: Bytes p -> [Item (Bytes p)]
toList = Bytes p -> [Item (Bytes p)]
forall e mr. (MemRead mr, Prim e) => mr -> [e]
toListMem
  {-# INLINE toList #-}

instance Eq (Bytes p) where
  Bytes p
b1 == :: Bytes p -> Bytes p -> Bool
== Bytes p
b2 = Bytes p -> Bytes p -> Bool
forall (p1 :: Pinned) (p2 :: Pinned). Bytes p1 -> Bytes p2 -> Bool
isSameBytes Bytes p
b1 Bytes p
b2 Bool -> Bool -> Bool
|| Bytes p -> Bytes p -> Bool
forall mr1 mr2. (MemRead mr1, MemRead mr2) => mr1 -> mr2 -> Bool
eqByteMem Bytes p
b1 Bytes p
b2
  {-# INLINE (==) #-}

instance Ord (Bytes p) where
  compare :: Bytes p -> Bytes p -> Ordering
compare Bytes p
b1 Bytes p
b2 =
    Count Word8 -> Count Word8 -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Count Word8
n (Bytes p -> Count Word8
forall (p :: Pinned). Bytes p -> Count Word8
byteCountBytes Bytes p
b2) Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> Bytes p
-> Off Word8 -> Bytes p -> Off Word8 -> Count Word8 -> Ordering
forall e (p1 :: Pinned) (p2 :: Pinned).
Prim e =>
Bytes p1
-> Off Word8 -> Bytes p2 -> Off Word8 -> Count e -> Ordering
compareByteOffBytes Bytes p
b1 Off Word8
0 Bytes p
b2 Off Word8
0 Count Word8
n
    where
      n :: Count Word8
n = Bytes p -> Count Word8
forall (p :: Pinned). Bytes p -> Count Word8
byteCountBytes Bytes p
b1
  {-# INLINE compare #-}

instance Typeable p => Semigroup.Semigroup (Bytes p) where
  <> :: Bytes p -> Bytes p -> Bytes p
(<>) = Bytes p -> Bytes p -> Bytes p
forall mr1 mr2 (ma :: * -> *).
(MemRead mr1, MemRead mr2, MemAlloc ma) =>
mr1 -> mr2 -> FrozenMem ma
appendMem
  {-# INLINE (<>) #-}
  sconcat :: NonEmpty (Bytes p) -> Bytes p
sconcat (Bytes p
x :| [Bytes p]
xs) = [Bytes p] -> FrozenMem (MBytes p)
forall mr (ma :: * -> *).
(MemRead mr, MemAlloc ma) =>
[mr] -> FrozenMem ma
concatMem (Bytes p
xBytes p -> [Bytes p] -> [Bytes p]
forall a. a -> [a] -> [a]
:[Bytes p]
xs)
  {-# INLINE sconcat #-}
  stimes :: b -> Bytes p -> Bytes p
stimes b
i = Int -> Bytes p -> FrozenMem (MBytes p)
forall (ma :: * -> *) mr.
(MemAlloc ma, MemRead mr) =>
Int -> mr -> FrozenMem ma
cycleMemN (b -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral b
i)
  {-# INLINE stimes #-}

instance Typeable p => Monoid.Monoid (Bytes p) where
  mappend :: Bytes p -> Bytes p -> Bytes p
mappend = Bytes p -> Bytes p -> Bytes p
forall mr1 mr2 (ma :: * -> *).
(MemRead mr1, MemRead mr2, MemAlloc ma) =>
mr1 -> mr2 -> FrozenMem ma
appendMem
  {-# INLINE mappend #-}
  mconcat :: [Bytes p] -> Bytes p
mconcat = [Bytes p] -> Bytes p
forall mr (ma :: * -> *).
(MemRead mr, MemAlloc ma) =>
[mr] -> FrozenMem ma
concatMem
  {-# INLINE mconcat #-}
  mempty :: Bytes p
mempty = Bytes p
forall (ma :: * -> *). MemAlloc ma => FrozenMem ma
emptyMem
  {-# INLINE mempty #-}


-- | A list of `ShowS` which covert bytes to base16 encoded strings. Each element of the list
-- is a function that will convert one byte.
--
-- ====__Example__
--
-- >>> :set -XDataKinds
-- >>> import Data.Prim.Memory
-- >>> concatMap ($ " ") $ showsHexMem (fromListMem [1 :: Int16 .. 15] :: Bytes 'Inc)
-- "01 00 02 00 03 00 04 00 05 00 06 00 07 00 08 00 09 00 0a 00 0b 00 0c 00 0d 00 0e 00 0f 00 "
--
-- @since 0.1.0
showsHexMem :: MemRead mr => mr -> [ShowS]
showsHexMem :: mr -> [ShowS]
showsHexMem mr
b = (Word8 -> ShowS) -> [Word8] -> [ShowS]
forall a b. (a -> b) -> [a] -> [b]
map Word8 -> ShowS
forall a. (Integral a, Show a) => a -> ShowS
toHex (mr -> [Word8]
forall e mr. (MemRead mr, Prim e) => mr -> [e]
toListMem mr
b :: [Word8])
  where
    toHex :: a -> ShowS
toHex a
b8 =
      (if a
b8 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0x0f
         then (Char
'0' Char -> ShowS
forall a. a -> [a] -> [a]
:)
         else ShowS
forall a. a -> a
id) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      a -> ShowS
forall a. (Integral a, Show a) => a -> ShowS
showHex a
b8

-- | Allocate a new region of memory and Ensure that it is filled with zeros before and
-- after it gets used. `PtrAccess` is not used directly, but instead is used to guarantee
-- that the memory is pinned and its contents will not get moved around by the garbage
-- collector.
--
-- @since 0.3.0
withScrubbedMutMem ::
     forall e ma m a.
     (MonadUnliftPrim RW m, Prim e, MemAlloc ma, PtrAccess RW (ma RW))
  => Count e
  -> (ma RW -> m a)
  -> m a
withScrubbedMutMem :: Count e -> (ma RW -> m a) -> m a
withScrubbedMutMem Count e
c ma RW -> m a
f = do
  ma RW
mem <- Count e -> m (ma RW)
forall e (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m, Prim e) =>
Count e -> m (ma s)
allocZeroMutMem Count e
c
  let _fptr :: IO (ForeignPtr e)
_fptr = ma RW -> IO (ForeignPtr e)
forall s p (m :: * -> *) a.
(PtrAccess s p, MonadPrim s m) =>
p -> m (ForeignPtr a)
toForeignPtr ma RW
mem :: IO (ForeignPtr e) -- Enforce the `PtrAccess` constraint.
  ma RW -> m a
f ma RW
mem m a -> m () -> m a
forall (m :: * -> *) a b. MonadUnliftPrim RW m => m a -> m b -> m a
`ufinally` ma RW -> Off Word8 -> Count Word8 -> Word8 -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> Count e -> e -> m ()
setMutMem ma RW
mem Off Word8
0 (Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
c) Word8
0
{-# INLINE withScrubbedMutMem #-}