<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p>Hi Thiago,<br>
</p>
<p>So, you have a memory segment with a given address, called
"attributes".</p>
<p>The code you have is:</p>
<p>1. obtaining the addres from "attributes"<br>
2. repackaging the address in a _new_ memory segment <br>
</p>
<p>Note that the segment you create in (2) is identical to the one
you started from. They have the same address. The only difference
is that the first segment has some meaningful spatial bounds (e.g.
it has a size), the second segment is a zero-length memory
segment (so you can't dereference it).</p>
<p>But from the perspective of passing this segment "by reference"
to a native function, they are exactly the same - e.g. when
passing a segment by reference, the Linker will call
"MemorySegment::address()" and pass that to the underlying
function. Since the address of segments in (1) and (2) is indeed
the same, I don't think these segments should behave differently
at all - meaning your code can be simplified, and you can just use
"attributes".</p>
<p>This misconception might have originated from the fact that FFM
doesn't provide true "stack allocation". So when you have a
segment you already have some address. Meaning that if the
original C code said something like "&foo", and "foo" is a
segment in the FFM code, you can just pass "foo" as a pointer
instead.</p>
<p>I hope this helps.<br>
</p>
<p>Maurizio<br>
</p>
<div class="moz-cite-prefix">On 29/04/2024 11:45, Thiago Milczarek
Sayão wrote:<br>
</div>
<blockquote type="cite" cite="mid:CAAP_wu=L-nLhc99tkTBfgviNi2QVAW7Cr+LDxfja+OM68SLcfw@mail.gmail.com">
<div dir="ltr">Hi,
<div><br>
</div>
<div>I'm doing some wayland binding experiments with jextract
and FFM.</div>
<div>So far, so good.</div>
<div><br>
</div>
<div>But there's one thing that could be better, or I am doing
it wrong (Please, excuse me if it's my mistake.).</div>
<div><br>
</div>
<div>The eglChooseConfig call expects the pointer of an address
on the second argument.</div>
<div><a href="https://registry.khronos.org/EGL/sdk/docs/man/html/eglChooseConfig.xhtml" moz-do-not-send="true" class="moz-txt-link-freetext">https://registry.khronos.org/EGL/sdk/docs/man/html/eglChooseConfig.xhtml</a><br>
</div>
<div><br>
</div>
<div>The below code works, but I feel it could be simpler. Maybe
a call like attributes.addressPointer() ?</div>
<div>
<div style="background-color:rgb(30,31,34);color:rgb(188,190,196)">
<pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt">MemorySegment eglDisplay = EGL.<span style="font-style:italic">eglGetDisplay</span>(display.getSegment());
<span style="font-style:italic">eglBindAPI</span>(<span style="font-style:italic">EGL_OPENGL_API</span>());
<span style="color:rgb(207,142,109)">if </span>(eglDisplay.equals(<span style="font-style:italic">EGL_NO_DISPLAY</span>())) {
System.<span style="color:rgb(199,125,187);font-style:italic">out</span>.println(<span style="color:rgb(106,171,115)">"NO DISPLAY"</span>);
<span style="color:rgb(207,142,109)">return null</span>;
}
<span style="color:rgb(207,142,109)">if </span>(<span style="font-style:italic">eglInitialize</span>(eglDisplay, MemorySegment.<span style="color:rgb(199,125,187);font-style:italic">NULL</span>, MemorySegment.<span style="color:rgb(199,125,187);font-style:italic">NULL</span>) == <span style="color:rgb(42,172,184)">0</span>) {
System.<span style="color:rgb(199,125,187);font-style:italic">out</span>.println(<span style="color:rgb(106,171,115)">"eglInitialize Failed"</span>);
<span style="color:rgb(207,142,109)">return null</span>;
};
<span style="color:rgb(207,142,109)">int</span>[] attribs = {
<span style="font-style:italic">EGL_RENDERABLE_TYPE</span>(),
<span style="font-style:italic">EGL_OPENGL_BIT</span>(),
<span style="font-style:italic">EGL_SURFACE_TYPE</span>(),
<span style="font-style:italic">EGL_WINDOW_BIT</span>(),
<span style="font-style:italic">EGL_RED_SIZE</span>(),
<span style="color:rgb(42,172,184)">8</span>,
<span style="font-style:italic">EGL_GREEN_SIZE</span>(),
<span style="color:rgb(42,172,184)">8</span>,
<span style="font-style:italic">EGL_BLUE_SIZE</span>(),
<span style="color:rgb(42,172,184)">8</span>,
<span style="font-style:italic">EGL_ALPHA_SIZE</span>(),
<span style="color:rgb(42,172,184)">8</span>,
<span style="font-style:italic">EGL_NONE</span>()
};
MemorySegment eglConfig = Arena.<span style="font-style:italic">global</span>().allocate(<span style="color:rgb(199,125,187);font-style:italic">C_POINTER</span>);
MemorySegment numConfigs = Arena.<span style="font-style:italic">global</span>().allocate(<span style="color:rgb(199,125,187);font-style:italic">EGLint</span>);
MemorySegment attributes = Arena.<span style="font-style:italic">global</span>().allocateFrom(<span style="color:rgb(199,125,187);font-style:italic">EGLint</span>, attribs);
<span style="color:rgb(207,142,109)">if </span>(EGL.<span style="font-style:italic">eglChooseConfig</span>(eglDisplay, MemorySegment.<span style="font-style:italic">ofAddress</span>(attributes.address()), eglConfig, <span style="color:rgb(42,172,184)">1</span>, numConfigs) == <span style="color:rgb(42,172,184)">0</span>) {
System.<span style="color:rgb(199,125,187);font-style:italic">out</span>.println(<span style="color:rgb(106,171,115)">"eglChooseConfig Failed"</span>);
<span style="color:rgb(207,142,109)">return null</span>;
}</pre>
</div>
</div>
</div>
</blockquote>
</body>
</html>