HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulebasement-0.0.16Haskell2010

Basement.UArray.Mutable

A simple array abstraction that allow to use typed array of bytes where the array is pinned in memory to allow easy use with Foreign interfaces, ByteString and always aligned to 64 bytes.

  • 1 type
  • 20 values
  • Packagebasement-0.0.16
  • Exports21
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceMutable.hs
datadata MUArray ty st
#

A Mutable array of types built on top of GHC primitive.

Element in this array can be modified in place.

Constructors

Property queries

5 declarations

Allocation & Copy

9 declarations
valuenew
  1. :: (PrimMonad prim, PrimType ty)
  2. => CountOf ty
  3. -> prim (MUArray ty (PrimState prim))
#

Create a new mutable array of size @n.

When memory for a new array is allocated, we decide if that memory region should be pinned (will not be copied around by GC) or unpinned (can be moved around by GC) depending on its size.

You can change the threshold value used by setting the environment variable HS_FOUNDATION_UARRAY_UNPINNED_MAX.

valuenewPinned
  1. :: (PrimMonad prim, PrimType ty)
  2. => CountOf ty
  3. -> prim (MUArray ty (PrimState prim))
#

Create a new pinned mutable array of size @n.

all the cells are uninitialized and could contains invalid values.

All mutable arrays are allocated on a 64 bits aligned addresses

valuecopyToPtr
  1. :: (PrimType ty, PrimMonad prim)
  2. => MUArray ty (PrimState prim)

    the source mutable array to copy

  3. -> Ptr ty

    The destination address where the copy is going to start

  4. -> prim ()
#

Copy all the block content to the memory starting at the destination address

Reading and Writing cells

6 declarations
valueunsafeWrite
  1. :: (PrimMonad prim, PrimType ty)
  2. => MUArray ty (PrimState prim)
  3. -> Offset ty
  4. -> ty
  5. -> prim ()
#

write to a cell in a mutable array without bounds checking.

Writing with invalid bounds will corrupt memory and your program will become unreliable. use write if unsure.

valueunsafeRead
  1. :: (PrimMonad prim, PrimType ty)
  2. => MUArray ty (PrimState prim)
  3. -> Offset ty
  4. -> prim ty
#

read from a cell in a mutable array without bounds checking.

Reading from invalid memory can return unpredictable and invalid values. use read if unsure.

valuewithMutablePtr
  1. :: (PrimMonad prim, PrimType ty)
  2. => MUArray ty (PrimState prim)
  3. -> Ptr ty -> prim a
  4. -> prim a
#

Create a pointer on the beginning of the mutable array and call a function f.

The mutable buffer can be mutated by the f function and the change will be reflected in the mutable array

If the mutable array is unpinned, a trampoline buffer is created and the data is only copied when f return.