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

Modulefoundation-0.0.30Haskell2010

Foundation.Array.Internal

Give access to Array non public functions which can be used to make certains optimisations.

Most of what is available here has no guarantees of stability. Anything can be removed and changed.

  • 1 type
  • 8 values
  • Packagefoundation-0.0.30
  • Exports9
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceInternal.hs
datadata UArray ty
#

An array of type built on top of GHC primitive.

The elements need to have fixed sized and the representation is a packed contiguous array in memory that can easily be passed to foreign interface

Constructors

Instances31IsList, Eq, Data, Ord, Show, Semigroup, …
valuefromForeignPtr :: PrimType ty => (ForeignPtr ty, Int, Int) -> UArray ty
#

Create a foreign UArray from foreign memory and given offset/size

No check are performed to make sure this is valid, so this is unsafe.

This is particularly useful when dealing with foreign memory and ByteString

valuewithPtr
  1. :: (PrimMonad prim, PrimType ty)
  2. => UArray ty
  3. -> Ptr ty -> prim a
  4. -> prim a
#

Get a Ptr pointing to the data in the UArray.

Since a UArray is immutable, this Ptr shouldn't be to use to modify the contents

If the UArray is pinned, then its address is returned as is, however if it's unpinned, a pinned copy of the UArray is made before getting the address.

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

    the source 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

valuerecast :: (PrimType a, PrimType b) => UArray a -> UArray b
#

Recast an array of type a to an array of b

a and b need to have the same size otherwise this raise an async exception

Mutable facilities

3 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

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.