.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "PERLXSTYPEMAP 1" .TH PERLXSTYPEMAP 1 2024-01-12 "perl v5.38.2" "Perl Programmers Reference Guide" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME perlxstypemap \- Perl XS C/Perl type mapping .SH DESCRIPTION .IX Header "DESCRIPTION" The more you think about interfacing between two languages, the more you'll realize that the majority of programmer effort has to go into converting between the data structures that are native to either of the languages involved. This trumps other matter such as differing calling conventions because the problem space is so much greater. There are simply more ways to shove data into memory than there are ways to implement a function call. .PP Perl XS' attempt at a solution to this is the concept of typemaps. At an abstract level, a Perl XS typemap is nothing but a recipe for converting from a certain Perl data structure to a certain C data structure and vice versa. Since there can be C types that are sufficiently similar to one another to warrant converting with the same logic, XS typemaps are represented by a unique identifier, henceforth called an \fBXS type\fR in this document. You can then tell the XS compiler that multiple C types are to be mapped with the same XS typemap. .PP In your XS code, when you define an argument with a C type or when you are using a \f(CW\*(C`CODE:\*(C'\fR and an \f(CW\*(C`OUTPUT:\*(C'\fR section together with a C return type of your XSUB, it'll be the typemapping mechanism that makes this easy. .SS "Anatomy of a typemap" .IX Subsection "Anatomy of a typemap" In more practical terms, the typemap is a collection of code fragments which are used by the \fBxsubpp\fR compiler to map C function parameters and values to Perl values. The typemap file may consist of three sections labelled \f(CW\*(C`TYPEMAP\*(C'\fR, \f(CW\*(C`INPUT\*(C'\fR, and \f(CW\*(C`OUTPUT\*(C'\fR. An unlabelled initial section is assumed to be a \f(CW\*(C`TYPEMAP\*(C'\fR section. The INPUT section tells the compiler how to translate Perl values into variables of certain C types. The OUTPUT section tells the compiler how to translate the values from certain C types into values Perl can understand. The TYPEMAP section tells the compiler which of the INPUT and OUTPUT code fragments should be used to map a given C type to a Perl value. The section labels \f(CW\*(C`TYPEMAP\*(C'\fR, \f(CW\*(C`INPUT\*(C'\fR, or \&\f(CW\*(C`OUTPUT\*(C'\fR must begin in the first column on a line by themselves, and must be in uppercase. .PP Each type of section can appear an arbitrary number of times and does not have to appear at all. For example, a typemap may commonly lack \f(CW\*(C`INPUT\*(C'\fR and \f(CW\*(C`OUTPUT\*(C'\fR sections if all it needs to do is associate additional C types with core XS types like T_PTROBJ. Lines that start with a hash \f(CW\*(C`#\*(C'\fR are considered comments and ignored in the \f(CW\*(C`TYPEMAP\*(C'\fR section, but are considered significant in \f(CW\*(C`INPUT\*(C'\fR and \f(CW\*(C`OUTPUT\*(C'\fR. Blank lines are generally ignored. .PP Traditionally, typemaps needed to be written to a separate file, conventionally called \f(CW\*(C`typemap\*(C'\fR in a CPAN distribution. With ExtUtils::ParseXS (the XS compiler) version 3.12 or better which comes with perl 5.16, typemaps can also be embedded directly into XS code using a HERE-doc like syntax: .PP .Vb 3 \& TYPEMAP: <int_member)); \& hv_stores(hash, "float_member", newSVnv(in\->float_member)); \& /* ... */ \& \& /* mortalize as thy stack is not refcounted */ \& sv_setsv(out, sv_2mortal(newRV_noinc((SV*)hash))); \& } .Ve .Sp The conversion from Perl to C is left as an exercise to the reader, but the prototype would be: .Sp .Vb 2 \& static foo_t * \& XS_unpack_foo_tPtr(SV *in); .Ve .Sp Instead of an actual C function that has to fetch the thread context using \f(CW\*(C`dTHX\*(C'\fR, you can define macros of the same name and avoid the overhead. Also, keep in mind to possibly free the memory allocated by \&\f(CW\*(C`XS_unpack_foo_tPtr\*(C'\fR. .IP T_PACKEDARRAY 4 .IX Item "T_PACKEDARRAY" T_PACKEDARRAY is similar to T_PACKED. In fact, the \f(CW\*(C`INPUT\*(C'\fR (Perl to XSUB) typemap is identical, but the \f(CW\*(C`OUTPUT\*(C'\fR typemap passes an additional argument to the \f(CW\*(C`XS_pack_$ntype\*(C'\fR function. This third parameter indicates the number of elements in the output so that the function can handle C arrays sanely. The variable needs to be declared by the user and must have the name \&\f(CW\*(C`count_$ntype\*(C'\fR where \f(CW$ntype\fR is the normalized C type name as explained above. The signature of the function would be for the example above and \f(CW\*(C`foo_t **\*(C'\fR: .Sp .Vb 2 \& static void \& XS_pack_foo_tPtrPtr(SV *out, foo_t *in, UV count_foo_tPtrPtr); .Ve .Sp The type of the third parameter is arbitrary as far as the typemap is concerned. It just has to be in line with the declared variable. .Sp Of course, unless you know the number of elements in the \&\f(CW\*(C`sometype **\*(C'\fR C array, within your XSUB, the return value from \&\f(CW\*(C`foo_t ** XS_unpack_foo_tPtrPtr(...)\*(C'\fR will be hard to decipher. Since the details are all up to the XS author (the typemap user), there are several solutions, none of which particularly elegant. The most commonly seen solution has been to allocate memory for N+1 pointers and assign \f(CW\*(C`NULL\*(C'\fR to the (N+1)th to facilitate iteration. .Sp Alternatively, using a customized typemap for your purposes in the first place is probably preferable. .IP T_DATAUNIT 4 .IX Item "T_DATAUNIT" NOT YET .IP T_CALLBACK 4 .IX Item "T_CALLBACK" NOT YET .IP T_ARRAY 4 .IX Item "T_ARRAY" This is used to convert the perl argument list to a C array and for pushing the contents of a C array onto the perl argument stack. .Sp The usual calling signature is .Sp .Vb 1 \& @out = array_func( @in ); .Ve .Sp Any number of arguments can occur in the list before the array but the input and output arrays must be the last elements in the list. .Sp When used to pass a perl list to C the XS writer must provide a function (named after the array type but with 'Ptr' substituted for \&'*') to allocate the memory required to hold the list. A pointer should be returned. It is up to the XS writer to free the memory on exit from the function. The variable \f(CW\*(C`ix_$var\*(C'\fR is set to the number of elements in the new array. .Sp When returning a C array to Perl the XS writer must provide an integer variable called \f(CW\*(C`size_$var\*(C'\fR containing the number of elements in the array. This is used to determine how many elements should be pushed onto the return argument stack. This is not required on input since Perl knows how many arguments are on the stack when the routine is called. Ordinarily this variable would be called \f(CW\*(C`size_RETVAL\*(C'\fR. .Sp Additionally, the type of each element is determined from the type of the array. If the array uses type \f(CW\*(C`intArray *\*(C'\fR xsubpp will automatically work out that it contains variables of type \f(CW\*(C`int\*(C'\fR and use that typemap entry to perform the copy of each element. All pointer '*' and 'Array' tags are removed from the name to determine the subtype. .IP T_STDIO 4 .IX Item "T_STDIO" This is used for passing perl filehandles to and from C using \&\f(CW\*(C`FILE *\*(C'\fR structures. .IP T_INOUT 4 .IX Item "T_INOUT" This is used for passing perl filehandles to and from C using \&\f(CW\*(C`PerlIO *\*(C'\fR structures. The file handle can used for reading and writing. This corresponds to the \f(CW\*(C`+<\*(C'\fR mode, see also T_IN and T_OUT. .Sp See perliol for more information on the Perl IO abstraction layer. Perl must have been built with \f(CW\*(C`\-Duseperlio\*(C'\fR. .Sp There is no check to assert that the filehandle passed from Perl to C was created with the right \f(CWopen()\fR mode. .Sp Hint: The perlxstut tutorial covers the T_INOUT, T_IN, and T_OUT XS types nicely. .IP T_IN 4 .IX Item "T_IN" Same as T_INOUT, but the filehandle that is returned from C to Perl can only be used for reading (mode \f(CW\*(C`<\*(C'\fR). .IP T_OUT 4 .IX Item "T_OUT" Same as T_INOUT, but the filehandle that is returned from C to Perl is set to use the open mode \f(CW\*(C`+>\*(C'\fR.