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 : provisional
10 -- Portability : portable
12 -- $Id: Alloc.hs,v 1.5 2002/02/05 17:32:25 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 )
33 import Foreign.Storable ( Storable(sizeOf) )
35 #ifdef __GLASGOW_HASKELL__
36 import GHC.Exception ( bracket )
48 -- allocate space for storable type
50 malloc :: Storable a => IO (Ptr a)
51 malloc = doMalloc undefined
53 doMalloc :: Storable a => a -> IO (Ptr a)
54 doMalloc dummy = mallocBytes (sizeOf dummy)
56 -- allocate given number of bytes of storage
58 mallocBytes :: Int -> IO (Ptr a)
59 mallocBytes size = failWhenNULL "malloc" (_malloc (fromIntegral size))
61 -- temporarily allocate space for a storable type
63 -- * the pointer passed as an argument to the function must *not* escape from
64 -- this function; in other words, in `alloca f' the allocated storage must
65 -- not be used after `f' returns
67 alloca :: Storable a => (Ptr a -> IO b) -> IO b
68 alloca = doAlloca undefined
70 doAlloca :: Storable a => a -> (Ptr a -> IO b) -> IO b
71 doAlloca dummy = allocaBytes (sizeOf dummy)
73 -- temporarily allocate the given number of bytes of storage
75 -- * the pointer passed as an argument to the function must *not* escape from
76 -- this function; in other words, in `allocaBytes n f' the allocated storage
77 -- must not be used after `f' returns
79 #ifdef __GLASGOW_HASKELL__
80 allocaBytes :: Int -> (Ptr a -> IO b) -> IO b
81 allocaBytes (I# size) action = IO $ \ s ->
82 case newPinnedByteArray# size s of { (# s, mbarr# #) ->
83 case unsafeFreezeByteArray# mbarr# s of { (# s, barr# #) ->
84 let addr = Ptr (byteArrayContents# barr#) in
85 case action addr of { IO action ->
86 case action s of { (# s, r #) ->
87 case touch# barr# s of { s ->
91 allocaBytes :: Int -> (Ptr a -> IO b) -> IO b
92 allocaBytes size = bracket (mallocBytes size) free
95 -- adjust a malloc'ed storage area to the given size
97 reallocBytes :: Ptr a -> Int -> IO (Ptr a)
98 reallocBytes ptr size =
99 failWhenNULL "realloc" (_realloc ptr (fromIntegral size))
101 -- free malloc'ed storage
103 free :: Ptr a -> IO ()
107 -- auxilliary routines
108 -- -------------------
110 -- asserts that the pointer returned from the action in the second argument is
113 failWhenNULL :: String -> IO (Ptr a) -> IO (Ptr a)
114 failWhenNULL name f = do
117 #ifdef __GLASGOW_HASKELL__
118 then ioException (IOError Nothing ResourceExhausted name
119 "out of memory" Nothing)
121 then ioError (userError (name++": out of memory"))
125 -- basic C routines needed for memory allocation
127 foreign import ccall unsafe "malloc" _malloc :: CSize -> IO (Ptr a)
128 foreign import ccall unsafe "realloc" _realloc :: Ptr a -> CSize -> IO (Ptr a)
129 foreign import ccall unsafe "free" _free :: Ptr a -> IO ()