This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Getopt::Long 2.34_04
[perl5.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
LW
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
LW
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
LW
14 */
15
ccfc67b7
JH
16/*
17=head1 Array Manipulation Functions
18*/
19
79072805 20#include "EXTERN.h"
864dbfa3 21#define PERL_IN_AV_C
79072805
LW
22#include "perl.h"
23
fb73857a 24void
864dbfa3 25Perl_av_reify(pTHX_ AV *av)
a0d0e21e
LW
26{
27 I32 key;
fb73857a 28
3c78fafa
GS
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
LW
42 (void)SvREFCNT_inc(sv);
43 }
29de640a
CS
44 key = AvARRAY(av) - AvALLOC(av);
45 while (key)
3280af22 46 AvALLOC(av)[--key] = &PL_sv_undef;
62b1ebc2 47 AvREIFY_off(av);
a0d0e21e
LW
48 AvREAL_on(av);
49}
50
cb50131a
CB
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
AL
63 MAGIC * const mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied);
64 if (mg) {
93965878
NIS
65 dSP;
66 ENTER;
67 SAVETMPS;
e788e7d3 68 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
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
NIS
76 FREETMPS;
77 LEAVE;
78 return;
79 }
a0d0e21e
LW
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
LW
91 if (AvREAL(av)) {
92 while (tmp)
3280af22 93 ary[--tmp] = &PL_sv_undef;
a0d0e21e 94 }
a0d0e21e
LW
95 if (key > AvMAX(av) - 10) {
96 newmax = key + AvMAX(av);
97 goto resize;
98 }
99 }
100 else {
2b573ace
JH
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
SB
108 MEM_SIZE bytes;
109 IV itmp;
c07a80fd 110#endif
4633a7c4 111
7bab3ede 112#ifdef MYMALLOC
8d6dde3e
IZ
113 newmax = malloced_size((void*)AvALLOC(av))/sizeof(SV*) - 1;
114
115 if (key <= newmax)
116 goto resized;
117#endif
a0d0e21e
LW
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
LW
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
SB
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
MB
136 if (AvMAX(av) > 64)
137 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
4633a7c4
LW
138 else
139 Safefree(AvALLOC(av));
140 AvALLOC(av) = ary;
141#endif
7bab3ede 142#ifdef MYMALLOC
8d6dde3e 143 resized:
9c5ffd7c 144#endif
a0d0e21e
LW
145 ary = AvALLOC(av) + AvMAX(av) + 1;
146 tmp = newmax - AvMAX(av);
3280af22
NIS
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
LW
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
LW
157 ary = AvALLOC(av) + 1;
158 tmp = newmax;
3280af22 159 AvALLOC(av)[0] = &PL_sv_undef; /* For the stacks */
a0d0e21e
LW
160 }
161 if (AvREAL(av)) {
162 while (tmp)
3280af22 163 ary[--tmp] = &PL_sv_undef;
a0d0e21e
LW
164 }
165
f880fe2f 166 SvPV_set(av, (char*)AvALLOC(av));
a0d0e21e
LW
167 AvMAX(av) = newmax;
168 }
169 }
170}
171
cb50131a
CB
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
LW
187{
188 SV *sv;
189
a0d0e21e
LW
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
MJD
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
MJD
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
DM
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
MJD
222 }
223 }
224
93965878
NIS
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
LW
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
LW
239 if (lval) {
240 sv = NEWSV(6,0);
463ee0b2 241 return av_store(av,key,sv);
79072805
LW
242 }
243 return 0;
244 }
4dbf4341 245 else if (AvREIFY(av)
246 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
0565a181 247 || SvIS_FREED(AvARRAY(av)[key]))) {
3280af22 248 AvARRAY(av)[key] = &PL_sv_undef; /* 1/2 reify */
4dbf4341 249 goto emptyness;
250 }
463ee0b2 251 return &AvARRAY(av)[key];
79072805
LW
252}
253
cb50131a
CB
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
LW
274 SV** ary;
275
a0d0e21e
LW
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
MJD
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
MJD
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
LW
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
LW
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
NIS
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
LW
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
LW
337 MAGIC* mg = SvMAGIC(av);
338 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
339 }
463ee0b2
LW
340 mg_set((SV*)av);
341 }
79072805
LW
342 return &ary[key];
343}
344
cb50131a
CB
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
LW
364}
365
cb50131a
CB
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
AL
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
TB
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
LW
399}
400
cb50131a
CB
401/*
402=for apidoc av_clear
403
404Clears an array, making it empty. Does not free the memory used by the
405array itself.
406
407=cut
408*/
409
79072805 410void
864dbfa3 411Perl_av_clear(pTHX_ register AV *av)
79072805
LW
412{
413 register I32 key;
414
7d55f622 415#ifdef DEBUGGING
32da55ab 416 if (SvREFCNT(av) == 0 && ckWARN_d(WARN_DEBUGGING)) {
9014280d 417 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), "Attempt to clear deleted array");
7d55f622 418 }
419#endif
a60c0954 420 if (!av)
79072805 421 return;
a0d0e21e 422
39caa665 423 if (SvREADONLY(av))
cea2e8a9 424 Perl_croak(aTHX_ PL_no_modify);
39caa665 425
93965878
NIS
426 /* Give any tie a chance to cleanup first */
427 if (SvRMAGICAL(av))
428 mg_clear((SV*)av);
429
a60c0954
NIS
430 if (AvMAX(av) < 0)
431 return;
432
a0d0e21e 433 if (AvREAL(av)) {
823a54a3 434 SV** const ary = AvARRAY(av);
93965878 435 key = AvFILLp(av) + 1;
a0d0e21e 436 while (key) {
823a54a3 437 SV * const sv = ary[--key];
6b42d12b
DM
438 /* undef the slot before freeing the value, because a
439 * destructor might try to modify this arrray */
3280af22 440 ary[key] = &PL_sv_undef;
6b42d12b 441 SvREFCNT_dec(sv);
a0d0e21e
LW
442 }
443 }
155aba94 444 if ((key = AvARRAY(av) - AvALLOC(av))) {
463ee0b2 445 AvMAX(av) += key;
f880fe2f 446 SvPV_set(av, (char*)AvALLOC(av));
79072805 447 }
93965878 448 AvFILLp(av) = -1;
fb73857a 449
79072805
LW
450}
451
cb50131a
CB
452/*
453=for apidoc av_undef
454
455Undefines the array. Frees the memory used by the array itself.
456
457=cut
458*/
459
79072805 460void
864dbfa3 461Perl_av_undef(pTHX_ register AV *av)
79072805 462{
463ee0b2 463 if (!av)
79072805 464 return;
93965878
NIS
465
466 /* Give any tie a chance to cleanup first */
14befaf4 467 if (SvTIED_mg((SV*)av, PERL_MAGIC_tied))
93965878
NIS
468 av_fill(av, -1); /* mg_clear() ? */
469
a0d0e21e 470 if (AvREAL(av)) {
a3b680e6 471 register I32 key = AvFILLp(av) + 1;
a0d0e21e
LW
472 while (key)
473 SvREFCNT_dec(AvARRAY(av)[--key]);
474 }
463ee0b2
LW
475 Safefree(AvALLOC(av));
476 AvALLOC(av) = 0;
f880fe2f 477 SvPV_set(av, (char*)0);
93965878 478 AvMAX(av) = AvFILLp(av) = -1;
79072805
LW
479}
480
cb50131a
CB
481/*
482=for apidoc av_push
483
484Pushes an SV onto the end of the array. The array will grow automatically
485to accommodate the addition.
486
487=cut
488*/
489
a0d0e21e 490void
864dbfa3 491Perl_av_push(pTHX_ register AV *av, SV *val)
93965878 492{
27da23d5 493 dVAR;
93965878 494 MAGIC *mg;
a0d0e21e
LW
495 if (!av)
496 return;
93965878 497 if (SvREADONLY(av))
cea2e8a9 498 Perl_croak(aTHX_ PL_no_modify);
93965878 499
14befaf4 500 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
93965878 501 dSP;
e788e7d3 502 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
503 PUSHMARK(SP);
504 EXTEND(SP,2);
33c27489 505 PUSHs(SvTIED_obj((SV*)av, mg));
93965878 506 PUSHs(val);
a60c0954
NIS
507 PUTBACK;
508 ENTER;
864dbfa3 509 call_method("PUSH", G_SCALAR|G_DISCARD);
a60c0954 510 LEAVE;
d3acc0f7 511 POPSTACK;
93965878
NIS
512 return;
513 }
514 av_store(av,AvFILLp(av)+1,val);
79072805
LW
515}
516
cb50131a
CB
517/*
518=for apidoc av_pop
519
520Pops an SV off the end of the array. Returns C<&PL_sv_undef> if the array
521is empty.
522
523=cut
524*/
525
79072805 526SV *
864dbfa3 527Perl_av_pop(pTHX_ register AV *av)
79072805 528{
27da23d5 529 dVAR;
79072805 530 SV *retval;
93965878 531 MAGIC* mg;
79072805 532
d19c0e07
MJD
533 if (!av)
534 return &PL_sv_undef;
43fcc5d2 535 if (SvREADONLY(av))
cea2e8a9 536 Perl_croak(aTHX_ PL_no_modify);
14befaf4 537 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
93965878 538 dSP;
e788e7d3 539 PUSHSTACKi(PERLSI_MAGIC);
924508f0 540 PUSHMARK(SP);
33c27489 541 XPUSHs(SvTIED_obj((SV*)av, mg));
a60c0954
NIS
542 PUTBACK;
543 ENTER;
864dbfa3 544 if (call_method("POP", G_SCALAR)) {
3280af22 545 retval = newSVsv(*PL_stack_sp--);
93965878 546 } else {
3280af22 547 retval = &PL_sv_undef;
93965878 548 }
a60c0954 549 LEAVE;
d3acc0f7 550 POPSTACK;
93965878
NIS
551 return retval;
552 }
d19c0e07
MJD
553 if (AvFILL(av) < 0)
554 return &PL_sv_undef;
93965878 555 retval = AvARRAY(av)[AvFILLp(av)];
3280af22 556 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
8990e307 557 if (SvSMAGICAL(av))
463ee0b2 558 mg_set((SV*)av);
79072805
LW
559 return retval;
560}
561
cb50131a
CB
562/*
563=for apidoc av_unshift
564
565Unshift the given number of C<undef> values onto the beginning of the
566array. The array will grow automatically to accommodate the addition. You
567must then use C<av_store> to assign values to these new elements.
568
569=cut
570*/
571
79072805 572void
864dbfa3 573Perl_av_unshift(pTHX_ register AV *av, register I32 num)
79072805 574{
27da23d5 575 dVAR;
79072805 576 register I32 i;
93965878 577 MAGIC* mg;
79072805 578
d19c0e07 579 if (!av)
79072805 580 return;
43fcc5d2 581 if (SvREADONLY(av))
cea2e8a9 582 Perl_croak(aTHX_ PL_no_modify);
93965878 583
14befaf4 584 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
93965878 585 dSP;
e788e7d3 586 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
587 PUSHMARK(SP);
588 EXTEND(SP,1+num);
33c27489 589 PUSHs(SvTIED_obj((SV*)av, mg));
93965878 590 while (num-- > 0) {
3280af22 591 PUSHs(&PL_sv_undef);
93965878
NIS
592 }
593 PUTBACK;
a60c0954 594 ENTER;
864dbfa3 595 call_method("UNSHIFT", G_SCALAR|G_DISCARD);
a60c0954 596 LEAVE;
d3acc0f7 597 POPSTACK;
93965878
NIS
598 return;
599 }
600
d19c0e07
MJD
601 if (num <= 0)
602 return;
49beac48
CS
603 if (!AvREAL(av) && AvREIFY(av))
604 av_reify(av);
a0d0e21e
LW
605 i = AvARRAY(av) - AvALLOC(av);
606 if (i) {
607 if (i > num)
608 i = num;
609 num -= i;
610
611 AvMAX(av) += i;
93965878 612 AvFILLp(av) += i;
f880fe2f 613 SvPV_set(av, (char*)(AvARRAY(av) - i));
a0d0e21e 614 }
d2719217 615 if (num) {
a3b680e6
AL
616 register SV **ary;
617 I32 slide;
67a38de0 618 i = AvFILLp(av);
e2b534e7
BT
619 /* Create extra elements */
620 slide = i > 0 ? i : 0;
621 num += slide;
67a38de0 622 av_extend(av, i + num);
93965878 623 AvFILLp(av) += num;
67a38de0
NIS
624 ary = AvARRAY(av);
625 Move(ary, ary + num, i + 1, SV*);
626 do {
3280af22 627 ary[--num] = &PL_sv_undef;
67a38de0 628 } while (num);
e2b534e7
BT
629 /* Make extra elements into a buffer */
630 AvMAX(av) -= slide;
631 AvFILLp(av) -= slide;
f880fe2f 632 SvPV_set(av, (char*)(AvARRAY(av) + slide));
79072805
LW
633 }
634}
635
cb50131a
CB
636/*
637=for apidoc av_shift
638
639Shifts an SV off the beginning of the array.
640
641=cut
642*/
643
79072805 644SV *
864dbfa3 645Perl_av_shift(pTHX_ register AV *av)
79072805 646{
27da23d5 647 dVAR;
79072805 648 SV *retval;
93965878 649 MAGIC* mg;
79072805 650
d19c0e07 651 if (!av)
3280af22 652 return &PL_sv_undef;
43fcc5d2 653 if (SvREADONLY(av))
cea2e8a9 654 Perl_croak(aTHX_ PL_no_modify);
14befaf4 655 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
93965878 656 dSP;
e788e7d3 657 PUSHSTACKi(PERLSI_MAGIC);
924508f0 658 PUSHMARK(SP);
33c27489 659 XPUSHs(SvTIED_obj((SV*)av, mg));
a60c0954
NIS
660 PUTBACK;
661 ENTER;
864dbfa3 662 if (call_method("SHIFT", G_SCALAR)) {
3280af22 663 retval = newSVsv(*PL_stack_sp--);
93965878 664 } else {
3280af22 665 retval = &PL_sv_undef;
a60c0954
NIS
666 }
667 LEAVE;
d3acc0f7 668 POPSTACK;
93965878
NIS
669 return retval;
670 }
d19c0e07
MJD
671 if (AvFILL(av) < 0)
672 return &PL_sv_undef;
463ee0b2 673 retval = *AvARRAY(av);
a0d0e21e 674 if (AvREAL(av))
3280af22 675 *AvARRAY(av) = &PL_sv_undef;
f880fe2f 676 SvPV_set(av, (char*)(AvARRAY(av) + 1));
463ee0b2 677 AvMAX(av)--;
93965878 678 AvFILLp(av)--;
8990e307 679 if (SvSMAGICAL(av))
463ee0b2 680 mg_set((SV*)av);
79072805
LW
681 return retval;
682}
683
cb50131a
CB
684/*
685=for apidoc av_len
686
687Returns the highest index in the array. Returns -1 if the array is
688empty.
689
690=cut
691*/
692
79072805 693I32
0d46e09a 694Perl_av_len(pTHX_ register const AV *av)
79072805 695{
463ee0b2 696 return AvFILL(av);
79072805
LW
697}
698
f3b76584
SC
699/*
700=for apidoc av_fill
701
702Ensure than an array has a given number of elements, equivalent to
703Perl's C<$#array = $fill;>.
704
705=cut
706*/
79072805 707void
864dbfa3 708Perl_av_fill(pTHX_ register AV *av, I32 fill)
79072805 709{
27da23d5 710 dVAR;
93965878 711 MAGIC *mg;
a0d0e21e 712 if (!av)
cea2e8a9 713 Perl_croak(aTHX_ "panic: null array");
79072805
LW
714 if (fill < 0)
715 fill = -1;
14befaf4 716 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
93965878
NIS
717 dSP;
718 ENTER;
719 SAVETMPS;
e788e7d3 720 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
721 PUSHMARK(SP);
722 EXTEND(SP,2);
33c27489 723 PUSHs(SvTIED_obj((SV*)av, mg));
a60c0954 724 PUSHs(sv_2mortal(newSViv(fill+1)));
93965878 725 PUTBACK;
864dbfa3 726 call_method("STORESIZE", G_SCALAR|G_DISCARD);
d3acc0f7 727 POPSTACK;
93965878
NIS
728 FREETMPS;
729 LEAVE;
730 return;
731 }
463ee0b2 732 if (fill <= AvMAX(av)) {
93965878 733 I32 key = AvFILLp(av);
a0d0e21e
LW
734 SV** ary = AvARRAY(av);
735
736 if (AvREAL(av)) {
737 while (key > fill) {
738 SvREFCNT_dec(ary[key]);
3280af22 739 ary[key--] = &PL_sv_undef;
a0d0e21e
LW
740 }
741 }
742 else {
743 while (key < fill)
3280af22 744 ary[++key] = &PL_sv_undef;
a0d0e21e
LW
745 }
746
93965878 747 AvFILLp(av) = fill;
8990e307 748 if (SvSMAGICAL(av))
463ee0b2
LW
749 mg_set((SV*)av);
750 }
a0d0e21e 751 else
3280af22 752 (void)av_store(av,fill,&PL_sv_undef);
79072805 753}
c750a3ec 754
f3b76584
SC
755/*
756=for apidoc av_delete
757
758Deletes the element indexed by C<key> from the array. Returns the
a6214072
DM
759deleted element. If C<flags> equals C<G_DISCARD>, the element is freed
760and null is returned.
f3b76584
SC
761
762=cut
763*/
146174a9
CB
764SV *
765Perl_av_delete(pTHX_ AV *av, I32 key, I32 flags)
766{
767 SV *sv;
768
769 if (!av)
770 return Nullsv;
771 if (SvREADONLY(av))
772 Perl_croak(aTHX_ PL_no_modify);
6f12eb6d
MJD
773
774 if (SvRMAGICAL(av)) {
35a4481c 775 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
6f12eb6d
MJD
776 if ((tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata))) {
777 /* Handle negative array indices 20020222 MJD */
35a4481c 778 SV **svp;
6f12eb6d
MJD
779 if (key < 0) {
780 unsigned adjust_index = 1;
781 if (tied_magic) {
823a54a3 782 SV * const * const negative_indices_glob =
6f12eb6d
MJD
783 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
784 tied_magic))),
785 NEGATIVE_INDICES_VAR, 16, 0);
786 if (negative_indices_glob
787 && SvTRUE(GvSV(*negative_indices_glob)))
788 adjust_index = 0;
789 }
790 if (adjust_index) {
791 key += AvFILL(av) + 1;
792 if (key < 0)
793 return Nullsv;
794 }
795 }
796 svp = av_fetch(av, key, TRUE);
797 if (svp) {
798 sv = *svp;
799 mg_clear(sv);
800 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
801 sv_unmagic(sv, PERL_MAGIC_tiedelem); /* No longer an element */
802 return sv;
803 }
804 return Nullsv;
805 }
806 }
807 }
808
146174a9
CB
809 if (key < 0) {
810 key += AvFILL(av) + 1;
811 if (key < 0)
812 return Nullsv;
813 }
6f12eb6d 814
146174a9
CB
815 if (key > AvFILLp(av))
816 return Nullsv;
817 else {
a6214072
DM
818 if (!AvREAL(av) && AvREIFY(av))
819 av_reify(av);
146174a9
CB
820 sv = AvARRAY(av)[key];
821 if (key == AvFILLp(av)) {
d9c63288 822 AvARRAY(av)[key] = &PL_sv_undef;
146174a9
CB
823 do {
824 AvFILLp(av)--;
825 } while (--key >= 0 && AvARRAY(av)[key] == &PL_sv_undef);
826 }
827 else
828 AvARRAY(av)[key] = &PL_sv_undef;
829 if (SvSMAGICAL(av))
830 mg_set((SV*)av);
831 }
832 if (flags & G_DISCARD) {
833 SvREFCNT_dec(sv);
834 sv = Nullsv;
835 }
fdb3bdd0 836 else if (AvREAL(av))
2c8ddff3 837 sv = sv_2mortal(sv);
146174a9
CB
838 return sv;
839}
840
841/*
f3b76584
SC
842=for apidoc av_exists
843
844Returns true if the element indexed by C<key> has been initialized.
146174a9 845
f3b76584
SC
846This relies on the fact that uninitialized array elements are set to
847C<&PL_sv_undef>.
848
849=cut
850*/
146174a9
CB
851bool
852Perl_av_exists(pTHX_ AV *av, I32 key)
853{
854 if (!av)
855 return FALSE;
6f12eb6d
MJD
856
857
858 if (SvRMAGICAL(av)) {
35a4481c 859 const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
6f12eb6d
MJD
860 if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) {
861 SV *sv = sv_newmortal();
862 MAGIC *mg;
863 /* Handle negative array indices 20020222 MJD */
864 if (key < 0) {
865 unsigned adjust_index = 1;
866 if (tied_magic) {
823a54a3 867 SV * const * const negative_indices_glob =
6f12eb6d
MJD
868 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
869 tied_magic))),
870 NEGATIVE_INDICES_VAR, 16, 0);
871 if (negative_indices_glob
872 && SvTRUE(GvSV(*negative_indices_glob)))
873 adjust_index = 0;
874 }
875 if (adjust_index) {
876 key += AvFILL(av) + 1;
877 if (key < 0)
878 return FALSE;
879 }
880 }
881
882 mg_copy((SV*)av, sv, 0, key);
883 mg = mg_find(sv, PERL_MAGIC_tiedelem);
884 if (mg) {
885 magic_existspack(sv, mg);
886 return (bool)SvTRUE(sv);
887 }
888
889 }
890 }
891
146174a9
CB
892 if (key < 0) {
893 key += AvFILL(av) + 1;
894 if (key < 0)
895 return FALSE;
896 }
6f12eb6d 897
146174a9
CB
898 if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef
899 && AvARRAY(av)[key])
900 {
901 return TRUE;
902 }
903 else
904 return FALSE;
905}
66610fdd 906
a3874608
NC
907SV **
908Perl_av_arylen_p(pTHX_ AV *av) {
909 dVAR;
910 MAGIC *mg = mg_find((SV*)av, PERL_MAGIC_arylen_p);
911
912 if (!mg) {
1b20cd17
NC
913 mg = sv_magicext((SV*)av, 0, PERL_MAGIC_arylen_p, &PL_vtbl_arylen_p,
914 0, 0);
a3874608
NC
915
916 if (!mg) {
917 Perl_die(aTHX_ "panic: av_arylen_p");
918 }
919 /* sv_magicext won't set this for us because we pass in a NULL obj */
920 mg->mg_flags |= MGf_REFCOUNTED;
921 }
922 return &(mg->mg_obj);
923}
924
66610fdd
RGS
925/*
926 * Local variables:
927 * c-indentation-style: bsd
928 * c-basic-offset: 4
929 * indent-tabs-mode: t
930 * End:
931 *
37442d52
RGS
932 * ex: set ts=8 sts=4 sw=4 noet:
933 */