packages feed

regional-pointers 0.4 → 0.5

raw patch · 9 files changed

+136/−106 lines, 9 filesdep +monad-peeldep −MonadCatchIO-transformersdep ~basedep ~regions

Dependencies added: monad-peel

Dependencies removed: MonadCatchIO-transformers

Dependency ranges changed: base, regions

Files

Foreign/C/String/Region.hs view
@@ -47,14 +47,14 @@ --------------------------------------------------------------------------------  -- from base:-import Prelude                           ( fromInteger, fromIntegral )+import Prelude                           ( fromIntegral ) import Data.Function                     ( ($) ) import Data.Bool                         ( Bool ) import Data.Int                          ( Int ) import Data.Char                         ( Char, String, ord ) import Data.List                         ( map, length ) import Control.Arrow                     ( first )-import Control.Monad                     ( return, (>>=), fail)+import Control.Monad                     ( return ) import Foreign.C.Types                   ( CChar, CWchar ) import Foreign.Storable                  ( Storable ) import qualified Foreign.C.String as FCS ( charIsRepresentable@@ -64,9 +64,9 @@                                          )  #ifdef __HADDOCK__-import Foreign.C.String ( CString,  CStringLen-                        , CWString, CWStringLen-                        )+import Foreign.C.String                  ( CString,  CStringLen+                                         , CWString, CWStringLen+                                         ) import qualified Foreign.C.String as FCS ( peekCString,  peekCStringLen                                          , newCString,   newCStringLen                                          , withCString,  withCStringLen@@ -77,6 +77,11 @@                                          ) #endif +#if __GLASGOW_HASKELL__ < 701+import Prelude                           ( fromInteger )+import Control.Monad                     ( (>>=), fail )+#endif+ #ifdef mingw32_HOST_OS -- These are only used in the mingw32 version of 'charsToCWchars': import Prelude                           ( (-), (+), mod, div )@@ -92,11 +97,11 @@ -- from transformers: import Control.Monad.IO.Class            ( MonadIO, liftIO ) --- from MonadCatchIO-transformers:-import Control.Monad.CatchIO             ( MonadCatchIO )+-- from monad-peel:+import Control.Monad.IO.Peel             ( MonadPeelIO )  -- from regions:-import Control.Monad.Trans.Region        ( RegionT, ParentOf )+import Control.Monad.Trans.Region        ( RegionT, AncestorRegion )  -- from ourselves: import Foreign.Marshal.Array.Region      ( newArray0, newArray@@ -131,14 +136,14 @@ -- | Marshal a NUL terminated C string into a Haskell string. -- -- Wraps: @Foreign.C.String.'FCS.peekCString'@-peekCString ∷ (pr `ParentOf` cr, MonadIO cr)+peekCString ∷ (pr `AncestorRegion` cr, MonadIO cr)              ⇒ RegionalCString pr → cr String peekCString = peekCAString  -- | Marshal a C string with explicit length into a Haskell string. -- -- Wraps: @Foreign.C.String.'FCS.peekCStringLen'@.-peekCStringLen ∷ (pr `ParentOf` cr, MonadIO cr)+peekCStringLen ∷ (pr `AncestorRegion` cr, MonadIO cr)                ⇒ RegionalCStringLen pr → cr String peekCStringLen = peekCAStringLen @@ -147,7 +152,7 @@ -- The Haskell string may /not/ contain any NUL characters -- -- Wraps: @Foreign.C.String.'FCS.newCString'@.-newCString ∷ MonadCatchIO pr+newCString ∷ MonadPeelIO pr            ⇒ String → RegionT s pr (RegionalCString (RegionT s pr)) newCString = newCAString @@ -155,7 +160,7 @@ -- explicit length information. -- -- Wraps: @Foreign.C.String.'FCS.newCStringLen'@.-newCStringLen ∷ MonadCatchIO pr+newCStringLen ∷ MonadPeelIO pr               ⇒ String → RegionT s pr (RegionalCStringLen (RegionT s pr)) newCStringLen = newCAStringLen @@ -168,7 +173,7 @@ --   via an exception). -- -- Wraps: @Foreign.C.String.'FCS.withCString'@.-withCString ∷ MonadCatchIO pr+withCString ∷ MonadPeelIO pr             ⇒ String             → (∀ s. RegionalCString (RegionT s pr) → RegionT s pr α)             → pr α@@ -181,7 +186,7 @@ --   via an exception). -- -- Wraps: @Foreign.C.String.'FCS.withCStringLen'@.-withCStringLen ∷ MonadCatchIO pr+withCStringLen ∷ MonadPeelIO pr                ⇒ String                → (∀ s. RegionalCStringLen (RegionT s pr) → RegionT s pr α)                → pr α@@ -200,14 +205,14 @@ -- | Marshal a NUL terminated C string into a Haskell string. -- -- Wraps: @Foreign.C.String.'FCS.peekCAString'@.-peekCAString ∷ (pr `ParentOf` cr, MonadIO cr)+peekCAString ∷ (pr `AncestorRegion` cr, MonadIO cr)              ⇒ RegionalCString pr → cr String peekCAString = unsafeWrap FCS.peekCAString  -- | Marshal a C string with explicit length into a Haskell string. -- -- Wraps: @Foreign.C.String.'FCS.peekCAStringLen'@.-peekCAStringLen ∷ (pr `ParentOf` cr, MonadIO cr)+peekCAStringLen ∷ (pr `AncestorRegion` cr, MonadIO cr)                 ⇒ RegionalCStringLen pr → cr String peekCAStringLen = liftIO ∘ FCS.peekCAStringLen ∘ first unsafePtr @@ -216,7 +221,7 @@ -- The Haskell string may /not/ contain any NUL characters -- -- Wraps: @Foreign.C.String.'FCS.newCAString'@.-newCAString ∷ MonadCatchIO pr+newCAString ∷ MonadPeelIO pr             ⇒ String → RegionT s pr (RegionalCString (RegionT s pr)) newCAString = newArray0 nUL ∘ charsToCChars @@ -224,7 +229,7 @@ -- explicit length information. -- -- Wraps: @Foreign.C.String.'FCS.newCAStringLen'@.-newCAStringLen ∷ MonadCatchIO pr+newCAStringLen ∷ MonadPeelIO pr                ⇒ String → RegionT s pr (RegionalCStringLen (RegionT s pr)) newCAStringLen = newArrayLen ∘ charsToCChars @@ -237,7 +242,7 @@ -- via an exception). -- -- Wraps: @Foreign.C.String.'FCS.withCAString'@.-withCAString ∷ MonadCatchIO pr+withCAString ∷ MonadPeelIO pr              ⇒ String              → (∀ s. RegionalCString (RegionT s pr) → RegionT s pr α)              → pr α@@ -250,7 +255,7 @@ --   via an exception). -- -- Wraps: @Foreign.C.String.'FCS.withCAStringLen'@.-withCAStringLen ∷ MonadCatchIO pr+withCAStringLen ∷ MonadPeelIO pr                 ⇒ String                 → (∀ s. RegionalCStringLen (RegionT s pr) → RegionT s pr α)                 → pr α@@ -278,14 +283,14 @@ -- | Marshal a NUL terminated C wide string into a Haskell string. -- -- Wraps: @Foreign.C.String.'FCS.peekCWString'@.-peekCWString ∷ (pr `ParentOf` cr, MonadIO cr)+peekCWString ∷ (pr `AncestorRegion` cr, MonadIO cr)              ⇒ RegionalCWString pr → cr String peekCWString = unsafeWrap FCS.peekCWString  -- | Marshal a C wide string with explicit length into a Haskell string. -- -- Wraps: @Foreign.C.String.'FCS.peekCWStringLen'@.-peekCWStringLen ∷ (pr `ParentOf` cr, MonadIO cr)+peekCWStringLen ∷ (pr `AncestorRegion` cr, MonadIO cr)                 ⇒ RegionalCWStringLen pr → cr String peekCWStringLen = liftIO ∘ FCS.peekCWStringLen ∘ first unsafePtr @@ -294,7 +299,7 @@ -- The Haskell string may /not/ contain any NUL characters. -- -- Wraps: @Foreign.C.String.'FCS.newCWString'@.-newCWString ∷ MonadCatchIO pr+newCWString ∷ MonadPeelIO pr             ⇒ String → RegionT s pr (RegionalCWString (RegionT s pr)) newCWString = newArray0 wNUL ∘ charsToCWchars @@ -302,7 +307,7 @@ -- with explicit length information. -- -- Wraps: @Foreign.C.String.'FCS.newCWStringLen'@.-newCWStringLen ∷ MonadCatchIO pr+newCWStringLen ∷ MonadPeelIO pr                ⇒ String → RegionT s pr (RegionalCWStringLen (RegionT s pr)) newCWStringLen = newArrayLen ∘ charsToCWchars @@ -315,7 +320,7 @@ --   normally or via an exception). -- -- Wraps: @Foreign.C.String.'FCS.withCWString'@.-withCWString ∷ MonadCatchIO pr+withCWString ∷ MonadPeelIO pr              ⇒ String              → (∀ s. RegionalCWString (RegionT s pr) → RegionT s pr α)              → pr α@@ -330,7 +335,7 @@ --   normally or via an exception). -- -- Wraps: @Foreign.C.String.'FCS.withCWStringLen'@.-withCWStringLen ∷ MonadCatchIO pr+withCWStringLen ∷ MonadPeelIO pr                 ⇒ String                 → (∀ s. RegionalCWStringLen (RegionT s pr) → RegionT s pr α)                 → pr α@@ -349,7 +354,7 @@ wNUL = 0  -- | allocate an array to hold the list and pair it with the number of elements.-newArrayLen ∷ (Storable α, MonadCatchIO pr)+newArrayLen ∷ (Storable α, MonadPeelIO pr)             ⇒ [α] → RegionT s pr (RegionalPtr α (RegionT s pr), Int) newArrayLen xs = do   rPtr ← newArray xs
Foreign/Marshal/Alloc/Region.hs view
@@ -32,7 +32,7 @@ --------------------------------------------------------------------------------  -- from base:-import Control.Monad                          ( (>>=), fail )+import Control.Monad                          ( (>>=) ) import Data.Function                          ( ($) ) import Data.Int                               ( Int ) import Foreign.Storable                       ( Storable, sizeOf )@@ -45,14 +45,19 @@ import qualified Foreign.Marshal.Alloc as FMA ( alloca, allocaBytes, malloc ) #endif +#if __GLASGOW_HASKELL__ < 701+import Control.Monad                          ( fail )+#endif+ -- from base-unicode-symbols: import Prelude.Unicode                        ( (⊥) )  -- from transformers: import Control.Monad.IO.Class                 ( liftIO ) --- from MonadCatchIO-transformers:-import Control.Monad.CatchIO                  ( MonadCatchIO, block )+-- from monad-peel:+import Control.Monad.IO.Peel                  ( MonadPeelIO )+import Control.Exception.Peel                 ( block )  -- from regions: import Control.Monad.Trans.Region             ( RegionT, runRegionT )@@ -75,7 +80,7 @@  Note that: @alloca = 'allocaBytes' $ 'sizeOf' (undefined :: &#945;)@ -}-alloca ∷ ∀ α pr β. (Storable α, MonadCatchIO pr)+alloca ∷ ∀ α pr β. (Storable α, MonadPeelIO pr)        ⇒ (∀ s. RegionalPtr α (RegionT s pr) → RegionT s pr β)        → pr β alloca = allocaBytes $ sizeOf ((⊥) ∷ α)@@ -89,7 +94,7 @@  Note that: @allocaBytes size f = 'runRegionT' $ 'mallocBytes' size >>= f@ -}-allocaBytes ∷ ∀ α pr β. MonadCatchIO pr+allocaBytes ∷ ∀ α pr β. MonadPeelIO pr             ⇒ Int             → (∀ s. RegionalPtr α (RegionT s pr) → RegionT s pr β)             → pr β@@ -108,7 +113,7 @@  Note that: @malloc = 'mallocBytes' $ 'sizeOf' (undefined :: &#945;)@ -}-malloc ∷ ∀ α pr s. (Storable α, MonadCatchIO pr)+malloc ∷ ∀ α pr s. (Storable α, MonadPeelIO pr)        ⇒ RegionT s pr (RegionalPtr α (RegionT s pr)) malloc = mallocBytes $ sizeOf ((⊥) ∷ α) @@ -117,7 +122,7 @@ This should provide a safer replacement for: @Foreign.Marshal.Alloc.'FMA.mallocBytes'@. -}-mallocBytes ∷ MonadCatchIO pr+mallocBytes ∷ MonadPeelIO pr             ⇒ Int             → RegionT s pr (RegionalPtr α (RegionT s pr)) mallocBytes size = block $ do@@ -125,10 +130,10 @@                      unsafeRegionalPtr ptr $ free ptr  -- TODO:--- realloc ∷ (Storable β, pr `ParentOf` cr, MonadIO cr)+-- realloc ∷ (Storable β, pr `AncestorRegion` cr, MonadIO cr) --         ⇒ RegionalPtr α pr → cr (RegionalPtr β pr) -- realloc = ...--- reallocBytes ∷ (pr `ParentOf` cr, MonadIO cr)+-- reallocBytes ∷ (pr `AncestorRegion` cr, MonadIO cr) --              ⇒ RegionalPtr α pr → Int → cr (RegionalPtr α pr) -- reallocBytes = ... 
Foreign/Marshal/Array/Region.hs view
@@ -55,11 +55,11 @@  -- from base: import Prelude                                ( (*), succ )-import Data.Function                          ( ($), flip, const )+import Data.Function                          ( ($), flip ) import Data.Int                               ( Int ) import Data.List                              ( length ) import Data.Eq                                ( Eq )-import Control.Monad                          ( return, (>>=), fail, (>>) )+import Control.Monad                          ( return ) import System.IO                              ( IO ) import Foreign.Ptr                            ( Ptr ) import Foreign.Storable                       ( Storable, sizeOf )@@ -72,6 +72,11 @@                                               , lengthArray0                                               , advancePtr                                               )++#if __GLASGOW_HASKELL__ < 701+import Control.Monad                          ( (>>=), fail, (>>) )+#endif+ -- from base-unicode-symbols: import Data.Function.Unicode                  ( (∘) ) import Prelude.Unicode                        ( (⊥) )@@ -79,11 +84,11 @@ -- from transformers: import Control.Monad.IO.Class                 ( MonadIO, liftIO ) --- from MonadCatchIO-transformers:-import Control.Monad.CatchIO                  ( MonadCatchIO )+-- from monad-peel:+import Control.Monad.IO.Peel                  ( MonadPeelIO )  -- from regions:-import Control.Monad.Trans.Region             ( RegionT , ParentOf )+import Control.Monad.Trans.Region             ( RegionT , AncestorRegion )  -- from ourselves: import Foreign.Ptr.Region                     ( RegionalPtr, mapRegionalPtr )@@ -103,19 +108,19 @@ -- | Allocate storage for the given number of elements of a storable type. -- -- Like 'malloc', but for multiple elements.-mallocArray ∷ ∀ α s pr. (Storable α, MonadCatchIO pr)+mallocArray ∷ ∀ α s pr. (Storable α, MonadPeelIO pr)             ⇒ Int → RegionT s pr (RegionalPtr α (RegionT s pr)) mallocArray size = mallocBytes $ size * sizeOf ((⊥) ∷ α)  -- | Like 'mallocArray', but add an extra position to hold a special termination -- element.-mallocArray0 ∷ (Storable α, MonadCatchIO pr)+mallocArray0 ∷ (Storable α, MonadPeelIO pr)              ⇒ Int → RegionT s pr (RegionalPtr α (RegionT s pr)) mallocArray0 = mallocArray ∘ succ  -- | Temporarily allocate space for the given number of elements (like 'alloca', -- but for multiple elements).-allocaArray ∷ ∀ α pr β. (Storable α, MonadCatchIO pr)+allocaArray ∷ ∀ α pr β. (Storable α, MonadPeelIO pr)             ⇒ Int             → (∀ s. RegionalPtr α (RegionT s pr) → RegionT s pr β)             → pr β@@ -123,7 +128,7 @@  -- | Like 'allocaArray', but add an extra position to hold a special termination -- element.-allocaArray0 ∷ ∀ α pr β. (Storable α, MonadCatchIO pr)+allocaArray0 ∷ ∀ α pr β. (Storable α, MonadPeelIO pr)              ⇒ Int              → (∀ s. RegionalPtr α (RegionT s pr) → RegionT s pr β)              → pr β@@ -150,21 +155,21 @@ -- linear stack space.) -- -- Wraps: @Foreign.Marshal.Array.'FMA.peekArray'@.-peekArray ∷ (Storable α, pr `ParentOf` cr, MonadIO cr)+peekArray ∷ (Storable α, pr `AncestorRegion` cr, MonadIO cr)           ⇒ Int → RegionalPtr α pr → cr [α] peekArray =  unsafeWrap2flp FMA.peekArray  -- | Convert an array terminated by the given end marker into a Haskell list. -- -- Wraps: @Foreign.Marshal.Array.'FMA.peekArray0'@.-peekArray0 ∷ (Storable α, Eq α, pr `ParentOf` cr, MonadIO cr)+peekArray0 ∷ (Storable α, Eq α, pr `AncestorRegion` cr, MonadIO cr)            ⇒ α → RegionalPtr α pr → cr [α] peekArray0 = unsafeWrap2flp FMA.peekArray0  -- | Write the list elements consecutive into memory. -- -- Wraps: @Foreign.Marshal.Array.'FMA.pokeArray'@.-pokeArray ∷ (Storable α, pr `ParentOf` cr, MonadIO cr)+pokeArray ∷ (Storable α, pr `AncestorRegion` cr, MonadIO cr)           ⇒ RegionalPtr α pr → [α] → cr () pokeArray = unsafeWrap2 FMA.pokeArray @@ -172,7 +177,7 @@ -- given marker element. -- -- Wraps: @Foreign.Marshal.Array.'FMA.pokeArray0'@.-pokeArray0 ∷ (Storable α, pr `ParentOf` cr, MonadIO cr)+pokeArray0 ∷ (Storable α, pr `AncestorRegion` cr, MonadIO cr)            ⇒ α → RegionalPtr α pr → [α] → cr () pokeArray0 m rp xs = liftIO $ FMA.pokeArray0 m (unsafePtr rp) xs @@ -185,7 +190,7 @@ -- sequence of storable values. -- -- Like 'new', but for multiple elements.-newArray ∷ (Storable α, MonadCatchIO pr)+newArray ∷ (Storable α, MonadPeelIO pr)          ⇒ [α] → RegionT s pr (RegionalPtr α (RegionT s pr )) newArray vals  = do   ptr ← mallocArray $ length vals@@ -194,7 +199,7 @@  -- | Write a list of storable elements into a newly allocated, consecutive -- sequence of storable values, where the end is fixed by the given end marker.-newArray0 ∷ (Storable α, MonadCatchIO pr)+newArray0 ∷ (Storable α, MonadPeelIO pr)           ⇒ α → [α] → RegionT s pr (RegionalPtr α (RegionT s pr)) newArray0 marker vals  = do   ptr ← mallocArray0 $ length vals@@ -204,23 +209,23 @@ -- | Temporarily store a list of storable values in memory. -- -- Like 'with', but for multiple elements.-withArray ∷ (Storable α, MonadCatchIO pr)+withArray ∷ (Storable α, MonadPeelIO pr)           ⇒ [α]           → (∀ s. RegionalPtr α (RegionT s pr) → RegionT s pr β)           → pr β-withArray vals = withArrayLen vals ∘ const+withArray vals f = withArrayLen vals $ \_ → f  -- | Like 'withArray', but a terminator indicates where the array ends.-withArray0 ∷ (Storable α, MonadCatchIO pr)+withArray0 ∷ (Storable α, MonadPeelIO pr)            ⇒ α            → [α]            → (∀ s. RegionalPtr α (RegionT s pr) → RegionT s pr β)            → pr β-withArray0 marker vals = withArrayLen0 marker vals ∘ const+withArray0 marker vals f = withArrayLen0 marker vals $ \_ → f  -- | Like 'withArray', but the action gets the number of values as an additional -- parameter.-withArrayLen ∷ (Storable α, MonadCatchIO pr)+withArrayLen ∷ (Storable α, MonadPeelIO pr)             ⇒ [α]             → (∀ s. Int → RegionalPtr α (RegionT s pr) → RegionT s pr β)             → pr β@@ -233,7 +238,7 @@     len = length vals  -- | Like 'withArrayLen', but a terminator indicates where the array ends.-withArrayLen0 ∷ (Storable α, MonadCatchIO pr)+withArrayLen0 ∷ (Storable α, MonadPeelIO pr)               ⇒ α               → [α]               → (∀ s. Int → RegionalPtr α (RegionT s pr) → RegionT s pr β)@@ -256,8 +261,8 @@ -- -- Wraps: @Foreign.Marshal.Array.'FMA.copyArray'@. copyArray ∷ ( Storable α-            , pr1 `ParentOf` cr-            , pr2 `ParentOf` cr+            , pr1 `AncestorRegion` cr+            , pr2 `AncestorRegion` cr             , MonadIO cr             )           ⇒ RegionalPtr α pr1 -- ^ Destination@@ -271,8 +276,8 @@ -- -- Wraps: @Foreign.Marshal.Array.'FMA.moveArray'@. moveArray ∷ ( Storable α-            , pr1 `ParentOf` cr-            , pr2 `ParentOf` cr+            , pr1 `AncestorRegion` cr+            , pr2 `AncestorRegion` cr             , MonadIO cr             )           ⇒ RegionalPtr α pr1 -- ^ Destination@@ -289,7 +294,7 @@ -- | Return the number of elements in an array, excluding the terminator. -- -- Wraps: @Foreign.Marshal.Array.'FMA.lengthArray0'@.-lengthArray0 ∷ (Storable α, Eq α, pr `ParentOf` cr, MonadIO cr)+lengthArray0 ∷ (Storable α, Eq α, pr `AncestorRegion` cr, MonadIO cr)              ⇒ α → RegionalPtr α pr → cr Int lengthArray0 = unsafeWrap2flp FMA.lengthArray0 
Foreign/Marshal/Utils/Region.hs view
@@ -43,7 +43,7 @@ -- from base: import Data.Function                          ( ($) ) import Data.Int                               ( Int )-import Control.Monad                          ( return, (>>=), fail, (>>) )+import Control.Monad                          ( return, (>>) ) import qualified Foreign.Marshal.Utils as FMU ( fromBool,  toBool                                               , copyBytes, moveBytes                                               )@@ -54,17 +54,21 @@ import Foreign.Storable                       ( sizeOf ) #endif +#if __GLASGOW_HASKELL__ < 701+import Control.Monad                          ( (>>=), fail )+#endif+ -- from base-unicode-symbols: import Data.Function.Unicode                  ( (∘) )  -- from transformers: import Control.Monad.IO.Class                 ( MonadIO, liftIO ) --- from MonadCatchIO-transformers:-import Control.Monad.CatchIO                  ( MonadCatchIO )+-- from monad-peel:+import Control.Monad.IO.Peel                  ( MonadPeelIO )  -- from regions:-import Control.Monad.Trans.Region             ( RegionT, ParentOf )+import Control.Monad.Trans.Region             ( RegionT, AncestorRegion )  -- from ourselves: import Foreign.Ptr.Region                     ( RegionalPtr )@@ -87,7 +91,7 @@ -- exception). -- -- This provides a safer replacement for @Foreign.Marshal.Utils.'FMU.with'@.-with ∷ (Storable α, MonadCatchIO pr)+with ∷ (Storable α, MonadPeelIO pr)      ⇒ α → (∀ s. RegionalPtr α (RegionT s pr) → RegionT s pr β) → pr β with val f = alloca $ \ptr → poke ptr val >> f ptr @@ -96,7 +100,7 @@ -- 'sizeOf' method from the instance of 'Storable' for the appropriate type. -- -- This provides a safer replacement for @Foreign.Marshal.Utils.'FMU.new'@.-new ∷ (Storable α, MonadCatchIO pr)+new ∷ (Storable α, MonadPeelIO pr)     ⇒ α → RegionT s pr (RegionalPtr α (RegionT s pr)) new val = do ptr ← malloc              poke ptr val@@ -118,8 +122,8 @@ -- first (destination); the copied areas may /not/ overlap -- -- Wraps: @Foreign.Marshal.Utils.'FMU.copyBytes'@.-copyBytes ∷ ( pr1 `ParentOf` cr-            , pr2 `ParentOf` cr+copyBytes ∷ ( pr1 `AncestorRegion` cr+            , pr2 `AncestorRegion` cr             , MonadIO cr             )           ⇒ RegionalPtr α pr1 -- ^ Destination@@ -132,8 +136,8 @@ -- first (destination); the copied areas /may/ overlap -- -- Wraps: @Foreign.Marshal.Utils.'FMU.moveBytes'@.-moveBytes ∷ ( pr1 `ParentOf` cr-            , pr2 `ParentOf` cr+moveBytes ∷ ( pr1 `AncestorRegion` cr+            , pr2 `AncestorRegion` cr             , MonadIO cr             )           ⇒ RegionalPtr α pr1 -- ^ Destination
Foreign/Ptr/Region.hs view
@@ -65,7 +65,11 @@ -- Note that @nullPtr@ is a pure value. This means it does not perform the -- side-effect of registering a finalizer like @free nullPtr@ -- in the 'RegionT' monad.-nullPtr ∷ RegionalPtr α r+--+-- Finally note that the region parameter of the 'RegionalPtr' is set to+-- 'RootRegion' which is the ancestor of any region. This allows 'nullPtr' to be+-- used in any region.+nullPtr ∷ RegionalPtr α RootRegion nullPtr = unsafePureRegionalPtr FP.nullPtr  
Foreign/Ptr/Region/Internal.hs view
@@ -31,18 +31,21 @@ --------------------------------------------------------------------------------  -- from base:-import Control.Monad ( return, (>>=), fail )+import Control.Monad ( return, liftM ) import Data.Function ( ($) ) import Data.Maybe    ( Maybe(Nothing, Just) ) import System.IO     ( IO ) import Foreign.Ptr   ( Ptr ) +-- from base-unicode-symbols:+import Data.Function.Unicode ( (∘) )+ -- from transformers: import Control.Monad.IO.Class ( MonadIO, liftIO )  -- from regions:-import Control.Monad.Trans.Region.OnExit ( CloseHandle, CloseAction, onExit )-import Control.Monad.Trans.Region        ( RegionT, Dup(dup) )+import Control.Monad.Trans.Region.OnExit ( FinalizerHandle, Finalizer, onExit )+import Control.Monad.Trans.Region        ( RegionT, RootRegion, Dup(dup) )   --------------------------------------------------------------------------------@@ -51,37 +54,40 @@  -- | A regional handle to memory. This should provide a safer replacement for -- @Foreign.Ptr.'Ptr'@-data RegionalPtr α (r ∷ * → *) = RegionalPtr !(Ptr α) !(Maybe (CloseHandle r))+data RegionalPtr α (r ∷ * → *) = RegionalPtr !(Ptr α) !(Maybe (FinalizerHandle r))  instance Dup (RegionalPtr α) where     dup (RegionalPtr ptr Nothing)   = return $ RegionalPtr ptr Nothing-    dup (RegionalPtr ptr (Just ch)) = do ch' ← dup ch-                                         return $ RegionalPtr ptr $ Just ch'+    dup (RegionalPtr ptr (Just ch)) = liftM (RegionalPtr ptr ∘ Just) $ dup ch + -------------------------------------------------------------------------------- -- * Constructing regional pointers --------------------------------------------------------------------------------  -- | Construct a regional pointer from a native pointer -- and an @IO@ computation that finalizes the pointer (like @free ptr@)--- which is executed when the region exits.+-- which is performed when the region terminates. ----- This function is considered unsafe because this library can't guarantee that--- the finalizer will actually finalize the pointer (suppose having @return ()@--- as the finalizer). You have to verify the correct finalisation yourself.+-- This function is unsafe because this library can't guarantee that the+-- finalizer will actually finalize the pointer (suppose having @return ()@ as+-- the finalizer). You have to verify the correct finalisation yourself. unsafeRegionalPtr ∷ MonadIO pr                   ⇒ Ptr α-                  → CloseAction+                  → Finalizer                   → RegionT s pr (RegionalPtr α (RegionT s pr))-unsafeRegionalPtr ptr finalize = do ch ← onExit finalize-                                    return $ RegionalPtr ptr $ Just ch+unsafeRegionalPtr ptr finalize = liftM (RegionalPtr ptr ∘ Just) $ onExit finalize  -- | Construct a regional pointer from a native pointer -- without registering a finalizer like @free ptr@. ----- This function is considered unsafe because this library can't guarantee the--- finalisation of the pointer, you have to do that yourself.-unsafePureRegionalPtr ∷ Ptr α → RegionalPtr α r+-- This function is unsafe because this library can't guarantee the finalisation+-- of the pointer, you have to verify the correct finalisation yourself.+--+-- Note that the region parameter of the 'RegionalPtr' is set to 'RootRegion'+-- which is the ancestor of any region. This allows the regional pointer to be+-- used in any region.+unsafePureRegionalPtr ∷ Ptr α → RegionalPtr α RootRegion unsafePureRegionalPtr ptr = RegionalPtr ptr Nothing  @@ -89,6 +95,11 @@ -- * Utility functions for lifting operations on Ptrs to RegionalPtrs -------------------------------------------------------------------------------- +-- | Retrieve the native pointer from a regional pointer.+--+-- This function is unsafe because it both allows you to @free@ the pointer+-- before the region terminates and use the pointer outside the region when it+-- is already freed. unsafePtr ∷ RegionalPtr α r → Ptr α unsafePtr (RegionalPtr ptr _) = ptr 
Foreign/Ptr/Region/Unsafe.hs view
@@ -20,10 +20,6 @@     , unsafePureRegionalPtr        -- * Unsafe utility functions for lifting operations on @Ptrs@ to @RegionalPtrs@--    --  | These operations are unsafe because they allow you to @free@ the-    --  regional pointer before exiting their region. So they enable you to-    --  perform @IO@ with already freed pointers.     , unsafePtr     , unsafeWrap, unsafeWrap2, unsafeWrap3     ) where
Foreign/Storable/Region.hs view
@@ -34,7 +34,7 @@ import Control.Monad.IO.Class           ( MonadIO )  -- from regions:-import Control.Monad.Trans.Region       ( ParentOf )+import Control.Monad.Trans.Region       ( AncestorRegion )  -- from ourselves: import Foreign.Ptr.Region               ( RegionalPtr )@@ -57,7 +57,7 @@ -- implementation of the function. -- -- Wraps: @Foreign.Storable.'FS.peekElemOff'@.-peekElemOff ∷ (pr `ParentOf` cr, Storable α, MonadIO cr)+peekElemOff ∷ (pr `AncestorRegion` cr, Storable α, MonadIO cr)             ⇒ RegionalPtr α pr → Int → cr α peekElemOff = unsafeWrap2 FS.peekElemOff @@ -68,7 +68,7 @@ -- >   poke (addr `plusPtr` (idx * sizeOf x)) x -- -- Wraps: @Foreign.Storable.'FS.pokeElemOff'@.-pokeElemOff ∷ (pr `ParentOf` cr, Storable α, MonadIO cr)+pokeElemOff ∷ (pr `AncestorRegion` cr, Storable α, MonadIO cr)             ⇒ RegionalPtr α pr → Int → α → cr () pokeElemOff = unsafeWrap3 FS.pokeElemOff @@ -78,7 +78,7 @@ -- > peekByteOff addr off = peek (addr `plusPtr` off) -- -- Wraps: @Foreign.Storable.'FS.peekByteOff'@.-peekByteOff ∷ (pr `ParentOf` cr, Storable α, MonadIO cr)+peekByteOff ∷ (pr `AncestorRegion` cr, Storable α, MonadIO cr)             ⇒ RegionalPtr β pr → Int → cr α peekByteOff = unsafeWrap2 FS.peekByteOff @@ -88,7 +88,7 @@ -- > pokeByteOff addr off x = poke (addr `plusPtr` off) x -- -- Wraps: @Foreign.Storable.'FS.pokeByteOff'@.-pokeByteOff ∷ (pr `ParentOf` cr, Storable α, MonadIO cr)+pokeByteOff ∷ (pr `AncestorRegion` cr, Storable α, MonadIO cr)             ⇒ RegionalPtr β pr → Int → α → cr () pokeByteOff = unsafeWrap3 FS.pokeByteOff @@ -101,7 +101,7 @@ -- is fulfilled. -- -- Wraps: @Foreign.Storable.'FS.peek'@.-peek ∷ (pr `ParentOf` cr, Storable α, MonadIO cr)+peek ∷ (pr `AncestorRegion` cr, Storable α, MonadIO cr)      ⇒ RegionalPtr α pr → cr α peek = unsafeWrap FS.peek @@ -109,7 +109,7 @@ -- might apply; see 'peek'. -- -- Wraps: @Foreign.Storable.'FS.poke'@.-poke ∷ (pr `ParentOf` cr, Storable α, MonadIO cr)+poke ∷ (pr `AncestorRegion` cr, Storable α, MonadIO cr)      ⇒ RegionalPtr α pr → α → cr () poke = unsafeWrap2 FS.poke 
regional-pointers.cabal view
@@ -1,5 +1,5 @@ name:          regional-pointers-version:       0.4+version:       0.5 cabal-version: >=1.6 build-type:    Custom license:       BSD3@@ -34,11 +34,11 @@  Library   GHC-Options: -Wall-  build-depends: base                      >= 4     && < 4.3-               , base-unicode-symbols      >= 0.1.1 && < 0.3-               , regions                   >= 0.6   && < 0.8-               , transformers              >= 0.2   && < 0.3-               , MonadCatchIO-transformers >= 0.2   && < 0.3+  build-depends: base                 >= 4     && < 4.4+               , base-unicode-symbols >= 0.1.1 && < 0.3+               , regions              >= 0.8   && < 0.9+               , transformers         >= 0.2   && < 0.3+               , monad-peel           >= 0.1   && < 0.2   exposed-modules: Foreign.Ptr.Region                    Foreign.Ptr.Region.Unsafe                    Foreign.Marshal.Alloc.Region