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