summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2007-01-29 18:28:16 +0000
committerNicholas Clark <nick@ccl4.org>2007-01-29 18:28:16 +0000
commit29a861e74521a5f903bccb023c86950d733fa0b7 (patch)
tree30399e656738dc7add2288fa2aebd589eeaaf041
parente30fbb82116d47ef2569f2af20359965dc7fcae1 (diff)
downloadperl-29a861e74521a5f903bccb023c86950d733fa0b7.tar.gz
Add av_create_and_push() and av_create_and_unshift_one() to refactor
out two repeated idioms. p4raw-id: //depot/perl@30064
-rw-r--r--av.c38
-rw-r--r--doio.c5
-rw-r--r--embed.fnc2
-rw-r--r--op.c41
-rw-r--r--perl.c35
-rw-r--r--proto.h8
6 files changed, 69 insertions, 60 deletions
diff --git a/av.c b/av.c
index 22eb6716f2..c6677a9ed8 100644
--- a/av.c
+++ b/av.c
@@ -487,6 +487,24 @@ Perl_av_undef(pTHX_ register AV *av)
}
/*
+
+=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 @@ Perl_av_pop(pTHX_ register AV *av)
}
/*
+
+=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
diff --git a/doio.c b/doio.c
index 49b1fed199..4b84bdb367 100644
--- a/doio.c
+++ b/doio.c
@@ -710,10 +710,9 @@ Perl_nextargv(pTHX_ register GV *gv)
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)) {
diff --git a/embed.fnc b/embed.fnc
index 5c33ad955e..0a369e3c03 100644
--- a/embed.fnc
+++ b/embed.fnc
@@ -106,11 +106,13 @@ Apd |void |av_fill |NN AV* ar|I32 fill
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
diff --git a/op.c b/op.c
index 97cebc0144..b952ef86dc 100644
--- a/op.c
+++ b/op.c
@@ -5410,10 +5410,8 @@ Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
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 @@ Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
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 @@ Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename)
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 */
}
}
diff --git a/perl.c b/perl.c
index 9c7b3148fd..335411654c 100644
--- a/perl.c
+++ b/perl.c
@@ -321,8 +321,8 @@ perl_construct(pTHXx)
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 @@ S_parse_body(pTHX_ char **env, XSINIT_t xsinit)
{
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 @@ S_parse_body(pTHX_ char **env, XSINIT_t xsinit)
#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 @@ Perl_moreswitches(pTHX_ char *s)
return s;
case 'A':
forbid_setid('A', -1);
- if (!PL_preambleav)
- PL_preambleav = newAV();
s++;
{
char * const start = s;
@@ -3200,7 +3193,7 @@ Perl_moreswitches(pTHX_ char *s)
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 @@ Perl_moreswitches(pTHX_ char *s)
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 @@ Perl_call_list(pTHX_ I32 oldscope, AV *paramList)
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)
diff --git a/proto.h b/proto.h
index f9bcc51e35..0272b80021 100644
--- a/proto.h
+++ b/proto.h
@@ -158,6 +158,10 @@ PERL_CALLCONV AV* Perl_av_make(pTHX_ I32 size, SV** svp)
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 SV** Perl_av_store(pTHX_ AV* ar, I32 key, SV* val)
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);