3 * Copyright (c) 1991-1997, 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
27 if (SvRMAGICAL(av) && mg_find((SV*)av,'P'))
28 warn("av_reify called on tied array");
31 while (key > AvFILLp(av) + 1)
32 AvARRAY(av)[--key] = &sv_undef;
34 sv = AvARRAY(av)[--key];
36 if (sv != &sv_undef) {
38 (void)SvREFCNT_inc(sv);
41 key = AvARRAY(av) - AvALLOC(av);
43 AvALLOC(av)[--key] = &sv_undef;
48 av_extend(AV *av, I32 key)
50 dTHR; /* only necessary if we have to extend stack */
52 if (SvRMAGICAL(av) && (mg = mg_find((SV*)av,'P'))) {
56 PUSHSTACKi(PERLSI_MAGIC);
60 PUSHs(sv_2mortal(newSViv(key+1)));
62 perl_call_method("EXTEND", G_SCALAR|G_DISCARD);
68 if (key > AvMAX(av)) {
73 if (AvALLOC(av) != AvARRAY(av)) {
74 ary = AvALLOC(av) + AvFILLp(av) + 1;
75 tmp = AvARRAY(av) - AvALLOC(av);
76 Move(AvARRAY(av), AvALLOC(av), AvFILLp(av)+1, SV*);
78 SvPVX(av) = (char*)AvALLOC(av);
81 ary[--tmp] = &sv_undef;
84 if (key > AvMAX(av) - 10) {
85 newmax = key + AvMAX(av);
91 #ifndef STRANGE_MALLOC
95 #if defined(MYMALLOC) && !defined(PURIFY)
96 newmax = malloced_size((void*)AvALLOC(av))/sizeof(SV*) - 1;
101 newmax = key + AvMAX(av) / 5;
103 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
104 Renew(AvALLOC(av),newmax+1, SV*);
106 bytes = (newmax + 1) * sizeof(SV*);
107 #define MALLOC_OVERHEAD 16
108 tmp = MALLOC_OVERHEAD;
109 while (tmp - MALLOC_OVERHEAD < bytes)
111 tmp -= MALLOC_OVERHEAD;
113 assert(tmp > newmax);
115 New(2,ary, newmax+1, SV*);
116 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
118 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
120 Safefree(AvALLOC(av));
124 ary = AvALLOC(av) + AvMAX(av) + 1;
125 tmp = newmax - AvMAX(av);
126 if (av == curstack) { /* Oops, grew stack (via av_store()?) */
127 stack_sp = AvALLOC(av) + (stack_sp - stack_base);
128 stack_base = AvALLOC(av);
129 stack_max = stack_base + newmax;
133 newmax = key < 3 ? 3 : key;
134 New(2,AvALLOC(av), newmax+1, SV*);
135 ary = AvALLOC(av) + 1;
137 AvALLOC(av)[0] = &sv_undef; /* For the stacks */
141 ary[--tmp] = &sv_undef;
144 SvPVX(av) = (char*)AvALLOC(av);
151 av_fetch(register AV *av, I32 key, I32 lval)
159 key += AvFILL(av) + 1;
164 if (SvRMAGICAL(av)) {
165 if (mg_find((SV*)av,'P')) {
168 mg_copy((SV*)av, sv, 0, key);
174 if (key > AvFILLp(av)) {
181 return av_store(av,key,sv);
183 if (AvARRAY(av)[key] == &sv_undef) {
187 return av_store(av,key,sv);
192 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
193 || SvTYPE(AvARRAY(av)[key]) == SVTYPEMASK)) {
194 AvARRAY(av)[key] = &sv_undef; /* 1/2 reify */
197 return &AvARRAY(av)[key];
201 av_store(register AV *av, I32 key, SV *val)
213 key += AvFILL(av) + 1;
218 if (SvREADONLY(av) && key >= AvFILL(av))
221 if (SvRMAGICAL(av)) {
222 if (mg_find((SV*)av,'P')) {
223 if (val != &sv_undef) {
224 mg_copy((SV*)av, val, 0, key);
230 if (!AvREAL(av) && AvREIFY(av))
235 if (AvFILLp(av) < key) {
238 if (av == curstack && key > stack_sp - stack_base)
239 stack_sp = stack_base + key; /* XPUSH in disguise */
241 ary[++AvFILLp(av)] = &sv_undef;
242 while (AvFILLp(av) < key);
247 SvREFCNT_dec(ary[key]);
249 if (SvSMAGICAL(av)) {
250 if (val != &sv_undef) {
251 MAGIC* mg = SvMAGIC(av);
252 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
264 av = (AV*)NEWSV(3,0);
265 sv_upgrade((SV *)av, SVt_PVAV);
269 AvMAX(av) = AvFILLp(av) = -1;
274 av_make(register I32 size, register SV **strp)
280 av = (AV*)NEWSV(8,0);
281 sv_upgrade((SV *) av,SVt_PVAV);
282 AvFLAGS(av) = AVf_REAL;
283 if (size) { /* `defined' was returning undef for size==0 anyway. */
286 SvPVX(av) = (char*)ary;
287 AvFILLp(av) = size - 1;
288 AvMAX(av) = size - 1;
289 for (i = 0; i < size; i++) {
292 sv_setsv(ary[i], *strp);
300 av_fake(register I32 size, register SV **strp)
305 av = (AV*)NEWSV(9,0);
306 sv_upgrade((SV *)av, SVt_PVAV);
307 New(4,ary,size+1,SV*);
309 Copy(strp,ary,size,SV*);
310 AvFLAGS(av) = AVf_REIFY;
311 SvPVX(av) = (char*)ary;
312 AvFILLp(av) = size - 1;
313 AvMAX(av) = size - 1;
323 av_clear(register AV *av)
329 if (SvREFCNT(av) <= 0) {
330 warn("Attempt to clear deleted array");
340 /* Give any tie a chance to cleanup first */
349 key = AvFILLp(av) + 1;
351 SvREFCNT_dec(ary[--key]);
352 ary[key] = &sv_undef;
355 if (key = AvARRAY(av) - AvALLOC(av)) {
357 SvPVX(av) = (char*)AvALLOC(av);
364 av_undef(register AV *av)
372 /* Give any tie a chance to cleanup first */
373 if (SvRMAGICAL(av) && mg_find((SV*)av,'P'))
374 av_fill(av, -1); /* mg_clear() ? */
377 key = AvFILLp(av) + 1;
379 SvREFCNT_dec(AvARRAY(av)[--key]);
381 Safefree(AvALLOC(av));
384 AvMAX(av) = AvFILLp(av) = -1;
386 SvREFCNT_dec(AvARYLEN(av));
392 av_push(register AV *av, SV *val)
400 if (SvRMAGICAL(av) && (mg = mg_find((SV*)av,'P'))) {
402 PUSHSTACKi(PERLSI_MAGIC);
409 perl_call_method("PUSH", G_SCALAR|G_DISCARD);
414 av_store(av,AvFILLp(av)+1,val);
418 av_pop(register AV *av)
423 if (!av || AvFILL(av) < 0)
427 if (SvRMAGICAL(av) && (mg = mg_find((SV*)av,'P'))) {
429 PUSHSTACKi(PERLSI_MAGIC);
434 if (perl_call_method("POP", G_SCALAR)) {
435 retval = newSVsv(*stack_sp--);
443 retval = AvARRAY(av)[AvFILLp(av)];
444 AvARRAY(av)[AvFILLp(av)--] = &sv_undef;
451 av_unshift(register AV *av, register I32 num)
462 if (SvRMAGICAL(av) && (mg = mg_find((SV*)av,'P'))) {
464 PUSHSTACKi(PERLSI_MAGIC);
473 perl_call_method("UNSHIFT", G_SCALAR|G_DISCARD);
479 if (!AvREAL(av) && AvREIFY(av))
481 i = AvARRAY(av) - AvALLOC(av);
489 SvPVX(av) = (char*)(AvARRAY(av) - i);
493 av_extend(av, i + num);
496 Move(ary, ary + num, i + 1, SV*);
498 ary[--num] = &sv_undef;
504 av_shift(register AV *av)
509 if (!av || AvFILL(av) < 0)
513 if (SvRMAGICAL(av) && (mg = mg_find((SV*)av,'P'))) {
515 PUSHSTACKi(PERLSI_MAGIC);
520 if (perl_call_method("SHIFT", G_SCALAR)) {
521 retval = newSVsv(*stack_sp--);
529 retval = *AvARRAY(av);
531 *AvARRAY(av) = &sv_undef;
532 SvPVX(av) = (char*)(AvARRAY(av) + 1);
541 av_len(register AV *av)
547 av_fill(register AV *av, I32 fill)
551 croak("panic: null array");
554 if (SvRMAGICAL(av) && (mg = mg_find((SV*)av,'P'))) {
558 PUSHSTACKi(PERLSI_MAGIC);
562 PUSHs(sv_2mortal(newSViv(fill+1)));
564 perl_call_method("STORESIZE", G_SCALAR|G_DISCARD);
570 if (fill <= AvMAX(av)) {
571 I32 key = AvFILLp(av);
572 SV** ary = AvARRAY(av);
576 SvREFCNT_dec(ary[key]);
577 ary[key--] = &sv_undef;
582 ary[++key] = &sv_undef;
590 (void)av_store(av,fill,&sv_undef);
594 /* AVHV: Support for treating arrays as if they were hashes. The
595 * first element of the array should be a hash reference that maps
596 * hash keys to array indices.
600 avhv_index_sv(SV* sv)
602 I32 index = SvIV(sv);
604 croak("Bad index while coercing array into hash");
611 SV **keysp = av_fetch(av, 0, FALSE);
618 if (SvTYPE(sv) == SVt_PVHV)
622 croak("Can't coerce array into hash");
627 avhv_fetch_ent(AV *av, SV *keysv, I32 lval, U32 hash)
630 HV *keys = avhv_keys(av);
633 he = hv_fetch_ent(keys, keysv, FALSE, hash);
635 croak("No such array field");
636 return av_fetch(av, avhv_index_sv(HeVAL(he)), lval);
640 avhv_exists_ent(AV *av, SV *keysv, U32 hash)
642 HV *keys = avhv_keys(av);
643 return hv_exists_ent(keys, keysv, hash);
647 avhv_iternext(AV *av)
649 HV *keys = avhv_keys(av);
650 return hv_iternext(keys);
654 avhv_iterval(AV *av, register HE *entry)
656 SV *sv = hv_iterval(avhv_keys(av), entry);
657 return *av_fetch(av, avhv_index_sv(sv), TRUE);