primitive-0.7.2.0: Primitive memory-related operations
Copyright(c) Roman Leshchinskiy 2009-2012
LicenseBSD-style
MaintainerRoman Leshchinskiy <rl@cse.unsw.edu.au>
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Data.Primitive.Types

Description

Basic types and classes for primitive array operations

Synopsis

Documentation

class Prim a where #

Class of types supporting primitive array operations. This includes interfacing with GC-managed memory (functions suffixed with ByteArray#) and interfacing with unmanaged memory (functions suffixed with Addr#). Endianness is platform-dependent.

Methods

sizeOf# :: a -> Int# #

Size of values of type a. The argument is not used.

alignment# :: a -> Int# #

Alignment of values of type a. The argument is not used.

indexByteArray# :: ByteArray# -> Int# -> a #

Read a value from the array. The offset is in elements of type a rather than in bytes.

readByteArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, a #) #

Read a value from the mutable array. The offset is in elements of type a rather than in bytes.

writeByteArray# :: MutableByteArray# s -> Int# -> a -> State# s -> State# s #

Write a value to the mutable array. The offset is in elements of type a rather than in bytes.

setByteArray# :: MutableByteArray# s -> Int# -> Int# -> a -> State# s -> State# s #

Fill a slice of the mutable array with a value. The offset and length of the chunk are in elements of type a rather than in bytes.

indexOffAddr# :: Addr# -> Int# -> a #

Read a value from a memory position given by an address and an offset. The memory block the address refers to must be immutable. The offset is in elements of type a rather than in bytes.

readOffAddr# :: Addr# -> Int# -> State# s -> (# State# s, a #) #

Read a value from a memory position given by an address and an offset. The offset is in elements of type a rather than in bytes.

writeOffAddr# :: Addr# -> Int# -> a -> State# s -> State# s #

Write a value to a memory position given by an address and an offset. The offset is in elements of type a rather than in bytes.

setOffAddr# :: Addr# -> Int# -> Int# -> a -> State# s -> State# s #

Fill a memory block given by an address, an offset and a length. The offset and length are in elements of type a rather than in bytes.

Instances

Instances details
Prim Char # 
Instance details

Defined in Data.Primitive.Types

Prim Double # 
Instance details

Defined in Data.Primitive.Types

Prim Float # 
Instance details

Defined in Data.Primitive.Types

Prim Int # 
Instance details

Defined in Data.Primitive.Types

Prim Int8 # 
Instance details

Defined in Data.Primitive.Types

Prim Int16 # 
Instance details

Defined in Data.Primitive.Types

Prim Int32 # 
Instance details

Defined in Data.Primitive.Types

Prim Int64 # 
Instance details

Defined in Data.Primitive.Types

Prim Word # 
Instance details

Defined in Data.Primitive.Types

Prim Word8 # 
Instance details

Defined in Data.Primitive.Types

Prim Word16 # 
Instance details

Defined in Data.Primitive.Types

Prim Word32 # 
Instance details

Defined in Data.Primitive.Types

Prim Word64 # 
Instance details

Defined in Data.Primitive.Types

Prim CDev # 
Instance details

Defined in Data.Primitive.Types

Prim CIno # 
Instance details

Defined in Data.Primitive.Types

Prim CMode # 
Instance details

Defined in Data.Primitive.Types

Prim COff # 
Instance details

Defined in Data.Primitive.Types

Prim CPid # 
Instance details

Defined in Data.Primitive.Types

Prim CSsize # 
Instance details

Defined in Data.Primitive.Types

Prim CGid # 
Instance details

Defined in Data.Primitive.Types

Prim CNlink # 
Instance details

Defined in Data.Primitive.Types

Prim CUid # 
Instance details

Defined in Data.Primitive.Types

Prim CCc # 
Instance details

Defined in Data.Primitive.Types

Prim CSpeed # 
Instance details

Defined in Data.Primitive.Types

Prim CTcflag # 
Instance details

Defined in Data.Primitive.Types

Prim CRLim # 
Instance details

Defined in Data.Primitive.Types

Prim CBlkSize # 
Instance details

Defined in Data.Primitive.Types

Prim CBlkCnt # 
Instance details

Defined in Data.Primitive.Types

Prim CClockId # 
Instance details

Defined in Data.Primitive.Types

Prim CFsBlkCnt # 
Instance details

Defined in Data.Primitive.Types

Prim CFsFilCnt # 
Instance details

Defined in Data.Primitive.Types

Prim CId # 
Instance details

Defined in Data.Primitive.Types

Prim CKey # 
Instance details

Defined in Data.Primitive.Types

Prim CTimer # 
Instance details

Defined in Data.Primitive.Types

Prim Fd # 
Instance details

Defined in Data.Primitive.Types

Prim CChar # 
Instance details

Defined in Data.Primitive.Types

Prim CSChar # 
Instance details

Defined in Data.Primitive.Types

Prim CUChar # 
Instance details

Defined in Data.Primitive.Types

Prim CShort # 
Instance details

Defined in Data.Primitive.Types

Prim CUShort # 
Instance details

Defined in Data.Primitive.Types

Prim CInt # 
Instance details

Defined in Data.Primitive.Types

Prim CUInt # 
Instance details

Defined in Data.Primitive.Types

Prim CLong # 
Instance details

Defined in Data.Primitive.Types

Prim CULong # 
Instance details

Defined in Data.Primitive.Types

Prim CLLong # 
Instance details

Defined in Data.Primitive.Types

Prim CULLong # 
Instance details

Defined in Data.Primitive.Types

Prim CBool # 
Instance details

Defined in Data.Primitive.Types

Prim CFloat # 
Instance details

Defined in Data.Primitive.Types

Prim CDouble # 
Instance details

Defined in Data.Primitive.Types

Prim CPtrdiff # 
Instance details

Defined in Data.Primitive.Types

Prim CSize # 
Instance details

Defined in Data.Primitive.Types

Prim CWchar # 
Instance details

Defined in Data.Primitive.Types

Prim CSigAtomic # 
Instance details

Defined in Data.Primitive.Types

Prim CClock # 
Instance details

Defined in Data.Primitive.Types

Prim CTime # 
Instance details

Defined in Data.Primitive.Types

Prim CUSeconds # 
Instance details

Defined in Data.Primitive.Types

Prim CSUSeconds # 
Instance details

Defined in Data.Primitive.Types

Prim CIntPtr # 
Instance details

Defined in Data.Primitive.Types

Prim CUIntPtr # 
Instance details

Defined in Data.Primitive.Types

Prim CIntMax # 
Instance details

Defined in Data.Primitive.Types

Prim CUIntMax # 
Instance details

Defined in Data.Primitive.Types

Prim WordPtr #

Since: 0.7.1.0

Instance details

Defined in Data.Primitive.Types

Prim IntPtr #

Since: 0.7.1.0

Instance details

Defined in Data.Primitive.Types

Prim (StablePtr a) # 
Instance details

Defined in Data.Primitive.Types

Prim (Ptr a) # 
Instance details

Defined in Data.Primitive.Types

Methods

sizeOf# :: Ptr a -> Int# #

alignment# :: Ptr a -> Int# #

indexByteArray# :: ByteArray# -> Int# -> Ptr a #

readByteArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, Ptr a #) #

writeByteArray# :: MutableByteArray# s -> Int# -> Ptr a -> State# s -> State# s #

setByteArray# :: MutableByteArray# s -> Int# -> Int# -> Ptr a -> State# s -> State# s #

indexOffAddr# :: Addr# -> Int# -> Ptr a #

readOffAddr# :: Addr# -> Int# -> State# s -> (# State# s, Ptr a #) #

writeOffAddr# :: Addr# -> Int# -> Ptr a -> State# s -> State# s #

setOffAddr# :: Addr# -> Int# -> Int# -> Ptr a -> State# s -> State# s #

Prim (FunPtr a) # 
Instance details

Defined in Data.Primitive.Types

Prim a => Prim (Min a) #

Since: 0.6.5.0

Instance details

Defined in Data.Primitive.Types

Methods

sizeOf# :: Min a -> Int# #

alignment# :: Min a -> Int# #

indexByteArray# :: ByteArray# -> Int# -> Min a #

readByteArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, Min a #) #

writeByteArray# :: MutableByteArray# s -> Int# -> Min a -> State# s -> State# s #

setByteArray# :: MutableByteArray# s -> Int# -> Int# -> Min a -> State# s -> State# s #

indexOffAddr# :: Addr# -> Int# -> Min a #

readOffAddr# :: Addr# -> Int# -> State# s -> (# State# s, Min a #) #

writeOffAddr# :: Addr# -> Int# -> Min a -> State# s -> State# s #

setOffAddr# :: Addr# -> Int# -> Int# -> Min a -> State# s -> State# s #

Prim a => Prim (Max a) #

Since: 0.6.5.0

Instance details

Defined in Data.Primitive.Types

Methods

sizeOf# :: Max a -> Int# #

alignment# :: Max a -> Int# #

indexByteArray# :: ByteArray# -> Int# -> Max a #

readByteArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, Max a #) #

writeByteArray# :: MutableByteArray# s -> Int# -> Max a -> State# s -> State# s #

setByteArray# :: MutableByteArray# s -> Int# -> Int# -> Max a -> State# s -> State# s #

indexOffAddr# :: Addr# -> Int# -> Max a #

readOffAddr# :: Addr# -> Int# -> State# s -> (# State# s, Max a #) #

writeOffAddr# :: Addr# -> Int# -> Max a -> State# s -> State# s #

setOffAddr# :: Addr# -> Int# -> Int# -> Max a -> State# s -> State# s #

Prim a => Prim (First a) #

Since: 0.6.5.0

Instance details

Defined in Data.Primitive.Types

Prim a => Prim (Last a) #

Since: 0.6.5.0

Instance details

Defined in Data.Primitive.Types

Prim a => Prim (Identity a) #

Since: 0.6.5.0

Instance details

Defined in Data.Primitive.Types

Prim a => Prim (Dual a) #

Since: 0.6.5.0

Instance details

Defined in Data.Primitive.Types

Prim a => Prim (Sum a) #

Since: 0.6.5.0

Instance details

Defined in Data.Primitive.Types

Methods

sizeOf# :: Sum a -> Int# #

alignment# :: Sum a -> Int# #

indexByteArray# :: ByteArray# -> Int# -> Sum a #

readByteArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, Sum a #) #

writeByteArray# :: MutableByteArray# s -> Int# -> Sum a -> State# s -> State# s #

setByteArray# :: MutableByteArray# s -> Int# -> Int# -> Sum a -> State# s -> State# s #

indexOffAddr# :: Addr# -> Int# -> Sum a #

readOffAddr# :: Addr# -> Int# -> State# s -> (# State# s, Sum a #) #

writeOffAddr# :: Addr# -> Int# -> Sum a -> State# s -> State# s #

setOffAddr# :: Addr# -> Int# -> Int# -> Sum a -> State# s -> State# s #

Prim a => Prim (Product a) #

Since: 0.6.5.0

Instance details

Defined in Data.Primitive.Types

Prim a => Prim (Down a) #

Since: 0.6.5.0

Instance details

Defined in Data.Primitive.Types

Prim a => Prim (Const a b) #

Since: 0.6.5.0

Instance details

Defined in Data.Primitive.Types

Methods

sizeOf# :: Const a b -> Int# #

alignment# :: Const a b -> Int# #

indexByteArray# :: ByteArray# -> Int# -> Const a b #

readByteArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, Const a b #) #

writeByteArray# :: MutableByteArray# s -> Int# -> Const a b -> State# s -> State# s #

setByteArray# :: MutableByteArray# s -> Int# -> Int# -> Const a b -> State# s -> State# s #

indexOffAddr# :: Addr# -> Int# -> Const a b #

readOffAddr# :: Addr# -> Int# -> State# s -> (# State# s, Const a b #) #

writeOffAddr# :: Addr# -> Int# -> Const a b -> State# s -> State# s #

setOffAddr# :: Addr# -> Int# -> Int# -> Const a b -> State# s -> State# s #

sizeOf :: Prim a => a -> Int #

Size of values of type a. The argument is not used.

This function has existed since 0.1, but was moved from Primitive to Types in version 0.6.3.0

alignment :: Prim a => a -> Int #

Alignment of values of type a. The argument is not used.

This function has existed since 0.1, but was moved from Primitive to Types in version 0.6.3.0

defaultSetByteArray# :: Prim a => MutableByteArray# s -> Int# -> Int# -> a -> State# s -> State# s #

An implementation of setByteArray# that calls writeByteArray# to set each element. This is helpful when writing a Prim instance for a multi-word data type for which there is no cpu-accelerated way to broadcast a value to contiguous memory. It is typically used alongside defaultSetOffAddr#. For example:

data Trip = Trip Int Int Int

instance Prim Trip
  sizeOf# _ = 3# *# sizeOf# (undefined :: Int)
  alignment# _ = alignment# (undefined :: Int)
  indexByteArray# arr# i# = ...
  readByteArray# arr# i# = ...
  writeByteArray# arr# i# (Trip a b c) =
    \s0 -> case writeByteArray# arr# (3# *# i#) a s0 of
       s1 -> case writeByteArray# arr# ((3# *# i#) +# 1#) b s1 of
         s2 -> case writeByteArray# arr# ((3# *# i#) +# 2# ) c s2 of
           s3 -> s3
  setByteArray# = defaultSetByteArray#
  indexOffAddr# addr# i# = ...
  readOffAddr# addr# i# = ...
  writeOffAddr# addr# i# (Trip a b c) =
    \s0 -> case writeOffAddr# addr# (3# *# i#) a s0 of
       s1 -> case writeOffAddr# addr# ((3# *# i#) +# 1#) b s1 of
         s2 -> case writeOffAddr# addr# ((3# *# i#) +# 2# ) c s2 of
           s3 -> s3
  setOffAddr# = defaultSetOffAddr#

defaultSetOffAddr# :: Prim a => Addr# -> Int# -> Int# -> a -> State# s -> State# s #

An implementation of setOffAddr# that calls writeOffAddr# to set each element. The documentation of defaultSetByteArray# provides an example of how to use this.

newtype PrimStorable a #

Newtype that uses a Prim instance to give rise to a Storable instance. This type is intended to be used with the DerivingVia extension available in GHC 8.6 and up. For example, consider a user-defined Prim instance for a multi-word data type.

data Uuid = Uuid Word64 Word64
  deriving Storable via (PrimStorable Uuid)
instance Prim Uuid where ...

Writing the Prim instance is tedious and unavoidable, but the Storable instance comes for free once the Prim instance is written.

Constructors

PrimStorable 

Fields

Instances

Instances details
Prim a => Storable (PrimStorable a) # 
Instance details

Defined in Data.Primitive.Types

data Ptr a #

A value of type Ptr a represents a pointer to an object, or an array of objects, which may be marshalled to or from Haskell values of type a.

The type a will often be an instance of class Storable which provides the marshalling operations. However this is not essential, and you can provide your own operations to access the pointer. For example you might write small foreign functions to get or set the fields of a C struct.

Constructors

Ptr Addr# 

Instances

Instances details
NFData1 Ptr

Since: deepseq-1.4.3.0

Instance details

Defined in Control.DeepSeq

Methods

liftRnf :: (a -> ()) -> Ptr a -> () #

Generic1 (URec (Ptr ()) :: k -> Type)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Associated Types

type Rep1 (URec (Ptr ())) :: k -> Type #

Methods

from1 :: forall (a :: k0). URec (Ptr ()) a -> Rep1 (URec (Ptr ())) a #

to1 :: forall (a :: k0). Rep1 (URec (Ptr ())) a -> URec (Ptr ()) a #

Eq (Ptr a)

Since: base-2.1

Instance details

Defined in GHC.Ptr

Methods

(==) :: Ptr a -> Ptr a -> Bool #

(/=) :: Ptr a -> Ptr a -> Bool #

Data a => Data (Ptr a)

Since: base-4.8.0.0

Instance details

Defined in Data.Data

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Ptr a -> c (Ptr a) #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Ptr a) #

toConstr :: Ptr a -> Constr #

dataTypeOf :: Ptr a -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Ptr a)) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Ptr a)) #

gmapT :: (forall b. Data b => b -> b) -> Ptr a -> Ptr a #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Ptr a -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Ptr a -> r #

gmapQ :: (forall d. Data d => d -> u) -> Ptr a -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Ptr a -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Ptr a -> m (Ptr a) #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Ptr a -> m (Ptr a) #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Ptr a -> m (Ptr a) #

Ord (Ptr a)

Since: base-2.1

Instance details

Defined in GHC.Ptr

Methods

compare :: Ptr a -> Ptr a -> Ordering #

(<) :: Ptr a -> Ptr a -> Bool #

(<=) :: Ptr a -> Ptr a -> Bool #

(>) :: Ptr a -> Ptr a -> Bool #

(>=) :: Ptr a -> Ptr a -> Bool #

max :: Ptr a -> Ptr a -> Ptr a #

min :: Ptr a -> Ptr a -> Ptr a #

Show (Ptr a)

Since: base-2.1

Instance details

Defined in GHC.Ptr

Methods

showsPrec :: Int -> Ptr a -> ShowS #

show :: Ptr a -> String #

showList :: [Ptr a] -> ShowS #

Foldable (UAddr :: Type -> Type)

Since: base-4.9.0.0

Instance details

Defined in Data.Foldable

Methods

fold :: Monoid m => UAddr m -> m #

foldMap :: Monoid m => (a -> m) -> UAddr a -> m #

foldMap' :: Monoid m => (a -> m) -> UAddr a -> m #

foldr :: (a -> b -> b) -> b -> UAddr a -> b #

foldr' :: (a -> b -> b) -> b -> UAddr a -> b #

foldl :: (b -> a -> b) -> b -> UAddr a -> b #

foldl' :: (b -> a -> b) -> b -> UAddr a -> b #

foldr1 :: (a -> a -> a) -> UAddr a -> a #

foldl1 :: (a -> a -> a) -> UAddr a -> a #

toList :: UAddr a -> [a] #

null :: UAddr a -> Bool #

length :: UAddr a -> Int #

elem :: Eq a => a -> UAddr a -> Bool #

maximum :: Ord a => UAddr a -> a #

minimum :: Ord a => UAddr a -> a #

sum :: Num a => UAddr a -> a #

product :: Num a => UAddr a -> a #

Traversable (UAddr :: Type -> Type)

Since: base-4.9.0.0

Instance details

Defined in Data.Traversable

Methods

traverse :: Applicative f => (a -> f b) -> UAddr a -> f (UAddr b) #

sequenceA :: Applicative f => UAddr (f a) -> f (UAddr a) #

mapM :: Monad m => (a -> m b) -> UAddr a -> m (UAddr b) #

