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 -- Marshalling support: basic routines for memory allocation
14 -----------------------------------------------------------------------------
16 module Foreign.Marshal.Alloc (
18 malloc, -- :: Storable a => IO (Ptr a)
19 mallocBytes, -- :: Int -> IO (Ptr a)
21 alloca, -- :: Storable a => (Ptr a -> IO b) -> IO b
22 allocaBytes, -- :: Int -> (Ptr a -> IO b) -> IO b
24 reallocBytes, -- :: Ptr a -> Int -> IO (Ptr a)
26 free -- :: Ptr a -> IO ()
30 import Foreign.Ptr ( Ptr, nullPtr )
31 import Foreign.C.TypesISO ( CSize )
32 import Foreign.Storable ( Storable(sizeOf) )
34 #ifdef __GLASGOW_HASKELL__
35 import GHC.Exception ( bracket )
47 -- |Allocate space for storable type. The size of the area allocated
48 -- is determined by the 'sizeOf' method from the instance of
49 -- 'Storable' for the appropriate 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, equivalent to C\'s @malloc()@.
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 (equivalent to
99 reallocBytes :: Ptr a -> Int -> IO (Ptr a)
100 reallocBytes ptr size =
101 failWhenNULL "realloc" (_realloc ptr (fromIntegral size))
103 -- |Free malloc\'ed storage (equivalent to
106 free :: Ptr a -> IO ()
110 -- auxilliary routines
111 -- -------------------
113 -- asserts that the pointer returned from the action in the second argument is
116 failWhenNULL :: String -> IO (Ptr a) -> IO (Ptr a)
117 failWhenNULL name f = do
120 #ifdef __GLASGOW_HASKELL__
121 then ioException (IOError Nothing ResourceExhausted name
122 "out of memory" Nothing)
124 then ioError (userError (name++": out of memory"))
128 -- basic C routines needed for memory allocation
130 foreign import ccall unsafe "malloc" _malloc :: CSize -> IO (Ptr a)
131 foreign import ccall unsafe "realloc" _realloc :: Ptr a -> CSize -> IO (Ptr a)
132 foreign import ccall unsafe "free" _free :: Ptr a -> IO ()