Home  |  Linux  | Mysql  | PHP  | XML
From:Sisyphus Date:Fri Aug 29 06:56:52 2008
Subject:Re: Problem with Inline C - returning an array of real numbers
----- Original Message ----- 
From: "Michael Pitzel" <michael_of_neb@yahoo.com>
.
.
#################################
The following line "works" when tested in a very small routine.
@results = map {$_} foo (var1, var2, var3, var4, var5, var6, var7, var8, 
var9, var10 );
In this example, I use the following stack method in the "C" routine to pass 
back what I want as this array:
Inline_Stack_Reset;
Inline_Stack_Push(sv_2mortal(newSViv(arg)));
...
Inline_Stack_Done;

#################################

That's only going to push one value onto the stack. For mutltiple values, 
you would normally see something like:

Inline_Stack_Reset;
for(i = 0; i < max; ++i)
    Inline_Stack_Push(sv_2mortal(newSViv(arg[i])));
Inline_Stack_Done;

 and don't forget to return them at the end:

Inline_Stack_Return(max);

##################################

Because, once I declare:
int foo (var1, var2, var3, var4, var5, var6, var7, var8, var9, var10 );
the integer declaration means that the return array disappears from the 
stack, and I have no real array.

##################################

Yep - that's right. If you want to return an array, the function needs to be 
"void".

##################################

Let me say it again.
The usage of void foo () and the above map method actually works, but only 
when used with tiny itsy bitsy stubbed out routines. It does not work when 
actual usage is attempted in the actual much larger set of routines. And, 
yes, I have checked the spellings. They're correct. This usage of the Perl 
stack ust does not work.

##################################

All that means is that you're doing something wrong :-)
You can return as many values as you want (available memory permitting).
Here we return an array of 100001 integers:

----------------------------------------------
use warnings;
use strict;

use Inline C => Config =>
    BUILD_NOISY => 1;

use Inline C => <<'EOC';

void foo(SV * x) {
     Inline_Stack_Vars;
     int i;

     Inline_Stack_Reset;

     for(i = 0; i < SvUV(x); ++i)
        Inline_Stack_Push(sv_2mortal(newSVuv(i * 3 + 17)));

     Inline_Stack_Done;
     Inline_Stack_Return(SvUV(x));
}


EOC

my @bar = foo(100001);
print "# ", scalar(@bar), " ", $bar[-1], "\n";

# prints:
# 100001 300017
----------------------------------------------

Cheers,
Rob

Navigate in group perl.inline at sever nntp.perl.org
Previous Next




  
© No Copyright
You are free to use Anything
Site Maintained by PHP Developer
Powered By PHP Consultants