Implement no copy memory

Jorn Vernee jbvernee at xs4all.nl
Mon May 20 14:11:24 UTC 2019


Why do you want to use sun.misc.Unsafe to get the data? You can get the 
data using the Panama API as well. e.g. by overlaying some other type:

     LayoutType<MyType> MyType_t = LayoutType.ofStruct(MyType.class);
     Pointer<MyType> ptr = buf.cast(NativeTypes.VOID).cast(MyType_t);
     MyType mt = ptr.get();
     // use 'mt'

Or by overlaying a ByteBuffer:

     int size = ...;
     ByteBuffer bb = buf.asDirectByteBuffer(size);
     // use 'bb'

FWIW, the Pointer.get() API uses Unsafe internally as well.

Jorn

Ardika Rommy Sanjaya schreef op 2019-05-20 15:56:
> Thanks for response, I really appreciate.
> 
> I want when every pcap_hander::fn callback function is called,
> buf::addr() returns the address of u_char *, after that I can use
> sun.misc.Unsafe to get the content data of u_char * based on that
> address.
> 
> Please refer below code:
> 
> This is libpcap api:
> 
> int     pcap_loop(pcap_t *, int, pcap_handler, u_char *);
> typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *,
>                              const u_char *);
> 
> Below is java with part, generated by jextract with little changes:
> 
> @NativeHeader(
>         libraries = {"pcap"}
> )
> public interface pcap_mapping {
> 
> 
> @NativeFunction("(u64:${pcap}i32u64:(u64:u8u64:${pcap_pkthdr}u64:u8)vu64:u8)i32")
>         int pcap_loop(Pointer<pcap> p, int cnt, Callback<pcap_handler>
> callback, Pointer<Byte> usr);
> 
>         @FunctionalInterface
>         @NativeCallback("(u64:u8u64:${pcap_pkthdr}u64:u8)v")
>         public interface pcap_handler {
>                 void fn(Pointer<Byte> buf, Pointer<pcap_pkthdr>
> pkthdr, Pointer<Byte> arg) throws IllegalAccessException;
>         }
> 
> }
> 
> Regards
> 
>> On 20 May 2019, at 20.30, Jorn Vernee <jbvernee at xs4all.nl> wrote:
>> 
>> Hi,
>> 
>>> It looks like pamana copy value from 'u_char *' into
>>> 'Pointer<Byte>
>>> buf', correct or not?
>> 
>> Pointer<Byte> is just a wrapper for the native pointer value. Only
>> the value of the pointer itself is copied, not the contents of the
>> memory it points to.
>> 
>>> Because when I call buf.addr() it returns 0,
>>> what zero mean?
>> 
>> Zero means NULL.
>> 
>>> How I can do with no copy? Just returns the address of 'u_char *'
>>> like
>>> below in callback method?
>> 
>> Pointer::addr returns the value of the pointer, to get the address
>> of the pointer value itself, à la:
>> 
>> uchar_t **bufRef = &buf;
>> 
>> This is currently not possible.
>> 
>> Can you explain a bit more about your use case? We might be able to
>> offer some suggestions.
>> 
>> Cheers,
>> Jorn
>> 
>> Ardika Rommy Sanjaya schreef op 2019-05-20 01:33:
>> 
>>> Hi,
>>> There is native function in libpcap library: typedef void
>>> (*pcap_handler)(u_char *, const struct pcap_pkthdr *, const u_char
>>> *);
>>> When I use jextract it generate code like below:
>>> @NativeHeader(...)
>>> public interface pcap_h {
>>> ...
>>> @NativeLocation(...)
>>> 
>> 
> @NativeFunction("(u64:${pcap}i32u64:(u64:u8u64:${pcap_pkthdr}u64:u8)vu64:u8)i32")
>>> int pcap_loop(Pointer<pcap> p, int cnt, Callback<pcap_handler>
>>> callback, Pointer<Byte> usr);
>>> @FunctionalInterface
>>> @NativeCallback("(u64:u8u64:${pcap_pkthdr}u64:u8)v")
>>> interface pcap_handler {
>>> void fn(Pointer<Byte> buf, Pointer<pcap_pkthdr> pkthdr,
>>> Pointer<Byte> usr) throws IllegalAccessException;
>>> }
>>> ...
>>> }
>>> It looks like pamana copy value from 'u_char *' into
>>> 'Pointer<Byte>
>>> buf', correct or not? Because when I call buf.addr() it returns 0,
>>> what zero mean?
>>> How I can do with no copy? Just returns the address of 'u_char *'
>>> like
>>> below in callback method?
>>> void fn(long memoryAddressOf_u_char_ptr, Pointer<pcap_pkthdr>
>>> pkthdr,
>>> Pointer<Byte> usr) throws IllegalAccessException;
>>> Thanks and Regards,
>>> Ardika Rommy Sanjaya


More information about the panama-dev mailing list