Fix test for perl version number in Makefile.PL
[p5sagit/p5-mst-13.2.git] / av.c
CommitLineData
a0d0e21e 1/* av.c
79072805 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
1d325971 4 * 2000, 2001, 2002, 2003, 2004, 2005 by Larry Wall and others
79072805 5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
a0d0e21e 9 */
10
11/*
12 * "...for the Entwives desired order, and plenty, and peace (by which they
13 * meant that things should remain where they had set them)." --Treebeard
79072805 14 */
15
ccfc67b7 16/*
17=head1 Array Manipulation Functions
18*/
19
79072805 20#include "EXTERN.h"
864dbfa3 21#define PERL_IN_AV_C
79072805 22#include "perl.h"
23
fb73857a 24void
864dbfa3 25Perl_av_reify(pTHX_ AV *av)
a0d0e21e 26{
27 I32 key;
fb73857a 28
ba5d1d60 29 assert(av);
30
3c78fafa 31 if (AvREAL(av))
32 return;
93965878 33#ifdef DEBUGGING
14befaf4 34 if (SvTIED_mg((SV*)av, PERL_MAGIC_tied) && ckWARN_d(WARN_DEBUGGING))
9014280d 35 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), "av_reify called on tied array");
93965878 36#endif
a0d0e21e 37 key = AvMAX(av) + 1;
93965878 38 while (key > AvFILLp(av) + 1)
3280af22 39 AvARRAY(av)[--key] = &PL_sv_undef;
a0d0e21e 40 while (key) {
4373e329 41 SV * const sv = AvARRAY(av)[--key];
a0d0e21e 42 assert(sv);
411caa50 43 if (sv != &PL_sv_undef)
a0d0e21e 44 (void)SvREFCNT_inc(sv);
45 }
29de640a 46 key = AvARRAY(av) - AvALLOC(av);
47 while (key)
3280af22 48 AvALLOC(av)[--key] = &PL_sv_undef;
62b1ebc2 49 AvREIFY_off(av);
a0d0e21e 50 AvREAL_on(av);
51}
52
cb50131a 53/*
54=for apidoc av_extend
55
56Pre-extend an array. The C<key> is the index to which the array should be
57extended.
58
59=cut
60*/
61
a0d0e21e 62void
864dbfa3 63Perl_av_extend(pTHX_ AV *av, I32 key)
a0d0e21e 64{
ba5d1d60 65 assert(av);
66
823a54a3 67 MAGIC * const mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied);
68 if (mg) {
93965878 69 dSP;
70 ENTER;
71 SAVETMPS;
e788e7d3 72 PUSHSTACKi(PERLSI_MAGIC);
924508f0 73 PUSHMARK(SP);
74 EXTEND(SP,2);
33c27489 75 PUSHs(SvTIED_obj((SV*)av, mg));
a60c0954 76 PUSHs(sv_2mortal(newSViv(key+1)));
93965878 77 PUTBACK;
864dbfa3 78 call_method("EXTEND", G_SCALAR|G_DISCARD);
d3acc0f7 79 POPSTACK;
93965878 80 FREETMPS;
81 LEAVE;
82 return;
83 }
a0d0e21e 84 if (key > AvMAX(av)) {
85 SV** ary;
86 I32 tmp;
87 I32 newmax;
88
89 if (AvALLOC(av) != AvARRAY(av)) {
93965878 90 ary = AvALLOC(av) + AvFILLp(av) + 1;
a0d0e21e 91 tmp = AvARRAY(av) - AvALLOC(av);
93965878 92 Move(AvARRAY(av), AvALLOC(av), AvFILLp(av)+1, SV*);
a0d0e21e 93 AvMAX(av) += tmp;
f880fe2f 94 SvPV_set(av, (char*)AvALLOC(av));
a0d0e21e 95 if (AvREAL(av)) {
96 while (tmp)
3280af22 97 ary[--tmp] = &PL_sv_undef;
a0d0e21e 98 }
a0d0e21e 99 if (key > AvMAX(av) - 10) {
100 newmax = key + AvMAX(av);
101 goto resize;
102 }
103 }
104 else {
2b573ace 105#ifdef PERL_MALLOC_WRAP
106 static const char oom_array_extend[] =
107 "Out of memory during array extend"; /* Duplicated in pp_hot.c */
108#endif
109
a0d0e21e 110 if (AvALLOC(av)) {
516a5887 111#if !defined(STRANGE_MALLOC) && !defined(MYMALLOC)
c1f7b11a 112 MEM_SIZE bytes;
113 IV itmp;
c07a80fd 114#endif
4633a7c4 115
7bab3ede 116#ifdef MYMALLOC
8d6dde3e 117 newmax = malloced_size((void*)AvALLOC(av))/sizeof(SV*) - 1;
118
119 if (key <= newmax)
120 goto resized;
121#endif
a0d0e21e 122 newmax = key + AvMAX(av) / 5;
123 resize:
2b573ace 124 MEM_WRAP_CHECK_1(newmax+1, SV*, oom_array_extend);
8d6dde3e 125#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
a0d0e21e 126 Renew(AvALLOC(av),newmax+1, SV*);
4633a7c4 127#else
128 bytes = (newmax + 1) * sizeof(SV*);
129#define MALLOC_OVERHEAD 16
c1f7b11a 130 itmp = MALLOC_OVERHEAD;
eb160463 131 while ((MEM_SIZE)(itmp - MALLOC_OVERHEAD) < bytes)
c1f7b11a 132 itmp += itmp;
133 itmp -= MALLOC_OVERHEAD;
134 itmp /= sizeof(SV*);
135 assert(itmp > newmax);
136 newmax = itmp - 1;
137 assert(newmax >= AvMAX(av));
a02a5408 138 Newx(ary, newmax+1, SV*);
4633a7c4 139 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
fba3b22e 140 if (AvMAX(av) > 64)
141 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
4633a7c4 142 else
143 Safefree(AvALLOC(av));
144 AvALLOC(av) = ary;
145#endif
7bab3ede 146#ifdef MYMALLOC
8d6dde3e 147 resized:
9c5ffd7c 148#endif
a0d0e21e 149 ary = AvALLOC(av) + AvMAX(av) + 1;
150 tmp = newmax - AvMAX(av);
3280af22 151 if (av == PL_curstack) { /* Oops, grew stack (via av_store()?) */
152 PL_stack_sp = AvALLOC(av) + (PL_stack_sp - PL_stack_base);
153 PL_stack_base = AvALLOC(av);
154 PL_stack_max = PL_stack_base + newmax;
a0d0e21e 155 }
156 }
157 else {
8d6dde3e 158 newmax = key < 3 ? 3 : key;
2b573ace 159 MEM_WRAP_CHECK_1(newmax+1, SV*, oom_array_extend);
a02a5408 160 Newx(AvALLOC(av), newmax+1, SV*);
a0d0e21e 161 ary = AvALLOC(av) + 1;
162 tmp = newmax;
3280af22 163 AvALLOC(av)[0] = &PL_sv_undef; /* For the stacks */
a0d0e21e 164 }
165 if (AvREAL(av)) {
166 while (tmp)
3280af22 167 ary[--tmp] = &PL_sv_undef;
a0d0e21e 168 }
169
f880fe2f 170 SvPV_set(av, (char*)AvALLOC(av));
a0d0e21e 171 AvMAX(av) = newmax;
172 }
173 }
174}
175
cb50131a 176/*
177=for apidoc av_fetch
178
179Returns the SV at the specified index in the array. The C<key> is the
180index. If C<lval> is set then the fetch will be part of a store. Check
181that the return value is non-null before dereferencing it to a C<SV*>.
182
183See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
184more information on how to use this function on tied arrays.
185
186=cut
187*/
188
79072805 189SV**
864dbfa3 190Perl_av_fetch(pTHX_ register AV *av, I32 key, I32 lval)
79072805 191{
192 SV *sv;
193
ba5d1d60 194 assert(av);
a0d0e21e 195
6f12eb6d 196 if (SvRMAGICAL(av)) {
35a4481c 197 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
6f12eb6d 198 if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) {
199 U32 adjust_index = 1;
200
201 if (tied_magic && key < 0) {
202 /* Handle negative array indices 20020222 MJD */
823a54a3 203 SV * const * const negative_indices_glob =
6f12eb6d 204 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
205 tied_magic))),
206 NEGATIVE_INDICES_VAR, 16, 0);
207
208 if (negative_indices_glob
209 && SvTRUE(GvSV(*negative_indices_glob)))
210 adjust_index = 0;
211 }
212
213 if (key < 0 && adjust_index) {
214 key += AvFILL(av) + 1;
215 if (key < 0)
216 return 0;
217 }
218
219 sv = sv_newmortal();
dd28f7bb 220 sv_upgrade(sv, SVt_PVLV);
221 mg_copy((SV*)av, sv, 0, key);
222 LvTYPE(sv) = 't';
223 LvTARG(sv) = sv; /* fake (SV**) */
224 return &(LvTARG(sv));
6f12eb6d 225 }
226 }
227
93965878 228 if (key < 0) {
229 key += AvFILL(av) + 1;
230 if (key < 0)
231 return 0;
232 }
233
93965878 234 if (key > AvFILLp(av)) {
a0d0e21e 235 if (!lval)
236 return 0;
352edd90 237 sv = NEWSV(5,0);
a0d0e21e 238 return av_store(av,key,sv);
79072805 239 }
3280af22 240 if (AvARRAY(av)[key] == &PL_sv_undef) {
4dbf4341 241 emptyness:
79072805 242 if (lval) {
243 sv = NEWSV(6,0);
463ee0b2 244 return av_store(av,key,sv);
79072805 245 }
246 return 0;
247 }
4dbf4341 248 else if (AvREIFY(av)
249 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
0565a181 250 || SvIS_FREED(AvARRAY(av)[key]))) {
3280af22 251 AvARRAY(av)[key] = &PL_sv_undef; /* 1/2 reify */
4dbf4341 252 goto emptyness;
253 }
463ee0b2 254 return &AvARRAY(av)[key];
79072805 255}
256
cb50131a 257/*
258=for apidoc av_store
259
260Stores an SV in an array. The array index is specified as C<key>. The
261return value will be NULL if the operation failed or if the value did not
262need to be actually stored within the array (as in the case of tied
263arrays). Otherwise it can be dereferenced to get the original C<SV*>. Note
264that the caller is responsible for suitably incrementing the reference
265count of C<val> before the call, and decrementing it if the function
266returned NULL.
267
268See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
269more information on how to use this function on tied arrays.
270
271=cut
272*/
273
79072805 274SV**
864dbfa3 275Perl_av_store(pTHX_ register AV *av, I32 key, SV *val)
79072805 276{
79072805 277 SV** ary;
278
ba5d1d60 279 assert(av);
280
43fcc5d2 281 if (!val)
3280af22 282 val = &PL_sv_undef;
463ee0b2 283
6f12eb6d 284 if (SvRMAGICAL(av)) {
35a4481c 285 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
6f12eb6d 286 if (tied_magic) {
287 /* Handle negative array indices 20020222 MJD */
288 if (key < 0) {
289 unsigned adjust_index = 1;
823a54a3 290 SV * const * const negative_indices_glob =
6f12eb6d 291 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
292 tied_magic))),
293 NEGATIVE_INDICES_VAR, 16, 0);
294 if (negative_indices_glob
295 && SvTRUE(GvSV(*negative_indices_glob)))
296 adjust_index = 0;
297 if (adjust_index) {
298 key += AvFILL(av) + 1;
299 if (key < 0)
300 return 0;
301 }
302 }
303 if (val != &PL_sv_undef) {
304 mg_copy((SV*)av, val, 0, key);
305 }
306 return 0;
307 }
308 }
309
310
a0d0e21e 311 if (key < 0) {
312 key += AvFILL(av) + 1;
313 if (key < 0)
314 return 0;
79072805 315 }
93965878 316
43fcc5d2 317 if (SvREADONLY(av) && key >= AvFILL(av))
cea2e8a9 318 Perl_croak(aTHX_ PL_no_modify);
93965878 319
49beac48 320 if (!AvREAL(av) && AvREIFY(av))
a0d0e21e 321 av_reify(av);
a0d0e21e 322 if (key > AvMAX(av))
323 av_extend(av,key);
463ee0b2 324 ary = AvARRAY(av);
93965878 325 if (AvFILLp(av) < key) {
a0d0e21e 326 if (!AvREAL(av)) {
3280af22 327 if (av == PL_curstack && key > PL_stack_sp - PL_stack_base)
328 PL_stack_sp = PL_stack_base + key; /* XPUSH in disguise */
a0d0e21e 329 do
3280af22 330 ary[++AvFILLp(av)] = &PL_sv_undef;
93965878 331 while (AvFILLp(av) < key);
79072805 332 }
93965878 333 AvFILLp(av) = key;
79072805 334 }
a0d0e21e 335 else if (AvREAL(av))
336 SvREFCNT_dec(ary[key]);
79072805 337 ary[key] = val;
8990e307 338 if (SvSMAGICAL(av)) {
3280af22 339 if (val != &PL_sv_undef) {
fabdb6c0 340 const MAGIC* const mg = SvMAGIC(av);
a0d0e21e 341 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
342 }
463ee0b2 343 mg_set((SV*)av);
344 }
79072805 345 return &ary[key];
346}
347
cb50131a 348/*
349=for apidoc newAV
350
351Creates a new AV. The reference count is set to 1.
352
353=cut
354*/
355
79072805 356AV *
864dbfa3 357Perl_newAV(pTHX)
79072805 358{
823a54a3 359 register AV * const av = (AV*)NEWSV(3,0);
79072805 360
a0d0e21e 361 sv_upgrade((SV *)av, SVt_PVAV);
a7f5e44d 362 /* sv_upgrade does AvREAL_only() */
463ee0b2 363 AvALLOC(av) = 0;
f880fe2f 364 SvPV_set(av, (char*)0);
93965878 365 AvMAX(av) = AvFILLp(av) = -1;
463ee0b2 366 return av;
79072805 367}
368
cb50131a 369/*
370=for apidoc av_make
371
372Creates a new AV and populates it with a list of SVs. The SVs are copied
373into the array, so they may be freed after the call to av_make. The new AV
374will have a reference count of 1.
375
376=cut
377*/
378
79072805 379AV *
864dbfa3 380Perl_av_make(pTHX_ register I32 size, register SV **strp)
79072805 381{
823a54a3 382 register AV * const av = (AV*)NEWSV(8,0);
79072805 383
a0d0e21e 384 sv_upgrade((SV *) av,SVt_PVAV);
a7f5e44d 385 /* sv_upgrade does AvREAL_only() */
a0288114 386 if (size) { /* "defined" was returning undef for size==0 anyway. */
dd374669 387 register SV** ary;
388 register I32 i;
a02a5408 389 Newx(ary,size,SV*);
573fa4ea 390 AvALLOC(av) = ary;
f880fe2f 391 SvPV_set(av, (char*)ary);
93965878 392 AvFILLp(av) = size - 1;
573fa4ea 393 AvMAX(av) = size - 1;
394 for (i = 0; i < size; i++) {
395 assert (*strp);
396 ary[i] = NEWSV(7,0);
397 sv_setsv(ary[i], *strp);
398 strp++;
399 }
79072805 400 }
463ee0b2 401 return av;
79072805 402}
403
cb50131a 404/*
405=for apidoc av_clear
406
407Clears an array, making it empty. Does not free the memory used by the
408array itself.
409
410=cut
411*/
412
79072805 413void
864dbfa3 414Perl_av_clear(pTHX_ register AV *av)
79072805 415{
416 register I32 key;
417
ba5d1d60 418 assert(av);
419
fabdb6c0 420/* XXX Should av_clear really be NN? */
7d55f622 421#ifdef DEBUGGING
32da55ab 422 if (SvREFCNT(av) == 0 && ckWARN_d(WARN_DEBUGGING)) {
9014280d 423 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), "Attempt to clear deleted array");
7d55f622 424 }
425#endif
a0d0e21e 426
39caa665 427 if (SvREADONLY(av))
cea2e8a9 428 Perl_croak(aTHX_ PL_no_modify);
39caa665 429
93965878 430 /* Give any tie a chance to cleanup first */
431 if (SvRMAGICAL(av))
432 mg_clear((SV*)av);
433
a60c0954 434 if (AvMAX(av) < 0)
435 return;
436
a0d0e21e 437 if (AvREAL(av)) {
823a54a3 438 SV** const ary = AvARRAY(av);
93965878 439 key = AvFILLp(av) + 1;
a0d0e21e 440 while (key) {
823a54a3 441 SV * const sv = ary[--key];
6b42d12b 442 /* undef the slot before freeing the value, because a
443 * destructor might try to modify this arrray */
3280af22 444 ary[key] = &PL_sv_undef;
6b42d12b 445 SvREFCNT_dec(sv);
a0d0e21e 446 }
447 }
155aba94 448 if ((key = AvARRAY(av) - AvALLOC(av))) {
463ee0b2 449 AvMAX(av) += key;
f880fe2f 450 SvPV_set(av, (char*)AvALLOC(av));
79072805 451 }
93965878 452 AvFILLp(av) = -1;
fb73857a 453
79072805 454}
455
cb50131a 456/*
457=for apidoc av_undef
458
459Undefines the array. Frees the memory used by the array itself.
460
461=cut
462*/
463
79072805 464void
864dbfa3 465Perl_av_undef(pTHX_ register AV *av)
79072805 466{
ba5d1d60 467 assert(av);
93965878 468
469 /* Give any tie a chance to cleanup first */
14befaf4 470 if (SvTIED_mg((SV*)av, PERL_MAGIC_tied))
93965878 471 av_fill(av, -1); /* mg_clear() ? */
472
a0d0e21e 473 if (AvREAL(av)) {
a3b680e6 474 register I32 key = AvFILLp(av) + 1;
a0d0e21e 475 while (key)
476 SvREFCNT_dec(AvARRAY(av)[--key]);
477 }
463ee0b2 478 Safefree(AvALLOC(av));
479 AvALLOC(av) = 0;
f880fe2f 480 SvPV_set(av, (char*)0);
93965878 481 AvMAX(av) = AvFILLp(av) = -1;
79072805 482}
483
cb50131a 484/*
485=for apidoc av_push
486
487Pushes an SV onto the end of the array. The array will grow automatically
488to accommodate the addition.
489
490=cut
491*/
492
a0d0e21e 493void
864dbfa3 494Perl_av_push(pTHX_ register AV *av, SV *val)
93965878 495{
27da23d5 496 dVAR;
93965878 497 MAGIC *mg;
ba5d1d60 498 assert(av);
499
93965878 500 if (SvREADONLY(av))
cea2e8a9 501 Perl_croak(aTHX_ PL_no_modify);
93965878 502
14befaf4 503 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
93965878 504 dSP;
e788e7d3 505 PUSHSTACKi(PERLSI_MAGIC);
924508f0 506 PUSHMARK(SP);
507 EXTEND(SP,2);
33c27489 508 PUSHs(SvTIED_obj((SV*)av, mg));
93965878 509 PUSHs(val);
a60c0954 510 PUTBACK;
511 ENTER;
864dbfa3 512 call_method("PUSH", G_SCALAR|G_DISCARD);
a60c0954 513 LEAVE;
d3acc0f7 514 POPSTACK;
93965878 515 return;
516 }
517 av_store(av,AvFILLp(av)+1,val);
79072805 518}
519
cb50131a 520/*
521=for apidoc av_pop
522
523Pops an SV off the end of the array. Returns C<&PL_sv_undef> if the array
524is empty.
525
526=cut
527*/
528
79072805 529SV *
864dbfa3 530Perl_av_pop(pTHX_ register AV *av)
79072805 531{
27da23d5 532 dVAR;
79072805 533 SV *retval;
93965878 534 MAGIC* mg;
79072805 535
ba5d1d60 536 assert(av);
537
43fcc5d2 538 if (SvREADONLY(av))
cea2e8a9 539 Perl_croak(aTHX_ PL_no_modify);
14befaf4 540 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
93965878 541 dSP;
e788e7d3 542 PUSHSTACKi(PERLSI_MAGIC);
924508f0 543 PUSHMARK(SP);
33c27489 544 XPUSHs(SvTIED_obj((SV*)av, mg));
a60c0954 545 PUTBACK;
546 ENTER;
864dbfa3 547 if (call_method("POP", G_SCALAR)) {
3280af22 548 retval = newSVsv(*PL_stack_sp--);
93965878 549 } else {
3280af22 550 retval = &PL_sv_undef;
93965878 551 }
a60c0954 552 LEAVE;
d3acc0f7 553 POPSTACK;
93965878 554 return retval;
555 }
d19c0e07 556 if (AvFILL(av) < 0)
557 return &PL_sv_undef;
93965878 558 retval = AvARRAY(av)[AvFILLp(av)];
3280af22 559 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
8990e307 560 if (SvSMAGICAL(av))
463ee0b2 561 mg_set((SV*)av);
79072805 562 return retval;
563}
564
cb50131a 565/*
566=for apidoc av_unshift
567
568Unshift the given number of C<undef> values onto the beginning of the
569array. The array will grow automatically to accommodate the addition. You
570must then use C<av_store> to assign values to these new elements.
571
572=cut
573*/
574
79072805 575void
864dbfa3 576Perl_av_unshift(pTHX_ register AV *av, register I32 num)
79072805 577{
27da23d5 578 dVAR;
79072805 579 register I32 i;
93965878 580 MAGIC* mg;
79072805 581
ba5d1d60 582 assert(av);
583
43fcc5d2 584 if (SvREADONLY(av))
cea2e8a9 585 Perl_croak(aTHX_ PL_no_modify);
93965878 586
14befaf4 587 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
93965878 588 dSP;
e788e7d3 589 PUSHSTACKi(PERLSI_MAGIC);
924508f0 590 PUSHMARK(SP);
591 EXTEND(SP,1+num);
33c27489 592 PUSHs(SvTIED_obj((SV*)av, mg));
93965878 593 while (num-- > 0) {
3280af22 594 PUSHs(&PL_sv_undef);
93965878 595 }
596 PUTBACK;
a60c0954 597 ENTER;
864dbfa3 598 call_method("UNSHIFT", G_SCALAR|G_DISCARD);
a60c0954 599 LEAVE;
d3acc0f7 600 POPSTACK;
93965878 601 return;
602 }
603
d19c0e07 604 if (num <= 0)
605 return;
49beac48 606 if (!AvREAL(av) && AvREIFY(av))
607 av_reify(av);
a0d0e21e 608 i = AvARRAY(av) - AvALLOC(av);
609 if (i) {
610 if (i > num)
611 i = num;
612 num -= i;
613
614 AvMAX(av) += i;
93965878 615 AvFILLp(av) += i;
f880fe2f 616 SvPV_set(av, (char*)(AvARRAY(av) - i));
a0d0e21e 617 }
d2719217 618 if (num) {
a3b680e6 619 register SV **ary;
620 I32 slide;
67a38de0 621 i = AvFILLp(av);
e2b534e7 622 /* Create extra elements */
623 slide = i > 0 ? i : 0;
624 num += slide;
67a38de0 625 av_extend(av, i + num);
93965878 626 AvFILLp(av) += num;
67a38de0 627 ary = AvARRAY(av);
628 Move(ary, ary + num, i + 1, SV*);
629 do {
3280af22 630 ary[--num] = &PL_sv_undef;
67a38de0 631 } while (num);
e2b534e7 632 /* Make extra elements into a buffer */
633 AvMAX(av) -= slide;
634 AvFILLp(av) -= slide;
f880fe2f 635 SvPV_set(av, (char*)(AvARRAY(av) + slide));
79072805 636 }
637}
638
cb50131a 639/*
640=for apidoc av_shift
641
642Shifts an SV off the beginning of the array.
643
644=cut
645*/
646
79072805 647SV *
864dbfa3 648Perl_av_shift(pTHX_ register AV *av)
79072805 649{
27da23d5 650 dVAR;
79072805 651 SV *retval;
93965878 652 MAGIC* mg;
79072805 653
ba5d1d60 654 assert(av);
655
43fcc5d2 656 if (SvREADONLY(av))
cea2e8a9 657 Perl_croak(aTHX_ PL_no_modify);
14befaf4 658 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
93965878 659 dSP;
e788e7d3 660 PUSHSTACKi(PERLSI_MAGIC);
924508f0 661 PUSHMARK(SP);
33c27489 662 XPUSHs(SvTIED_obj((SV*)av, mg));
a60c0954 663 PUTBACK;
664 ENTER;
864dbfa3 665 if (call_method("SHIFT", G_SCALAR)) {
3280af22 666 retval = newSVsv(*PL_stack_sp--);
93965878 667 } else {
3280af22 668 retval = &PL_sv_undef;
a60c0954 669 }
670 LEAVE;
d3acc0f7 671 POPSTACK;
93965878 672 return retval;
673 }
d19c0e07 674 if (AvFILL(av) < 0)
675 return &PL_sv_undef;
463ee0b2 676 retval = *AvARRAY(av);
a0d0e21e 677 if (AvREAL(av))
3280af22 678 *AvARRAY(av) = &PL_sv_undef;
f880fe2f 679 SvPV_set(av, (char*)(AvARRAY(av) + 1));
463ee0b2 680 AvMAX(av)--;
93965878 681 AvFILLp(av)--;
8990e307 682 if (SvSMAGICAL(av))
463ee0b2 683 mg_set((SV*)av);
79072805 684 return retval;
685}
686
cb50131a 687/*
688=for apidoc av_len
689
690Returns the highest index in the array. Returns -1 if the array is
691empty.
692
693=cut
694*/
695
79072805 696I32
0d46e09a 697Perl_av_len(pTHX_ register const AV *av)
79072805 698{
ba5d1d60 699 assert(av);
463ee0b2 700 return AvFILL(av);
79072805 701}
702
f3b76584 703/*
704=for apidoc av_fill
705
706Ensure than an array has a given number of elements, equivalent to
707Perl's C<$#array = $fill;>.
708
709=cut
710*/
79072805 711void
864dbfa3 712Perl_av_fill(pTHX_ register AV *av, I32 fill)
79072805 713{
27da23d5 714 dVAR;
93965878 715 MAGIC *mg;
ba5d1d60 716
717 assert(av);
718
79072805 719 if (fill < 0)
720 fill = -1;
14befaf4 721 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
93965878 722 dSP;
723 ENTER;
724 SAVETMPS;
e788e7d3 725 PUSHSTACKi(PERLSI_MAGIC);
924508f0 726 PUSHMARK(SP);
727 EXTEND(SP,2);
33c27489 728 PUSHs(SvTIED_obj((SV*)av, mg));
a60c0954 729 PUSHs(sv_2mortal(newSViv(fill+1)));
93965878 730 PUTBACK;
864dbfa3 731 call_method("STORESIZE", G_SCALAR|G_DISCARD);
d3acc0f7 732 POPSTACK;
93965878 733 FREETMPS;
734 LEAVE;
735 return;
736 }
463ee0b2 737 if (fill <= AvMAX(av)) {
93965878 738 I32 key = AvFILLp(av);
fabdb6c0 739 SV** const ary = AvARRAY(av);
a0d0e21e 740
741 if (AvREAL(av)) {
742 while (key > fill) {
743 SvREFCNT_dec(ary[key]);
3280af22 744 ary[key--] = &PL_sv_undef;
a0d0e21e 745 }
746 }
747 else {
748 while (key < fill)
3280af22 749 ary[++key] = &PL_sv_undef;
a0d0e21e 750 }
751
93965878 752 AvFILLp(av) = fill;
8990e307 753 if (SvSMAGICAL(av))
463ee0b2 754 mg_set((SV*)av);
755 }
a0d0e21e 756 else
3280af22 757 (void)av_store(av,fill,&PL_sv_undef);
79072805 758}
c750a3ec 759
f3b76584 760/*
761=for apidoc av_delete
762
763Deletes the element indexed by C<key> from the array. Returns the
a6214072 764deleted element. If C<flags> equals C<G_DISCARD>, the element is freed
765and null is returned.
f3b76584 766
767=cut
768*/
146174a9 769SV *
770Perl_av_delete(pTHX_ AV *av, I32 key, I32 flags)
771{
772 SV *sv;
773
ba5d1d60 774 assert(av);
775
146174a9 776 if (SvREADONLY(av))
777 Perl_croak(aTHX_ PL_no_modify);
6f12eb6d 778
779 if (SvRMAGICAL(av)) {
35a4481c 780 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
6f12eb6d 781 if ((tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata))) {
782 /* Handle negative array indices 20020222 MJD */
35a4481c 783 SV **svp;
6f12eb6d 784 if (key < 0) {
785 unsigned adjust_index = 1;
786 if (tied_magic) {
823a54a3 787 SV * const * const negative_indices_glob =
6f12eb6d 788 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
789 tied_magic))),
790 NEGATIVE_INDICES_VAR, 16, 0);
791 if (negative_indices_glob
792 && SvTRUE(GvSV(*negative_indices_glob)))
793 adjust_index = 0;
794 }
795 if (adjust_index) {
796 key += AvFILL(av) + 1;
797 if (key < 0)
fabdb6c0 798 return NULL;
6f12eb6d 799 }
800 }
801 svp = av_fetch(av, key, TRUE);
802 if (svp) {
803 sv = *svp;
804 mg_clear(sv);
805 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
806 sv_unmagic(sv, PERL_MAGIC_tiedelem); /* No longer an element */
807 return sv;
808 }
fabdb6c0 809 return NULL;
6f12eb6d 810 }
811 }
812 }
813
146174a9 814 if (key < 0) {
815 key += AvFILL(av) + 1;
816 if (key < 0)
fabdb6c0 817 return NULL;
146174a9 818 }
6f12eb6d 819
146174a9 820 if (key > AvFILLp(av))
fabdb6c0 821 return NULL;
146174a9 822 else {
a6214072 823 if (!AvREAL(av) && AvREIFY(av))
824 av_reify(av);
146174a9 825 sv = AvARRAY(av)[key];
826 if (key == AvFILLp(av)) {
d9c63288 827 AvARRAY(av)[key] = &PL_sv_undef;
146174a9 828 do {
829 AvFILLp(av)--;
830 } while (--key >= 0 && AvARRAY(av)[key] == &PL_sv_undef);
831 }
832 else
833 AvARRAY(av)[key] = &PL_sv_undef;
834 if (SvSMAGICAL(av))
835 mg_set((SV*)av);
836 }
837 if (flags & G_DISCARD) {
838 SvREFCNT_dec(sv);
fabdb6c0 839 sv = NULL;
146174a9 840 }
fdb3bdd0 841 else if (AvREAL(av))
2c8ddff3 842 sv = sv_2mortal(sv);
146174a9 843 return sv;
844}
845
846/*
f3b76584 847=for apidoc av_exists
848
849Returns true if the element indexed by C<key> has been initialized.
146174a9 850
f3b76584 851This relies on the fact that uninitialized array elements are set to
852C<&PL_sv_undef>.
853
854=cut
855*/
146174a9 856bool
857Perl_av_exists(pTHX_ AV *av, I32 key)
858{
ba5d1d60 859 assert(av);
6f12eb6d 860
861 if (SvRMAGICAL(av)) {
35a4481c 862 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
6f12eb6d 863 if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) {
fabdb6c0 864 SV * const sv = sv_newmortal();
6f12eb6d 865 MAGIC *mg;
866 /* Handle negative array indices 20020222 MJD */
867 if (key < 0) {
868 unsigned adjust_index = 1;
869 if (tied_magic) {
823a54a3 870 SV * const * const negative_indices_glob =
6f12eb6d 871 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
872 tied_magic))),
873 NEGATIVE_INDICES_VAR, 16, 0);
874 if (negative_indices_glob
875 && SvTRUE(GvSV(*negative_indices_glob)))
876 adjust_index = 0;
877 }
878 if (adjust_index) {
879 key += AvFILL(av) + 1;
880 if (key < 0)
881 return FALSE;
882 }
883 }
884
885 mg_copy((SV*)av, sv, 0, key);
886 mg = mg_find(sv, PERL_MAGIC_tiedelem);
887 if (mg) {
888 magic_existspack(sv, mg);
889 return (bool)SvTRUE(sv);
890 }
891
892 }
893 }
894
146174a9 895 if (key < 0) {
896 key += AvFILL(av) + 1;
897 if (key < 0)
898 return FALSE;
899 }
6f12eb6d 900
146174a9 901 if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef
902 && AvARRAY(av)[key])
903 {
904 return TRUE;
905 }
906 else
907 return FALSE;
908}
66610fdd 909
a3874608 910SV **
911Perl_av_arylen_p(pTHX_ AV *av) {
912 dVAR;
ba5d1d60 913 MAGIC *mg;
914
915 assert(av);
916
917 mg = mg_find((SV*)av, PERL_MAGIC_arylen_p);
a3874608 918
919 if (!mg) {
1b20cd17 920 mg = sv_magicext((SV*)av, 0, PERL_MAGIC_arylen_p, &PL_vtbl_arylen_p,
921 0, 0);
c82c7adc 922 assert(mg);
a3874608 923 /* sv_magicext won't set this for us because we pass in a NULL obj */
924 mg->mg_flags |= MGf_REFCOUNTED;
925 }
926 return &(mg->mg_obj);
927}
928
66610fdd 929/*
930 * Local variables:
931 * c-indentation-style: bsd
932 * c-basic-offset: 4
933 * indent-tabs-mode: t
934 * End:
935 *
37442d52 936 * ex: set ts=8 sts=4 sw=4 noet:
937 */