[project @ 2003-06-12 10:55:57 by malcolm]
[ghc-base.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         , FinalizerPtr
23         , newForeignPtr
24         , newForeignPtr_
25         , addForeignPtrFinalizer
26         , withForeignPtr
27         , unsafeForeignPtrToPtr
28         , touchForeignPtr
29         , castForeignPtr
30
31         , mallocForeignPtr
32         , mallocForeignPtrBytes
33         , mallocForeignPtrArray
34         , mallocForeignPtrArray0
35         ) 
36         where
37
38 import Foreign.Ptr
39
40 #ifdef __NHC__
41 import NHC.FFI
42   ( ForeignPtr
43   , FinalizerPtr
44   , newForeignPtr
45   , addForeignPtrFinalizer
46   , withForeignPtr
47   , unsafeForeignPtrToPtr
48   , touchForeignPtr
49   , castForeignPtr
50   , Storable(sizeOf)
51   , malloc, mallocBytes, finalizerFree
52   )
53 #endif
54
55 #ifdef __HUGS__
56 import Hugs.ForeignPtr
57 #endif
58
59 #ifndef __NHC__
60 import Foreign.Storable ( Storable(sizeOf) )
61 #endif
62
63 #ifdef __GLASGOW_HASKELL__
64 import GHC.Base
65 import GHC.IOBase
66 import GHC.Num
67 import GHC.Err          ( undefined )
68 import GHC.ForeignPtr
69 #endif
70
71 #if !defined(__NHC__) && !defined(__GLASGOW_HASKELL__)
72 import Foreign.Marshal.Alloc    ( malloc, mallocBytes, finalizerFree )
73 import Data.Dynamic
74
75 #include "Dynamic.h"
76 INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr")
77
78 instance Eq (ForeignPtr a) where 
79     p == q  =  unsafeForeignPtrToPtr p == unsafeForeignPtrToPtr q
80
81 instance Ord (ForeignPtr a) where 
82     compare p q  =  compare (unsafeForeignPtrToPtr p) (unsafeForeignPtrToPtr q)
83
84 instance Show (ForeignPtr a) where
85     showsPrec p f = showsPrec p (unsafeForeignPtrToPtr f)
86 #endif
87
88
89 #ifndef __NHC__
90 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
91 -- ^This is a way to look at the pointer living inside a
92 -- foreign object.  This function takes a function which is
93 -- applied to that pointer. The resulting 'IO' action is then
94 -- executed. The foreign object is kept alive at least during
95 -- the whole action, even if it is not used directly
96 -- inside. Note that it is not safe to return the pointer from
97 -- the action and use it after the action completes. All uses
98 -- of the pointer should be inside the
99 -- 'withForeignPtr' bracket.  The reason for
100 -- this unsafety is the same as for
101 -- 'unsafeForeignPtrToPtr' below: the finalizer
102 -- may run earlier than expected, because the compiler can only
103 -- track usage of the 'ForeignPtr' object, not
104 -- a 'Ptr' object made from it.
105 --
106 -- This function is normally used for marshalling data to
107 -- or from the object pointed to by the
108 -- 'ForeignPtr', using the operations from the
109 -- 'Storable' class.
110 withForeignPtr fo io
111   = do r <- io (unsafeForeignPtrToPtr fo)
112        touchForeignPtr fo
113        return r
114 #endif /* ! __NHC__ */
115
116 #ifdef __HUGS__
117 -- temporary aliasing until hugs catches up
118 unsafeForeignPtrToPtr = foreignPtrToPtr
119 #endif
120
121 #ifndef __GLASGOW_HASKELL__
122 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
123 mallocForeignPtr = do
124   r <- malloc
125   newForeignPtr r finalizerFree
126
127 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
128 mallocForeignPtrBytes n = do
129   r <- mallocBytes n
130   newForeignPtr r finalizerFree
131 #endif /* __HUGS__ || __NHC__ */
132
133 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
134 mallocForeignPtrArray  = doMalloc undefined
135   where
136     doMalloc            :: Storable a => a -> Int -> IO (ForeignPtr a)
137     doMalloc dummy size  = mallocForeignPtrBytes (size * sizeOf dummy)
138
139 mallocForeignPtrArray0      :: Storable a => Int -> IO (ForeignPtr a)
140 mallocForeignPtrArray0 size  = mallocForeignPtrArray (size + 1)