Implement no copy memory
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Mon May 20 14:21:19 UTC 2019
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> 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