primal-memory-0.1.0.0: Unified interface for memory managemenet.

Copyright(c) Alexey Kuleshevich 2020
LicenseBSD3
MaintainerAlexey Kuleshevich <alexey@kuleshevi.ch>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Data.Prim.Memory

Contents

Description

 
Synopsis

Documentation

data Pinned Source #

In Haskell there is a distinction between pinned or unpinned memory.

Pinned memory is such, when allocated, it is guaranteed not to move throughout the lifetime of a program. In other words the address pointer that refers to allocated bytes will not change until it gets garbage collected because it is no longer referenced by anything. Unpinned memory on the other hand can be moved around during GC, which helps to reduce memory fragmentation.

Pinned/unpinnned choice during allocation is a bit of a lie, because when attempt is made to allocate memory as unpinned, but requested size is a bit more than a certain threashold (somewhere around 3KiB) it might still be allocated as pinned. Because of that fact through out the "primal" universe there is a distinction between memory that is either Pinned or Inconclusive.

It is possible to use one of toPinnedBytes or toPinnedMBytes to get a conclusive type.

Since: 0.1.0

Constructors

Pin 
Inc 

Immutable

data Bytes (p :: Pinned) Source #

An immutable region of memory which was allocated either as pinned or unpinned.

Constructor is not exported for safety. Violating type level Pinned kind is very dangerous. Type safe constructor fromByteArray# and unwrapper toByteArray# should be used instead. As a backdoor, of course, the actual constructor is available in Data.Prim.Memory.Internal module and specially unsafe function castPinnedBytes was crafted.

Instances
PtrAccess s (Bytes Pin) Source #

Read-only access, but it is not enforced.

Instance details

Defined in Data.Prim.Memory.ForeignPtr

Methods

toForeignPtr :: MonadPrim s m => Bytes Pin -> m (ForeignPtr a) Source #

withPtrAccess :: MonadPrim s m => Bytes Pin -> (Ptr a -> m b) -> m b Source #

withNoHaltPtrAccess :: MonadUnliftPrim s m => Bytes Pin -> (Ptr a -> m b) -> m b Source #

