Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Thanks for the notes on proper allocation/free and converting between pointers and integers.

The approach I outlined is very similar to T_PTROBJ, both of which are about the best you can do (which isn't very good in my book) if you need to have Perl deal with data structures that Perl does not allocate.

Since you can use Safefree/New, then you are allocating the space and you can have a better interface by having Perl allocate the space instead.

SV * xs_new( ... ) CODE: size_t len= number_of_bytes_needed(); sv_grow( RETVAL, len ); // "malloc" void *buf= SvPV( RETVAL ); size_t used= constructor( buf, len, ... ); // Tell Perl how much of the buffer is actually in use: SvCUR_set( RETVAL, used );
Then you end up with a Perl scalar that contains a string value which is the contents of the buffer you allocated and Perl will be happy to free() it when it needs to and you don't even need a DESTROY method for that anymore. You can also access the buffer data directly from Perl now in case there are simple things you want to do that don't really require that XS code be written.

You can get the pointer to this buffer by simply using "char *" arguments in other XS functions.

And if you want a destroy method for other reasons, then I'd still do the bless and DESTROY in Perl code rather than in XS.

Be aware that passing this string value around directly in Perl can easily result in the value being copied (a new buffer malloc()d and the data copied from the old buffer to the new buffer) and that you can modify the buffer from Perl code such that Perl decides to realloc() the buffer which can relocate it in memory (and that both of these things can invalidate some types of data such a buffers that contain pointers into themselves).

Note that the value is not copied when you use the string as an argument to a function but that typical Perl functions include my $buf= shift(@_); or my( $buf, ... )= @_; which is where the copy takes place. So using the string directly in an XS function with a parameter type of "char *" works great.

I realize that PodMaster probably knows most or all of this, but I include this information for others that might read this node.

So you might want to wrap this string up as an object so passing around the object will just be copying references to the string (or references to some data structure that contains the string) and the string itself won't get copied.

Doing that means that you have an extra dereference required in your methods. You can do that in Perl, but if that is the only bit of "Perl written in C" that you need for that method, you might just do it all in XS. The big problems with writing "Perl in C" for XS is if you are creating things on the stack (everyone gets the mortal/refcount stuff wrong even though it isn't that complicated/confusing if you really get your mind around it) or trying to deal with Perl variables that users pass to you (XS code only deals with very vanilla variables and simply breaks in the face of tie or magic or a need for autovivification, etc. -- I've been thinking about croaking on "thinkfirst" in my XS code in future).

                - tye

In reply to Re: Tie::PureDB is out (thanks/PV) by tye
in thread panic: leave_scope inconsistency. (need help creating Tie::PureDB) by PodMaster

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (5)
    As of 2017-12-12 20:05 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (335 votes). Check out past polls.