3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005 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)
32 if (SvTIED_mg((SV*)av, PERL_MAGIC_tied) && ckWARN_d(WARN_DEBUGGING))
33 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), "av_reify called on tied array");
36 while (key > AvFILLp(av) + 1)
37 AvARRAY(av)[--key] = &PL_sv_undef;
39 SV * const sv = AvARRAY(av)[--key];
41 if (sv != &PL_sv_undef)
42 (void)SvREFCNT_inc(sv);
44 key = AvARRAY(av) - AvALLOC(av);
46 AvALLOC(av)[--key] = &PL_sv_undef;
54 Pre-extend an array. The C<key> is the index to which the array should be
61 Perl_av_extend(pTHX_ AV *av, I32 key)
64 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
68 PUSHSTACKi(PERLSI_MAGIC);
71 PUSHs(SvTIED_obj((SV*)av, mg));
72 PUSHs(sv_2mortal(newSViv(key+1)));
74 call_method("EXTEND", G_SCALAR|G_DISCARD);
80 if (key > AvMAX(av)) {
85 if (AvALLOC(av) != AvARRAY(av)) {
86 ary = AvALLOC(av) + AvFILLp(av) + 1;
87 tmp = AvARRAY(av) - AvALLOC(av);
88 Move(AvARRAY(av), AvALLOC(av), AvFILLp(av)+1, SV*);
90 SvPV_set(av, (char*)AvALLOC(av));
93 ary[--tmp] = &PL_sv_undef;
96 if (key > AvMAX(av) - 10) {
97 newmax = key + AvMAX(av);
102 #ifdef PERL_MALLOC_WRAP
103 static const char oom_array_extend[] =
104 "Out of memory during array extend"; /* Duplicated in pp_hot.c */
108 #if !defined(STRANGE_MALLOC) && !defined(MYMALLOC)
114 newmax = malloced_size((void*)AvALLOC(av))/sizeof(SV*) - 1;
119 newmax = key + AvMAX(av) / 5;
121 MEM_WRAP_CHECK_1(newmax+1, SV*, oom_array_extend);
122 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
123 Renew(AvALLOC(av),newmax+1, SV*);
125 bytes = (newmax + 1) * sizeof(SV*);
126 #define MALLOC_OVERHEAD 16
127 itmp = MALLOC_OVERHEAD;
128 while ((MEM_SIZE)(itmp - MALLOC_OVERHEAD) < bytes)
130 itmp -= MALLOC_OVERHEAD;
132 assert(itmp > newmax);
134 assert(newmax >= AvMAX(av));
135 Newx(ary, newmax+1, SV*);
136 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
138 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
140 Safefree(AvALLOC(av));
146 ary = AvALLOC(av) + AvMAX(av) + 1;
147 tmp = newmax - AvMAX(av);
148 if (av == PL_curstack) { /* Oops, grew stack (via av_store()?) */
149 PL_stack_sp = AvALLOC(av) + (PL_stack_sp - PL_stack_base);
150 PL_stack_base = AvALLOC(av);
151 PL_stack_max = PL_stack_base + newmax;
155 newmax = key < 3 ? 3 : key;
156 MEM_WRAP_CHECK_1(newmax+1, SV*, oom_array_extend);
157 Newx(AvALLOC(av), newmax+1, SV*);
158 ary = AvALLOC(av) + 1;
160 AvALLOC(av)[0] = &PL_sv_undef; /* For the stacks */
164 ary[--tmp] = &PL_sv_undef;
167 SvPV_set(av, (char*)AvALLOC(av));
176 Returns the SV at the specified index in the array. The C<key> is the
177 index. If C<lval> is set then the fetch will be part of a store. Check
178 that the return value is non-null before dereferencing it to a C<SV*>.
180 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
181 more information on how to use this function on tied arrays.
187 Perl_av_fetch(pTHX_ register AV *av, I32 key, I32 lval)
194 if (SvRMAGICAL(av)) {
195 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
196 if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) {
197 U32 adjust_index = 1;
199 if (tied_magic && key < 0) {
200 /* Handle negative array indices 20020222 MJD */
201 SV **negative_indices_glob =
202 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
204 NEGATIVE_INDICES_VAR, 16, 0);
206 if (negative_indices_glob
207 && SvTRUE(GvSV(*negative_indices_glob)))
211 if (key < 0 && adjust_index) {
212 key += AvFILL(av) + 1;
218 sv_upgrade(sv, SVt_PVLV);
219 mg_copy((SV*)av, sv, 0, key);
221 LvTARG(sv) = sv; /* fake (SV**) */
222 return &(LvTARG(sv));
227 key += AvFILL(av) + 1;
232 if (key > AvFILLp(av)) {
236 return av_store(av,key,sv);
238 if (AvARRAY(av)[key] == &PL_sv_undef) {
242 return av_store(av,key,sv);
247 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
248 || SvTYPE(AvARRAY(av)[key]) == SVTYPEMASK)) {
249 AvARRAY(av)[key] = &PL_sv_undef; /* 1/2 reify */
252 return &AvARRAY(av)[key];
258 Stores an SV in an array. The array index is specified as C<key>. The
259 return value will be NULL if the operation failed or if the value did not
260 need to be actually stored within the array (as in the case of tied
261 arrays). Otherwise it can be dereferenced to get the original C<SV*>. Note
262 that the caller is responsible for suitably incrementing the reference
263 count of C<val> before the call, and decrementing it if the function
266 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
267 more information on how to use this function on tied arrays.
273 Perl_av_store(pTHX_ register AV *av, I32 key, SV *val)
282 if (SvRMAGICAL(av)) {
283 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
285 /* Handle negative array indices 20020222 MJD */
287 unsigned adjust_index = 1;
288 SV **negative_indices_glob =
289 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
291 NEGATIVE_INDICES_VAR, 16, 0);
292 if (negative_indices_glob
293 && SvTRUE(GvSV(*negative_indices_glob)))
296 key += AvFILL(av) + 1;
301 if (val != &PL_sv_undef) {
302 mg_copy((SV*)av, val, 0, key);
310 key += AvFILL(av) + 1;
315 if (SvREADONLY(av) && key >= AvFILL(av))
316 Perl_croak(aTHX_ PL_no_modify);
318 if (!AvREAL(av) && AvREIFY(av))
323 if (AvFILLp(av) < key) {
325 if (av == PL_curstack && key > PL_stack_sp - PL_stack_base)
326 PL_stack_sp = PL_stack_base + key; /* XPUSH in disguise */
328 ary[++AvFILLp(av)] = &PL_sv_undef;
329 while (AvFILLp(av) < key);
334 SvREFCNT_dec(ary[key]);
336 if (SvSMAGICAL(av)) {
337 if (val != &PL_sv_undef) {
338 MAGIC* mg = SvMAGIC(av);
339 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
349 Creates a new AV. The reference count is set to 1.
359 av = (AV*)NEWSV(3,0);
360 sv_upgrade((SV *)av, SVt_PVAV);
361 /* sv_upgrade does AvREAL_only() */
363 SvPV_set(av, (char*)0);
364 AvMAX(av) = AvFILLp(av) = -1;
371 Creates a new AV and populates it with a list of SVs. The SVs are copied
372 into the array, so they may be freed after the call to av_make. The new AV
373 will have a reference count of 1.
379 Perl_av_make(pTHX_ register I32 size, register SV **strp)
383 av = (AV*)NEWSV(8,0);
384 sv_upgrade((SV *) av,SVt_PVAV);
385 /* sv_upgrade does AvREAL_only() */
386 if (size) { /* "defined" was returning undef for size==0 anyway. */
391 SvPV_set(av, (char*)ary);
392 AvFILLp(av) = size - 1;
393 AvMAX(av) = size - 1;
394 for (i = 0; i < size; i++) {
397 sv_setsv(ary[i], *strp);
405 Perl_av_fake(pTHX_ register I32 size, register SV **strp)
410 av = (AV*)NEWSV(9,0);
411 sv_upgrade((SV *)av, SVt_PVAV);
412 Newx(ary,size+1,SV*);
414 Copy(strp,ary,size,SV*);
416 SvPV_set(av, (char*)ary);
417 AvFILLp(av) = size - 1;
418 AvMAX(av) = size - 1;
430 Clears an array, making it empty. Does not free the memory used by the
437 Perl_av_clear(pTHX_ register AV *av)
442 if (SvREFCNT(av) == 0 && ckWARN_d(WARN_DEBUGGING)) {
443 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), "Attempt to clear deleted array");
450 Perl_croak(aTHX_ PL_no_modify);
452 /* Give any tie a chance to cleanup first */
460 SV** ary = AvARRAY(av);
461 key = AvFILLp(av) + 1;
463 SV * sv = ary[--key];
464 /* undef the slot before freeing the value, because a
465 * destructor might try to modify this arrray */
466 ary[key] = &PL_sv_undef;
470 if ((key = AvARRAY(av) - AvALLOC(av))) {
472 SvPV_set(av, (char*)AvALLOC(av));
481 Undefines the array. Frees the memory used by the array itself.
487 Perl_av_undef(pTHX_ register AV *av)
492 /* Give any tie a chance to cleanup first */
493 if (SvTIED_mg((SV*)av, PERL_MAGIC_tied))
494 av_fill(av, -1); /* mg_clear() ? */
497 register I32 key = AvFILLp(av) + 1;
499 SvREFCNT_dec(AvARRAY(av)[--key]);
501 Safefree(AvALLOC(av));
503 SvPV_set(av, (char*)0);
504 AvMAX(av) = AvFILLp(av) = -1;
505 /* It's in magic - it must already be gone. */
506 assert (!AvARYLEN(av));
512 Pushes an SV onto the end of the array. The array will grow automatically
513 to accommodate the addition.
519 Perl_av_push(pTHX_ register AV *av, SV *val)
526 Perl_croak(aTHX_ PL_no_modify);
528 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
530 PUSHSTACKi(PERLSI_MAGIC);
533 PUSHs(SvTIED_obj((SV*)av, mg));
537 call_method("PUSH", G_SCALAR|G_DISCARD);
542 av_store(av,AvFILLp(av)+1,val);
548 Pops an SV off the end of the array. Returns C<&PL_sv_undef> if the array
555 Perl_av_pop(pTHX_ register AV *av)
564 Perl_croak(aTHX_ PL_no_modify);
565 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
567 PUSHSTACKi(PERLSI_MAGIC);
569 XPUSHs(SvTIED_obj((SV*)av, mg));
572 if (call_method("POP", G_SCALAR)) {
573 retval = newSVsv(*PL_stack_sp--);
575 retval = &PL_sv_undef;
583 retval = AvARRAY(av)[AvFILLp(av)];
584 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
591 =for apidoc av_unshift
593 Unshift the given number of C<undef> values onto the beginning of the
594 array. The array will grow automatically to accommodate the addition. You
595 must then use C<av_store> to assign values to these new elements.
601 Perl_av_unshift(pTHX_ register AV *av, register I32 num)
610 Perl_croak(aTHX_ PL_no_modify);
612 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
614 PUSHSTACKi(PERLSI_MAGIC);
617 PUSHs(SvTIED_obj((SV*)av, mg));
623 call_method("UNSHIFT", G_SCALAR|G_DISCARD);
631 if (!AvREAL(av) && AvREIFY(av))
633 i = AvARRAY(av) - AvALLOC(av);
641 SvPV_set(av, (char*)(AvARRAY(av) - i));
647 /* Create extra elements */
648 slide = i > 0 ? i : 0;
650 av_extend(av, i + num);
653 Move(ary, ary + num, i + 1, SV*);
655 ary[--num] = &PL_sv_undef;
657 /* Make extra elements into a buffer */
659 AvFILLp(av) -= slide;
660 SvPV_set(av, (char*)(AvARRAY(av) + slide));
667 Shifts an SV off the beginning of the array.
673 Perl_av_shift(pTHX_ register AV *av)
682 Perl_croak(aTHX_ PL_no_modify);
683 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
685 PUSHSTACKi(PERLSI_MAGIC);
687 XPUSHs(SvTIED_obj((SV*)av, mg));
690 if (call_method("SHIFT", G_SCALAR)) {
691 retval = newSVsv(*PL_stack_sp--);
693 retval = &PL_sv_undef;
701 retval = *AvARRAY(av);
703 *AvARRAY(av) = &PL_sv_undef;
704 SvPV_set(av, (char*)(AvARRAY(av) + 1));
715 Returns the highest index in the array. Returns -1 if the array is
722 Perl_av_len(pTHX_ const register AV *av)
730 Ensure than an array has a given number of elements, equivalent to
731 Perl's C<$#array = $fill;>.
736 Perl_av_fill(pTHX_ register AV *av, I32 fill)
741 Perl_croak(aTHX_ "panic: null array");
744 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
748 PUSHSTACKi(PERLSI_MAGIC);
751 PUSHs(SvTIED_obj((SV*)av, mg));
752 PUSHs(sv_2mortal(newSViv(fill+1)));
754 call_method("STORESIZE", G_SCALAR|G_DISCARD);
760 if (fill <= AvMAX(av)) {
761 I32 key = AvFILLp(av);
762 SV** ary = AvARRAY(av);
766 SvREFCNT_dec(ary[key]);
767 ary[key--] = &PL_sv_undef;
772 ary[++key] = &PL_sv_undef;
780 (void)av_store(av,fill,&PL_sv_undef);
784 =for apidoc av_delete
786 Deletes the element indexed by C<key> from the array. Returns the
787 deleted element. If C<flags> equals C<G_DISCARD>, the element is freed
788 and null is returned.
793 Perl_av_delete(pTHX_ AV *av, I32 key, I32 flags)
800 Perl_croak(aTHX_ PL_no_modify);
802 if (SvRMAGICAL(av)) {
803 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
804 if ((tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata))) {
805 /* Handle negative array indices 20020222 MJD */
808 unsigned adjust_index = 1;
810 SV **negative_indices_glob =
811 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
813 NEGATIVE_INDICES_VAR, 16, 0);
814 if (negative_indices_glob
815 && SvTRUE(GvSV(*negative_indices_glob)))
819 key += AvFILL(av) + 1;
824 svp = av_fetch(av, key, TRUE);
828 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
829 sv_unmagic(sv, PERL_MAGIC_tiedelem); /* No longer an element */
838 key += AvFILL(av) + 1;
843 if (key > AvFILLp(av))
846 if (!AvREAL(av) && AvREIFY(av))
848 sv = AvARRAY(av)[key];
849 if (key == AvFILLp(av)) {
850 AvARRAY(av)[key] = &PL_sv_undef;
853 } while (--key >= 0 && AvARRAY(av)[key] == &PL_sv_undef);
856 AvARRAY(av)[key] = &PL_sv_undef;
860 if (flags & G_DISCARD) {
870 =for apidoc av_exists
872 Returns true if the element indexed by C<key> has been initialized.
874 This relies on the fact that uninitialized array elements are set to
880 Perl_av_exists(pTHX_ AV *av, I32 key)
886 if (SvRMAGICAL(av)) {
887 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
888 if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) {
889 SV *sv = sv_newmortal();
891 /* Handle negative array indices 20020222 MJD */
893 unsigned adjust_index = 1;
895 SV **negative_indices_glob =
896 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
898 NEGATIVE_INDICES_VAR, 16, 0);
899 if (negative_indices_glob
900 && SvTRUE(GvSV(*negative_indices_glob)))
904 key += AvFILL(av) + 1;
910 mg_copy((SV*)av, sv, 0, key);
911 mg = mg_find(sv, PERL_MAGIC_tiedelem);
913 magic_existspack(sv, mg);
914 return (bool)SvTRUE(sv);
921 key += AvFILL(av) + 1;
926 if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef
936 Perl_av_arylen_p(pTHX_ AV *av) {
938 MAGIC *mg = mg_find((SV*)av, PERL_MAGIC_arylen_p);
941 mg = sv_magicext((SV*)av, 0, PERL_MAGIC_arylen_p, &PL_vtbl_arylen_p,
945 Perl_die(aTHX_ "panic: av_arylen_p");
947 /* sv_magicext won't set this for us because we pass in a NULL obj */
948 mg->mg_flags |= MGf_REFCOUNTED;
950 return &(mg->mg_obj);
955 * c-indentation-style: bsd
957 * indent-tabs-mode: t
960 * ex: set ts=8 sts=4 sw=4 noet: