in reply to (tye)Re4: XS question: typemap for a pointer to a pointer?
in thread XS question: typemap for a pointer to a pointer?

Ok. Here's what I tried, but libnet_build_ip() is still crapping. I understand what you said about making all the functions accept the same type at the end of your Re4. Some where accepting the buf itself and others a pointer to it even when my calls were using passing simply $buf each time. This is how I changed the XS file to fix this (I think) for the two functions in question:

int libnet_init_packet(p_size, buf) size_t p_size u_char & buf CODE: u_char * buf_temp; buf_temp = &buf; RETVAL = libnet_init_packet(p_size, &buf_temp); OUTPUT: RETVAL buf int libnet_destroy_packet(buf) u_char & buf CODE: u_char * buf_temp; buf_temp = &buf; RETVAL = libnet_destroy_packet(&buf_temp); OUTPUT: RETVAL buf sv_setsv(ST(0),&PL_sv_undef);
The resultant C code appears to hold the proper type for the passed in $buf now...
XS(XS_Net__LibnetRaw_libnet_init_packet) { dXSARGS; if (items != 2) Perl_croak(aTHX_ "Usage: Net::LibnetRaw::libnet_init_packet(p_ +size, buf)"); { size_t p_size = (size_t)SvIV(ST(0)); u_char buf = (u_char)SvUV(ST(1)); int RETVAL; dXSTARG; #line 39 "LibnetRaw.xs" u_char * buf_temp; buf_temp = &buf; RETVAL = libnet_init_packet(p_size, &buf_temp); #line 106 "LibnetRaw.c" sv_setuv(ST(1), (UV)buf); SvSETMAGIC(ST(1)); XSprePUSH; PUSHi((IV)RETVAL); } XSRETURN(1); } XS(XS_Net__LibnetRaw_libnet_destroy_packet) { dXSARGS; if (items != 1) Perl_croak(aTHX_ "Usage: Net::LibnetRaw::libnet_destroy_packet +(buf)"); { u_char buf = (u_char)SvUV(ST(0)); int RETVAL; dXSTARG; #line 50 "LibnetRaw.xs" u_char * buf_temp; buf_temp = &buf; RETVAL = libnet_destroy_packet(&buf_temp); #line 127 "LibnetRaw.c" sv_setsv(ST(0),&PL_sv_undef); SvSETMAGIC(ST(0)); XSprePUSH; PUSHi((IV)RETVAL); } XSRETURN(1); }
So now, the type for buf passed in is u_char just like the other calls. I just noticed some other value vs. pointer to value mistakes I've made (It seems * and & are not quite as similar as I thought). That aside, I still think the error I am getting does not make sense.

Replies are listed 'Best First'.
(tye)Re5: XS question: typemap for a pointer to a pointer?
by tye (Sage) on Apr 11, 2001 at 22:22 UTC

    No, you've made things worse. buf needs to always be u_char * or u_char * &. u_char & is completely different.

    Try again and/or reread what I wrote. You want $buf to hold an opaque UV that gets converted to/from a u_char *. Some of your calls should pass a reference to this u_char * rather than passing the u_char * directly (that is what the "&" is for). Those routines are the ones that might change the pointer (by allocating or reallocating the memory) and are also the ones that need buf mentioned in the OUTPUT: section.

            - tye (but my friends call me "Tye")
      Ok, I think you are bit confused about the calls. A couple of posts ago, you wrote:

      Now for the source of your problem (I think):
      XS(XS_Net__LibnetRaw_libnet_init_packet) [...] u_char * buf = (u_char *)SvPV(ST(1),PL_ +na); [...] RETVAL = libnet_init_packet(p_size, &buf); sv_setpv((SV*)ST(1), buf);
      This function seems to have used a different typemap for buf which is trying to extract the '\0'-terminated string that it is supposed to point to. I think all you need to do is fix this typemap to be like the other functions. I'm going to check some other things I noticed and I'll reply separately if I detect any problems.
      It seems that you are under the impression that libnet_init_packet() and functions like libnet_do_checksum() have the same prototype for buf. They don't. As per my original post, the prototype for libnet_init_packet() is<
      int libnet_init_packet(size_t p_size, u_char **buf);
      This function asks for a pointer to a pointer. So the pointed-to pointer (confusing, I know...) is being altered. This is how the function gets the pointer returns from malloc() back to the program. The other functions are prototyped like this:
      int libnet_build_ip([...], u_char *buf);
      The pointer itself is not being modified so the pointer to a pointer is unnecessasry. But the data being pointed to is. If I understand correctly, you thought that this was the way in wich libnet_init_packet() and libnet_destroy_packet() were also prototyped.

      So my feeling is that, like a C program would using this library, the value stored in the top level of the program in $buf should be a pointer. In C, I usually do something like this:
      u_char *buf; libnet_init_packet(IP_H + TCP_H, &buf); /* This calls malloc() and set +s buf as a pointer to that new memory */ [...] libnet_build_ip([...], buf); /* The pointer value is the same, but dat +a pointed to is altered */
      So essentially I am attempt to make the perl calls the same. I did try it some others ways, based on your suggestions and my own tinkering. In the past couple of days, I have tried a few things. Not being experience with XS, I am obviously confused on some issues. The unary & was one example.

      For now, though, I don't have a lot of time to devote to contemplating this problem. I still have some ideas that I might attempt to bounce off this thread in the future.

        Yes, I flip-flopped between thinking that all of the functions used u_char **buf and thinking what you just described. So parts of what I wrote may be a bit confusing (sorry, and more on this later). But my final analysis is correct based on what you write above.

        You need to have all of the functions use u_char *buf or u_char * &buf and nothing else. The typemap defines a way to convert between a u_char * and a Perl UV (unsigned long integer). The & tells XS to use &buf as the argument to the function. So you specify the & when the function expects u_char **buf.

        Where you quote me above, I have quoted code that shows that XS is trying to convert to/from a Perl PV (string) instead of a UV. This probably means that you used char *buf instead of u_char *buf. So what I wrote wasn't due to confusion about whether buf was a pointer or a pointer-to-a-pointer for that case.

        You do need to use the same typemap (u_char *) for all of the functions. It is just that some also need the & as well.

        The Perl variable, $buf, will hold an unsigned integer value that is the value of the u_char * pointer (just typecasted).

        Does that make more sense?

                - tye (but my friends call me "Tye")