From: Paul Marquess Date: Mon, 29 Oct 2001 23:11:58 +0000 (+0000) Subject: RE: [PATCH h2xs] Use of static data in XS modules X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a129b8464589d5dad30757dd98425feafa1446f5;p=p5sagit%2Fp5-mst-13.2.git RE: [PATCH h2xs] Use of static data in XS modules Message-ID: Add compat.h. p4raw-id: //depot/perl@12780 --- diff --git a/lib/h2xs.t b/lib/h2xs.t index 912edae..0c5920e 100644 --- a/lib/h2xs.t +++ b/lib/h2xs.t @@ -48,6 +48,7 @@ Defaulting to backwards compatibility with perl $thisversion If you intend this module to be compatible with earlier perl versions, please specify a minimum perl version with the -b option. +Writing $name/compat.h Writing $name/$name.pm Writing $name/$name.xs Writing $name/fallback.c @@ -60,6 +61,7 @@ Writing $name/MANIFEST EOXSFILES "-f -n $name -b $thisversion", $], <<"EOXSFILES", +Writing $name/compat.h Writing $name/$name.pm Writing $name/$name.xs Writing $name/fallback.c @@ -72,6 +74,7 @@ Writing $name/MANIFEST EOXSFILES "-f -n $name -b 5.6.1", "5.006001", <<"EOXSFILES", +Writing $name/compat.h Writing $name/$name.pm Writing $name/$name.xs Writing $name/fallback.c @@ -84,6 +87,7 @@ Writing $name/MANIFEST EOXSFILES "-f -n $name -b 5.5.3", "5.00503", <<"EOXSFILES", +Writing $name/compat.h Writing $name/$name.pm Writing $name/$name.xs Writing $name/fallback.c @@ -105,6 +109,7 @@ Writing $name/MANIFEST EONOXSFILES "-f -n $name $header -b $thisversion", $], <<"EOXSFILES", +Writing $name/compat.h Writing $name/$name.pm Writing $name/$name.xs Writing $name/fallback.c diff --git a/utils/h2xs.PL b/utils/h2xs.PL index 6d6169b..b856d89 100644 --- a/utils/h2xs.PL +++ b/utils/h2xs.PL @@ -818,6 +818,139 @@ my %vdecl_hash; my @vdecls; if( ! $opt_X ){ # use XS, unless it was disabled + open(COMPAT, ">compat.h") || die "Can't create $ext$modpname/compat.h: $!\n"; + warn "Writing $ext$modpname/compat.h\n"; + print COMPAT <= perl5.004_68 */ +#define dMY_CXT_SV \ + SV *my_cxt_sv = *hv_fetch(PL_modglobal, MY_CXT_KEY, \ + sizeof(MY_CXT_KEY)-1, TRUE) +#endif /* < perl5.004_68 */ + +/* This declaration should be used within all functions that use the + * interpreter-local data. */ +#define dMY_CXT \ + dMY_CXT_SV; \ + my_cxt_t *my_cxtp = (my_cxt_t*)SvUV(my_cxt_sv) + +/* Creates and zeroes the per-interpreter data. + * (We allocate my_cxtp in a Perl SV so that it will be released when + * the interpreter goes away.) */ +#define MY_CXT_INIT \ + dMY_CXT_SV; \ + /* newSV() allocates one more than needed */ \ + my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1));\ + Zero(my_cxtp, 1, my_cxt_t); \ + sv_setuv(my_cxt_sv, (UV)my_cxtp) + +/* This macro must be used to access members of the my_cxt_t structure. + * e.g. MYCXT.some_data */ +#define MY_CXT (*my_cxtp) + +/* Judicious use of these macros can reduce the number of times dMY_CXT + * is used. Use is similar to pTHX, aTHX etc. */ +#define pMY_CXT my_cxt_t *my_cxtp +#define pMY_CXT_ pMY_CXT, +#define _pMY_CXT ,pMY_CXT +#define aMY_CXT my_cxtp +#define aMY_CXT_ aMY_CXT, +#define _aMY_CXT ,aMY_CXT + +#else /* single interpreter */ + +#ifndef NOOP +# define NOOP (void)0 +#endif + +#ifdef HASATTRIBUTE +# define PERL_UNUSED_DECL __attribute__((unused)) +#else +# define PERL_UNUSED_DECL +#endif + +#ifndef dNOOP +# define dNOOP extern int Perl___notused PERL_UNUSED_DECL +#endif + +#define START_MY_CXT static my_cxt_t my_cxt; +#define dMY_CXT_SV dNOOP +#define dMY_CXT dNOOP +#define MY_CXT_INIT NOOP +#define MY_CXT my_cxt + +#define pMY_CXT void +#define pMY_CXT_ +#define _pMY_CXT +#define aMY_CXT +#define aMY_CXT_ +#define _aMY_CXT + +#endif + +#endif /* perl < 5.7.2 */ + +/* End of file compat.h */ + +EOM + close COMPAT ; + open(XS, ">$modfname.xs") || die "Can't create $ext$modpname/$modfname.xs: $!\n"; if ($opt_x) { require Config; # Run-time directive @@ -1197,6 +1330,7 @@ print XS <<"END"; #include "EXTERN.h" #include "perl.h" #include "XSUB.h" +#include "compat.h" END if( @path_h ){