Skip to content

Support for unaligned reads/writes #154

@clyring

Description

@clyring

Today, for reading and writing basic values like Int32s to locations in memory we have:

  • The Storable class from base
  • The Prim class from primitive
  • Primops like readInt32OffAddr# and writeInt32OffAddr#

All of these have in common that they may only work for sufficiently aligned addresses. (...though for the primops this is not currently documented anywhere obvious.) This leads library authors to writing embarrassing workarounds like this one when there is a need to read from or write to a potentially un-aligned memory location. The code this workaround produces on non-whitelisted architectures is correct but very poor.

It's clear to me that GHC should provide a family of primops like readInt32OffUnalignedAddr# and writeInt32OffUnalignedAddr#. (This should be easy to implement.)

What's less clear to me is whether and how more polite wrappers should be provided. For example, adding a pokeUnaligned :: Ptr a -> a -> IO () method to Storable a might be convenient, but this operation is not very commonly needed and a default implementation will probably be both necessary to avoid pointless breakage and a performance foot-gun. (The instances in base would use efficient non-default implementations via new primops, of course.) What do others think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    approvedApproved by CLC votebase-4.20Implemented in base-4.20 (GHC 9.10)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions