Change 30064 by [EMAIL PROTECTED] on 2007/01/29 18:28:16

        Add av_create_and_push() and av_create_and_unshift_one() to refactor
        out two repeated idioms.

Affected files ...

... //depot/perl/av.c#116 edit
... //depot/perl/doio.c#354 edit
... //depot/perl/embed.fnc#459 edit
... //depot/perl/op.c#884 edit
... //depot/perl/perl.c#783 edit
... //depot/perl/pod/perlapi.pod#282 edit
... //depot/perl/proto.h#797 edit

Differences ...

==== //depot/perl/av.c#116 (text) ====
Index: perl/av.c
--- perl/av.c#115~29221~        2006-11-06 14:21:05.000000000 -0800
+++ perl/av.c   2007-01-29 10:28:16.000000000 -0800
@@ -487,6 +487,24 @@
 }
 
 /*
+
+=for apidoc av_create_and_push
+
+Push an SV onto the end of the array, creating the array if necessary.
+A small internal helper function to remove a commonly duplicated idiom.
+
+=cut
+*/
+
+void
+Perl_av_create_and_push(pTHX_ AV **const avp, SV *const val)
+{
+    if (!*avp)
+       *avp = newAV();
+    av_push(*avp, val);
+}
+
+/*
 =for apidoc av_push
 
 Pushes an SV onto the end of the array.  The array will grow automatically
@@ -568,6 +586,26 @@
 }
 
 /*
+
+=for apidoc av_create_and_unshift_one
+
+Unshifts an SV onto the beginning of the array, creating the array if
+necessary.
+A small internal helper function to remove a commonly duplicated idiom.
+
+=cut
+*/
+
+SV **
+Perl_av_create_and_unshift_one(pTHX_ AV **const avp, SV *const val)
+{
+    if (!*avp)
+       *avp = newAV();
+    av_unshift(*avp, 1);
+    return av_store(*avp, 0, val);
+}
+
+/*
 =for apidoc av_unshift
 
 Unshift the given number of C<undef> values onto the beginning of the

==== //depot/perl/doio.c#354 (text) ====
Index: perl/doio.c
--- perl/doio.c#353~29696~      2007-01-05 02:47:58.000000000 -0800
+++ perl/doio.c 2007-01-29 10:28:16.000000000 -0800
@@ -710,10 +710,9 @@
     if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
        IoFLAGS(io) &= ~IOf_START;
        if (PL_inplace) {
-           if (!PL_argvout_stack)
-               PL_argvout_stack = newAV();
            assert(PL_defoutgv);
-           av_push(PL_argvout_stack, SvREFCNT_inc_simple_NN(PL_defoutgv));
+           Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
+                                   SvREFCNT_inc_simple_NN(PL_defoutgv));
        }
     }
     if (PL_filemode & (S_ISUID|S_ISGID)) {

==== //depot/perl/embed.fnc#459 (text) ====
Index: perl/embed.fnc
--- perl/embed.fnc#458~30034~   2007-01-27 09:03:59.000000000 -0800
+++ perl/embed.fnc      2007-01-29 10:28:16.000000000 -0800
@@ -106,11 +106,13 @@
 ApdR   |I32    |av_len         |NN const AV* ar
 ApdR   |AV*    |av_make        |I32 size|NN SV** svp
 Apd    |SV*    |av_pop         |NN AV* ar
+ApdoxM |void   |av_create_and_push|NN AV **const avp|NN SV *const val
 Apd    |void   |av_push        |NN AV* ar|NN SV* val
 p      |void   |av_reify       |NN AV* ar
 ApdR   |SV*    |av_shift       |NN AV* ar
 Apd    |SV**   |av_store       |NN AV* ar|I32 key|NULLOK SV* val
 Apd    |void   |av_undef       |NN AV* ar
+ApdoxM |SV**   |av_create_and_unshift_one|NN AV **const avp|NN SV *const val
 Apd    |void   |av_unshift     |NN AV* ar|I32 num
 Apo    |SV**   |av_arylen_p    |NN AV* av
 pR     |OP*    |bind_match     |I32 type|NN OP* left|NN OP* pat

==== //depot/perl/op.c#884 (text) ====
Index: perl/op.c
--- perl/op.c#883~30043~        2007-01-27 14:48:39.000000000 -0800
+++ perl/op.c   2007-01-29 10:28:16.000000000 -0800
@@ -5410,10 +5410,8 @@
            SAVECOPFILE(&PL_compiling);
            SAVECOPLINE(&PL_compiling);
 
-           if (!PL_beginav)
-               PL_beginav = newAV();
            DEBUG_x( dump_sub(gv) );
-           av_push(PL_beginav, (SV*)cv);
+           Perl_av_create_and_push(aTHX_ &PL_beginav, (SV*)cv);
            GvCV(gv) = 0;               /* cv has been hijacked */
            call_list(oldscope, PL_beginav);
 
@@ -5422,39 +5420,28 @@
            LEAVE;
        }
        else if (strEQ(s, "END") && !PL_error_count) {
-           if (!PL_endav)
-               PL_endav = newAV();
            DEBUG_x( dump_sub(gv) );
-           av_unshift(PL_endav, 1);
-           av_store(PL_endav, 0, (SV*)cv);
+           Perl_av_create_and_unshift_one(aTHX_ &PL_endav, (SV*)cv);
            GvCV(gv) = 0;               /* cv has been hijacked */
        }
        else if (strEQ(s, "UNITCHECK") && !PL_error_count) {
            /* It's never too late to run a unitcheck block */
-           if (!PL_unitcheckav)
-               PL_unitcheckav = newAV();
            DEBUG_x( dump_sub(gv) );
-           av_unshift(PL_unitcheckav, 1);
-           av_store(PL_unitcheckav, 0, (SV*)cv);
+           Perl_av_create_and_unshift_one(aTHX_ &PL_unitcheckav, (SV*)cv);
            GvCV(gv) = 0;               /* cv has been hijacked */
        }
        else if (strEQ(s, "CHECK") && !PL_error_count) {
-           if (!PL_checkav)
-               PL_checkav = newAV();
            DEBUG_x( dump_sub(gv) );
            if (PL_main_start && ckWARN(WARN_VOID))
                Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK 
block");
-           av_unshift(PL_checkav, 1);
-           av_store(PL_checkav, 0, (SV*)cv);
+           Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, (SV*)cv);
            GvCV(gv) = 0;               /* cv has been hijacked */
        }
        else if (strEQ(s, "INIT") && !PL_error_count) {
-           if (!PL_initav)
-               PL_initav = newAV();
            DEBUG_x( dump_sub(gv) );
            if (PL_main_start && ckWARN(WARN_VOID))
                Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT 
block");
-           av_push(PL_initav, (SV*)cv);
+           Perl_av_create_and_push(aTHX_ &PL_initav, (SV*)cv);
            GvCV(gv) = 0;               /* cv has been hijacked */
        }
     }
@@ -5651,33 +5638,23 @@
            goto done;
 
        if (strEQ(s, "BEGIN")) {
-           if (!PL_beginav)
-               PL_beginav = newAV();
-           av_push(PL_beginav, (SV*)cv);
+           Perl_av_create_and_push(aTHX_ &PL_beginav, (SV*)cv);
            GvCV(gv) = 0;               /* cv has been hijacked */
        }
        else if (strEQ(s, "END")) {
-           if (!PL_endav)
-               PL_endav = newAV();
-           av_unshift(PL_endav, 1);
-           av_store(PL_endav, 0, (SV*)cv);
+           Perl_av_create_and_unshift_one(aTHX_ &PL_endav, (SV*)cv);
            GvCV(gv) = 0;               /* cv has been hijacked */
        }
        else if (strEQ(s, "CHECK")) {
-           if (!PL_checkav)
-               PL_checkav = newAV();
            if (PL_main_start && ckWARN(WARN_VOID))
                Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK 
block");
-           av_unshift(PL_checkav, 1);
-           av_store(PL_checkav, 0, (SV*)cv);
+           Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, (SV*)cv);
            GvCV(gv) = 0;               /* cv has been hijacked */
        }
        else if (strEQ(s, "INIT")) {
-           if (!PL_initav)
-               PL_initav = newAV();
            if (PL_main_start && ckWARN(WARN_VOID))
                Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT 
block");
-           av_push(PL_initav, (SV*)cv);
+           Perl_av_create_and_push(aTHX_ &PL_initav, (SV*)cv);
            GvCV(gv) = 0;               /* cv has been hijacked */
        }
     }

==== //depot/perl/perl.c#783 (text) ====
Index: perl/perl.c
--- perl/perl.c#782~29877~      2007-01-18 15:51:45.000000000 -0800
+++ perl/perl.c 2007-01-29 10:28:16.000000000 -0800
@@ -321,8 +321,8 @@
     sv_setpvn(PERL_DEBUG_PAD(1), "", 0);       /* ext/re needs these */
     sv_setpvn(PERL_DEBUG_PAD(2), "", 0);       /* even without DEBUGGING. */
 #ifdef USE_ITHREADS
-    PL_regex_padav = newAV();
-    av_push(PL_regex_padav,(SV*)newAV());    /* First entry is an array of 
empty elements */
+    /* First entry is an array of empty elements */
+    Perl_av_create_and_push(aTHX_ &PL_regex_padav,(SV*)newAV());
     PL_regex_pad = AvARRAY(PL_regex_padav);
 #endif
 #ifdef USE_REENTRANT_API
