[project @ 2001-08-04 06:19:54 by ken]
[ghc-hetmet.git] / ghc / lib / std / PrelStorable.lhs
index f02b832..92a39b0 100644 (file)
@@ -1,5 +1,5 @@
 % -----------------------------------------------------------------------------
-% $Id: PrelStorable.lhs,v 1.3 2001/02/28 00:01:03 qrczak Exp $
+% $Id: PrelStorable.lhs,v 1.8 2001/07/24 06:31:35 ken Exp $
 %
 % (c) The FFI task force, 2000
 %
@@ -7,6 +7,8 @@
 A class for primitive marshaling
 
 \begin{code}
+{-# OPTIONS -fno-implicit-prelude #-}
+
 #include "MachDeps.h"
 
 module PrelStorable
@@ -18,16 +20,17 @@ module PrelStorable
             peekByteOff,    -- :: Ptr b -> Int      -> IO a
             pokeByteOff,    -- :: Ptr b -> Int -> a -> IO ()
             peek,           -- :: Ptr a             -> IO a
-            poke)           -- :: Ptr a        -> a -> IO ()
+            poke,           -- :: Ptr a        -> a -> IO ()
+            destruct)       -- :: Ptr a             -> IO ()
         ) where
 \end{code}
 
 \begin{code}
-import Char            ( chr, ord )
 import Monad           ( liftM )
 
 #ifdef __GLASGOW_HASKELL__
 import PrelStable      ( StablePtr )
+import PrelNum
 import PrelInt
 import PrelWord
 import PrelCTypes
@@ -35,6 +38,7 @@ import PrelCTypesISO
 import PrelStable
 import PrelPtr
 import PrelFloat
+import PrelErr
 import PrelIOBase
 import PrelBase
 #endif
@@ -64,6 +68,10 @@ class Storable a where
    peek        :: Ptr a      -> IO a
    poke        :: Ptr a -> a -> IO ()
 
+   -- free memory associated with the object
+   -- (except the object pointer itself)
+   destruct    :: Ptr a -> IO ()
+
    -- circular default instances
    peekElemOff = peekElemOff_ undefined
       where peekElemOff_ :: a -> Ptr a -> Int -> IO a
@@ -75,6 +83,8 @@ class Storable a where
 
    peek ptr = peekElemOff ptr 0
    poke ptr = pokeElemOff ptr 0
+
+   destruct _ = return ()
 \end{code}
 
 System-dependent, but rather obvious instances
@@ -86,12 +96,6 @@ instance Storable Bool where
    peekElemOff p i   = liftM (/= (0::CInt)) $ peekElemOff (castPtr p) i
    pokeElemOff p i x = pokeElemOff (castPtr p) i (if x then 1 else 0::CInt)
 
