9d7416c3c6cdc2cd600519f0914f35ad5c6e3df9
[packages/random.git] / Foreign / Ptr.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 --
4 -- Module : Foreign.Ptr
5 -- Copyright : (c) The FFI task force 2001
6 -- License : BSD-style (see the file libraries/core/LICENSE)
7 --
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
11 --
12 -- $Id: Ptr.hs,v 1.4 2002/02/27 14:31:44 simonmar Exp $
13 --
14 -- Pointer types.
15 --
16 -----------------------------------------------------------------------------
17
18 module Foreign.Ptr (
19 --------------------------------------------------------------------
20 -- Data pointers.
21
22 Ptr(..), -- data Ptr a
23 nullPtr, -- :: Ptr a
24 castPtr, -- :: Ptr a -> Ptr b
25 plusPtr, -- :: Ptr a -> Int -> Ptr b
26 alignPtr, -- :: Ptr a -> Int -> Ptr a
27 minusPtr, -- :: Ptr a -> Ptr b -> Int
28
29 --------------------------------------------------------------------
30 -- Function pointers.
31
32 FunPtr(..), -- data FunPtr a
33 nullFunPtr, -- :: FunPtr a
34 castFunPtr, -- :: FunPtr a -> FunPtr b
35 castFunPtrToPtr, -- :: FunPtr a -> Ptr b
36 castPtrToFunPtr, -- :: Ptr a -> FunPtr b
37
38 freeHaskellFunPtr, -- :: FunPtr a -> IO ()
39 -- Free the function pointer created by foreign export dynamic.
40
41 ) where
42
43 import Data.Dynamic
44
45 #ifdef __GLASGOW_HASKELL__
46 import GHC.Ptr
47 import GHC.IOBase
48 import GHC.Err
49 import GHC.Base
50 import GHC.Num
51 import GHC.List
52 import GHC.Show
53 import Numeric
54 #endif
55
56 #include "MachDeps.h"
57
58 #ifdef __GLASGOW_HASKELL__
59 #if (WORD_SIZE_IN_BITS == 32 || WORD_SIZE_IN_BITS == 64)
60 instance Show (Ptr a) where
61 showsPrec p (Ptr a) rs = pad_out (showHex (word2Integer(int2Word#(addr2Int# a))) "") rs
62 where
63 -- want 0s prefixed to pad it out to a fixed length.
64 pad_out ('0':'x':ls) rs =
65 '0':'x':(replicate (2*SIZEOF_HSPTR - length ls) '0') ++ ls ++ rs
66 -- word2Integer :: Word# -> Integer (stolen from Word.lhs)
67 word2Integer w = case word2Integer# w of
68 (# s, d #) -> J# s d
69 #endif
70 #endif
71
72 foreign import "freeHaskellFunctionPtr" unsafe
73 freeHaskellFunPtr :: FunPtr a -> IO ()
74
75 #include "Dynamic.h"
76 INSTANCE_TYPEABLE1(Ptr,ptrTc,"Ptr")