[project @ 2003-06-12 10:59:00 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   , 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 "Dynamic.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 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
92 -- ^This is a way to look at the pointer living inside a
93 -- foreign object.  This function takes a function which is
94 -- applied to that pointer. The resulting 'IO' action is then
95 -- executed. The foreign object is kept alive at least during
96 -- the whole action, even if it is not used directly
97 -- inside. Note that it is not safe to return the pointer from
98 -- the action and use it after the action completes. All uses
99 -- of the pointer should be inside the
100 -- 'withForeignPtr' bracket.  The reason for
101 -- this unsafety is the same as for
102 -- 'unsafeForeignPtrToPtr' below: the finalizer
103 -- may run earlier than expected, because the compiler can only
104 -- track usage of the 'ForeignPtr' object, not
105 -- a 'Ptr' object made from it.
106 --
107 -- This function is normally used for marshalling data to
108 -- or from the object pointed to by the
109 -- 'ForeignPtr', using the operations from the
110 -- 'Storable' class.
111 withForeignPtr fo io
112   = do r <- io (unsafeForeignPtrToPtr fo)
113        touchForeignPtr fo
114        return r
115 #endif /* ! __NHC__ */
116
117 #ifdef __HUGS__
118 -- temporary aliasing until hugs catches up
119 unsafeForeignPtrToPtr = foreignPtrToPtr
120 #endif
121
122 #ifndef __GLASGOW_HASKELL__
123 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
124 mallocForeignPtr = do
125   r <- malloc
126   newForeignPtr r finalizerFree
127
128 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
129 mallocForeignPtrBytes n = do
130   r <- mallocBytes n
131   newForeignPtr r finalizerFree
132 #endif /* __HUGS__ || __NHC__ */
133
134 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
135 mallocForeignPtrArray  = doMalloc undefined
136   where
137     doMalloc            :: Storable a => a -> Int -> IO (ForeignPtr a)
138     doMalloc dummy size  = mallocForeignPtrBytes (size * sizeOf dummy)
139
140 mallocForeignPtrArray0      :: Storable a => Int -> IO (ForeignPtr a)
141 mallocForeignPtrArray0 size  = mallocForeignPtrArray (size + 1)