<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div><br></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Maurizio Cimadamore" <maurizio.cimadamore@oracle.com><br><b>To: </b>"Remi Forax" <forax@univ-mlv.fr><br><b>Cc: </b>"panama-dev" <panama-dev@openjdk.org><br><b>Sent: </b>Wednesday, June 5, 2024 12:18:47 PM<br><b>Subject: </b>Re: Arena/Segment allocator and zero initialized MemorySegment<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><p><br>
    </p>
    <div class="moz-cite-prefix">On 05/06/2024 11:11, <a class="moz-txt-link-abbreviated" href="mailto:forax@univ-mlv.fr" target="_blank">forax@univ-mlv.fr</a>
      wrote:<br>
    </div>
    <blockquote cite="mid:334543192.41852752.1717582306322.JavaMail.zimbra@univ-eiffel.fr">
      <pre>Here is an example
<a class="moz-txt-link-freetext" href="https://urldefense.com/v3/__https://github.com/forax/memory-mapper/blob/master/src/main/java/com/github/forax/memorymapper/MemoryCollections.java*L772__;Iw!!ACWV5N9M2RV99hQ!PUJIR9R4tNJ_QKk01hd0VFW5mMO0KsBeJk0WxuQsq4MzmNZ0c5WvuGhrAHSJQJGDJog9qIZghlkqNDjgBnxZ8JXAYA$" target="_blank">https://urldefense.com/v3/__https://github.com/forax/memory-mapper/blob/master/src/main/java/com/github/forax/memorymapper/MemoryCollections.java*L772__;Iw!!ACWV5N9M2RV99hQ!PUJIR9R4tNJ_QKk01hd0VFW5mMO0KsBeJk0WxuQsq4MzmNZ0c5WvuGhrAHSJQJGDJog9qIZghlkqNDjgBnxZ8JXAYA$</a>
 

I want to have a List of records (or primitive type) implemented as an 
array of flatten values.
I need a way to allocate a zeroed MemorySegment, and I need a way to 
grow the storage using the Array.copyOf() semantics.

For the history, I've an existing non-public library that uses Unsafe + 
ASM for the code generation and i'm trying to replace it with a new 
publicly available library that uses the foreign memory API + the method
 handle API + Hidden classes.</pre>
    </blockquote>
    <p>As you are allocating only one segment, the use of
      SegmentAllocator seems fine here. But... if you later on need to
      grow storage, it's not a one-shot allocation anymore, and I
      believe you need an Arena (as you might need to start managing
      different segments with same lifetime).<br>
    </p>
    <p>You can also go a different route, and use calloc/realloc
      directly (via Linker), and avoid the allocator parameter (not sure
      how your old API dealt with deallocation safely).</p></blockquote><div><br></div><div>The old library uses Unsafe.allocateMemory()/reallocateMemory()/setMemory()/copyMemory()/freeMemory() and uses the finalizer mechanism for the safe but late deallocation.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>For now, the new library do not try to reallocate, so when it needs to grow the storage, it asks for a new MemorySegment and copy the data.</div><div>It seems I should use an Arena instead of a SegmentAllocator because two subsequent calls to allocate() should not return the same address, but i have two reasons to not using it. Arena does not garantee that the storage is zeroed and it keeps the storage until the Arena is closed so if the list grows several times, it will keep all previous segments in memory.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>As far as i understand what an Arena is, an Arena is virtual, it's not contiguous memory*, so supporting deallocation is possible (i'm not saying it should) and having a way to ask for a zero initialized MemorySegement is also something possible (again, not saying it should).<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;">
    <p>Maurizio</p></blockquote><div><br></div><div>RĂ©mi</div><div>* it should be stated more clearly in the javadoc of Arena, IMO, the only other runtime system i know that uses the word arena with the same semantics is Zig, all others are using.arena with the meaning of a region based allocator (see <a href="https://en.wikipedia.org/wiki/Special:Search?go=Go&search=arena%20memory)">https://en.wikipedia.org/wiki/Special:Search?go=Go&search=arena%20memory)</a>.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div>
    </div></div></body></html>