3 * Copyright (c) 1991-1999, 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;
50 Perl_av_extend(pTHX_ AV *av, I32 key)
52 dTHR; /* only necessary if we have to extend stack */
54 if (mg = SvTIED_mg((SV*)av, 'P')) {
58 PUSHSTACKi(PERLSI_MAGIC);
61 PUSHs(SvTIED_obj((SV*)av, mg));
62 PUSHs(sv_2mortal(newSViv(key+1)));
64 call_method("EXTEND", G_SCALAR|G_DISCARD);
70 if (key > AvMAX(av)) {
75 if (AvALLOC(av) != AvARRAY(av)) {
76 ary = AvALLOC(av) + AvFILLp(av) + 1;
77 tmp = AvARRAY(av) - AvALLOC(av);
78 Move(AvARRAY(av), AvALLOC(av), AvFILLp(av)+1, SV*);
80 SvPVX(av) = (char*)AvALLOC(av);
83 ary[--tmp] = &PL_sv_undef;
86 if (key > AvMAX(av) - 10) {
87 newmax = key + AvMAX(av);
93 #ifndef STRANGE_MALLOC
98 #if defined(MYMALLOC) && !defined(PURIFY) && !defined(LEAKTEST)
99 newmax = malloced_size((void*)AvALLOC(av))/sizeof(SV*) - 1;
104 newmax = key + AvMAX(av) / 5;
106 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
107 Renew(AvALLOC(av),newmax+1, SV*);
109 bytes = (newmax + 1) * sizeof(SV*);
110 #define MALLOC_OVERHEAD 16
111 itmp = MALLOC_OVERHEAD;
112 while (itmp - MALLOC_OVERHEAD < bytes)
114 itmp -= MALLOC_OVERHEAD;
116 assert(itmp > newmax);
118 assert(newmax >= AvMAX(av));
119 New(2,ary, newmax+1, SV*);
120 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
122 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
124 Safefree(AvALLOC(av));
128 ary = AvALLOC(av) + AvMAX(av) + 1;
129 tmp = newmax - AvMAX(av);
130 if (av == PL_curstack) { /* Oops, grew stack (via av_store()?) */
131 PL_stack_sp = AvALLOC(av) + (PL_stack_sp - PL_stack_base);
132 PL_stack_base = AvALLOC(av);
133 PL_stack_max = PL_stack_base + newmax;
137 newmax = key < 3 ? 3 : key;
138 New(2,AvALLOC(av), newmax+1, SV*);
139 ary = AvALLOC(av) + 1;
141 AvALLOC(av)[0] = &PL_sv_undef; /* For the stacks */
145 ary[--tmp] = &PL_sv_undef;
148 SvPVX(av) = (char*)AvALLOC(av);
155 Perl_av_fetch(pTHX_ register AV *av, I32 key, I32 lval)
163 key += AvFILL(av) + 1;
168 if (SvRMAGICAL(av)) {
169 if (mg_find((SV*)av,'P') || mg_find((SV*)av,'D')) {
172 mg_copy((SV*)av, sv, 0, key);
174 return &PL_av_fetch_sv;
178 if (key > AvFILLp(av)) {
182 return av_store(av,key,sv);
184 if (AvARRAY(av)[key] == &PL_sv_undef) {
188 return av_store(av,key,sv);
193 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
194 || SvTYPE(AvARRAY(av)[key]) == SVTYPEMASK)) {
195 AvARRAY(av)[key] = &PL_sv_undef; /* 1/2 reify */
198 return &AvARRAY(av)[key];
202 Perl_av_store(pTHX_ register AV *av, I32 key, SV *val)
214 key += AvFILL(av) + 1;
219 if (SvREADONLY(av) && key >= AvFILL(av))
220 Perl_croak(aTHX_ PL_no_modify);
222 if (SvRMAGICAL(av)) {
223 if (mg_find((SV*)av,'P')) {
224 if (val != &PL_sv_undef) {
225 mg_copy((SV*)av, val, 0, key);
231 if (!AvREAL(av) && AvREIFY(av))
236 if (AvFILLp(av) < key) {
239 if (av == PL_curstack && key > PL_stack_sp - PL_stack_base)
240 PL_stack_sp = PL_stack_base + key; /* XPUSH in disguise */
242 ary[++AvFILLp(av)] = &PL_sv_undef;
243 while (AvFILLp(av) < key);
248 SvREFCNT_dec(ary[key]);
250 if (SvSMAGICAL(av)) {
251 if (val != &PL_sv_undef) {
252 MAGIC* mg = SvMAGIC(av);
253 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
265 av = (AV*)NEWSV(3,0);
266 sv_upgrade((SV *)av, SVt_PVAV);
270 AvMAX(av) = AvFILLp(av) = -1;
275 Perl_av_make(pTHX_ register I32 size, register SV **strp)
281 av = (AV*)NEWSV(8,0);
282 sv_upgrade((SV *) av,SVt_PVAV);
283 AvFLAGS(av) = AVf_REAL;
284 if (size) { /* `defined' was returning undef for size==0 anyway. */
287 SvPVX(av) = (char*)ary;
288 AvFILLp(av) = size - 1;
289 AvMAX(av) = size - 1;
290 for (i = 0; i < size; i++) {
293 sv_setsv(ary[i], *strp);
301 Perl_av_fake(pTHX_ register I32 size, register SV **strp)
306 av = (AV*)NEWSV(9,0);
307 sv_upgrade((SV *)av, SVt_PVAV);
308 New(4,ary,size+1,SV*);
310 Copy(strp,ary,size,SV*);
311 AvFLAGS(av) = AVf_REIFY;
312 SvPVX(av) = (char*)ary;
313 AvFILLp(av) = size - 1;
314 AvMAX(av) = size - 1;
324 Perl_av_clear(pTHX_ register AV *av)
330 if (SvREFCNT(av) <= 0 && ckWARN_d(WARN_DEBUGGING)) {
331 Perl_warner(aTHX_ WARN_DEBUGGING, "Attempt to clear deleted array");
339 Perl_croak(aTHX_ PL_no_modify);
341 /* Give any tie a chance to cleanup first */
350 key = AvFILLp(av) + 1;
352 SvREFCNT_dec(ary[--key]);
353 ary[key] = &PL_sv_undef;
356 if (key = AvARRAY(av) - AvALLOC(av)) {
358 SvPVX(av) = (char*)AvALLOC(av);
365 Perl_av_undef(pTHX_ register AV *av)
373 /* Give any tie a chance to cleanup first */
374 if (SvTIED_mg((SV*)av, 'P'))
375 av_fill(av, -1); /* mg_clear() ? */
378 key = AvFILLp(av) + 1;
380 SvREFCNT_dec(AvARRAY(av)[--key]);
382 Safefree(AvALLOC(av));
385 AvMAX(av) = AvFILLp(av) = -1;
387 SvREFCNT_dec(AvARYLEN(av));
393 Perl_av_push(pTHX_ register AV *av, SV *val)
399 Perl_croak(aTHX_ PL_no_modify);
401 if (mg = SvTIED_mg((SV*)av, 'P')) {
403 PUSHSTACKi(PERLSI_MAGIC);
406 PUSHs(SvTIED_obj((SV*)av, mg));
410 call_method("PUSH", G_SCALAR|G_DISCARD);
415 av_store(av,AvFILLp(av)+1,val);
419 Perl_av_pop(pTHX_ register AV *av)
424 if (!av || AvFILL(av) < 0)
427 Perl_croak(aTHX_ PL_no_modify);
428 if (mg = SvTIED_mg((SV*)av, 'P')) {
430 PUSHSTACKi(PERLSI_MAGIC);
432 XPUSHs(SvTIED_obj((SV*)av, mg));
435 if (call_method("POP", G_SCALAR)) {
436 retval = newSVsv(*PL_stack_sp--);
438 retval = &PL_sv_undef;
444 retval = AvARRAY(av)[AvFILLp(av)];
445 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
452 Perl_av_unshift(pTHX_ register AV *av, register I32 num)
461 Perl_croak(aTHX_ PL_no_modify);
463 if (mg = SvTIED_mg((SV*)av, 'P')) {
465 PUSHSTACKi(PERLSI_MAGIC);
468 PUSHs(SvTIED_obj((SV*)av, mg));
474 call_method("UNSHIFT", G_SCALAR|G_DISCARD);
480 if (!AvREAL(av) && AvREIFY(av))
482 i = AvARRAY(av) - AvALLOC(av);
490 SvPVX(av) = (char*)(AvARRAY(av) - i);
494 av_extend(av, i + num);
497 Move(ary, ary + num, i + 1, SV*);
499 ary[--num] = &PL_sv_undef;
505 Perl_av_shift(pTHX_ register AV *av)
510 if (!av || AvFILL(av) < 0)
513 Perl_croak(aTHX_ PL_no_modify);
514 if (mg = SvTIED_mg((SV*)av, 'P')) {
516 PUSHSTACKi(PERLSI_MAGIC);
518 XPUSHs(SvTIED_obj((SV*)av, mg));
521 if (call_method("SHIFT", G_SCALAR)) {
522 retval = newSVsv(*PL_stack_sp--);
524 retval = &PL_sv_undef;
530 retval = *AvARRAY(av);
532 *AvARRAY(av) = &PL_sv_undef;
533 SvPVX(av) = (char*)(AvARRAY(av) + 1);
542 Perl_av_len(pTHX_ register AV *av)
548 Perl_av_fill(pTHX_ register AV *av, I32 fill)
552 Perl_croak(aTHX_ "panic: null array");
555 if (mg = SvTIED_mg((SV*)av, 'P')) {
559 PUSHSTACKi(PERLSI_MAGIC);
562 PUSHs(SvTIED_obj((SV*)av, mg));
563 PUSHs(sv_2mortal(newSViv(fill+1)));
565 call_method("STORESIZE", G_SCALAR|G_DISCARD);
571 if (fill <= AvMAX(av)) {
572 I32 key = AvFILLp(av);
573 SV** ary = AvARRAY(av);
577 SvREFCNT_dec(ary[key]);
578 ary[key--] = &PL_sv_undef;
583 ary[++key] = &PL_sv_undef;
591 (void)av_store(av,fill,&PL_sv_undef);
595 /* AVHV: Support for treating arrays as if they were hashes. The
596 * first element of the array should be a hash reference that maps
597 * hash keys to array indices.
601 S_avhv_index_sv(pTHX_ SV* sv)
603 I32 index = SvIV(sv);
605 Perl_croak(aTHX_ "Bad index while coercing array into hash");
610 Perl_avhv_keys(pTHX_ AV *av)
612 SV **keysp = av_fetch(av, 0, FALSE);
619 if (SvTYPE(sv) == SVt_PVHV)
623 Perl_croak(aTHX_ "Can't coerce array into hash");
628 Perl_avhv_fetch_ent(pTHX_ AV *av, SV *keysv, I32 lval, U32 hash)
631 HV *keys = avhv_keys(av);
635 he = hv_fetch_ent(keys, keysv, FALSE, hash);
637 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\"", SvPV(keysv,n_a));
638 return av_fetch(av, avhv_index_sv(HeVAL(he)), lval);
641 /* Check for the existence of an element named by a given key.
643 * This relies on the fact that uninitialized array elements
644 * are set to &PL_sv_undef.
647 Perl_avhv_exists_ent(pTHX_ AV *av, SV *keysv, U32 hash)
649 HV *keys = avhv_keys(av);
653 he = hv_fetch_ent(keys, keysv, FALSE, hash);
654 if (!he || !SvOK(HeVAL(he)))
657 ix = SvIV(HeVAL(he));
659 /* If the array hasn't been extended to reach the key yet then
660 * it hasn't been accessed and thus does not exist. We use
661 * AvFILL() rather than AvFILLp() to handle tied av. */
662 if (ix > 0 && ix <= AvFILL(av)
664 || (AvARRAY(av)[ix] && AvARRAY(av)[ix] != &PL_sv_undef)))
672 Perl_avhv_iternext(pTHX_ AV *av)
674 HV *keys = avhv_keys(av);
675 return hv_iternext(keys);
679 Perl_avhv_iterval(pTHX_ AV *av, register HE *entry)
681 SV *sv = hv_iterval(avhv_keys(av), entry);
682 return *av_fetch(av, avhv_index_sv(sv), TRUE);