Will unaligned memory access work on all platforms?
Manuel Bleichenbacher
manuel.bleichenbacher at gmail.com
Thu Dec 28 15:52:07 UTC 2023
Hi,
Instances of MemoryLayout support specifying alignment constraints.
However, if the alignment is set to 1 byte for all layouts, will unaligned
access work on all platforms?
In a short test on a Mac with Apple Silicon, unaligned access works with
both native and heap segments, across 8-byte boundaries using
MemorySegment.get(), MemorySegment.set(), VarHandle.get() and
VarHandle.set(). And the result will likely be the same on x86 machine,
independent of the operating system. But is it guaranteed to work on any
operating system and any CPU architecture, even on those ones where machine
instructions may not read from or write to unaligned addresses?
If it does not work on all platform, what would happen instead? Will it
throw an exception? Could it crash the JVM? Is the behavior the same for
native and heap segments?
While researching this topic, I kept wondering where to look for
documentation about it. The only description I've found is in documentation
of the MemorySegment class. It has quite a long chapter about alignment,
yet it doesn't really tell why you would want to use alignment constraints
in the first place and what happens if an unaligned access is made anyway,
with or without alignment constraint on the layout.
Regards
Manuel Bleichenbacher
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20231228/aa5bc054/attachment.htm>
More information about the panama-dev
mailing list