{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, RoleAnnotations #-}
{-# OPTIONS_HADDOCK not-home #-}
module Data.Array.Storable.Internals (
StorableArray(..),
withStorableArray,
touchStorableArray,
unsafeForeignPtrToStorableArray,
) where
import Data.Array.Base
import Data.Array.MArray
import Foreign hiding (newArray)
data StorableArray i e = StorableArray !i !i Int !(ForeignPtr e)
type role StorableArray nominal nominal
instance Storable e => MArray StorableArray e IO where
getBounds :: forall i. Ix i => StorableArray i e -> IO (i, i)
getBounds (StorableArray i
l i
u Int
_ ForeignPtr e
_) = forall (m :: * -> *) a. Monad m => a -> m a
return (i
l,i
u)
getNumElements :: forall i. Ix i => StorableArray i e -> IO Int
getNumElements (StorableArray i
_l i
_u Int
n ForeignPtr e
_) = forall (m :: * -> *) a. Monad m => a -> m a
return Int
n
newArray :: forall i. Ix i => (i, i) -> e -> IO (StorableArray i e)
newArray (i
l,i
u) e
initialValue = do
ForeignPtr e
fp <- forall a. Storable a => Int -> IO (ForeignPtr a)
mallocForeignPtrArray Int
size
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr e
fp forall a b. (a -> b) -> a -> b
$ \Ptr e
a ->
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ [forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr e
a Int
i e
initialValue | Int
i <- [Int
0..Int
sizeforall a. Num a => a -> a -> a
-Int
1]]
forall (m :: * -> *) a. Monad m => a -> m a
return (forall i e. i -> i -> Int -> ForeignPtr e -> StorableArray i e
StorableArray i
l i
u Int
size ForeignPtr e
fp)
where
size :: Int
size = forall a. Ix a => (a, a) -> Int
rangeSize (i
l,i
u)
unsafeNewArray_ :: forall i. Ix i => (i, i) -> IO (StorableArray i e)
unsafeNewArray_ (i
l,i
u) = do
let n :: Int
n = forall a. Ix a => (a, a) -> Int
rangeSize (i
l,i
u)
ForeignPtr e
fp <- forall a. Storable a => Int -> IO (ForeignPtr a)
mallocForeignPtrArray Int
n
forall (m :: * -> *) a. Monad m => a -> m a
return (forall i e. i -> i -> Int -> ForeignPtr e -> StorableArray i e
StorableArray i
l i
u Int
n ForeignPtr e
fp)
newArray_ :: forall i. Ix i => (i, i) -> IO (StorableArray i e)
newArray_ = forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
unsafeNewArray_
unsafeRead :: forall i. Ix i => StorableArray i e -> Int -> IO e
unsafeRead (StorableArray i
_ i
_ Int
_ ForeignPtr e
fp) Int
i =
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr e
fp forall a b. (a -> b) -> a -> b
$ \Ptr e
a -> forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr e
a Int
i
unsafeWrite :: forall i. Ix i => StorableArray i e -> Int -> e -> IO ()
unsafeWrite (StorableArray i
_ i
_ Int
_ ForeignPtr e
fp) Int
i e
e =
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr e
fp forall a b. (a -> b) -> a -> b
$ \Ptr e
a -> forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr e
a Int
i e
e
withStorableArray :: StorableArray i e -> (Ptr e -> IO a) -> IO a
withStorableArray :: forall i e a. StorableArray i e -> (Ptr e -> IO a) -> IO a
withStorableArray (StorableArray i
_ i
_ Int
_ ForeignPtr e
fp) Ptr e -> IO a
f = forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr e
fp Ptr e -> IO a
f
touchStorableArray :: StorableArray i e -> IO ()
touchStorableArray :: forall i e. StorableArray i e -> IO ()
touchStorableArray (StorableArray i
_ i
_ Int
_ ForeignPtr e
fp) = forall a. ForeignPtr a -> IO ()
touchForeignPtr ForeignPtr e
fp
unsafeForeignPtrToStorableArray
:: Ix i => ForeignPtr e -> (i,i) -> IO (StorableArray i e)
unsafeForeignPtrToStorableArray :: forall i e.
Ix i =>
ForeignPtr e -> (i, i) -> IO (StorableArray i e)
unsafeForeignPtrToStorableArray ForeignPtr e
p (i
l,i
u) =
forall (m :: * -> *) a. Monad m => a -> m a
return (forall i e. i -> i -> Int -> ForeignPtr e -> StorableArray i e
StorableArray i
l i
u (forall a. Ix a => (a, a) -> Int
rangeSize (i
l,i
u)) ForeignPtr e
p)