From 111d9435b72fdf4dcf606e6da7baa06c2ae2190f Mon Sep 17 00:00:00 2001 From: simonm Date: Tue, 26 Jan 1999 12:25:02 +0000 Subject: [PATCH] [project @ 1999-01-26 12:24:57 by simonm] More stable name additions/changes. --- ghc/docs/libraries/Stable.sgml | 126 ++++++++++++++++++++++++++++++++++++++++ ghc/lib/exts/Foreign.lhs | 3 +- ghc/lib/exts/MutableArray.lhs | 1 + ghc/lib/exts/Stable.lhs | 47 +++++++++++++++ ghc/lib/std/Makefile | 2 +- ghc/lib/std/PrelErr.lhs | 2 +- ghc/lib/std/PrelForeign.lhs | 46 --------------- ghc/lib/std/PrelGHC.hi-boot | 6 ++ ghc/lib/std/PrelStable.lhs | 45 ++++++++++++++ 9 files changed, 229 insertions(+), 49 deletions(-) create mode 100644 ghc/docs/libraries/Stable.sgml create mode 100644 ghc/lib/exts/Stable.lhs create mode 100644 ghc/lib/std/PrelStable.lhs diff --git a/ghc/docs/libraries/Stable.sgml b/ghc/docs/libraries/Stable.sgml new file mode 100644 index 0000000..2d1dc36 --- /dev/null +++ b/ghc/docs/libraries/Stable.sgml @@ -0,0 +1,126 @@ + +

+ +This module provides two kinds of stable references to Haskell +objects, stable names and stable pointers. + + +

