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