{-# LANGUAGE MagicHash #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.Primitive.PVar.Unsafe
( PVar(..)
, rawPVar
, rawPinnedPVar
, rawAlignedPinnedPVar
, rawStorablePVar
, rawAlignedStorablePVar
, peekPrim
, pokePrim
, toPtrPVar
, unsafeToPtrPVar
, unsafeToForeignPtrPVar
, zeroPVar
, sizeOfPVar#
, alignmentPVar#
, setPVar#
, atomicModifyIntArray#
, atomicModifyIntArray_#
, copyFromByteArrayPVar
, copyFromMutableByteArrayPVar
, copyPVarToMutableByteArray
, isByteArrayPinned
, isMutableByteArrayPinned
, isByteArrayPinned#
, isMutableByteArrayPinned#
, showsType
, unI#
)
where
import Control.Monad.Primitive (MonadPrim, primitive_)
import Data.Primitive.PVar.Internal
import Data.Primitive.ByteArray (ByteArray(..), MutableByteArray(..))
import Data.Primitive.Types
import GHC.Exts as Exts
import GHC.ForeignPtr
import Data.Typeable
unsafeToForeignPtrPVar :: PVar a s -> ForeignPtr a
unsafeToForeignPtrPVar pvar@(PVar mba#) =
case unsafeToPtrPVar pvar of
Ptr addr# -> ForeignPtr addr# (PlainPtr (unsafeCoerce# mba#))
{-# INLINE unsafeToForeignPtrPVar #-}
toPtrPVar :: PVar a s -> Maybe (Ptr a)
toPtrPVar pvar
| isPinnedPVar pvar = Just $ unsafeToPtrPVar pvar
| otherwise = Nothing
{-# INLINE toPtrPVar #-}
setPVar# ::
(MonadPrim s m, Prim a)
=> PVar a s
-> Int#
-> m ()
setPVar# pvar@(PVar mba#) a# =
primitive_ (Exts.setByteArray# mba# 0# (sizeOfPVar# pvar) a#)
{-# INLINE setPVar# #-}
zeroPVar :: (MonadPrim s m, Prim a) => PVar a s -> m ()
zeroPVar pvar = setPVar# pvar 0#
{-# INLINE zeroPVar #-}
copyPVarToMutableByteArray ::
(MonadPrim s m, Prim a)
=> PVar a s
-> MutableByteArray s
-> Int
-> m ()
copyPVarToMutableByteArray pvar mba offset =
copyBytesPVarToMutableByteArray pvar mba (offset * sizeOfPVar pvar)
{-# INLINE copyPVarToMutableByteArray #-}
copyFromByteArrayPVar ::
(MonadPrim s m, Prim a)
=> ByteArray
-> Int
-> PVar a s
-> m ()
copyFromByteArrayPVar ba offset pvar =
copyBytesFromByteArrayPVar ba (offset * sizeOfPVar pvar) pvar
{-# INLINE copyFromByteArrayPVar #-}
copyFromMutableByteArrayPVar ::
(MonadPrim s m, Prim a)
=> MutableByteArray s
-> Int
-> PVar a s
-> m ()
copyFromMutableByteArrayPVar mba offset pvar =
copyBytesFromMutableByteArrayPVar mba (offset * sizeOfPVar pvar) pvar
{-# INLINE copyFromMutableByteArrayPVar #-}
copyBytesPVarToMutableByteArray ::
(MonadPrim s m, Prim a)
=> PVar a s
-> MutableByteArray s
-> Int
-> m ()
copyBytesPVarToMutableByteArray pvar@(PVar mbas#) (MutableByteArray mbad#) (I# offset#) =
primitive_ (copyMutableByteArray# mbas# 0# mbad# offset# (sizeOfPVar# pvar))
{-# INLINE copyBytesPVarToMutableByteArray #-}
copyBytesFromByteArrayPVar ::
(MonadPrim s m, Prim a)
=> ByteArray
-> Int
-> PVar a s
-> m ()
copyBytesFromByteArrayPVar (ByteArray ba#) (I# offset#) pvar@(PVar mba#) =
primitive_ (copyByteArray# ba# offset# mba# 0# (sizeOfPVar# pvar))
{-# INLINE copyBytesFromByteArrayPVar #-}
copyBytesFromMutableByteArrayPVar ::
(MonadPrim s m, Prim a)
=> MutableByteArray s
-> Int
-> PVar a s
-> m ()
copyBytesFromMutableByteArrayPVar (MutableByteArray mbas#) (I# offset#) pvar@(PVar mbad#) =
primitive_ (copyMutableByteArray# mbas# offset# mbad# 0# (sizeOfPVar# pvar))
{-# INLINE copyBytesFromMutableByteArrayPVar #-}
showsType :: Typeable t => proxy t -> ShowS
showsType = showsTypeRep . typeRep
isByteArrayPinned :: ByteArray -> Bool
isByteArrayPinned (ByteArray arr#) = isTrue# (isByteArrayPinned# arr#)
{-# INLINE isByteArrayPinned #-}
isMutableByteArrayPinned :: MutableByteArray s -> Bool
isMutableByteArrayPinned (MutableByteArray marr#) = isTrue# (isMutableByteArrayPinned# marr#)
{-# INLINE isMutableByteArrayPinned #-}