sequence :: Monad m => UAddr (m a) -> m (UAddr a) #

Storable (Ptr a)

Since: base-2.1

Instance details

Defined in Foreign.Storable

Methods

sizeOf :: Ptr a -> Int #

alignment :: Ptr a -> Int #

peekElemOff :: Ptr (Ptr a) -> Int -> IO (Ptr a) #

pokeElemOff :: Ptr (Ptr a) -> Int -> Ptr a -> IO () #

peekByteOff :: Ptr b -> Int -> IO (Ptr a) #

pokeByteOff :: Ptr b -> Int -> Ptr a -> IO () #

peek :: Ptr (Ptr a) -> IO (Ptr a) #

poke :: Ptr (Ptr a) -> Ptr a -> IO () #

NFData (Ptr a)

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Ptr a -> () #

Prim (Ptr a) # 
Instance details

Defined in Data.Primitive.Types

Methods

sizeOf# :: Ptr a -> Int# #

alignment# :: Ptr a -> Int# #

indexByteArray# :: ByteArray# -> Int# -> Ptr a #

readByteArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, Ptr a #) #

writeByteArray# :: MutableByteArray# s -> Int# -> Ptr a -> State# s -> State# s #

setByteArray# :: MutableByteArray# s -> Int# -> Int# -> Ptr a -> State# s -> State# s #

