<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p>I second what Paul said. Alignment of heap memory is not
something FFM API supports, as much it is something FFM knows how
to "cope" with.</p>
<p>That is, the only thing we know and can rely upon, is that an
heap array of type T can provide no more alignment than T itself
(this is what we mean when we say, in the Javadoc, that address of
heap segments is "virtualized").<br>
</p>
<p>This means that an int[] can provide up to 4-byte alignment, a
long[] can provide up to 8-byte alignment and so on.</p>
<p>If you need 16-byte alignment there's currently no bullet proof
way to get there. Which is why all access operations with
alignment > 8 bytes will fail on a heap memory segment,
regardless of the source.</p>
<p>This alignment-agnostic design has allowed FFM to coexist with
radical enhancements that squeezed object layouts down to 8 bytes
(such as Project Lillput [1]) -- we have seen how the BB API
required some adjustments in this area to cope with that [2].</p>
<p>So, as Paul says, I don't expect any significant change in this
area -- as overcommitting to alignment of heap memory will likely
result in less freedom for the JVM runtime and GC design.</p>
<p>To help navigate this area, there's a new method [3] since Java
23 which allows developers to know the max alignment supported by
a given memory segment (whether heap or native).<br>
</p>
<p>Cheers<br>
Maurizio<br>
</p>
<p>[1] - <a class="moz-txt-link-freetext" href="https://openjdk.org/jeps/450">https://openjdk.org/jeps/450</a><br>
[2] - <a class="moz-txt-link-freetext" href="https://github.com/openjdk/jdk/pull/16681">https://github.com/openjdk/jdk/pull/16681</a><br>
[3] -
<a class="moz-txt-link-freetext" href="https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/foreign/MemorySegment.html#maxByteAlignment()">https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/foreign/MemorySegment.html#maxByteAlignment()</a><br>
</p>
<div class="moz-cite-prefix">On 19/05/2025 16:51, Paul Sandoz wrote:<br>
</div>
<blockquote type="cite" cite="mid:E877EC27-F067-4511-8CC3-36CF20F08850@oracle.com">
Note that will not guarantee stable alignment. The GC can move the
allocated array.
<div><br>
</div>
<div>I don’t know if we will ever get to supporting hyper-aligned
heap objects, it's likely complex for GCs to support and to
surface that up to developers. There are more important things
to work on.</div>
<div> </div>
<div>Paul.<br id="lineBreakAtBeginningOfMessage">
<div><br>
<blockquote type="cite">
<div>On May 19, 2025, at 6:18 AM, Per-Ake Minborg
<a class="moz-txt-link-rfc2396E" href="mailto:per-ake.minborg@oracle.com"><per-ake.minborg@oracle.com></a> wrote:</div>
<br class="Apple-interchange-newline">
<div>
<div class="elementToProof" style="font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;">
Hi again Lee!<br>
<br>
1.<br>
<br>
The VM/GC has its own rules for how arrays are
allocated, and so, there is no way (at least not now) to
control primitive array alignment except for the way you
mentioned — a primitive array is always aligned to its
component's natural alignment. A very simple way to
allocate and align heap arrays could be to implement a
method similar to this one:<br>
<br>
<br>
<span style="font-family: monospace;">static
MemorySegment heapSegment(MemoryLayout layout) {<br>
byte[] arr = new byte[(int) layout.byteSize() *
(int)layout.byteAlignment() - 1];<br>
SegmentAllocator allocator =
SegmentAllocator.slicingAllocator(MemorySegment.ofArray(arr));<br>
return allocator.allocate(layout);<br>
}</span><br>
<br>
</div>
<div class="elementToProof" style="font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;">
If memory efficiency is a concern, a more complex
solution can be written that leverages the implicit
alignment of arrays of<span class="Apple-converted-space"> </span><code>int</code> and<span class="Apple-converted-space"> </span><code>long.</code></div>
<div class="elementToProof" style="font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;">
<br>
</div>
</div>
</blockquote>
<div><br>
</div>
<div><br>
</div>
</div>
</div>
</blockquote>
</body>
</html>