2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[Foreign]{Module @Foreign@}
8 {-# OPTIONS -fno-implicit-prelude #-}
23 %*********************************************************
25 \subsection{Classes @CCallable@ and @CReturnable@}
27 %*********************************************************
33 instance CCallable Char
34 instance CCallable Char#
35 instance CReturnable Char
37 instance CCallable Int
38 instance CCallable Int#
39 instance CReturnable Int
41 -- DsCCall knows how to pass strings...
42 instance CCallable [Char]
44 instance CCallable Float
45 instance CCallable Float#
46 instance CReturnable Float
48 instance CCallable Double
49 instance CCallable Double#
50 instance CReturnable Double
52 instance CCallable Addr
53 instance CCallable Addr#
54 instance CReturnable Addr
56 instance CCallable Word
57 instance CCallable Word#
58 instance CReturnable Word
61 instance CCallable (MutableByteArray s ix)
62 instance CCallable (MutableByteArray# s)
64 instance CCallable (ByteArray ix)
65 instance CCallable ByteArray#
67 instance CReturnable () -- Why, exactly?
71 %*********************************************************
73 \subsection{Type @ForeignObj@ and its operations}
75 %*********************************************************
78 --Defined in PrelBase: data ForeignObj = ForeignObj ForeignObj#
79 instance CCallable ForeignObj
80 instance CCallable ForeignObj#
82 eqForeignObj :: ForeignObj -> ForeignObj -> Bool
83 makeForeignObj :: Addr -> Addr -> PrimIO ForeignObj
84 writeForeignObj :: ForeignObj -> Addr -> PrimIO ()
86 {- derived op - attaching a free() finaliser to a malloc() allocated reference. -}
87 makeMallocPtr :: Addr -> PrimIO ForeignObj
89 makeForeignObj (A# obj) (A# finaliser) = ST ( \ (S# s#) ->
90 case makeForeignObj# obj finaliser s# of
91 StateAndForeignObj# s1# fo# -> (ForeignObj fo#, S# s1#))
93 writeForeignObj (ForeignObj fo#) (A# datum#) = ST ( \ (S# s#) ->
94 case writeForeignObj# fo# datum# s# of { s1# -> ((), S# s1#) } )
96 makeMallocPtr a = makeForeignObj a (``&free''::Addr)
99 = unsafePerformPrimIO (_ccall_ eqForeignObj mp1 mp2) /= (0::Int)
101 instance Eq ForeignObj where
102 p == q = eqForeignObj p q
103 p /= q = not (eqForeignObj p q)
107 %*********************************************************
109 \subsection{Type @StablePtr@ and its operations}
111 %*********************************************************
114 #ifndef __PARALLEL_HASKELL__
115 data StablePtr a = StablePtr (StablePtr# a)
116 instance CCallable (StablePtr a)
117 instance CCallable (StablePtr# a)
118 instance CReturnable (StablePtr a)
120 -- Nota Bene: it is important {\em not\/} to inline calls to
121 -- @makeStablePtr#@ since the corresponding macro is very long and we'll
122 -- get terrible code-bloat.
124 makeStablePtr :: a -> PrimIO (StablePtr a)
125 deRefStablePtr :: StablePtr a -> PrimIO a
126 freeStablePtr :: StablePtr a -> PrimIO ()
127 performGC :: PrimIO ()
129 {-# INLINE deRefStablePtr #-}
130 {-# INLINE freeStablePtr #-}
131 {-# INLINE performGC #-}
133 makeStablePtr f = ST $ \ (S# rw1#) ->
134 case makeStablePtr# f rw1# of
135 StateAndStablePtr# rw2# sp# -> (StablePtr sp#, S# rw2#)
137 deRefStablePtr (StablePtr sp#) = ST $ \ (S# rw1#) ->
138 case deRefStablePtr# sp# rw1# of
139 StateAndPtr# rw2# a -> (a, S# rw2#)
141 freeStablePtr sp = _ccall_ freeStablePointer sp
143 performGC = _ccall_GC_ StgPerformGarbageCollection
145 #endif /* !__PARALLEL_HASKELL__ */
148 %*********************************************************
150 \subsection{Ghastly return types}
152 %*********************************************************
155 #ifndef __PARALLEL_HASKELL__
156 data StateAndStablePtr# s a = StateAndStablePtr# (State# s) (StablePtr# a)
158 data StateAndForeignObj# s = StateAndForeignObj# (State# s) ForeignObj#