diff options
author | Simon Marlow <simonmar@microsoft.com> | 2006-05-09 09:26:06 +0000 |
---|---|---|
committer | Simon Marlow <simonmar@microsoft.com> | 2006-05-09 09:26:06 +0000 |
commit | aac3a2cfd7140ff535eae5a299aadb1c3c9c81ad (patch) | |
tree | 064e918c040717b58d1a34390a2683567fe74844 /libraries/base/GHC/ForeignPtr.hs | |
parent | a8e9e765dfdfd2e3df96e64988f0ce42d600581d (diff) | |
download | haskell-aac3a2cfd7140ff535eae5a299aadb1c3c9c81ad.tar.gz |
add WordPtr and IntPtr types to Foreign.Ptr, with associated conversions
As suggested by John Meacham.
I had to move the Show instance for Ptr into GHC.ForeignPtr to avoid
recursive dependencies.
Diffstat (limited to 'libraries/base/GHC/ForeignPtr.hs')
-rw-r--r-- | libraries/base/GHC/ForeignPtr.hs | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/libraries/base/GHC/ForeignPtr.hs b/libraries/base/GHC/ForeignPtr.hs index c9217ef836..4c8113653c 100644 --- a/libraries/base/GHC/ForeignPtr.hs +++ b/libraries/base/GHC/ForeignPtr.hs @@ -31,16 +31,17 @@ module GHC.ForeignPtr ) where import Control.Monad ( sequence_ ) -import Foreign.Ptr import Foreign.Storable +import Numeric ( showHex ) -import GHC.List ( null ) +import GHC.Show +import GHC.Num +import GHC.List ( null, replicate, length ) import GHC.Base import GHC.IOBase import GHC.STRef ( STRef(..) ) -import GHC.Ptr ( Ptr(..) ) +import GHC.Ptr ( Ptr(..), FunPtr, castFunPtrToPtr ) import GHC.Err -import GHC.Show -- |The type 'ForeignPtr' represents references to objects that are -- maintained in a foreign language, i.e., that are not part of the @@ -82,6 +83,23 @@ instance Ord (ForeignPtr a) where instance Show (ForeignPtr a) where showsPrec p f = showsPrec p (unsafeForeignPtrToPtr f) +#include "MachDeps.h" + +#if (WORD_SIZE_IN_BITS == 32 || WORD_SIZE_IN_BITS == 64) +instance Show (Ptr a) where + showsPrec p (Ptr a) rs = pad_out (showHex (word2Integer(int2Word#(addr2Int# a))) "") rs + where + -- want 0s prefixed to pad it out to a fixed length. + pad_out ls rs = + '0':'x':(replicate (2*SIZEOF_HSPTR - length ls) '0') ++ ls ++ rs + -- word2Integer :: Word# -> Integer (stolen from Word.lhs) + word2Integer w = case word2Integer# w of + (# s, d #) -> J# s d + +instance Show (FunPtr a) where + showsPrec p = showsPrec p . castFunPtrToPtr +#endif + -- |A Finalizer is represented as a pointer to a foreign function that, at -- finalisation time, gets as an argument a plain pointer variant of the -- foreign pointer that the finalizer is associated with. |