1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Foreign.Marshal.Alloc
5 -- Copyright : (c) The FFI task force 2001
6 -- License : BSD-style (see the file libraries/core/LICENSE)
8 -- Maintainer : ffi@haskell.org
9 -- Stability : experimental
10 -- Portability : non-portable
12 -- $Id: Alloc.hs,v 1.1 2001/06/28 14:15:03 simonmar Exp $
14 -- Marshalling support: basic routines for memory allocation
16 -----------------------------------------------------------------------------
18 module Foreign.Marshal.Alloc (
19 malloc, -- :: Storable a => IO (Ptr a)
20 mallocBytes, -- :: Int -> IO (Ptr a)
22 alloca, -- :: Storable a => (Ptr a -> IO b) -> IO b
23 allocaBytes, -- :: Int -> (Ptr a -> IO b) -> IO b
25 reallocBytes, -- :: Ptr a -> Int -> IO (Ptr a)
27 free -- :: Ptr a -> IO ()
31 import Foreign.Ptr ( Ptr, nullPtr )
32 import Foreign.C.TypesISO ( CSize )
34 #ifdef __GLASGOW_HASKELL__
35 import GHC.Exception ( bracket )
36 import GHC.Storable ( Storable(sizeOf) )
47 -- allocate space for storable type
49 malloc :: Storable a => IO (Ptr a)
50 malloc = doMalloc undefined
52 doMalloc :: Storable a => a -> IO (Ptr a)
53 doMalloc dummy = mallocBytes (sizeOf dummy)
55 -- allocate given number of bytes of storage
57 mallocBytes :: Int -> IO (Ptr a)
58 mallocBytes size = failWhenNULL "malloc" (_malloc (fromIntegral size))
60 -- temporarily allocate space for a storable type
62 -- * the pointer passed as an argument to the function must *not* escape from
63 -- this function; in other words, in `alloca f' the allocated storage must
64 -- not be used after `f' returns
66 alloca :: Storable a => (Ptr a -> IO b) -> IO b
67 alloca = doAlloca undefined
69 doAlloca :: Storable a => a -> (Ptr a -> IO b) -> IO b
70 doAlloca dummy = allocaBytes (sizeOf dummy)
72 -- temporarily allocate the given number of bytes of storage
74 -- * the pointer passed as an argument to the function must *not* escape from
75 -- this function; in other words, in `allocaBytes n f' the allocated storage
76 -- must not be used after `f' returns
78 allocaBytes :: Int -> (Ptr a -> IO b) -> IO b
79 allocaBytes size = bracket (mallocBytes size) free
81 -- adjust a malloc'ed storage area to the given size
83 reallocBytes :: Ptr a -> Int -> IO (Ptr a)
84 reallocBytes ptr size =
85 failWhenNULL "realloc" (_realloc ptr (fromIntegral size))
87 -- free malloc'ed storage
89 free :: Ptr a -> IO ()
93 -- auxilliary routines
94 -- -------------------
96 -- asserts that the pointer returned from the action in the second argument is
99 failWhenNULL :: String -> IO (Ptr a) -> IO (Ptr a)
100 failWhenNULL name f = do
103 #ifdef __GLASGOW_HASKELL__
104 then ioException (IOError Nothing ResourceExhausted name
105 "out of memory" Nothing)
107 then ioError (userError (name++": out of memory"))
111 -- basic C routines needed for memory allocation
113 foreign import "malloc" unsafe _malloc :: CSize -> IO (Ptr a)
114 foreign import "realloc" unsafe _realloc :: Ptr a -> CSize -> IO (Ptr a)
115 foreign import "free" unsafe _free :: Ptr a -> IO ()