Typeable p => IsList (Bytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Associated Types

type Item (Bytes p) :: Type #

Methods

fromList :: [Item (Bytes p)] -> Bytes p #

fromListN :: Int -> [Item (Bytes p)] -> Bytes p #

toList :: Bytes p -> [Item (Bytes p)] #

Eq (Bytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Methods

(==) :: Bytes p -> Bytes p -> Bool #

(/=) :: Bytes p -> Bytes p -> Bool #

Ord (Bytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Methods

compare :: Bytes p -> Bytes p -> Ordering #

(<) :: Bytes p -> Bytes p -> Bool #

(<=) :: Bytes p -> Bytes p -> Bool #

(>) :: Bytes p -> Bytes p -> Bool #

(>=) :: Bytes p -> Bytes p -> Bool #

max :: Bytes p -> Bytes p -> Bytes p #

min :: Bytes p -> Bytes p -> Bytes p #

Show (Bytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Methods

showsPrec :: Int -> Bytes p -> ShowS #

show :: Bytes p -> String #

showList :: [Bytes p] -> ShowS #

Typeable p => Semigroup (Bytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Methods

(<>) :: Bytes p -> Bytes p -> Bytes p #

sconcat :: NonEmpty (Bytes p) -> Bytes p #

stimes :: Integral b => b -> Bytes p -> Bytes p #

Typeable p => Monoid (Bytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Methods

mempty :: Bytes p #

mappend :: Bytes p -> Bytes p -> Bytes p #

mconcat :: [Bytes p] -> Bytes p #

NFData (Bytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Bytes.Internal

Methods

rnf :: Bytes p -> () #

MemRead (Bytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Methods

byteCountMem :: Bytes p -> Count Word8 Source #

indexOffMem :: Prim e => Bytes p -> Off e -> e Source #

indexByteOffMem :: Prim e => Bytes p -> Off Word8 -> e Source #

copyByteOffToMBytesMem :: (MonadPrim s m, Prim e) => Bytes p -> Off Word8 -> MBytes p0 s -> Off Word8 -> Count e -> m () Source #

copyByteOffToPtrMem :: (MonadPrim s m, Prim e) => Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m () Source #

compareByteOffToPtrMem :: (MonadPrim s m, Prim e) => Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering Source #

compareByteOffToBytesMem :: (MonadPrim s m, Prim e) => Bytes p -> Off Word8 -> Bytes p0 -> Off Word8 -> Count e -> m Ordering Source #

compareByteOffMem :: (MemRead r', Prim e) => r' -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering Source #

type Item (Bytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

type Item (Bytes p) = Word8

class MemRead r Source #

Instances
MemRead ShortByteString Source # 
Instance details

Defined in Data.Prim.Memory.Internal

MemRead ByteString Source # 
Instance details

Defined in Data.Prim.Memory.Internal

MemRead (Bytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Methods

byteCountMem :: Bytes p -> Count Word8 Source #

indexOffMem :: Prim e => Bytes p -> Off e -> e Source #

indexByteOffMem :: Prim e => Bytes p -> Off Word8 -> e Source #

copyByteOffToMBytesMem :: (MonadPrim s m, Prim e) => Bytes p -> Off Word8 -> MBytes p0 s -> Off Word8 -> Count e -> m () Source #

copyByteOffToPtrMem :: (MonadPrim s m, Prim e) => Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m () Source #

compareByteOffToPtrMem :: (MonadPrim s m, Prim e) => Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering Source #

compareByteOffToBytesMem :: (MonadPrim s m, Prim e) => Bytes p -> Off Word8 -> Bytes p0 -> Off Word8 -> Count e -> m Ordering Source #

compareByteOffMem :: (MemRead r', Prim e) => r' -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering Source #

MemRead (Addr e) Source # 
Instance details

Defined in Data.Prim.Memory.Addr

Methods

byteCountMem :: Addr e -> Count Word8 Source #

indexOffMem :: Prim e0 => Addr e -> Off e0 -> e0 Source #

indexByteOffMem :: Prim e0 => Addr e -> Off Word8 -> e0 Source #

copyByteOffToMBytesMem :: (MonadPrim s m, Prim e0) => Addr e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e0 -> m () Source #

copyByteOffToPtrMem :: (MonadPrim s m, Prim e0) => Addr e -> Off Word8 -> Ptr e0 -> Off Word8 -> Count e0 -> m () Source #

compareByteOffToPtrMem :: (MonadPrim s m, Prim e0) => Addr e -> Off Word8 -> Ptr e0 -> Off Word8 -> Count e0 -> m Ordering Source #

compareByteOffToBytesMem :: (MonadPrim s m, Prim e0) => Addr e -> Off Word8 -> Bytes p -> Off Word8 -> Count e0 -> m Ordering Source #

compareByteOffMem :: (MemRead r', Prim e0) => r' -> Off Word8 -> Addr e -> Off Word8 -> Count e0 -> Ordering Source #

MemRead (ByteArray p e) Source # 
Instance details

Defined in Data.Prim.Memory.ByteArray

Methods

byteCountMem :: ByteArray p e -> Count Word8 Source #

indexOffMem :: Prim e0 => ByteArray p e -> Off e0 -> e0 Source #

indexByteOffMem :: Prim e0 => ByteArray p e -> Off Word8 -> e0 Source #

copyByteOffToMBytesMem :: (MonadPrim s m, Prim e0) => ByteArray p e -> Off Word8 -> MBytes p0 s -> Off Word8 -> Count e0 -> m () Source #

copyByteOffToPtrMem :: (MonadPrim s m, Prim e0) => ByteArray p e -> Off Word8 -> Ptr e0 -> Off Word8 -> Count e0 -> m () Source #

compareByteOffToPtrMem :: (MonadPrim s m, Prim e0) => ByteArray p e -> Off Word8 -> Ptr e0 -> Off Word8 -> Count e0 -> m Ordering Source #

compareByteOffToBytesMem :: (MonadPrim s m, Prim e0) => ByteArray p e -> Off Word8 -> Bytes p0 -> Off Word8 -> Count e0 -> m Ordering Source #

compareByteOffMem :: (MemRead r', Prim e0) => r' -> Off Word8 -> ByteArray p e -> Off Word8 -> Count e0 -> Ordering Source #

countMem Source #

Arguments

:: (MemRead r, Prim e) 
=> r

Read-only memory type

-> Count e 

Figure out how many elements can fit into the region of memory. It is possible that there is a remainder of bytes left, see countRemMem for getting that too.

Examples

Expand
>>> b = fromListMem [0 .. 5 :: Word8] :: Bytes 'Pin
>>> b
[0x00,0x01,0x02,0x03,0x04,0x05]
>>> countMem b :: Count Word16
Count {unCount = 3}
>>> countMem b :: Count Word32
Count {unCount = 1}

Since: 0.1.0

countRemMem :: forall e r. (MemRead r, Prim e) => r -> (Count e, Count Word8) Source #

Compute how many elements and a byte size remainder that can fit into the region of memory.

Examples

Expand
>>> b = fromListMem [0 .. 5 :: Word8] :: Bytes 'Pin
>>> b
[0x00,0x01,0x02,0x03,0x04,0x05]
>>> countRemMem @Word16 b
(Count {unCount = 3},0)
>>> countRemMem @Word32 b
(Count {unCount = 1},2)

Since: 0.1.0

indexOffMem :: (MemRead r, Prim e) => r -> Off e -> e Source #

eqMem :: (MemRead r1, MemRead r2) => r1 -> r2 -> Bool Source #

compareMem Source #

Arguments

:: (MemRead r1, MemRead r2, Prim e) 
=> r1

First region of memory

-> Off e

Offset in number of elements into the first region

-> r2

Second region of memory

-> Off e

Offset in number of elements into the second region

-> Count e

Number of elements to compare

-> Ordering 

Compare two regions of memory byte-by-byte. It will return EQ whenever both regions are exactly the same and LT or GT as soon as the first byte is reached that is less than or greater than respectfully in the first region when compared to the second one. It is safe for both regions to refer to the same part of memory, since this is a pure function and both regions of memory are read-only.

Mutable

data MBytes (p :: Pinned) s Source #

Mutable region of memory which was allocated either as pinned or unpinned.

Constructor is not exported for safety. Violating type level Pinned kind is very dangerous. Type safe constructor fromMutableByteArray# and unwrapper toMutableByteArray# should be used instead. As a backdoor, of course, the actual constructor is available in Data.Prim.Memory.Internal module and specially unsafe function castPinnedMBytes was crafted.

Instances
PtrAccess s (MBytes Pin s) Source # 
Instance details

Defined in Data.Prim.Memory.ForeignPtr

Methods

toForeignPtr :: MonadPrim s m => MBytes Pin s -> m (ForeignPtr a) Source #

withPtrAccess :: MonadPrim s m => MBytes Pin s -> (Ptr a -> m b) -> m b Source #

withNoHaltPtrAccess :: MonadUnliftPrim s m => MBytes Pin s -> (Ptr a -> m b) -> m b Source #

MemWrite (MBytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Methods

readOffMem :: (MonadPrim s m, Prim e) => MBytes p s -> Off e -> m e Source #

readByteOffMem :: (MonadPrim s m, Prim e) => MBytes p s -> Off Word8 -> m e Source #

writeOffMem :: (MonadPrim s m, Prim e) => MBytes p s -> Off e -> e -> m () Source #

writeByteOffMem :: (MonadPrim s m, Prim e) => MBytes p s -> Off Word8 -> e -> m () Source #

moveByteOffToMBytesMem :: (MonadPrim s m, Prim e) => MBytes p s -> Off Word8 -> MBytes p0 s -> Off Word8 -> Count e -> m () Source #

moveByteOffToPtrMem :: (MonadPrim s m, Prim e) => MBytes p s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m () Source #

copyByteOffMem :: (MonadPrim s m, MemRead r, Prim e) => r -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m () Source #

moveByteOffMem :: (MonadPrim s m, MemWrite w', Prim e) => w' s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m () Source #

setMem :: (MonadPrim s m, Prim e) => MBytes p s -> Off e -> Count e -> e -> m () Source #

Typeable p => MemAlloc (MBytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Associated Types

type FrozenMem (MBytes p) = (fa :: Type) Source #

Methods

getByteCountMem :: MonadPrim s m => MBytes p s -> m (Count Word8) Source #

allocByteCountMem :: MonadPrim s m => Count Word8 -> m (MBytes p s) Source #

thawMem :: MonadPrim s m => FrozenMem (MBytes p) -> m (MBytes p s) Source #

freezeMem :: MonadPrim s m => MBytes p s -> m (FrozenMem (MBytes p)) Source #

resizeMem :: (MonadPrim s m, Prim e) => MBytes p s -> Count e -> m (MBytes p s) Source #

NFData (MBytes p s) Source # 
Instance details

Defined in Data.Prim.Memory.Bytes.Internal

Methods

rnf :: MBytes p s -> () #

type FrozenMem (MBytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

type FrozenMem (MBytes p) = Bytes p

class (MemRead (FrozenMem a), MemWrite a) => MemAlloc a Source #

Generalized memory allocation and pure/mutable state conversion.

Minimal complete definition

getByteCountMem, allocByteCountMem, thawMem, freezeMem

Associated Types

type FrozenMem a = (fa :: Type) | fa -> a Source #

Instances
MemAlloc MByteString Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Associated Types

type FrozenMem MByteString = (fa :: Type) Source #

Typeable p => MemAlloc (MBytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Associated Types

type FrozenMem (MBytes p) = (fa :: Type) Source #

Methods

getByteCountMem :: MonadPrim s m => MBytes p s -> m (Count Word8) Source #

allocByteCountMem :: MonadPrim s m => Count Word8 -> m (MBytes p s) Source #

thawMem :: MonadPrim s m => FrozenMem (MBytes p) -> m (MBytes p s) Source #

freezeMem :: MonadPrim s m => MBytes p s -> m (FrozenMem (MBytes p)) Source #

resizeMem :: (MonadPrim s m, Prim e) => MBytes p s -> Count e -> m (MBytes p s) Source #

MemAlloc (MAddr e) Source # 
Instance details

Defined in Data.Prim.Memory.Addr

Associated Types

type FrozenMem (MAddr e) = (fa :: Type) Source #

Methods

getByteCountMem :: MonadPrim s m => MAddr e s -> m (Count Word8) Source #

allocByteCountMem :: MonadPrim s m => Count Word8 -> m (MAddr e s) Source #

thawMem :: MonadPrim s m => FrozenMem (MAddr e) -> m (MAddr e s) Source #

freezeMem :: MonadPrim s m => MAddr e s -> m (FrozenMem (MAddr e)) Source #

resizeMem :: (MonadPrim s m, Prim e0) => MAddr e s -> Count e0 -> m (MAddr e s) Source #

Typeable p => MemAlloc (MByteArray p e) Source # 
Instance details

Defined in Data.Prim.Memory.ByteArray

Associated Types

type FrozenMem (MByteArray p e) = (fa :: Type) Source #

Methods

getByteCountMem :: MonadPrim s m => MByteArray p e s -> m (Count Word8) Source #

allocByteCountMem :: MonadPrim s m => Count Word8 -> m (MByteArray p e s) Source #

thawMem :: MonadPrim s m => FrozenMem (MByteArray p e) -> m (MByteArray p e s) Source #

freezeMem :: MonadPrim s m => MByteArray p e s -> m (FrozenMem (MByteArray p e)) Source #

resizeMem :: (MonadPrim s m, Prim e0) => MByteArray p e s -> Count e0 -> m (MByteArray p e s) Source #

class MemWrite w Source #

Instances
MemWrite MByteString Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Methods

readOffMem :: (MonadPrim s m, Prim e) => MByteString s -> Off e -> m e Source #

readByteOffMem :: (MonadPrim s m, Prim e) => MByteString s -> Off Word8 -> m e Source #

writeOffMem :: (MonadPrim s m, Prim e) => MByteString s -> Off e -> e -> m () Source #

writeByteOffMem :: (MonadPrim s m, Prim e) => MByteString s -> Off Word8 -> e -> m () Source #

moveByteOffToMBytesMem :: (MonadPrim s m, Prim e) => MByteString s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m () Source #

moveByteOffToPtrMem :: (MonadPrim s m, Prim e) => MByteString s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m () Source #

copyByteOffMem :: (MonadPrim s m, MemRead r, Prim e) => r -> Off Word8 -> MByteString s -> Off Word8 -> Count e -> m () Source #

moveByteOffMem :: (MonadPrim s m, MemWrite w', Prim e) => w' s -> Off Word8 -> MByteString s -> Off Word8 -> Count e -> m () Source #

setMem :: (MonadPrim s m, Prim e) => MByteString s -> Off e -> Count e -> e -> m () Source #

MemWrite (MBytes p) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Methods

readOffMem :: (MonadPrim s m, Prim e) => MBytes p s -> Off e -> m e Source #

readByteOffMem :: (MonadPrim s m, Prim e) => MBytes p s -> Off Word8 -> m e Source #

writeOffMem :: (MonadPrim s m, Prim e) => MBytes p s -> Off e -> e -> m () Source #

writeByteOffMem :: (MonadPrim s m, Prim e) => MBytes p s -> Off Word8 -> e -> m () Source #

moveByteOffToMBytesMem :: (MonadPrim s m, Prim e) => MBytes p s -> Off Word8 -> MBytes p0 s -> Off Word8 -> Count e -> m () Source #

moveByteOffToPtrMem :: (MonadPrim s m, Prim e) => MBytes p s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m () Source #

copyByteOffMem :: (MonadPrim s m, MemRead r, Prim e) => r -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m () Source #

moveByteOffMem :: (MonadPrim s m, MemWrite w', Prim e) => w' s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m () Source #

setMem :: (MonadPrim s m, Prim e) => MBytes p s -> Off e -> Count e -> e -> m () Source #

MemWrite (MemState (ForeignPtr a)) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Methods

readOffMem :: (MonadPrim s m, Prim e) => MemState (ForeignPtr a) s -> Off e -> m e Source #

readByteOffMem :: (MonadPrim s m, Prim e) => MemState (ForeignPtr a) s -> Off Word8 -> m e Source #

writeOffMem :: (MonadPrim s m, Prim e) => MemState (ForeignPtr a) s -> Off e -> e -> m () Source #

writeByteOffMem :: (MonadPrim s m, Prim e) => MemState (ForeignPtr a) s -> Off Word8 -> e -> m () Source #

moveByteOffToMBytesMem :: (MonadPrim s m, Prim e) => MemState (ForeignPtr a) s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m () Source #

moveByteOffToPtrMem :: (MonadPrim s m, Prim e) => MemState (ForeignPtr a) s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m () Source #

copyByteOffMem :: (MonadPrim s m, MemRead r, Prim e) => r -> Off Word8 -> MemState (ForeignPtr a) s -> Off Word8 -> Count e -> m () Source #

moveByteOffMem :: (MonadPrim s m, MemWrite w', Prim e) => w' s -> Off Word8 -> MemState (ForeignPtr a) s -> Off Word8 -> Count e -> m () Source #

setMem :: (MonadPrim s m, Prim e) => MemState (ForeignPtr a) s -> Off e -> Count e -> e -> m () Source #

MemWrite (MAddr e) Source # 
Instance details

Defined in Data.Prim.Memory.Addr

Methods

readOffMem :: (MonadPrim s m, Prim e0) => MAddr e s -> Off e0 -> m e0 Source #

readByteOffMem :: (MonadPrim s m, Prim e0) => MAddr e s -> Off Word8 -> m e0 Source #

writeOffMem :: (MonadPrim s m, Prim e0) => MAddr e s -> Off e0 -> e0 -> m () Source #

writeByteOffMem :: (MonadPrim s m, Prim e0) => MAddr e s -> Off Word8 -> e0 -> m () Source #

moveByteOffToMBytesMem :: (MonadPrim s m, Prim e0) => MAddr e s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e0 -> m () Source #

moveByteOffToPtrMem :: (MonadPrim s m, Prim e0) => MAddr e s -> Off Word8 -> Ptr e0 -> Off Word8 -> Count e0 -> m () Source #

copyByteOffMem :: (MonadPrim s m, MemRead r, Prim e0) => r -> Off Word8 -> MAddr e s -> Off Word8 -> Count e0 -> m () Source #

moveByteOffMem :: (MonadPrim s m, MemWrite w', Prim e0) => w' s -> Off Word8 -> MAddr e s -> Off Word8 -> Count e0 -> m () Source #

setMem :: (MonadPrim s m, Prim e0) => MAddr e s -> Off e0 -> Count e0 -> e0 -> m () Source #

MemWrite (MByteArray p e) Source # 
Instance details

Defined in Data.Prim.Memory.ByteArray

Methods

readOffMem :: (MonadPrim s m, Prim e0) => MByteArray p e s -> Off e0 -> m e0 Source #

readByteOffMem :: (MonadPrim s m, Prim e0) => MByteArray p e s -> Off Word8 -> m e0 Source #

writeOffMem :: (MonadPrim s m, Prim e0) => MByteArray p e s -> Off e0 -> e0 -> m () Source #

writeByteOffMem :: (MonadPrim s m, Prim e0) => MByteArray p e s -> Off Word8 -> e0 -> m () Source #

moveByteOffToMBytesMem :: (MonadPrim s m, Prim e0) => MByteArray p e s -> Off Word8 -> MBytes p0 s -> Off Word8 -> Count e0 -> m () Source #

moveByteOffToPtrMem :: (MonadPrim s m, Prim e0) => MByteArray p e s -> Off Word8 -> Ptr e0 -> Off Word8 -> Count e0 -> m () Source #

copyByteOffMem :: (MonadPrim s m, MemRead r, Prim e0) => r -> Off Word8 -> MByteArray p e s -> Off Word8 -> Count e0 -> m () Source #

moveByteOffMem :: (MonadPrim s m, MemWrite w', Prim e0) => w' s -> Off Word8 -> MByteArray p e s -> Off Word8 -> Count e0 -> m () Source #

setMem :: (MonadPrim s m, Prim e0) => MByteArray p e s -> Off e0 -> Count e0 -> e0 -> m () Source #

getCountMem :: (MemAlloc r, MonadPrim s m, Prim e) => r s -> m (Count e) Source #

getCountRemMem :: (MemAlloc r, MonadPrim s m, Prim e) => r s -> m (Count e, Count Word8) Source #

readOffMem :: (MemWrite w, MonadPrim s m, Prim e) => w s -> Off e -> m e Source #

writeOffMem :: (MemWrite w, MonadPrim s m, Prim e) => w s -> Off e -> e -> m () Source #

modifyFetchOldMem :: (MemWrite w, MonadPrim s m, Prim b) => w s -> Off b -> (b -> b) -> m b Source #

modifyFetchOldMemM :: (MemWrite w, MonadPrim s m, Prim b) => w s -> Off b -> (b -> m b) -> m b Source #

modifyFetchNewMem :: (MemWrite w, MonadPrim s m, Prim b) => w s -> Off b -> (b -> b) -> m b Source #

modifyFetchNewMemM :: (MemWrite w, MonadPrim s m, Prim b) => w s -> Off b -> (b -> m b) -> m b Source #

setMem Source #

Arguments

:: (MemWrite w, MonadPrim s m, Prim e) 
=> w s

Writable memory. Must have enough bytes, at least: (off+count)*(sizeOf e)

-> Off e

An offset into writable memory at which element setting should start.

-> Count e

Numer of cells to write the elemnt into

-> e

Element to write into all memory cells specified by offset and count. Even if the count is 0 this element might be still fully evaluated.

-> m () 

Write the same value into each cell starting at an offset.

copyMem Source #

Arguments

:: (MonadPrim s m, MemRead r, MemWrite w, Prim e) 
=> r

Source memory region

-> Off e

Offset into the source in number of elements

-> w s

Destination memory region

-> Off e

Offset into destination in number of elements

-> Count e

Number of elements to copy over

-> m () 

moveMem Source #

Arguments

:: (MonadPrim s m, MemWrite w1, MemWrite w2, Prim e) 
=> w1 s

Source memory region

-> Off e

Offset into the source in number of elements

-> w2 s

Destination memory region

-> Off e

Offset into destination in number of elements

-> Count e

Number of elements to copy over

-> m () 

newtype MemState a s Source #

A wrapper that adds a phantom state token. It can be use with types that either doesn't have such state token or are designed to work in IO and therefore restricted to RW. Using this wrapper is very much unsafe, so make sure you know what you are doing.

Constructors

MemState 

Fields

Instances
MemWrite (MemState (ForeignPtr a)) Source # 
Instance details

Defined in Data.Prim.Memory.Internal

Methods

readOffMem :: (MonadPrim s m, Prim e) => MemState (ForeignPtr a) s -> Off e -> m e Source #

readByteOffMem :: (MonadPrim s m, Prim e) => MemState (ForeignPtr a) s -> Off Word8 -> m e Source #

writeOffMem :: (MonadPrim s m, Prim e) => MemState (ForeignPtr a) s -> Off e -> e -> m () Source #

writeByteOffMem :: (MonadPrim s m, Prim e) => MemState (ForeignPtr a) s -> Off Word8 -> e -> m () Source #

moveByteOffToMBytesMem :: (MonadPrim s m, Prim e) => MemState (ForeignPtr a) s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m () Source #

moveByteOffToPtrMem :: (MonadPrim s m, Prim e) => MemState (ForeignPtr a) s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m () Source #

copyByteOffMem :: (MonadPrim s m, MemRead r, Prim e) => r -> Off Word8 -> MemState (ForeignPtr a) s -> Off Word8 -> Count e -> m () Source #

moveByteOffMem :: (MonadPrim s m, MemWrite w', Prim e) => w' s -> Off Word8 -> MemState (ForeignPtr a) s -> Off Word8 -> Count e -> m () Source #

setMem :: (MonadPrim s m, Prim e) => MemState (ForeignPtr a) s -> Off e -> Count e -> e -> m () Source #

allocMem :: (MemAlloc a, MonadPrim s m, Prim e) => Count e -> m (a s) Source #

Allocate enough memory for number of elements. Memory is not initialized and may contain garbage. Use allocZeroMem if clean memory is needed.

Unsafe Count
Negative element count will result in unpredictable behavior

Since: 0.1.0

allocZeroMem :: (MemAlloc a, MonadPrim s m, Prim e) => Count e -> m (a s) Source #

Same as allocMem, but also use memset to initialize all the new memory to zeros.

Unsafe Count
Negative element count will result in unpredictable behavior

Since: 0.1.0

thawMem :: (MemAlloc a, MonadPrim s m) => FrozenMem a -> m (a s) Source #

thawCloneMem :: (MemRead r, MemAlloc a, MonadPrim s m) => r -> m (a s) Source #

thawCopyMem :: (MemRead r, MemAlloc a, MonadPrim s m, Prim e) => r -> Off e -> Count e -> m (a s) Source #

freezeMem :: (MemAlloc a, MonadPrim s m) => a s -> m (FrozenMem a) Source #

freezeCloneMem :: (MemAlloc a, MonadPrim s m) => a s -> m (FrozenMem a) Source #

freezeCopyMem :: (MemAlloc a, MonadPrim s m, Prim e) => a s -> Off e -> Count e -> m (FrozenMem a) Source #

createMemST :: (MemAlloc a, Prim e) => Count e -> (forall s. a s -> ST s b) -> (b, FrozenMem a) Source #

createMemST_ :: (MemAlloc a, Prim e) => Count e -> (forall s. a s -> ST s b) -> FrozenMem a Source #

createZeroMemST :: (MemAlloc a, Prim e) => Count e -> (forall s. a s -> ST s b) -> (b, FrozenMem a) Source #

createZeroMemST_ :: (MemAlloc a, Prim e) => Count e -> (forall s. a s -> ST s b) -> FrozenMem a Source #

emptyMem :: MemAlloc a => FrozenMem a Source #

Chunk of empty memory.

singletonMem :: forall e a. (MemAlloc a, Prim e) => e -> FrozenMem a Source #

A region of memory that hold a single element.

cycleMemN :: (MemAlloc a, MemRead r) => Int -> r -> FrozenMem a Source #

Make n copies of supplied region of memory into a contiguous chunk of memory.

Byte operations

More often than not it is desired to operate on the offset and count of the actual type of intereset we are dealing with in memory. But sometimes it is necessary to specify things in 8bit steps, this is where byte size offsets and counts will come in handy.

indexByteOffMem :: (MemRead r, Prim e) => r -> Off Word8 -> e Source #

compareByteOffMem :: (MemRead r, MemRead r', Prim e) => r' -> Off Word8 -> r -> Off Word8 -> Count e -> Ordering Source #

Mutable

readByteOffMem :: (MemWrite w, MonadPrim s m, Prim e) => w s -> Off Word8 -> m e Source #

writeByteOffMem :: (MemWrite w, MonadPrim s m, Prim e) => w s -> Off Word8 -> e -> m () Source #

copyByteOffMem :: (MemWrite w, MonadPrim s m, MemRead r, Prim e) => r -> Off Word8 -> w s -> Off Word8 -> Count e -> m () Source #

moveByteOffMem :: (MemWrite w, MonadPrim s m, MemWrite w', Prim e) => w' s -> Off Word8 -> w s -> Off Word8 -> Count e -> m () Source #

Conversion

convertMem :: (MemRead r, MemAlloc a) => r -> FrozenMem a Source #

O(n) - Convert a read-only memory region into a newly allocated other type of memory region

>>> import Data.ByteString
>>> bs = pack [0x10 .. 0x20]
>>> bs
"\DLE\DC1\DC2\DC3\DC4\NAK\SYN\ETB\CAN\EM\SUB\ESC\FS\GS\RS\US "
>>> convertMem bs :: Bytes 'Inc
[0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20]

Since: 0.1.0

List

toListMem :: (MemRead r, Prim e) => r -> [e] Source #

It is only guaranteed to convert the whole memory to a list whenever the size of allocated memory is exactly divisible by the size of the element, otherwise there will be some slack left unaccounted for.

toListSlackMem :: forall e r. (MemRead r, Prim e) => r -> ([e], [Word8]) Source #

Same as toListMem, except if there is some slack at the end of the memory that didn't fit in a list it will be returned as a list of bytes

Examples

Expand
>>> import Data.Word
>>> :set -XDataKinds
>>> a = fromListMem [0 .. 10 :: Word8] :: Bytes 'Pin
>>> a
[0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a]
>>> toListSlackMem a :: ([Word8], [Word8])
([0,1,2,3,4,5,6,7,8,9,10],[])
>>> toListSlackMem a :: ([Word16], [Word8])
([256,770,1284,1798,2312],[10])
>>> toListSlackMem a :: ([Word32], [Word8])
([50462976,117835012],[8,9,10])
>>> toListSlackMem a :: ([Word64], [Word8])
([506097522914230528],[8,9,10])

Since: 0.1.0

toByteListMem :: MemAlloc a => FrozenMem a -> [Word8] Source #

Convert a memory region to a list of bytes. Equivalent to unpack for ByteString

>>> toByteListMem (fromByteListMem [0..10] :: Bytes 'Pin)
[0,1,2,3,4,5,6,7,8,9,10]

Since: 0.1.0

fromByteListMem :: MemAlloc a => [Word8] -> FrozenMem a Source #

Load a list of bytes into a newly allocated memory region. Equivalent to pack for ByteString

Examples

Expand
>>> fromByteListMem [0..10] :: Bytes 'Pin
[0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a]

Since: 0.1.0

fromListMem :: (MemAlloc a, Prim e) => [e] -> FrozenMem a Source #

fromListMemN :: (MemAlloc a, Prim e) => Count e -> [e] -> (Ordering, FrozenMem a) Source #

loadListMem :: (MonadPrim s m, MemAlloc r, Prim e) => [e] -> r s -> m Ordering Source #

Returns EQ if the full list did fit into the supplied memory chunk exactly. Otherwise it will return either LT if the list was smaller than allocated memory or GT if the list was bigger than the available memory and did not fit into MBytes.

loadListMem_ :: (MonadPrim s m, MemAlloc r, Prim e) => [e] -> r s -> m () Source #

loadListMemN :: (MemWrite r, MonadPrim s m, Prim e) => Count e -> Count Word8 -> [e] -> r s -> m Ordering Source #

loadListMemN_ :: (MemWrite r, MonadPrim s m, Prim e) => Count e -> [e] -> r s -> m () Source #

Helpers

foldrCountMem :: (MemRead r, Prim e) => Count e -> (e -> b -> b) -> b -> r -> b Source #

Right fold that is useful for converting to list while tapping into list fusion.