<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi and thank you for the encouragement!</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I am a bit uncertain as to why you would like to generally work with heap segments if you do not simultaneously have access to the corresponding backing arrays?<br>
<br>
1 and 2) What you could do is to implement your own HeapArena that allocates a larger segment up front and perhaps also support recycling of</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
memory. The latter feature will avoid zeroing out recycled memory portions but will come with additional security caveats as old content now can leak. <br>
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
3) At the very end of Jorn's article, you can read more on VarHandles and invoke exact behavior. In short, this will not affect your performance in most cases as the C2 compiler will be able to see through the code. You can also experiment with setting the
 system property parameter "java.lang.invoke.VarHandle.VAR_HANDLE_SEGMENT_FORCE_EXACT" to "true" if you want to see what actually happens under your specific workloads.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Best, Per Minborg</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> panama-dev <panama-dev-retn@openjdk.org> on behalf of ÁõÏ£³¿ <benrush0705@gmail.com><br>
<b>Sent:</b> Wednesday, March 27, 2024 5:47 PM<br>
<b>To:</b> panama-dev@openjdk.org <panama-dev@openjdk.org><br>
<b>Subject:</b> Can we have some heap segment allocation API in the future?</font>
<div> </div>
</div>
<div>
<div dir="ltr">Hi there, I am really happy that Panama-FFI finally released, congratulation! 
<div><br>
</div>
<div>I have been using Panama's features since JDK19, and when I am migrating to JDK22, I found that Heap segment could be passed to native functions if the call is really short, which is really helpful in lots of ways.</div>
<div><br>
</div>
<div>However, allocating heap segments are not so convenient as allocating  native segments, it seems that we could only use MemorySegment.ofArray() to transform an array into a memorysegment.</div>
<div><br>
</div>
<div>Hence I got several question:</div>
<div><br>
</div>
<div>1. Could there be a HeapArena, which targets at allocating heap segments with given byteSize and byteAlignment, just like native segments? That would be pretty helpful in a lot of scenarios.</div>
<div><br>
</div>
<div>2. Could there be an API, for developers, to allocate a heap segment, with no zero-bytes filled, so that heap allocation would be much faster?</div>
<div><br>
</div>
<div>3. I noticed that, when calling MemorySegment.get() or MemorySegment.set() methods, it will use the underlying Varhandle to do the job, I read about Varhandle in <a href="https://jornvernee.github.io/methodhandles/2024/01/19/methodhandle-primer.html">https://jornvernee.github.io/methodhandles/2024/01/19/methodhandle-primer.html</a>,
 and when I run the following code:</div>
<div><br>
</div>
<div>
<div style="background-color:rgb(30,31,34); color:rgb(188,190,196)">
<pre style="font-family:"Cascadia Mono",monospace; font-size:10.5pt">System.<span style="color:rgb(199,125,187); font-style:italic">out</span>.println(ValueLayout.<span style="color:rgb(199,125,187); font-style:italic">JAVA_INT</span>.varHandle().hasInvokeExactBehavior());</pre>
</div>
</div>
<div><br>
</div>
<div>the result is false, so there is no InvokeExactBehaviour, which is not recommended. I thought this could be a mistake, or it's intended to handle the auto-boxing conversion, please take a look at this.</div>
<div><br>
</div>
<div>Best regards!</div>
</div>
</div>
</body>
</html>