3 * Copyright (c) 1991-2000, 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, 'P') && 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) {
39 (void)SvREFCNT_inc(sv);
42 key = AvARRAY(av) - AvALLOC(av);
44 AvALLOC(av)[--key] = &PL_sv_undef;
52 Pre-extend an array. The C<key> is the index to which the array should be
59 Perl_av_extend(pTHX_ AV *av, I32 key)
61 dTHR; /* only necessary if we have to extend stack */
63 if (mg = SvTIED_mg((SV*)av, 'P')) {
67 PUSHSTACKi(PERLSI_MAGIC);
70 PUSHs(SvTIED_obj((SV*)av, mg));
71 PUSHs(sv_2mortal(newSViv(key+1)));
73 call_method("EXTEND", G_SCALAR|G_DISCARD);
79 if (key > AvMAX(av)) {
84 if (AvALLOC(av) != AvARRAY(av)) {
85 ary = AvALLOC(av) + AvFILLp(av) + 1;
86 tmp = AvARRAY(av) - AvALLOC(av);
87 Move(AvARRAY(av), AvALLOC(av), AvFILLp(av)+1, SV*);
89 SvPVX(av) = (char*)AvALLOC(av);
92 ary[--tmp] = &PL_sv_undef;
95 if (key > AvMAX(av) - 10) {
96 newmax = key + AvMAX(av);
102 #ifndef STRANGE_MALLOC
107 #if defined(MYMALLOC) && !defined(LEAKTEST)
108 newmax = malloced_size((void*)AvALLOC(av))/sizeof(SV*) - 1;
113 newmax = key + AvMAX(av) / 5;
115 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
116 Renew(AvALLOC(av),newmax+1, SV*);
118 bytes = (newmax + 1) * sizeof(SV*);
119 #define MALLOC_OVERHEAD 16
120 itmp = MALLOC_OVERHEAD;
121 while (itmp - MALLOC_OVERHEAD < bytes)
123 itmp -= MALLOC_OVERHEAD;
125 assert(itmp > newmax);
127 assert(newmax >= AvMAX(av));
128 New(2,ary, newmax+1, SV*);
129 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
131 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
133 Safefree(AvALLOC(av));
137 ary = AvALLOC(av) + AvMAX(av) + 1;
138 tmp = newmax - AvMAX(av);
139 if (av == PL_curstack) { /* Oops, grew stack (via av_store()?) */
140 PL_stack_sp = AvALLOC(av) + (PL_stack_sp - PL_stack_base);
141 PL_stack_base = AvALLOC(av);
142 PL_stack_max = PL_stack_base + newmax;
146 newmax = key < 3 ? 3 : key;
147 New(2,AvALLOC(av), newmax+1, SV*);
148 ary = AvALLOC(av) + 1;
150 AvALLOC(av)[0] = &PL_sv_undef; /* For the stacks */
154 ary[--tmp] = &PL_sv_undef;
157 SvPVX(av) = (char*)AvALLOC(av);
166 Returns the SV at the specified index in the array. The C<key> is the
167 index. If C<lval> is set then the fetch will be part of a store. Check
168 that the return value is non-null before dereferencing it to a C<SV*>.
170 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
171 more information on how to use this function on tied arrays.
177 Perl_av_fetch(pTHX_ register AV *av, I32 key, I32 lval)
185 key += AvFILL(av) + 1;
190 if (SvRMAGICAL(av)) {
191 if (mg_find((SV*)av,'P') || mg_find((SV*)av,'D')) {
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)
253 key += AvFILL(av) + 1;
258 if (SvREADONLY(av) && key >= AvFILL(av))
259 Perl_croak(aTHX_ PL_no_modify);
261 if (SvRMAGICAL(av)) {
262 if (mg_find((SV*)av,'P')) {
263 if (val != &PL_sv_undef) {
264 mg_copy((SV*)av, val, 0, key);
270 if (!AvREAL(av) && AvREIFY(av))
275 if (AvFILLp(av) < key) {
278 if (av == PL_curstack && key > PL_stack_sp - PL_stack_base)
279 PL_stack_sp = PL_stack_base + key; /* XPUSH in disguise */
281 ary[++AvFILLp(av)] = &PL_sv_undef;
282 while (AvFILLp(av) < key);
287 SvREFCNT_dec(ary[key]);
289 if (SvSMAGICAL(av)) {
290 if (val != &PL_sv_undef) {
291 MAGIC* mg = SvMAGIC(av);
292 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
302 Creates a new AV. The reference count is set to 1.
312 av = (AV*)NEWSV(3,0);
313 sv_upgrade((SV *)av, SVt_PVAV);
317 AvMAX(av) = AvFILLp(av) = -1;
324 Creates a new AV and populates it with a list of SVs. The SVs are copied
325 into the array, so they may be freed after the call to av_make. The new AV
326 will have a reference count of 1.
332 Perl_av_make(pTHX_ register I32 size, register SV **strp)
338 av = (AV*)NEWSV(8,0);
339 sv_upgrade((SV *) av,SVt_PVAV);
340 AvFLAGS(av) = AVf_REAL;
341 if (size) { /* `defined' was returning undef for size==0 anyway. */
344 SvPVX(av) = (char*)ary;
345 AvFILLp(av) = size - 1;
346 AvMAX(av) = size - 1;
347 for (i = 0; i < size; i++) {
350 sv_setsv(ary[i], *strp);
358 Perl_av_fake(pTHX_ register I32 size, register SV **strp)
363 av = (AV*)NEWSV(9,0);
364 sv_upgrade((SV *)av, SVt_PVAV);
365 New(4,ary,size+1,SV*);
367 Copy(strp,ary,size,SV*);
368 AvFLAGS(av) = AVf_REIFY;
369 SvPVX(av) = (char*)ary;
370 AvFILLp(av) = size - 1;
371 AvMAX(av) = size - 1;
383 Clears an array, making it empty. Does not free the memory used by the
390 Perl_av_clear(pTHX_ register AV *av)
396 if (SvREFCNT(av) == 0 && ckWARN_d(WARN_DEBUGGING)) {
397 Perl_warner(aTHX_ WARN_DEBUGGING, "Attempt to clear deleted array");
405 Perl_croak(aTHX_ PL_no_modify);
407 /* Give any tie a chance to cleanup first */
416 key = AvFILLp(av) + 1;
418 SvREFCNT_dec(ary[--key]);
419 ary[key] = &PL_sv_undef;
422 if (key = AvARRAY(av) - AvALLOC(av)) {
424 SvPVX(av) = (char*)AvALLOC(av);
433 Undefines the array. Frees the memory used by the array itself.
439 Perl_av_undef(pTHX_ register AV *av)
447 /* Give any tie a chance to cleanup first */
448 if (SvTIED_mg((SV*)av, 'P'))
449 av_fill(av, -1); /* mg_clear() ? */
452 key = AvFILLp(av) + 1;
454 SvREFCNT_dec(AvARRAY(av)[--key]);
456 Safefree(AvALLOC(av));
459 AvMAX(av) = AvFILLp(av) = -1;
461 SvREFCNT_dec(AvARYLEN(av));
469 Pushes an SV onto the end of the array. The array will grow automatically
470 to accommodate the addition.
476 Perl_av_push(pTHX_ register AV *av, SV *val)
482 Perl_croak(aTHX_ PL_no_modify);
484 if (mg = SvTIED_mg((SV*)av, 'P')) {
486 PUSHSTACKi(PERLSI_MAGIC);
489 PUSHs(SvTIED_obj((SV*)av, mg));
493 call_method("PUSH", G_SCALAR|G_DISCARD);
498 av_store(av,AvFILLp(av)+1,val);
504 Pops an SV off the end of the array. Returns C<&PL_sv_undef> if the array
511 Perl_av_pop(pTHX_ register AV *av)
516 if (!av || AvFILL(av) < 0)
519 Perl_croak(aTHX_ PL_no_modify);
520 if (mg = SvTIED_mg((SV*)av, 'P')) {
522 PUSHSTACKi(PERLSI_MAGIC);
524 XPUSHs(SvTIED_obj((SV*)av, mg));
527 if (call_method("POP", G_SCALAR)) {
528 retval = newSVsv(*PL_stack_sp--);
530 retval = &PL_sv_undef;
536 retval = AvARRAY(av)[AvFILLp(av)];
537 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
544 =for apidoc av_unshift
546 Unshift the given number of C<undef> values onto the beginning of the
547 array. The array will grow automatically to accommodate the addition. You
548 must then use C<av_store> to assign values to these new elements.
554 Perl_av_unshift(pTHX_ register AV *av, register I32 num)
563 Perl_croak(aTHX_ PL_no_modify);
565 if (mg = SvTIED_mg((SV*)av, 'P')) {
567 PUSHSTACKi(PERLSI_MAGIC);
570 PUSHs(SvTIED_obj((SV*)av, mg));
576 call_method("UNSHIFT", G_SCALAR|G_DISCARD);
582 if (!AvREAL(av) && AvREIFY(av))
584 i = AvARRAY(av) - AvALLOC(av);
592 SvPVX(av) = (char*)(AvARRAY(av) - i);
596 av_extend(av, i + num);
599 Move(ary, ary + num, i + 1, SV*);
601 ary[--num] = &PL_sv_undef;
609 Shifts an SV off the beginning of the array.
615 Perl_av_shift(pTHX_ register AV *av)
620 if (!av || AvFILL(av) < 0)
623 Perl_croak(aTHX_ PL_no_modify);
624 if (mg = SvTIED_mg((SV*)av, 'P')) {
626 PUSHSTACKi(PERLSI_MAGIC);
628 XPUSHs(SvTIED_obj((SV*)av, mg));
631 if (call_method("SHIFT", G_SCALAR)) {
632 retval = newSVsv(*PL_stack_sp--);
634 retval = &PL_sv_undef;
640 retval = *AvARRAY(av);
642 *AvARRAY(av) = &PL_sv_undef;
643 SvPVX(av) = (char*)(AvARRAY(av) + 1);
654 Returns the highest index in the array. Returns -1 if the array is
661 Perl_av_len(pTHX_ register AV *av)
667 Perl_av_fill(pTHX_ register AV *av, I32 fill)
671 Perl_croak(aTHX_ "panic: null array");
674 if (mg = SvTIED_mg((SV*)av, 'P')) {
678 PUSHSTACKi(PERLSI_MAGIC);
681 PUSHs(SvTIED_obj((SV*)av, mg));
682 PUSHs(sv_2mortal(newSViv(fill+1)));
684 call_method("STORESIZE", G_SCALAR|G_DISCARD);
690 if (fill <= AvMAX(av)) {
691 I32 key = AvFILLp(av);
692 SV** ary = AvARRAY(av);
696 SvREFCNT_dec(ary[key]);
697 ary[key--] = &PL_sv_undef;
702 ary[++key] = &PL_sv_undef;
710 (void)av_store(av,fill,&PL_sv_undef);
714 Perl_av_delete(pTHX_ AV *av, I32 key, I32 flags)
721 Perl_croak(aTHX_ PL_no_modify);
723 key += AvFILL(av) + 1;
727 if (SvRMAGICAL(av)) {
729 if ((mg_find((SV*)av,'P') || mg_find((SV*)av,'D'))
730 && (svp = av_fetch(av, key, TRUE)))
734 if (mg_find(sv, 'p')) {
735 sv_unmagic(sv, 'p'); /* No longer an element */
738 return Nullsv; /* element cannot be deleted */
741 if (key > AvFILLp(av))
744 sv = AvARRAY(av)[key];
745 if (key == AvFILLp(av)) {
748 } while (--key >= 0 && AvARRAY(av)[key] == &PL_sv_undef);
751 AvARRAY(av)[key] = &PL_sv_undef;
755 if (flags & G_DISCARD) {
763 * This relies on the fact that uninitialized array elements
764 * are set to &PL_sv_undef.
768 Perl_av_exists(pTHX_ AV *av, I32 key)
773 key += AvFILL(av) + 1;
777 if (SvRMAGICAL(av)) {
778 if (mg_find((SV*)av,'P') || mg_find((SV*)av,'D')) {
779 SV *sv = sv_newmortal();
780 mg_copy((SV*)av, sv, 0, key);
781 magic_existspack(sv, mg_find(sv, 'p'));
785 if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef
794 /* AVHV: Support for treating arrays as if they were hashes. The
795 * first element of the array should be a hash reference that maps
796 * hash keys to array indices.
800 S_avhv_index_sv(pTHX_ SV* sv)
802 I32 index = SvIV(sv);
804 Perl_croak(aTHX_ "Bad index while coercing array into hash");
809 Perl_avhv_keys(pTHX_ AV *av)
811 SV **keysp = av_fetch(av, 0, FALSE);
818 if (SvTYPE(sv) == SVt_PVHV)
822 Perl_croak(aTHX_ "Can't coerce array into hash");
827 Perl_avhv_fetch_ent(pTHX_ AV *av, SV *keysv, I32 lval, U32 hash)
830 HV *keys = avhv_keys(av);
834 he = hv_fetch_ent(keys, keysv, FALSE, hash);
836 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\"", SvPV(keysv,n_a));
837 return av_fetch(av, avhv_index_sv(HeVAL(he)), lval);
841 Perl_avhv_delete_ent(pTHX_ AV *av, SV *keysv, I32 flags, U32 hash)
843 HV *keys = avhv_keys(av);
846 he = hv_fetch_ent(keys, keysv, FALSE, hash);
847 if (!he || !SvOK(HeVAL(he)))
850 return av_delete(av, avhv_index_sv(HeVAL(he)), flags);
853 /* Check for the existence of an element named by a given key.
857 Perl_avhv_exists_ent(pTHX_ AV *av, SV *keysv, U32 hash)
859 HV *keys = avhv_keys(av);
862 he = hv_fetch_ent(keys, keysv, FALSE, hash);
863 if (!he || !SvOK(HeVAL(he)))
866 return av_exists(av, avhv_index_sv(HeVAL(he)));
870 Perl_avhv_iternext(pTHX_ AV *av)
872 HV *keys = avhv_keys(av);
873 return hv_iternext(keys);
877 Perl_avhv_iterval(pTHX_ AV *av, register HE *entry)
879 SV *sv = hv_iterval(avhv_keys(av), entry);
880 return *av_fetch(av, avhv_index_sv(sv), TRUE);