Passing pointer of address

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Apr 29 11:08:32 UTC 2024


Hi Thiago,

So, you have a memory segment with a given address, called "attributes".

The code you have is:

1. obtaining the addres from "attributes"
2. repackaging the address in a _new_ memory segment

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).

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".

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.

I hope this helps.

Maurizio

On 29/04/2024 11:45, Thiago Milczarek Sayão wrote:
> Hi,
>
> I'm doing some wayland binding experiments with jextract and FFM.
> So far, so good.
>
> But there's one thing that could be better, or I am doing it wrong 
> (Please, excuse me if it's my mistake.).
>
> The eglChooseConfig call expects the pointer of an address on the 
> second argument.
> https://registry.khronos.org/EGL/sdk/docs/man/html/eglChooseConfig.xhtml
>
> The below code works, but I feel it could be simpler. Maybe a call 
> like attributes.addressPointer() ?
> MemorySegment eglDisplay = EGL.eglGetDisplay(display.getSegment());
> eglBindAPI(EGL_OPENGL_API());
>
> if (eglDisplay.equals(EGL_NO_DISPLAY())) {
>      System.out.println("NO DISPLAY");
>      return null;
> }
>
> if (eglInitialize(eglDisplay, MemorySegment.NULL, MemorySegment.NULL) ==0) {
>      System.out.println("eglInitialize Failed");
>      return null;
> };
>
> int[] attribs = {
>      EGL_RENDERABLE_TYPE(),
>      EGL_OPENGL_BIT(),
>      EGL_SURFACE_TYPE(),
>      EGL_WINDOW_BIT(),
>      EGL_RED_SIZE(),
>      8,
>      EGL_GREEN_SIZE(),
>      8,
>      EGL_BLUE_SIZE(),
>      8,
>      EGL_ALPHA_SIZE(),
>      8,
>      EGL_NONE()
> };
>
> MemorySegment eglConfig = Arena.global().allocate(C_POINTER);
> MemorySegment numConfigs = Arena.global().allocate(EGLint);
> MemorySegment attributes = Arena.global().allocateFrom(EGLint, attribs);
>
> if (EGL.eglChooseConfig(eglDisplay, MemorySegment.ofAddress(attributes.address()), eglConfig,1, numConfigs) ==0) {
>      System.out.println("eglChooseConfig Failed");
>      return null;
> }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20240429/3e7d8425/attachment-0001.htm>


More information about the panama-dev mailing list