diff options
| author | Ben Gamari <ben@smart-cactus.org> | 2020-04-19 11:27:49 -0400 |
|---|---|---|
| committer | Ben Gamari <ben@smart-cactus.org> | 2020-12-27 11:24:25 -0500 |
| commit | 61ac39dd3c68838d3ab4168849eae91af5c1a077 (patch) | |
| tree | b5251c3ce1ff57b98143880fffda036f0bdda8f2 | |
| parent | d9ac1a672b44466bfd2f29519915110cd5c31297 (diff) | |
| download | haskell-61ac39dd3c68838d3ab4168849eae91af5c1a077.tar.gz | |
base: Add unsafeWithForeignPtr
(cherry picked from commit eb21e9ea8459fb7e5769397d8322963c9755f3b3)
| -rw-r--r-- | libraries/base/Foreign/ForeignPtr/Imp.hs | 25 | ||||
| -rw-r--r-- | libraries/base/GHC/ForeignPtr.hs | 39 |
2 files changed, 38 insertions, 26 deletions
diff --git a/libraries/base/Foreign/ForeignPtr/Imp.hs b/libraries/base/Foreign/ForeignPtr/Imp.hs index 2fc18689a9..3af5da13a9 100644 --- a/libraries/base/Foreign/ForeignPtr/Imp.hs +++ b/libraries/base/Foreign/ForeignPtr/Imp.hs @@ -66,31 +66,6 @@ newForeignPtr finalizer p addForeignPtrFinalizer finalizer fObj return fObj -withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b --- ^This is a way to look at the pointer living inside a --- foreign object. This function takes a function which is --- applied to that pointer. The resulting 'IO' action is then --- executed. The foreign object is kept alive at least during --- the whole action, even if it is not used directly --- inside. Note that it is not safe to return the pointer from --- the action and use it after the action completes. All uses --- of the pointer should be inside the --- 'withForeignPtr' bracket. The reason for --- this unsafeness is the same as for --- 'unsafeForeignPtrToPtr' below: the finalizer --- may run earlier than expected, because the compiler can only --- track usage of the 'ForeignPtr' object, not --- a 'Ptr' object made from it. --- --- This function is normally used for marshalling data to --- or from the object pointed to by the --- 'ForeignPtr', using the operations from the --- 'Storable' class. -withForeignPtr fo io - = do r <- io (unsafeForeignPtrToPtr fo) - touchForeignPtr fo - return r - -- | This variant of 'newForeignPtr' adds a finalizer that expects an -- environment in addition to the finalized pointer. The environment -- that will be passed to the finalizer is fixed by the second argument to diff --git a/libraries/base/GHC/ForeignPtr.hs b/libraries/base/GHC/ForeignPtr.hs index 27e83f453f..9482cc46b5 100644 --- a/libraries/base/GHC/ForeignPtr.hs +++ b/libraries/base/GHC/ForeignPtr.hs @@ -46,8 +46,11 @@ module GHC.ForeignPtr unsafeForeignPtrToPtr, castForeignPtr, plusForeignPtr, - -- * Finalization + -- * Control over lifetype + withForeignPtr, + unsafeWithForeignPtr, touchForeignPtr, + -- * Finalization finalizeForeignPtr -- * Commentary -- $commentary @@ -504,6 +507,40 @@ newForeignPtr_ (Ptr obj) = do r <- newIORef NoFinalizers return (ForeignPtr obj (PlainForeignPtr r)) +withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b +-- ^This is a way to look at the pointer living inside a +-- foreign object. This function takes a function which is +-- applied to that pointer. The resulting 'IO' action is then +-- executed. The foreign object is kept alive at least during +-- the whole action, even if it is not used directly +-- inside. Note that it is not safe to return the pointer from +-- the action and use it after the action completes. All uses +-- of the pointer should be inside the +-- 'withForeignPtr' bracket. The reason for +-- this unsafeness is the same as for +-- 'unsafeForeignPtrToPtr' below: the finalizer +-- may run earlier than expected, because the compiler can only +-- track usage of the 'ForeignPtr' object, not +-- a 'Ptr' object made from it. +-- +-- This function is normally used for marshalling data to +-- or from the object pointed to by the +-- 'ForeignPtr', using the operations from the +-- 'Storable' class. +withForeignPtr = unsafeWithForeignPtr + +-- | This is similar to 'withForeignPtr' but comes with an important caveat: +-- the user must guarantee that the continuation does not diverge (e.g. loop or +-- throw an exception). In exchange for this loss of generality, this function +-- offers the ability of GHC to optimise more aggressively. +-- +-- See issue #17760 for the motivation for this function. +unsafeWithForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b +unsafeWithForeignPtr fo f = do + r <- f (unsafeForeignPtrToPtr fo) + touchForeignPtr fo + return r + touchForeignPtr :: ForeignPtr a -> IO () -- ^This function ensures that the foreign object in -- question is alive at the given place in the sequence of IO |
