{-# Language CPP #-}
module System.Mem.Weak.ForeignPtr
( mkWeakForeignPtr
, mkWeakForeignPtrPtr
, mkWeakForeignPtrPair
) where
import GHC.ForeignPtr
import System.Mem.Weak
import System.Mem.Weak.IORef
mkWeakForeignPtr :: ForeignPtr k -> v -> Maybe (IO ()) -> IO (Weak v)
mkWeakForeignPtr :: ForeignPtr k -> v -> Maybe (IO ()) -> IO (Weak v)
mkWeakForeignPtr (ForeignPtr Addr#
_ ForeignPtrContents
k) v
v Maybe (IO ())
fin = IORef Finalizers -> v -> Maybe (IO ()) -> IO (Weak v)
forall k v. IORef k -> v -> Maybe (IO ()) -> IO (Weak v)
mkWeakIORef' (ForeignPtrContents -> IORef Finalizers
finalizers ForeignPtrContents
k) v
v Maybe (IO ())
fin where
finalizers :: ForeignPtrContents -> IORef Finalizers
finalizers PlainPtr{} = [Char] -> IORef Finalizers
forall a. HasCallStack => [Char] -> a
error [Char]
"mkWeakForeignPtr: PlainPtr encountered"
#if MIN_VERSION_base(4,15,0)
finalizers FinalPtr = error "mkWeakForeignPtr: FinalPtr encountered"
#endif
finalizers (MallocPtr MutableByteArray# RealWorld
_ IORef Finalizers
p) = IORef Finalizers
p
finalizers (PlainForeignPtr IORef Finalizers
p) = IORef Finalizers
p
{-# inlinable mkWeakForeignPtr #-}
mkWeakForeignPtrPtr :: ForeignPtr a -> Maybe (IO ()) -> IO (Weak (ForeignPtr a))
mkWeakForeignPtrPtr :: ForeignPtr a -> Maybe (IO ()) -> IO (Weak (ForeignPtr a))
mkWeakForeignPtrPtr ForeignPtr a
k = ForeignPtr a
-> ForeignPtr a -> Maybe (IO ()) -> IO (Weak (ForeignPtr a))
forall k v. ForeignPtr k -> v -> Maybe (IO ()) -> IO (Weak v)
mkWeakForeignPtr ForeignPtr a
k ForeignPtr a
k
{-# inlinable mkWeakForeignPtrPtr #-}
mkWeakForeignPtrPair :: ForeignPtr k -> v -> Maybe (IO ()) -> IO (Weak (ForeignPtr k, v))
mkWeakForeignPtrPair :: ForeignPtr k -> v -> Maybe (IO ()) -> IO (Weak (ForeignPtr k, v))
mkWeakForeignPtrPair ForeignPtr k
k v
v = ForeignPtr k
-> (ForeignPtr k, v)
-> Maybe (IO ())
-> IO (Weak (ForeignPtr k, v))
forall k v. ForeignPtr k -> v -> Maybe (IO ()) -> IO (Weak v)
mkWeakForeignPtr ForeignPtr k
k (ForeignPtr k
k,v
v)
{-# inlinable mkWeakForeignPtrPair #-}