Ptr.hs 2.35 KB
Newer Older
1
2
{-# OPTIONS -fno-implicit-prelude #-}
-----------------------------------------------------------------------------
3
-- |
4
5
-- Module      :  Foreign.Ptr
-- Copyright   :  (c) The FFI task force 2001
6
-- License     :  BSD-style (see the file libraries/base/LICENSE)
7
8
-- 
-- Maintainer  :  ffi@haskell.org
9
10
-- Stability   :  provisional
-- Portability :  portable
11
--
12
-- This module provides typed pointers to foreign data.  It is part
13
14
-- of the Foreign Function Interface (FFI) and will normally be
-- imported via the "Foreign" module.
15
16
17
18
--
-----------------------------------------------------------------------------

module Foreign.Ptr (
19
20

    -- * Data pointers
21
    
22
    Ptr,      -- data Ptr a
23
24
25
26
27
28
    nullPtr,      -- :: Ptr a
    castPtr,      -- :: Ptr a -> Ptr b
    plusPtr,      -- :: Ptr a -> Int -> Ptr b
    alignPtr,     -- :: Ptr a -> Int -> Ptr a
    minusPtr,     -- :: Ptr a -> Ptr b -> Int
    
29
    -- * Function pointers
30
    
31
    FunPtr,      -- data FunPtr a
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    nullFunPtr,      -- :: FunPtr a
    castFunPtr,      -- :: FunPtr a -> FunPtr b
    castFunPtrToPtr, -- :: FunPtr a -> Ptr b
    castPtrToFunPtr, -- :: Ptr a -> FunPtr b
    
    freeHaskellFunPtr, -- :: FunPtr a -> IO ()
    -- Free the function pointer created by foreign export dynamic.

 ) where

#ifdef __GLASGOW_HASKELL__
import GHC.Ptr
import GHC.IOBase
import GHC.Err
46
47
48
49
50
51
52
import GHC.Base
import GHC.Num
import GHC.List
import GHC.Show
import Numeric
#endif

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifdef __NHC__
import NHC.FFI
  ( Ptr
  , nullPtr
  , castPtr
  , plusPtr
  , alignPtr
  , minusPtr
  , FunPtr
  , nullFunPtr
  , castFunPtr
  , castFunPtrToPtr
  , castPtrToFunPtr
  , freeHaskellFunPtr
  )
#endif

ross's avatar
ross committed
70
71
72
#ifdef __HUGS__
import Hugs.Ptr
#endif
73
74

#ifdef __GLASGOW_HASKELL__
75
76
#include "MachDeps.h"

77
78
79
80
81
#if (WORD_SIZE_IN_BITS == 32 || WORD_SIZE_IN_BITS == 64)
instance Show (Ptr a) where
   showsPrec p (Ptr a) rs = pad_out (showHex (word2Integer(int2Word#(addr2Int# a))) "") rs
     where
        -- want 0s prefixed to pad it out to a fixed length.
82
       pad_out ls rs = 
83
84
85
86
	  '0':'x':(replicate (2*SIZEOF_HSPTR - length ls) '0') ++ ls ++ rs
       -- word2Integer :: Word# -> Integer (stolen from Word.lhs)
       word2Integer w = case word2Integer# w of
			(# s, d #) -> J# s d
87
88
89

instance Show (FunPtr a) where
   showsPrec p = showsPrec p . castFunPtrToPtr
90
#endif
91

92
foreign import ccall unsafe "freeHaskellFunctionPtr"
93
    freeHaskellFunPtr :: FunPtr a -> IO ()
94
#endif