[project @ 2003-06-12 10:53:15 by simonmar]
[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 #ifndef __NHC__
32         , mallocForeignPtr
33         , mallocForeignPtrBytes
34         , mallocForeignPtrArray
35         , mallocForeignPtrArray0
36 #endif
37         ) 
38         where
39
40 import Foreign.Ptr
41
42 #ifdef __NHC__
43 import NHC.FFI
44   ( ForeignPtr
45   , FinalizerPtr
46   , newForeignPtr
47   , addForeignPtrFinalizer
48   , withForeignPtr
49   , unsafeForeignPtrToPtr
50   , touchForeignPtr
51   , castForeignPtr
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 #ifdef __HUGS__
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__ */
132
133 #ifndef __NHC__
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)
142 #endif