Dear fellow monks,
I am trying to use one of my own c files to extend (and ultimately speed up) one of my perl script.
I read the perlxstut and friends
I would like to pass a reference to an array of integers to a C function I have. The protype of the function looks like that:
double foo_in_c(int *, int);
I did something in XS that works but that in my opinion is not very elegant, and also not very efficient (I have to copy the array).
double
foo(aref)
SV * aref
INIT:
int i, last_index;
int *array;
if ((!SvROK(aref)) || (SvTYPE(SvRV(aref)) != SVt_PVAV) || ((last_i
+ndex = av_len((AV *)SvRV(aref))) < 0))
{
XSRETURN_UNDEF;
}
array = (int*)calloc(last_index+1, sizeof(int)
CODE:
for(i=0;i<=last_index;i++)
array[i] = SvIV(*av_fetch((AV *)SvRV(array), i, 0));
RETVAL = foo_in_c(array, last_index+1);
OUTPUT:
RETVAL
My question is simple: does anyone know of a better way to do that? Especially, how is it possible to avoid the copying part?
Thanks in advance,
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.