On Apr 5, 2006, at 1:02 PM, Igor Sysoev wrote:

is the effective way to return data from XS to perl without unnecessary
memory coping ?

Assuming that your C function can write to a passed in char* and that you can know the length in advance, the best way is to create an SV with a string buffer of sufficient length, write to the that buffer directly, and put that SV on the stack.

    SV*
    efficient_read_from_c()
    PREINIT:
        SV     *fresh_sv;
        STRLEN  len;
        char   *ptr;
    CODE:
        len = get_len();
        SV* fresh_sv = newSV(len);
        SvPOK_on(fresh_sv);
        ptr = SvPVX(fresh_sv);
        if (get_string(ptr))
            SvCUR_set(fresh_sv, len);
        *SvEND(fresh_sv) = '\0';
        RETVAL = fresh_sv;
    OUTPUT: RETVAL

This same subject was just discussed at some length on PerlMonks: <http://www.perlmonks.org/index.pl?node_id=541706>.

Marvin Humphrey
Rectangular Research
http://www.rectangular.com/

Reply via email to