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)); |
4633a7c4 |
135 | New(2,ary, newmax+1, SV*); |
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); |
a0d0e21e |
157 | New(2,AvALLOC(av), newmax+1, SV*); |
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); |
463ee0b2 |
361 | AvREAL_on(av); |
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); |
a0d0e21e |
385 | AvFLAGS(av) = AVf_REAL; |
573fa4ea |
386 | if (size) { /* `defined' was returning undef for size==0 anyway. */ |
dd374669 |
387 | register SV** ary; |
388 | register I32 i; |
573fa4ea |
389 | New(4,ary,size,SV*); |
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); |
79072805 |
412 | New(4,ary,size+1,SV*); |
463ee0b2 |
413 | AvALLOC(av) = ary; |
79072805 |
414 | Copy(strp,ary,size,SV*); |
a0d0e21e |
415 | AvFLAGS(av) = AVf_REIFY; |
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; |
448 | /*SUPPRESS 560*/ |
a0d0e21e |
449 | |
39caa665 |
450 | if (SvREADONLY(av)) |
cea2e8a9 |
451 | Perl_croak(aTHX_ PL_no_modify); |
39caa665 |
452 | |
93965878 |
453 | /* Give any tie a chance to cleanup first */ |
454 | if (SvRMAGICAL(av)) |
455 | mg_clear((SV*)av); |
456 | |
a60c0954 |
457 | if (AvMAX(av) < 0) |
458 | return; |
459 | |
a0d0e21e |
460 | if (AvREAL(av)) { |
dd374669 |
461 | SV** ary = AvARRAY(av); |
93965878 |
462 | key = AvFILLp(av) + 1; |
a0d0e21e |
463 | while (key) { |
6b42d12b |
464 | SV * sv = ary[--key]; |
465 | /* undef the slot before freeing the value, because a |
466 | * destructor might try to modify this arrray */ |
3280af22 |
467 | ary[key] = &PL_sv_undef; |
6b42d12b |
468 | SvREFCNT_dec(sv); |
a0d0e21e |
469 | } |
470 | } |
155aba94 |
471 | if ((key = AvARRAY(av) - AvALLOC(av))) { |
463ee0b2 |
472 | AvMAX(av) += key; |
f880fe2f |
473 | SvPV_set(av, (char*)AvALLOC(av)); |
79072805 |
474 | } |
93965878 |
475 | AvFILLp(av) = -1; |
fb73857a |
476 | |
79072805 |
477 | } |
478 | |
cb50131a |
479 | /* |
480 | =for apidoc av_undef |
481 | |
482 | Undefines the array. Frees the memory used by the array itself. |
483 | |
484 | =cut |
485 | */ |
486 | |
79072805 |
487 | void |
864dbfa3 |
488 | Perl_av_undef(pTHX_ register AV *av) |
79072805 |
489 | { |
490 | register I32 key; |
491 | |
463ee0b2 |
492 | if (!av) |
79072805 |
493 | return; |
494 | /*SUPPRESS 560*/ |
93965878 |
495 | |
496 | /* Give any tie a chance to cleanup first */ |
14befaf4 |
497 | if (SvTIED_mg((SV*)av, PERL_MAGIC_tied)) |
93965878 |
498 | av_fill(av, -1); /* mg_clear() ? */ |
499 | |
a0d0e21e |
500 | if (AvREAL(av)) { |
93965878 |
501 | key = AvFILLp(av) + 1; |
a0d0e21e |
502 | while (key) |
503 | SvREFCNT_dec(AvARRAY(av)[--key]); |
504 | } |
463ee0b2 |
505 | Safefree(AvALLOC(av)); |
506 | AvALLOC(av) = 0; |
f880fe2f |
507 | SvPV_set(av, (char*)0); |
93965878 |
508 | AvMAX(av) = AvFILLp(av) = -1; |
748a9306 |
509 | if (AvARYLEN(av)) { |
510 | SvREFCNT_dec(AvARYLEN(av)); |
511 | AvARYLEN(av) = 0; |
512 | } |
79072805 |
513 | } |
514 | |
cb50131a |
515 | /* |
516 | =for apidoc av_push |
517 | |
518 | Pushes an SV onto the end of the array. The array will grow automatically |
519 | to accommodate the addition. |
520 | |
521 | =cut |
522 | */ |
523 | |
a0d0e21e |
524 | void |
864dbfa3 |
525 | Perl_av_push(pTHX_ register AV *av, SV *val) |
93965878 |
526 | { |
27da23d5 |
527 | dVAR; |
93965878 |
528 | MAGIC *mg; |
a0d0e21e |
529 | if (!av) |
530 | return; |
93965878 |
531 | if (SvREADONLY(av)) |
cea2e8a9 |
532 | Perl_croak(aTHX_ PL_no_modify); |
93965878 |
533 | |
14befaf4 |
534 | if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) { |
93965878 |
535 | dSP; |
e788e7d3 |
536 | PUSHSTACKi(PERLSI_MAGIC); |
924508f0 |
537 | PUSHMARK(SP); |
538 | EXTEND(SP,2); |
33c27489 |
539 | PUSHs(SvTIED_obj((SV*)av, mg)); |
93965878 |
540 | PUSHs(val); |
a60c0954 |
541 | PUTBACK; |
542 | ENTER; |
864dbfa3 |
543 | call_method("PUSH", G_SCALAR|G_DISCARD); |
a60c0954 |
544 | LEAVE; |
d3acc0f7 |
545 | POPSTACK; |
93965878 |
546 | return; |
547 | } |
548 | av_store(av,AvFILLp(av)+1,val); |
79072805 |
549 | } |
550 | |
cb50131a |
551 | /* |
552 | =for apidoc av_pop |
553 | |
554 | Pops an SV off the end of the array. Returns C<&PL_sv_undef> if the array |
555 | is empty. |
556 | |
557 | =cut |
558 | */ |
559 | |
79072805 |
560 | SV * |
864dbfa3 |
561 | Perl_av_pop(pTHX_ register AV *av) |
79072805 |
562 | { |
27da23d5 |
563 | dVAR; |
79072805 |
564 | SV *retval; |
93965878 |
565 | MAGIC* mg; |
79072805 |
566 | |
d19c0e07 |
567 | if (!av) |
568 | return &PL_sv_undef; |
43fcc5d2 |
569 | if (SvREADONLY(av)) |
cea2e8a9 |
570 | Perl_croak(aTHX_ PL_no_modify); |
14befaf4 |
571 | if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) { |
93965878 |
572 | dSP; |
e788e7d3 |
573 | PUSHSTACKi(PERLSI_MAGIC); |
924508f0 |
574 | PUSHMARK(SP); |
33c27489 |
575 | XPUSHs(SvTIED_obj((SV*)av, mg)); |
a60c0954 |
576 | PUTBACK; |
577 | ENTER; |
864dbfa3 |
578 | if (call_method("POP", G_SCALAR)) { |
3280af22 |
579 | retval = newSVsv(*PL_stack_sp--); |
93965878 |
580 | } else { |
3280af22 |
581 | retval = &PL_sv_undef; |
93965878 |
582 | } |
a60c0954 |
583 | LEAVE; |
d3acc0f7 |
584 | POPSTACK; |
93965878 |
585 | return retval; |
586 | } |
d19c0e07 |
587 | if (AvFILL(av) < 0) |
588 | return &PL_sv_undef; |
93965878 |
589 | retval = AvARRAY(av)[AvFILLp(av)]; |
3280af22 |
590 | AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef; |
8990e307 |
591 | if (SvSMAGICAL(av)) |
463ee0b2 |
592 | mg_set((SV*)av); |
79072805 |
593 | return retval; |
594 | } |
595 | |
cb50131a |
596 | /* |
597 | =for apidoc av_unshift |
598 | |
599 | Unshift the given number of C<undef> values onto the beginning of the |
600 | array. The array will grow automatically to accommodate the addition. You |
601 | must then use C<av_store> to assign values to these new elements. |
602 | |
603 | =cut |
604 | */ |
605 | |
79072805 |
606 | void |
864dbfa3 |
607 | Perl_av_unshift(pTHX_ register AV *av, register I32 num) |
79072805 |
608 | { |
27da23d5 |
609 | dVAR; |
79072805 |
610 | register I32 i; |
67a38de0 |
611 | register SV **ary; |
93965878 |
612 | MAGIC* mg; |
e2b534e7 |
613 | I32 slide; |
79072805 |
614 | |
d19c0e07 |
615 | if (!av) |
79072805 |
616 | return; |
43fcc5d2 |
617 | if (SvREADONLY(av)) |
cea2e8a9 |
618 | Perl_croak(aTHX_ PL_no_modify); |
93965878 |
619 | |
14befaf4 |
620 | if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) { |
93965878 |
621 | dSP; |
e788e7d3 |
622 | PUSHSTACKi(PERLSI_MAGIC); |
924508f0 |
623 | PUSHMARK(SP); |
624 | EXTEND(SP,1+num); |
33c27489 |
625 | PUSHs(SvTIED_obj((SV*)av, mg)); |
93965878 |
626 | while (num-- > 0) { |
3280af22 |
627 | PUSHs(&PL_sv_undef); |
93965878 |
628 | } |
629 | PUTBACK; |
a60c0954 |
630 | ENTER; |
864dbfa3 |
631 | call_method("UNSHIFT", G_SCALAR|G_DISCARD); |
a60c0954 |
632 | LEAVE; |
d3acc0f7 |
633 | POPSTACK; |
93965878 |
634 | return; |
635 | } |
636 | |
d19c0e07 |
637 | if (num <= 0) |
638 | return; |
49beac48 |
639 | if (!AvREAL(av) && AvREIFY(av)) |
640 | av_reify(av); |
a0d0e21e |
641 | i = AvARRAY(av) - AvALLOC(av); |
642 | if (i) { |
643 | if (i > num) |
644 | i = num; |
645 | num -= i; |
646 | |
647 | AvMAX(av) += i; |
93965878 |
648 | AvFILLp(av) += i; |
f880fe2f |
649 | SvPV_set(av, (char*)(AvARRAY(av) - i)); |
a0d0e21e |
650 | } |
d2719217 |
651 | if (num) { |
67a38de0 |
652 | i = AvFILLp(av); |
e2b534e7 |
653 | /* Create extra elements */ |
654 | slide = i > 0 ? i : 0; |
655 | num += slide; |
67a38de0 |
656 | av_extend(av, i + num); |
93965878 |
657 | AvFILLp(av) += num; |
67a38de0 |
658 | ary = AvARRAY(av); |
659 | Move(ary, ary + num, i + 1, SV*); |
660 | do { |
3280af22 |
661 | ary[--num] = &PL_sv_undef; |
67a38de0 |
662 | } while (num); |
e2b534e7 |
663 | /* Make extra elements into a buffer */ |
664 | AvMAX(av) -= slide; |
665 | AvFILLp(av) -= slide; |
f880fe2f |
666 | SvPV_set(av, (char*)(AvARRAY(av) + slide)); |
79072805 |
667 | } |
668 | } |
669 | |
cb50131a |
670 | /* |
671 | =for apidoc av_shift |
672 | |
673 | Shifts an SV off the beginning of the array. |
674 | |
675 | =cut |
676 | */ |
677 | |
79072805 |
678 | SV * |
864dbfa3 |
679 | Perl_av_shift(pTHX_ register AV *av) |
79072805 |
680 | { |
27da23d5 |
681 | dVAR; |
79072805 |
682 | SV *retval; |
93965878 |
683 | MAGIC* mg; |
79072805 |
684 | |
d19c0e07 |
685 | if (!av) |
3280af22 |
686 | return &PL_sv_undef; |
43fcc5d2 |
687 | if (SvREADONLY(av)) |
cea2e8a9 |
688 | Perl_croak(aTHX_ PL_no_modify); |
14befaf4 |
689 | if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) { |
93965878 |
690 | dSP; |
e788e7d3 |
691 | PUSHSTACKi(PERLSI_MAGIC); |
924508f0 |
692 | PUSHMARK(SP); |
33c27489 |
693 | XPUSHs(SvTIED_obj((SV*)av, mg)); |
a60c0954 |
694 | PUTBACK; |
695 | ENTER; |
864dbfa3 |
696 | if (call_method("SHIFT", G_SCALAR)) { |
3280af22 |
697 | retval = newSVsv(*PL_stack_sp--); |
93965878 |
698 | } else { |
3280af22 |
699 | retval = &PL_sv_undef; |
a60c0954 |
700 | } |
701 | LEAVE; |
d3acc0f7 |
702 | POPSTACK; |
93965878 |
703 | return retval; |
704 | } |
d19c0e07 |
705 | if (AvFILL(av) < 0) |
706 | return &PL_sv_undef; |
463ee0b2 |
707 | retval = *AvARRAY(av); |
a0d0e21e |
708 | if (AvREAL(av)) |
3280af22 |
709 | *AvARRAY(av) = &PL_sv_undef; |
f880fe2f |
710 | SvPV_set(av, (char*)(AvARRAY(av) + 1)); |
463ee0b2 |
711 | AvMAX(av)--; |
93965878 |
712 | AvFILLp(av)--; |
8990e307 |
713 | if (SvSMAGICAL(av)) |
463ee0b2 |
714 | mg_set((SV*)av); |
79072805 |
715 | return retval; |
716 | } |
717 | |
cb50131a |
718 | /* |
719 | =for apidoc av_len |
720 | |
721 | Returns the highest index in the array. Returns -1 if the array is |
722 | empty. |
723 | |
724 | =cut |
725 | */ |
726 | |
79072805 |
727 | I32 |
35a4481c |
728 | Perl_av_len(pTHX_ const register AV *av) |
79072805 |
729 | { |
463ee0b2 |
730 | return AvFILL(av); |
79072805 |
731 | } |
732 | |
f3b76584 |
733 | /* |
734 | =for apidoc av_fill |
735 | |
736 | Ensure than an array has a given number of elements, equivalent to |
737 | Perl's C<$#array = $fill;>. |
738 | |
739 | =cut |
740 | */ |
79072805 |
741 | void |
864dbfa3 |
742 | Perl_av_fill(pTHX_ register AV *av, I32 fill) |
79072805 |
743 | { |
27da23d5 |
744 | dVAR; |
93965878 |
745 | MAGIC *mg; |
a0d0e21e |
746 | if (!av) |
cea2e8a9 |
747 | Perl_croak(aTHX_ "panic: null array"); |
79072805 |
748 | if (fill < 0) |
749 | fill = -1; |
14befaf4 |
750 | if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) { |
93965878 |
751 | dSP; |
752 | ENTER; |
753 | SAVETMPS; |
e788e7d3 |
754 | PUSHSTACKi(PERLSI_MAGIC); |
924508f0 |
755 | PUSHMARK(SP); |
756 | EXTEND(SP,2); |
33c27489 |
757 | PUSHs(SvTIED_obj((SV*)av, mg)); |
a60c0954 |
758 | PUSHs(sv_2mortal(newSViv(fill+1))); |
93965878 |
759 | PUTBACK; |
864dbfa3 |
760 | call_method("STORESIZE", G_SCALAR|G_DISCARD); |
d3acc0f7 |
761 | POPSTACK; |
93965878 |
762 | FREETMPS; |
763 | LEAVE; |
764 | return; |
765 | } |
463ee0b2 |
766 | if (fill <= AvMAX(av)) { |
93965878 |
767 | I32 key = AvFILLp(av); |
a0d0e21e |
768 | SV** ary = AvARRAY(av); |
769 | |
770 | if (AvREAL(av)) { |
771 | while (key > fill) { |
772 | SvREFCNT_dec(ary[key]); |
3280af22 |
773 | ary[key--] = &PL_sv_undef; |
a0d0e21e |
774 | } |
775 | } |
776 | else { |
777 | while (key < fill) |
3280af22 |
778 | ary[++key] = &PL_sv_undef; |
a0d0e21e |
779 | } |
780 | |
93965878 |
781 | AvFILLp(av) = fill; |
8990e307 |
782 | if (SvSMAGICAL(av)) |
463ee0b2 |
783 | mg_set((SV*)av); |
784 | } |
a0d0e21e |
785 | else |
3280af22 |
786 | (void)av_store(av,fill,&PL_sv_undef); |
79072805 |
787 | } |
c750a3ec |
788 | |
f3b76584 |
789 | /* |
790 | =for apidoc av_delete |
791 | |
792 | Deletes the element indexed by C<key> from the array. Returns the |
a6214072 |
793 | deleted element. If C<flags> equals C<G_DISCARD>, the element is freed |
794 | and null is returned. |
f3b76584 |
795 | |
796 | =cut |
797 | */ |
146174a9 |
798 | SV * |
799 | Perl_av_delete(pTHX_ AV *av, I32 key, I32 flags) |
800 | { |
801 | SV *sv; |
802 | |
803 | if (!av) |
804 | return Nullsv; |
805 | if (SvREADONLY(av)) |
806 | Perl_croak(aTHX_ PL_no_modify); |
6f12eb6d |
807 | |
808 | if (SvRMAGICAL(av)) { |
35a4481c |
809 | const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied); |
6f12eb6d |
810 | if ((tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata))) { |
811 | /* Handle negative array indices 20020222 MJD */ |
35a4481c |
812 | SV **svp; |
6f12eb6d |
813 | if (key < 0) { |
814 | unsigned adjust_index = 1; |
815 | if (tied_magic) { |
816 | SV **negative_indices_glob = |
817 | hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av, |
818 | tied_magic))), |
819 | NEGATIVE_INDICES_VAR, 16, 0); |
820 | if (negative_indices_glob |
821 | && SvTRUE(GvSV(*negative_indices_glob))) |
822 | adjust_index = 0; |
823 | } |
824 | if (adjust_index) { |
825 | key += AvFILL(av) + 1; |
826 | if (key < 0) |
827 | return Nullsv; |
828 | } |
829 | } |
830 | svp = av_fetch(av, key, TRUE); |
831 | if (svp) { |
832 | sv = *svp; |
833 | mg_clear(sv); |
834 | if (mg_find(sv, PERL_MAGIC_tiedelem)) { |
835 | sv_unmagic(sv, PERL_MAGIC_tiedelem); /* No longer an element */ |
836 | return sv; |
837 | } |
838 | return Nullsv; |
839 | } |
840 | } |
841 | } |
842 | |
146174a9 |
843 | if (key < 0) { |
844 | key += AvFILL(av) + 1; |
845 | if (key < 0) |
846 | return Nullsv; |
847 | } |
6f12eb6d |
848 | |
146174a9 |
849 | if (key > AvFILLp(av)) |
850 | return Nullsv; |
851 | else { |
a6214072 |
852 | if (!AvREAL(av) && AvREIFY(av)) |
853 | av_reify(av); |
146174a9 |
854 | sv = AvARRAY(av)[key]; |
855 | if (key == AvFILLp(av)) { |
d9c63288 |
856 | AvARRAY(av)[key] = &PL_sv_undef; |
146174a9 |
857 | do { |
858 | AvFILLp(av)--; |
859 | } while (--key >= 0 && AvARRAY(av)[key] == &PL_sv_undef); |
860 | } |
861 | else |
862 | AvARRAY(av)[key] = &PL_sv_undef; |
863 | if (SvSMAGICAL(av)) |
864 | mg_set((SV*)av); |
865 | } |
866 | if (flags & G_DISCARD) { |
867 | SvREFCNT_dec(sv); |
868 | sv = Nullsv; |
869 | } |
fdb3bdd0 |
870 | else if (AvREAL(av)) |
2c8ddff3 |
871 | sv = sv_2mortal(sv); |
146174a9 |
872 | return sv; |
873 | } |
874 | |
875 | /* |
f3b76584 |
876 | =for apidoc av_exists |
877 | |
878 | Returns true if the element indexed by C<key> has been initialized. |
146174a9 |
879 | |
f3b76584 |
880 | This relies on the fact that uninitialized array elements are set to |
881 | C<&PL_sv_undef>. |
882 | |
883 | =cut |
884 | */ |
146174a9 |
885 | bool |
886 | Perl_av_exists(pTHX_ AV *av, I32 key) |
887 | { |
888 | if (!av) |
889 | return FALSE; |
6f12eb6d |
890 | |
891 | |
892 | if (SvRMAGICAL(av)) { |
35a4481c |
893 | const MAGIC * const tied_magic = mg_find((SV*)av, PERL_MAGIC_tied); |
6f12eb6d |
894 | if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) { |
895 | SV *sv = sv_newmortal(); |
896 | MAGIC *mg; |
897 | /* Handle negative array indices 20020222 MJD */ |
898 | if (key < 0) { |
899 | unsigned adjust_index = 1; |
900 | if (tied_magic) { |
901 | SV **negative_indices_glob = |
902 | hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av, |
903 | tied_magic))), |
904 | NEGATIVE_INDICES_VAR, 16, 0); |
905 | if (negative_indices_glob |
906 | && SvTRUE(GvSV(*negative_indices_glob))) |
907 | adjust_index = 0; |
908 | } |
909 | if (adjust_index) { |
910 | key += AvFILL(av) + 1; |
911 | if (key < 0) |
912 | return FALSE; |
913 | } |
914 | } |
915 | |
916 | mg_copy((SV*)av, sv, 0, key); |
917 | mg = mg_find(sv, PERL_MAGIC_tiedelem); |
918 | if (mg) { |
919 | magic_existspack(sv, mg); |
920 | return (bool)SvTRUE(sv); |
921 | } |
922 | |
923 | } |
924 | } |
925 | |
146174a9 |
926 | if (key < 0) { |
927 | key += AvFILL(av) + 1; |
928 | if (key < 0) |
929 | return FALSE; |
930 | } |
6f12eb6d |
931 | |
146174a9 |
932 | if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef |
933 | && AvARRAY(av)[key]) |
934 | { |
935 | return TRUE; |
936 | } |
937 | else |
938 | return FALSE; |
939 | } |
66610fdd |
940 | |
941 | /* |
942 | * Local variables: |
943 | * c-indentation-style: bsd |
944 | * c-basic-offset: 4 |
945 | * indent-tabs-mode: t |
946 | * End: |
947 | * |
37442d52 |
948 | * ex: set ts=8 sts=4 sw=4 noet: |
949 | */ |