[project @ 2005-02-02 15:21:02 by simonmar]
[ghc-base.git] / Foreign / Ptr.hs
1 {-# OPTIONS_GHC -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/base/LICENSE)
7 -- 
8 -- Maintainer  :  ffi@haskell.org
9 -- Stability   :  provisional
10 -- Portability :  portable
11 --
12 -- This module provides typed pointers to foreign data.  It is part
13 -- of the Foreign Function Interface (FFI) and will normally be
14 -- imported via the "Foreign" module.
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     -- * Function pointers
30     
31     FunPtr,      -- data FunPtr a
32     nullFunPtr,      -- :: FunPtr a
33     castFunPtr,      -- :: FunPtr a -> FunPtr b
34     castFunPtrToPtr, -- :: FunPtr a -> Ptr b
35     castPtrToFunPtr, -- :: Ptr a -> FunPtr b
36     
37     freeHaskellFunPtr, -- :: FunPtr a -> IO ()
38     -- Free the function pointer created by foreign export dynamic.
39
40  ) where
41
42 #ifdef __GLASGOW_HASKELL__
43 import GHC.Ptr
44 import GHC.IOBase
45 import GHC.Base
46 import GHC.Num
47 import GHC.List
48 import GHC.Show
49 import Numeric
50 #endif
51
52 #ifdef __NHC__
53 import NHC.FFI
54   ( Ptr
55   , nullPtr
56   , castPtr
57   , plusPtr
58   , alignPtr
59   , minusPtr
60   , FunPtr
61   , nullFunPtr
62   , castFunPtr
63   , castFunPtrToPtr
64   , castPtrToFunPtr
65   , freeHaskellFunPtr
66   )
67 #endif
68
69 #ifdef __HUGS__
70 import Hugs.Ptr
71 #endif
72
73 #ifdef __GLASGOW_HASKELL__
74 #include "MachDeps.h"
75
76 #if (WORD_SIZE_IN_BITS == 32 || WORD_SIZE_IN_BITS == 64)
77 instance Show (Ptr a) where
78    showsPrec p (Ptr a) rs = pad_out (showHex (word2Integer(int2Word#(addr2Int# a))) "") rs
79      where
80         -- want 0s prefixed to pad it out to a fixed length.
81        pad_out ls rs = 
82           '0':'x':(replicate (2*SIZEOF_HSPTR - length ls) '0') ++ ls ++ rs
83        -- word2Integer :: Word# -> Integer (stolen from Word.lhs)
84        word2Integer w = case word2Integer# w of
85                         (# s, d #) -> J# s d
86
87 instance Show (FunPtr a) where
88    showsPrec p = showsPrec p . castFunPtrToPtr
89 #endif
90
91 -- | Release the storage associated with the given 'FunPtr', which
92 -- must have been obtained from a wrapper stub.  This should be called
93 -- whenever the return value from a foreign import wrapper function is
94 -- no longer required; otherwise, the storage it uses will leak.
95 foreign import ccall unsafe "freeHaskellFunctionPtr"
96     freeHaskellFunPtr :: FunPtr a -> IO ()
97 #endif