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