65294cefea71b469d8e45e37640bdeb65c14258f
[packages/old-time.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/base/LICENSE)
7 --
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
11 --
12 -- Marshalling support: basic routines for memory allocation
13 --
14 -----------------------------------------------------------------------------
15
16 module Foreign.Marshal.Alloc (
17 -- * Allocation
18 malloc, -- :: Storable a => IO (Ptr a)
19 mallocBytes, -- :: Int -> IO (Ptr a)
20
21 alloca, -- :: Storable a => (Ptr a -> IO b) -> IO b
22 allocaBytes, -- :: Int -> (Ptr a -> IO b) -> IO b
23
24 realloc, -- :: Storable b => Ptr a -> IO (Ptr b)
25 reallocBytes, -- :: Ptr a -> Int -> IO (Ptr a)
26
27 free, -- :: Ptr a -> IO ()
28 finalizerFree -- :: FinalizerPtr a
29 ) where
30
31 import Data.Maybe
32 import Foreign.Ptr ( Ptr, nullPtr, FunPtr )
33 import Foreign.ForeignPtr ( FinalizerPtr )
34 import Foreign.C.Types ( CSize )
35 import Foreign.Storable ( Storable(sizeOf) )
36
37 #ifdef __GLASGOW_HASKELL__
38 import GHC.IOBase
39 import GHC.Real
40 import GHC.Ptr
41 import GHC.Err
42 import GHC.Base
43 #elif defined(__NHC__)
44 import IO ( bracket )
45 #else
46 import Control.Exception ( bracket )
47 #endif
48
49
50 -- exported functions
51 -- ------------------
52
53 -- |Allocate space for storable type. The size of the area allocated
54 -- is determined by the 'sizeOf' method from the instance of
55 -- 'Storable' for the appropriate type.
56 --
57 malloc :: Storable a => IO (Ptr a)
58 malloc = doMalloc undefined
59 where
60 doMalloc :: Storable a => a -> IO (Ptr a)
61 doMalloc dummy = mallocBytes (sizeOf dummy)
62
63 -- |Allocate given number of bytes of storage, equivalent to C\'s @malloc()@.
64 --
65 mallocBytes :: Int -> IO (Ptr a)
66 mallocBytes size = failWhenNULL "malloc" (_malloc (fromIntegral size))
67
68 -- |Temporarily allocate space for a storable type.
69 --
70 -- * the pointer passed as an argument to the function must /not/ escape from
71 -- this function; in other words, in @alloca f@ the allocated storage must
72 -- not be used after @f@ returns
73 --
74 alloca :: Storable a => (Ptr a -> IO b) -> IO b
75 alloca = doAlloca undefined
76 where
77 doAlloca :: Storable a => a -> (Ptr a -> IO b) -> IO b
78 doAlloca dummy = allocaBytes (sizeOf dummy)
79
80 -- |Temporarily allocate the given number of bytes of storage.
81 --
82 -- * the pointer passed as an argument to the function must /not/ escape from
83 -- this function; in other words, in @allocaBytes n f@ the allocated storage
84 -- must not be used after @f@ returns
85 --
86 #ifdef __GLASGOW_HASKELL__
87 allocaBytes :: Int -> (Ptr a -> IO b) -> IO b
88 allocaBytes (I# size) action = IO $ \ s ->
89 case newPinnedByteArray# size s of { (# s, mbarr# #) ->
90 case unsafeFreezeByteArray# mbarr# s of { (# s, barr# #) ->
91 let addr = Ptr (byteArrayContents# barr#) in
92 case action addr of { IO action ->
93 case action s of { (# s, r #) ->
94 case touch# barr# s of { s ->
95 (# s, r #)
96 }}}}}
97 #else
98 allocaBytes :: Int -> (Ptr a -> IO b) -> IO b
99 allocaBytes size = bracket (mallocBytes size) free
100 #endif
101
102 -- |Adjust a malloc\'ed storage area to the given size of the required type
103 -- (corresponds to C\'s @realloc()@).
104 --
105 realloc :: Storable b => Ptr a -> IO (Ptr b)
106 realloc = doRealloc undefined
107 where
108 doRealloc :: Storable b => b -> Ptr a -> IO (Ptr b)
109 doRealloc dummy ptr = let
110 size = fromIntegral (sizeOf dummy)
111 in
112 failWhenNULL "realloc" (_realloc ptr size)
113
114 -- |Adjust a malloc\'ed storage area to the given size (equivalent to
115 -- C\'s @realloc()@).
116 --
117 reallocBytes :: Ptr a -> Int -> IO (Ptr a)
118 reallocBytes ptr size =
119 failWhenNULL "realloc" (_realloc ptr (fromIntegral size))
120
121 -- |Free malloc\'ed storage (equivalent to
122 -- C\'s @free()@)
123 --
124 free :: Ptr a -> IO ()
125 free = _free
126
127
128 -- auxilliary routines
129 -- -------------------
130
131 -- asserts that the pointer returned from the action in the second argument is
132 -- non-null
133 --
134 failWhenNULL :: String -> IO (Ptr a) -> IO (Ptr a)
135 failWhenNULL name f = do
136 addr <- f
137 if addr == nullPtr
138 #ifdef __GLASGOW_HASKELL__
139 then ioException (IOError Nothing ResourceExhausted name
140 "out of memory" Nothing)
141 #else
142 then ioError (userError (name++": out of memory"))
143 #endif
144 else return addr
145
146 -- basic C routines needed for memory allocation
147 --
148 foreign import ccall unsafe "stdlib.h malloc" _malloc :: CSize -> IO (Ptr a)
149 foreign import ccall unsafe "stdlib.h realloc" _realloc :: Ptr a -> CSize -> IO (Ptr b)
150 foreign import ccall unsafe "stdlib.h free" _free :: Ptr a -> IO ()
151
152 -- | A pointer to a foreign function equivalent to 'free', which may be used
153 -- as a finalizer for storage allocated with 'malloc' or 'mallocBytes'.
154 foreign import ccall unsafe "stdlib.h &free" finalizerFree :: FinalizerPtr a