Implement no copy memory

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Tue May 21 09:40:54 UTC 2019


On 21/05/2019 09:49, Florian Weimer wrote:
> * Maurizio Cimadamore:
>
>> On 21/05/2019 05:26, Ardika Rommy Sanjaya wrote:
>>> If using panama should I cast the Pointer<Byte> before get the
>>> content data?
>> In Panama you would do this:
>>
>> Pointer<Byte> buf = ...
>>
>> Pointer<Short> ps = buf.cast(NativeTypes.VOID).cast(NativeTypes.UINT16);
>> short s = ps.get();
>>
>> or...
>>
>> Pointer<Integer> pi = buf.cast(NativeTypes.VOID).cast(NativeTypes.UINT32);
>> int i = pi.get();
>>
>> Note that you have to cast through VOID first, otherwise the Panama
>> runtime will complain about incompatible cast.
> Wouldn't it make sense for packet parsing to construct a ByteBuffer
> first, and do the parsing using that?

It really depends on how low-level you want to go. If it's packet 
parsing what you want to do, it might be possible to describe the layout 
of the packet you want to parse using a 'Layout' and then you could 
overlay a custom strcut layout onto the pointer, and then access memory 
via ordinary getter calls.

If you want to go the byte-buffer way, there's a way to do that too, 
using the Pointer::asDirectByteBuffer method.

http://hg.openjdk.java.net/panama/dev/file/c65ecdbf4155/src/java.base/share/classes/java/foreign/memory/Pointer.java#l217

The new memory access-centric API we're working on will also be solid 
option, moving forward, to read (or write) from native memory - through 
the VarHandle API [1]. With that API you can build a VarHandle to 
dereference an address at a given layout element - so if you know the 
layout of the region you are accessing, you can get a bunch of VarHandle 
to do the read/write and the JIT will be able to get quite good peak 
performances (we're currently some 15% behind raw usage of Unsafe, with 
room to improve).

[1] - http://cr.openjdk.java.net/~mcimadamore/panama/memaccess.html

>
> Thanks,
> Florian


More information about the panama-dev mailing list