[project @ 2003-03-27 10:24:43 by simonmar]
[haskell-directory.git] / Foreign / ForeignPtr.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Foreign.ForeignPtr
5 -- Copyright   :  (c) The University of Glasgow 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 -- The 'ForeignPtr' type and operations.  This module is part of the
13 -- Foreign Function Interface (FFI) and will usually be imported via
14 -- the "Foreign" module.
15 --
16 -----------------------------------------------------------------------------
17
18 module Foreign.ForeignPtr
19         ( 
20         -- * Finalised data pointers
21           ForeignPtr
22         , newForeignPtr
23         , addForeignPtrFinalizer
24         , withForeignPtr
25         , foreignPtrToPtr
26         , touchForeignPtr
27         , castForeignPtr
28
29 #ifndef __NHC__
30         , mallocForeignPtr
31         , mallocForeignPtrBytes
32         , mallocForeignPtrArray
33         , mallocForeignPtrArray0
34 #endif
35         ) 
36         where
37
38 #ifdef __NHC__
39 import NHC.FFI
40   ( ForeignPtr
41   , newForeignPtr
42   , addForeignPtrFinalizer
43   , withForeignPtr
44   , foreignPtrToPtr
45   , touchForeignPtr
46   , castForeignPtr
47   )
48 #endif
49
50 #ifdef __GLASGOW_HASKELL__
51 import GHC.ForeignPtr
52 #endif
53
54 #ifdef __HUGS__
55 import Hugs.ForeignPtr
56 #endif
57
58 #ifndef __NHC__
59 import Foreign.Storable ( Storable(sizeOf) )
60 #endif
61
62 #ifdef __GLASGOW_HASKELL__
63 import GHC.Base
64 import GHC.IOBase
65 import GHC.Num
66 import GHC.Err          ( undefined )
67 #endif
68
69 #ifndef __NHC__
70 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
71 mallocForeignPtrArray  = doMalloc undefined
72   where
73     doMalloc            :: Storable a => a -> Int -> IO (ForeignPtr a)
74     doMalloc dummy size  = mallocForeignPtrBytes (size * sizeOf dummy)
75
76 mallocForeignPtrArray0      :: Storable a => Int -> IO (ForeignPtr a)
77 mallocForeignPtrArray0 size  = mallocForeignPtrArray (size + 1)
78 #endif