{-# LANGUAGE Unsafe #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_HADDOCK not-home #-}
module GHC.Internal.Foreign.ForeignPtr.Imp
(
ForeignPtr
, FinalizerPtr
, FinalizerEnvPtr
, newForeignPtr
, newForeignPtr_
, addForeignPtrFinalizer
, newForeignPtrEnv
, addForeignPtrFinalizerEnv
, withForeignPtr
, finalizeForeignPtr
, unsafeForeignPtrToPtr
, touchForeignPtr
, castForeignPtr
, plusForeignPtr
, mallocForeignPtr
, mallocForeignPtrBytes
, mallocForeignPtrArray
, mallocForeignPtrArray0
)
where
import GHC.Internal.Foreign.Ptr
import GHC.Internal.Foreign.Storable ( Storable(sizeOf) )
import GHC.Internal.Base
import GHC.Internal.Num
import GHC.Internal.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 fObj <- Ptr a -> IO (ForeignPtr a)
forall a. Ptr a -> IO (ForeignPtr a)
newForeignPtr_ Ptr a
p
addForeignPtrFinalizer finalizer fObj
return 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 fObj <- Ptr a -> IO (ForeignPtr a)
forall a. Ptr a -> IO (ForeignPtr a)
newForeignPtr_ Ptr a
p
addForeignPtrFinalizerEnv finalizer env fObj
return 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)