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

Hi, Still on the perlXS issue (damn, its complicated) This is my header file:
#include <TM_TranslationAPI.h> #include "TM_DefaultParams.h" // This data structure holds an array of address and the number of act +ual addresses typedef struct TM_TranslationAdresses{ u64 addressCount; u64 addresses[10*MAX_NUM_OF_ALIASES]; } TM_TranslationAdresses; TM_TranslationAdresses * findEAsForGivenRAAndContext( u64 raAddress, u64 lpid, u64 pid);
As you can see - contains only one structure and one function

This is my XS file (trans.xs):

#include "/usr/include/stdio.h" #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include "TM_TranslationsDB.h" MODULE = trans PACKAGE = trans PROTOTYPES: ENABLE TM_TranslationAdresses * findEAsForGivenRAAndContext(raAddress, lpid, pid) u64 raAddress u64 lpid u64 pid

this is the typemap I use:

TYPEMAP TM_TranslationAdresses * T_PTROBJ u64 T_U_LONG_LONG ################################################## INPUT T_U_LONG_LONG $var = (unsigned long long int)SvUV($arg) ################################################## OUTPUT T_U_LONG_LONG sv_setuv($arg, (UV)$var);

this is the trans.pm file I use:

package trans; use 5.010001; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( ); our $VERSION = '0.01'; require XSLoader; XSLoader::load('trans', $VERSION); 1;

and finally - this is the simple perl program I try to test everything:

#!/usr/bin/perl -w use lib '/gpfs/haifa-p4/08/tm/team/hagaih/perlWithC/trans/blib/arch/au +to/trans'; use lib '/gpfs/haifa-p4/08/tm/team/hagaih/perlWithC/trans/blib/lib'; use trans; use Math::BigInt; use Data::Dumper; my $lpid=0; my $pid=5; my $raAddress=Math::BigInt->new("0x00000000_01F560C0"); my $result=trans::findEAsForGivenRAAndContext($raAddress, $lpid, $pid) +; print $result->addressCount;

everything compiles great. I know the C code is working since I wrote a simple C program to verify it.

but when I run the perl program I get this error:

Can't locate object method "addressCount" via package "TM_TranslationA +dressesPtr" at test_translations.pl ...
Please ... will appreciate any help on the matter

Replies are listed 'Best First'.
Re: perlXS - missing object methods
by Corion (Patriarch) on Mar 28, 2016 at 08:01 UTC

    I don't know much about XS, but certainly it does not create accessor methods for you. It might be that you can access the addressCount property through $result->{addressCount} but as you get back a C struct, I doubt even that is the case.

    I think you will need to write XS functions that return you the addressCount and the addresses from the C struct to Perl.

Re: perlXS - missing object methods
by syphilis (Archbishop) on Mar 28, 2016 at 11:03 UTC
    There's a few things to puzzle over here:

    Similar to your earlier attempt, I don't see anything that offers instruction on what findEAsForGivenRAAndContext() actually does.
    I can see the data types of its arguments, and the data type that it returns, but that's about it.
    Perhaps this somehow accounts for the error message about a (non-existent) "TM_TranslationAdressesPtr" package - but I really don't know how that hitherto unmentioned package earns a mention.

    The u64 typemapping you've done implies that your perl's IV/UV is the same size as as a signed/unsigned long long int - so I wonder why such typemapping is necessary.
    Is it just so that you can have the "u64" symbol as an alias for unsigned long long int ?

    AFAICT, when $raAddress gets passed to findEAsForGivenRAAndContext(), instead of having assigned it as:
    my $raAddress=Math::BigInt->new("0x00000000_01F560C0"); it might just as well have been assigned as: my $raAddress=0x01F560C0;
    I would probably rewrite findEAsForGivenRAAndContext() so that it returns the 2 u64 values - such that instead of coding (in perl):
    my $result=trans::findEAsForGivenRAAndContext($raAddress, $lpid, $pid) +;
    one codes it as:
    my @result=trans::findEAsForGivenRAAndContext($raAddress, $lpid, $pid) +; print $result[0]; # addressCount
    But there are other ways, and perhaps I'm not looking at it right.

    Cheers,
    Rob
Re: perlXS - missing object methods
by Anonymous Monk on Mar 28, 2016 at 07:58 UTC
    addressCount doesn't appear in your xs file