in reply to Passing reference to perl from C code

For most things that Perl does with strings, it will want the string in a scalar, which implies that the string is in a buffer that Perl allocated. So most ways that you turn a C char * into a Perl string involve allocating a new buffer (often using strlen() to determine the size) and then copying the entire string into the new buffer.

The problem with this usually isn't running out of memory (since the space is usually reused if you do this over and over), but that all of the copying gets slow.

My personal preference when dealing with large strings going between Perl and C is to have Perl allocate a big scalar and put the string in there and then have C deal with that copy of the string. This is because Perl nearly refuses to even look at strings if they aren't in buffers that it allocated. [ About the only thing Perl can do with other strings involves using several iterations of pack() and unpack() to copy parts of the string into Perl's own buffers or just passing the pointer around to other C code. ]

Usually the C APIs don't have much problem with this. Rarely you find C code that insists on allocating its own buffers and also refuses to deal properly with a moved copy of the data.

Exactly how to allocate this SV to hold your string depends on, well, the details, which you didn't give. Feel free to post details if you have problems there.

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

Replies are listed 'Best First'.
Re: (tye)Re: Passing reference to perl from C code
by Anonymous Monk on Dec 21, 2000 at 22:05 UTC
    Hi Tye, Yes, You are correct. Thanks. We have to copy the string (using XPUSH) into perl stack. Our problem will be like this, we have to call the perl subroutine with some 200 to 300 string of huge size during every invocation. Please see the below C code used to call the perl subroutine.
    please note that the macros are not in the order. dSP; SAVETEMPS; PUSKMARK; XPUSHs(SV equivalent for the First string); XPUSHs(SV equivalent for the second string); : : : XPUSHs(SV equivalent for the Nth string); call_pv("pkg1::Process",G_SCALAR); something goes here
    If we have something like passing the address of the strings as in the C calls
    void Process(char *p) { // some processing } main() { char Str[500]; strcpy(Str,"slkdjaslkdjsljdlkjas"); Process(&Str);
    Is there any way to push the address of the string using XPUSHs() and that string can be used by the perl script?. Regards, Poorna

      Is there any way to push the address of the string using XPUSHs() and that string can be used by the perl script?

      I suggest you reread my previous reply as you don't appear to have understood it. The answer is "no". The best you can do is not recreate the SV equivalent of each string over and over.

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