This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
threads is no longer customized, as of commit c0ff91434b
[perl5.git] / ext / XS-Typemap / Typemap.xs
index 9684f50..3fa0e74 100644 (file)
@@ -25,10 +25,15 @@ typedef int intRef; /* T_PTRREF */
 typedef int intObj; /* T_PTROBJ */
 typedef int intRefIv; /* T_REF_IV_PTR */
 typedef int intArray; /* T_ARRAY */
+typedef int intTINT; /* T_INT */
+typedef int intTLONG; /* T_LONG */
 typedef short shortOPQ;   /* T_OPAQUE */
 typedef int intOpq;   /* T_OPAQUEPTR */
+typedef unsigned intUnsigned; /* T_U_INT */
+typedef PerlIO * inputfh; /* T_IN */
+typedef PerlIO * outputfh; /* T_OUT */
 
-/* A structure to test T_OPAQUEPTR */
+/* A structure to test T_OPAQUEPTR and T_PACKED */
 struct t_opaqueptr {
   int a;
   int b;
@@ -36,6 +41,7 @@ struct t_opaqueptr {
 };
 
 typedef struct t_opaqueptr astruct;
+typedef struct t_opaqueptr anotherstruct;
 
 /* Some static memory for the tests */
 static I32 xst_anint;
@@ -60,26 +66,190 @@ intArray * intArrayPtr( int nelem ) {
     return array;
 }
 
+/* test T_PACKED */
+STATIC void
+XS_pack_anotherstructPtr(SV *out, anotherstruct *in)
+{
+    dTHX;
+    HV *hash = newHV();
+    if (NULL == hv_stores(hash, "a", newSViv(in->a)))
+      croak("Failed to store data in hash");
+    if (NULL == hv_stores(hash, "b", newSViv(in->b)))
+      croak("Failed to store data in hash");
+    if (NULL == hv_stores(hash, "c", newSVnv(in->c)))
+      croak("Failed to store data in hash");
+    sv_setsv(out, sv_2mortal(newRV_noinc((SV*)hash)));
+}
 
-MODULE = XS::Typemap   PACKAGE = XS::Typemap
+STATIC anotherstruct *
+XS_unpack_anotherstructPtr(SV *in)
+{
+    dTHX; /* rats, this is expensive */
+    /* this is similar to T_HVREF since we chose to use a hash */
+    HV *inhash;
+    SV **elem;
+    anotherstruct *out;
+    SV *const tmp = in;
+    SvGETMAGIC(tmp);
+    if (SvROK(tmp) && SvTYPE(SvRV(tmp)) == SVt_PVHV)
+       inhash = (HV*)SvRV(tmp);
+    else
+        Perl_croak(aTHX_ "Argument is not a HASH reference");
+
+    /* FIXME dunno if supposed to use perl mallocs here */
+    Newxz(out, 1, anotherstruct);
+
+    elem = hv_fetchs(inhash, "a", 0);
+    if (elem == NULL)
+      Perl_croak(aTHX_ "Shouldn't happen: hv_fetchs returns NULL");
+    out->a = SvIV(*elem);
+
+    elem = hv_fetchs(inhash, "b", 0);
+    if (elem == NULL)
+      Perl_croak(aTHX_ "Shouldn't happen: hv_fetchs returns NULL");
+    out->b = SvIV(*elem);
+
+    elem = hv_fetchs(inhash, "c", 0);
+    if (elem == NULL)
+      Perl_croak(aTHX_ "Shouldn't happen: hv_fetchs returns NULL");
+    out->c = SvNV(*elem);
+
+    return out;
+}
 
-PROTOTYPES: DISABLE
+/* test T_PACKEDARRAY */
+STATIC void
+XS_pack_anotherstructPtrPtr(SV *out, anotherstruct **in, UV cnt)
+{
+    dTHX;
+    UV i;
+    AV *ary = newAV();
+    for (i = 0; i < cnt; ++i) {
+        HV *hash = newHV();
+        if (NULL == hv_stores(hash, "a", newSViv(in[i]->a)))
+          croak("Failed to store data in hash");
+        if (NULL == hv_stores(hash, "b", newSViv(in[i]->b)))
+          croak("Failed to store data in hash");
+        if (NULL == hv_stores(hash, "c", newSVnv(in[i]->c)))
+          croak("Failed to store data in hash");
+        av_push(ary, newRV_noinc((SV*)hash));
+    }
+    sv_setsv(out, sv_2mortal(newRV_noinc((SV*)ary)));
+}
 
-=head1 TYPEMAPS
+STATIC anotherstruct **
+XS_unpack_anotherstructPtrPtr(SV *in)
+{
+    dTHX; /* rats, this is expensive */
+    /* this is similar to T_HVREF since we chose to use a hash */
+    HV *inhash;
+    AV *inary;
+    SV **elem;
+    anotherstruct **out;
+    UV nitems, i;
+    SV *tmp;
+
+    /* safely deref the input array ref */
+    tmp = in;
+    SvGETMAGIC(tmp);
+    if (SvROK(tmp) && SvTYPE(SvRV(tmp)) == SVt_PVAV)
+        inary = (AV*)SvRV(tmp);
+    else
+        Perl_croak(aTHX_ "Argument is not an ARRAY reference");
+
+    nitems = av_tindex(inary) + 1;
+
+    /* FIXME dunno if supposed to use perl mallocs here */
+    /* N+1 elements so we know the last one is NULL */
+    Newxz(out, nitems+1, anotherstruct*);
+
+    /* WARNING: in real code, we'd have to Safefree() on exception, but
+     *          since we're testing perl, if we croak() here, stuff is
+     *          rotten anyway! */
+    for (i = 0; i < nitems; ++i) {
+        Newxz(out[i], 1, anotherstruct);
+        elem = av_fetch(inary, i, 0);
+        if (elem == NULL)
+            Perl_croak(aTHX_ "Shouldn't happen: av_fetch returns NULL");
+        tmp = *elem;
+        SvGETMAGIC(tmp);
+        if (SvROK(tmp) && SvTYPE(SvRV(tmp)) == SVt_PVHV)
+            inhash = (HV*)SvRV(tmp);
+        else
+            Perl_croak(aTHX_ "Array element %"UVuf" is not a HASH reference", i);
+
+        elem = hv_fetchs(inhash, "a", 0);
+        if (elem == NULL)
+            Perl_croak(aTHX_ "Shouldn't happen: hv_fetchs returns NULL");
+        out[i]->a = SvIV(*elem);
+
+        elem = hv_fetchs(inhash, "b", 0);
+        if (elem == NULL)
+            Perl_croak(aTHX_ "Shouldn't happen: hv_fetchs returns NULL");
+        out[i]->b = SvIV(*elem);
+
+        elem = hv_fetchs(inhash, "c", 0);
+        if (elem == NULL)
+            Perl_croak(aTHX_ "Shouldn't happen: hv_fetchs returns NULL");
+        out[i]->c = SvNV(*elem);
+    }
+
+    return out;
+}
 
-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.
+/* no special meaning as far as typemaps are concerned,
+ * just for convenience */
+void
+XS_release_anotherstructPtrPtr(anotherstruct **in)
+{
+    unsigned int i = 0;
+    while (in[i] != NULL)
+        Safefree(in[i++]);
+    Safefree(in);
+}
 
-=over 4
 
-=item T_SV
+MODULE = XS::Typemap   PACKAGE = XS::Typemap
 
-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.
+PROTOTYPES: DISABLE
 
-=cut
+TYPEMAP: <<END_OF_TYPEMAP
+
+# Typemap file for typemap testing
+# includes bonus typemap entries
+# Mainly so that all the standard typemaps can be exercised even when
+# there is not a corresponding type explicitly identified in the standard
+# typemap
+
+svtype           T_ENUM
+intRef *         T_PTRREF
+intRef           T_IV
+intObj *         T_PTROBJ
+intObj           T_IV
+intRefIv *       T_REF_IV_PTR
+intRefIv         T_IV
+intArray *       T_ARRAY
+intOpq           T_IV
+intOpq   *       T_OPAQUEPTR
+intUnsigned      T_U_INT
+intTINT          T_INT
+intTLONG         T_LONG
+shortOPQ         T_OPAQUE
+shortOPQ *       T_OPAQUEPTR
+astruct *        T_OPAQUEPTR
+anotherstruct *  T_PACKED
+anotherstruct ** T_PACKEDARRAY
+AV_FIXED *      T_AVREF_REFCOUNT_FIXED
+HV_FIXED *      T_HVREF_REFCOUNT_FIXED
+CV_FIXED *      T_CVREF_REFCOUNT_FIXED
+SVREF_FIXED     T_SVREF_REFCOUNT_FIXED
+inputfh          T_IN
+outputfh         T_OUT
+
+END_OF_TYPEMAP
+
+
+## T_SV
 
 SV *
 T_SV( sv )
@@ -96,15 +266,8 @@ T_SV( sv )
  OUTPUT:
   RETVAL
 
-=item T_SVREF
-
-Used to pass in and return a reference to an SV.
-
-Note that this typemap does not decrement the reference count
-when returning the reference to an SV*.
-See also: T_SVREF_REFCOUNT_FIXED
 
-=cut
+## T_SVREF
 
 SVREF
 T_SVREF( svref )
@@ -114,14 +277,8 @@ T_SVREF( svref )
  OUTPUT:
   RETVAL
 
-=item T_SVREF_FIXED
 
-Used to pass in and return a reference to an SV.
-This is a fixed
-variant of T_SVREF that decrements the refcount appropriately
-when returning a reference to an SV*. Introduced in perl 5.15.4.
-
-=cut
+## T_SVREF_FIXED
 
 SVREF_FIXED
 T_SVREF_REFCOUNT_FIXED( svref )
@@ -132,15 +289,8 @@ T_SVREF_REFCOUNT_FIXED( svref )
  OUTPUT:
   RETVAL
 
-=item 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 count
-when returning an AV*. See also: T_AVREF_REFCOUNT_FIXED
 
-=cut
+## T_AVREF
 
 AV *
 T_AVREF( av )
@@ -150,14 +300,8 @@ T_AVREF( av )
  OUTPUT:
   RETVAL
 
-=item 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 fixed
-variant of T_AVREF that decrements the refcount appropriately
-when returning an AV*. Introduced in perl 5.15.4.
-
-=cut
+## T_AVREF_REFCOUNT_FIXED
 
 AV_FIXED*
 T_AVREF_REFCOUNT_FIXED( av )
@@ -168,15 +312,8 @@ T_AVREF_REFCOUNT_FIXED( av )
  OUTPUT:
   RETVAL
 
-=item 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 count
-when returning an HV*. See also: T_HVREF_REFCOUNT_FIXED
-
-=cut
+## T_HVREF
 
 HV *
 T_HVREF( hv )
@@ -186,14 +323,8 @@ T_HVREF( hv )
  OUTPUT:
   RETVAL
 
-=item 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 fixed
-variant of T_HVREF that decrements the refcount appropriately
-when returning an HV*. Introduced in perl 5.15.4.
 
-=cut
+## T_HVREF_REFCOUNT_FIXED
 
 HV_FIXED*
 T_HVREF_REFCOUNT_FIXED( hv )
@@ -205,16 +336,7 @@ T_HVREF_REFCOUNT_FIXED( hv )
   RETVAL
 
 
-=item 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.
-
-Note that this typemap does not decrement the reference count
-when returning an HV*. See also: T_HVREF_REFCOUNT_FIXED
-
-=cut
+## T_CVREF
 
 CV *
 T_CVREF( cv )
@@ -224,17 +346,8 @@ T_CVREF( cv )
  OUTPUT:
   RETVAL
 
-=item 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 pointer
-to a CV.
 
-This is a fixed
-variant of T_HVREF that decrements the refcount appropriately
-when returning an HV*. Introduced in perl 5.15.4.
-
-=cut
+## T_CVREF_REFCOUNT_FIXED
 
 CV_FIXED *
 T_CVREF_REFCOUNT_FIXED( cv )
@@ -245,22 +358,8 @@ T_CVREF_REFCOUNT_FIXED( cv )
  OUTPUT:
   RETVAL
 
-=item 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 C<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 L<POSIX|POSIX> module makes extensive use of this type.
 
-=cut
+## T_SYSRET
 
 # Test a successful return
 
@@ -280,11 +379,7 @@ T_SYSRET_fail()
  OUTPUT:
   RETVAL
 
-=item T_UV
-
-An unsigned integer.
-
-=cut
+## T_UV
 
 unsigned int
 T_UV( uv )
@@ -294,12 +389,8 @@ T_UV( uv )
  OUTPUT:
   RETVAL
 
-=item 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.
 
-=cut
+## T_IV
 
 long
 T_IV( iv )
@@ -309,21 +400,19 @@ T_IV( iv )
  OUTPUT:
   RETVAL
 
-=item T_INT
 
-A signed integer. This typemap converts the Perl value to a native
-integer type (the C<int> type on the current platform). When returning
-the value to perl it is processed in the same way as for T_IV.
+## T_INT
 
-Its behaviour is identical to using an C<int> type in XS with T_IV.
-
-=item T_ENUM
+intTINT
+T_INT( i )
+  intTINT i
+ CODE:
+  RETVAL = i;
+ OUTPUT:
+  RETVAL
 
-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.
 
-=cut
+## T_ENUM
 
 # The test should return the value for SVt_PVHV.
 # 11 at the present time but we can't not rely on this
@@ -336,12 +425,8 @@ T_ENUM()
  OUTPUT:
   RETVAL
 
-=item T_BOOL
 
-A boolean type. This can be used to pass true and false values to and
-from C.
-
-=cut
+## T_BOOL
 
 bool
 T_BOOL( in )
@@ -351,27 +436,46 @@ T_BOOL( in )
  OUTPUT:
   RETVAL
 
-=item T_U_INT
+bool
+T_BOOL_2( in )
+  bool in
+ CODE:
+    PERL_UNUSED_VAR(RETVAL);
+ OUTPUT:
+   in
 
-This is for unsigned integers. It is equivalent to using T_UV
-but explicitly casts the variable to type C<unsigned int>.
-The default type for C<unsigned int> is T_UV.
+void
+T_BOOL_OUT( out, in )
+  bool out
+  bool in
+ CODE:
+ out = in;
+ OUTPUT:
+   out
 
-=item T_SHORT
+## T_U_INT
 
-Short integers. This is equivalent to T_IV but explicitly casts
-the return to type C<short>. The default typemap for C<short>
-is T_IV.
+intUnsigned
+T_U_INT( uint )
+  intUnsigned uint
+ CODE:
+  RETVAL = uint;
+ OUTPUT:
+  RETVAL
 
-=item T_U_SHORT
 
-Unsigned short integers. This is equivalent to T_UV but explicitly
-casts the return to type C<unsigned short>. The default typemap for
-C<unsigned short> is T_UV.
+## T_SHORT
+
+short
+T_SHORT( s )
+  short s
+ CODE:
+  RETVAL = s;
+ OUTPUT:
+  RETVAL
 
-T_U_SHORT is used for type C<U16> in the standard typemap.
 
-=cut
+## T_U_SHORT
 
 U16
 T_U_SHORT( in )
@@ -382,21 +486,17 @@ T_U_SHORT( in )
   RETVAL
 
 
-=item T_LONG
-
-Long integers. This is equivalent to T_IV but explicitly casts
-the return to type C<long>. The default typemap for C<long>
-is T_IV.
+## T_LONG
 
-=item T_U_LONG
-
-Unsigned long integers. This is equivalent to T_UV but explicitly
-casts the return to type C<unsigned long>. The default typemap for
-C<unsigned long> is T_UV.
-
-T_U_LONG is used for type C<U32> in the standard typemap.
+intTLONG
+T_LONG( in )
+  intTLONG in
+ CODE:
+  RETVAL = in;
+ OUTPUT:
+  RETVAL
 
-=cut
+## T_U_LONG
 
 U32
 T_U_LONG( in )
@@ -406,11 +506,8 @@ T_U_LONG( in )
  OUTPUT:
   RETVAL
 
-=item T_CHAR
-
-Single 8-bit characters.
 
-=cut
+## T_CHAR
 
 char
 T_CHAR( in );
@@ -421,11 +518,7 @@ T_CHAR( in );
   RETVAL
 
 
-=item T_U_CHAR
-
-An unsigned byte.
-
-=cut
+## T_U_CHAR
 
 unsigned char
 T_U_CHAR( in );
@@ -436,12 +529,7 @@ T_U_CHAR( in );
   RETVAL
 
 
-=item T_FLOAT
-
-A floating point number. This typemap guarantees to return a variable
-cast to a C<float>.
-
-=cut
+## T_FLOAT
 
 float
 T_FLOAT( in )
@@ -451,13 +539,8 @@ T_FLOAT( in )
  OUTPUT:
   RETVAL
 
-=item 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.
-
-=cut
+## T_NV
 
 NV
 T_NV( in )
@@ -467,12 +550,8 @@ T_NV( in )
  OUTPUT:
   RETVAL
 
-=item T_DOUBLE
-
-A double precision floating point number. This typemap guarantees to
-return a variable cast to a C<double>.
 
-=cut
+## T_DOUBLE
 
 double
 T_DOUBLE( in )
@@ -482,11 +561,8 @@ T_DOUBLE( in )
  OUTPUT:
   RETVAL
 
-=item T_PV
 
-A string (char *).
-
-=cut
+## T_PV
 
 char *
 T_PV( in )
@@ -496,12 +572,15 @@ T_PV( in )
  OUTPUT:
   RETVAL
 
-=item T_PTR
+char *
+T_PV_null()
+ CODE:
+  RETVAL = NULL;
+ OUTPUT:
+  RETVAL
 
-A memory address (pointer). Typically associated with a C<void *>
-type.
 
-=cut
+## T_PTR
 
 # Pass in a value. Store the value in some static memory and
 # then return the pointer
@@ -525,16 +604,8 @@ T_PTR_IN( ptr )
  OUTPUT:
   RETVAL
 
-=item 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.
-
-=cut
+## T_PTRREF
 
 # Similar test to T_PTR
 # Pass in a value. Store the value in some static memory and
@@ -560,19 +631,7 @@ T_PTRREF_IN( ptr )
   RETVAL
 
 
-
-=item 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'.
-
-=cut
+## T_PTROBJ
 
 # Similar test to T_PTRREF
 # Pass in a value. Store the value in some static memory and
@@ -601,21 +660,12 @@ T_PTROBJ_IN( ptr )
 
 MODULE = XS::Typemap PACKAGE = XS::Typemap
 
-=item T_REF_IV_REF
-
-NOT YET
-
-=item 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).
+## T_REF_IV_REF
+## NOT YET
 
