{-# LANGUAGE Unsafe #-}
{-# LANGUAGE CPP, NoImplicitPrelude, MagicHash, RoleAnnotations #-}
{-# LANGUAGE UnboxedTuples #-}
{-# OPTIONS_HADDOCK not-home #-}
module GHC.Ptr (
Ptr(..), FunPtr(..),
nullPtr, castPtr, plusPtr, alignPtr, minusPtr,
nullFunPtr, castFunPtr,
castFunPtrToPtr, castPtrToFunPtr,
) where
import GHC.Base
import GHC.Show
import GHC.Num
import GHC.List ( length, replicate )
import Numeric ( showHex )
#include "MachDeps.h"
type role Ptr phantom
data Ptr a = Ptr Addr#
deriving ( Ptr a -> Ptr a -> Bool
forall a. Ptr a -> Ptr a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Ptr a -> Ptr a -> Bool
$c/= :: forall a. Ptr a -> Ptr a -> Bool
== :: Ptr a -> Ptr a -> Bool
$c== :: forall a. Ptr a -> Ptr a -> Bool
Eq
, Ptr a -> Ptr a -> Bool
Ptr a -> Ptr a -> Ordering
forall a. Eq (Ptr a)
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ptr a -> Ptr a -> Bool
forall a. Ptr a -> Ptr a -> Ordering
forall a. Ptr a -> Ptr a -> Ptr a
min :: Ptr a -> Ptr a -> Ptr a
$cmin :: forall a. Ptr a -> Ptr a -> Ptr a
max :: Ptr a -> Ptr a -> Ptr a
$cmax :: forall a. Ptr a -> Ptr a -> Ptr a
>= :: Ptr a -> Ptr a -> Bool
$c>= :: forall a. Ptr a -> Ptr a -> Bool
> :: Ptr a -> Ptr a -> Bool
$c> :: forall a. Ptr a -> Ptr a -> Bool
<= :: Ptr a -> Ptr a -> Bool
$c<= :: forall a. Ptr a -> Ptr a -> Bool
< :: Ptr a -> Ptr a -> Bool
$c< :: forall a. Ptr a -> Ptr a -> Bool
compare :: Ptr a -> Ptr a -> Ordering
$ccompare :: forall a. Ptr a -> Ptr a -> Ordering
Ord
)
nullPtr :: Ptr a
nullPtr :: forall a. Ptr a
nullPtr = forall a. Addr# -> Ptr a
Ptr Addr#
nullAddr#
castPtr :: Ptr a -> Ptr b
castPtr :: forall a b. Ptr a -> Ptr b
castPtr = coerce :: forall a b. Coercible a b => a -> b
coerce
plusPtr :: Ptr a -> Int -> Ptr b
plusPtr :: forall a b. Ptr a -> Int -> Ptr b
plusPtr (Ptr Addr#
addr) (I# Int#
d) = forall a. Addr# -> Ptr a
Ptr (Addr# -> Int# -> Addr#
plusAddr# Addr#
addr Int#
d)
alignPtr :: Ptr a -> Int -> Ptr a
alignPtr :: forall a. Ptr a -> Int -> Ptr a
alignPtr addr :: Ptr a
addr@(Ptr Addr#
a) (I# Int#
i)
= case Addr# -> Int# -> Int#
remAddr# Addr#
a Int#
i of {
Int#
0# -> Ptr a
addr;
Int#
n -> forall a. Addr# -> Ptr a
Ptr (Addr# -> Int# -> Addr#
plusAddr# Addr#
a (Int#
i Int# -> Int# -> Int#
-# Int#
n)) }
minusPtr :: Ptr a -> Ptr b -> Int
minusPtr :: forall a b. Ptr a -> Ptr b -> Int
minusPtr (Ptr Addr#
a1) (Ptr Addr#
a2) = Int# -> Int
I# (Addr# -> Addr# -> Int#
minusAddr# Addr#
a1 Addr#
a2)
type role FunPtr phantom
data FunPtr a = FunPtr Addr# deriving (FunPtr a -> FunPtr a -> Bool
forall a. FunPtr a -> FunPtr a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FunPtr a -> FunPtr a -> Bool
$c/= :: forall a. FunPtr a -> FunPtr a -> Bool
== :: FunPtr a -> FunPtr a -> Bool
$c== :: forall a. FunPtr a -> FunPtr a -> Bool
Eq, FunPtr a -> FunPtr a -> Bool
FunPtr a -> FunPtr a -> Ordering
forall a. Eq (FunPtr a)
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. FunPtr a -> FunPtr a -> Bool
forall a. FunPtr a -> FunPtr a -> Ordering
forall a. FunPtr a -> FunPtr a -> FunPtr a
min :: FunPtr a -> FunPtr a -> FunPtr a
$cmin :: forall a. FunPtr a -> FunPtr a -> FunPtr a
max :: FunPtr a -> FunPtr a -> FunPtr a
$cmax :: forall a. FunPtr a -> FunPtr a -> FunPtr a
>= :: FunPtr a -> FunPtr a -> Bool
$c>= :: forall a. FunPtr a -> FunPtr a -> Bool
> :: FunPtr a -> FunPtr a -> Bool
$c> :: forall a. FunPtr a -> FunPtr a -> Bool
<= :: FunPtr a -> FunPtr a -> Bool
$c<= :: forall a. FunPtr a -> FunPtr a -> Bool
< :: FunPtr a -> FunPtr a -> Bool
$c< :: forall a. FunPtr a -> FunPtr a -> Bool
compare :: FunPtr a -> FunPtr a -> Ordering
$ccompare :: forall a. FunPtr a -> FunPtr a -> Ordering
Ord)
nullFunPtr :: FunPtr a
nullFunPtr :: forall a. FunPtr a
nullFunPtr = forall a. Addr# -> FunPtr a
FunPtr Addr#
nullAddr#
castFunPtr :: FunPtr a -> FunPtr b
castFunPtr :: forall a b. FunPtr a -> FunPtr b
castFunPtr = coerce :: forall a b. Coercible a b => a -> b
coerce
castFunPtrToPtr :: FunPtr a -> Ptr b
castFunPtrToPtr :: forall a b. FunPtr a -> Ptr b
castFunPtrToPtr (FunPtr Addr#
addr) = forall a. Addr# -> Ptr a
Ptr Addr#
addr
castPtrToFunPtr :: Ptr a -> FunPtr b
castPtrToFunPtr :: forall a b. Ptr a -> FunPtr b
castPtrToFunPtr (Ptr Addr#
addr) = forall a. Addr# -> FunPtr a
FunPtr Addr#
addr
instance Show (Ptr a) where
showsPrec :: Int -> Ptr a -> ShowS
showsPrec Int
_ (Ptr Addr#
a) String
rs = ShowS
pad_out (forall a. (Integral a, Show a) => a -> ShowS
showHex (Word# -> Integer
integerFromWord#(Int# -> Word#
int2Word#(Addr# -> Int#
addr2Int# Addr#
a))) String
"")
where
pad_out :: ShowS
pad_out String
ls =
Char
'0'forall a. a -> [a] -> [a]
:Char
'x'forall a. a -> [a] -> [a]
:(forall a. Int -> a -> [a]
replicate (Int
2forall a. Num a => a -> a -> a
*SIZEOF_HSPTR - length ls) '0') ++ ls ++ rs
instance Show (FunPtr a) where
showsPrec :: Int -> FunPtr a -> ShowS
showsPrec Int
p = forall a. Show a => Int -> a -> ShowS
showsPrec Int
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. FunPtr a -> Ptr b
castFunPtrToPtr