portable implementation of WordPtr/IntPtr for non-GHC
authorRoss Paterson <ross@soi.city.ac.uk>
Wed, 10 May 2006 00:18:26 +0000 (00:18 +0000)
committerRoss Paterson <ross@soi.city.ac.uk>
Wed, 10 May 2006 00:18:26 +0000 (00:18 +0000)
plus much tweaking of imports to avoid cycles

Foreign/C/Types.hs
Foreign/Ptr.hs
Foreign/Storable.hs
include/HsBase.h

index 625c4b3..7e8c5a3 100644 (file)
@@ -81,8 +81,11 @@ import GHC.Show
 import GHC.Read
 import GHC.Num
 #else
-import Control.Monad
-import Foreign.Ptr
+import Control.Monad   ( liftM )
+#endif
+
+#ifdef __HUGS__
+import Hugs.Ptr                ( castPtr )
 #endif
 
 #include "HsBaseConfig.h"
index e6eb205..cc5008c 100644 (file)
@@ -37,6 +37,7 @@ module Foreign.Ptr (
     freeHaskellFunPtr, -- :: FunPtr a -> IO ()
     -- Free the function pointer created by foreign export dynamic.
 
+#ifndef __NHC__
     -- * Integral types with lossless conversion to/from pointers
     IntPtr,
     ptrToIntPtr,
@@ -44,6 +45,7 @@ module Foreign.Ptr (
     WordPtr,
     ptrToWordPtr,
     wordPtrToPtr
+#endif
  ) where
 
 #ifdef __GLASGOW_HASKELL__
@@ -51,22 +53,23 @@ import GHC.Ptr
 import GHC.IOBase
 import GHC.Base
 import GHC.Num
-import GHC.List
 import GHC.Read
 import GHC.Real
 import GHC.Show
 import GHC.Enum
 import GHC.Word                ( Word(..) )
-import Data.Bits
-import Data.Typeable   ( Typeable(..), mkTyCon, mkTyConApp )
-import Numeric
-import Foreign.C.Types
 
-import Foreign.Storable
 import Data.Int
 import Data.Word
+#else
+import Foreign.C.Types
 #endif
 
+import Control.Monad   ( liftM )
+import Data.Bits
+import Data.Typeable   ( Typeable(..), mkTyCon, mkTyConApp )
+import Foreign.Storable ( Storable(..) )
+
 #ifdef __NHC__
 import NHC.FFI
   ( Ptr
@@ -95,16 +98,19 @@ import Hugs.Ptr
 -- no longer required; otherwise, the storage it uses will leak.
 foreign import ccall unsafe "freeHaskellFunctionPtr"
     freeHaskellFunPtr :: FunPtr a -> IO ()
+#endif
 
-#include "HsBaseConfig.h"
-#include "CTypes.h"
+#ifndef __NHC__
+# include "HsBaseConfig.h"
+# include "CTypes.h"
 
--- | An unsigend integral type that can be losslessly converted to and from
+# ifdef __GLASGOW_HASKELL__
+-- | An unsigned integral type that can be losslessly converted to and from
 -- @Ptr@.
 INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",Word)
        -- Word and Int are guaranteed pointer-sized in GHC
 
--- | A sigend integral type that can be losslessly converted to and from
+-- | A signed integral type that can be losslessly converted to and from
 -- @Ptr@.
 INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",Int)
        -- Word and Int are guaranteed pointer-sized in GHC
@@ -124,4 +130,25 @@ ptrToIntPtr (Ptr a#) = IntPtr (I# (addr2Int# a#))
 -- | casts an @IntPtr@ to a @Ptr@
 intPtrToPtr :: IntPtr -> Ptr a
 intPtrToPtr (IntPtr (I# i#)) = Ptr (int2Addr# i#)
-#endif
+
+# else /* !__GLASGOW_HASKELL__ */
+
+INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",CUIntPtr)
+INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",CIntPtr)
+
+{-# CFILES cbits/PrelIOUtils.c #-}
+
+foreign import ccall unsafe "__hscore_to_uintptr"
+    ptrToWordPtr :: Ptr a -> WordPtr
+
+foreign import ccall unsafe "__hscore_from_uintptr"
+    wordPtrToPtr :: WordPtr -> Ptr a
+
+foreign import ccall unsafe "__hscore_to_intptr"
+    ptrToIntPtr :: Ptr a -> IntPtr
+
+foreign import ccall unsafe "__hscore_from_intptr"
+    intPtrToPtr :: IntPtr -> Ptr a
+
+# endif /* !__GLASGOW_HASKELL__ */
+#endif /* !__NHC_ */
index cfe0524..c48746b 100644 (file)
@@ -54,12 +54,12 @@ import GHC.Base
 #else
 import Data.Int
 import Data.Word
-import Foreign.Ptr
 import Foreign.StablePtr
 #endif
 
 #ifdef __HUGS__
 import Hugs.Prelude
+import Hugs.Ptr
 import Hugs.Storable
 #endif
 
index 57873e0..143b29d 100644 (file)
 #if HAVE_WCTYPE_H
 #include <wctype.h>
 #endif
+#if HAVE_INTTYPES_H
+# include <inttypes.h>
+#elif HAVE_STDINT_H
+# include <stdint.h>
+#endif
 
 #if !defined(mingw32_HOST_OS) && !defined(irix_HOST_OS)
 # if HAVE_SYS_RESOURCE_H
@@ -765,5 +770,11 @@ INLINE unsigned int __hscore_get_osver(void) { return _osver; }
 extern char** environ;
 INLINE char **__hscore_environ() { return environ; }
 
+/* lossless conversions between pointers and integral types */
+INLINE void *    __hscore_from_uintptr(uintptr_t n) { return (void *)n; }
+INLINE void *    __hscore_from_intptr (intptr_t n)  { return (void *)n; }
+INLINE uintptr_t __hscore_to_uintptr  (void *p)     { return (uintptr_t)p; }
+INLINE intptr_t  __hscore_to_intptr   (void *p)     { return (intptr_t)p; }
+
 #endif /* __HSBASE_H__ */