-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'.
 
-=cut
+## T_REF_IV_PTR
 
 # Similar test to T_PTROBJ
 # Pass in a value. Store the value in some static memory and
@@ -645,37 +695,19 @@ T_REF_IV_PTR_IN( ptr )
 
 MODULE = XS::Typemap PACKAGE = XS::Typemap
 
-=item T_PTRDESC
-
-NOT YET
-
-=item T_REFREF
-
-NOT YET
+## T_PTRDESC
+## NOT YET
 
-=item T_REFOBJ
 
-NOT YET
+## T_REFREF
+## NOT YET
 
-=item 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.
+## T_REFOBJ
+## NOT YET
 
-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 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).
-
-=cut
+## T_OPAQUEPTR
 
 intOpq *
 T_OPAQUEPTR_IN( val )
@@ -727,24 +759,7 @@ T_OPAQUEPTR_OUT_struct( test )
   XPUSHs(sv_2mortal(newSVnv(test->c)));
 
 
-=item 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 C<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.
-
-=cut
+## T_OPAQUE
 
 shortOPQ
 T_OPAQUE_IN( val )
@@ -762,25 +777,6 @@ T_OPAQUE_OUT( val )
  OUTPUT:
   RETVAL
 
-=item 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 C<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.
-
-=cut
-
 array(int,3)
 T_OPAQUE_array( a,b,c)
   int a
@@ -797,57 +793,79 @@ T_OPAQUE_array( a,b,c)
   RETVAL
 
 
-=item T_PACKED
-
-NOT YET
-
-=item T_PACKEDARRAY
-
-NOT YET
-
-=item T_DATAUNIT
+## T_PACKED
 
-NOT YET
-
-=item T_CALLBACK
-
-NOT YET
-
-=item T_ARRAY
+void
+T_PACKED_in(in)
+  anotherstruct *in;
+ PPCODE:
+  mXPUSHi(in->a);
+  mXPUSHi(in->b);
+  mXPUSHn(in->c);
+  Safefree(in);
+  XSRETURN(3);
+
+anotherstruct *
+T_PACKED_out(a, b ,c)
+  int a;
+  int b;
+  double c;
+ CODE:
+  Newxz(RETVAL, 1, anotherstruct);
+  RETVAL->a = a;
+  RETVAL->b = b;
+  RETVAL->c = c;
+ OUTPUT: RETVAL
+ CLEANUP:
+  Safefree(RETVAL);
 
-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.
+## T_PACKEDARRAY
 
-The usual calling signature is
+void
+T_PACKEDARRAY_in(in)
+  anotherstruct **in;
+ PREINIT:
+  unsigned int i = 0;
+ PPCODE:
+  while (in[i] != NULL) {
+    mXPUSHi(in[i]->a);
+    mXPUSHi(in[i]->b);
+    mXPUSHn(in[i]->c);
+    ++i;
+  }
+  XS_release_anotherstructPtrPtr(in);
+  XSRETURN(3*i);
+
+anotherstruct **
+T_PACKEDARRAY_out(...)
+ PREINIT:
+  unsigned int i, nstructs, count_anotherstructPtrPtr;
+ CODE:
+  if ((items % 3) != 0)
+    croak("Need nitems divisible by 3");
+  nstructs = (unsigned int)(items / 3);
+  count_anotherstructPtrPtr = nstructs;
+  Newxz(RETVAL, nstructs+1, anotherstruct *);
+  for (i = 0; i < nstructs; ++i) {
+    Newxz(RETVAL[i], 1, anotherstruct);
+    RETVAL[i]->a = SvIV(ST(3*i));
+    RETVAL[i]->b = SvIV(ST(3*i+1));
+    RETVAL[i]->c = SvNV(ST(3*i+2));
+  }
+ OUTPUT: RETVAL
+ CLEANUP:
+  XS_release_anotherstructPtrPtr(RETVAL);
 
