[project @ 2001-08-17 12:50:34 by simonmar]
[ghc-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.3 2001/08/17 12:50:34 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 import GHC.Prim
43 #endif
44
45
46 -- exported functions
47 -- ------------------
48
49 -- allocate space for storable type
50 --
51 malloc :: Storable a => IO (Ptr a)
52 malloc  = doMalloc undefined
53   where
54     doMalloc       :: Storable a => a -> IO (Ptr a)
55     doMalloc dummy  = mallocBytes (sizeOf dummy)
56
57 -- allocate given number of bytes of storage
58 --
59 mallocBytes      :: Int -> IO (Ptr a)
60 mallocBytes size  = failWhenNULL "malloc" (_malloc (fromIntegral size))
61
62 -- temporarily allocate space for a storable type
63 --
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
67 --
68 alloca :: Storable a => (Ptr a -> IO b) -> IO b
69 alloca  = doAlloca undefined
70   where
71     doAlloca       :: Storable a => a -> (Ptr a -> IO b) -> IO b
72     doAlloca dummy  = allocaBytes (sizeOf dummy)
73
74 -- temporarily allocate the given number of bytes of storage
75 --
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
79 --
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 ->
89      (# s, r #)
90   }}}}}
91 #else
92 allocaBytes      :: Int -> (Ptr a -> IO b) -> IO b
93 allocaBytes size  = bracket (mallocBytes size) free
94 #endif
95
96 -- adjust a malloc'ed storage area to the given size
97 --
98 reallocBytes          :: Ptr a -> Int -> IO (Ptr a)
99 reallocBytes ptr size  = 
100   failWhenNULL "realloc" (_realloc ptr (fromIntegral size))
101
102 -- free malloc'ed storage
103 --
104 free :: Ptr a -> IO ()
105 free  = _free
106
107
108 -- auxilliary routines
109 -- -------------------
110
111 -- asserts that the pointer returned from the action in the second argument is
112 -- non-null
113 --
114 failWhenNULL :: String -> IO (Ptr a) -> IO (Ptr a)
115 failWhenNULL name f = do
116    addr <- f
117    if addr == nullPtr
118 #ifdef __GLASGOW_HASKELL__
119       then ioException (IOError Nothing ResourceExhausted name 
120                                         "out of memory" Nothing)
121 #else
122       then ioError (userError (name++": out of memory"))
123 #endif
124       else return addr
125
126 -- basic C routines needed for memory allocation
127 --
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 ()