+void
+Perl_av_reify(pTHX_ AV *av)
+{
+ I32 key;
+ SV* sv;
+
+ 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");
+#endif
+ key = AvMAX(av) + 1;
+ while (key > AvFILLp(av) + 1)
+ AvARRAY(av)[--key] = &PL_sv_undef;
+ while (key) {
+ sv = AvARRAY(av)[--key];
+ assert(sv);
+ if (sv != &PL_sv_undef)
+ (void)SvREFCNT_inc(sv);
+ }
+ key = AvARRAY(av) - AvALLOC(av);
+ while (key)
+ AvALLOC(av)[--key] = &PL_sv_undef;
+ AvREIFY_off(av);
+ AvREAL_on(av);
+}
+
+/*
+=for apidoc av_extend
+
+Pre-extend an array. The C<key> is the index to which the array should be
+extended.
+
+=cut
+*/
+
+void
+Perl_av_extend(pTHX_ AV *av, I32 key)
+{
+ MAGIC *mg;
+ 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));
+ PUSHs(sv_2mortal(newSViv(key+1)));
+ PUTBACK;
+ call_method("EXTEND", G_SCALAR|G_DISCARD);
+ POPSTACK;
+ FREETMPS;
+ LEAVE;
+ return;
+ }
+ if (key > AvMAX(av)) {
+ SV** ary;
+ I32 tmp;
+ I32 newmax;
+
+ if (AvALLOC(av) != AvARRAY(av)) {
+ ary = AvALLOC(av) + AvFILLp(av) + 1;
+ tmp = AvARRAY(av) - AvALLOC(av);
+ Move(AvARRAY(av), AvALLOC(av), AvFILLp(av)+1, SV*);
+ AvMAX(av) += tmp;
+ SvPVX(av) = (char*)AvALLOC(av);
+ if (AvREAL(av)) {
+ while (tmp)
+ ary[--tmp] = &PL_sv_undef;
+ }
+
+ if (key > AvMAX(av) - 10) {
+ newmax = key + AvMAX(av);
+ goto resize;
+ }
+ }
+ else {
+ if (AvALLOC(av)) {
+#if !defined(STRANGE_MALLOC) && !defined(MYMALLOC)
+ MEM_SIZE bytes;
+ IV itmp;
+#endif
+
+#if defined(MYMALLOC) && !defined(LEAKTEST)
+ newmax = malloced_size((void*)AvALLOC(av))/sizeof(SV*) - 1;
+
+ if (key <= newmax)
+ goto resized;
+#endif
+ newmax = key + AvMAX(av) / 5;
+ resize:
+#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
+ Renew(AvALLOC(av),newmax+1, SV*);
+#else
+ bytes = (newmax + 1) * sizeof(SV*);
+#define MALLOC_OVERHEAD 16
+ itmp = MALLOC_OVERHEAD;
+ while (itmp - MALLOC_OVERHEAD < bytes)
+ itmp += itmp;
+ itmp -= MALLOC_OVERHEAD;
+ itmp /= sizeof(SV*);
+ assert(itmp > newmax);
+ newmax = itmp - 1;
+ assert(newmax >= AvMAX(av));
+ New(2,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*));
+ else
+ Safefree(AvALLOC(av));
+ AvALLOC(av) = ary;
+#endif
+#if defined(MYMALLOC) && !defined(LEAKTEST)
+ resized:
+#endif
+ ary = AvALLOC(av) + AvMAX(av) + 1;
+ tmp = newmax - AvMAX(av);
+ if (av == PL_curstack) { /* Oops, grew stack (via av_store()?) */
+ PL_stack_sp = AvALLOC(av) + (PL_stack_sp - PL_stack_base);
+ PL_stack_base = AvALLOC(av);
+ PL_stack_max = PL_stack_base + newmax;
+ }
+ }
+ else {
+ newmax = key < 3 ? 3 : key;
+ New(2,AvALLOC(av), newmax+1, SV*);
+ ary = AvALLOC(av) + 1;
+ tmp = newmax;
+ AvALLOC(av)[0] = &PL_sv_undef; /* For the stacks */
+ }
+ if (AvREAL(av)) {
+ while (tmp)
+ ary[--tmp] = &PL_sv_undef;
+ }
+
+ SvPVX(av) = (char*)AvALLOC(av);
+ AvMAX(av) = newmax;
+ }
+ }
+}
+
+/*
+=for apidoc av_fetch
+
+Returns the SV at the specified index in the array. The C<key> is the
+index. If C<lval> is set then the fetch will be part of a store. Check
+that the return value is non-null before dereferencing it to a C<SV*>.
+
+See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
+more information on how to use this function on tied arrays.
+
+=cut
+*/
+