{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
-- |
-- Module      : Data.Prim.Memory.PArray
-- Copyright   : (c) Alexey Kuleshevich 2020
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <alexey@kuleshevi.ch>
-- Stability   : experimental
-- Portability : non-portable
--
module Data.Prim.Memory.PArray
  ( PArray(..)
  , PMArray(..)
  , Pinned(..)
  , fromBytesPArray
  , toBytesPArray
  , fromUArrayPArray
  , toUArrayPArray
  , castPArray
  , fromMBytesPMArray
  , toMBytesPMArray
  , fromUMArrayPMArray
  , toUMArrayPMArray
  , castPMArray
  , allocPMArray
  , allocPinnedPMArray
  , allocAlignedPMArray
  , allocUnpinnedPMArray
  , shrinkPMArray
  , resizePMArray
  , reallocPMArray
  , isPinnedPArray
  , isPinnedPMArray

  , thawPArray
  , freezePMArray
  , sizePArray
  , getSizePMArray
  , readPMArray
  , writePMArray

  , setPMArray
  , copyPArrayToPMArray
  , movePMArrayToPMArray
  ) where

import Control.DeepSeq
import Control.Prim.Monad
import Data.Prim
import Data.Prim.Array (Size(..), UArray(..), UMArray(..))
import Data.Prim.Memory.Bytes
import Data.Prim.Memory.Fold
import Data.Prim.Memory.ForeignPtr
import Data.Prim.Memory.Internal
import Foreign.Prim

-- | An immutable array with elements of type @e@
newtype PArray (p :: Pinned) e = PArray (Bytes p)
  deriving (PArray p e -> ()
(PArray p e -> ()) -> NFData (PArray p e)
forall a. (a -> ()) -> NFData a
forall (p :: Pinned) e. PArray p e -> ()
rnf :: PArray p e -> ()
$crnf :: forall (p :: Pinned) e. PArray p e -> ()
NFData, b -> PArray p e -> PArray p e
NonEmpty (PArray p e) -> PArray p e
PArray p e -> PArray p e -> PArray p e
(PArray p e -> PArray p e -> PArray p e)
-> (NonEmpty (PArray p e) -> PArray p e)
-> (forall b. Integral b => b -> PArray p e -> PArray p e)
-> Semigroup (PArray p e)
forall b. Integral b => b -> PArray p e -> PArray p e
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall (p :: Pinned) e.
Typeable p =>
NonEmpty (PArray p e) -> PArray p e
forall (p :: Pinned) e.
Typeable p =>
PArray p e -> PArray p e -> PArray p e
forall (p :: Pinned) e b.
(Typeable p, Integral b) =>
b -> PArray p e -> PArray p e
stimes :: b -> PArray p e -> PArray p e
$cstimes :: forall (p :: Pinned) e b.
(Typeable p, Integral b) =>
b -> PArray p e -> PArray p e
sconcat :: NonEmpty (PArray p e) -> PArray p e
$csconcat :: forall (p :: Pinned) e.
Typeable p =>
NonEmpty (PArray p e) -> PArray p e
<> :: PArray p e -> PArray p e -> PArray p e
$c<> :: forall (p :: Pinned) e.
Typeable p =>
PArray p e -> PArray p e -> PArray p e
Semigroup, Semigroup (PArray p e)
PArray p e
Semigroup (PArray p e)
-> PArray p e
-> (PArray p e -> PArray p e -> PArray p e)
-> ([PArray p e] -> PArray p e)
-> Monoid (PArray p e)
[PArray p e] -> PArray p e
PArray p e -> PArray p e -> PArray p e
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall (p :: Pinned) e. Typeable p => Semigroup (PArray p e)
forall (p :: Pinned) e. Typeable p => PArray p e
forall (p :: Pinned) e. Typeable p => [PArray p e] -> PArray p e
forall (p :: Pinned) e.
Typeable p =>
PArray p e -> PArray p e -> PArray p e
mconcat :: [PArray p e] -> PArray p e
$cmconcat :: forall (p :: Pinned) e. Typeable p => [PArray p e] -> PArray p e
mappend :: PArray p e -> PArray p e -> PArray p e
$cmappend :: forall (p :: Pinned) e.
Typeable p =>
PArray p e -> PArray p e -> PArray p e
mempty :: PArray p e
$cmempty :: forall (p :: Pinned) e. Typeable p => PArray p e
$cp1Monoid :: forall (p :: Pinned) e. Typeable p => Semigroup (PArray p e)
Monoid, mr' -> Off Word8 -> PArray p e -> Off Word8 -> Count e -> Ordering
PArray p e -> Count Word8
PArray p e -> Off e -> e
PArray p e -> Off Word8 -> e
PArray p e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
PArray p e
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
PArray p e
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
PArray p e
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
PArray p e -> PArray p e -> Bool
(PArray p e -> PArray p e -> Bool)
-> (PArray p e -> Count Word8)
-> (forall e. Prim e => PArray p e -> Off e -> e)
-> (forall e. Prim e => PArray p e -> Off Word8 -> e)
-> (forall s (m :: * -> *) e (p :: Pinned).
    (MonadPrim s m, Prim e) =>
    PArray p e
    -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ())
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    PArray p e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ())
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    PArray p e
    -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering)
-> (forall e (p :: Pinned).
    Prim e =>
    PArray p e
    -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering)
-> (forall mr' e.
    (MemRead mr', Prim e) =>
    mr' -> Off Word8 -> PArray p e -> Off Word8 -> Count e -> Ordering)
-> MemRead (PArray p e)
forall e. Prim e => PArray p e -> Off e -> e
forall e. Prim e => PArray p e -> Off Word8 -> e
forall mr.
(mr -> mr -> Bool)
-> (mr -> Count Word8)
-> (forall e. Prim e => mr -> Off e -> e)
-> (forall e. Prim e => mr -> Off Word8 -> e)
-> (forall s (m :: * -> *) e (p :: Pinned).
    (MonadPrim s m, Prim e) =>
    mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ())
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ())
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering)
-> (forall e (p :: Pinned).
    Prim e =>
    mr -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering)
-> (forall mr' e.
    (MemRead mr', Prim e) =>
    mr' -> Off Word8 -> mr -> Off Word8 -> Count e -> Ordering)
-> MemRead mr
forall mr' e.
(MemRead mr', Prim e) =>
mr' -> Off Word8 -> PArray p e -> Off Word8 -> Count e -> Ordering
forall e (p :: Pinned).
Prim e =>
PArray p e
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PArray p e
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PArray p e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
PArray p e
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall (p :: Pinned) e. PArray p e -> Count Word8
forall (p :: Pinned) e. PArray p e -> PArray p e -> Bool
forall (p :: Pinned) e e. Prim e => PArray p e -> Off e -> e
forall (p :: Pinned) e e. Prim e => PArray p e -> Off Word8 -> e
forall (p :: Pinned) e mr' e.
(MemRead mr', Prim e) =>
mr' -> Off Word8 -> PArray p e -> Off Word8 -> Count e -> Ordering
forall (p :: Pinned) e e (p :: Pinned).
Prim e =>
PArray p e
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PArray p e
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PArray p e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall (p :: Pinned) e s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
PArray p e
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
compareByteOffMem :: mr' -> Off Word8 -> PArray p e -> Off Word8 -> Count e -> Ordering
$ccompareByteOffMem :: forall (p :: Pinned) e mr' e.
(MemRead mr', Prim e) =>
mr' -> Off Word8 -> PArray p e -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem :: PArray p e
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
$ccompareByteOffToBytesMem :: forall (p :: Pinned) e e (p :: Pinned).
Prim e =>
PArray p e
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToPtrMem :: PArray p e
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
$ccompareByteOffToPtrMem :: forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PArray p e
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
copyByteOffToPtrMem :: PArray p e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
$ccopyByteOffToPtrMem :: forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PArray p e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem :: PArray p e
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
$ccopyByteOffToMBytesMem :: forall (p :: Pinned) e s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
PArray p e
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
indexByteOffMem :: PArray p e -> Off Word8 -> e
$cindexByteOffMem :: forall (p :: Pinned) e e. Prim e => PArray p e -> Off Word8 -> e
indexOffMem :: PArray p e -> Off e -> e
$cindexOffMem :: forall (p :: Pinned) e e. Prim e => PArray p e -> Off e -> e
byteCountMem :: PArray p e -> Count Word8
$cbyteCountMem :: forall (p :: Pinned) e. PArray p e -> Count Word8
isSameMem :: PArray p e -> PArray p e -> Bool
$cisSameMem :: forall (p :: Pinned) e. PArray p e -> PArray p e -> Bool
MemRead)
type role PArray nominal nominal


instance (Prim e, Eq e) => Eq (PArray p e) where
  == :: PArray p e -> PArray p e -> Bool
(==) = forall mr. (Prim e, Eq e, MemRead mr) => mr -> mr -> Bool
forall e mr. (Prim e, Eq e, MemRead mr) => mr -> mr -> Bool
eqMem @e
  {-# INLINE (==) #-}

instance (Prim e, Ord e) => Ord (PArray p e) where
  compare :: PArray p e -> PArray p e -> Ordering
compare = forall mr. (Prim e, Ord e, MemRead mr) => mr -> mr -> Ordering
forall e mr. (Prim e, Ord e, MemRead mr) => mr -> mr -> Ordering
compareMem @e
  {-# INLINE compare #-}

-- | A mutable array with elements of type @e@
newtype PMArray (p :: Pinned) e s = PMArray (MBytes p s)
  deriving (PMArray p e s -> ()
(PMArray p e s -> ()) -> NFData (PMArray p e s)
forall a. (a -> ()) -> NFData a
forall (p :: Pinned) e s. PMArray p e s -> ()
rnf :: PMArray p e s -> ()
$crnf :: forall (p :: Pinned) e s. PMArray p e s -> ()
NFData, mr -> Off Word8 -> PMArray p e s -> Off Word8 -> Count e -> m ()
mw' s -> Off Word8 -> PMArray p e s -> Off Word8 -> Count e -> m ()
PMArray p e s -> PMArray p e s -> Bool
PMArray p e s -> Off e -> m e
PMArray p e s -> Off Word8 -> m e
PMArray p e s -> Off e -> e -> m ()
PMArray p e s -> Off Word8 -> e -> m ()
PMArray p e s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
PMArray p e s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
PMArray p e s -> Off e -> Count e -> e -> m ()
(forall s. PMArray p e s -> PMArray p e s -> Bool)
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    PMArray p e s -> Off e -> m e)
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    PMArray p e s -> Off Word8 -> m e)
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    PMArray p e s -> Off e -> e -> m ())
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    PMArray p e s -> Off Word8 -> e -> m ())
-> (forall s (m :: * -> *) e (p :: Pinned).
    (MonadPrim s m, Prim e) =>
    PMArray p e s
    -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ())
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    PMArray p e s
    -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ())
-> (forall s (m :: * -> *) mr e.
    (MonadPrim s m, MemRead mr, Prim e) =>
    mr -> Off Word8 -> PMArray p e s -> Off Word8 -> Count e -> m ())
-> (forall s (m :: * -> *) (mw' :: * -> *) e.
    (MonadPrim s m, MemWrite mw', Prim e) =>
    mw' s
    -> Off Word8 -> PMArray p e s -> Off Word8 -> Count e -> m ())
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    PMArray p e s -> Off e -> Count e -> e -> m ())
-> MemWrite (PMArray p e)
forall s. PMArray p e s -> PMArray p e s -> Bool
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off e -> m e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off e -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off e -> Count e -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off Word8 -> m e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off Word8 -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) mr e.
(MonadPrim s m, MemRead mr, Prim e) =>
mr -> Off Word8 -> PMArray p e s -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
PMArray p e s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) (mw' :: * -> *) e.
(MonadPrim s m, MemWrite mw', Prim e) =>
mw' s -> Off Word8 -> PMArray p e s -> Off Word8 -> Count e -> m ()
forall (p :: Pinned) e s. PMArray p e s -> PMArray p e s -> Bool
forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off e -> m e
forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off e -> e -> m ()
forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off e -> Count e -> e -> m ()
forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off Word8 -> m e
forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off Word8 -> e -> m ()
forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall (p :: Pinned) e s (m :: * -> *) mr e.
(MonadPrim s m, MemRead mr, Prim e) =>
mr -> Off Word8 -> PMArray p e s -> Off Word8 -> Count e -> m ()
forall (p :: Pinned) e s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
PMArray p e s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall (p :: Pinned) e s (m :: * -> *) (mw' :: * -> *) e.
(MonadPrim s m, MemWrite mw', Prim e) =>
mw' s -> Off Word8 -> PMArray p e s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *).
(forall s. mw s -> mw s -> Bool)
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    mw s -> Off e -> m e)
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    mw s -> Off Word8 -> m e)
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    mw s -> Off e -> e -> m ())
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    mw s -> Off Word8 -> e -> m ())
-> (forall s (m :: * -> *) e (p :: Pinned).
    (MonadPrim s m, Prim e) =>
    mw s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ())
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    mw s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ())
-> (forall s (m :: * -> *) mr e.
    (MonadPrim s m, MemRead mr, Prim e) =>
    mr -> Off Word8 -> mw s -> Off Word8 -> Count e -> m ())
