Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) XS language reference manualAn overview of the Perl interpreter (Berikutnya)
Internals and C language interface

Perl XS C/Perl type mapping

Daftar Isi

NAME

perlxstypemap - Perl XS C/Perl type mapping

DESCRIPTION

The more you think about interfacing between two languages, the moreyou'll realize that the majority of programmer effort has to go intoconverting between the data structures that are native to either ofthe languages involved. This trumps other matter such as differingcalling conventions because the problem space is so much greater.There are simply more ways to shove data into memory than there areways to implement a function call.

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 forconverting from a certain Perl data structure to a certain Cdata structure and vice versa. Since there can be C types thatare sufficiently similar to warrant converting with the same logic,XS typemaps are represented by a unique identifier, henceforthcalled an <XS type> in this document. You can then tell the XScompiler that multiple C types are to be mapped with the sameXS typemap.

In your XS code, when you define an argument with a C type or whenyou are using a CODE: and an OUTPUT: section together with aC return type of your XSUB, it'll be the typemapping mechanism thatmakes this easy.

Anatomy of a typemap

In more practical terms, the typemap is a collection of codefragments which are used by the xsubpp compiler to map C functionparameters and values to Perl values. The typemap file may consistof three sections labelled TYPEMAP, INPUT, and OUTPUT.An unlabelled initial section is assumed to be a TYPEMAP section.The INPUT section tells the compiler how to translate Perl valuesinto variables of certain C types. The OUTPUT section tells thecompiler how to translate the values from certain C types into valuesPerl can understand. The TYPEMAP section tells the compiler whichof the INPUT and OUTPUT code fragments should be used to map a givenC type to a Perl value. The section labels TYPEMAP, INPUT, orOUTPUT must begin in the first column on a line by themselves,and must be in uppercase.

Each type of section can appear an arbitrary number of timesand does not have to appear at all. For example, a typemap maycommonly lack INPUT and OUTPUT sections if all it needs todo is associate additional C types with core XS types like T_PTROBJ.Lines that start with a hash # are considered comments and ignoredin the TYPEMAP section, but are considered significant in INPUTand OUTPUT. Blank lines are generally ignored.

Traditionally, typemaps needed to be written to a separate file,conventionally called typemap in a CPAN distribution. WithExtUtils::ParseXS (the XS compiler) version 3.12 or better whichcomes with perl 5.16, typemaps can also be embedded directly intoXS code using a HERE-doc like syntax:

  1. TYPEMAP: <<HERE
  2. ...
  3. HERE

where HERE can be replaced by other identifiers like with normalPerl HERE-docs. All details below about the typemap textual formatremain valid.

The TYPEMAP section should contain one pair of C type andXS type per line as follows. An example from the core typemap file:

  1. TYPEMAP
  2. # all variants of char* is handled by the T_PV typemap
  3. char * T_PV
  4. const char * T_PV
  5. unsigned char * T_PV
  6. ...

The INPUT and OUTPUT sections have identical formats, that is,each unindented line starts a new in- or output map respectively.A new in- or output map must start with the name of the XS type tomap on a line by itself, followed by the code that implements itindented on the following lines. Example:

  1. INPUT
  2. T_PV
  3. $var = ($type)SvPV_nolen($arg)
  4. T_PTR
  5. $var = INT2PTR($type,SvIV($arg))

We'll get to the meaning of those Perlish-looking variables in alittle bit.

Finally, here's an example of the full typemap file for mapping Cstrings of the char * type to Perl scalars/strings:

  1. TYPEMAP
  2. char * T_PV
  3. INPUT
  4. T_PV
  5. $var = ($type)SvPV_nolen($arg)
  6. OUTPUT
  7. T_PV
  8. sv_setpv((SV*)$arg, $var);

Here's a more complicated example: suppose that you wantedstruct netconfig to be blessed into the class Net::Config.One way to do this is to use underscores (_) to separate packagenames, as follows:

  1. typedef struct netconfig * Net_Config;

