<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p><br>
</p>
<div class="moz-cite-prefix">On 17/10/2023 00:54, tison wrote:<br>
</div>
<blockquote type="cite" cite="mid:CALL9TY+qPXFTHE1i5dXNSHE6LtEH6yqP8O74GPkpGud+DSLbqA@mail.gmail.com">
<div dir="ltr">
<div class="gmail_default" style="font-family:arial,sans-serif">A
confined/shared arena is defined to associate the same
lifecycle for all memory segments it allocates - that they
will be deallocated on the arena closed.</div>
<div class="gmail_default" style="font-family:arial,sans-serif"><br>
</div>
<div class="gmail_default" style="font-family:arial,sans-serif">But
it can be normal that I want to preallocate a large arena and
allocate/deallocate memory segments on this arena. In this
case, what design pattern can help since there is no explicit
deallocate method (I found we have one in the early release
but it's absent now, why?)</div>
<div class="gmail_default" style="font-family:arial,sans-serif"><br>
</div>
<div>
<div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">
<div><font face="arial, sans-serif">Best,</font></div>
<div><font face="arial, sans-serif">tison.</font></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<p><font face="arial, sans-serif">I think the pattern you describe
is that of a memory pool. That is, some memory segment is
allocated at the start of the application, and its contents are
recycled by multiple clients.</font></p>
<p><font face="arial, sans-serif">This can be done using a custom
arena, such as the one decribed here:</font></p>
<p><font face="arial, sans-serif"><a class="moz-txt-link-freetext" href="https://cr.openjdk.org/~mcimadamore/panama/why_lifetimes.html">https://cr.openjdk.org/~mcimadamore/panama/why_lifetimes.html</a></font></p>
<p><font face="arial, sans-serif">Note the example:<br>
<br>
```<br>
</font>SlicingPool pool = new SlicingPool();<br>
<br>
...<br>
<br>
try (Arena slicingArena1 = pool.acquire()) {<br>
<br>
MemorySegment segment1 = slicingArena1.allocate(100);<br>
<br>
} // 'segment1' becomes invalid here<br>
<br>
...<br>
<br>
try (Arena slicingArena2 = pool.acquire()) {<br>
<br>
MemorySegment segment2 = slicingArena2.allocate(100);<br>
<br>
} // 'segment2' becomes invalid here<br>
```</p>
<p>That is, clients can come and go, and they will just get a _new_
arena that is based off a slice of the original segment. This
simple implementation of course only works in a single thread.</p>
<p>There were other examples of memory pools provided in the past
which also worked across multiple threads:</p>
<p><a class="moz-txt-link-freetext" href="https://github.com/openjdk/panama-foreign/pulls">https://github.com/openjdk/panama-foreign/pulls</a></p>
<p>(unfortunately this is a rather old PR, and its code has diverged
from the current API, but it could be used as inspiration).</p>
<p>Maurizio<br>
</p>
<p><br>
</p>
</body>
</html>