-
Notifications
You must be signed in to change notification settings - Fork 17
Description
Today, for reading and writing basic values like Int32
s to locations in memory we have:
- The
Storable
class frombase
- The
Prim
class fromprimitive
- Primops like
readInt32OffAddr#
andwriteInt32OffAddr#
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?