indexOffAddr# :: Addr# -> Int# -> Ptr a #

readOffAddr# :: Addr# -> Int# -> State# s -> (# State# s, Ptr a #) #

writeOffAddr# :: Addr# -> Int# -> Ptr a -> State# s -> State# s #

setOffAddr# :: Addr# -> Int# -> Int# -> Ptr a -> State# s -> State# s #

Functor (URec (Ptr ()) :: Type -> Type)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

fmap :: (a -> b) -> URec (Ptr ()) a -> URec (Ptr ()) b #

(<$) :: a -> URec (Ptr ()) b -> URec (Ptr ()) a #

Eq (URec (Ptr ()) p)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

(==) :: URec (Ptr ()) p -> URec (Ptr ()) p -> Bool #

(/=) :: URec (Ptr ()) p -> URec (Ptr ()) p -> Bool #

Ord (URec (Ptr ()) p)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

compare :: URec (Ptr ()) p -> URec (Ptr ()) p -> Ordering #

(<) :: URec (Ptr ()) p -> URec (Ptr ()) p -> Bool #

(<=) :: URec (Ptr ()) p -> URec (Ptr ()) p -> Bool #

(>) :: URec (Ptr ()) p -> URec (Ptr ()) p -> Bool #

(>=) :: URec (Ptr ()) p -> URec (Ptr ()) p -> Bool #

