[foreign-memaccess+abi] RFR: 8291639: Improve the ability to visualize a MemorySegment in human readable forms [v18]
Maurizio Cimadamore
mcimadamore at openjdk.org
Wed May 31 12:08:25 UTC 2023
On Wed, 31 May 2023 12:05:43 GMT, Per Minborg <pminborg at openjdk.org> wrote:
>> This PR proposes to add a "Record Mapper" that allows `MemorySegment` instances to be projected onto `Record` types given `GroupLayout` instances.
>>
>> Here is a simple example of how to use the mapper:
>>
>>
>> private static final GroupLayout POINT_LAYOUT = MemoryLayout.structLayout(
>> JAVA_INT.withName("x"),
>> JAVA_INT.withName("y"));
>>
>> MemorySegment segment = MemorySegment.ofArray(new int[]{3, 4});
>>
>> Point point = POINT_LAYOUT.recordMapper(Point.class)
>> .apply(segment); // Point[x=3, y=4]
>>
>>
>> I think the implementation can be improved later, for example by de-duplicating handling of arrays and maybe add recursive "un-pealing" of multidimensional arrays.
>
> Per Minborg has updated the pull request incrementally with one additional commit since the last revision:
>
> Clean up code
src/java.base/share/classes/jdk/internal/foreign/LayoutRecordMapper.java line 470:
> 468:
> 469: R[] result = (R[]) Array.newInstance(type, Math.toIntExact(count));
> 470: Spliterator<MemorySegment> spliterator = segment.spliterator(elementLayout);
Using MemorySegment::elements allows you to avoid interacting with the spliterator directly (which I don't think you need here?)
-------------
PR Review Comment: https://git.openjdk.org/panama-foreign/pull/833#discussion_r1211604761
More information about the panama-dev
mailing list