[foreign-memaccess+abi] RFR: 8291639: Improve the ability to visualize a MemorySegment in human readable forms [v2]

Glavo duke at openjdk.org
Fri May 26 15:46:15 UTC 2023


On Fri, 26 May 2023 15:24:56 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> Is it possible that we will get something like this in the future:
>> 
>> C Header:
>> 
>> struct Point {
>>     int x, y;
>> };
>> 
>> extern void f(struct Point p);
>> 
>> 
>> Java Code:
>> 
>> record Point(int x, int y) {}
>> 
>> ValueLayout pointLayout = MemoryLayout.structLayout(
>>                 JAVA_INT.withName("x"),
>>                 JAVA_INT.withName("y"))
>>         .withCarrier(Point.class);
>> 
>> MethodHandle f = nativeLinker.downcallHandle(lookup.find("f").get(), FunctionDescriptor.ofVoid(pointLayout));
>> f.invoke(new Point(10, 20)); // ok
>
>> Is it possible that we will get something like this in the future:
> 
> I believe record mapper is a "high-level" kind of mapping, so supporting this mapping from the linker out of the box would be too magic. Note also that, if you pull on that string, you might also want to express memory dereference directly using records. We thought about this kind of stuff but found it problematic because, while it's easy to read a segment "as a record" the other way around is not easy. As you point out there are lifetime issues, but also issues when it comes to writing unions (which fields of the record/union do we write?).
> 
> One of the strength of the Linker API (and FFM API) is that it is built out of very simple primitives, but it also allows composition. So, if you have a downcall which returns a segment, it is quite easy to adapt it to one that returns a record (same for var handles).
> 
> But records are only _one_ of the possible ways to map foreign types into Java-land (and one that has few limitations, as for unions). Other clients might prefer mapping structs/unions with simple classes which wrap a memory segment (which might not be too bad if these classes are Valhalla values). From a design perspective, the linker should be relatively agnostic about these kinds of high-level mappings (but should allow for tools that desire to do so, to provide said mappings).

@mcimadamore I realized that establishing a mapping between record and MemorySegment was an overly high-level operation, so I later modified the above example to focus on mapping between record and struct.



> ```java
> record Point(int x, int y) {}
> 
> ValueLayout pointLayout = MemoryLayout.structLayout(
>                 JAVA_INT.withName("x"),
>                 JAVA_INT.withName("y"))
>         .withCarrier(Point.class);
> 
> MethodHandle f = nativeLinker.downcallHandle(lookup.find("f").get(), FunctionDescriptor.ofVoid(pointLayout));
> f.invoke(new Point(10, 20)); // ok
> ```

Now we do not have any type that directly corresponds to the struct type of C, so we have to convert the value of the structure to MemorySegment before passing it.

I think this status quo is unsatisfactory. Because of this, I think providing the ability to establish a mapping between record and struct is meaningful.

-------------

PR Comment: https://git.openjdk.org/panama-foreign/pull/833#issuecomment-1564585925


More information about the panama-dev mailing list