And then provide a typemap entry T_PTROBJ_SPECIAL that mapsunderscores to double-colons (::), and declare Net_Config to be ofthat type:

  1. TYPEMAP
  2. Net_Config T_PTROBJ_SPECIAL
  3. INPUT
  4. T_PTROBJ_SPECIAL
  5. if (sv_derived_from($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")){
  6. IV tmp = SvIV((SV*)SvRV($arg));
  7. $var = INT2PTR($type, tmp);
  8. }
  9. else
  10. croak(\"$var is not of type ${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")
  11. OUTPUT
  12. T_PTROBJ_SPECIAL
  13. sv_setref_pv($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\",
  14. (void*)$var);

The INPUT and OUTPUT sections substitute underscores for double-colonson the fly, giving the desired effect. This example demonstrates someof the power and versatility of the typemap facility.

The INT2PTR macro (defined in perl.h) casts an integer to a pointerof a given type, taking care of the possible different size of integersand pointers. There are also PTR2IV, PTR2UV, PTR2NV macros,to map the other way, which may be useful in OUTPUT sections.

The Role of the typemap File in Your Distribution

The default typemap in the lib/ExtUtils directory of the Perl sourcecontains many useful types which can be used by Perl extensions. Someextensions define additional typemaps which they keep in their own directory.These additional typemaps may reference INPUT and OUTPUT maps in the maintypemap. The xsubpp compiler will allow the extension's own typemap tooverride any mappings which are in the default typemap. Instead of usingan additional typemap file, typemaps may be embedded verbatim in XSwith a heredoc-like syntax. See the documentation on the TYPEMAP: XSkeyword.

For CPAN distributions, you can assume that the XS types defined bythe perl core are already available. Additionally, the core typemaphas default XS types for a large number of C types. For example, ifyou simply return a char * from your XSUB, the core typemap willhave this C type associated with the T_PV XS type. That means yourC string will be copied into the PV (pointer value) slot of a new scalarthat will be returned from your XSUB to to Perl.

If you're developing a CPAN distribution using XS, you may add your ownfile called typemap to the distribution. That file may containtypemaps that either map types that are specific to your code or thatoverride the core typemap file's mappings for common C types.

Sharing typemaps Between CPAN Distributions

Starting with ExtUtils::ParseXS version 3.13_01 (comes with perl 5.16and better), it is rather easy to share typemap code between multipleCPAN distributions. The general idea is to share it as a module thatoffers a certain API and have the dependent modules declare that as abuilt-time requirement and import the typemap into the XS. An exampleof such a typemap-sharing module on CPAN isExtUtils::Typemaps::Basic. Two steps to getting that module'stypemaps available in your code:

  • Declare ExtUtils::Typemaps::Basic as a build-time dependencyin Makefile.PL (use BUILD_REQUIRES), or in your Build.PL(use build_requires).

  • Include the following line in the XS section of your XS file:(don't break the line)

    1. INCLUDE_COMMAND: $^X -MExtUtils::Typemaps::Cmd
    2. -e "print embeddable_typemap(q{Basic})"

Writing typemap Entries

Each INPUT or OUTPUT typemap entry is a double-quoted Perl string thatwill be evaluated in the presence of certain variables to get thefinal C code for mapping a certain C type.

This means that you can embed Perl code in your typemap (C) code usingconstructs such as${ perl code that evaluates to scalar reference here }. A commonuse case is to generate error messages that refer to the true functionname even when using the ALIAS XS feature:

  1. ${ $ALIAS ? \q[GvNAME(CvGV(cv))] : \qq[\"$pname\"] }

For many typemap examples, refer to the core typemap file that can befound in the perl source tree at lib/ExtUtils/typemap.

The Perl variables that are available for interpolation into typemapsare the following:

  • $var - the name of the input or output variable, eg. RETVAL forreturn values.

  • $type - the raw C type of the parameter, any : replaced with_.

  • $ntype - the supplied type with * replaced with Ptr.e.g. for a type of Foo::Bar, $ntype is Foo::Bar

  • $arg - the stack entry, that the parameter is input from or outputto, e.g. ST(0)

  • $argoff - the argument stack offset of the argument. ie. 0 for thefirst argument, etc.

  • $pname - the full name of the XSUB, with including the PACKAGEname, with any PREFIX stripped. This is the non-ALIAS name.

  • $Package - the package specified by the most recent PACKAGEkeyword.

  • $ALIAS - non-zero if the current XSUB has any aliases declared withALIAS.

Full Listing of Core Typemaps

Each C type is represented by an entry in the typemap file thatis responsible for converting perl variables (SV, AV, HV, CV, etc.)to and from that type. The following sections list all XS typesthat come with perl by default.

  • T_SV

    This simply passes the C representation of the Perl variable (an SV*)in and out of the XS layer. This can be used if the C code wantsto deal directly with the Perl variable.

  • T_SVREF

    Used to pass in and return a reference to an SV.

    Note that this typemap does not decrement the reference countwhen returning the reference to an SV*.See also: T_SVREF_REFCOUNT_FIXED

  • T_SVREF_FIXED

    Used to pass in and return a reference to an SV.This is a fixedvariant of T_SVREF that decrements the refcount appropriatelywhen returning a reference to an SV*. Introduced in perl 5.15.4.

  • T_AVREF

    From the perl level this is a reference to a perl array.From the C level this is a pointer to an AV.

    Note that this typemap does not decrement the reference countwhen returning an AV*. See also: T_AVREF_REFCOUNT_FIXED

  • T_AVREF_REFCOUNT_FIXED

    From the perl level this is a reference to a perl array.From the C level this is a pointer to an AV. This is a fixedvariant of T_AVREF that decrements the refcount appropriatelywhen returning an AV*. Introduced in perl 5.15.4.

  • T_HVREF

    From the perl level this is a reference to a perl hash.From the C level this is a pointer to an HV.

    Note that this typemap does not decrement the reference countwhen returning an HV*. See also: T_HVREF_REFCOUNT_FIXED

  • T_HVREF_REFCOUNT_FIXED

    From the perl level this is a reference to a perl hash.From the C level this is a pointer to an HV. This is a fixedvariant of T_HVREF that decrements the refcount appropriatelywhen returning an HV*. Introduced in perl 5.15.4.

  • T_CVREF

    From the perl level this is a reference to a perl subroutine(e.g. $sub = sub { 1 };). From the C level this is a pointerto a CV.

    Note that this typemap does not decrement the reference countwhen returning an HV*. See also: T_HVREF_REFCOUNT_FIXED

  • T_CVREF_REFCOUNT_FIXED

    From the perl level this is a reference to a perl subroutine(e.g. $sub = sub { 1 };). From the C level this is a pointerto a CV.

    This is a fixedvariant of T_HVREF that decrements the refcount appropriatelywhen returning an HV*. Introduced in perl 5.15.4.

  • T_SYSRET

    The T_SYSRET typemap is used to process return values from system calls.It is only meaningful when passing values from C to perl (there isno concept of passing a system return value from Perl to C).

    System calls return -1 on error (setting ERRNO with the reason)and (usually) 0 on success. If the return value is -1 this typemapreturns undef. If the return value is not -1, this typemaptranslates a 0 (perl false) to "0 but true" (whichis perl true) or returns the value itself, to indicate that thecommand succeeded.

    The POSIX module makes extensive use of this type.

  • T_UV

    An unsigned integer.

  • T_IV

    A signed integer. This is cast to the required integer type whenpassed to C and converted to an IV when passed back to Perl.

  • T_INT

    A signed integer. This typemap converts the Perl value to a nativeinteger type (the int type on the current platform). When returningthe value to perl it is processed in the same way as for T_IV.

    Its behaviour is identical to using an int type in XS with T_IV.

  • T_ENUM

    An enum value. Used to transfer an enum componentfrom C. There is no reason to pass an enum value to C sinceit is stored as an IV inside perl.

  • T_BOOL

    A boolean type. This can be used to pass true and false values to andfrom C.

  • T_U_INT

    This is for unsigned integers. It is equivalent to using T_UVbut explicitly casts the variable to type unsigned int.The default type for unsigned int is T_UV.

  • T_SHORT

    Short integers. This is equivalent to T_IV but explicitly caststhe return to type short. The default typemap for shortis T_IV.

  • T_U_SHORT

    Unsigned short integers. This is equivalent to T_UV but explicitlycasts the return to type unsigned short. The default typemap forunsigned short is T_UV.

    T_U_SHORT is used for type U16 in the standard typemap.

  • T_LONG

    Long integers. This is equivalent to T_IV but explicitly caststhe return to type long. The default typemap for longis T_IV.

  • T_U_LONG

    Unsigned long integers. This is equivalent to T_UV but explicitlycasts the return to type unsigned long. The default typemap forunsigned long is T_UV.

    T_U_LONG is used for type U32 in the standard typemap.

  • T_CHAR

    Single 8-bit characters.

  • T_U_CHAR

    An unsigned byte.

  • T_FLOAT

    A floating point number. This typemap guarantees to return a variablecast to a float.

  • T_NV

    A Perl floating point number. Similar to T_IV and T_UV in that thereturn type is cast to the requested numeric type rather thanto a specific type.

  • T_DOUBLE

    A double precision floating point number. This typemap guarantees toreturn a variable cast to a double.

  • T_PV

    A string (char *).

  • T_PTR

    A memory address (pointer). Typically associated with a void *type.

  • T_PTRREF

    Similar to T_PTR except that the pointer is stored in a scalar and thereference to that scalar is returned to the caller. This can be usedto hide the actual pointer value from the programmer since it is usuallynot required directly from within perl.

    The typemap checks that a scalar reference is passed from perl to XS.

  • T_PTROBJ

    Similar to T_PTRREF except that the reference is blessed into a class.This allows the pointer to be used as an object. Most commonly used todeal with C structs. The typemap checks that the perl object passedinto the XS routine is of the correct class (or part of a subclass).

    The pointer is blessed into a class that is derived from the nameof type of the pointer but with all '*' in the name replaced with'Ptr'.

  • T_REF_IV_REF

    NOT YET

  • T_REF_IV_PTR

    Similar to T_PTROBJ in that the pointer is blessed into a scalar object.The difference is that when the object is passed back into XS it must beof the correct type (inheritance is not supported).

    The pointer is blessed into a class that is derived from the nameof type of the pointer but with all '*' in the name replaced with'Ptr'.

  • T_PTRDESC

    NOT YET

  • T_REFREF

    Similar to T_PTRREF, except the pointer stored in the referenced scalaris dereferenced and copied to the output variable. This means thatT_REFREF is to T_PTRREF as T_OPAQUE is to T_OPAQUEPTR. All clear?

    Only the INPUT part of this is implemented (Perl to XSUB) and thereare no known users in core or on CPAN.

  • T_REFOBJ

    NOT YET

  • T_OPAQUEPTR

    This can be used to store bytes in the string component of theSV. Here the representation of the data is irrelevant to perl and thebytes themselves are just stored in the SV. It is assumed that the Cvariable is a pointer (the bytes are copied from that memorylocation). If the pointer is pointing to something that isrepresented by 8 bytes then those 8 bytes are stored in the SV (andlength() will report a value of 8). This entry is similar to T_OPAQUE.

    In principle the unpack() command can be used to convert the bytesback to a number (if the underlying type is known to be a number).

    This entry can be used to store a C structure (the numberof bytes to be copied is calculated using the C sizeof function)and can be used as an alternative to T_PTRREF without having to worryabout a memory leak (since Perl will clean up the SV).

  • T_OPAQUE

    This can be used to store data from non-pointer types in the stringpart of an SV. It is similar to T_OPAQUEPTR except that thetypemap retrieves the pointer directly rather than assuming itis being supplied. For example, if an integer is imported intoPerl using T_OPAQUE rather than T_IV the underlying bytes representingthe integer will be stored in the SV but the actual integer value willnot be available. i.e. The data is opaque to perl.

    The data may be retrieved using the unpack function if theunderlying type of the byte stream is known.

    T_OPAQUE supports input and output of simple types.T_OPAQUEPTR can be used to pass these bytes back into C if a pointeris acceptable.

  • Implicit array

    xsubpp supports a special syntax for returningpacked C arrays to perl. If the XS return type is given as

    1. array(type, nelem)

    xsubpp will copy the contents of nelem * sizeof(type) bytes fromRETVAL to an SV and push it onto the stack. This is only really usefulif the number of items to be returned is known at compile time and youdon't mind having a string of bytes in your SV. Use T_ARRAY to push avariable number of arguments onto the return stack (they won't bepacked as a single string though).

    This is similar to using T_OPAQUEPTR but can be used to process morethan one element.

  • T_PACKED

    Calls user-supplied functions for conversion. For OUTPUT(XSUB to Perl), a function named XS_pack_$ntype is calledwith the output Perl scalar and the C variable to convert from.$ntype is the normalized C type that is to be mapped toPerl. Normalized means that all * are replaced by thestring Ptr. The return value of the function is ignored.

    Conversely for INPUT (Perl to XSUB) mapping, thefunction named XS_unpack_$ntype is called with the input Perlscalar as argument and the return value is cast to the mappedC type and assigned to the output C variable.

    An example conversion function for a typemapped structfoo_t * might be:

    1. static void
    2. XS_pack_foo_tPtr(SV *out, foo_t *in)
    3. {
    4. dTHX; /* alas, signature does not include pTHX_ */
    5. HV* hash = newHV();
    6. hv_stores(hash, "int_member", newSViv(in->int_member));
    7. hv_stores(hash, "float_member", newSVnv(in->float_member));
    8. /* ... */
    9. /* mortalize as thy stack is not refcounted */
    10. sv_setsv(out, sv_2mortal(newRV_noinc((SV*)hash)));
    11. }

    The conversion from Perl to C is left as an exercise to the reader,but the prototype would be:

    1. static foo_t *
    2. XS_unpack_foo_tPtr(SV *in);

    Instead of an actual C function that has to fetch the thread contextusing dTHX, you can define macros of the same name and avoid theoverhead. Also, keep in mind to possibly free the memory allocated byXS_unpack_foo_tPtr.

  • T_PACKEDARRAY

    T_PACKEDARRAY is similar to T_PACKED. In fact, the INPUT (Perlto XSUB) typemap is indentical, but the OUTPUT typemap passesan additional argument to the XS_pack_$ntype function. Thisthird parameter indicates the number of elements in the outputso that the function can handle C arrays sanely. The variableneeds to be declared by the user and must have the namecount_$ntype where $ntype is the normalized C type nameas explained above. The signature of the function would be forthe example above and foo_t **:

    1. static void
    2. XS_pack_foo_tPtrPtr(SV *out, foo_t *in, UV count_foo_tPtrPtr);

    The type of the third parameter is arbitrary as far as the typemapis concerned. It just has to be in line with the declared variable.

    Of course, unless you know the number of elements in thesometype ** C array, within your XSUB, the return value fromfoo_t ** XS_unpack_foo_tPtrPtr(...) will be hard to decypher.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 forN+1 pointers and assign NULL to the (N+1)th to facilitateiteration.

    Alternatively, using a customized typemap for your purposes inthe first place is probably preferrable.

  • T_DATAUNIT

    NOT YET

  • T_CALLBACK

    NOT YET

  • T_ARRAY

    This is used to convert the perl argument list to a C arrayand for pushing the contents of a C array onto the perlargument stack.

    The usual calling signature is

    1. @out = array_func( @in );

    Any number of arguments can occur in the list before the array butthe input and output arrays must be the last elements in the list.

    When used to pass a perl list to C the XS writer must provide afunction (named after the array type but with 'Ptr' substituted for'*') to allocate the memory required to hold the list. A pointershould be returned. It is up to the XS writer to free the memory onexit from the function. The variable ix_$var is set to the numberof elements in the new array.

    When returning a C array to Perl the XS writer must provide an integervariable called size_$var containing the number of elements in thearray. This is used to determine how many elements should be pushedonto the return argument stack. This is not required on input sincePerl knows how many arguments are on the stack when the routine iscalled. Ordinarily this variable would be called size_RETVAL.

    Additionally, the type of each element is determined from the type ofthe array. If the array uses type intArray * xsubpp willautomatically work out that it contains variables of type int anduse that typemap entry to perform the copy of each element. Allpointer '*' and 'Array' tags are removed from the name to determinethe subtype.

  • T_STDIO

    This is used for passing perl filehandles to and from C usingFILE * structures.

  • T_INOUT

    This is used for passing perl filehandles to and from C usingPerlIO * structures. The file handle can used for reading andwriting. This corresponds to the +< mode, see also T_INand T_OUT.

    See perliol for more information on the Perl IO abstractionlayer. Perl must have been built with -Duseperlio.

    There is no check to assert that the filehandle passed from Perlto C was created with the right open() mode.

    Hint: The perlxstut tutorial covers the T_INOUT, T_IN, and T_OUTXS types nicely.

  • T_IN

    Same as T_INOUT, but the filehandle that is returned from C to Perlcan only be used for reading (mode <).

  • T_OUT

    Same as T_INOUT, but the filehandle that is returned from C to Perlis set to use the open mode +>.

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) XS language reference manualAn overview of the Perl interpreter (Berikutnya)