<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1250">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
{page:WordSection1;}
--></style>
</head>
<body lang="EN-US" link="blue" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">Hi Gevin,</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Those are interesting questions.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">First of all MemorySegment is a wrapper for pointer and length of data after this pointer. It does not represent physical memory – it’s more like C pointer.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In order to free memory you have to check it with allocator implementation.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Allocator can register itself with session to perform clean-up when session is closed, as memory session performs role of synchronizier between Java and C. When memory session get’s closed Java will throw exception, to prevent accessing
potentially invalid segment.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I hope this clarified a bit.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Best regards,</p>
<p class="MsoNormal">Radoslaw Smogura</p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="mso-element:para-border-div;border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="border:none;padding:0in"><b>From: </b><a href="mailto:ray.gavin97@gmail.com">Gavin Ray</a><br>
<b>Sent: </b>Friday, August 26, 2022 8:47 PM<br>
<b>To: </b><a href="mailto:panama-dev@openjdk.org">panama-dev@openjdk.org</a><br>
<b>Subject: </b>How to free (close) a MemorySegment slice allocated to a resource by a parent allocator?</p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">Hello all,<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I'm trying to finish the implementation of a Buffer Pool manager that uses the Foreign Memory API<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">The below may not be entirely correct (I'm new to the concept of memory management)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">One thing I can't seem to figure out at all, is how to manually deallocate memory handed out to individual pages<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">I have a contiguous block of memory/arena used for the buffer pool, each page gets a PAGE_SIZE chunk<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">If I want to delete a page from the pool, is it just zeroing the memory, or do I want to close the slice that was allocated?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Thank you =)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Code below:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">==========================================<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal">class BufferPoolManager implements AutoCloseable {<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> private static final int PAGE_SIZE = 4096;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> private static final int NUM_PAGES = 100;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"> private final MemorySession allocator = MemorySession.openConfined();<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> private final MemorySegment bufferPoolArena = allocator.allocate(NUM_PAGES * PAGE_SIZE);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"> private final DiskManager diskManager;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> private final List<HeapFilePage> pages;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> private final List<Integer> freeList;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"> public BufferPoolManager(String fileName) throws IOException {<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> this.diskManager = new DiskManager(fileName);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> this.pages = new ArrayList<>();<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> this.freeList = new ArrayList<>();<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> for (int i = 0; i < NUM_PAGES; i++) {<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> MemorySegment buffer = bufferPoolArena.asSlice(i * PAGE_SIZE, PAGE_SIZE);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> HeapFilePage page = new HeapFilePage(buffer);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> page.setPageId(i);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> pages.add(page);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> freeList.add(i);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> }<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> }<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">}<o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>