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