HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleprimitive-0.9.1.0Haskell2010

Data.Primitive.ByteArray

Primitive operations on byte arrays. Most functions in this module include an element type in their type signature and interpret the unit for offsets and lengths as that element. A few functions (e.g. copyByteArray, freezeByteArray) do not include an element type. Such functions interpret offsets and lengths as units of 8-bit words.

  • 4 types
  • 46 values
  • Packageprimitive-0.9.1.0
  • Exports50
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceByteArray.hs

Types

4 declarations
datadata ByteArray
#

Lifted wrapper for ByteArray#.

Since ByteArray# is an unlifted type and not a member of kind Type, things like [ByteArray#] or IO ByteArray# are ill-typed. To work around this inconvenience this module provides a standard lifted wrapper, inhabiting Type. Clients are expected to use ByteArray in higher-level APIs, but wrap and unwrap ByteArray internally as they please and use functions from GHC.Exts.

Constructors

Instances10IsList, Eq, Data, Ord, Show, Semigroup, …
  • IsList ByteArrayDefined in base-4.20.2.0 · Data.Array.Byte
  • Eq ByteArrayDefined in base-4.20.2.0 · Data.Array.Byte
  • Data ByteArrayDefined in base-4.20.2.0 · Data.Array.Byte
  • Ord ByteArrayDefined in base-4.20.2.0 · Data.Array.Byte

    Non-lexicographic ordering. This compares the lengths of the byte arrays first and uses a lexicographic ordering if the lengths are equal. Subject to change between major versions.

  • Show ByteArrayDefined in base-4.20.2.0 · Data.Array.Byte
  • Semigroup ByteArrayDefined in base-4.20.2.0 · Data.Array.Byte
  • Monoid ByteArrayDefined in base-4.20.2.0 · Data.Array.Byte
  • NFData ByteArrayDefined in deepseq-1.5.0.0 · Control.DeepSeq
  • Lift ByteArrayDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • type Item ByteArray = Word8Defined in base-4.20.2.0 · Data.Array.Byte
datadata MutableByteArray s
#

Lifted wrapper for MutableByteArray#.

Since MutableByteArray# is an unlifted type and not a member of kind Type, things like [MutableByteArray#] or IO MutableByteArray# are ill-typed. To work around this inconvenience this module provides a standard lifted wrapper, inhabiting Type. Clients are expected to use MutableByteArray in higher-level APIs, but wrap and unwrap MutableByteArray internally as they please and use functions from GHC.Exts.

Instances3Eq, Data, NFData
datadata ByteArray#
#

A boxed, unlifted datatype representing a region of raw memory in the garbage-collected heap, which is not scanned for pointers during garbage collection.

It is created by freezing a MutableByteArray# with unsafeFreezeByteArray#. Freezing is essentially a no-op, as MutableByteArray# and ByteArray# share the same heap structure under the hood.

The immutable and mutable variants are commonly used for scenarios requiring high-performance data structures, like Text, Primitive Vector, Unboxed Array, and ShortByteString.

Another application of fundamental importance is Integer, which is backed by ByteArray#.

The representation on the heap of a Byte Array is:

+------------+-----------------+-----------------------+
|            |                 |                       |
|   HEADER   | SIZE (in bytes) |       PAYLOAD         |
|            |                 |                       |
+------------+-----------------+-----------------------+

To obtain a pointer to actual payload (e.g., for FFI purposes) use byteArrayContents# or mutableByteArrayContents#.

Alternatively, enabling the UnliftedFFITypes extension allows to mention ByteArray# and MutableByteArray# in FFI type signatures directly.

datadata MutableByteArray# a
#

A mutable ByteAray#. It can be created in three ways:

  • newByteArray#: Create an unpinned array.

  • newPinnedByteArray#: This will create a pinned array,

  • newAlignedPinnedByteArray#: This will create a pinned array, with a custom alignment.

Unpinned arrays can be moved around during garbage collection, so you must not store or pass pointers to these values if there is a chance for the garbage collector to kick in. That said, even unpinned arrays can be passed to unsafe FFI calls, because no garbage collection happens during these unsafe calls (see Guaranteed Call Safety in the GHC Manual). For safe FFI calls, byte arrays must be not only pinned, but also kept alive by means of the keepAlive# function for the duration of a call (that's because garbage collection cannot move a pinned array, but is free to scrap it altogether).

Allocation

5 declarations

Create a new mutable byte array of the specified size in bytes. The underlying memory is left uninitialized.

Note: this function does not check if the input is non-negative.

Create a pinned byte array of the specified size in bytes. The garbage collector is guaranteed not to move it. The underlying memory is left uninitialized.

Note: this function does not check if the input is non-negative.

valuenewAlignedPinnedByteArray
  1. :: PrimMonad m
  2. => Int

    size

  3. -> Int

    alignment

  4. -> m (MutableByteArray (PrimState m))
#

Create a pinned byte array of the specified size in bytes and with the given alignment. The garbage collector is guaranteed not to move it. The underlying memory is left uninitialized.

Note: this function does not check if the input is non-negative.

Resize a mutable byte array. The new size is given in bytes.

This will either resize the array in-place or, if not possible, allocate the contents into a new, unpinned array and copy the original array's contents.

To avoid undefined behaviour, the original MutableByteArray shall not be accessed anymore after a resizeMutableByteArray has been performed. Moreover, no reference to the old one should be kept in order to allow garbage collection of the original MutableByteArray in case a new MutableByteArray had to be allocated.

