*/
/*
- * "...for the Entwives desired order, and plenty, and peace (by which they
- * meant that things should remain where they had set them)." --Treebeard
+ * '...for the Entwives desired order, and plenty, and peace (by which they
+ * meant that things should remain where they had set them).' --Treebeard
+ *
+ * [p.476 of _The Lord of the Rings_, III/iv: "Treebeard"]
*/
/*
if (AvREAL(av))
return;
#ifdef DEBUGGING
- if (SvTIED_mg((SV*)av, PERL_MAGIC_tied) && ckWARN_d(WARN_DEBUGGING))
- Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), "av_reify called on tied array");
+ if (SvTIED_mg((const SV *)av, PERL_MAGIC_tied))
+ Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING), "av_reify called on tied array");
#endif
key = AvMAX(av) + 1;
while (key > AvFILLp(av) + 1)
PERL_ARGS_ASSERT_AV_EXTEND;
assert(SvTYPE(av) == SVt_PVAV);
- mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied);
+ mg = SvTIED_mg((const SV *)av, PERL_MAGIC_tied);
if (mg) {
- dSP;
- ENTER;
- SAVETMPS;
- PUSHSTACKi(PERLSI_MAGIC);
- PUSHMARK(SP);
- EXTEND(SP,2);
- PUSHs(SvTIED_obj((SV*)av, mg));
- mPUSHi(key + 1);
- PUTBACK;
- call_method("EXTEND", G_SCALAR|G_DISCARD);
- POPSTACK;
- FREETMPS;
- LEAVE;
+ SV *arg1 = sv_newmortal();
+ sv_setiv(arg1, (IV)(key + 1));
+ magic_methcall(MUTABLE_SV(av), mg, "EXTEND", G_DISCARD, 1, arg1, NULL);
return;
}
if (key > AvMAX(av)) {
the current lazy system of only writing to it if our caller
has a need for more space. NWC */
newmax = Perl_safesysmalloc_size((void*)AvALLOC(av)) /
- sizeof(SV*) - 1;
+ sizeof(const SV *) - 1;
if (key <= newmax)
goto resized;
#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
Renew(AvALLOC(av),newmax+1, SV*);
#else
- bytes = (newmax + 1) * sizeof(SV*);
+ bytes = (newmax + 1) * sizeof(const SV *);
#define MALLOC_OVERHEAD 16
itmp = MALLOC_OVERHEAD;
while ((MEM_SIZE)(itmp - MALLOC_OVERHEAD) < bytes)
itmp += itmp;
itmp -= MALLOC_OVERHEAD;
- itmp /= sizeof(SV*);
+ itmp /= sizeof(const SV *);
assert(itmp > newmax);
newmax = itmp - 1;
assert(newmax >= AvMAX(av));
Newx(ary, newmax+1, SV*);
Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
if (AvMAX(av) > 64)
- offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
+ offer_nice_chunk(AvALLOC(av),
+ (AvMAX(av)+1) * sizeof(const SV *));
else
Safefree(AvALLOC(av));
AvALLOC(av) = ary;
assert(SvTYPE(av) == SVt_PVAV);
if (SvRMAGICAL(av)) {
- const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
- if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) {
+ const MAGIC * const tied_magic
+ = mg_find((const SV *)av, PERL_MAGIC_tied);
+ if (tied_magic || mg_find((const SV *)av, PERL_MAGIC_regdata)) {
SV *sv;
if (key < 0) {
I32 adjust_index = 1;
if (tied_magic) {
/* Handle negative array indices 20020222 MJD */
SV * const * const negative_indices_glob =
- hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av, tied_magic))),
+ hv_fetch(SvSTASH(SvRV(SvTIED_obj(MUTABLE_SV(av),
+ tied_magic))),
NEGATIVE_INDICES_VAR, 16, 0);
if (negative_indices_glob && SvTRUE(GvSV(*negative_indices_glob)))
sv = sv_newmortal();
sv_upgrade(sv, SVt_PVLV);
- mg_copy((SV*)av, sv, 0, key);
+ mg_copy(MUTABLE_SV(av), sv, 0, key);
+ if (!tied_magic) /* for regdata, force leavesub to make copies */
+ SvTEMP_off(sv);
LvTYPE(sv) = 't';
LvTARG(sv) = sv; /* fake (SV**) */
return &(LvTARG(sv));
val = &PL_sv_undef;
if (SvRMAGICAL(av)) {
- const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
+ const MAGIC * const tied_magic = mg_find((const SV *)av, PERL_MAGIC_tied);
if (tied_magic) {
/* Handle negative array indices 20020222 MJD */
if (key < 0) {
bool adjust_index = 1;
SV * const * const negative_indices_glob =
- hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
+ hv_fetch(SvSTASH(SvRV(SvTIED_obj(MUTABLE_SV(av),
tied_magic))),
NEGATIVE_INDICES_VAR, 16, 0);
if (negative_indices_glob
}
}
if (val != &PL_sv_undef) {
- mg_copy((SV*)av, val, 0, key);
+ mg_copy(MUTABLE_SV(av), val, 0, key);
}
return NULL;
}
}
if (SvREADONLY(av) && key >= AvFILL(av))
- Perl_croak(aTHX_ PL_no_modify);
+ Perl_croak(aTHX_ "%s", PL_no_modify);
if (!AvREAL(av) && AvREIFY(av))
av_reify(av);
if (SvSMAGICAL(av)) {
const MAGIC* const mg = SvMAGIC(av);
if (val != &PL_sv_undef) {
- sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
+ sv_magic(val, MUTABLE_SV(av), toLOWER(mg->mg_type), 0, key);
}
if (PL_delaymagic && mg->mg_type == PERL_MAGIC_isa)
PL_delaymagic |= DM_ARRAY;
else
- mg_set((SV*)av);
+ mg_set(MUTABLE_SV(av));
}
return &ary[key];
}
AV *
Perl_av_make(pTHX_ register I32 size, register SV **strp)
{
- register AV * const av = (AV*)newSV_type(SVt_PVAV);
+ register AV * const av = MUTABLE_AV(newSV_type(SVt_PVAV));
/* sv_upgrade does AvREAL_only() */
PERL_ARGS_ASSERT_AV_MAKE;
assert(SvTYPE(av) == SVt_PVAV);
AvFILLp(av) = AvMAX(av) = size - 1;
for (i = 0; i < size; i++) {
assert (*strp);
+
+ /* Don't let sv_setsv swipe, since our source array might
+ have multiple references to the same temp scalar (e.g.
+ from a list slice) */
+
ary[i] = newSV(0);
- sv_setsv(ary[i], *strp);
+ sv_setsv_flags(ary[i], *strp,
+ SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL);
strp++;
}
}
assert(SvTYPE(av) == SVt_PVAV);
#ifdef DEBUGGING
- if (SvREFCNT(av) == 0 && ckWARN_d(WARN_DEBUGGING)) {
- Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), "Attempt to clear deleted array");
+ if (SvREFCNT(av) == 0) {
+ Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING), "Attempt to clear deleted array");
}
#endif
if (SvREADONLY(av))
- Perl_croak(aTHX_ PL_no_modify);
+ Perl_croak(aTHX_ "%s", PL_no_modify);
/* Give any tie a chance to cleanup first */
if (SvRMAGICAL(av)) {
if (PL_delaymagic && mg && mg->mg_type == PERL_MAGIC_isa)
PL_delaymagic |= DM_ARRAY;
else
- mg_clear((SV*)av);
+ mg_clear(MUTABLE_SV(av));
}
if (AvMAX(av) < 0)
assert(SvTYPE(av) == SVt_PVAV);
/* Give any tie a chance to cleanup first */
- if (SvTIED_mg((SV*)av, PERL_MAGIC_tied))
+ if (SvTIED_mg((const SV *)av, PERL_MAGIC_tied))
av_fill(av, -1);
if (AvREAL(av)) {
AvARRAY(av) = NULL;
AvMAX(av) = AvFILLp(av) = -1;
- if(SvRMAGICAL(av)) mg_clear((SV*)av);
+ if(SvRMAGICAL(av)) mg_clear(MUTABLE_SV(av));
}
/*
=for apidoc av_push
Pushes an SV onto the end of the array. The array will grow automatically
-to accommodate the addition.
+to accommodate the addition. Like C<av_store>, this takes ownership of one
+reference count.
=cut
*/
assert(SvTYPE(av) == SVt_PVAV);
if (SvREADONLY(av))
- Perl_croak(aTHX_ PL_no_modify);
-
- if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
- dSP;
- PUSHSTACKi(PERLSI_MAGIC);
- PUSHMARK(SP);
- EXTEND(SP,2);
- PUSHs(SvTIED_obj((SV*)av, mg));
- PUSHs(val);
- PUTBACK;
- ENTER;
- call_method("PUSH", G_SCALAR|G_DISCARD);
- LEAVE;
- POPSTACK;
+ Perl_croak(aTHX_ "%s", PL_no_modify);
+
+ if ((mg = SvTIED_mg((const SV *)av, PERL_MAGIC_tied))) {
+ magic_methcall(MUTABLE_SV(av), mg, "PUSH", G_DISCARD, 1, val, NULL);
return;
}
av_store(av,AvFILLp(av)+1,val);
assert(SvTYPE(av) == SVt_PVAV);
if (SvREADONLY(av))
- Perl_croak(aTHX_ PL_no_modify);
- if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
- dSP;
- PUSHSTACKi(PERLSI_MAGIC);
- PUSHMARK(SP);
- XPUSHs(SvTIED_obj((SV*)av, mg));
- PUTBACK;
- ENTER;
- if (call_method("POP", G_SCALAR)) {
- retval = newSVsv(*PL_stack_sp--);
- } else {
- retval = &PL_sv_undef;
- }
- LEAVE;
- POPSTACK;
+ Perl_croak(aTHX_ "%s", PL_no_modify);
+ if ((mg = SvTIED_mg((const SV *)av, PERL_MAGIC_tied))) {
+ retval = magic_methcall(MUTABLE_SV(av), mg, "POP", 0, 0, NULL, NULL);
+ if (retval)
+ retval = newSVsv(retval);
return retval;
}
if (AvFILL(av) < 0)
retval = AvARRAY(av)[AvFILLp(av)];
AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
if (SvSMAGICAL(av))
- mg_set((SV*)av);
+ mg_set(MUTABLE_SV(av));
return retval;
}
assert(SvTYPE(av) == SVt_PVAV);
if (SvREADONLY(av))
- Perl_croak(aTHX_ PL_no_modify);
-
- if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
- dSP;
- PUSHSTACKi(PERLSI_MAGIC);
- PUSHMARK(SP);
- EXTEND(SP,1+num);
- PUSHs(SvTIED_obj((SV*)av, mg));
- while (num-- > 0) {
- PUSHs(&PL_sv_undef);
- }
- PUTBACK;
- ENTER;
- call_method("UNSHIFT", G_SCALAR|G_DISCARD);
- LEAVE;
- POPSTACK;
+ Perl_croak(aTHX_ "%s", PL_no_modify);
+
+ if ((mg = SvTIED_mg((const SV *)av, PERL_MAGIC_tied))) {
+ magic_methcall(MUTABLE_SV(av), mg, "UNSHIFT", G_DISCARD,
+ -num, NULL, NULL);
return;
}
assert(SvTYPE(av) == SVt_PVAV);
if (SvREADONLY(av))
- Perl_croak(aTHX_ PL_no_modify);
- if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
- dSP;
- PUSHSTACKi(PERLSI_MAGIC);
- PUSHMARK(SP);
- XPUSHs(SvTIED_obj((SV*)av, mg));
- PUTBACK;
- ENTER;
- if (call_method("SHIFT", G_SCALAR)) {
- retval = newSVsv(*PL_stack_sp--);
- } else {
- retval = &PL_sv_undef;
- }
- LEAVE;
- POPSTACK;
+ Perl_croak(aTHX_ "%s", PL_no_modify);
+ if ((mg = SvTIED_mg((const SV *)av, PERL_MAGIC_tied))) {
+ retval = magic_methcall(MUTABLE_SV(av), mg, "SHIFT", 0, 0, NULL, NULL);
+ if (retval)
+ retval = newSVsv(retval);
return retval;
}
if (AvFILL(av) < 0)
AvMAX(av)--;
AvFILLp(av)--;
if (SvSMAGICAL(av))
- mg_set((SV*)av);
+ mg_set(MUTABLE_SV(av));
return retval;
}
*/
I32
-Perl_av_len(pTHX_ register const AV *av)
+Perl_av_len(pTHX_ AV *av)
{
PERL_ARGS_ASSERT_AV_LEN;
assert(SvTYPE(av) == SVt_PVAV);
if (fill < 0)
fill = -1;
- if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
- dSP;
- ENTER;
- SAVETMPS;
- PUSHSTACKi(PERLSI_MAGIC);
- PUSHMARK(SP);
- EXTEND(SP,2);
- PUSHs(SvTIED_obj((SV*)av, mg));
- mPUSHi(fill + 1);
- PUTBACK;
- call_method("STORESIZE", G_SCALAR|G_DISCARD);
- POPSTACK;
- FREETMPS;
- LEAVE;
+ if ((mg = SvTIED_mg((const SV *)av, PERL_MAGIC_tied))) {
+ SV *arg1 = sv_newmortal();
+ sv_setiv(arg1, (IV)(fill + 1));
+ magic_methcall(MUTABLE_SV(av), mg, "STORESIZE", G_DISCARD,
+ 1, arg1, NULL);
return;
}
if (fill <= AvMAX(av)) {
AvFILLp(av) = fill;
if (SvSMAGICAL(av))
- mg_set((SV*)av);
+ mg_set(MUTABLE_SV(av));
}
else
(void)av_store(av,fill,&PL_sv_undef);
assert(SvTYPE(av) == SVt_PVAV);
if (SvREADONLY(av))
- Perl_croak(aTHX_ PL_no_modify);
+ Perl_croak(aTHX_ "%s", PL_no_modify);
if (SvRMAGICAL(av)) {
- const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
- if ((tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata))) {
+ const MAGIC * const tied_magic
+ = mg_find((const SV *)av, PERL_MAGIC_tied);
+ if ((tied_magic || mg_find((const SV *)av, PERL_MAGIC_regdata))) {
/* Handle negative array indices 20020222 MJD */
SV **svp;
if (key < 0) {
unsigned adjust_index = 1;
if (tied_magic) {
SV * const * const negative_indices_glob =
- hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
+ hv_fetch(SvSTASH(SvRV(SvTIED_obj(MUTABLE_SV(av),
tied_magic))),
NEGATIVE_INDICES_VAR, 16, 0);
if (negative_indices_glob
else
AvARRAY(av)[key] = &PL_sv_undef;
if (SvSMAGICAL(av))
- mg_set((SV*)av);
+ mg_set(MUTABLE_SV(av));
}
if (flags & G_DISCARD) {
SvREFCNT_dec(sv);
assert(SvTYPE(av) == SVt_PVAV);
if (SvRMAGICAL(av)) {
- const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
- if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) {
+ const MAGIC * const tied_magic
+ = mg_find((const SV *)av, PERL_MAGIC_tied);
+ if (tied_magic || mg_find((const SV *)av, PERL_MAGIC_regdata)) {
SV * const sv = sv_newmortal();
MAGIC *mg;
/* Handle negative array indices 20020222 MJD */
unsigned adjust_index = 1;
if (tied_magic) {
SV * const * const negative_indices_glob =
- hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
+ hv_fetch(SvSTASH(SvRV(SvTIED_obj(MUTABLE_SV(av),
tied_magic))),
NEGATIVE_INDICES_VAR, 16, 0);
if (negative_indices_glob
}
}
- mg_copy((SV*)av, sv, 0, key);
+ mg_copy(MUTABLE_SV(av), sv, 0, key);
mg = mg_find(sv, PERL_MAGIC_tiedelem);
if (mg) {
magic_existspack(sv, mg);
- return (bool)SvTRUE(sv);
+ return cBOOL(SvTRUE(sv));
}
}
PERL_ARGS_ASSERT_GET_AUX_MG;
assert(SvTYPE(av) == SVt_PVAV);
- mg = mg_find((SV*)av, PERL_MAGIC_arylen_p);
+ mg = mg_find((const SV *)av, PERL_MAGIC_arylen_p);
if (!mg) {
- mg = sv_magicext((SV*)av, 0, PERL_MAGIC_arylen_p, &PL_vtbl_arylen_p,
- 0, 0);
+ mg = sv_magicext(MUTABLE_SV(av), 0, PERL_MAGIC_arylen_p,
+ &PL_vtbl_arylen_p, 0, 0);
assert(mg);
/* sv_magicext won't set this for us because we pass in a NULL obj */
mg->mg_flags |= MGf_REFCOUNTED;