-{-# OPTIONS -fno-implicit-prelude #-}
+{-# OPTIONS_GHC -XNoImplicitPrelude #-}
-----------------------------------------------------------------------------
-- |
-- Module : Foreign.Marshal.Alloc
mallocBytes, -- :: Int -> IO (Ptr a)
realloc, -- :: Storable b => Ptr a -> IO (Ptr b)
- reallocBytes, -- :: Ptr a -> Int -> IO (Ptr a)
+ reallocBytes, -- :: Ptr a -> Int -> IO (Ptr a)
free, -- :: Ptr a -> IO ()
finalizerFree -- :: FinalizerPtr a
) where
import Data.Maybe
-import Foreign.Ptr ( Ptr, nullPtr, FunPtr )
-import Foreign.C.Types ( CSize )
-import Foreign.Storable ( Storable(sizeOf) )
+import Foreign.Ptr ( Ptr, nullPtr, FunPtr )
+import Foreign.C.Types ( CSize )
+import Foreign.Storable ( Storable(sizeOf) )
#ifdef __GLASGOW_HASKELL__
-import Foreign.ForeignPtr ( FinalizerPtr )
+import Foreign.ForeignPtr ( FinalizerPtr )
import GHC.IOBase
import GHC.Real
import GHC.Ptr
import GHC.Base
import GHC.Num
#elif defined(__NHC__)
-import NHC.FFI ( FinalizerPtr, CInt(..) )
-import IO ( bracket )
+import NHC.FFI ( FinalizerPtr, CInt(..) )
+import IO ( bracket )
#else
-import Control.Exception ( bracket )
+import Control.Exception ( bracket )
#endif
#ifdef __HUGS__
-import Hugs.ForeignPtr ( FinalizerPtr )
+import Hugs.Prelude ( IOException(IOError),
+ IOErrorType(ResourceExhausted) )
+import Hugs.ForeignPtr ( FinalizerPtr )
#endif
malloc :: Storable a => IO (Ptr a)
malloc = doMalloc undefined
where
- doMalloc :: Storable a => a -> IO (Ptr a)
+ doMalloc :: Storable b => b -> IO (Ptr b)
doMalloc dummy = mallocBytes (sizeOf dummy)
-- |Allocate a block of memory of the given number of bytes.
alloca :: Storable a => (Ptr a -> IO b) -> IO b
alloca = doAlloca undefined
where
- doAlloca :: Storable a => a -> (Ptr a -> IO b) -> IO b
+ doAlloca :: Storable a' => a' -> (Ptr a' -> IO b') -> IO b'
doAlloca dummy = allocaBytes (sizeOf dummy)
-- |@'allocaBytes' n f@ executes the computation @f@, passing as argument
realloc :: Storable b => Ptr a -> IO (Ptr b)
realloc = doRealloc undefined
where
- doRealloc :: Storable b => b -> Ptr a -> IO (Ptr b)
+ doRealloc :: Storable b' => b' -> Ptr a' -> IO (Ptr b')
doRealloc dummy ptr = let
- size = fromIntegral (sizeOf dummy)
- in
- failWhenNULL "realloc" (_realloc ptr size)
+ size = fromIntegral (sizeOf dummy)
+ in
+ failWhenNULL "realloc" (_realloc ptr size)
-- |Resize a memory area that was allocated with 'malloc' or 'mallocBytes'
-- to the given size. The returned pointer may refer to an entirely
failWhenNULL name f = do
addr <- f
if addr == nullPtr
-#ifdef __GLASGOW_HASKELL__
- then ioException (IOError Nothing ResourceExhausted name
- "out of memory" Nothing)
+#if __GLASGOW_HASKELL__ || __HUGS__
+ then ioError (IOError Nothing ResourceExhausted name
+ "out of memory" Nothing)
#else
then ioError (userError (name++": out of memory"))
#endif