@@ -1796,10 +1796,7 @@
            {
                SV *opts_prog;
 
-               if (!PL_preambleav)
-                   PL_preambleav = newAV();
-               av_push(PL_preambleav,
-                       newSVpvs("use Config;"));
+               Perl_av_create_and_push(aTHX_ &PL_preambleav, newSVpvs("use 
Config;"));
                if (*++s != ':')  {
                    STRLEN opts;
                
@@ -2060,10 +2057,8 @@
 
 #ifdef USE_SITECUSTOMIZE
     if (!minus_f) {
-       if (!PL_preambleav)
-           PL_preambleav = newAV();
-       av_unshift(PL_preambleav, 1);
-       (void)av_store(PL_preambleav, 0, Perl_newSVpvf(aTHX_ "BEGIN { do 
'%s/sitecustomize.pl' }", SITELIB_EXP));
+       (void)Perl_av_create_and_unshift_one(aTHX_ &PL_preambleav,
+                                            Perl_newSVpvf(aTHX_ "BEGIN { do 
'%s/sitecustomize.pl' }", SITELIB_EXP));
     }
 #endif
 
@@ -3182,8 +3177,6 @@
        return s;
     case 'A':
        forbid_setid('A', -1);
-       if (!PL_preambleav)
-           PL_preambleav = newAV();
        s++;
        {
            char * const start = s;
@@ -3200,7 +3193,7 @@
            else if (*s != '\0') {
                Perl_croak(aTHX_ "Can't use '%c' after -A%.*s", *s, 
(int)(s-start), start);
            }
-           av_push(PL_preambleav, sv);
+           Perl_av_create_and_push(aTHX_ &PL_preambleav, sv);
            return s;
        }
     case 'M':
@@ -3238,9 +3231,7 @@
                sv_catpvs(sv,  "\0)");
            }
            s += strlen(s);
-           if (!PL_preambleav)
-               PL_preambleav = newAV();
-           av_push(PL_preambleav, sv);
+           Perl_av_create_and_push(aTHX_ &PL_preambleav, sv);
        }
        else
            Perl_croak(aTHX_ "Missing argument to -%c", *(s-1));
@@ -5134,21 +5125,15 @@
        if (PL_savebegin) {
            if (paramList == PL_beginav) {
                /* save PL_beginav for compiler */
-               if (! PL_beginav_save)
-                   PL_beginav_save = newAV();
-               av_push(PL_beginav_save, (SV*)cv);
+               Perl_av_create_and_push(aTHX_ &PL_beginav_save, (SV*)cv);
            }
            else if (paramList == PL_checkav) {
                /* save PL_checkav for compiler */
-               if (! PL_checkav_save)
-                   PL_checkav_save = newAV();
-               av_push(PL_checkav_save, (SV*)cv);
+               Perl_av_create_and_push(aTHX_ &PL_checkav_save, (SV*)cv);
            }
            else if (paramList == PL_unitcheckav) {
                /* save PL_unitcheckav for compiler */
-               if (! PL_unitcheckav_save)
-                   PL_unitcheckav_save = newAV();
-               av_push(PL_unitcheckav_save, (SV*)cv);
+               Perl_av_create_and_push(aTHX_ &PL_unitcheckav_save, (SV*)cv);
            }
        } else {
            if (!PL_madskills)

==== //depot/perl/proto.h#797 (text+w) ====
Index: perl/proto.h
--- perl/proto.h#796~30034~     2007-01-27 09:03:59.000000000 -0800
+++ perl/proto.h        2007-01-29 10:28:16.000000000 -0800
@@ -158,6 +158,10 @@
 PERL_CALLCONV SV*      Perl_av_pop(pTHX_ AV* ar)
                        __attribute__nonnull__(pTHX_1);
 
+PERL_CALLCONV void     Perl_av_create_and_push(pTHX_ AV **const avp, SV *const 
val)
+                       __attribute__nonnull__(pTHX_1)
+                       __attribute__nonnull__(pTHX_2);
+
 PERL_CALLCONV void     Perl_av_push(pTHX_ AV* ar, SV* val)
                        __attribute__nonnull__(pTHX_1)
                        __attribute__nonnull__(pTHX_2);
@@ -175,6 +179,10 @@
 PERL_CALLCONV void     Perl_av_undef(pTHX_ AV* ar)
                        __attribute__nonnull__(pTHX_1);
 
+PERL_CALLCONV SV**     Perl_av_create_and_unshift_one(pTHX_ AV **const avp, SV 
*const val)
+                       __attribute__nonnull__(pTHX_1)
+                       __attribute__nonnull__(pTHX_2);
+
 PERL_CALLCONV void     Perl_av_unshift(pTHX_ AV* ar, I32 num)
                        __attribute__nonnull__(pTHX_1);
 
End of Patch.

Reply via email to