{-# LANGUAGE Unsafe #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_HADDOCK not-home #-}
module Foreign.ForeignPtr.Imp
(
ForeignPtr
, FinalizerPtr
, FinalizerEnvPtr
, newForeignPtr
, newForeignPtr_
, addForeignPtrFinalizer
, newForeignPtrEnv
, addForeignPtrFinalizerEnv
, withForeignPtr
, finalizeForeignPtr
, unsafeForeignPtrToPtr
, touchForeignPtr
, castForeignPtr
, plusForeignPtr
, mallocForeignPtr
, mallocForeignPtrBytes
, mallocForeignPtrArray
, mallocForeignPtrArray0
)
where
import Foreign.Ptr
import Foreign.Storable ( Storable(sizeOf) )
import GHC.Base
import GHC.Num
import GHC.ForeignPtr
newForeignPtr :: FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr :: forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr a
finalizer Ptr a
p
= do ForeignPtr a
fObj <- Ptr a -> IO (ForeignPtr a)
forall a. Ptr a -> IO (ForeignPtr a)
newForeignPtr_ Ptr a
p
FinalizerPtr a -> ForeignPtr a -> IO ()
forall a. FinalizerPtr a -> ForeignPtr a -> IO ()
addForeignPtrFinalizer FinalizerPtr a
finalizer ForeignPtr a
fObj
ForeignPtr a -> IO (ForeignPtr a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ForeignPtr a
fObj
newForeignPtrEnv ::
FinalizerEnvPtr env a -> Ptr env -> Ptr a -> IO (ForeignPtr a)
newForeignPtrEnv :: forall env a.
FinalizerEnvPtr env a -> Ptr env -> Ptr a -> IO (ForeignPtr a)
newForeignPtrEnv FinalizerEnvPtr env a
finalizer Ptr env
env Ptr a
p
= do ForeignPtr a
fObj <- Ptr a -> IO (ForeignPtr a)
forall a. Ptr a -> IO (ForeignPtr a)
newForeignPtr_ Ptr a
p
FinalizerEnvPtr env a -> Ptr env -> ForeignPtr a -> IO ()
forall env a.
FinalizerEnvPtr env a -> Ptr env -> ForeignPtr a -> IO ()
addForeignPtrFinalizerEnv FinalizerEnvPtr env a
finalizer Ptr env
env ForeignPtr a
fObj
ForeignPtr a -> IO (ForeignPtr a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ForeignPtr a
fObj
mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
mallocForeignPtrArray :: forall a. Storable a => Int -> IO (ForeignPtr a)
mallocForeignPtrArray = a -> Int -> IO (ForeignPtr a)
forall b. Storable b => b -> Int -> IO (ForeignPtr b)
doMalloc a
forall a. HasCallStack => a
undefined
where
doMalloc :: Storable b => b -> Int -> IO (ForeignPtr b)
doMalloc :: forall b. Storable b => b -> Int -> IO (ForeignPtr b)
doMalloc b
dummy Int
size = Int -> IO (ForeignPtr b)
forall a. Int -> IO (ForeignPtr a)
mallocForeignPtrBytes (Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
* b -> Int
forall a. Storable a => a -> Int
sizeOf b
dummy)
mallocForeignPtrArray0 :: Storable a => Int -> IO (ForeignPtr a)
mallocForeignPtrArray0 :: forall a. Storable a => Int -> IO (ForeignPtr a)
mallocForeignPtrArray0 Int
size = Int -> IO (ForeignPtr a)
forall a. Storable a => Int -> IO (ForeignPtr a)
mallocForeignPtrArray (Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)