-> (forall s (m :: * -> *) (mw' :: * -> *) e.
    (MonadPrim s m, MemWrite mw', Prim e) =>
    mw' s -> Off Word8 -> mw s -> Off Word8 -> Count e -> m ())
-> (forall s (m :: * -> *) e.
    (MonadPrim s m, Prim e) =>
    mw s -> Off e -> Count e -> e -> m ())
-> MemWrite mw
setMutMem :: PMArray p e s -> Off e -> Count e -> e -> m ()
$csetMutMem :: forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off e -> Count e -> e -> m ()
moveByteOffMutMem :: mw' s -> Off Word8 -> PMArray p e s -> Off Word8 -> Count e -> m ()
$cmoveByteOffMutMem :: forall (p :: Pinned) e s (m :: * -> *) (mw' :: * -> *) e.
(MonadPrim s m, MemWrite mw', Prim e) =>
mw' s -> Off Word8 -> PMArray p e s -> Off Word8 -> Count e -> m ()
copyByteOffMem :: mr -> Off Word8 -> PMArray p e s -> Off Word8 -> Count e -> m ()
$ccopyByteOffMem :: forall (p :: Pinned) e s (m :: * -> *) mr e.
(MonadPrim s m, MemRead mr, Prim e) =>
mr -> Off Word8 -> PMArray p e s -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem :: PMArray p e s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
$cmoveByteOffToPtrMutMem :: forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem :: PMArray p e s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
$cmoveByteOffToMBytesMutMem :: forall (p :: Pinned) e s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
PMArray p e s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
writeByteOffMutMem :: PMArray p e s -> Off Word8 -> e -> m ()
$cwriteByteOffMutMem :: forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off Word8 -> e -> m ()
writeOffMutMem :: PMArray p e s -> Off e -> e -> m ()
$cwriteOffMutMem :: forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off e -> e -> m ()
readByteOffMutMem :: PMArray p e s -> Off Word8 -> m e
$creadByteOffMutMem :: forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off Word8 -> m e
readOffMutMem :: PMArray p e s -> Off e -> m e
$creadOffMutMem :: forall (p :: Pinned) e s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
PMArray p e s -> Off e -> m e
isSameMutMem :: PMArray p e s -> PMArray p e s -> Bool
$cisSameMutMem :: forall (p :: Pinned) e s. PMArray p e s -> PMArray p e s -> Bool
MemWrite)
type role PMArray nominal nominal nominal

-- | Read-only access, but it is not enforced.
instance PtrAccess s (PArray 'Pin e) where
  toForeignPtr :: PArray 'Pin e -> m (ForeignPtr a)
toForeignPtr = ForeignPtr a -> m (ForeignPtr a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ForeignPtr a -> m (ForeignPtr a))
-> (PArray 'Pin e -> ForeignPtr a)
-> PArray 'Pin e
-> m (ForeignPtr a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bytes 'Pin -> ForeignPtr a
forall e. Bytes 'Pin -> ForeignPtr e
toForeignPtrBytes (Bytes 'Pin -> ForeignPtr a)
-> (PArray 'Pin e -> Bytes 'Pin) -> PArray 'Pin e -> ForeignPtr a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PArray 'Pin e -> Bytes 'Pin
forall (p :: Pinned) e. PArray p e -> Bytes p
toBytesPArray
  {-# INLINE toForeignPtr #-}
  withPtrAccess :: PArray 'Pin e -> (Ptr a -> m b) -> m b
withPtrAccess PArray 'Pin e
b = Bytes 'Pin -> (Ptr a -> m b) -> m b
forall s (m :: * -> *) e b.
MonadPrim s m =>
Bytes 'Pin -> (Ptr e -> m b) -> m b
withPtrBytes (PArray 'Pin e -> Bytes 'Pin
forall (p :: Pinned) e. PArray p e -> Bytes p
toBytesPArray PArray 'Pin e
b)
  {-# INLINE withPtrAccess #-}
  withNoHaltPtrAccess :: PArray 'Pin e -> (Ptr a -> m b) -> m b
withNoHaltPtrAccess PArray 'Pin e
b = Bytes 'Pin -> (Ptr a -> m b) -> m b
forall s (m :: * -> *) e b.
MonadUnliftPrim s m =>
Bytes 'Pin -> (Ptr e -> m b) -> m b
withNoHaltPtrBytes (PArray 'Pin e -> Bytes 'Pin
forall (p :: Pinned) e. PArray p e -> Bytes p
toBytesPArray PArray 'Pin e
b)
  {-# INLINE withNoHaltPtrAccess #-}

instance PtrAccess s (PMArray 'Pin e s) where
  toForeignPtr :: PMArray 'Pin e s -> m (ForeignPtr a)
toForeignPtr = ForeignPtr a -> m (ForeignPtr a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ForeignPtr a -> m (ForeignPtr a))
-> (PMArray 'Pin e s -> ForeignPtr a)
-> PMArray 'Pin e s
-> m (ForeignPtr a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MBytes 'Pin s -> ForeignPtr a
forall s e. MBytes 'Pin s -> ForeignPtr e
toForeignPtrMBytes (MBytes 'Pin s -> ForeignPtr a)
-> (PMArray 'Pin e s -> MBytes 'Pin s)
-> PMArray 'Pin e s
-> ForeignPtr a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PMArray 'Pin e s -> MBytes 'Pin s
forall (p :: Pinned) e s. PMArray p e s -> MBytes p s
toMBytesPMArray
  {-# INLINE toForeignPtr #-}
  withPtrAccess :: PMArray 'Pin e s -> (Ptr a -> m b) -> m b
withPtrAccess PMArray 'Pin e s
mb = MBytes 'Pin s -> (Ptr a -> m b) -> m b
forall s (m :: * -> *) e b.
MonadPrim s m =>
MBytes 'Pin s -> (Ptr e -> m b) -> m b
withPtrMBytes (PMArray 'Pin e s -> MBytes 'Pin s
forall (p :: Pinned) e s. PMArray p e s -> MBytes p s
toMBytesPMArray PMArray 'Pin e s
mb)
  {-# INLINE withPtrAccess #-}
  withNoHaltPtrAccess :: PMArray 'Pin e s -> (Ptr a -> m b) -> m b
withNoHaltPtrAccess PMArray 'Pin e s
mb = MBytes 'Pin s -> (Ptr a -> m b) -> m b
forall s (m :: * -> *) e b.
MonadUnliftPrim s m =>
MBytes 'Pin s -> (Ptr e -> m b) -> m b
withNoHaltPtrMBytes (PMArray 'Pin e s -> MBytes 'Pin s
forall (p :: Pinned) e s. PMArray p e s -> MBytes p s
toMBytesPMArray PMArray 'Pin e s
mb)
  {-# INLINE withNoHaltPtrAccess #-}

instance Typeable p => MemAlloc (PMArray p e) where
  type FrozenMem (PMArray p e) = PArray p e
  getByteCountMutMem :: PMArray p e s -> m (Count Word8)
getByteCountMutMem = MBytes p s -> m (Count Word8)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (Count Word8)
getByteCountMutMem (MBytes p s -> m (Count Word8))
-> (PMArray p e s -> MBytes p s)
-> PMArray p e s
-> m (Count Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PMArray p e s -> MBytes p s
forall (p :: Pinned) e s. PMArray p e s -> MBytes p s
toMBytesPMArray
  {-# INLINE getByteCountMutMem #-}
  allocMutMem :: Count e -> m (PMArray p e s)
allocMutMem = (MBytes p s -> PMArray p e s)
-> m (MBytes p s) -> m (PMArray p e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes p s -> PMArray p e s
forall (p :: Pinned) s e. MBytes p s -> PMArray p e s
fromMBytesPMArray (m (MBytes p s) -> m (PMArray p e s))
-> (Count e -> m (MBytes p s)) -> Count e -> m (PMArray p e s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 (PMArray p e) -> m (PMArray p e s)
thawMem = FrozenMem (PMArray p e) -> m (PMArray p e s)
forall s (m :: * -> *) (p :: Pinned) e.
MonadPrim s m =>
PArray p e -> m (PMArray p e s)
thawPArray
  {-# INLINE thawMem #-}
  freezeMutMem :: PMArray p e s -> m (FrozenMem (PMArray p e))
freezeMutMem = PMArray p e s -> m (FrozenMem (PMArray p e))
forall s (m :: * -> *) (p :: Pinned) e.
MonadPrim s m =>
PMArray p e s -> m (PArray p e)
freezePMArray
  {-# INLINE freezeMutMem #-}
  reallocMutMem :: PMArray p e s -> Count e -> m (PMArray p e s)
reallocMutMem PMArray p e s
mba = (MBytes p s -> PMArray p e s)
-> m (MBytes p s) -> m (PMArray p e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes p s -> PMArray p e s
forall (p :: Pinned) s e. MBytes p s -> PMArray p e s
fromMBytesPMArray (m (MBytes p s) -> m (PMArray p e s))
-> (Count e -> m (MBytes p s)) -> Count e -> m (PMArray p e s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 (PMArray p e s -> MBytes p s
forall (p :: Pinned) e s. PMArray p e s -> MBytes p s
toMBytesPMArray PMArray p e s
mba)
  {-# INLINE reallocMutMem #-}

instance (Typeable p, Prim e) => IsList (PArray p e) where
  type Item (PArray p e) = e
  fromList :: [Item (PArray p e)] -> PArray p e
fromList = [Item (PArray p e)] -> PArray p e
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
[e] -> FrozenMem ma
fromListMem
  fromListN :: Int -> [Item (PArray p e)] -> PArray p e
fromListN Int
n = Count e -> [e] -> FrozenMem (PMArray p e)
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
Count e -> [e] -> FrozenMem ma
fromListZeroMemN_ (Int -> Count e
forall e. Int -> Count e
Count Int
n)
  toList :: PArray p e -> [Item (PArray p e)]
toList = PArray p e -> [Item (PArray p e)]
forall e mr. (MemRead mr, Prim e) => mr -> [e]
toListMem

instance Typeable p => IsString (PArray p Char) where
  fromString :: String -> PArray p Char
fromString = String -> PArray p Char
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
[e] -> FrozenMem ma
fromListMem

instance (Show e, Prim e) => Show (PArray p e) where
  show :: PArray p e -> String
show = [e] -> String
forall a. Show a => a -> String
show ([e] -> String) -> (PArray p e -> [e]) -> PArray p e -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PArray p e -> [e]
forall e (p :: Pinned). Prim e => PArray p e -> [e]
toListPArray


toListPArray :: Prim e => PArray p e -> [e]
toListPArray :: PArray p e -> [e]
toListPArray = PArray p e -> [e]
forall e mr. (MemRead mr, Prim e) => mr -> [e]
toListMem

castPArray :: PArray p e' -> PArray p e
castPArray :: PArray p e' -> PArray p e
castPArray = PArray p e' -> PArray p e
coerce

-- | /O(1)/ - Cast `PArray` to `UArray`
--
-- @since 0.3.0
toUArrayPArray :: PArray p e -> UArray e
toUArrayPArray :: PArray p e -> UArray e
toUArrayPArray PArray p e
pa = ByteArray# -> UArray e
forall e. ByteArray# -> UArray e
UArray (Bytes p -> ByteArray#
forall (p :: Pinned). Bytes p -> ByteArray#
toByteArray# (PArray p e -> Bytes p
forall (p :: Pinned) e. PArray p e -> Bytes p
toBytesPArray PArray p e
pa))
{-# INLINE toUArrayPArray #-}

-- | /O(1)/ - Cast `UArray` to `PArray`
--
-- @since 0.3.0
fromUArrayPArray :: UArray e -> PArray 'Inc e
fromUArrayPArray :: UArray e -> PArray 'Inc e
fromUArrayPArray (UArray ByteArray#
ba#) = Bytes 'Inc -> PArray 'Inc e
forall (p :: Pinned) e. Bytes p -> PArray p e
fromBytesPArray (ByteArray# -> Bytes 'Inc
fromByteArray# ByteArray#
ba#)
{-# INLINE fromUArrayPArray #-}

fromBytesPArray :: Bytes p -> PArray p e
fromBytesPArray :: Bytes p -> PArray p e
fromBytesPArray = Bytes p -> PArray p e
coerce

toBytesPArray :: PArray p e -> Bytes p
toBytesPArray :: PArray p e -> Bytes p
toBytesPArray = PArray p e -> Bytes p
coerce

castPMArray :: PMArray p e' s -> PMArray p e s
castPMArray :: PMArray p e' s -> PMArray p e s
castPMArray = PMArray p e' s -> PMArray p e s
coerce

fromMBytesPMArray :: MBytes p s -> PMArray p e s
fromMBytesPMArray :: MBytes p s -> PMArray p e s
fromMBytesPMArray = MBytes p s -> PMArray p e s
coerce

toMBytesPMArray :: PMArray p e s -> MBytes p s
toMBytesPMArray :: PMArray p e s -> MBytes p s
toMBytesPMArray = PMArray p e s -> MBytes p s
coerce

-- | /O(1)/ - Cast `PMArray` to `UMArray`
--
-- @since 0.3.0
toUMArrayPMArray :: PMArray p e s -> UMArray e s
toUMArrayPMArray :: PMArray p e s -> UMArray e s
toUMArrayPMArray PMArray p e s
pa = MutableByteArray# s -> UMArray e s
forall e s. MutableByteArray# s -> UMArray e s
UMArray (MBytes p s -> MutableByteArray# s
forall (p :: Pinned) s. MBytes p s -> MutableByteArray# s
toMutableByteArray# (PMArray p e s -> MBytes p s
forall (p :: Pinned) e s. PMArray p e s -> MBytes p s
toMBytesPMArray PMArray p e s
pa))
{-# INLINE toUMArrayPMArray #-}

-- | /O(1)/ - Cast `UMArray` to `PMArray`
--
-- @since 0.3.0
fromUMArrayPMArray :: UMArray e s -> PMArray 'Inc e s
fromUMArrayPMArray :: UMArray e s -> PMArray 'Inc e s
fromUMArrayPMArray (UMArray MutableByteArray# s
mba#) = MBytes 'Inc s -> PMArray 'Inc e s
forall (p :: Pinned) s e. MBytes p s -> PMArray p e s
fromMBytesPMArray (MutableByteArray# s -> MBytes 'Inc s
forall s. MutableByteArray# s -> MBytes 'Inc s
fromMutableByteArray# MutableByteArray# s
mba#)
{-# INLINE fromUMArrayPMArray #-}


sizePArray :: forall e p. Prim e => PArray p e -> Size
sizePArray :: PArray p e -> Size
sizePArray = (Count e -> Size
coerce :: Count e -> Size) (Count e -> Size) -> (PArray p e -> Count e) -> PArray p e -> Size
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bytes p -> Count e
forall e (p :: Pinned). Prim e => Bytes p -> Count e
countBytes (Bytes p -> Count e)
-> (PArray p e -> Bytes p) -> PArray p e -> Count e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PArray p e -> Bytes p
forall (p :: Pinned) e. PArray p e -> Bytes p
toBytesPArray
{-# INLINE sizePArray #-}

getSizePMArray :: forall e p m s. (MonadPrim s m, Prim e) => PMArray p e s -> m Size
getSizePMArray :: PMArray p e s -> m Size
getSizePMArray = (Count e -> Size) -> m (Count e) -> m Size
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Count e -> Size
coerce :: Count e -> Size) (m (Count e) -> m Size)
-> (PMArray p e s -> m (Count e)) -> PMArray p e s -> m Size
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MBytes p s -> m (Count e)
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> m (Count e)
getCountMBytes (MBytes p s -> m (Count e))
-> (PMArray p e s -> MBytes p s) -> PMArray p e s -> m (Count e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PMArray p e s -> MBytes p s
forall (p :: Pinned) e s. PMArray p e s -> MBytes p s
toMBytesPMArray
{-# INLINE getSizePMArray #-}

allocPMArray ::
     forall e p m s . (Typeable p, Prim e, MonadPrim s m) => Size -> m (PMArray p e s)
allocPMArray :: Size -> m (PMArray p e s)
allocPMArray Size
sz = MBytes p s -> PMArray p e s
forall (p :: Pinned) s e. MBytes p s -> PMArray p e s
fromMBytesPMArray (MBytes p s -> PMArray p e s)
-> m (MBytes p s) -> m (PMArray p e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 (Size -> Count e
coerce Size
sz :: Count e)
{-# INLINE allocPMArray #-}

allocUnpinnedPMArray :: forall e m s . (MonadPrim s m, Prim e) => Size -> m (PMArray 'Inc e s)
allocUnpinnedPMArray :: Size -> m (PMArray 'Inc e s)
allocUnpinnedPMArray Size
sz = MBytes 'Inc s -> PMArray 'Inc e s
forall (p :: Pinned) s e. MBytes p s -> PMArray p e s
fromMBytesPMArray (MBytes 'Inc s -> PMArray 'Inc e s)
-> m (MBytes 'Inc s) -> m (PMArray 'Inc e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Count e -> m (MBytes 'Inc s)
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Count e -> m (MBytes 'Inc s)
allocUnpinnedMBytes (Size -> Count e
coerce Size
sz :: Count e)
{-# INLINE allocUnpinnedPMArray #-}

allocPinnedPMArray :: forall e m s . (MonadPrim s m, Prim e) => Size -> m (PMArray 'Pin e s)
allocPinnedPMArray :: Size -> m (PMArray 'Pin e s)
allocPinnedPMArray Size
sz = MBytes 'Pin s -> PMArray 'Pin e s
forall (p :: Pinned) s e. MBytes p s -> PMArray p e s
fromMBytesPMArray (MBytes 'Pin s -> PMArray 'Pin e s)
-> m (MBytes 'Pin s) -> m (PMArray 'Pin e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Count e -> m (MBytes 'Pin s)
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Count e -> m (MBytes 'Pin s)
allocPinnedMBytes (Size -> Count e
coerce Size
sz :: Count e)
{-# INLINE allocPinnedPMArray #-}

allocAlignedPMArray ::
     (MonadPrim s m, Prim e)
  => Count e -- ^ Size in number of bytes
  -> m (PMArray 'Pin e s)
allocAlignedPMArray :: Count e -> m (PMArray 'Pin e s)
allocAlignedPMArray = (MBytes 'Pin s -> PMArray 'Pin e s)
-> m (MBytes 'Pin s) -> m (PMArray 'Pin e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes 'Pin s -> PMArray 'Pin e s
forall (p :: Pinned) s e. MBytes p s -> PMArray p e s
fromMBytesPMArray (m (MBytes 'Pin s) -> m (PMArray 'Pin e s))
-> (Count e -> m (MBytes 'Pin s))
-> Count e
-> m (PMArray 'Pin e s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Count e -> m (MBytes 'Pin s)
forall e (m :: * -> *) s.
(MonadPrim s m, Prim e) =>
Count e -> m (MBytes 'Pin s)
allocAlignedMBytes
{-# INLINE allocAlignedPMArray #-}

freezePMArray :: MonadPrim s m => PMArray p e s -> m (PArray p e)
freezePMArray :: PMArray p e s -> m (PArray p e)
freezePMArray = (Bytes p -> PArray p e) -> m (Bytes p) -> m (PArray p e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bytes p -> PArray p e
forall (p :: Pinned) e. Bytes p -> PArray p e
fromBytesPArray (m (Bytes p) -> m (PArray p e))
-> (PMArray p e s -> m (Bytes p))
-> PMArray p e s
-> m (PArray p e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MBytes p s -> m (Bytes p)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes (MBytes p s -> m (Bytes p))
-> (PMArray p e s -> MBytes p s) -> PMArray p e s -> m (Bytes p)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PMArray p e s -> MBytes p s
forall (p :: Pinned) e s. PMArray p e s -> MBytes p s
toMBytesPMArray
{-# INLINE freezePMArray #-}

thawPArray :: MonadPrim s m => PArray p e -> m (PMArray p e s)
thawPArray :: PArray p e -> m (PMArray p e s)
thawPArray = (MBytes p s -> PMArray p e s)
-> m (MBytes p s) -> m (PMArray p e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes p s -> PMArray p e s
forall (p :: Pinned) s e. MBytes p s -> PMArray p e s
fromMBytesPMArray (m (MBytes p s) -> m (PMArray p e s))
-> (PArray p e -> m (MBytes p s))
-> PArray p e
-> m (PMArray p e s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bytes p -> m (MBytes p s)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
Bytes p -> m (MBytes p s)
thawBytes (Bytes p -> m (MBytes p s))
-> (PArray p e -> Bytes p) -> PArray p e -> m (MBytes p s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PArray p e -> Bytes p
forall (p :: Pinned) e. PArray p e -> Bytes p
toBytesPArray
{-# INLINE thawPArray #-}

-- | Shrink mutable bytes to new specified count of elements. The new count must be less
-- than or equal to the current count as reported by `getCountPMArray`.
shrinkPMArray ::
     forall e p m s. (MonadPrim s m, Prim e)
  => PMArray p e s
  -> Size
  -> m ()
shrinkPMArray :: PMArray p e s -> Size -> m ()
shrinkPMArray PMArray p e s
mba Size
sz = MBytes p s -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Count e -> m ()
shrinkMBytes (PMArray p e s -> MBytes p s
forall (p :: Pinned) e s. PMArray p e s -> MBytes p s
toMBytesPMArray PMArray p e s
mba) (Size -> Count e
coerce Size
sz :: Count e)
{-# INLINE shrinkPMArray #-}


-- | Attempt to resize mutable bytes in place.
--
-- * New bytes might be allocated, with the copy of an old one.
-- * Old references should not be kept around to allow GC to claim it
-- * Old references should not be used to avoid undefined behavior
resizePMArray ::
     forall e p m s. (MonadPrim s m, Prim e)
  => PMArray p e s
  -> Size
  -> m (PMArray 'Inc e s)
resizePMArray :: PMArray p e s -> Size -> m (PMArray 'Inc e s)
resizePMArray PMArray p e s
mba Size
sz =
  MBytes 'Inc s -> PMArray 'Inc e s
forall (p :: Pinned) s e. MBytes p s -> PMArray p e s
fromMBytesPMArray (MBytes 'Inc s -> PMArray 'Inc e s)
-> m (MBytes 'Inc s) -> m (PMArray 'Inc e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
  MBytes p s -> Count e -> m (MBytes 'Inc s)
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Count e -> m (MBytes 'Inc s)
resizeMBytes (PMArray p e s -> MBytes p s
forall (p :: Pinned) e s. PMArray p e s -> MBytes p s
toMBytesPMArray PMArray p e s
mba) (Size -> Count e
coerce Size
sz :: Count e)
{-# INLINE resizePMArray #-}

reallocPMArray ::
     forall e p m s. (MonadPrim s m, Typeable p,  Prim e)
  => PMArray p e s
  -> Size
  -> m (PMArray p e s)
reallocPMArray :: PMArray p e s -> Size -> m (PMArray p e s)
reallocPMArray PMArray p e s
mba Size
sz =
  MBytes p s -> PMArray p e s
forall (p :: Pinned) s e. MBytes p s -> PMArray p e s
fromMBytesPMArray (MBytes p s -> PMArray p e s)
-> m (MBytes p s) -> m (PMArray p e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
  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 (PMArray p e s -> MBytes p s
forall (p :: Pinned) e s. PMArray p e s -> MBytes p s
toMBytesPMArray PMArray p e s
mba) (Size -> Count e
coerce Size
sz :: Count e)
{-# INLINABLE reallocPMArray #-}


isPinnedPArray :: PArray p e -> Bool
isPinnedPArray :: PArray p e -> Bool
isPinnedPArray (PArray Bytes p
b) = Bytes p -> Bool
forall (p :: Pinned). Bytes p -> Bool
isPinnedBytes Bytes p
b
{-# INLINE isPinnedPArray #-}

isPinnedPMArray :: PMArray p e s -> Bool
isPinnedPMArray :: PMArray p e s -> Bool
isPinnedPMArray (PMArray MBytes p s
mb) = MBytes p s -> Bool
forall (p :: Pinned) d. MBytes p d -> Bool
isPinnedMBytes MBytes p s
mb
{-# INLINE isPinnedPMArray #-}

readPMArray :: (MonadPrim s m, Prim e) => PMArray p e s -> Int -> m e
readPMArray :: PMArray p e s -> Int -> m e
readPMArray (PMArray MBytes p s
mb) = 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 MBytes p s
mb (Off e -> m e) -> (Int -> Off e) -> Int -> m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Off e
coerce
{-# INLINE readPMArray #-}

writePMArray :: (MonadPrim s m, Prim e) => PMArray p e s -> Int -> e -> m ()
writePMArray :: PMArray p e s -> Int -> e -> m ()
writePMArray (PMArray MBytes p s
mb) Int
o = 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 MBytes p s
mb (Int -> Off e
coerce Int
o)
{-# INLINE writePMArray #-}



setPMArray ::
     forall e p m s. (MonadPrim s m, Prim e)
  => PMArray p e s -- ^ Chunk of memory to fill
  -> Int -- ^ Offset in number of elements
  -> Size -- ^ Number of cells to fill
  -> e -- ^ A value to fill the cells with
  -> m ()
setPMArray :: PMArray p e s -> Int -> Size -> e -> m ()
setPMArray (PMArray MBytes p s
mb) Int
off Size
sz = 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 MBytes p s
mb (Int -> Off e
coerce Int
off) (Size -> Count e
coerce Size
sz)
{-# INLINE setPMArray #-}

copyPArrayToPMArray ::
     forall e p m s. (MonadPrim s m, Prim e)
  => PArray p e
  -> Int
  -> PMArray p e s
  -> Int
  -> Size
  -> m ()
copyPArrayToPMArray :: PArray p e -> Int -> PMArray p e s -> Int -> Size -> m ()
copyPArrayToPMArray PArray p e
ba Int
srcOff PMArray p e s
mba Int
dstOff Size
sz =
  PArray p e -> Off e -> PMArray p e 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 PArray p e
ba (Int -> Off e
coerce Int
srcOff) PMArray p e s
mba (Int -> Off e
coerce Int
dstOff) (Size -> Count e
coerce Size
sz Count e -> PArray p e -> Count e
forall e (proxy :: * -> *). Count e -> proxy e -> Count e
`countForProxyTypeOf` PArray p e
ba)
{-# INLINE copyPArrayToPMArray #-}

movePMArrayToPMArray ::
     forall e p m s. (MonadPrim s m, Prim e)
  => PMArray p e s
  -> Int
  -> PMArray p e s
  -> Int
  -> Size
  -> m ()
movePMArrayToPMArray :: PMArray p e s -> Int -> PMArray p e s -> Int -> Size -> m ()
movePMArrayToPMArray PMArray p e s
ba Int
srcOff PMArray p e s
mba Int
dstOff Size
sz =
  PMArray p e s -> Off e -> PMArray p e s -> Off e -> Count e -> m ()
forall s (m :: * -> *) (mw1 :: * -> *) (mw2 :: * -> *) e.
(MonadPrim s m, MemWrite mw1, MemWrite mw2, Prim e) =>
mw1 s -> Off e -> mw2 s -> Off e -> Count e -> m ()
moveMutMem PMArray p e s
ba (Int -> Off e
coerce Int
srcOff) PMArray p e s
mba (Int -> Off e
coerce Int
dstOff) (Size -> Count e
coerce Size
sz :: Count e)
{-# INLINE movePMArrayToPMArray #-}



-- toPtrPArray :: PArray Pin e -> Ptr e
-- toPtrPArray (PArray ba#) = Ptr (byteArrayContents# ba#)
-- {-# INLINE toPtrPArray #-}

-- toPtrPMArray :: PMArray Pin e s -> Ptr e
-- toPtrPMArray (PMArray mba#) = Ptr (mutablePArrayContents# mba#)
-- {-# INLINE toPtrPMArray #-}

-- -- | Pointer access to immutable `PArray` should be for read only purposes, but it is
-- -- not enforced. Any mutation will break referential transparency
-- withPtrPArray :: MonadPrim s m => PArray Pin e -> (Ptr e -> m b) -> m b
-- withPtrPArray b f = do
--   res <- f (toPtrPArray b)
--   res <$ touch b
-- {-# INLINE withPtrPArray #-}

-- -- | Same as `withPtrPArray`, but is suitable for actions that don't terminate
-- withNoHaltPtrPArray :: MonadUnliftPrim s m => PArray Pin e -> (Ptr e -> m b) -> m b
-- withNoHaltPtrPArray b f = withAliveUnliftPrim b $ f (toPtrPArray b)
-- {-# INLINE withNoHaltPtrPArray #-}

-- withPtrPMArray :: MonadPrim s m => PMArray Pin e s -> (Ptr e -> m b) -> m b
-- withPtrPMArray mb f = do
--   res <- f (toPtrPMArray mb)
--   res <$ touch mb
-- {-# INLINE withPtrPMArray #-}

-- withNoHaltPtrPMArray :: MonadUnliftPrim s m => PMArray Pin e s -> (Ptr e -> m b) -> m b
-- withNoHaltPtrPMArray mb f = withAliveUnliftPrim mb $ f (toPtrPMArray mb)
-- {-# INLINE withNoHaltPtrPMArray #-}


-- -- -- | Check if two byte arrays refer to pinned memory and compare their pointers.
-- -- isSamePArray :: PArray p1 e -> PArray p2 e -> Bool
-- -- isSamePArray (PArray b1#) (PArray b2#) = isTrue# (isSameByteArray# b1# b2#)
-- -- {-# INLINE[0] isSamePArray #-}
-- -- {-# RULES
-- -- "isSamePinnedPArray" isSamePArray = isSamePinnedPArray
-- --   #-}

-- -- -- | Perform pointer equality on pinned `PArray`.
-- -- isSamePinnedPArray :: PArray Pin e -> PArray Pin e -> Bool
-- -- isSamePinnedPArray pb e1 pb2 = toPtrPArray pb e1 == toPtrPArray pb e2
-- -- {-# INLINE isSamePinnedPArray #-}



-- -- byteStringConvertError :: String -> a
-- -- byteStringConvertError msg = error $ "Cannot convert 'ByteString'. " ++ msg
-- -- {-# NOINLINE byteStringConvertError #-}