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