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, '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)
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, 'P'))) {
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 #ifndef STRANGE_MALLOC
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));
134 ary = AvALLOC(av) + AvMAX(av) + 1;
135 tmp = newmax - AvMAX(av);
136 if (av == PL_curstack) { /* Oops, grew stack (via av_store()?) */
137 PL_stack_sp = AvALLOC(av) + (PL_stack_sp - PL_stack_base);
138 PL_stack_base = AvALLOC(av);
139 PL_stack_max = PL_stack_base + newmax;
143 newmax = key < 3 ? 3 : key;
144 New(2,AvALLOC(av), newmax+1, SV*);
145 ary = AvALLOC(av) + 1;
147 AvALLOC(av)[0] = &PL_sv_undef; /* For the stacks */
151 ary[--tmp] = &PL_sv_undef;
154 SvPVX(av) = (char*)AvALLOC(av);
163 Returns the SV at the specified index in the array. The C<key> is the
164 index. If C<lval> is set then the fetch will be part of a store. Check
165 that the return value is non-null before dereferencing it to a C<SV*>.
167 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
168 more information on how to use this function on tied arrays.
174 Perl_av_fetch(pTHX_ register AV *av, I32 key, I32 lval)
182 key += AvFILL(av) + 1;
187 if (SvRMAGICAL(av)) {
188 if (mg_find((SV*)av,'P') || mg_find((SV*)av,'D')) {
190 mg_copy((SV*)av, sv, 0, key);
192 return &PL_av_fetch_sv;
196 if (key > AvFILLp(av)) {
200 return av_store(av,key,sv);
202 if (AvARRAY(av)[key] == &PL_sv_undef) {
206 return av_store(av,key,sv);
211 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
212 || SvTYPE(AvARRAY(av)[key]) == SVTYPEMASK)) {
213 AvARRAY(av)[key] = &PL_sv_undef; /* 1/2 reify */
216 return &AvARRAY(av)[key];
222 Stores an SV in an array. The array index is specified as C<key>. The
223 return value will be NULL if the operation failed or if the value did not
224 need to be actually stored within the array (as in the case of tied
225 arrays). Otherwise it can be dereferenced to get the original C<SV*>. Note
226 that the caller is responsible for suitably incrementing the reference
227 count of C<val> before the call, and decrementing it if the function
230 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
231 more information on how to use this function on tied arrays.
237 Perl_av_store(pTHX_ register AV *av, I32 key, SV *val)
247 key += AvFILL(av) + 1;
252 if (SvREADONLY(av) && key >= AvFILL(av))
253 Perl_croak(aTHX_ PL_no_modify);
255 if (SvRMAGICAL(av)) {
256 if (mg_find((SV*)av,'P')) {
257 if (val != &PL_sv_undef) {
258 mg_copy((SV*)av, val, 0, key);
264 if (!AvREAL(av) && AvREIFY(av))
269 if (AvFILLp(av) < key) {
271 if (av == PL_curstack && key > PL_stack_sp - PL_stack_base)
272 PL_stack_sp = PL_stack_base + key; /* XPUSH in disguise */
274 ary[++AvFILLp(av)] = &PL_sv_undef;
275 while (AvFILLp(av) < key);
280 SvREFCNT_dec(ary[key]);
282 if (SvSMAGICAL(av)) {
283 if (val != &PL_sv_undef) {
284 MAGIC* mg = SvMAGIC(av);
285 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
295 Creates a new AV. The reference count is set to 1.
305 av = (AV*)NEWSV(3,0);
306 sv_upgrade((SV *)av, SVt_PVAV);
310 AvMAX(av) = AvFILLp(av) = -1;
317 Creates a new AV and populates it with a list of SVs. The SVs are copied
318 into the array, so they may be freed after the call to av_make. The new AV
319 will have a reference count of 1.
325 Perl_av_make(pTHX_ register I32 size, register SV **strp)
331 av = (AV*)NEWSV(8,0);
332 sv_upgrade((SV *) av,SVt_PVAV);
333 AvFLAGS(av) = AVf_REAL;
334 if (size) { /* `defined' was returning undef for size==0 anyway. */
337 SvPVX(av) = (char*)ary;
338 AvFILLp(av) = size - 1;
339 AvMAX(av) = size - 1;
340 for (i = 0; i < size; i++) {
343 sv_setsv(ary[i], *strp);
351 Perl_av_fake(pTHX_ register I32 size, register SV **strp)
356 av = (AV*)NEWSV(9,0);
357 sv_upgrade((SV *)av, SVt_PVAV);
358 New(4,ary,size+1,SV*);
360 Copy(strp,ary,size,SV*);
361 AvFLAGS(av) = AVf_REIFY;
362 SvPVX(av) = (char*)ary;
363 AvFILLp(av) = size - 1;
364 AvMAX(av) = size - 1;
376 Clears an array, making it empty. Does not free the memory used by the
383 Perl_av_clear(pTHX_ register AV *av)
389 if (SvREFCNT(av) == 0 && ckWARN_d(WARN_DEBUGGING)) {
390 Perl_warner(aTHX_ WARN_DEBUGGING, "Attempt to clear deleted array");
398 Perl_croak(aTHX_ PL_no_modify);
400 /* Give any tie a chance to cleanup first */
409 key = AvFILLp(av) + 1;
411 SvREFCNT_dec(ary[--key]);
412 ary[key] = &PL_sv_undef;
415 if ((key = AvARRAY(av) - AvALLOC(av))) {
417 SvPVX(av) = (char*)AvALLOC(av);
426 Undefines the array. Frees the memory used by the array itself.
432 Perl_av_undef(pTHX_ register AV *av)
440 /* Give any tie a chance to cleanup first */
441 if (SvTIED_mg((SV*)av, 'P'))
442 av_fill(av, -1); /* mg_clear() ? */
445 key = AvFILLp(av) + 1;
447 SvREFCNT_dec(AvARRAY(av)[--key]);
449 Safefree(AvALLOC(av));
452 AvMAX(av) = AvFILLp(av) = -1;
454 SvREFCNT_dec(AvARYLEN(av));
462 Pushes an SV onto the end of the array. The array will grow automatically
463 to accommodate the addition.
469 Perl_av_push(pTHX_ register AV *av, SV *val)
475 Perl_croak(aTHX_ PL_no_modify);
477 if ((mg = SvTIED_mg((SV*)av, 'P'))) {
479 PUSHSTACKi(PERLSI_MAGIC);
482 PUSHs(SvTIED_obj((SV*)av, mg));
486 call_method("PUSH", G_SCALAR|G_DISCARD);
491 av_store(av,AvFILLp(av)+1,val);
497 Pops an SV off the end of the array. Returns C<&PL_sv_undef> if the array
504 Perl_av_pop(pTHX_ register AV *av)
509 if (!av || AvFILL(av) < 0)
512 Perl_croak(aTHX_ PL_no_modify);
513 if ((mg = SvTIED_mg((SV*)av, 'P'))) {
515 PUSHSTACKi(PERLSI_MAGIC);
517 XPUSHs(SvTIED_obj((SV*)av, mg));
520 if (call_method("POP", G_SCALAR)) {
521 retval = newSVsv(*PL_stack_sp--);
523 retval = &PL_sv_undef;
529 retval = AvARRAY(av)[AvFILLp(av)];
530 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
537 =for apidoc av_unshift
539 Unshift the given number of C<undef> values onto the beginning of the
540 array. The array will grow automatically to accommodate the addition. You
541 must then use C<av_store> to assign values to these new elements.
547 Perl_av_unshift(pTHX_ register AV *av, register I32 num)
557 Perl_croak(aTHX_ PL_no_modify);
559 if ((mg = SvTIED_mg((SV*)av, 'P'))) {
561 PUSHSTACKi(PERLSI_MAGIC);
564 PUSHs(SvTIED_obj((SV*)av, mg));
570 call_method("UNSHIFT", G_SCALAR|G_DISCARD);
576 if (!AvREAL(av) && AvREIFY(av))
578 i = AvARRAY(av) - AvALLOC(av);
586 SvPVX(av) = (char*)(AvARRAY(av) - i);
590 /* Create extra elements */
591 slide = i > 0 ? i : 0;
593 av_extend(av, i + num);
596 Move(ary, ary + num, i + 1, SV*);
598 ary[--num] = &PL_sv_undef;
600 /* Make extra elements into a buffer */
602 AvFILLp(av) -= slide;
603 SvPVX(av) = (char*)(AvARRAY(av) + slide);
610 Shifts an SV off the beginning of the array.
616 Perl_av_shift(pTHX_ register AV *av)
621 if (!av || AvFILL(av) < 0)
624 Perl_croak(aTHX_ PL_no_modify);
625 if ((mg = SvTIED_mg((SV*)av, 'P'))) {
627 PUSHSTACKi(PERLSI_MAGIC);
629 XPUSHs(SvTIED_obj((SV*)av, mg));
632 if (call_method("SHIFT", G_SCALAR)) {
633 retval = newSVsv(*PL_stack_sp--);
635 retval = &PL_sv_undef;
641 retval = *AvARRAY(av);
643 *AvARRAY(av) = &PL_sv_undef;
644 SvPVX(av) = (char*)(AvARRAY(av) + 1);
655 Returns the highest index in the array. Returns -1 if the array is
662 Perl_av_len(pTHX_ register AV *av)
670 Ensure than an array has a given number of elements, equivalent to
671 Perl's C<$#array = $fill;>.
676 Perl_av_fill(pTHX_ register AV *av, I32 fill)
680 Perl_croak(aTHX_ "panic: null array");
683 if ((mg = SvTIED_mg((SV*)av, 'P'))) {
687 PUSHSTACKi(PERLSI_MAGIC);
690 PUSHs(SvTIED_obj((SV*)av, mg));
691 PUSHs(sv_2mortal(newSViv(fill+1)));
693 call_method("STORESIZE", G_SCALAR|G_DISCARD);
699 if (fill <= AvMAX(av)) {
700 I32 key = AvFILLp(av);
701 SV** ary = AvARRAY(av);
705 SvREFCNT_dec(ary[key]);
706 ary[key--] = &PL_sv_undef;
711 ary[++key] = &PL_sv_undef;
719 (void)av_store(av,fill,&PL_sv_undef);
723 =for apidoc av_delete
725 Deletes the element indexed by C<key> from the array. Returns the
726 deleted element. C<flags> is currently ignored.
731 Perl_av_delete(pTHX_ AV *av, I32 key, I32 flags)
738 Perl_croak(aTHX_ PL_no_modify);
740 key += AvFILL(av) + 1;
744 if (SvRMAGICAL(av)) {
746 if ((mg_find((SV*)av,'P') || mg_find((SV*)av,'D'))
747 && (svp = av_fetch(av, key, TRUE)))
751 if (mg_find(sv, 'p')) {
752 sv_unmagic(sv, 'p'); /* No longer an element */
755 return Nullsv; /* element cannot be deleted */
758 if (key > AvFILLp(av))
761 sv = AvARRAY(av)[key];
762 if (key == AvFILLp(av)) {
765 } while (--key >= 0 && AvARRAY(av)[key] == &PL_sv_undef);
768 AvARRAY(av)[key] = &PL_sv_undef;
772 if (flags & G_DISCARD) {
780 =for apidoc av_exists
782 Returns true if the element indexed by C<key> has been initialized.
784 This relies on the fact that uninitialized array elements are set to
790 Perl_av_exists(pTHX_ AV *av, I32 key)
795 key += AvFILL(av) + 1;
799 if (SvRMAGICAL(av)) {
800 if (mg_find((SV*)av,'P') || mg_find((SV*)av,'D')) {
801 SV *sv = sv_newmortal();
804 mg_copy((SV*)av, sv, 0, key);
805 mg = mg_find(sv, 'p');
807 magic_existspack(sv, mg);
812 if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef
821 /* AVHV: Support for treating arrays as if they were hashes. The
822 * first element of the array should be a hash reference that maps
823 * hash keys to array indices.
827 S_avhv_index_sv(pTHX_ SV* sv)
829 I32 index = SvIV(sv);
831 Perl_croak(aTHX_ "Bad index while coercing array into hash");
836 S_avhv_index(pTHX_ AV *av, SV *keysv, U32 hash)
842 keys = avhv_keys(av);
843 he = hv_fetch_ent(keys, keysv, FALSE, hash);
845 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\"", SvPV(keysv,n_a));
846 return avhv_index_sv(HeVAL(he));
850 Perl_avhv_keys(pTHX_ AV *av)
852 SV **keysp = av_fetch(av, 0, FALSE);
859 if (SvTYPE(sv) == SVt_PVHV)
863 Perl_croak(aTHX_ "Can't coerce array into hash");
868 Perl_avhv_store_ent(pTHX_ AV *av, SV *keysv, SV *val, U32 hash)
870 return av_store(av, avhv_index(av, keysv, hash), val);
874 Perl_avhv_fetch_ent(pTHX_ AV *av, SV *keysv, I32 lval, U32 hash)
876 return av_fetch(av, avhv_index(av, keysv, hash), lval);
880 Perl_avhv_delete_ent(pTHX_ AV *av, SV *keysv, I32 flags, U32 hash)
882 HV *keys = avhv_keys(av);
885 he = hv_fetch_ent(keys, keysv, FALSE, hash);
886 if (!he || !SvOK(HeVAL(he)))
889 return av_delete(av, avhv_index_sv(HeVAL(he)), flags);
892 /* Check for the existence of an element named by a given key.
896 Perl_avhv_exists_ent(pTHX_ AV *av, SV *keysv, U32 hash)
898 HV *keys = avhv_keys(av);
901 he = hv_fetch_ent(keys, keysv, FALSE, hash);
902 if (!he || !SvOK(HeVAL(he)))
905 return av_exists(av, avhv_index_sv(HeVAL(he)));
909 Perl_avhv_iternext(pTHX_ AV *av)
911 HV *keys = avhv_keys(av);
912 return hv_iternext(keys);
916 Perl_avhv_iterval(pTHX_ AV *av, register HE *entry)
918 SV *sv = hv_iterval(avhv_keys(av), entry);
919 return *av_fetch(av, avhv_index_sv(sv), TRUE);