Rafaël Garcia-Suarez > perl > ext/XS/Typemap/Typemap.xs

Download:
perl-5.10.0.tar.gz

Annotate this POD

Source   Latest Release: perl-5.8.9

TYPEMAPS ^

Each C type is represented by an entry in the typemap file that is responsible for converting perl variables (SV, AV, HV and CV) to and from that type.

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 wants to deal directly with the Perl variable.

T_SVREF

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

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.

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.

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 pointer to a CV.

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 is no 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 typemap returns undef. If the return value is not -1, this typemap translates a 0 (perl false) to "0 but true" (which is perl true) or returns the value itself, to indicate that the command 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 when passed 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 native integer type (the int type on the current platform). When returning the 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 component from C. There is no reason to pass an enum value to C since it is stored as an IV inside perl.

T_BOOL

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

T_U_INT

This is for unsigned integers. It is equivalent to using T_UV but 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 casts the return to type short. The default typemap for short is T_IV.

T_U_SHORT

Unsigned short integers. This is equivalent to T_UV but explicitly casts the return to type unsigned short. The default typemap for unsigned 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 casts the return to type long. The default typemap for long is T_IV.

T_U_LONG

Unsigned long integers. This is equivalent to T_UV but explicitly casts the return to type unsigned long. The default typemap for unsigned 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 variable cast to a float.

T_NV

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

T_DOUBLE

A double precision floating point number. This typemap guarantees to return 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 the reference to that scalar is returned to the caller. This can be used to hide the actual pointer value from the programmer since it is usually not 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 to deal with C structs. The typemap checks that the perl object passed into 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 name of 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 be of the correct type (inheritance is not supported).

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

T_PTRDESC

NOT YET

T_REFREF

NOT YET

T_REFOBJ

NOT YET

T_OPAQUEPTR

This can be used to store bytes in the string component of the SV. Here the representation of the data is irrelevant to perl and the bytes themselves are just stored in the SV. It is assumed that the C variable is a pointer (the bytes are copied from that memory location). If the pointer is pointing to something that is represented by 8 bytes then those 8 bytes are stored in the SV (and length() will report a value of 8). This entry is similar to T_OPAQUE.

In principal the unpack() command can be used to convert the bytes back to a number (if the underlying type is known to be a number).

This entry can be used to store a C structure (the number of bytes to be copied is calculated using the C sizeof function) and can be used as an alternative to T_PTRREF without having to worry about 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 string part of an SV. It is similar to T_OPAQUEPTR except that the typemap retrieves the pointer directly rather than assuming it is being supplied. For example if an integer is imported into Perl using T_OPAQUE rather than T_IV the underlying bytes representing the integer will be stored in the SV but the actual integer value will not be available. i.e. The data is opaque to perl.

The data may be retrieved using the unpack function if the underlying 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 pointer is acceptable.

Implicit array

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

  array(type, nelem)

xsubpp will copy the contents of nelem * sizeof(type) bytes from RETVAL to an SV and push it onto the stack. This is only really useful if the number of items to be returned is known at compile time and you don't mind having a string of bytes in your SV. Use T_ARRAY to push a variable number of arguments onto the return stack (they won't be packed as a single string though).

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

T_PACKED

NOT YET

T_PACKEDARRAY

NOT YET

T_DATAUNIT

NOT YET

T_CALLBACK

NOT YET

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.

The usual calling signature is

  @out = array_func( @in );

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.

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 ix_$var is set to the number of elements in the new array.

When returning a C array to Perl the XS writer must provide an integer variable called size_$var 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 size_RETVAL.

Additionally, the type of each element is determined from the type of the array. If the array uses type intArray * xsubpp will automatically work out that it contains variables of type int 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.

T_STDIO

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

T_IN

NOT YET

T_INOUT

This is used for passing perl filehandles to and from C using PerlIO * structures. The file handle can used for reading and writing.

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

T_OUT

NOT YET

syntax highlighting: