3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "...for the Entwives desired order, and plenty, and peace (by which they
13 * meant that things should remain where they had set them)." --Treebeard
17 =head1 Array Manipulation Functions
25 Perl_av_reify(pTHX_ AV *av)
35 if (SvTIED_mg((SV*)av, PERL_MAGIC_tied) && ckWARN_d(WARN_DEBUGGING))
36 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), "av_reify called on tied array");
39 while (key > AvFILLp(av) + 1)
40 AvARRAY(av)[--key] = &PL_sv_undef;
42 SV * const sv = AvARRAY(av)[--key];
44 if (sv != &PL_sv_undef)
45 SvREFCNT_inc_simple_void_NN(sv);
47 key = AvARRAY(av) - AvALLOC(av);
49 AvALLOC(av)[--key] = &PL_sv_undef;
57 Pre-extend an array. The C<key> is the index to which the array should be
64 Perl_av_extend(pTHX_ AV *av, I32 key)
71 mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied);
76 PUSHSTACKi(PERLSI_MAGIC);
79 PUSHs(SvTIED_obj((SV*)av, mg));
80 PUSHs(sv_2mortal(newSViv(key+1)));
82 call_method("EXTEND", G_SCALAR|G_DISCARD);
88 if (key > AvMAX(av)) {
93 if (AvALLOC(av) != AvARRAY(av)) {
94 ary = AvALLOC(av) + AvFILLp(av) + 1;
95 tmp = AvARRAY(av) - AvALLOC(av);
96 Move(AvARRAY(av), AvALLOC(av), AvFILLp(av)+1, SV*);
98 AvARRAY(av) = AvALLOC(av);
101 ary[--tmp] = &PL_sv_undef;
103 if (key > AvMAX(av) - 10) {
104 newmax = key + AvMAX(av);
109 #ifdef PERL_MALLOC_WRAP
110 static const char oom_array_extend[] =
111 "Out of memory during array extend"; /* Duplicated in pp_hot.c */
115 #if !defined(STRANGE_MALLOC) && !defined(MYMALLOC)
121 newmax = malloced_size((void*)AvALLOC(av))/sizeof(SV*) - 1;
126 newmax = key + AvMAX(av) / 5;
128 MEM_WRAP_CHECK_1(newmax+1, SV*, oom_array_extend);
129 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
130 Renew(AvALLOC(av),newmax+1, SV*);
132 bytes = (newmax + 1) * sizeof(SV*);
133 #define MALLOC_OVERHEAD 16
134 itmp = MALLOC_OVERHEAD;
135 while ((MEM_SIZE)(itmp - MALLOC_OVERHEAD) < bytes)
137 itmp -= MALLOC_OVERHEAD;
139 assert(itmp > newmax);
141 assert(newmax >= AvMAX(av));
142 Newx(ary, newmax+1, SV*);
143 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
145 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
147 Safefree(AvALLOC(av));
153 ary = AvALLOC(av) + AvMAX(av) + 1;
154 tmp = newmax - AvMAX(av);
155 if (av == PL_curstack) { /* Oops, grew stack (via av_store()?) */
156 PL_stack_sp = AvALLOC(av) + (PL_stack_sp - PL_stack_base);
157 PL_stack_base = AvALLOC(av);
158 PL_stack_max = PL_stack_base + newmax;
162 newmax = key < 3 ? 3 : key;
163 MEM_WRAP_CHECK_1(newmax+1, SV*, oom_array_extend);
164 Newx(AvALLOC(av), newmax+1, SV*);
165 ary = AvALLOC(av) + 1;
167 AvALLOC(av)[0] = &PL_sv_undef; /* For the stacks */
171 ary[--tmp] = &PL_sv_undef;
174 AvARRAY(av) = AvALLOC(av);
183 Returns the SV at the specified index in the array. The C<key> is the
184 index. If C<lval> is set then the fetch will be part of a store. Check
185 that the return value is non-null before dereferencing it to a C<SV*>.
187 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
188 more information on how to use this function on tied arrays.
194 Perl_av_fetch(pTHX_ register AV *av, I32 key, I32 lval)
200 if (SvRMAGICAL(av)) {
201 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
202 if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) {
205 I32 adjust_index = 1;
207 /* Handle negative array indices 20020222 MJD */
208 SV * const * const negative_indices_glob =
209 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av, tied_magic))),
210 NEGATIVE_INDICES_VAR, 16, 0);
212 if (negative_indices_glob && SvTRUE(GvSV(*negative_indices_glob)))
217 key += AvFILL(av) + 1;
224 sv_upgrade(sv, SVt_PVLV);
225 mg_copy((SV*)av, sv, 0, key);
227 LvTARG(sv) = sv; /* fake (SV**) */
228 return &(LvTARG(sv));
233 key += AvFILL(av) + 1;
238 if (key > AvFILLp(av)) {
241 return av_store(av,key,newSV(0));
243 if (AvARRAY(av)[key] == &PL_sv_undef) {
246 return av_store(av,key,newSV(0));
250 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
251 || SvIS_FREED(AvARRAY(av)[key]))) {
252 AvARRAY(av)[key] = &PL_sv_undef; /* 1/2 reify */
255 return &AvARRAY(av)[key];
261 Stores an SV in an array. The array index is specified as C<key>. The
262 return value will be NULL if the operation failed or if the value did not
263 need to be actually stored within the array (as in the case of tied
264 arrays). Otherwise it can be dereferenced to get the original C<SV*>. Note
265 that the caller is responsible for suitably incrementing the reference
266 count of C<val> before the call, and decrementing it if the function
269 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
270 more information on how to use this function on tied arrays.
276 Perl_av_store(pTHX_ register AV *av, I32 key, SV *val)
283 /* S_regclass relies on being able to pass in a NULL sv
284 (unicode_alternate may be NULL).
290 if (SvRMAGICAL(av)) {
291 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
293 /* Handle negative array indices 20020222 MJD */
295 bool adjust_index = 1;
296 SV * const * const negative_indices_glob =
297 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
299 NEGATIVE_INDICES_VAR, 16, 0);
300 if (negative_indices_glob
301 && SvTRUE(GvSV(*negative_indices_glob)))
304 key += AvFILL(av) + 1;
309 if (val != &PL_sv_undef) {
310 mg_copy((SV*)av, val, 0, key);
318 key += AvFILL(av) + 1;
323 if (SvREADONLY(av) && key >= AvFILL(av))
324 Perl_croak(aTHX_ PL_no_modify);
326 if (!AvREAL(av) && AvREIFY(av))
331 if (AvFILLp(av) < key) {
333 if (av == PL_curstack && key > PL_stack_sp - PL_stack_base)
334 PL_stack_sp = PL_stack_base + key; /* XPUSH in disguise */
336 ary[++AvFILLp(av)] = &PL_sv_undef;
337 } while (AvFILLp(av) < key);
342 SvREFCNT_dec(ary[key]);
344 if (SvSMAGICAL(av)) {
345 const MAGIC* const mg = SvMAGIC(av);
346 if (val != &PL_sv_undef) {
347 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
349 if (PL_delaymagic && mg->mg_type == PERL_MAGIC_isa)
350 PL_delaymagic |= DM_ARRAY;
360 Creates a new AV. The reference count is set to 1.
368 register AV * const av = (AV*)newSV_type(SVt_PVAV);
369 /* sv_upgrade does AvREAL_only() */
372 AvMAX(av) = AvFILLp(av) = -1;
379 Creates a new AV and populates it with a list of SVs. The SVs are copied
380 into the array, so they may be freed after the call to av_make. The new AV
381 will have a reference count of 1.
387 Perl_av_make(pTHX_ register I32 size, register SV **strp)
389 register AV * const av = (AV*)newSV_type(SVt_PVAV);
390 /* sv_upgrade does AvREAL_only() */
391 if (size) { /* "defined" was returning undef for size==0 anyway. */
397 AvFILLp(av) = AvMAX(av) = size - 1;
398 for (i = 0; i < size; i++) {
401 sv_setsv(ary[i], *strp);
411 Clears an array, making it empty. Does not free the memory used by the
418 Perl_av_clear(pTHX_ register AV *av)
425 if (SvREFCNT(av) == 0 && ckWARN_d(WARN_DEBUGGING)) {
426 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), "Attempt to clear deleted array");
431 Perl_croak(aTHX_ PL_no_modify);
433 /* Give any tie a chance to cleanup first */
434 if (SvRMAGICAL(av)) {
435 const MAGIC* const mg = SvMAGIC(av);
436 if (PL_delaymagic && mg->mg_type == PERL_MAGIC_isa)
437 PL_delaymagic |= DM_ARRAY;
446 SV** const ary = AvARRAY(av);
447 I32 index = AvFILLp(av) + 1;
449 SV * const sv = ary[--index];
450 /* undef the slot before freeing the value, because a
451 * destructor might try to modify this array */
452 ary[index] = &PL_sv_undef;
456 extra = AvARRAY(av) - AvALLOC(av);
459 AvARRAY(av) = AvALLOC(av);
468 Undefines the array. Frees the memory used by the array itself.
474 Perl_av_undef(pTHX_ register AV *av)
478 /* Give any tie a chance to cleanup first */
479 if (SvTIED_mg((SV*)av, PERL_MAGIC_tied))
483 register I32 key = AvFILLp(av) + 1;
485 SvREFCNT_dec(AvARRAY(av)[--key]);
488 Safefree(AvALLOC(av));
491 AvMAX(av) = AvFILLp(av) = -1;
493 if(SvRMAGICAL(av)) mg_clear((SV*)av);
498 =for apidoc av_create_and_push
500 Push an SV onto the end of the array, creating the array if necessary.
501 A small internal helper function to remove a commonly duplicated idiom.
507 Perl_av_create_and_push(pTHX_ AV **const avp, SV *const val)
517 Pushes an SV onto the end of the array. The array will grow automatically
518 to accommodate the addition.
524 Perl_av_push(pTHX_ register AV *av, SV *val)
531 Perl_croak(aTHX_ PL_no_modify);
533 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
535 PUSHSTACKi(PERLSI_MAGIC);
538 PUSHs(SvTIED_obj((SV*)av, mg));
542 call_method("PUSH", G_SCALAR|G_DISCARD);
547 av_store(av,AvFILLp(av)+1,val);
553 Pops an SV off the end of the array. Returns C<&PL_sv_undef> if the array
560 Perl_av_pop(pTHX_ register AV *av)
569 Perl_croak(aTHX_ PL_no_modify);
570 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
572 PUSHSTACKi(PERLSI_MAGIC);
574 XPUSHs(SvTIED_obj((SV*)av, mg));
577 if (call_method("POP", G_SCALAR)) {
578 retval = newSVsv(*PL_stack_sp--);
580 retval = &PL_sv_undef;
588 retval = AvARRAY(av)[AvFILLp(av)];
589 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
597 =for apidoc av_create_and_unshift_one
599 Unshifts an SV onto the beginning of the array, creating the array if
601 A small internal helper function to remove a commonly duplicated idiom.
607 Perl_av_create_and_unshift_one(pTHX_ AV **const avp, SV *const val)
612 return av_store(*avp, 0, val);
616 =for apidoc av_unshift
618 Unshift the given number of C<undef> values onto the beginning of the
619 array. The array will grow automatically to accommodate the addition. You
620 must then use C<av_store> to assign values to these new elements.
626 Perl_av_unshift(pTHX_ register AV *av, register I32 num)
635 Perl_croak(aTHX_ PL_no_modify);
637 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
639 PUSHSTACKi(PERLSI_MAGIC);
642 PUSHs(SvTIED_obj((SV*)av, mg));
648 call_method("UNSHIFT", G_SCALAR|G_DISCARD);
656 if (!AvREAL(av) && AvREIFY(av))
658 i = AvARRAY(av) - AvALLOC(av);
666 AvARRAY(av) = AvARRAY(av) - i;
670 const I32 i = AvFILLp(av);
671 /* Create extra elements */
672 const I32 slide = i > 0 ? i : 0;
674 av_extend(av, i + num);
677 Move(ary, ary + num, i + 1, SV*);
679 ary[--num] = &PL_sv_undef;
681 /* Make extra elements into a buffer */
683 AvFILLp(av) -= slide;
684 AvARRAY(av) = AvARRAY(av) + slide;
691 Shifts an SV off the beginning of the array.
697 Perl_av_shift(pTHX_ register AV *av)
706 Perl_croak(aTHX_ PL_no_modify);
707 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
709 PUSHSTACKi(PERLSI_MAGIC);
711 XPUSHs(SvTIED_obj((SV*)av, mg));
714 if (call_method("SHIFT", G_SCALAR)) {
715 retval = newSVsv(*PL_stack_sp--);
717 retval = &PL_sv_undef;
725 retval = *AvARRAY(av);
727 *AvARRAY(av) = &PL_sv_undef;
728 AvARRAY(av) = AvARRAY(av) + 1;
739 Returns the highest index in the array. The number of elements in the
740 array is C<av_len(av) + 1>. Returns -1 if the array is empty.
746 Perl_av_len(pTHX_ register const AV *av)
755 Set the highest index in the array to the given number, equivalent to
756 Perl's C<$#array = $fill;>.
758 The number of elements in the an array will be C<fill + 1> after
759 av_fill() returns. If the array was previously shorter then the
760 additional elements appended are set to C<PL_sv_undef>. If the array
761 was longer, then the excess elements are freed. C<av_fill(av, -1)> is
762 the same as C<av_clear(av)>.
767 Perl_av_fill(pTHX_ register AV *av, I32 fill)
776 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
780 PUSHSTACKi(PERLSI_MAGIC);
783 PUSHs(SvTIED_obj((SV*)av, mg));
784 PUSHs(sv_2mortal(newSViv(fill+1)));
786 call_method("STORESIZE", G_SCALAR|G_DISCARD);
792 if (fill <= AvMAX(av)) {
793 I32 key = AvFILLp(av);
794 SV** const ary = AvARRAY(av);
798 SvREFCNT_dec(ary[key]);
799 ary[key--] = &PL_sv_undef;
804 ary[++key] = &PL_sv_undef;
812 (void)av_store(av,fill,&PL_sv_undef);
816 =for apidoc av_delete
818 Deletes the element indexed by C<key> from the array. Returns the
819 deleted element. If C<flags> equals C<G_DISCARD>, the element is freed
820 and null is returned.
825 Perl_av_delete(pTHX_ AV *av, I32 key, I32 flags)
833 Perl_croak(aTHX_ PL_no_modify);
835 if (SvRMAGICAL(av)) {
836 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
837 if ((tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata))) {
838 /* Handle negative array indices 20020222 MJD */
841 unsigned adjust_index = 1;
843 SV * const * const negative_indices_glob =
844 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
846 NEGATIVE_INDICES_VAR, 16, 0);
847 if (negative_indices_glob
848 && SvTRUE(GvSV(*negative_indices_glob)))
852 key += AvFILL(av) + 1;
857 svp = av_fetch(av, key, TRUE);
861 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
862 sv_unmagic(sv, PERL_MAGIC_tiedelem); /* No longer an element */
871 key += AvFILL(av) + 1;
876 if (key > AvFILLp(av))
879 if (!AvREAL(av) && AvREIFY(av))
881 sv = AvARRAY(av)[key];
882 if (key == AvFILLp(av)) {
883 AvARRAY(av)[key] = &PL_sv_undef;
886 } while (--key >= 0 && AvARRAY(av)[key] == &PL_sv_undef);
889 AvARRAY(av)[key] = &PL_sv_undef;
893 if (flags & G_DISCARD) {
903 =for apidoc av_exists
905 Returns true if the element indexed by C<key> has been initialized.
907 This relies on the fact that uninitialized array elements are set to
913 Perl_av_exists(pTHX_ AV *av, I32 key)
918 if (SvRMAGICAL(av)) {
919 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
920 if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) {
921 SV * const sv = sv_newmortal();
923 /* Handle negative array indices 20020222 MJD */
925 unsigned adjust_index = 1;
927 SV * const * const negative_indices_glob =
928 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
930 NEGATIVE_INDICES_VAR, 16, 0);
931 if (negative_indices_glob
932 && SvTRUE(GvSV(*negative_indices_glob)))
936 key += AvFILL(av) + 1;
942 mg_copy((SV*)av, sv, 0, key);
943 mg = mg_find(sv, PERL_MAGIC_tiedelem);
945 magic_existspack(sv, mg);
946 return (bool)SvTRUE(sv);
953 key += AvFILL(av) + 1;
958 if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef
968 Perl_av_arylen_p(pTHX_ AV *av) {
974 mg = mg_find((SV*)av, PERL_MAGIC_arylen_p);
977 mg = sv_magicext((SV*)av, 0, PERL_MAGIC_arylen_p, &PL_vtbl_arylen_p,
980 /* sv_magicext won't set this for us because we pass in a NULL obj */
981 mg->mg_flags |= MGf_REFCOUNTED;
983 return &(mg->mg_obj);
988 * c-indentation-style: bsd
990 * indent-tabs-mode: t
993 * ex: set ts=8 sts=4 sw=4 noet: