Skip to content

Commit

Permalink
Don't perform unaligned writes
Browse files Browse the repository at this point in the history
Some architectures, such as SPARC, trap on unaligned memory accesses,
and poke makes no guarantees about its behaviour if the address is not
sufficiently aligned. Therefore we should use a temporary buffer if
asked to write to an unaligned address.
  • Loading branch information
jrtc27 committed Jul 24, 2017
1 parent a1c6a8b commit 03c4186
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Data/ByteString/Builder/Prim/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ liftFixedToBounded = toB

{-# INLINE CONLIKE storableToF #-}
storableToF :: forall a. Storable a => FixedPrim a
storableToF = FP (sizeOf (undefined :: a)) (\x op -> poke (castPtr op) x)
storableToF = FP (sizeOf (undefined :: a)) $ \x op ->
if (ptrToWordPtr op) `mod` (fromIntegral (alignment (undefined :: a))) == 0 then poke (castPtr op) x
else with x $ \tp -> copyBytes op (castPtr tp) (sizeOf (undefined :: a))

{-
{-# INLINE CONLIKE liftIOF #-}
Expand Down

0 comments on commit 03c4186

Please sign in to comment.