-  @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.
+## T_DATAUNIT
+## NOT YET
 
-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 C<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 C<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 C<size_RETVAL>.
+## T_CALLBACK
+## NOT YET
 
-Additionally, the type of each element is determined from the type of
-the array. If the array uses type C<intArray *> xsubpp will
-automatically work out that it contains variables of type C<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.
 
-=cut
+## T_ARRAY
 
 # Test passes in an integer array and returns it along with
 # the number of elements
@@ -857,6 +875,9 @@ the subtype.
 # using PPCODE. This means that only the first element
 # is returned. KLUGE this by using CLEANUP to return before the
 # end.
+# Note: I read this as: The "T_ARRAY" typemap is really rather broken,
+#       at least for OUTPUT. That is apart from the general design
+#       weaknesses. --Steffen
 
 intArray *
 T_ARRAY( dummy, array, ... )
@@ -875,12 +896,7 @@ T_ARRAY( dummy, array, ... )
   XSRETURN(size_RETVAL);
 
 
-=item T_STDIO
-
-This is used for passing perl filehandles to and from C using
-C<FILE *> structures.
-
-=cut
+## T_STDIO
 
 FILE *
 T_STDIO_open( file )
@@ -916,24 +932,32 @@ T_STDIO_print( stream, string )
   RETVAL
 
 
-=item T_IN
-
-NOT YET
+## T_INOUT
 
-=item T_INOUT
+PerlIO *
+T_INOUT(in)
+  PerlIO *in;
+ CODE:
+  RETVAL = in; /* silly test but better than nothing */
+ OUTPUT: RETVAL
 
-This is used for passing perl filehandles to and from C using
-C<PerlIO *> structures. The file handle can used for reading and
-writing.
 
-See L<perliol> for more information on the Perl IO abstraction
-layer. Perl must have been built with C<-Duseperlio>.
+## T_IN
 
-=item T_OUT
+inputfh
+T_IN(in)
+  inputfh in;
+ CODE:
+  RETVAL = in; /* silly test but better than nothing */
+ OUTPUT: RETVAL
 
-NOT YET
 
-=back
+## T_OUT
 
-=cut
+outputfh
+T_OUT(in)
+  outputfh in;
+ CODE:
+  RETVAL = in; /* silly test but better than nothing */
+ OUTPUT: RETVAL