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 (SvTIED_mg((SV*)av, 'P'))
28 warn("av_reify called on tied array");
31 while (key > AvFILLp(av) + 1)
32 AvARRAY(av)[--key] = &PL_sv_undef;
34 sv = AvARRAY(av)[--key];
36 if (sv != &PL_sv_undef) {
38 (void)SvREFCNT_inc(sv);
41 key = AvARRAY(av) - AvALLOC(av);
43 AvALLOC(av)[--key] = &PL_sv_undef;
49 av_extend(AV *av, I32 key)
51 dTHR; /* only necessary if we have to extend stack */
53 if (mg = SvTIED_mg((SV*)av, 'P')) {
57 PUSHSTACKi(PERLSI_MAGIC);
60 PUSHs(SvTIED_obj((SV*)av, mg));
61 PUSHs(sv_2mortal(newSViv(key+1)));
63 perl_call_method("EXTEND", G_SCALAR|G_DISCARD);
69 if (key > AvMAX(av)) {
74 if (AvALLOC(av) != AvARRAY(av)) {
75 ary = AvALLOC(av) + AvFILLp(av) + 1;
76 tmp = AvARRAY(av) - AvALLOC(av);
77 Move(AvARRAY(av), AvALLOC(av), AvFILLp(av)+1, SV*);
79 SvPVX(av) = (char*)AvALLOC(av);
82 ary[--tmp] = &PL_sv_undef;
85 if (key > AvMAX(av) - 10) {
86 newmax = key + AvMAX(av);
92 #ifndef STRANGE_MALLOC
96 #if defined(MYMALLOC) && !defined(PURIFY) && !defined(LEAKTEST)
97 newmax = malloced_size((void*)AvALLOC(av))/sizeof(SV*) - 1;
102 newmax = key + AvMAX(av) / 5;
104 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
105 Renew(AvALLOC(av),newmax+1, SV*);
107 bytes = (newmax + 1) * sizeof(SV*);
108 #define MALLOC_OVERHEAD 16
109 tmp = MALLOC_OVERHEAD;
110 while (tmp - MALLOC_OVERHEAD < bytes)
112 tmp -= MALLOC_OVERHEAD;
114 assert(tmp > newmax);
116 New(2,ary, newmax+1, SV*);
117 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
119 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
121 Safefree(AvALLOC(av));
125 ary = AvALLOC(av) + AvMAX(av) + 1;
126 tmp = newmax - AvMAX(av);
127 if (av == PL_curstack) { /* Oops, grew stack (via av_store()?) */
128 PL_stack_sp = AvALLOC(av) + (PL_stack_sp - PL_stack_base);
129 PL_stack_base = AvALLOC(av);
130 PL_stack_max = PL_stack_base + newmax;
134 newmax = key < 3 ? 3 : key;
135 New(2,AvALLOC(av), newmax+1, SV*);
136 ary = AvALLOC(av) + 1;
138 AvALLOC(av)[0] = &PL_sv_undef; /* For the stacks */
142 ary[--tmp] = &PL_sv_undef;
145 SvPVX(av) = (char*)AvALLOC(av);
152 av_fetch(register AV *av, I32 key, I32 lval)
160 key += AvFILL(av) + 1;
165 if (SvRMAGICAL(av)) {
166 if (mg_find((SV*)av,'P') || mg_find((SV*)av,'D')) {
169 mg_copy((SV*)av, sv, 0, key);
171 return &PL_av_fetch_sv;
175 if (key > AvFILLp(av)) {
179 return av_store(av,key,sv);
181 if (AvARRAY(av)[key] == &PL_sv_undef) {
185 return av_store(av,key,sv);
190 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
191 || SvTYPE(AvARRAY(av)[key]) == SVTYPEMASK)) {
192 AvARRAY(av)[key] = &PL_sv_undef; /* 1/2 reify */
195 return &AvARRAY(av)[key];
199 av_store(register AV *av, I32 key, SV *val)
211 key += AvFILL(av) + 1;
216 if (SvREADONLY(av) && key >= AvFILL(av))
219 if (SvRMAGICAL(av)) {
220 if (mg_find((SV*)av,'P')) {
221 if (val != &PL_sv_undef) {
222 mg_copy((SV*)av, val, 0, key);
228 if (!AvREAL(av) && AvREIFY(av))
233 if (AvFILLp(av) < key) {
236 if (av == PL_curstack && key > PL_stack_sp - PL_stack_base)
237 PL_stack_sp = PL_stack_base + key; /* XPUSH in disguise */
239 ary[++AvFILLp(av)] = &PL_sv_undef;
240 while (AvFILLp(av) < key);
245 SvREFCNT_dec(ary[key]);
247 if (SvSMAGICAL(av)) {
248 if (val != &PL_sv_undef) {
249 MAGIC* mg = SvMAGIC(av);
250 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
262 av = (AV*)NEWSV(3,0);
263 sv_upgrade((SV *)av, SVt_PVAV);
267 AvMAX(av) = AvFILLp(av) = -1;
272 av_make(register I32 size, register SV **strp)
278 av = (AV*)NEWSV(8,0);
279 sv_upgrade((SV *) av,SVt_PVAV);
280 AvFLAGS(av) = AVf_REAL;
281 if (size) { /* `defined' was returning undef for size==0 anyway. */
284 SvPVX(av) = (char*)ary;
285 AvFILLp(av) = size - 1;
286 AvMAX(av) = size - 1;
287 for (i = 0; i < size; i++) {
290 sv_setsv(ary[i], *strp);
298 av_fake(register I32 size, register SV **strp)
303 av = (AV*)NEWSV(9,0);
304 sv_upgrade((SV *)av, SVt_PVAV);
305 New(4,ary,size+1,SV*);
307 Copy(strp,ary,size,SV*);
308 AvFLAGS(av) = AVf_REIFY;
309 SvPVX(av) = (char*)ary;
310 AvFILLp(av) = size - 1;
311 AvMAX(av) = size - 1;
321 av_clear(register AV *av)
327 if (SvREFCNT(av) <= 0) {
328 warn("Attempt to clear deleted array");
338 /* Give any tie a chance to cleanup first */
347 key = AvFILLp(av) + 1;
349 SvREFCNT_dec(ary[--key]);
350 ary[key] = &PL_sv_undef;
353 if (key = AvARRAY(av) - AvALLOC(av)) {
355 SvPVX(av) = (char*)AvALLOC(av);
362 av_undef(register AV *av)
370 /* Give any tie a chance to cleanup first */
371 if (SvTIED_mg((SV*)av, 'P'))
372 av_fill(av, -1); /* mg_clear() ? */
375 key = AvFILLp(av) + 1;
377 SvREFCNT_dec(AvARRAY(av)[--key]);
379 Safefree(AvALLOC(av));
382 AvMAX(av) = AvFILLp(av) = -1;
384 SvREFCNT_dec(AvARYLEN(av));
390 av_push(register AV *av, SV *val)
398 if (mg = SvTIED_mg((SV*)av, 'P')) {
400 PUSHSTACKi(PERLSI_MAGIC);
403 PUSHs(SvTIED_obj((SV*)av, mg));
407 perl_call_method("PUSH", G_SCALAR|G_DISCARD);
412 av_store(av,AvFILLp(av)+1,val);
416 av_pop(register AV *av)
421 if (!av || AvFILL(av) < 0)
425 if (mg = SvTIED_mg((SV*)av, 'P')) {
427 PUSHSTACKi(PERLSI_MAGIC);
429 XPUSHs(SvTIED_obj((SV*)av, mg));
432 if (perl_call_method("POP", G_SCALAR)) {
433 retval = newSVsv(*PL_stack_sp--);
435 retval = &PL_sv_undef;
441 retval = AvARRAY(av)[AvFILLp(av)];
442 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
449 av_unshift(register AV *av, register I32 num)
460 if (mg = SvTIED_mg((SV*)av, 'P')) {
462 PUSHSTACKi(PERLSI_MAGIC);
465 PUSHs(SvTIED_obj((SV*)av, mg));
471 perl_call_method("UNSHIFT", G_SCALAR|G_DISCARD);
477 if (!AvREAL(av) && AvREIFY(av))
479 i = AvARRAY(av) - AvALLOC(av);
487 SvPVX(av) = (char*)(AvARRAY(av) - i);
491 av_extend(av, i + num);
494 Move(ary, ary + num, i + 1, SV*);
496 ary[--num] = &PL_sv_undef;
502 av_shift(register AV *av)
507 if (!av || AvFILL(av) < 0)
511 if (mg = SvTIED_mg((SV*)av, 'P')) {
513 PUSHSTACKi(PERLSI_MAGIC);
515 XPUSHs(SvTIED_obj((SV*)av, mg));
518 if (perl_call_method("SHIFT", G_SCALAR)) {
519 retval = newSVsv(*PL_stack_sp--);
521 retval = &PL_sv_undef;
527 retval = *AvARRAY(av);
529 *AvARRAY(av) = &PL_sv_undef;
530 SvPVX(av) = (char*)(AvARRAY(av) + 1);
539 av_len(register AV *av)
545 av_fill(register AV *av, I32 fill)
549 croak("panic: null array");
552 if (mg = SvTIED_mg((SV*)av, 'P')) {
556 PUSHSTACKi(PERLSI_MAGIC);
559 PUSHs(SvTIED_obj((SV*)av, mg));
560 PUSHs(sv_2mortal(newSViv(fill+1)));
562 perl_call_method("STORESIZE", G_SCALAR|G_DISCARD);
568 if (fill <= AvMAX(av)) {
569 I32 key = AvFILLp(av);
570 SV** ary = AvARRAY(av);
574 SvREFCNT_dec(ary[key]);
575 ary[key--] = &PL_sv_undef;
580 ary[++key] = &PL_sv_undef;
588 (void)av_store(av,fill,&PL_sv_undef);
592 /* AVHV: Support for treating arrays as if they were hashes. The
593 * first element of the array should be a hash reference that maps
594 * hash keys to array indices.
598 avhv_index_sv(SV* sv)
600 I32 index = SvIV(sv);
602 croak("Bad index while coercing array into hash");
609 SV **keysp = av_fetch(av, 0, FALSE);
616 if (SvTYPE(sv) == SVt_PVHV)
620 croak("Can't coerce array into hash");
625 avhv_fetch_ent(AV *av, SV *keysv, I32 lval, U32 hash)
628 HV *keys = avhv_keys(av);
631 he = hv_fetch_ent(keys, keysv, FALSE, hash);
633 croak("No such array field");
634 return av_fetch(av, avhv_index_sv(HeVAL(he)), lval);
638 avhv_exists_ent(AV *av, SV *keysv, U32 hash)
640 HV *keys = avhv_keys(av);
641 return hv_exists_ent(keys, keysv, hash);
645 avhv_iternext(AV *av)
647 HV *keys = avhv_keys(av);
648 return hv_iternext(keys);
652 avhv_iterval(AV *av, register HE *entry)
654 SV *sv = hv_iterval(avhv_keys(av), entry);
655 return *av_fetch(av, avhv_index_sv(sv), TRUE);