max :: URec (Ptr ()) p -> URec (Ptr ()) p -> URec (Ptr ()) p #

min :: URec (Ptr ()) p -> URec (Ptr ()) p -> URec (Ptr ()) p #

Generic (URec (Ptr ()) p)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Associated Types

type Rep (URec (Ptr ()) p) :: Type -> Type #

Methods

from :: URec (Ptr ()) p -> Rep (URec (Ptr ()) p) x #

to :: Rep (URec (Ptr ()) p) x -> URec (Ptr ()) p #

data URec (Ptr ()) (p :: k)

Used for marking occurrences of Addr#

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

data URec (Ptr ()) (p :: k) = UAddr {}
type Rep1 (URec (Ptr ()) :: k -> Type) 
Instance details

Defined in GHC.Generics

type Rep1 (URec (Ptr ()) :: k -> Type) = D1 ('MetaData "URec" "GHC.Generics" "base" 'False) (C1 ('MetaCons "UAddr" 'PrefixI 'True) (S1 ('MetaSel ('Just "uAddr#") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (UAddr :: k -> Type)))
type Rep (URec (Ptr ()) p) 
Instance details

Defined in GHC.Generics

type Rep (URec (Ptr ()) p) = D1 ('MetaData "URec" "GHC.Generics" "base" 'False) (C1 ('MetaCons "UAddr" 'PrefixI 'True) (S1 ('MetaSel ('Just "uAddr#") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (UAddr :: Type -> Type)))