+ +A +data StablePtr a -- abstract, instance of: Eq. +makeStablePtr :: a -> IO (StablePtr a) +deRefStablePtr :: StablePtr a -> IO a +freeStablePtr :: StablePtr a -> IO () + + +Care must be taken to free stable pointers that are no longer required +using the + The object referenced by the stable pointer will be retained in +the heap. + The runtime system's internal stable pointer table will grow, +which imposes an overhead on garbage collection. + + +Notes: + + + If The reverse is not necessarily true: if two stable pointers are +not equal, it doesn't mean that they don't refer to the same Haskell +object (although they probably don't). Calling + + +The C interface (which is brought into scope by +typedef StablePtr /* abstract, probably an unsigned long */ +extern StgPtr deRefStablePtr(StgStablePtr stable_ptr); +static void freeStablePtr(StgStablePtr sp); +static StgStablePtr splitStablePtr(StgStablePtr sp); + + +The functions Stable Names +

+ +A haskell object can be given a +data StableName a -- abstract, instance Eq. +makeStableName :: a -> IO (StableName a) +hashStableName :: StableName a -> Int + + +All these operations run in constant time. + +Stable names have the following properties: + + + If The reverse is not necessarily true: if two stable names are +not equal, it doesn't mean that they don't refer to the same Haskell +object (although they probably don't). + There is no There is no There is a + +Properties (1) and (2) are similar to stable pointers, but the key +differences are that you can't get back to the original object from a +stable name, and you can convert one to an IO (StableName a) + , hashStableName -- :: StableName a -> Int + + , StablePtr {-a-} -- abstract. + , makeStablePtr -- :: a -> IO (StablePtr a) + , deRefStablePtr -- :: StablePtr a -> IO a + , freeStablePtr -- :: StablePtr a -> IO () + ) + + where + +import PrelBase +import PrelIOBase +import PrelStable + +----------------------------------------------------------------------------- +-- Stable Names + +data StableName a = StableName (StableName# a) + +makeStableName :: a -> IO (StableName a) +hashStableName :: StableName a -> Int + +makeStableName a = IO $ \ s -> + case makeStableName# a s of (# s', sn #) -> (# s', StableName sn #) + +hashStableName (StableName sn) = I# (stableNameToInt# sn) + +instance Eq (StableName a) where + (StableName sn1) == (StableName sn2) = + case eqStableName# sn1 sn2 of + 0# -> False + _ -> True + +\end{code} diff --git a/ghc/lib/std/Makefile b/ghc/lib/std/Makefile index 81feb53..d32470f 100644 --- a/ghc/lib/std/Makefile +++ b/ghc/lib/std/Makefile @@ -58,7 +58,7 @@ PrelNumExtra_HC_OPTS += -H30m -K2m PrelBase_HC_OPTS += -H10m PrelRead_HC_OPTS += -H16m PrelTup_HC_OPTS += -H12m -PrelNum_HC_OPTS += -H12m -K3m +PrelNum_HC_OPTS += -H12m -K4m PrelArr_HC_OPTS += -H8m PrelHandle_HC_OPTS += -H14m Time_HC_OPTS += -H18m diff --git a/ghc/lib/std/PrelErr.lhs b/ghc/lib/std/PrelErr.lhs index a96044c..70b3f47 100644 --- a/ghc/lib/std/PrelErr.lhs +++ b/ghc/lib/std/PrelErr.lhs @@ -41,7 +41,7 @@ import PrelPack ( packString ) import PrelArr ( ByteArray(..) ) #ifndef __PARALLEL_HASKELL__ -import PrelForeign ( StablePtr, deRefStablePtr ) +import PrelStable ( StablePtr, deRefStablePtr ) #endif --------------------------------------------------------------- diff --git a/ghc/lib/std/PrelForeign.lhs b/ghc/lib/std/PrelForeign.lhs index a61d27a..f149108 100644 --- a/ghc/lib/std/PrelForeign.lhs +++ b/ghc/lib/std/PrelForeign.lhs @@ -59,52 +59,6 @@ instance Eq ForeignObj where %********************************************************* %* * -\subsection{Type @StablePtr@ and its operations} -%* * -%********************************************************* - -\begin{code} -#ifndef __PARALLEL_HASKELL__ -data StablePtr a = StablePtr (StablePtr# a) -instance CCallable (StablePtr a) -instance CCallable (StablePtr# a) -instance CReturnable (StablePtr a) - --- Nota Bene: it is important {\em not\/} to inline calls to --- @makeStablePtr#@ since the corresponding macro is very long and we'll --- get terrible code-bloat. - -makeStablePtr :: a -> IO (StablePtr a) -deRefStablePtr :: StablePtr a -> IO a -freeStablePtr :: StablePtr a -> IO () - -{-# INLINE deRefStablePtr #-} -{-# INLINE freeStablePtr #-} - -makeStablePtr f = IO $ \ rw1# -> - case makeStablePtr# f rw1# of - (# rw2#, sp# #) -> (# rw2#, StablePtr sp# #) - -deRefStablePtr (StablePtr sp#) = IO $ \ rw1# -> - deRefStablePtr# sp# rw1# - -freeStablePtr sp = _ccall_ freeStablePointer sp - -eqStablePtr :: StablePtr a -> StablePtr b -> Bool -eqStablePtr (StablePtr sp1#) (StablePtr sp2#) = - case eqStablePtr# sp1# sp2# of - 0# -> False - _ -> True - -instance Eq (StablePtr a) where - p == q = eqStablePtr p q - p /= q = not (eqStablePtr p q) - -#endif /* !__PARALLEL_HASKELL__ */ -\end{code} - -%********************************************************* -%* * \subsection{Unpacking Foreigns} %* * %********************************************************* diff --git a/ghc/lib/std/PrelGHC.hi-boot b/ghc/lib/std/PrelGHC.hi-boot index 1a1cbe8..f9e879c 100644 --- a/ghc/lib/std/PrelGHC.hi-boot +++ b/ghc/lib/std/PrelGHC.hi-boot @@ -298,6 +298,12 @@ indexWord64OffForeignObj# makeStablePtr# deRefStablePtr# eqStablePtr# + + StableName# + makeStableName# + eqStableName# + stableNameToInt# + reallyUnsafePtrEquality# unsafeCoerce# diff --git a/ghc/lib/std/PrelStable.lhs b/ghc/lib/std/PrelStable.lhs new file mode 100644 index 0000000..664ade7 --- /dev/null +++ b/ghc/lib/std/PrelStable.lhs @@ -0,0 +1,45 @@ +% ----------------------------------------------------------------------------- +% $Id: PrelStable.lhs,v 1.1 1999/01/26 12:25:01 simonm Exp $ +% +% (c) The GHC Team, 1992-1999 +% + +\begin{code} +{-# OPTIONS -fno-implicit-prelude #-} + +module PrelStable + ( StablePtr(..) + , makeStablePtr -- :: a -> IO (StablePtr a) + , deRefStablePtr -- :: StablePtr a -> a + , freeStablePtr -- :: StablePtr a -> IO () + ) where + +import PrelBase +import PrelIOBase + +----------------------------------------------------------------------------- +-- Stable Pointers + +data StablePtr a = StablePtr (StablePtr# a) + +instance CCallable (StablePtr a) +instance CCallable (StablePtr# a) +instance CReturnable (StablePtr a) + +makeStablePtr :: a -> IO (StablePtr a) +deRefStablePtr :: StablePtr a -> IO a +freeStablePtr :: StablePtr a -> IO () + +makeStablePtr a = IO $ \ s -> + case makeStablePtr# a s of (# s', sp #) -> (# s', StablePtr sp #) + +deRefStablePtr (StablePtr sp) = IO $ \s -> deRefStablePtr# sp s + +freeStablePtr sp = _ccall_ freeStablePtr sp + +instance Eq (StablePtr a) where + (StablePtr sp1) == (StablePtr sp2) = + case eqStablePtr# sp1 sp2 of + 0# -> False + _ -> True +\end{code} -- 1.7.10.4