Complex extensions in C

A

arne.muller

Hello,

I'm looking for some resources (books, web-sites, docs ...) on how to
write complex perl extensions in C. I've read the perlxstut, but still
don't understand how to convert complex types.

Specificly, my C-routine needs a pointer to a structure, some members
of this structure are pointers to type double arrays. On the perl side
this structure is an object. The return value of the C function is a
different type of structure, but again containing pointers to array (on
the perl side this is an object).

Maybe you can point out some places on the web or news grou articles (I
didn't find much!).

greetings,

Arne
 
A

anno4000

Hello,

I'm looking for some resources (books, web-sites, docs ...) on how to
write complex perl extensions in C. I've read the perlxstut, but still
don't understand how to convert complex types.

Specificly, my C-routine needs a pointer to a structure, some members
of this structure are pointers to type double arrays. On the perl side
this structure is an object. The return value of the C function is a
different type of structure, but again containing pointers to array (on
the perl side this is an object).

So you want to translate Perl objects (which presumably contain lists of
numbers) into C structs for consumption by XS routines. The basic
method is to use pack() to build the struct in a string. You pass the
string to the XS routine as such and cast it to a (pointer to) the type
of struct you have built. If you have got it right (which will be
non-trivial), you'll find a workable struct in your XS program.

You'll have to use the cumbersome "p" template to create pointers
from one part of the string to another.

Anno
 
B

Ben Morrow

Quoth (e-mail address removed)-berlin.de:
So you want to translate Perl objects (which presumably contain lists of
numbers) into C structs for consumption by XS routines. The basic
method is to use pack() to build the struct in a string. You pass the
string to the XS routine as such and cast it to a (pointer to) the type
of struct you have built. If you have got it right (which will be
non-trivial), you'll find a workable struct in your XS program.

You'll have to use the cumbersome "p" template to create pointers
from one part of the string to another.

Note that there are nontrivial issues regarding struct packing and the
like (you will likely need a little C program to construct the pack
template for you using offsetof). You can also pass a hash to an XS
routine that then builds the struct by pulling the members out of the
hash in C.

It may be easier to make the Perl side properly OO, or implement a tied
hash/array, and keep the data in 'C format' all the time.

Ben
 
A

arne.muller

A Perl object or merely an opaque object held in Perl? I.E. does Perl have
to do anything with it, other than just pass the root pointer back into C?

Well, this should be a real perl object. The part that I'd like to
implement in C is just part of a larger application in which this
object lives ...

Arne
 
A

arne.muller

Ben said:
Quoth (e-mail address removed)-berlin.de:

Note that there are nontrivial issues regarding struct packing and the
like (you will likely need a little C program to construct the pack
template for you using offsetof). You can also pass a hash to an XS
routine that then builds the struct by pulling the members out of the
hash in C.

It may be easier to make the Perl side properly OO, or implement a tied
hash/array, and keep the data in 'C format' all the time.

Maybe it's easier to write a wrapper routine in perl that takes the
perl object, extracts it's attributes (scalars and arrays) and passes
it to a C-wrapper routine which re-creates the proper C-structs from
it's function arguments. Finally the C-routine that takes athe pointe
rargument is called from this wrapper. This means the perl object gets
copied, and there's a perl object and a C struct. This may just be fine
for me.

regards,

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top