Implement no copy memory

Ardika Rommy Sanjaya ardikars at gmail.com
Tue May 21 04:55:59 UTC 2019


Update

> On 21 May 2019, at 11.26, Ardika Rommy Sanjaya <ardikars at gmail.com> wrote:
> 
>> how are you calling the function? From the pcap doc, I see this:
>> 
>> 	void got_packet(u_char *args, const struct pcap_pkthdr *header,
>> 	    const u_char *packet);
> Sorry, my mistake, fixed.
> 
> I'm using sun.misc.Unsafe to get the content data and it's deprecated on java 9+ also jdk.intenal.misc.Unsafe not exported.
> If using panama should I cast the Pointer<Byte> before get the content data? Aside using direct ByteBuffer.
> 
> For example if I want to get 2 byte and then 4 byte with Unsafe I can do like below:
> 
> Unsafe UNSAFE = ...
> 
> UNSAFE.getShort(..)
> UNSAFE.getInt(...)
> 
> Thanks and Regards
> 
> 
> 
> 
>> On 20 May 2019, at 21.21, Maurizio Cimadamore <maurizio.cimadamore at oracle.com <mailto:maurizio.cimadamore at oracle.com>> wrote:
>> 
>> Hi,
>> how are you calling the function? From the pcap doc, I see this:
>> 
>> 	void got_packet(u_char *args, const struct pcap_pkthdr *header,
>> 	    const u_char *packet);
>> Let's examine this in more detail. First, you'll notice that the       function has a void return type. This is logical, because pcap_loop() wouldn't know how to handle a return value anyway. The first argument corresponds to the last argument of pcap_loop(). 
>> 
>> So, if you see that buf.addr() == 0, that probably means you passed NULL as last parameter of the pcap_loop call?
>> 
>> Maurizio
>> 
>> On 20/05/2019 14:56, Ardika Rommy Sanjaya wrote:
>>> 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> <mailto: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