Skip to content

Commit

Permalink
allocaBytes, withForeignPtr
Browse files Browse the repository at this point in the history
  • Loading branch information
snoyberg committed Oct 15, 2010
1 parent d514bca commit 1b6bee1
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Control/Monad/Invert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module Control.Monad.Invert
, bracket_
-- * Memory allocation
, alloca
, allocaBytes
, withForeignPtr
) where

import Prelude hiding (catch)
Expand All @@ -25,6 +27,8 @@ import Data.Monoid (Monoid)
import qualified Foreign.Marshal.Alloc as A
import Foreign.Storable (Storable)
import Foreign.Ptr (Ptr)
import Foreign.ForeignPtr (ForeignPtr)
import qualified Foreign.ForeignPtr as F

class Monad m => MonadInvertIO m where
data InvertedIO m :: * -> *
Expand Down Expand Up @@ -105,3 +109,10 @@ bracket_ acquire cleanup action = revertIO $ \a -> E.bracket_

alloca :: (Storable a, MonadInvertIO m) => (Ptr a -> m b) -> m b
alloca f = revertIO $ \x -> A.alloca $ flip invertIO x . f

allocaBytes :: MonadInvertIO m => Int -> (Ptr a -> m b) -> m b
allocaBytes i f = revertIO $ \x -> A.allocaBytes i $ flip invertIO x . f

withForeignPtr :: MonadInvertIO m => ForeignPtr a -> (Ptr a -> m b) -> m b
withForeignPtr p f =
revertIO $ \x -> F.withForeignPtr p $ flip invertIO x . f

0 comments on commit 1b6bee1

Please sign in to comment.