Element access

3 declarations
valueindexByteArray :: Prim a => ByteArray -> Int -> a
#

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

Note: this function does not do bounds checking.

Char Element Access

3 declarations

GHC provides two sets of element accessors for Char. One set faithfully represents Char as 32-bit words using UTF-32. The other set represents Char as 8-bit words using Latin-1 (ISO-8859-1), and the write operation has undefined behavior for codepoints outside of the ASCII and Latin-1 blocks. The Prim instance for Char uses the UTF-32 set of operators.

Write a character to the byte array, encoding it with Latin-1 as a single byte. Behavior is undefined for codepoints outside of the ASCII and Latin-1 blocks. The offset is given in bytes.

Note: this function does not do bounds checking.

valueindexCharArray :: ByteArray -> Int -> Char
#

Read an 8-bit element from the byte array, interpreting it as a Latin-1-encoded character. The offset is given in bytes.

Note: this function does not do bounds checking.

Constructing

3 declarations

Folding

1 declaration

Comparing

1 declaration
valuecompareByteArrays
  1. :: ByteArray

    array A

  2. -> Int

    offset A, given in bytes

  3. -> ByteArray

    array B

  4. -> Int

    offset B, given in bytes

  5. -> Int

    length of the slice, given in bytes

  6. -> Ordering
#

Lexicographic comparison of equal-length slices into two byte arrays. This wraps the compareByteArrays# primop, which wraps memcmp.

Freezing and thawing

6 declarations
valuefreezeByteArray
  1. :: PrimMonad m
  2. => MutableByteArray (PrimState m)

    source

  3. -> Int

    offset in bytes

  4. -> Int

    length in bytes

  5. -> m ByteArray
#

Create an immutable copy of a slice of a byte array. The offset and length are given in bytes.

This operation makes a copy of the specified section, so it is safe to continue using the mutable array afterward.

Note: The provided array should contain the full subrange specified by the two Ints, but this is not checked.

valuethawByteArray
  1. :: PrimMonad m
  2. => ByteArray

    source

  3. -> Int

    offset in bytes

  4. -> Int

    length in bytes

  5. -> m (MutableByteArray (PrimState m))
#

Create a mutable byte array from a slice of an immutable byte array. The offset and length are given in bytes.

This operation makes a copy of the specified slice, so it is safe to use the immutable array afterward.

Note: The provided array should contain the full subrange specified by the two Ints, but this is not checked.

Block operations

12 declarations
valuecopyByteArray
  1. :: PrimMonad m
  2. => MutableByteArray (PrimState m)

    destination array

  3. -> Int

    offset into destination array

  4. -> ByteArray

    source array

  5. -> Int

    offset into source array

  6. -> Int

    number of bytes to copy

  7. -> m ()
#

Copy a slice of an immutable byte array to a mutable byte array.

Note: this function does not do bounds or overlap checking.

valuecopyByteArrayToPtr
  1. :: (PrimMonad m, Prim a)
  2. => Ptr a

    destination

  3. -> ByteArray

    source array

  4. -> Int

    offset into source array, interpreted as elements of type a

  5. -> Int

    number of elements to copy

  6. -> m ()
#

Copy a slice of a byte array to an unmanaged pointer address. These must not overlap. The offset and length are given in elements, not in bytes.

Note: this function does not do bounds or overlap checking.

valuecopyMutableByteArrayToPtr
  1. :: (PrimMonad m, Prim a)
  2. => Ptr a

    destination

  3. -> MutableByteArray (PrimState m)

    source array

  4. -> Int

    offset into source array, interpreted as elements of type a

  5. -> Int

    number of elements to copy

  6. -> m ()
#

Copy a slice of a mutable byte array to an unmanaged pointer address. These must not overlap. The offset and length are given in elements, not in bytes.

Note: this function does not do bounds or overlap checking.

valuecopyPtrToMutableByteArray
  1. :: (PrimMonad m, Prim a)
  2. => MutableByteArray (PrimState m)

    destination array

  3. -> Int

    destination offset given in elements of type a

  4. -> Ptr a

    source pointer

  5. -> Int

    number of elements

  6. -> m ()
#

Copy from an unmanaged pointer address to a byte array. These must not overlap. The offset and length are given in elements, not in bytes.

Note: this function does not do bounds or overlap checking.

valuesetByteArray
  1. :: (Prim a, PrimMonad m)
  2. => MutableByteArray (PrimState m)

    array to fill

  3. -> Int

    offset into array

  4. -> Int

    number of values to fill

  5. -> a

    value to fill with

  6. -> m ()
#

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

Note: this function does not do bounds checking.

valuecloneByteArray
  1. :: ByteArray

    source array

  2. -> Int

    offset into destination array

  3. -> Int

    number of bytes to copy

  4. -> ByteArray
#

Return a newly allocated array with the specified subrange of the provided array. The provided array should contain the full subrange specified by the two Ints, but this is not checked.

Information

12 declarations

Check whether or not the byte array is pinned. Pinned byte arrays cannot be moved by the garbage collector. It is safe to use byteArrayContents on such byte arrays.

Caution: This function is only available when compiling with GHC 8.2 or newer.

Create a foreign pointer that points to the array's data. This operation is only safe on pinned byte arrays. The array's data is not garbage collected while references to the foreign pointer exist. Writing to the array through the foreign pointer results in undefined behavior.