4a86c9400a684fae2525ce4a60fa8db2c91be911
[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   , newForeignPtr_
46   , addForeignPtrFinalizer
47   , withForeignPtr
48   , unsafeForeignPtrToPtr
49   , touchForeignPtr
50   , castForeignPtr
51   , Storable(sizeOf)
52   , malloc, mallocBytes, finalizerFree
53   )
54 #endif
55
56 #ifdef __HUGS__
57 import Hugs.ForeignPtr
58 #endif
59
60 #ifndef __NHC__
61 import Foreign.Storable ( Storable(sizeOf) )
62 #endif
63
64 #ifdef __GLASGOW_HASKELL__
65 import GHC.Base
66 import GHC.IOBase
67 import GHC.Num
68 import GHC.Err          ( undefined )
69 import GHC.ForeignPtr
70 #endif
71
72 #if !defined(__NHC__) && !defined(__GLASGOW_HASKELL__)
73 import Foreign.Marshal.Alloc    ( malloc, mallocBytes, finalizerFree )
74 import Data.Dynamic
75
76 #include "Typeable.h"
77 INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr")
78
79 instance Eq (ForeignPtr a) where 
80     p == q  =  unsafeForeignPtrToPtr p == unsafeForeignPtrToPtr q
81
82 instance Ord (ForeignPtr a) where 
83     compare p q  =  compare (unsafeForeignPtrToPtr p) (unsafeForeignPtrToPtr q)
84
85 instance Show (ForeignPtr a) where
86     showsPrec p f = showsPrec p (unsafeForeignPtrToPtr f)
87 #endif
88
89
90 #ifndef __NHC__
91 newForeignPtr :: Ptr a -> FinalizerPtr a -> IO (ForeignPtr a)
92 -- ^Turns a plain memory reference into a foreign pointer, and
93 -- associates a finaliser with the reference.  The finaliser will be executed
94 -- after the last reference to the foreign object is dropped.  Note that there
95 -- is no guarantee on how soon the finaliser is executed after the last
96 -- reference was dropped; this depends on the details of the Haskell storage
97 -- manager. The only guarantee is that the finaliser runs before the program
98 -- terminates.
99 newForeignPtr p finalizer
100   = do fObj <- newForeignPtr_ p
101        addForeignPtrFinalizer fObj finalizer
102        return fObj
103
104 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
105 -- ^This is a way to look at the pointer living inside a
106 -- foreign object.  This function takes a function which is
107 -- applied to that pointer. The resulting 'IO' action is then
108 -- executed. The foreign object is kept alive at least during
109 -- the whole action, even if it is not used directly
110 -- inside. Note that it is not safe to return the pointer from
111 -- the action and use it after the action completes. All uses
112 -- of the pointer should be inside the
113 -- 'withForeignPtr' bracket.  The reason for
114 -- this unsafety is the same as for
115 -- 'unsafeForeignPtrToPtr' below: the finalizer
116 -- may run earlier than expected, because the compiler can only
117 -- track usage of the 'ForeignPtr' object, not
118 -- a 'Ptr' object made from it.
119 --
120 -- This function is normally used for marshalling data to
121 -- or from the object pointed to by the
122 -- 'ForeignPtr', using the operations from the
123 -- 'Storable' class.
124 withForeignPtr fo io
125   = do r <- io (unsafeForeignPtrToPtr fo)
126        touchForeignPtr fo
127        return r
128 #endif /* ! __NHC__ */
129
130 #ifndef __GLASGOW_HASKELL__
131 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
132 mallocForeignPtr = do
133   r <- malloc
134   newForeignPtr r finalizerFree
135
136 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
137 mallocForeignPtrBytes n = do
138   r <- mallocBytes n
139   newForeignPtr r finalizerFree
140 #endif /* __HUGS__ || __NHC__ */
141
142 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
143 mallocForeignPtrArray  = doMalloc undefined
144   where
145     doMalloc            :: Storable a => a -> Int -> IO (ForeignPtr a)
146     doMalloc dummy size  = mallocForeignPtrBytes (size * sizeOf dummy)
147
148 mallocForeignPtrArray0      :: Storable a => Int -> IO (ForeignPtr a)
149 mallocForeignPtrArray0 size  = mallocForeignPtrArray (size + 1)