{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_HADDOCK hide #-}
module Foreign.Marshal.Pure.Internal where
import Control.Exception
import qualified Data.Functor.Linear as Data
import Data.Kind (Constraint, Type)
import Data.Word (Word8)
import Foreign.Marshal.Alloc
import Foreign.Marshal.Utils
import Foreign.Ptr
import Foreign.Storable
import Foreign.Storable.Tuple ()
import Prelude.Linear hiding (Eq (..), ($))
import System.IO.Unsafe
import qualified Unsafe.Linear as Unsafe
import Prelude (Eq (..), return, ($), (<$>), (<*>), (=<<))
data Dict :: Constraint -> Type where
Dict :: (c) => Dict c
class KnownRepresentable a where
storable :: Dict (Storable a)
default storable :: (Storable a) => Dict (Storable a)
storable = forall (c :: Constraint). c => Dict c
Dict
instance KnownRepresentable Word
instance KnownRepresentable Int
instance KnownRepresentable (Ptr a)
instance KnownRepresentable ()
instance
(KnownRepresentable a, KnownRepresentable b) =>
KnownRepresentable (a, b)
where
storable :: Dict (Storable (a, b))
storable =
case (forall a. KnownRepresentable a => Dict (Storable a)
storable @a, forall a. KnownRepresentable a => Dict (Storable a)
storable @b) of
(Dict (Storable a)
Dict, Dict (Storable b)
Dict) -> forall (c :: Constraint). c => Dict c
Dict
instance
(KnownRepresentable a, KnownRepresentable b, KnownRepresentable c) =>
KnownRepresentable (a, b, c)
where
storable :: Dict (Storable (a, b, c))
storable =
case (forall a. KnownRepresentable a => Dict (Storable a)
storable @a, forall a. KnownRepresentable a => Dict (Storable a)
storable @b, forall a. KnownRepresentable a => Dict (Storable a)
storable @c) of
(Dict (Storable a)
Dict, Dict (Storable b)
Dict, Dict (Storable c)
Dict) -> forall (c :: Constraint). c => Dict c
Dict
instance (Storable a) => Storable (Ur a) where
sizeOf :: Ur a -> Int
sizeOf Ur a
_ = forall a. Storable a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined :: a)
alignment :: Ur a -> Int
alignment Ur a
_ = forall a. Storable a => a -> Int
alignment (forall a. HasCallStack => a
undefined :: a)
peek :: Ptr (Ur a) -> IO (Ur a)
peek Ptr (Ur a)
ptr = forall a. a -> Ur a
Ur forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Storable a => Ptr a -> IO a
peek (forall a b. Ptr a -> Ptr b
castPtr Ptr (Ur a)
ptr :: Ptr a)
poke :: Ptr (Ur a) -> Ur a -> IO ()
poke Ptr (Ur a)
ptr (Ur a
a) = forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a b. Ptr a -> Ptr b
castPtr Ptr (Ur a)
ptr :: Ptr a) a
a
instance (KnownRepresentable a) => KnownRepresentable (Ur a) where
storable :: Dict (Storable (Ur a))
storable | Dict (Storable a)
Dict <- forall a. KnownRepresentable a => Dict (Storable a)
storable @a = forall (c :: Constraint). c => Dict c
Dict
instance (Storable a) => Storable (Maybe a) where
sizeOf :: Maybe a -> Int
sizeOf Maybe a
x = forall a. Storable a => a -> Int
sizeOf (forall a. Maybe a -> a
stripMaybe Maybe a
x) forall a. Additive a => a %1 -> a %1 -> a
+ Int
1
alignment :: Maybe a -> Int
alignment Maybe a
x = forall a. Storable a => a -> Int
alignment (forall a. Maybe a -> a
stripMaybe Maybe a
x)
peek :: Ptr (Maybe a) -> IO (Maybe a)
peek Ptr (Maybe a)
ptr = do
Word8
filled <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr (Maybe a)
ptr forall a b. (a -> b) -> a -> b
$ forall a. Storable a => a -> Int
sizeOf forall a b. (a -> b) -> a -> b
$ forall a. Maybe a -> a
stripMaybe forall a b. (a -> b) -> a -> b
$ forall a. Ptr a -> a
stripPtr Ptr (Maybe a)
ptr
case Word8
filled forall a. Eq a => a -> a -> Bool
== (Word8
1 :: Word8) of
Bool
True -> do
a
x <- forall a. Storable a => Ptr a -> IO a
peek (forall a. Ptr (Maybe a) -> Ptr a
stripMaybePtr Ptr (Maybe a)
ptr)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just a
x)
Bool
False ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
poke :: Ptr (Maybe a) -> Maybe a -> IO ()
poke Ptr (Maybe a)
ptr Maybe a
Nothing = forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr (Maybe a)
ptr (forall a. Storable a => a -> Int
sizeOf forall a b. (a -> b) -> a -> b
$ forall a. Maybe a -> a
stripMaybe forall a b. (a -> b) -> a -> b
$ forall a. Ptr a -> a
stripPtr Ptr (Maybe a)
ptr) (Word8
0 :: Word8)
poke Ptr (Maybe a)
ptr (Just a
a) = do
forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a. Ptr (Maybe a) -> Ptr a
stripMaybePtr Ptr (Maybe a)
ptr) a
a
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr (Maybe a)
ptr (forall a. Storable a => a -> Int
sizeOf a
a) (Word8
1 :: Word8)
stripMaybe :: Maybe a -> a
stripMaybe :: forall a. Maybe a -> a
stripMaybe Maybe a
_ = forall a. HasCallStack => [Char] -> a
error [Char]
"stripMaybe"
stripMaybePtr :: Ptr (Maybe a) -> Ptr a
stripMaybePtr :: forall a. Ptr (Maybe a) -> Ptr a
stripMaybePtr = forall a b. Ptr a -> Ptr b
castPtr
stripPtr :: Ptr a -> a
stripPtr :: forall a. Ptr a -> a
stripPtr Ptr a
_ = forall a. HasCallStack => [Char] -> a
error [Char]
"stripPtr"
instance (KnownRepresentable a) => KnownRepresentable (Maybe a) where
storable :: Dict (Storable (Maybe a))
storable | Dict (Storable a)
Dict <- forall a. KnownRepresentable a => Dict (Storable a)
storable @a = forall (c :: Constraint). c => Dict c
Dict
class (KnownRepresentable (AsKnown a)) => Representable a where
type AsKnown a :: Type
toKnown :: a %1 -> AsKnown a
ofKnown :: AsKnown a %1 -> a
default toKnown ::
(MkRepresentable a b, AsKnown a ~ AsKnown b) => a %1 -> AsKnown a
default ofKnown ::
(MkRepresentable a b, AsKnown a ~ AsKnown b) => AsKnown a %1 -> a
toKnown a
a = forall a. Representable a => a %1 -> AsKnown a
toKnown (forall a b. MkRepresentable a b => a %1 -> b
toRepr a
a)
ofKnown AsKnown a
b = forall a b. MkRepresentable a b => b %1 -> a
ofRepr (forall a. Representable a => AsKnown a %1 -> a
ofKnown AsKnown a
b)
instance Representable Word where
type AsKnown Word = Word
toKnown :: Word %1 -> AsKnown Word
toKnown = forall a (q :: Multiplicity). a %q -> a
id
ofKnown :: AsKnown Word %1 -> Word
ofKnown = forall a (q :: Multiplicity). a %q -> a
id
instance Representable Int where
type AsKnown Int = Int
toKnown :: Int %1 -> AsKnown Int
toKnown = forall a (q :: Multiplicity). a %q -> a
id
ofKnown :: AsKnown Int %1 -> Int
ofKnown = forall a (q :: Multiplicity). a %q -> a
id
instance Representable (Ptr a) where
type AsKnown (Ptr a) = Ptr a
toKnown :: Ptr a %1 -> AsKnown (Ptr a)
toKnown = forall a (q :: Multiplicity). a %q -> a
id
ofKnown :: AsKnown (Ptr a) %1 -> Ptr a
ofKnown = forall a (q :: Multiplicity). a %q -> a
id
instance Representable () where
type AsKnown () = ()
toKnown :: () %1 -> AsKnown ()
toKnown = forall a (q :: Multiplicity). a %q -> a
id
ofKnown :: AsKnown () %1 -> ()
ofKnown = forall a (q :: Multiplicity). a %q -> a
id
instance
(Representable a, Representable b) =>
Representable (a, b)
where
type AsKnown (a, b) = (AsKnown a, AsKnown b)
toKnown :: (a, b) %1 -> AsKnown (a, b)
toKnown (a
a, b
b) = (forall a. Representable a => a %1 -> AsKnown a
toKnown a
a, forall a. Representable a => a %1 -> AsKnown a
toKnown b
b)
ofKnown :: AsKnown (a, b) %1 -> (a, b)
ofKnown (AsKnown a
x, AsKnown b
y) = (forall a. Representable a => AsKnown a %1 -> a
ofKnown AsKnown a
x, forall a. Representable a => AsKnown a %1 -> a
ofKnown AsKnown b
y)
instance
(Representable a, Representable b, Representable c) =>
Representable (a, b, c)
where
type AsKnown (a, b, c) = (AsKnown a, AsKnown b, AsKnown c)
toKnown :: (a, b, c) %1 -> AsKnown (a, b, c)
toKnown (a
a, b
b, c
c) = (forall a. Representable a => a %1 -> AsKnown a
toKnown a
a, forall a. Representable a => a %1 -> AsKnown a
toKnown b
b, forall a. Representable a => a %1 -> AsKnown a
toKnown c
c)
ofKnown :: AsKnown (a, b, c) %1 -> (a, b, c)
ofKnown (AsKnown a
x, AsKnown b
y, AsKnown c
z) = (forall a. Representable a => AsKnown a %1 -> a
ofKnown AsKnown a
x, forall a. Representable a => AsKnown a %1 -> a
ofKnown AsKnown b
y, forall a. Representable a => AsKnown a %1 -> a
ofKnown AsKnown c
z)
instance (Representable a) => Representable (Maybe a) where
type AsKnown (Maybe a) = Maybe (AsKnown a)
toKnown :: Maybe a %1 -> AsKnown (Maybe a)
toKnown (Just a
x) = forall a. a -> Maybe a
Just (forall a. Representable a => a %1 -> AsKnown a
toKnown a
x)
toKnown Maybe a
Nothing = forall a. Maybe a
Nothing
ofKnown :: AsKnown (Maybe a) %1 -> Maybe a
ofKnown (Just AsKnown a
x) = forall a. a -> Maybe a
Just (forall a. Representable a => AsKnown a %1 -> a
ofKnown AsKnown a
x)
ofKnown Maybe (AsKnown a)
AsKnown (Maybe a)
Nothing = forall a. Maybe a
Nothing
class (Representable b) => MkRepresentable a b | a -> b where
toRepr :: a %1 -> b
ofRepr :: b %1 -> a
data Pool where
Pool :: DLL (Ptr ()) -> Pool
data DLL a = DLL {forall a. DLL a -> Ptr (DLL a)
prev :: Ptr (DLL a), forall a. DLL a -> Ptr a
elt :: Ptr a, forall a. DLL a -> Ptr (DLL a)
next :: Ptr (DLL a)}
deriving (DLL a -> DLL a -> Bool
forall a. DLL a -> DLL a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DLL a -> DLL a -> Bool
$c/= :: forall a. DLL a -> DLL a -> Bool
== :: DLL a -> DLL a -> Bool
$c== :: forall a. DLL a -> DLL a -> Bool
Eq)
instance Storable (DLL a) where
sizeOf :: DLL a -> Int
sizeOf DLL a
_ = forall a. Storable a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined :: (Ptr (DLL a), Ptr a, Ptr (DLL a)))
alignment :: DLL a -> Int
alignment DLL a
_ = forall a. Storable a => a -> Int
alignment (forall a. HasCallStack => a
undefined :: (Ptr (DLL a), Ptr a, Ptr (DLL a)))
peek :: Ptr (DLL a) -> IO (DLL a)
peek Ptr (DLL a)
ptr = do
(Ptr (DLL a)
p, Ptr a
e, Ptr (DLL a)
n) <- forall a. Storable a => Ptr a -> IO a
peek (forall a b. Ptr a -> Ptr b
castPtr Ptr (DLL a)
ptr :: Ptr (Ptr (DLL a), Ptr a, Ptr (DLL a)))
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Ptr (DLL a) -> Ptr a -> Ptr (DLL a) -> DLL a
DLL Ptr (DLL a)
p Ptr a
e Ptr (DLL a)
n
poke :: Ptr (DLL a) -> DLL a -> IO ()
poke Ptr (DLL a)
ptr (DLL Ptr (DLL a)
p Ptr a
e Ptr (DLL a)
n) =
forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a b. Ptr a -> Ptr b
castPtr Ptr (DLL a)
ptr :: Ptr (Ptr (DLL a), Ptr a, Ptr (DLL a))) (Ptr (DLL a)
p, Ptr a
e, Ptr (DLL a)
n)
insertAfter :: (Storable a) => DLL a -> a -> IO (Ptr (DLL a))
insertAfter :: forall a. Storable a => DLL a -> a -> IO (Ptr (DLL a))
insertAfter DLL a
start a
ptr = do
DLL a
secondLink <- forall a. Storable a => Ptr a -> IO a
peek forall a b. (a -> b) -> a -> b
$ forall a. DLL a -> Ptr (DLL a)
next DLL a
start
DLL a
newLink <- forall a. Ptr (DLL a) -> Ptr a -> Ptr (DLL a) -> DLL a
DLL forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Storable a => a -> IO (Ptr a)
new DLL a
start forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Storable a => a -> IO (Ptr a)
new a
ptr forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Storable a => a -> IO (Ptr a)
new DLL a
secondLink
forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a. DLL a -> Ptr (DLL a)
next DLL a
start) DLL a
newLink
forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a. DLL a -> Ptr (DLL a)
prev DLL a
secondLink) DLL a
newLink
forall a. Storable a => a -> IO (Ptr a)
new DLL a
newLink
delete :: DLL a -> IO ()
delete :: forall a. DLL a -> IO ()
delete DLL a
link = do
DLL a
prevLink <- forall a. Storable a => Ptr a -> IO a
peek forall a b. (a -> b) -> a -> b
$ forall a. DLL a -> Ptr (DLL a)
prev DLL a
link
DLL a
nextLink <- forall a. Storable a => Ptr a -> IO a
peek forall a b. (a -> b) -> a -> b
$ forall a. DLL a -> Ptr (DLL a)
next DLL a
link
forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a. DLL a -> Ptr (DLL a)
next DLL a
prevLink) DLL a
nextLink
forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a. DLL a -> Ptr (DLL a)
prev DLL a
nextLink) DLL a
prevLink
freeAll :: DLL (Ptr ()) -> DLL (Ptr ()) -> IO ()
freeAll :: DLL (Ptr ()) -> DLL (Ptr ()) -> IO ()
freeAll DLL (Ptr ())
start DLL (Ptr ())
end = do
DLL (Ptr ())
nextLink <- forall a. Storable a => Ptr a -> IO a
peek (forall a. DLL a -> Ptr (DLL a)
next DLL (Ptr ())
start)
if DLL (Ptr ())
nextLink forall a. Eq a => a -> a -> Bool
== DLL (Ptr ())
end
then do
forall a. Ptr a -> IO ()
free (forall a. DLL a -> Ptr (DLL a)
next DLL (Ptr ())
start)
forall a. Ptr a -> IO ()
free (forall a. DLL a -> Ptr (DLL a)
prev DLL (Ptr ())
end)
else do
forall a. DLL a -> IO ()
delete DLL (Ptr ())
nextLink
forall a. Ptr a -> IO ()
free (forall a. DLL a -> Ptr (DLL a)
prev DLL (Ptr ())
nextLink)
forall a. Ptr a -> IO ()
free (forall a. DLL a -> Ptr a
elt DLL (Ptr ())
nextLink)
forall a. Ptr a -> IO ()
free (forall a. DLL a -> Ptr (DLL a)
next DLL (Ptr ())
nextLink)
DLL (Ptr ()) -> DLL (Ptr ()) -> IO ()
freeAll DLL (Ptr ())
start DLL (Ptr ())
end
withPool :: (Pool %1 -> Ur b) %1 -> Ur b
withPool :: forall b. (Pool %1 -> Ur b) %1 -> Ur b
withPool Pool %1 -> Ur b
scope = forall a b (p :: Multiplicity) (x :: Multiplicity).
(a %p -> b) %1 -> a %x -> b
Unsafe.toLinear forall b. (Pool %1 -> Ur b) -> Ur b
performScope Pool %1 -> Ur b
scope
where
performScope :: (Pool %1 -> Ur b) -> Ur b
performScope :: forall b. (Pool %1 -> Ur b) -> Ur b
performScope Pool %1 -> Ur b
scope' = forall a. IO a -> a
unsafeDupablePerformIO forall a b. (a -> b) -> a -> b
$ do
Ptr (DLL (Ptr ()))
backPtr <- forall a. Storable a => IO (Ptr a)
malloc
let end :: DLL (Ptr ())
end = forall a. Ptr (DLL a) -> Ptr a -> Ptr (DLL a) -> DLL a
DLL Ptr (DLL (Ptr ()))
backPtr forall a. Ptr a
nullPtr forall a. Ptr a
nullPtr
DLL (Ptr ())
start <- forall a. Ptr (DLL a) -> Ptr a -> Ptr (DLL a) -> DLL a
DLL forall a. Ptr a
nullPtr forall a. Ptr a
nullPtr forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Storable a => a -> IO (Ptr a)
new DLL (Ptr ())
end
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (DLL (Ptr ()))
backPtr DLL (Ptr ())
start
forall a. a -> IO a
evaluate (Pool %1 -> Ur b
scope' (DLL (Ptr ()) -> Pool
Pool DLL (Ptr ())
start))
forall a b. IO a -> IO b -> IO a
`finally`
(DLL (Ptr ()) -> DLL (Ptr ()) -> IO ()
freeAll DLL (Ptr ())
start DLL (Ptr ())
end)
instance Consumable Pool where
consume :: Pool %1 -> ()
consume (Pool DLL (Ptr ())
_) = ()
instance Dupable Pool where
dupR :: Pool %1 -> Replicator Pool
dupR (Pool DLL (Ptr ())
l) = forall (f :: * -> *) a. Applicative f => a -> f a
Data.pure (DLL (Ptr ()) -> Pool
Pool DLL (Ptr ())
l)
data Box a where
Box :: Ptr (DLL (Ptr ())) -> Ptr a -> Box a
instance Storable (Box a) where
sizeOf :: Box a -> Int
sizeOf Box a
_ = forall a. Storable a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined :: (Ptr (DLL (Ptr ())), Ptr a))
alignment :: Box a -> Int
alignment Box a
_ = forall a. Storable a => a -> Int
alignment (forall a. HasCallStack => a
undefined :: (Ptr (DLL (Ptr ())), Ptr a))
peek :: Ptr (Box a) -> IO (Box a)
peek Ptr (Box a)
ptr = do
(Ptr (DLL (Ptr ()))
pool, Ptr a
ptr') <- forall a. Storable a => Ptr a -> IO a
peek (forall a b. Ptr a -> Ptr b
castPtr Ptr (Box a)
ptr :: Ptr (Ptr (DLL (Ptr ())), Ptr a))
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ptr (DLL (Ptr ())) -> Ptr a -> Box a
Box Ptr (DLL (Ptr ()))
pool Ptr a
ptr')
poke :: Ptr (Box a) -> Box a -> IO ()
poke Ptr (Box a)
ptr (Box Ptr (DLL (Ptr ()))
pool Ptr a
ptr') =
forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a b. Ptr a -> Ptr b
castPtr Ptr (Box a)
ptr :: Ptr (Ptr (DLL (Ptr ())), Ptr a)) (Ptr (DLL (Ptr ()))
pool, Ptr a
ptr')
instance KnownRepresentable (Box a)
instance Representable (Box a) where
type AsKnown (Box a) = Box a
ofKnown :: AsKnown (Box a) %1 -> Box a
ofKnown = forall a (q :: Multiplicity). a %q -> a
id
toKnown :: Box a %1 -> AsKnown (Box a)
toKnown = forall a (q :: Multiplicity). a %q -> a
id
reprPoke :: forall a. (Representable a) => Ptr a -> a %1 -> IO ()
reprPoke :: forall a. Representable a => Ptr a -> a %1 -> IO ()
reprPoke Ptr a
ptr a
a
| Dict (Storable (AsKnown a))
Dict <- forall a. KnownRepresentable a => Dict (Storable a)
storable @(AsKnown a) =
forall a b (p :: Multiplicity) (x :: Multiplicity).
(a %p -> b) %1 -> a %x -> b
Unsafe.toLinear (forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr :: Ptr (AsKnown a))) (forall a. Representable a => a %1 -> AsKnown a
toKnown a
a)
reprNew :: forall a. (Representable a) => a %1 -> IO (Ptr a)
reprNew :: forall a. Representable a => a %1 -> IO (Ptr a)
reprNew a
a =
forall a b (p :: Multiplicity) (x :: Multiplicity).
(a %p -> b) %1 -> a %x -> b
Unsafe.toLinear a -> IO (Ptr a)
mkPtr a
a
where
mkPtr :: a -> IO (Ptr a)
mkPtr :: a -> IO (Ptr a)
mkPtr a
a' | Dict (Storable (AsKnown a))
Dict <- forall a. KnownRepresentable a => Dict (Storable a)
storable @(AsKnown a) =
do
Ptr (AsKnown a)
ptr0 <- forall a. Storable a => IO (Ptr a)
malloc @(AsKnown a)
let ptr :: Ptr a
ptr = forall a b. Ptr a -> Ptr b
castPtr Ptr (AsKnown a)
ptr0 :: Ptr a
forall a. Representable a => Ptr a -> a %1 -> IO ()
reprPoke Ptr a
ptr a
a'
forall (m :: * -> *) a. Monad m => a -> m a
return Ptr a
ptr
alloc :: forall a. (Representable a) => a %1 -> Pool %1 -> Box a
alloc :: forall a. Representable a => a %1 -> Pool %1 -> Box a
alloc a
a (Pool DLL (Ptr ())
pool) =
forall a b (p :: Multiplicity) (x :: Multiplicity).
(a %p -> b) %1 -> a %x -> b
Unsafe.toLinear a -> Box a
mkPtr a
a
where
mkPtr :: a -> Box a
mkPtr :: a -> Box a
mkPtr a
a' = forall a. IO a -> a
unsafeDupablePerformIO forall a b. (a -> b) -> a -> b
$ do
Ptr a
ptr <- forall a. Representable a => a %1 -> IO (Ptr a)
reprNew a
a'
Ptr (DLL (Ptr ()))
poolPtr <- forall a. Storable a => DLL a -> a -> IO (Ptr (DLL a))
insertAfter DLL (Ptr ())
pool (forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr :: Ptr ())
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ptr (DLL (Ptr ())) -> Ptr a -> Box a
Box Ptr (DLL (Ptr ()))
poolPtr Ptr a
ptr)
reprPeek :: forall a. (Representable a) => Ptr a -> IO a
reprPeek :: forall a. Representable a => Ptr a -> IO a
reprPeek Ptr a
ptr | Dict (Storable (AsKnown a))
Dict <- forall a. KnownRepresentable a => Dict (Storable a)
storable @(AsKnown a) = do
AsKnown a
knownRepr <- forall a. Storable a => Ptr a -> IO a
peek (forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr :: Ptr (AsKnown a))
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Representable a => AsKnown a %1 -> a
ofKnown AsKnown a
knownRepr)
deconstruct :: (Representable a) => Box a %1 -> a
deconstruct :: forall a. Representable a => Box a %1 -> a
deconstruct (Box Ptr (DLL (Ptr ()))
poolPtr Ptr a
ptr) = forall a. IO a -> a
unsafeDupablePerformIO forall a b. (a -> b) -> a -> b
$
forall a. IO a -> IO a
mask_ forall a b. (a -> b) -> a -> b
$ do
a
res <- forall a. Representable a => Ptr a -> IO a
reprPeek Ptr a
ptr
forall a. DLL a -> IO ()
delete forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. Storable a => Ptr a -> IO a
peek Ptr (DLL (Ptr ()))
poolPtr
forall a. Ptr a -> IO ()
free Ptr a
ptr
forall a. Ptr a -> IO ()
free Ptr (DLL (Ptr ()))
poolPtr
forall (m :: * -> *) a. Monad m => a -> m a
return a
res