3 * Copyright (c) 1991-2001, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "...for the Entwives desired order, and plenty, and peace (by which they
12 * meant that things should remain where they had set them)." --Treebeard
20 Perl_av_reify(pTHX_ AV *av)
28 if (SvTIED_mg((SV*)av, PERL_MAGIC_tied) && ckWARN_d(WARN_DEBUGGING))
29 Perl_warner(aTHX_ WARN_DEBUGGING, "av_reify called on tied array");
32 while (key > AvFILLp(av) + 1)
33 AvARRAY(av)[--key] = &PL_sv_undef;
35 sv = AvARRAY(av)[--key];
37 if (sv != &PL_sv_undef)
38 (void)SvREFCNT_inc(sv);
40 key = AvARRAY(av) - AvALLOC(av);
42 AvALLOC(av)[--key] = &PL_sv_undef;
50 Pre-extend an array. The C<key> is the index to which the array should be
57 Perl_av_extend(pTHX_ AV *av, I32 key)
60 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
64 PUSHSTACKi(PERLSI_MAGIC);
67 PUSHs(SvTIED_obj((SV*)av, mg));
68 PUSHs(sv_2mortal(newSViv(key+1)));
70 call_method("EXTEND", G_SCALAR|G_DISCARD);
76 if (key > AvMAX(av)) {
81 if (AvALLOC(av) != AvARRAY(av)) {
82 ary = AvALLOC(av) + AvFILLp(av) + 1;
83 tmp = AvARRAY(av) - AvALLOC(av);
84 Move(AvARRAY(av), AvALLOC(av), AvFILLp(av)+1, SV*);
86 SvPVX(av) = (char*)AvALLOC(av);
89 ary[--tmp] = &PL_sv_undef;
92 if (key > AvMAX(av) - 10) {
93 newmax = key + AvMAX(av);
99 #if !defined(STRANGE_MALLOC) && !defined(MYMALLOC)
104 #if defined(MYMALLOC) && !defined(LEAKTEST)
105 newmax = malloced_size((void*)AvALLOC(av))/sizeof(SV*) - 1;
110 newmax = key + AvMAX(av) / 5;
112 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
113 Renew(AvALLOC(av),newmax+1, SV*);
115 bytes = (newmax + 1) * sizeof(SV*);
116 #define MALLOC_OVERHEAD 16
117 itmp = MALLOC_OVERHEAD;
118 while (itmp - MALLOC_OVERHEAD < bytes)
120 itmp -= MALLOC_OVERHEAD;
122 assert(itmp > newmax);
124 assert(newmax >= AvMAX(av));
125 New(2,ary, newmax+1, SV*);
126 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
128 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
130 Safefree(AvALLOC(av));
133 #if defined(MYMALLOC) && !defined(LEAKTEST)
136 ary = AvALLOC(av) + AvMAX(av) + 1;
137 tmp = newmax - AvMAX(av);
138 if (av == PL_curstack) { /* Oops, grew stack (via av_store()?) */
139 PL_stack_sp = AvALLOC(av) + (PL_stack_sp - PL_stack_base);
140 PL_stack_base = AvALLOC(av);
141 PL_stack_max = PL_stack_base + newmax;
145 newmax = key < 3 ? 3 : key;
146 New(2,AvALLOC(av), newmax+1, SV*);
147 ary = AvALLOC(av) + 1;
149 AvALLOC(av)[0] = &PL_sv_undef; /* For the stacks */
153 ary[--tmp] = &PL_sv_undef;
156 SvPVX(av) = (char*)AvALLOC(av);
165 Returns the SV at the specified index in the array. The C<key> is the
166 index. If C<lval> is set then the fetch will be part of a store. Check
167 that the return value is non-null before dereferencing it to a C<SV*>.
169 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
170 more information on how to use this function on tied arrays.
176 Perl_av_fetch(pTHX_ register AV *av, I32 key, I32 lval)
184 key += AvFILL(av) + 1;
189 if (SvRMAGICAL(av)) {
190 if (mg_find((SV*)av, PERL_MAGIC_tied) ||
191 mg_find((SV*)av, PERL_MAGIC_regdata))
194 mg_copy((SV*)av, sv, 0, key);
196 return &PL_av_fetch_sv;
200 if (key > AvFILLp(av)) {
204 return av_store(av,key,sv);
206 if (AvARRAY(av)[key] == &PL_sv_undef) {
210 return av_store(av,key,sv);
215 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
216 || SvTYPE(AvARRAY(av)[key]) == SVTYPEMASK)) {
217 AvARRAY(av)[key] = &PL_sv_undef; /* 1/2 reify */
220 return &AvARRAY(av)[key];
226 Stores an SV in an array. The array index is specified as C<key>. The
227 return value will be NULL if the operation failed or if the value did not
228 need to be actually stored within the array (as in the case of tied
229 arrays). Otherwise it can be dereferenced to get the original C<SV*>. Note
230 that the caller is responsible for suitably incrementing the reference
231 count of C<val> before the call, and decrementing it if the function
234 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
235 more information on how to use this function on tied arrays.
241 Perl_av_store(pTHX_ register AV *av, I32 key, SV *val)
251 key += AvFILL(av) + 1;
256 if (SvREADONLY(av) && key >= AvFILL(av))
257 Perl_croak(aTHX_ PL_no_modify);
259 if (SvRMAGICAL(av)) {
260 if (mg_find((SV*)av, PERL_MAGIC_tied)) {
261 if (val != &PL_sv_undef) {
262 mg_copy((SV*)av, val, 0, key);
268 if (!AvREAL(av) && AvREIFY(av))
273 if (AvFILLp(av) < key) {
275 if (av == PL_curstack && key > PL_stack_sp - PL_stack_base)
276 PL_stack_sp = PL_stack_base + key; /* XPUSH in disguise */
278 ary[++AvFILLp(av)] = &PL_sv_undef;
279 while (AvFILLp(av) < key);
284 SvREFCNT_dec(ary[key]);
286 if (SvSMAGICAL(av)) {
287 if (val != &PL_sv_undef) {
288 MAGIC* mg = SvMAGIC(av);
289 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
299 Creates a new AV. The reference count is set to 1.
309 av = (AV*)NEWSV(3,0);
310 sv_upgrade((SV *)av, SVt_PVAV);
314 AvMAX(av) = AvFILLp(av) = -1;
321 Creates a new AV and populates it with a list of SVs. The SVs are copied
322 into the array, so they may be freed after the call to av_make. The new AV
323 will have a reference count of 1.
329 Perl_av_make(pTHX_ register I32 size, register SV **strp)
335 av = (AV*)NEWSV(8,0);
336 sv_upgrade((SV *) av,SVt_PVAV);
337 AvFLAGS(av) = AVf_REAL;
338 if (size) { /* `defined' was returning undef for size==0 anyway. */
341 SvPVX(av) = (char*)ary;
342 AvFILLp(av) = size - 1;
343 AvMAX(av) = size - 1;
344 for (i = 0; i < size; i++) {
347 sv_setsv(ary[i], *strp);
355 Perl_av_fake(pTHX_ register I32 size, register SV **strp)
360 av = (AV*)NEWSV(9,0);
361 sv_upgrade((SV *)av, SVt_PVAV);
362 New(4,ary,size+1,SV*);
364 Copy(strp,ary,size,SV*);
365 AvFLAGS(av) = AVf_REIFY;
366 SvPVX(av) = (char*)ary;
367 AvFILLp(av) = size - 1;
368 AvMAX(av) = size - 1;
380 Clears an array, making it empty. Does not free the memory used by the
387 Perl_av_clear(pTHX_ register AV *av)
393 if (SvREFCNT(av) == 0 && ckWARN_d(WARN_DEBUGGING)) {
394 Perl_warner(aTHX_ WARN_DEBUGGING, "Attempt to clear deleted array");
402 Perl_croak(aTHX_ PL_no_modify);
404 /* Give any tie a chance to cleanup first */
413 key = AvFILLp(av) + 1;
415 SvREFCNT_dec(ary[--key]);
416 ary[key] = &PL_sv_undef;
419 if ((key = AvARRAY(av) - AvALLOC(av))) {
421 SvPVX(av) = (char*)AvALLOC(av);
430 Undefines the array. Frees the memory used by the array itself.
436 Perl_av_undef(pTHX_ register AV *av)
444 /* Give any tie a chance to cleanup first */
445 if (SvTIED_mg((SV*)av, PERL_MAGIC_tied))
446 av_fill(av, -1); /* mg_clear() ? */
449 key = AvFILLp(av) + 1;
451 SvREFCNT_dec(AvARRAY(av)[--key]);
453 Safefree(AvALLOC(av));
456 AvMAX(av) = AvFILLp(av) = -1;
458 SvREFCNT_dec(AvARYLEN(av));
466 Pushes an SV onto the end of the array. The array will grow automatically
467 to accommodate the addition.
473 Perl_av_push(pTHX_ register AV *av, SV *val)
479 Perl_croak(aTHX_ PL_no_modify);
481 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
483 PUSHSTACKi(PERLSI_MAGIC);
486 PUSHs(SvTIED_obj((SV*)av, mg));
490 call_method("PUSH", G_SCALAR|G_DISCARD);
495 av_store(av,AvFILLp(av)+1,val);
501 Pops an SV off the end of the array. Returns C<&PL_sv_undef> if the array
508 Perl_av_pop(pTHX_ register AV *av)
513 if (!av || AvFILL(av) < 0)
516 Perl_croak(aTHX_ PL_no_modify);
517 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
519 PUSHSTACKi(PERLSI_MAGIC);
521 XPUSHs(SvTIED_obj((SV*)av, mg));
524 if (call_method("POP", G_SCALAR)) {
525 retval = newSVsv(*PL_stack_sp--);
527 retval = &PL_sv_undef;
533 retval = AvARRAY(av)[AvFILLp(av)];
534 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
541 =for apidoc av_unshift
543 Unshift the given number of C<undef> values onto the beginning of the
544 array. The array will grow automatically to accommodate the addition. You
545 must then use C<av_store> to assign values to these new elements.
551 Perl_av_unshift(pTHX_ register AV *av, register I32 num)
561 Perl_croak(aTHX_ PL_no_modify);
563 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
565 PUSHSTACKi(PERLSI_MAGIC);
568 PUSHs(SvTIED_obj((SV*)av, mg));
574 call_method("UNSHIFT", G_SCALAR|G_DISCARD);
580 if (!AvREAL(av) && AvREIFY(av))
582 i = AvARRAY(av) - AvALLOC(av);
590 SvPVX(av) = (char*)(AvARRAY(av) - i);
594 /* Create extra elements */
595 slide = i > 0 ? i : 0;
597 av_extend(av, i + num);
600 Move(ary, ary + num, i + 1, SV*);
602 ary[--num] = &PL_sv_undef;
604 /* Make extra elements into a buffer */
606 AvFILLp(av) -= slide;
607 SvPVX(av) = (char*)(AvARRAY(av) + slide);
614 Shifts an SV off the beginning of the array.
620 Perl_av_shift(pTHX_ register AV *av)
625 if (!av || AvFILL(av) < 0)
628 Perl_croak(aTHX_ PL_no_modify);
629 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
631 PUSHSTACKi(PERLSI_MAGIC);
633 XPUSHs(SvTIED_obj((SV*)av, mg));
636 if (call_method("SHIFT", G_SCALAR)) {
637 retval = newSVsv(*PL_stack_sp--);
639 retval = &PL_sv_undef;
645 retval = *AvARRAY(av);
647 *AvARRAY(av) = &PL_sv_undef;
648 SvPVX(av) = (char*)(AvARRAY(av) + 1);
659 Returns the highest index in the array. Returns -1 if the array is
666 Perl_av_len(pTHX_ register AV *av)
674 Ensure than an array has a given number of elements, equivalent to
675 Perl's C<$#array = $fill;>.
680 Perl_av_fill(pTHX_ register AV *av, I32 fill)
684 Perl_croak(aTHX_ "panic: null array");
687 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
691 PUSHSTACKi(PERLSI_MAGIC);
694 PUSHs(SvTIED_obj((SV*)av, mg));
695 PUSHs(sv_2mortal(newSViv(fill+1)));
697 call_method("STORESIZE", G_SCALAR|G_DISCARD);
703 if (fill <= AvMAX(av)) {
704 I32 key = AvFILLp(av);
705 SV** ary = AvARRAY(av);
709 SvREFCNT_dec(ary[key]);
710 ary[key--] = &PL_sv_undef;
715 ary[++key] = &PL_sv_undef;
723 (void)av_store(av,fill,&PL_sv_undef);
727 =for apidoc av_delete
729 Deletes the element indexed by C<key> from the array. Returns the
730 deleted element. C<flags> is currently ignored.
735 Perl_av_delete(pTHX_ AV *av, I32 key, I32 flags)
742 Perl_croak(aTHX_ PL_no_modify);
744 key += AvFILL(av) + 1;
748 if (SvRMAGICAL(av)) {
750 if ((mg_find((SV*)av, PERL_MAGIC_tied) ||
751 mg_find((SV*)av, PERL_MAGIC_regdata))
752 && (svp = av_fetch(av, key, TRUE)))
756 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
757 sv_unmagic(sv, PERL_MAGIC_tiedelem); /* No longer an element */
760 return Nullsv; /* element cannot be deleted */
763 if (key > AvFILLp(av))
766 sv = AvARRAY(av)[key];
767 if (key == AvFILLp(av)) {
770 } while (--key >= 0 && AvARRAY(av)[key] == &PL_sv_undef);
773 AvARRAY(av)[key] = &PL_sv_undef;
777 if (flags & G_DISCARD) {
785 =for apidoc av_exists
787 Returns true if the element indexed by C<key> has been initialized.
789 This relies on the fact that uninitialized array elements are set to
795 Perl_av_exists(pTHX_ AV *av, I32 key)
800 key += AvFILL(av) + 1;
804 if (SvRMAGICAL(av)) {
805 if (mg_find((SV*)av, PERL_MAGIC_tied) ||
806 mg_find((SV*)av, PERL_MAGIC_regdata))
808 SV *sv = sv_newmortal();
811 mg_copy((SV*)av, sv, 0, key);
812 mg = mg_find(sv, PERL_MAGIC_tiedelem);
814 magic_existspack(sv, mg);
819 if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef
828 /* AVHV: Support for treating arrays as if they were hashes. The
829 * first element of the array should be a hash reference that maps
830 * hash keys to array indices.
834 S_avhv_index_sv(pTHX_ SV* sv)
836 I32 index = SvIV(sv);
838 Perl_croak(aTHX_ "Bad index while coercing array into hash");
843 S_avhv_index(pTHX_ AV *av, SV *keysv, U32 hash)
849 keys = avhv_keys(av);
850 he = hv_fetch_ent(keys, keysv, FALSE, hash);
852 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\"", SvPV(keysv,n_a));
853 return avhv_index_sv(HeVAL(he));
857 Perl_avhv_keys(pTHX_ AV *av)
859 SV **keysp = av_fetch(av, 0, FALSE);
866 if (SvTYPE(sv) == SVt_PVHV)
870 Perl_croak(aTHX_ "Can't coerce array into hash");
875 Perl_avhv_store_ent(pTHX_ AV *av, SV *keysv, SV *val, U32 hash)
877 return av_store(av, avhv_index(av, keysv, hash), val);
881 Perl_avhv_fetch_ent(pTHX_ AV *av, SV *keysv, I32 lval, U32 hash)
883 return av_fetch(av, avhv_index(av, keysv, hash), lval);
887 Perl_avhv_delete_ent(pTHX_ AV *av, SV *keysv, I32 flags, U32 hash)
889 HV *keys = avhv_keys(av);
892 he = hv_fetch_ent(keys, keysv, FALSE, hash);
893 if (!he || !SvOK(HeVAL(he)))
896 return av_delete(av, avhv_index_sv(HeVAL(he)), flags);
899 /* Check for the existence of an element named by a given key.
903 Perl_avhv_exists_ent(pTHX_ AV *av, SV *keysv, U32 hash)
905 HV *keys = avhv_keys(av);
908 he = hv_fetch_ent(keys, keysv, FALSE, hash);
909 if (!he || !SvOK(HeVAL(he)))
912 return av_exists(av, avhv_index_sv(HeVAL(he)));
916 Perl_avhv_iternext(pTHX_ AV *av)
918 HV *keys = avhv_keys(av);
919 return hv_iternext(keys);
923 Perl_avhv_iterval(pTHX_ AV *av, register HE *entry)
925 SV *sv = hv_iterval(avhv_keys(av), entry);
926 return *av_fetch(av, avhv_index_sv(sv), TRUE);