Remove Hugs98 specific code
[packages/base.git] / Foreign / ForeignPtr / Imp.hs
1 {-# LANGUAGE Unsafe #-}
2 {-# LANGUAGE CPP, NoImplicitPrelude #-}
3 {-# OPTIONS_HADDOCK hide #-}
4
5 -----------------------------------------------------------------------------
6 -- |
7 -- Module : Foreign.ForeignPtr.Imp
8 -- Copyright : (c) The University of Glasgow 2001
9 -- License : BSD-style (see the file libraries/base/LICENSE)
10 --
11 -- Maintainer : ffi@haskell.org
12 -- Stability : provisional
13 -- Portability : portable
14 --
15 -- The 'ForeignPtr' type and operations. This module is part of the
16 -- Foreign Function Interface (FFI) and will usually be imported via
17 -- the "Foreign" module.
18 --
19 -----------------------------------------------------------------------------
20
21 module Foreign.ForeignPtr.Imp
22 (
23 -- * Finalised data pointers
24 ForeignPtr
25 , FinalizerPtr
26 #ifdef __GLASGOW_HASKELL__
27 , FinalizerEnvPtr
28 #endif
29 -- ** Basic operations
30 , newForeignPtr
31 , newForeignPtr_
32 , addForeignPtrFinalizer
33 #ifdef __GLASGOW_HASKELL__
34 , newForeignPtrEnv
35 , addForeignPtrFinalizerEnv
36 #endif
37 , withForeignPtr
38
39 #ifdef __GLASGOW_HASKELL__
40 , finalizeForeignPtr
41 #endif
42
43 -- ** Low-level operations
44 , unsafeForeignPtrToPtr
45 , touchForeignPtr
46 , castForeignPtr
47
48 -- ** Allocating managed memory
49 , mallocForeignPtr
50 , mallocForeignPtrBytes
51 , mallocForeignPtrArray
52 , mallocForeignPtrArray0
53 )
54 where
55
56 import Foreign.Ptr
57 import Foreign.Storable ( Storable(sizeOf) )
58
59 #ifdef __GLASGOW_HASKELL__
60 import GHC.Base
61 import GHC.Num
62 import GHC.ForeignPtr
63 #endif
64
65 #if !defined(__GLASGOW_HASKELL__)
66 import Foreign.Marshal.Alloc ( malloc, mallocBytes, finalizerFree )
67
68 instance Eq (ForeignPtr a) where
69 p == q = unsafeForeignPtrToPtr p == unsafeForeignPtrToPtr q
70
71 instance Ord (ForeignPtr a) where
72 compare p q = compare (unsafeForeignPtrToPtr p) (unsafeForeignPtrToPtr q)
73
74 instance Show (ForeignPtr a) where
75 showsPrec p f = showsPrec p (unsafeForeignPtrToPtr f)
76 #endif
77
78
79 newForeignPtr :: FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
80 -- ^Turns a plain memory reference into a foreign pointer, and
81 -- associates a finalizer with the reference. The finalizer will be
82 -- executed after the last reference to the foreign object is dropped.
83 -- There is no guarantee of promptness, however the finalizer will be
84 -- executed before the program exits.
85 newForeignPtr finalizer p
86 = do fObj <- newForeignPtr_ p
87 addForeignPtrFinalizer finalizer fObj
88 return fObj
89
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 unsafeness 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
115 #ifdef __GLASGOW_HASKELL__
116 -- | This variant of 'newForeignPtr' adds a finalizer that expects an
117 -- environment in addition to the finalized pointer. The environment
118 -- that will be passed to the finalizer is fixed by the second argument to
119 -- 'newForeignPtrEnv'.
120 newForeignPtrEnv ::
121 FinalizerEnvPtr env a -> Ptr env -> Ptr a -> IO (ForeignPtr a)
122 newForeignPtrEnv finalizer env p
123 = do fObj <- newForeignPtr_ p
124 addForeignPtrFinalizerEnv finalizer env fObj
125 return fObj
126 #endif
127
128 #ifndef __GLASGOW_HASKELL__
129 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
130 mallocForeignPtr = do
131 r <- malloc
132 newForeignPtr finalizerFree r
133
134 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
135 mallocForeignPtrBytes n = do
136 r <- mallocBytes n
137 newForeignPtr finalizerFree r
138 #endif /* !__GLASGOW_HASKELL__ */
139
140 -- | This function is similar to 'Foreign.Marshal.Array.mallocArray',
141 -- but yields a memory area that has a finalizer attached that releases
142 -- the memory area. As with 'mallocForeignPtr', it is not guaranteed that
143 -- the block of memory was allocated by 'Foreign.Marshal.Alloc.malloc'.
144 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
145 mallocForeignPtrArray = doMalloc undefined
146 where
147 doMalloc :: Storable b => b -> Int -> IO (ForeignPtr b)
148 doMalloc dummy size = mallocForeignPtrBytes (size * sizeOf dummy)
149
150 -- | This function is similar to 'Foreign.Marshal.Array.mallocArray0',
151 -- but yields a memory area that has a finalizer attached that releases
152 -- the memory area. As with 'mallocForeignPtr', it is not guaranteed that
153 -- the block of memory was allocated by 'Foreign.Marshal.Alloc.malloc'.
154 mallocForeignPtrArray0 :: Storable a => Int -> IO (ForeignPtr a)
155 mallocForeignPtrArray0 size = mallocForeignPtrArray (size + 1)
156