3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 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)
30 PERL_ARGS_ASSERT_AV_REIFY;
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)
69 PERL_ARGS_ASSERT_AV_EXTEND;
71 mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied);
76 PUSHSTACKi(PERLSI_MAGIC);
79 PUSHs(SvTIED_obj((SV*)av, mg));
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)
120 #ifdef Perl_safesysmalloc_size
121 newmax = Perl_safesysmalloc_size((void*)AvALLOC(av)) /
127 newmax = key + AvMAX(av) / 5;
129 MEM_WRAP_CHECK_1(newmax+1, SV*, oom_array_extend);
130 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
131 Renew(AvALLOC(av),newmax+1, SV*);
133 bytes = (newmax + 1) * sizeof(SV*);
134 #define MALLOC_OVERHEAD 16
135 itmp = MALLOC_OVERHEAD;
136 while ((MEM_SIZE)(itmp - MALLOC_OVERHEAD) < bytes)
138 itmp -= MALLOC_OVERHEAD;
140 assert(itmp > newmax);
142 assert(newmax >= AvMAX(av));
143 Newx(ary, newmax+1, SV*);
144 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
146 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
148 Safefree(AvALLOC(av));
151 #ifdef Perl_safesysmalloc_size
154 ary = AvALLOC(av) + AvMAX(av) + 1;
155 tmp = newmax - AvMAX(av);
156 if (av == PL_curstack) { /* Oops, grew stack (via av_store()?) */
157 PL_stack_sp = AvALLOC(av) + (PL_stack_sp - PL_stack_base);
158 PL_stack_base = AvALLOC(av);
159 PL_stack_max = PL_stack_base + newmax;
163 newmax = key < 3 ? 3 : key;
164 MEM_WRAP_CHECK_1(newmax+1, SV*, oom_array_extend);
165 Newx(AvALLOC(av), newmax+1, SV*);
166 ary = AvALLOC(av) + 1;
168 AvALLOC(av)[0] = &PL_sv_undef; /* For the stacks */
172 ary[--tmp] = &PL_sv_undef;
175 AvARRAY(av) = AvALLOC(av);
184 Returns the SV at the specified index in the array. The C<key> is the
185 index. If C<lval> is set then the fetch will be part of a store. Check
186 that the return value is non-null before dereferencing it to a C<SV*>.
188 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
189 more information on how to use this function on tied arrays.
195 Perl_av_fetch(pTHX_ register AV *av, I32 key, I32 lval)
199 PERL_ARGS_ASSERT_AV_FETCH;
201 if (SvRMAGICAL(av)) {
202 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
203 if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) {
206 I32 adjust_index = 1;
208 /* Handle negative array indices 20020222 MJD */
209 SV * const * const negative_indices_glob =
210 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av, tied_magic))),
211 NEGATIVE_INDICES_VAR, 16, 0);
213 if (negative_indices_glob && SvTRUE(GvSV(*negative_indices_glob)))
218 key += AvFILL(av) + 1;
225 sv_upgrade(sv, SVt_PVLV);
226 mg_copy((SV*)av, sv, 0, key);
228 LvTARG(sv) = sv; /* fake (SV**) */
229 return &(LvTARG(sv));
234 key += AvFILL(av) + 1;
239 if (key > AvFILLp(av)) {
242 return av_store(av,key,newSV(0));
244 if (AvARRAY(av)[key] == &PL_sv_undef) {
247 return av_store(av,key,newSV(0));
251 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
252 || SvIS_FREED(AvARRAY(av)[key]))) {
253 AvARRAY(av)[key] = &PL_sv_undef; /* 1/2 reify */
256 return &AvARRAY(av)[key];
262 Stores an SV in an array. The array index is specified as C<key>. The
263 return value will be NULL if the operation failed or if the value did not
264 need to be actually stored within the array (as in the case of tied
265 arrays). Otherwise it can be dereferenced to get the original C<SV*>. Note
266 that the caller is responsible for suitably incrementing the reference
267 count of C<val> before the call, and decrementing it if the function
270 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
271 more information on how to use this function on tied arrays.
277 Perl_av_store(pTHX_ register AV *av, I32 key, SV *val)
282 PERL_ARGS_ASSERT_AV_STORE;
284 /* S_regclass relies on being able to pass in a NULL sv
285 (unicode_alternate may be NULL).
291 if (SvRMAGICAL(av)) {
292 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
294 /* Handle negative array indices 20020222 MJD */
296 bool adjust_index = 1;
297 SV * const * const negative_indices_glob =
298 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
300 NEGATIVE_INDICES_VAR, 16, 0);
301 if (negative_indices_glob
302 && SvTRUE(GvSV(*negative_indices_glob)))
305 key += AvFILL(av) + 1;
310 if (val != &PL_sv_undef) {
311 mg_copy((SV*)av, val, 0, key);
319 key += AvFILL(av) + 1;
324 if (SvREADONLY(av) && key >= AvFILL(av))
325 Perl_croak(aTHX_ PL_no_modify);
327 if (!AvREAL(av) && AvREIFY(av))
332 if (AvFILLp(av) < key) {
334 if (av == PL_curstack && key > PL_stack_sp - PL_stack_base)
335 PL_stack_sp = PL_stack_base + key; /* XPUSH in disguise */
337 ary[++AvFILLp(av)] = &PL_sv_undef;
338 } while (AvFILLp(av) < key);
343 SvREFCNT_dec(ary[key]);
345 if (SvSMAGICAL(av)) {
346 const MAGIC* const mg = SvMAGIC(av);
347 if (val != &PL_sv_undef) {
348 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
350 if (PL_delaymagic && mg->mg_type == PERL_MAGIC_isa)
351 PL_delaymagic |= DM_ARRAY;
361 Creates a new AV and populates it with a list of SVs. The SVs are copied
362 into the array, so they may be freed after the call to av_make. The new AV
363 will have a reference count of 1.
369 Perl_av_make(pTHX_ register I32 size, register SV **strp)
371 register AV * const av = (AV*)newSV_type(SVt_PVAV);
372 /* sv_upgrade does AvREAL_only() */
373 PERL_ARGS_ASSERT_AV_MAKE;
374 if (size) { /* "defined" was returning undef for size==0 anyway. */
380 AvFILLp(av) = AvMAX(av) = size - 1;
381 for (i = 0; i < size; i++) {
384 sv_setsv(ary[i], *strp);
394 Clears an array, making it empty. Does not free the memory used by the
401 Perl_av_clear(pTHX_ register AV *av)
406 PERL_ARGS_ASSERT_AV_CLEAR;
408 if (SvREFCNT(av) == 0 && ckWARN_d(WARN_DEBUGGING)) {
409 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), "Attempt to clear deleted array");
414 Perl_croak(aTHX_ PL_no_modify);
416 /* Give any tie a chance to cleanup first */
417 if (SvRMAGICAL(av)) {
418 const MAGIC* const mg = SvMAGIC(av);
419 if (PL_delaymagic && mg->mg_type == PERL_MAGIC_isa)
420 PL_delaymagic |= DM_ARRAY;
429 SV** const ary = AvARRAY(av);
430 I32 index = AvFILLp(av) + 1;
432 SV * const sv = ary[--index];
433 /* undef the slot before freeing the value, because a
434 * destructor might try to modify this array */
435 ary[index] = &PL_sv_undef;
439 extra = AvARRAY(av) - AvALLOC(av);
442 AvARRAY(av) = AvALLOC(av);
451 Undefines the array. Frees the memory used by the array itself.
457 Perl_av_undef(pTHX_ register AV *av)
459 PERL_ARGS_ASSERT_AV_UNDEF;
461 /* Give any tie a chance to cleanup first */
462 if (SvTIED_mg((SV*)av, PERL_MAGIC_tied))
466 register I32 key = AvFILLp(av) + 1;
468 SvREFCNT_dec(AvARRAY(av)[--key]);
471 Safefree(AvALLOC(av));
474 AvMAX(av) = AvFILLp(av) = -1;
476 if(SvRMAGICAL(av)) mg_clear((SV*)av);
481 =for apidoc av_create_and_push
483 Push an SV onto the end of the array, creating the array if necessary.
484 A small internal helper function to remove a commonly duplicated idiom.
490 Perl_av_create_and_push(pTHX_ AV **const avp, SV *const val)
492 PERL_ARGS_ASSERT_AV_CREATE_AND_PUSH;
501 Pushes an SV onto the end of the array. The array will grow automatically
502 to accommodate the addition.
508 Perl_av_push(pTHX_ register AV *av, SV *val)
513 PERL_ARGS_ASSERT_AV_PUSH;
516 Perl_croak(aTHX_ PL_no_modify);
518 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
520 PUSHSTACKi(PERLSI_MAGIC);
523 PUSHs(SvTIED_obj((SV*)av, mg));
527 call_method("PUSH", G_SCALAR|G_DISCARD);
532 av_store(av,AvFILLp(av)+1,val);
538 Pops an SV off the end of the array. Returns C<&PL_sv_undef> if the array
545 Perl_av_pop(pTHX_ register AV *av)
551 PERL_ARGS_ASSERT_AV_POP;
554 Perl_croak(aTHX_ PL_no_modify);
555 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
557 PUSHSTACKi(PERLSI_MAGIC);
559 XPUSHs(SvTIED_obj((SV*)av, mg));
562 if (call_method("POP", G_SCALAR)) {
563 retval = newSVsv(*PL_stack_sp--);
565 retval = &PL_sv_undef;
573 retval = AvARRAY(av)[AvFILLp(av)];
574 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
582 =for apidoc av_create_and_unshift_one
584 Unshifts an SV onto the beginning of the array, creating the array if
586 A small internal helper function to remove a commonly duplicated idiom.
592 Perl_av_create_and_unshift_one(pTHX_ AV **const avp, SV *const val)
594 PERL_ARGS_ASSERT_AV_CREATE_AND_UNSHIFT_ONE;
598 return av_store(*avp, 0, val);
602 =for apidoc av_unshift
604 Unshift the given number of C<undef> values onto the beginning of the
605 array. The array will grow automatically to accommodate the addition. You
606 must then use C<av_store> to assign values to these new elements.
612 Perl_av_unshift(pTHX_ register AV *av, register I32 num)
618 PERL_ARGS_ASSERT_AV_UNSHIFT;
621 Perl_croak(aTHX_ PL_no_modify);
623 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
625 PUSHSTACKi(PERLSI_MAGIC);
628 PUSHs(SvTIED_obj((SV*)av, mg));
634 call_method("UNSHIFT", G_SCALAR|G_DISCARD);
642 if (!AvREAL(av) && AvREIFY(av))
644 i = AvARRAY(av) - AvALLOC(av);
652 AvARRAY(av) = AvARRAY(av) - i;
656 const I32 i = AvFILLp(av);
657 /* Create extra elements */
658 const I32 slide = i > 0 ? i : 0;
660 av_extend(av, i + num);
663 Move(ary, ary + num, i + 1, SV*);
665 ary[--num] = &PL_sv_undef;
667 /* Make extra elements into a buffer */
669 AvFILLp(av) -= slide;
670 AvARRAY(av) = AvARRAY(av) + slide;
677 Shifts an SV off the beginning of the array.
683 Perl_av_shift(pTHX_ register AV *av)
689 PERL_ARGS_ASSERT_AV_SHIFT;
692 Perl_croak(aTHX_ PL_no_modify);
693 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
695 PUSHSTACKi(PERLSI_MAGIC);
697 XPUSHs(SvTIED_obj((SV*)av, mg));
700 if (call_method("SHIFT", G_SCALAR)) {
701 retval = newSVsv(*PL_stack_sp--);
703 retval = &PL_sv_undef;
711 retval = *AvARRAY(av);
713 *AvARRAY(av) = &PL_sv_undef;
714 AvARRAY(av) = AvARRAY(av) + 1;
725 Returns the highest index in the array. The number of elements in the
726 array is C<av_len(av) + 1>. Returns -1 if the array is empty.
732 Perl_av_len(pTHX_ register const AV *av)
734 PERL_ARGS_ASSERT_AV_LEN;
741 Set the highest index in the array to the given number, equivalent to
742 Perl's C<$#array = $fill;>.
744 The number of elements in the an array will be C<fill + 1> after
745 av_fill() returns. If the array was previously shorter then the
746 additional elements appended are set to C<PL_sv_undef>. If the array
747 was longer, then the excess elements are freed. C<av_fill(av, -1)> is
748 the same as C<av_clear(av)>.
753 Perl_av_fill(pTHX_ register AV *av, I32 fill)
758 PERL_ARGS_ASSERT_AV_FILL;
762 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
766 PUSHSTACKi(PERLSI_MAGIC);
769 PUSHs(SvTIED_obj((SV*)av, mg));
772 call_method("STORESIZE", G_SCALAR|G_DISCARD);
778 if (fill <= AvMAX(av)) {
779 I32 key = AvFILLp(av);
780 SV** const ary = AvARRAY(av);
784 SvREFCNT_dec(ary[key]);
785 ary[key--] = &PL_sv_undef;
790 ary[++key] = &PL_sv_undef;
798 (void)av_store(av,fill,&PL_sv_undef);
802 =for apidoc av_delete
804 Deletes the element indexed by C<key> from the array. Returns the
805 deleted element. If C<flags> equals C<G_DISCARD>, the element is freed
806 and null is returned.
811 Perl_av_delete(pTHX_ AV *av, I32 key, I32 flags)
816 PERL_ARGS_ASSERT_AV_DELETE;
819 Perl_croak(aTHX_ PL_no_modify);
821 if (SvRMAGICAL(av)) {
822 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
823 if ((tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata))) {
824 /* Handle negative array indices 20020222 MJD */
827 unsigned adjust_index = 1;
829 SV * const * const negative_indices_glob =
830 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
832 NEGATIVE_INDICES_VAR, 16, 0);
833 if (negative_indices_glob
834 && SvTRUE(GvSV(*negative_indices_glob)))
838 key += AvFILL(av) + 1;
843 svp = av_fetch(av, key, TRUE);
847 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
848 sv_unmagic(sv, PERL_MAGIC_tiedelem); /* No longer an element */
857 key += AvFILL(av) + 1;
862 if (key > AvFILLp(av))
865 if (!AvREAL(av) && AvREIFY(av))
867 sv = AvARRAY(av)[key];
868 if (key == AvFILLp(av)) {
869 AvARRAY(av)[key] = &PL_sv_undef;
872 } while (--key >= 0 && AvARRAY(av)[key] == &PL_sv_undef);
875 AvARRAY(av)[key] = &PL_sv_undef;
879 if (flags & G_DISCARD) {
889 =for apidoc av_exists
891 Returns true if the element indexed by C<key> has been initialized.
893 This relies on the fact that uninitialized array elements are set to
899 Perl_av_exists(pTHX_ AV *av, I32 key)
902 PERL_ARGS_ASSERT_AV_EXISTS;
904 if (SvRMAGICAL(av)) {
905 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
906 if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) {
907 SV * const sv = sv_newmortal();
909 /* Handle negative array indices 20020222 MJD */
911 unsigned adjust_index = 1;
913 SV * const * const negative_indices_glob =
914 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
916 NEGATIVE_INDICES_VAR, 16, 0);
917 if (negative_indices_glob
918 && SvTRUE(GvSV(*negative_indices_glob)))
922 key += AvFILL(av) + 1;
928 mg_copy((SV*)av, sv, 0, key);
929 mg = mg_find(sv, PERL_MAGIC_tiedelem);
931 magic_existspack(sv, mg);
932 return (bool)SvTRUE(sv);
939 key += AvFILL(av) + 1;
944 if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef
954 S_get_aux_mg(pTHX_ AV *av) {
958 PERL_ARGS_ASSERT_GET_AUX_MG;
960 mg = mg_find((SV*)av, PERL_MAGIC_arylen_p);
963 mg = sv_magicext((SV*)av, 0, PERL_MAGIC_arylen_p, &PL_vtbl_arylen_p,
966 /* sv_magicext won't set this for us because we pass in a NULL obj */
967 mg->mg_flags |= MGf_REFCOUNTED;
973 Perl_av_arylen_p(pTHX_ AV *av) {
974 MAGIC *const mg = get_aux_mg(av);
976 PERL_ARGS_ASSERT_AV_ARYLEN_P;
978 return &(mg->mg_obj);
982 Perl_av_iter_p(pTHX_ AV *av) {
983 MAGIC *const mg = get_aux_mg(av);
985 PERL_ARGS_ASSERT_AV_ITER_P;
987 #if IVSIZE == I32SIZE
988 return (IV *)&(mg->mg_len);
994 mg->mg_ptr = (char *) temp;
996 return (IV *)mg->mg_ptr;
1002 * c-indentation-style: bsd
1004 * indent-tabs-mode: t
1007 * ex: set ts=8 sts=4 sw=4 noet: