[project @ 2002-04-24 16:31:37 by simonmar]
[packages/base.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.7 2002/04/24 16: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 #ifdef __GLASGOW_HASKELL__
44 import GHC.Ptr
45 import GHC.IOBase
46 import GHC.Err
47 import GHC.Base
48 import GHC.Num
49 import GHC.List
50 import GHC.Show
51 import Numeric
52 #endif
53
54 #include "MachDeps.h"
55
56 #ifdef __GLASGOW_HASKELL__
57 #if (WORD_SIZE_IN_BITS == 32 || WORD_SIZE_IN_BITS == 64)
58 instance Show (Ptr a) where
59 showsPrec p (Ptr a) rs = pad_out (showHex (word2Integer(int2Word#(addr2Int# a))) "") rs
60 where
61 -- want 0s prefixed to pad it out to a fixed length.
62 pad_out ('0':'x':ls) rs =
63 '0':'x':(replicate (2*SIZEOF_HSPTR - length ls) '0') ++ ls ++ rs
64 -- word2Integer :: Word# -> Integer (stolen from Word.lhs)
65 word2Integer w = case word2Integer# w of
66 (# s, d #) -> J# s d
67 #endif
68 #endif
69
70 foreign import ccall unsafe "freeHaskellFunctionPtr"
71 freeHaskellFunPtr :: FunPtr a -> IO ()