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