http://www.perlmonks.org?node_id=35187

pope has asked for the wisdom of the Perl Monks concerning the following question:

Dean Roehrich's CookBookB comes with a great example (Struct3) demonstrating how to make a C struct into a perl hash, and vice versa. But the structure in the example has fixed size elements:
typedef struct ab_input { long lTrackId; char szDescription[80]; } AB_IN;
I need to do a similar task, but with several pointers to char, instead of a fixed size array of char, because the size is unknown in advance:
typedef struct { /* char fn[256]; */ char *fn; /* char *desc; */ long size; } cstype;
I also have made the necessary lines for dynamic allocation in XS_unpack_cstypePtr(). Here is the RFile.xs:
#ifdef __cplusplus extern "C" { #endif #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #ifdef __cplusplus } #endif typedef struct { /* char fn[256]; */ char *fn; /* char *desc; */ long size; } cstype; extern char* _open_read(char *fn); extern char* _open_read_st(cstype *stru); /* Will convert a Perl HV* to a C cstype* */ cstype * XS_unpack_cstypePtr( rv ) SV *rv; { HV *hv; SV **ssv; cstype *abin; char *temp; STRLEN len; if( SvROK( rv ) && (SvTYPE(SvRV(rv)) == SVt_PVHV) ) hv = (HV*)SvRV(rv); else{ warn("XS_unpack_cstypePtr: rv was not an HV ref"); return( (cstype*)NULL ); } abin = (cstype*)safemalloc( sizeof( cstype ) ); if( abin == NULL ){ warn("XS_unpack_cstypePtr: unable to malloc cstype"); return( (cstype*)NULL); } abin->fn[0] = '\0'; /* abin->desc[0] = '\0'; */ abin->size = 0; ssv = hv_fetch( hv, "size", 4, 0 ); if( ssv != NULL ){ if( SvIOK( *ssv ) ) abin->size = (long)SvIV( *ssv ); else{ warn("XS_unpack_cstypePtr: hash elem size was not IOK"); } } else warn("XS_unpack_cstypePtr: hash elem size was null"); ssv = hv_fetch( hv, "fn", 2, 0 ); if( ssv != NULL ){ if( SvPOK( *ssv ) ) { temp = SvPV(*ssv, len); if ((abin->fn = (char*) safemalloc((len + 1) * sizeof(char))) == NULL) return (cstype*)NULL; strcpy( abin->fn, temp ); } else{ warn("XS_unpack_cstypePtr: hash elem fn was not POK"); } } else warn("XS_unpack_cstypePtr: hash elem fn was null"); /* ssv = hv_fetch( hv, "desc", 4, 0 ); if( ssv != NULL ){ if( SvPOK( *ssv ) ) { temp = SvPV(*ssv, len); if ((abin->desc = (char*) safemalloc((len + 1) * sizeof(char))) == NULL) return (cstype*)NULL; strcpy( abin->desc, temp ); } else{ warn("XS_unpack_cstypePtr: hash elem desc was not POK"); } } else warn("XS_unpack_cstypePtr: hash elem desc was null"); */ return( abin ); } MODULE = RFile PACKAGE = RFile PROTOTYPES: DISABLE SV* read_file(fn) char *fn PREINIT: char *content; CODE: content = _open_read(fn); if (content == NULL) XSRETURN_UNDEF; RETVAL = newSVpv(content, 0); OUTPUT: RETVAL SV* read_st(stru) cstype *stru CODE: printf("# Hello, string=(%s)\n", stru->fn ); RETVAL = newSVpv(stru->fn, 0); OUTPUT: RETVAL
RFile.pm:
package RFile; use strict; use Carp; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); require Exporter; require DynaLoader; require AutoLoader; @ISA = qw(Exporter DynaLoader); $VERSION = '0.01'; bootstrap RFile $VERSION; sub new { my $class = shift; my $self = { m => [ { fn => 'Makefile.PL', size => 100, desc => 'test', }, { fn => 'typemap', size => 100, desc => 'test', }, { fn => 'RFile.c', size => 100, desc => 'test', }, ], }; bless $self, $class; } sub dump { my ($self, $idx) = @_; return RFile::read_st($self->{m}->[$idx]); } 1;
But this gives coredump:
my $n = new RFile; for (0..2) { $c = $n->dump($_); print $c, "\n"; }
I must be missing something important in the XS_unpack function which is necessary when dealing with dynamically allocated structure element. Could someone help me on this?