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.3 2001/08/17 12:50:34 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 )
49 -- allocate space for storable type
51 malloc :: Storable a => IO (Ptr a)
52 malloc = doMalloc undefined
54 doMalloc :: Storable a => a -> IO (Ptr a)
55 doMalloc dummy = mallocBytes (sizeOf dummy)
57 -- allocate given number of bytes of storage
59 mallocBytes :: Int -> IO (Ptr a)
60 mallocBytes size = failWhenNULL "malloc" (_malloc (fromIntegral size))
62 -- temporarily allocate space for a storable type
64 -- * the pointer passed as an argument to the function must *not* escape from
65 -- this function; in other words, in `alloca f' the allocated storage must
66 -- not be used after `f' returns
68 alloca :: Storable a => (Ptr a -> IO b) -> IO b
69 alloca = doAlloca undefined
71 doAlloca :: Storable a => a -> (Ptr a -> IO b) -> IO b
72 doAlloca dummy = allocaBytes (sizeOf dummy)
74 -- temporarily allocate the given number of bytes of storage
76 -- * the pointer passed as an argument to the function must *not* escape from
77 -- this function; in other words, in `allocaBytes n f' the allocated storage
78 -- must not be used after `f' returns
80 #ifdef __GLASGOW_HASKELL__
81 allocaBytes :: Int -> (Ptr a -> IO b) -> IO b
82 allocaBytes (I# size) action = IO $ \ s ->
83 case newPinnedByteArray# size s of { (# s, mbarr# #) ->
84 case unsafeFreezeByteArray# mbarr# s of { (# s, barr# #) ->
85 let addr = Ptr (byteArrayContents# barr#) in
86 case action addr of { IO action ->
87 case action s of { (# s, r #) ->
88 case touch# barr# s of { s ->
92 allocaBytes :: Int -> (Ptr a -> IO b) -> IO b
93 allocaBytes size = bracket (mallocBytes size) free
96 -- adjust a malloc'ed storage area to the given size
98 reallocBytes :: Ptr a -> Int -> IO (Ptr a)
99 reallocBytes ptr size =
100 failWhenNULL "realloc" (_realloc ptr (fromIntegral size))
102 -- free malloc'ed storage
104 free :: Ptr a -> IO ()
108 -- auxilliary routines
109 -- -------------------
111 -- asserts that the pointer returned from the action in the second argument is
114 failWhenNULL :: String -> IO (Ptr a) -> IO (Ptr a)
115 failWhenNULL name f = do
118 #ifdef __GLASGOW_HASKELL__
119 then ioException (IOError Nothing ResourceExhausted name
120 "out of memory" Nothing)
122 then ioError (userError (name++": out of memory"))
126 -- basic C routines needed for memory allocation
128 foreign import "malloc" unsafe _malloc :: CSize -> IO (Ptr a)
129 foreign import "realloc" unsafe _realloc :: Ptr a -> CSize -> IO (Ptr a)
130 foreign import "free" unsafe _free :: Ptr a -> IO ()