-instance Storable (FunPtr a) where
-   sizeOf          (FunPtr x) = sizeOf x
-   alignment       (FunPtr x) = alignment x
-   peekElemOff p i            = liftM FunPtr $ peekElemOff (castPtr p) i
-   pokeElemOff p i (FunPtr x) = pokeElemOff (castPtr p) i x
-
 #define STORABLE(T,size,align,read,write)      \
 instance Storable (T) where {                  \
     sizeOf    _ = size;                                \
@@ -111,6 +115,9 @@ STORABLE(Word,SIZEOF_LONG,ALIGNMENT_LONG,
 STORABLE((Ptr a),SIZEOF_VOID_P,ALIGNMENT_VOID_P,
         readPtrOffPtr,writePtrOffPtr)
 
+STORABLE((FunPtr a),SIZEOF_VOID_P,ALIGNMENT_VOID_P,
+        readFunPtrOffPtr,writeFunPtrOffPtr)
+
 STORABLE((StablePtr a),SIZEOF_VOID_P,ALIGNMENT_VOID_P,
         readStablePtrOffPtr,writeStablePtrOffPtr)
 
@@ -182,6 +189,7 @@ readWideCharOffPtr  :: Ptr Char          -> Int -> IO Char
 readIntOffPtr       :: Ptr Int           -> Int -> IO Int
 readWordOffPtr      :: Ptr Word          -> Int -> IO Word
 readPtrOffPtr       :: Ptr (Ptr a)       -> Int -> IO (Ptr a)
+readFunPtrOffPtr    :: Ptr (FunPtr a)    -> Int -> IO (FunPtr a)
 readFloatOffPtr     :: Ptr Float         -> Int -> IO Float
 readDoubleOffPtr    :: Ptr Double        -> Int -> IO Double
 readStablePtrOffPtr :: Ptr (StablePtr a) -> Int -> IO (StablePtr a)
@@ -202,6 +210,8 @@ readWordOffPtr (Ptr a) (I# i)
   = IO $ \s -> case readWordOffAddr# a i s      of (# s2, x #) -> (# s2, W# x #)
 readPtrOffPtr (Ptr a) (I# i)
   = IO $ \s -> case readAddrOffAddr# a i s      of (# s2, x #) -> (# s2, Ptr x #)
+readFunPtrOffPtr (Ptr a) (I# i)
+  = IO $ \s -> case readAddrOffAddr# a i s      of (# s2, x #) -> (# s2, FunPtr x #)
 readFloatOffPtr (Ptr a) (I# i)
   = IO $ \s -> case readFloatOffAddr# a i s     of (# s2, x #) -> (# s2, F# x #)
 readDoubleOffPtr (Ptr a) (I# i)
@@ -214,21 +224,32 @@ readInt16OffPtr (Ptr a) (I# i)
   = IO $ \s -> case readInt16OffAddr# a i s     of (# s2, x #) -> (# s2, I16# x #)
 readInt32OffPtr (Ptr a) (I# i)
   = IO $ \s -> case readInt32OffAddr# a i s     of (# s2, x #) -> (# s2, I32# x #)
+#if WORD_SIZE_IN_BYTES == 4
 readInt64OffPtr (Ptr a) (I# i)
   = IO $ \s -> case readInt64OffAddr# a i s     of (# s2, x #) -> (# s2, I64# x #)
+#else
+readInt64OffPtr (Ptr a) (I# i)
+  = IO $ \s -> case readIntOffAddr# a i s       of (# s2, x #) -> (# s2, I64# x #)
+#endif
 readWord8OffPtr (Ptr a) (I# i)
   = IO $ \s -> case readWord8OffAddr# a i s     of (# s2, x #) -> (# s2, W8# x #)
 readWord16OffPtr (Ptr a) (I# i)
   = IO $ \s -> case readWord16OffAddr# a i s    of (# s2, x #) -> (# s2, W16# x #)
 readWord32OffPtr (Ptr a) (I# i)
   = IO $ \s -> case readWord32OffAddr# a i s    of (# s2, x #) -> (# s2, W32# x #)
+#if WORD_SIZE_IN_BYTES == 4
 readWord64OffPtr (Ptr a) (I# i)
   = IO $ \s -> case readWord64OffAddr# a i s    of (# s2, x #) -> (# s2, W64# x #)
+#else
+readWord64OffPtr (Ptr a) (I# i)
+  = IO $ \s -> case readWordOffAddr# a i s      of (# s2, x #) -> (# s2, W64# x #)
+#endif
 
 writeWideCharOffPtr  :: Ptr Char          -> Int -> Char        -> IO ()
 writeIntOffPtr       :: Ptr Int           -> Int -> Int         -> IO ()
 writeWordOffPtr      :: Ptr Word          -> Int -> Word        -> IO ()
 writePtrOffPtr       :: Ptr (Ptr a)       -> Int -> Ptr a       -> IO ()
+writeFunPtrOffPtr    :: Ptr (FunPtr a)    -> Int -> FunPtr a    -> IO ()
 writeFloatOffPtr     :: Ptr Float         -> Int -> Float       -> IO ()
 writeDoubleOffPtr    :: Ptr Double        -> Int -> Double      -> IO ()
 writeStablePtrOffPtr :: Ptr (StablePtr a) -> Int -> StablePtr a -> IO ()
@@ -249,6 +270,8 @@ writeWordOffPtr (Ptr a) (I# i) (W# x)
   = IO $ \s -> case writeWordOffAddr# a i x s      of s2 -> (# s2, () #)
 writePtrOffPtr (Ptr a) (I# i) (Ptr x)
   = IO $ \s -> case writeAddrOffAddr# a i x s      of s2 -> (# s2, () #)
+writeFunPtrOffPtr (Ptr a) (I# i) (FunPtr x)
+  = IO $ \s -> case writeAddrOffAddr# a i x s      of s2 -> (# s2, () #)
 writeFloatOffPtr (Ptr a) (I# i) (F# x)
   = IO $ \s -> case writeFloatOffAddr# a i x s     of s2 -> (# s2, () #)
 writeDoubleOffPtr (Ptr a) (I# i) (D# x)
@@ -261,16 +284,26 @@ writeInt16OffPtr (Ptr a) (I# i) (I16# x)
   = IO $ \s -> case writeInt16OffAddr# a i x s     of s2 -> (# s2, () #)
 writeInt32OffPtr (Ptr a) (I# i) (I32# x)
   = IO $ \s -> case writeInt32OffAddr# a i x s     of s2 -> (# s2, () #)
+#if WORD_SIZE_IN_BYTES == 4
 writeInt64OffPtr (Ptr a) (I# i) (I64# x)
   = IO $ \s -> case writeInt64OffAddr# a i x s     of s2 -> (# s2, () #)
+#else
+writeInt64OffPtr (Ptr a) (I# i) (I64# x)
+  = IO $ \s -> case writeIntOffAddr# a i x s       of s2 -> (# s2, () #)
+#endif
 writeWord8OffPtr (Ptr a) (I# i) (W8# x)
   = IO $ \s -> case writeWord8OffAddr# a i x s     of s2 -> (# s2, () #)
 writeWord16OffPtr (Ptr a) (I# i) (W16# x)
   = IO $ \s -> case writeWord16OffAddr# a i x s    of s2 -> (# s2, () #)
 writeWord32OffPtr (Ptr a) (I# i) (W32# x)
   = IO $ \s -> case writeWord32OffAddr# a i x s    of s2 -> (# s2, () #)
+#if WORD_SIZE_IN_BYTES == 4
 writeWord64OffPtr (Ptr a) (I# i) (W64# x)
   = IO $ \s -> case writeWord64OffAddr# a i x s    of s2 -> (# s2, () #)
+#else
+writeWord64OffPtr (Ptr a) (I# i) (W64# x)
+  = IO $ \s -> case writeWordOffAddr# a i x s      of s2 -> (# s2, () #)
+#endif
 
 #endif /* __GLASGOW_HASKELL__ */
 \end{code}