[project @ 1996-01-18 16:33:17 by partain]
[ghc-hetmet.git] / ghc / lib / glaExts / PreludeGlaMisc.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1994
3 %
4 \section[PreludeGlaMisc]{Miscellaneous Glasgow Stuff}
5
6 \begin{code}
7 module PreludeGlaMisc( PreludeGlaMisc.. {-, PreludePS..-} ) where
8
9 import Cls
10 import Core
11 import IInt
12 import List             ( (++) )
13 import PreludeGlaST
14 import PS               ( _PackedString, _unpackPS )
15 import TyArray          ( Array(..) )
16 import TyComplex
17 import Text
18 \end{code}
19
20 Note: the above used to say:
21
22 \begin{pseudocode}
23 module PreludeGlaMisc( 
24         _MallocPtr,
25
26 #ifndef __PARALLEL_HASKELL__
27         _StablePtr,
28         makeStablePtr, deRefStablePtr, freeStablePtr,
29
30         performGC
31 #endif /* !__PARALLEL_HASKELL__ */
32
33         ) where
34 \end{pseudocode}
35
36 But then the names @_MallocPtr@ and @_StablePtr@ get shoved out into
37 the interface file and anyone importing it becomes unhappy about
38 seeing a preludish name.
39
40 They report: 
41
42 @
43 Bad name on a datatype constructor (a Prelude name?): _MallocPtr
44 @
45
46 (This is horrid!)
47
48 (Oh, btw, don't try not exporting them either - that just makes the
49 info-tables, etc local to this module so that no-one can get at them.)
50
51
52
53
54
55 The next two definitions must match those in
56 @compiler/prelude/TysWiredIn.lhs@ exactly.
57
58 \begin{code}
59 #ifndef __PARALLEL_HASKELL__
60
61 -- ** MOVED TO prelude/TysBasic.hs **
62 -- data _MallocPtr = _MallocPtr MallocPtr#
63 -- data _StablePtr a = _StablePtr (StablePtr# a)
64
65 \end{code}
66
67 Nota Bene: it is important {\em not\/} to inline calls to
68 @makeStablePtr#@ since the corresponding macro is very long and we'll
69 get terrible code-bloat.
70
71 \begin{code}
72 makeStablePtr :: a -> PrimIO (_StablePtr a)
73 deRefStablePtr :: _StablePtr a -> PrimIO a
74 freeStablePtr :: _StablePtr a -> PrimIO ()
75
76 eqMallocPtr :: _MallocPtr -> _MallocPtr -> Bool
77
78 performGC :: PrimIO ()
79
80 {-# INLINE deRefStablePtr #-}
81 {-# INLINE freeStablePtr #-}
82 {-# INLINE performGC #-}
83
84 makeStablePtr f (S# rw1#) = 
85         case makeStablePtr# f rw1# of
86           StateAndStablePtr# rw2# sp# -> (_StablePtr sp#, S# rw2#)
87
88 deRefStablePtr (_StablePtr sp#) (S# rw1#) =
89         case deRefStablePtr# sp# rw1# of
90           StateAndPtr# rw2# a -> (a, S# rw2#)
91
92 freeStablePtr sp = _ccall_ freeStablePointer sp
93
94 eqMallocPtr mp1 mp2 = unsafePerformPrimIO (
95         _ccall_ eqMallocPtr mp1 mp2
96         )
97         /= (0::Int)
98
99 instance Eq _MallocPtr where 
100         p == q = eqMallocPtr p q
101         p /= q = if eqMallocPtr p q then False else True
102
103 performGC = _ccall_GC_ StgPerformGarbageCollection
104
105 #endif /* !__PARALLEL_HASKELL__ */
106 \end{code}
107
108 Like they say: this is as good a place as any to put it:
109
110 \begin{code}
111 addr2Int :: _Addr -> Int
112 addr2Int (A# a#) = I# (addr2Int# a#)
113
114 int2Addr :: Int -> _Addr
115 int2Addr (I# i#) = A# (int2Addr# i#)
116 \end{code}