in reply to Re^2: perl XS - passing array to C and getting it back
in thread perl XS - passing array to C and getting it back
I need the first option.
Then do_nothing1() below will get you started:
#! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => '_1159002', CLEAN_AFTER_BUILD => 0 +; #define IS_VARS Inline_Stack_Vars #define IS_RESET Inline_Stack_Reset #define IS_ITEMS Inline_Stack_Items #define IS_ITEM( n ) Inline_Stack_Item( n ) #define IS_PUSHS( s ) Inline_Stack_Push( sv_2mortal( s ) ) #define IS_PUSHIV( iv ) Inline_Stack_Push( sv_2mortal( newSViv( iv ) ) + ) #define IS_PUSHUV( uv ) Inline_Stack_Push( sv_2mortal( newSVuv( uv ) ) + ) #define IS_PUSHNV( nv ) Inline_Stack_Push( sv_2mortal( newSVnv( nv ) ) + ) #define IS_DONE Inline_Stack_Done double do_nothing1( AV *x, int sizeOfX ) { int index; for( index=0; index<sizeOfX; index++ ) { av_store( x, index, newSVnv( 1.0 ) ); } return SvNV( *av_fetch( x, 0, NULL ) ); } void do_nothing2( AV *a, SV *size ) { IS_VARS; int index; IS_RESET; for( index = 0; index < SvIV( size ); ++index ) { IS_PUSHNV( 1.0 ); } IS_DONE; } void do_nothing3( SV*dummy, ... ) { IS_VARS; int index; for( index = 0; index < IS_ITEMS; ++index ) { IS_ITEM( index ) = newSVnv( 1.0 ); } return; } AV *do_nothing4( AV *x ) { IS_VARS; AV *r = newAV(); int index; for( index = 0; index < IS_ITEMS; ++index ) { av_push( r, newSVnv( SvNV( *av_fetch( x, index, NULL ) ) + 1.0 + ) ); } return r; } END_C my @a = ( 0 ) x 10; print do_nothing1( \@a, 10 ); print "@a"; my @b = ( 0 ) x 10; print join ' ', do_nothing2( \@b, 10 ); print join ' ', do_nothing3( 1 .. 10 ); my @c = ( 1 .. 10 ); print join ' ', do_nothing4( \@c );
If you have Inline::C installed and you run the above code you'll get this output:
C:\test>1159002.pl 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
If you then look in the _inline directory tree created below the cwd where you run this, and look for the file _Inline/build/_1159002/_1159002.xs you'll see how it all fits together:
C:\test>type _Inline\build\_1159002\_1159002.xs #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "INLINE.h" #define IS_VARS Inline_Stack_Vars #define IS_RESET Inline_Stack_Reset #define IS_ITEMS Inline_Stack_Items #define IS_ITEM( n ) Inline_Stack_Item( n ) #define IS_PUSHS( s ) Inline_Stack_Push( sv_2mortal( s ) ) #define IS_PUSHIV( iv ) Inline_Stack_Push( sv_2mortal( newSViv( iv ) ) + ) #define IS_PUSHUV( uv ) Inline_Stack_Push( sv_2mortal( newSVuv( uv ) ) + ) #define IS_PUSHNV( nv ) Inline_Stack_Push( sv_2mortal( newSVnv( nv ) ) + ) #define IS_DONE Inline_Stack_Done double do_nothing1( AV *x, int sizeOfX ) { int index; for( index=0; index<sizeOfX; index++ ) { av_store( x, index, newSVnv( 1.0 ) ); } return SvNV( *av_fetch( x, 0, NULL ) ); } void do_nothing2( AV *a, SV *size ) { IS_VARS; int index; IS_RESET; for( index = 0; index < SvIV( size ); ++index ) { IS_PUSHNV( 1.0 ); } IS_DONE; } void do_nothing3( SV*dummy, ... ) { IS_VARS; int index; for( index = 0; index < IS_ITEMS; ++index ) { IS_ITEM( index ) = newSVnv( 1.0 ); } return; } AV *do_nothing4( AV *x ) { IS_VARS; AV *r = newAV(); int index; for( index = 0; index < IS_ITEMS; ++index ) { av_push( r, newSVnv( SvNV( *av_fetch( x, index, NULL ) ) + 1.0 + ) ); } return r; } MODULE = _1159002 PACKAGE = main PROTOTYPES: DISABLE double do_nothing1 (x, sizeOfX) AV * x int sizeOfX void do_nothing2 (a, size) AV * a SV * size PREINIT: I32* temp; PPCODE: temp = PL_markstack_ptr++; do_nothing2(a, size); if (PL_markstack_ptr != temp) { /* truly void, because dXSARGS not invoked */ PL_markstack_ptr = temp; XSRETURN_EMPTY; /* return empty stack */ } /* must have used dXSARGS; list context implied */ return; /* assume stack size is correct */ void do_nothing3 (dummy, ...) SV * dummy PREINIT: I32* temp; PPCODE: temp = PL_markstack_ptr++; do_nothing3(dummy); if (PL_markstack_ptr != temp) { /* truly void, because dXSARGS not invoked */ PL_markstack_ptr = temp; XSRETURN_EMPTY; /* return empty stack */ } /* must have used dXSARGS; list context implied */ return; /* assume stack size is correct */ AV * do_nothing4 (x) AV * x
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: perl XS - passing array to C and getting it back
by kopolov (Acolyte) on Mar 29, 2016 at 13:11 UTC | |
by Corion (Patriarch) on Mar 29, 2016 at 13:20 UTC | |
by BrowserUk (Patriarch) on Mar 29, 2016 at 13:35 UTC | |
by Tux (Canon) on Mar 29, 2016 at 13:56 UTC |