<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">
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 <per-ake.minborg@oracle.com> 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>
</body>
</html>