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*);
415 AvFLAGS(av) = AVf_REIFY;
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 /* Need to check SvMAGICAL, as during global destruction it may be that
506 AvARYLEN(av) has been freed before av, and hence the SvANY() pointer
507 is now part of the linked list of SV heads, rather than pointing to
508 the original body. */
509 /* FIXME - audit the code for other bugs like this one. */
510 if (AvARYLEN(av) && SvMAGICAL(AvARYLEN(av))) {
511 MAGIC *mg = mg_find (AvARYLEN(av), PERL_MAGIC_arylen);
514 /* arylen scalar holds a pointer back to the array, but doesn't
515 own a reference. Hence the we (the array) are about to go away
516 with it still pointing at us. Clear its pointer, else it would
517 be pointing at free memory. See the comment in sv_magic about
518 reference loops, and why it can't own a reference to us. */
522 SvREFCNT_dec(AvARYLEN(av));
530 Pushes an SV onto the end of the array. The array will grow automatically
531 to accommodate the addition.
537 Perl_av_push(pTHX_ register AV *av, SV *val)
543 Perl_croak(aTHX_ PL_no_modify);
545 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
547 PUSHSTACKi(PERLSI_MAGIC);
550 PUSHs(SvTIED_obj((SV*)av, mg));
554 call_method("PUSH", G_SCALAR|G_DISCARD);
559 av_store(av,AvFILLp(av)+1,val);
565 Pops an SV off the end of the array. Returns C<&PL_sv_undef> if the array
572 Perl_av_pop(pTHX_ register AV *av)
580 Perl_croak(aTHX_ PL_no_modify);
581 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
583 PUSHSTACKi(PERLSI_MAGIC);
585 XPUSHs(SvTIED_obj((SV*)av, mg));
588 if (call_method("POP", G_SCALAR)) {
589 retval = newSVsv(*PL_stack_sp--);
591 retval = &PL_sv_undef;
599 retval = AvARRAY(av)[AvFILLp(av)];
600 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
607 =for apidoc av_unshift
609 Unshift the given number of C<undef> values onto the beginning of the
610 array. The array will grow automatically to accommodate the addition. You
611 must then use C<av_store> to assign values to these new elements.
617 Perl_av_unshift(pTHX_ register AV *av, register I32 num)
625 Perl_croak(aTHX_ PL_no_modify);
627 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
629 PUSHSTACKi(PERLSI_MAGIC);
632 PUSHs(SvTIED_obj((SV*)av, mg));
638 call_method("UNSHIFT", G_SCALAR|G_DISCARD);
646 if (!AvREAL(av) && AvREIFY(av))
648 i = AvARRAY(av) - AvALLOC(av);
656 SvPV_set(av, (char*)(AvARRAY(av) - i));
662 /* Create extra elements */
663 slide = i > 0 ? i : 0;
665 av_extend(av, i + num);
668 Move(ary, ary + num, i + 1, SV*);
670 ary[--num] = &PL_sv_undef;
672 /* Make extra elements into a buffer */
674 AvFILLp(av) -= slide;
675 SvPV_set(av, (char*)(AvARRAY(av) + slide));
682 Shifts an SV off the beginning of the array.
688 Perl_av_shift(pTHX_ register AV *av)
696 Perl_croak(aTHX_ PL_no_modify);
697 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
699 PUSHSTACKi(PERLSI_MAGIC);
701 XPUSHs(SvTIED_obj((SV*)av, mg));
704 if (call_method("SHIFT", G_SCALAR)) {
705 retval = newSVsv(*PL_stack_sp--);
707 retval = &PL_sv_undef;
715 retval = *AvARRAY(av);
717 *AvARRAY(av) = &PL_sv_undef;
718 SvPV_set(av, (char*)(AvARRAY(av) + 1));
729 Returns the highest index in the array. Returns -1 if the array is
736 Perl_av_len(pTHX_ register AV *av)
744 Ensure than an array has a given number of elements, equivalent to
745 Perl's C<$#array = $fill;>.
750 Perl_av_fill(pTHX_ register AV *av, I32 fill)
754 Perl_croak(aTHX_ "panic: null array");
757 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
761 PUSHSTACKi(PERLSI_MAGIC);
764 PUSHs(SvTIED_obj((SV*)av, mg));
765 PUSHs(sv_2mortal(newSViv(fill+1)));
767 call_method("STORESIZE", G_SCALAR|G_DISCARD);
773 if (fill <= AvMAX(av)) {
774 I32 key = AvFILLp(av);
775 SV** ary = AvARRAY(av);
779 SvREFCNT_dec(ary[key]);
780 ary[key--] = &PL_sv_undef;
785 ary[++key] = &PL_sv_undef;
793 (void)av_store(av,fill,&PL_sv_undef);
797 =for apidoc av_delete
799 Deletes the element indexed by C<key> from the array. Returns the
800 deleted element. If C<flags> equals C<G_DISCARD>, the element is freed
801 and null is returned.
806 Perl_av_delete(pTHX_ AV *av, I32 key, I32 flags)
813 Perl_croak(aTHX_ PL_no_modify);
815 if (SvRMAGICAL(av)) {
816 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
817 if ((tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata))) {
818 /* Handle negative array indices 20020222 MJD */
821 unsigned adjust_index = 1;
823 SV **negative_indices_glob =
824 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
826 NEGATIVE_INDICES_VAR, 16, 0);
827 if (negative_indices_glob
828 && SvTRUE(GvSV(*negative_indices_glob)))
832 key += AvFILL(av) + 1;
837 svp = av_fetch(av, key, TRUE);
841 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
842 sv_unmagic(sv, PERL_MAGIC_tiedelem); /* No longer an element */
851 key += AvFILL(av) + 1;
856 if (key > AvFILLp(av))
859 if (!AvREAL(av) && AvREIFY(av))
861 sv = AvARRAY(av)[key];
862 if (key == AvFILLp(av)) {
863 AvARRAY(av)[key] = &PL_sv_undef;
866 } while (--key >= 0 && AvARRAY(av)[key] == &PL_sv_undef);
869 AvARRAY(av)[key] = &PL_sv_undef;
873 if (flags & G_DISCARD) {
883 =for apidoc av_exists
885 Returns true if the element indexed by C<key> has been initialized.
887 This relies on the fact that uninitialized array elements are set to
893 Perl_av_exists(pTHX_ AV *av, I32 key)
899 if (SvRMAGICAL(av)) {
900 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
901 if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) {
902 SV *sv = sv_newmortal();
904 /* Handle negative array indices 20020222 MJD */
906 unsigned adjust_index = 1;
908 SV **negative_indices_glob =
909 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
911 NEGATIVE_INDICES_VAR, 16, 0);
912 if (negative_indices_glob
913 && SvTRUE(GvSV(*negative_indices_glob)))
917 key += AvFILL(av) + 1;
923 mg_copy((SV*)av, sv, 0, key);
924 mg = mg_find(sv, PERL_MAGIC_tiedelem);
926 magic_existspack(sv, mg);
927 return (bool)SvTRUE(sv);
934 key += AvFILL(av) + 1;
939 if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef
948 /* AVHV: Support for treating arrays as if they were hashes. The
949 * first element of the array should be a hash reference that maps
950 * hash keys to array indices.
954 S_avhv_index_sv(pTHX_ SV* sv)
956 I32 index = SvIV(sv);
958 Perl_croak(aTHX_ "Bad index while coercing array into hash");
963 S_avhv_index(pTHX_ AV *av, SV *keysv, U32 hash)
969 keys = avhv_keys(av);
970 he = hv_fetch_ent(keys, keysv, FALSE, hash);
972 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\"", SvPV(keysv,n_a));
973 return avhv_index_sv(HeVAL(he));
977 Perl_avhv_keys(pTHX_ AV *av)
979 SV **keysp = av_fetch(av, 0, FALSE);
985 if (ckWARN(WARN_DEPRECATED) && !sv_isa(sv, "pseudohash"))
986 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
987 "Pseudo-hashes are deprecated");
989 if (SvTYPE(sv) == SVt_PVHV)
993 Perl_croak(aTHX_ "Can't coerce array into hash");
998 Perl_avhv_store_ent(pTHX_ AV *av, SV *keysv, SV *val, U32 hash)
1000 return av_store(av, avhv_index(av, keysv, hash), val);
1004 Perl_avhv_fetch_ent(pTHX_ AV *av, SV *keysv, I32 lval, U32 hash)
1006 return av_fetch(av, avhv_index(av, keysv, hash), lval);
1010 Perl_avhv_delete_ent(pTHX_ AV *av, SV *keysv, I32 flags, U32 hash)
1012 HV *keys = avhv_keys(av);
1015 he = hv_fetch_ent(keys, keysv, FALSE, hash);
1016 if (!he || !SvOK(HeVAL(he)))
1019 return av_delete(av, avhv_index_sv(HeVAL(he)), flags);
1022 /* Check for the existence of an element named by a given key.
1026 Perl_avhv_exists_ent(pTHX_ AV *av, SV *keysv, U32 hash)
1028 HV *keys = avhv_keys(av);
1031 he = hv_fetch_ent(keys, keysv, FALSE, hash);
1032 if (!he || !SvOK(HeVAL(he)))
1035 return av_exists(av, avhv_index_sv(HeVAL(he)));
1039 Perl_avhv_iternext(pTHX_ AV *av)
1041 HV *keys = avhv_keys(av);
1042 return hv_iternext(keys);
1046 Perl_avhv_iterval(pTHX_ AV *av, register HE *entry)
1048 SV *sv = hv_iterval(avhv_keys(av), entry);
1049 return *av_fetch(av, avhv_index_sv(sv), TRUE);
1054 * c-indentation-style: bsd
1056 * indent-tabs-mode: t
1059 * ex: set ts=8 sts=4 sw=4 noet: