--------------------------------------------- NEW C CODE: ---------------------------------------------
SV * create_image(int size) { char * image; New(0,image,size,char); return sv_setref_pv(newSViv(0),"image", (void *)image); }
void clear_image(SV * p,int size) { char * image = (char *) SvIV(SvRV(p)); memset(image,CLEAR,size); }
void destroy_image(SV * p) { char * image = (char *) SvIV(SvRV(p)); Safefree(image); }
---------------------------------------------
Basically, you cast the char * as an SV * when you create it and return it to Perl, and cast it as a char * when you pass it into a subroutine. Basically, I had to add one line of code to each subroutine (for each pointer passed), and I could leave the rest of the code as it was.
I hope someone else finds this useful.
Well I, for one, find it interesting and I'm glad you posted it.
One thing puzzles me. If you do:
$x = create_image($integer);
what manipulations can you then perform on the char string (from perl) ?
To make things a little simpler (for me), I changed the second argument of sv_setref_pv() to NULL, thus avoiding the blessing of the returned SV. I also wrote the function so that it created a null-terminated char string. I then tried to dereference $x from perl with '$$x', but that doesn't produce the original char string.
It's not hard to 'dereference' $x from the C side - clear_image() does that nicely - but how does one dereference (or do anything useful with) $x from the perl side ?
Just curious ....
Cheers, Rob
--
Any emails containing attachments will be deleted from my ISP's mail server before I even get to see them. If you wish to email me an attachment, please provide advance warning so that I can make the necessary arrangements.