[project @ 2002-04-24 16:31:37 by simonmar]
[packages/base.git] / Foreign / Marshal / Alloc.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module : Foreign.Marshal.Alloc
5 -- Copyright : (c) The FFI task force 2001
6 -- License : BSD-style (see the file libraries/core/LICENSE)
7 --
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
11 --
12 -- $Id: Alloc.hs,v 1.6 2002/04/24 16:31:44 simonmar Exp $
13 --
14 -- Marshalling support: basic routines for memory allocation
15 --
16 -----------------------------------------------------------------------------
17
18 module Foreign.Marshal.Alloc (
19 malloc, -- :: Storable a => IO (Ptr a)
20 mallocBytes, -- :: Int -> IO (Ptr a)
21
22 alloca, -- :: Storable a => (Ptr a -> IO b) -> IO b
23 allocaBytes, -- :: Int -> (Ptr a -> IO b) -> IO b
24
25 reallocBytes, -- :: Ptr a -> Int -> IO (Ptr a)
26
27 free -- :: Ptr a -> IO ()
28 ) where
29
30 import Data.Maybe
31 import Foreign.Ptr ( Ptr, nullPtr )
32 import Foreign.C.TypesISO ( CSize )
33 import Foreign.Storable ( Storable(sizeOf) )
34
35 #ifdef __GLASGOW_HASKELL__
36 import GHC.Exception ( bracket )
37 import GHC.IOBase
38 import GHC.Real
39 import GHC.Ptr
40 import GHC.Err
41 import GHC.Base
42 #endif
43
44
45 -- exported functions
46 -- ------------------
47
48 -- allocate space for storable type
49 --
50 malloc :: Storable a => IO (Ptr a)
51 malloc = doMalloc undefined
52 where
53 doMalloc :: Storable a => a -> IO (Ptr a)
54 doMalloc dummy = mallocBytes (sizeOf dummy)
55
56 -- allocate given number of bytes of storage
57 --
58 mallocBytes :: Int -> IO (Ptr a)
59 mallocBytes size = failWhenNULL "malloc" (_malloc (fromIntegral size))
60
61 -- temporarily allocate space for a storable type
62 --
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
66 --
67 alloca :: Storable a => (Ptr a -> IO b) -> IO b
68 alloca = doAlloca undefined
69 where
70 doAlloca :: Storable a => a -> (Ptr a -> IO b) -> IO b
71 doAlloca dummy = allocaBytes (sizeOf dummy)
72
73 -- temporarily allocate the given number of bytes of storage
74 --
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
78 --
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 ->
88 (# s, r #)
89 }}}}}
90 #else
91 allocaBytes :: Int -> (Ptr a -> IO b) -> IO b
92 allocaBytes size = bracket (mallocBytes size) free
93 #endif
94
95 -- adjust a malloc'ed storage area to the given size
96 --
97 reallocBytes :: Ptr a -> Int -> IO (Ptr a)
98 reallocBytes ptr size =
99 failWhenNULL "realloc" (_realloc ptr (fromIntegral size))
100
101 -- free malloc'ed storage
102 --
103 free :: Ptr a -> IO ()
104 free = _free
105
106
107 -- auxilliary routines
108 -- -------------------
109
110 -- asserts that the pointer returned from the action in the second argument is
111 -- non-null
112 --
113 failWhenNULL :: String -> IO (Ptr a) -> IO (Ptr a)
114 failWhenNULL name f = do
115 addr <- f
116 if addr == nullPtr
117 #ifdef __GLASGOW_HASKELL__
118 then ioException (IOError Nothing ResourceExhausted name
119 "out of memory" Nothing)
120 #else
121 then ioError (userError (name++": out of memory"))
122 #endif
123 else return addr
124
125 -- basic C routines needed for memory allocation
126 --
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 ()