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'))) {
59 PUSHs(sv_2mortal(newSViv(key+1)));
61 perl_call_method("EXTEND", G_SCALAR|G_DISCARD);
66 if (key > AvMAX(av)) {
71 if (AvALLOC(av) != AvARRAY(av)) {
72 ary = AvALLOC(av) + AvFILLp(av) + 1;
73 tmp = AvARRAY(av) - AvALLOC(av);
74 Move(AvARRAY(av), AvALLOC(av), AvFILLp(av)+1, SV*);
76 SvPVX(av) = (char*)AvALLOC(av);
79 ary[--tmp] = &sv_undef;
82 if (key > AvMAX(av) - 10) {
83 newmax = key + AvMAX(av);
89 #ifndef STRANGE_MALLOC
93 newmax = key + AvMAX(av) / 5;
96 Renew(AvALLOC(av),newmax+1, SV*);
98 bytes = (newmax + 1) * sizeof(SV*);
99 #define MALLOC_OVERHEAD 16
100 tmp = MALLOC_OVERHEAD;
101 while (tmp - MALLOC_OVERHEAD < bytes)
103 tmp -= MALLOC_OVERHEAD;
105 assert(tmp > newmax);
107 New(2,ary, newmax+1, SV*);
108 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
110 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
112 Safefree(AvALLOC(av));
115 ary = AvALLOC(av) + AvMAX(av) + 1;
116 tmp = newmax - AvMAX(av);
117 if (av == curstack) { /* Oops, grew stack (via av_store()?) */
118 stack_sp = AvALLOC(av) + (stack_sp - stack_base);
119 stack_base = AvALLOC(av);
120 stack_max = stack_base + newmax;
124 newmax = key < 4 ? 4 : key;
125 New(2,AvALLOC(av), newmax+1, SV*);
126 ary = AvALLOC(av) + 1;
128 AvALLOC(av)[0] = &sv_undef; /* For the stacks */
132 ary[--tmp] = &sv_undef;
135 SvPVX(av) = (char*)AvALLOC(av);
142 av_fetch(register AV *av, I32 key, I32 lval)
150 key += AvFILL(av) + 1;
155 if (SvRMAGICAL(av)) {
156 if (mg_find((SV*)av,'P')) {
159 mg_copy((SV*)av, sv, 0, key);
165 if (key > AvFILLp(av)) {
172 return av_store(av,key,sv);
174 if (AvARRAY(av)[key] == &sv_undef) {
178 return av_store(av,key,sv);
183 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
184 || SvTYPE(AvARRAY(av)[key]) == SVTYPEMASK)) {
185 AvARRAY(av)[key] = &sv_undef; /* 1/2 reify */
188 return &AvARRAY(av)[key];
192 av_store(register AV *av, I32 key, SV *val)
204 key += AvFILL(av) + 1;
209 if (SvREADONLY(av) && key >= AvFILL(av))
212 if (SvRMAGICAL(av)) {
213 if (mg_find((SV*)av,'P')) {
214 if (val != &sv_undef) {
215 mg_copy((SV*)av, val, 0, key);
221 if (!AvREAL(av) && AvREIFY(av))
226 if (AvFILLp(av) < key) {
229 if (av == curstack && key > stack_sp - stack_base)
230 stack_sp = stack_base + key; /* XPUSH in disguise */
232 ary[++AvFILLp(av)] = &sv_undef;
233 while (AvFILLp(av) < key);
238 SvREFCNT_dec(ary[key]);
240 if (SvSMAGICAL(av)) {
241 if (val != &sv_undef) {
242 MAGIC* mg = SvMAGIC(av);
243 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
255 av = (AV*)NEWSV(3,0);
256 sv_upgrade((SV *)av, SVt_PVAV);
260 AvMAX(av) = AvFILLp(av) = -1;
265 av_make(register I32 size, register SV **strp)
271 av = (AV*)NEWSV(8,0);
272 sv_upgrade((SV *) av,SVt_PVAV);
273 AvFLAGS(av) = AVf_REAL;
274 if (size) { /* `defined' was returning undef for size==0 anyway. */
277 SvPVX(av) = (char*)ary;
278 AvFILLp(av) = size - 1;
279 AvMAX(av) = size - 1;
280 for (i = 0; i < size; i++) {
283 sv_setsv(ary[i], *strp);
291 av_fake(register I32 size, register SV **strp)
296 av = (AV*)NEWSV(9,0);
297 sv_upgrade((SV *)av, SVt_PVAV);
298 New(4,ary,size+1,SV*);
300 Copy(strp,ary,size,SV*);
301 AvFLAGS(av) = AVf_REIFY;
302 SvPVX(av) = (char*)ary;
303 AvFILLp(av) = size - 1;
304 AvMAX(av) = size - 1;
314 av_clear(register AV *av)
320 if (SvREFCNT(av) <= 0) {
321 warn("Attempt to clear deleted array");
328 /* Give any tie a chance to cleanup first */
337 key = AvFILLp(av) + 1;
339 SvREFCNT_dec(ary[--key]);
340 ary[key] = &sv_undef;
343 if (key = AvARRAY(av) - AvALLOC(av)) {
345 SvPVX(av) = (char*)AvALLOC(av);
352 av_undef(register AV *av)
360 /* Give any tie a chance to cleanup first */
361 if (SvRMAGICAL(av) && mg_find((SV*)av,'P'))
362 av_fill(av, -1); /* mg_clear() ? */
365 key = AvFILLp(av) + 1;
367 SvREFCNT_dec(AvARRAY(av)[--key]);
369 Safefree(AvALLOC(av));
373 AvMAX(av) = AvFILLp(av) = -1;
375 SvREFCNT_dec(AvARYLEN(av));
381 av_push(register AV *av, SV *val)
389 if (SvRMAGICAL(av) && (mg = mg_find((SV*)av,'P'))) {
397 perl_call_method("PUSH", G_SCALAR|G_DISCARD);
401 av_store(av,AvFILLp(av)+1,val);
405 av_pop(register AV *av)
410 if (!av || AvFILL(av) < 0)
414 if (SvRMAGICAL(av) && (mg = mg_find((SV*)av,'P'))) {
420 if (perl_call_method("POP", G_SCALAR)) {
421 retval = newSVsv(*stack_sp--);
428 retval = AvARRAY(av)[AvFILLp(av)];
429 AvARRAY(av)[AvFILLp(av)--] = &sv_undef;
436 av_unshift(register AV *av, register I32 num)
447 if (SvRMAGICAL(av) && (mg = mg_find((SV*)av,'P'))) {
457 perl_call_method("UNSHIFT", G_SCALAR|G_DISCARD);
462 if (!AvREAL(av) && AvREIFY(av))
464 i = AvARRAY(av) - AvALLOC(av);
472 SvPVX(av) = (char*)(AvARRAY(av) - i);
476 av_extend(av, i + num);
479 Move(ary, ary + num, i + 1, SV*);
481 ary[--num] = &sv_undef;
487 av_shift(register AV *av)
492 if (!av || AvFILL(av) < 0)
496 if (SvRMAGICAL(av) && (mg = mg_find((SV*)av,'P'))) {
502 if (perl_call_method("SHIFT", G_SCALAR)) {
503 retval = newSVsv(*stack_sp--);
510 retval = *AvARRAY(av);
512 *AvARRAY(av) = &sv_undef;
513 SvPVX(av) = (char*)(AvARRAY(av) + 1);
522 av_len(register AV *av)
528 av_fill(register AV *av, I32 fill)
532 croak("panic: null array");
535 if (SvRMAGICAL(av) && (mg = mg_find((SV*)av,'P'))) {
542 PUSHs(sv_2mortal(newSViv(fill+1)));
544 perl_call_method("STORESIZE", G_SCALAR|G_DISCARD);
549 if (fill <= AvMAX(av)) {
550 I32 key = AvFILLp(av);
551 SV** ary = AvARRAY(av);
555 SvREFCNT_dec(ary[key]);
556 ary[key--] = &sv_undef;
561 ary[++key] = &sv_undef;
569 (void)av_store(av,fill,&sv_undef);
579 keysp = av_fetch(av, 0, FALSE);
586 if (SvTYPE(sv) == SVt_PVHV)
591 croak("Can't coerce array into hash");
596 avhv_fetch(AV *av, char *key, U32 klen, I32 lval)
599 HV *keys = avhv_keys(av);
602 indsvp = hv_fetch(keys, key, klen, FALSE);
606 croak("Bad index while coercing array into hash");
611 ind = AvFILL(av) + 1;
612 hv_store(keys, key, klen, newSViv(ind), 0);
614 return av_fetch(av, ind, lval);
618 avhv_fetch_ent(AV *av, SV *keysv, I32 lval, U32 hash)
621 HV *keys = avhv_keys(av);
625 he = hv_fetch_ent(keys, keysv, FALSE, hash);
627 ind = SvIV(HeVAL(he));
629 croak("Bad index while coercing array into hash");
634 ind = AvFILL(av) + 1;
635 hv_store_ent(keys, keysv, newSViv(ind), 0);
637 return av_fetch(av, ind, lval);
641 avhv_store(AV *av, char *key, U32 klen, SV *val, U32 hash)
644 HV *keys = avhv_keys(av);
647 indsvp = hv_fetch(keys, key, klen, FALSE);
651 croak("Bad index while coercing array into hash");
653 ind = AvFILL(av) + 1;
654 hv_store(keys, key, klen, newSViv(ind), hash);
656 return av_store(av, ind, val);
660 avhv_store_ent(AV *av, SV *keysv, SV *val, U32 hash)
662 HV *keys = avhv_keys(av);
666 he = hv_fetch_ent(keys, keysv, FALSE, hash);
668 ind = SvIV(HeVAL(he));
670 croak("Bad index while coercing array into hash");
672 ind = AvFILL(av) + 1;
673 hv_store_ent(keys, keysv, newSViv(ind), hash);
675 return av_store(av, ind, val);
679 avhv_exists_ent(AV *av, SV *keysv, U32 hash)
681 HV *keys = avhv_keys(av);
682 return hv_exists_ent(keys, keysv, hash);
686 avhv_exists(AV *av, char *key, U32 klen)
688 HV *keys = avhv_keys(av);
689 return hv_exists(keys, key, klen);
692 /* avhv_delete leaks. Caller can re-index and compress if so desired. */
694 avhv_delete(AV *av, char *key, U32 klen, I32 flags)
696 HV *keys = avhv_keys(av);
701 sv = hv_delete(keys, key, klen, 0);
706 croak("Bad index while coercing array into hash");
707 svp = av_fetch(av, ind, FALSE);
710 if (flags & G_DISCARD) {
714 sv = sv_2mortal(*svp);
720 /* avhv_delete_ent leaks. Caller can re-index and compress if so desired. */
722 avhv_delete_ent(AV *av, SV *keysv, I32 flags, U32 hash)
724 HV *keys = avhv_keys(av);
729 sv = hv_delete_ent(keys, keysv, 0, hash);
734 croak("Bad index while coercing array into hash");
735 svp = av_fetch(av, ind, FALSE);
738 if (flags & G_DISCARD) {
742 sv = sv_2mortal(*svp);
749 avhv_iterinit(AV *av)
751 HV *keys = avhv_keys(av);
752 return hv_iterinit(keys);
756 avhv_iternext(AV *av)
758 HV *keys = avhv_keys(av);
759 return hv_iternext(keys);
763 avhv_iterval(AV *av, register HE *entry)
765 HV *keys = avhv_keys(av);
769 sv = hv_iterval(keys, entry);
772 croak("Bad index while coercing array into hash");
773 return *av_fetch(av, ind, TRUE);
777 avhv_iternextsv(AV *av, char **key, I32 *retlen)
779 HV *keys = avhv_keys(av);
784 he = hv_iternext(keys);
787 *key = hv_iterkey(he, retlen);
788 sv = hv_iterval(keys, he);
791 croak("Bad index while coercing array into hash");
792 return *av_fetch(av, ind, TRUE);