3 * Copyright (c) 1991-1997, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "...for the Entwives desired order, and plenty, and peace (by which they
12 * meant that things should remain where they had set them)." --Treebeard
18 static void av_reify _((AV* av));
28 while (key > AvFILL(av) + 1)
29 AvARRAY(av)[--key] = &sv_undef;
31 sv = AvARRAY(av)[--key];
34 (void)SvREFCNT_inc(sv);
36 key = AvARRAY(av) - AvALLOC(av);
38 AvALLOC(av)[--key] = &sv_undef;
47 if (key > AvMAX(av)) {
52 if (AvALLOC(av) != AvARRAY(av)) {
53 ary = AvALLOC(av) + AvFILL(av) + 1;
54 tmp = AvARRAY(av) - AvALLOC(av);
55 Move(AvARRAY(av), AvALLOC(av), AvFILL(av)+1, SV*);
57 SvPVX(av) = (char*)AvALLOC(av);
60 ary[--tmp] = &sv_undef;
63 if (key > AvMAX(av) - 10) {
64 newmax = key + AvMAX(av);
70 #ifndef STRANGE_MALLOC
74 newmax = key + AvMAX(av) / 5;
77 Renew(AvALLOC(av),newmax+1, SV*);
79 bytes = (newmax + 1) * sizeof(SV*);
80 #define MALLOC_OVERHEAD 16
81 tmp = MALLOC_OVERHEAD;
82 while (tmp - MALLOC_OVERHEAD < bytes)
84 tmp -= MALLOC_OVERHEAD;
88 New(2,ary, newmax+1, SV*);
89 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
90 if (AvMAX(av) > 64 && !nice_chunk) {
91 nice_chunk = (char*)AvALLOC(av);
92 nice_chunk_size = (AvMAX(av) + 1) * sizeof(SV*);
95 Safefree(AvALLOC(av));
98 ary = AvALLOC(av) + AvMAX(av) + 1;
99 tmp = newmax - AvMAX(av);
100 if (av == curstack) { /* Oops, grew stack (via av_store()?) */
101 stack_sp = AvALLOC(av) + (stack_sp - stack_base);
102 stack_base = AvALLOC(av);
103 stack_max = stack_base + newmax;
107 newmax = key < 4 ? 4 : key;
108 New(2,AvALLOC(av), newmax+1, SV*);
109 ary = AvALLOC(av) + 1;
111 AvALLOC(av)[0] = &sv_undef; /* For the stacks */
115 ary[--tmp] = &sv_undef;
118 SvPVX(av) = (char*)AvALLOC(av);
125 av_fetch(av,key,lval)
135 if (SvRMAGICAL(av)) {
136 if (mg_find((SV*)av,'P')) {
138 mg_copy((SV*)av, sv, 0, key);
145 key += AvFILL(av) + 1;
149 else if (key > AvFILL(av)) {
156 return av_store(av,key,sv);
158 if (AvARRAY(av)[key] == &sv_undef) {
162 return av_store(av,key,sv);
167 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
168 || SvTYPE(AvARRAY(av)[key]) == SVTYPEMASK)) {
169 AvARRAY(av)[key] = &sv_undef; /* 1/2 reify */
172 return &AvARRAY(av)[key];
188 if (SvRMAGICAL(av)) {
189 if (mg_find((SV*)av,'P')) {
190 if (val != &sv_undef)
191 mg_copy((SV*)av, val, 0, key);
197 key += AvFILL(av) + 1;
201 if (SvREADONLY(av) && key >= AvFILL(av))
203 if (!AvREAL(av) && AvREIFY(av))
208 if (AvFILL(av) < key) {
210 if (av == curstack && key > stack_sp - stack_base)
211 stack_sp = stack_base + key; /* XPUSH in disguise */
213 ary[++AvFILL(av)] = &sv_undef;
214 while (AvFILL(av) < key);
219 SvREFCNT_dec(ary[key]);
221 if (SvSMAGICAL(av)) {
222 if (val != &sv_undef) {
223 MAGIC* mg = SvMAGIC(av);
224 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
236 av = (AV*)NEWSV(3,0);
237 sv_upgrade((SV *)av, SVt_PVAV);
241 AvMAX(av) = AvFILL(av) = -1;
254 av = (AV*)NEWSV(8,0);
255 sv_upgrade((SV *) av,SVt_PVAV);
256 New(4,ary,size+1,SV*);
258 AvFLAGS(av) = AVf_REAL;
259 SvPVX(av) = (char*)ary;
260 AvFILL(av) = size - 1;
261 AvMAX(av) = size - 1;
262 for (i = 0; i < size; i++) {
265 sv_setsv(ary[i], *strp);
279 av = (AV*)NEWSV(9,0);
280 sv_upgrade((SV *)av, SVt_PVAV);
281 New(4,ary,size+1,SV*);
283 Copy(strp,ary,size,SV*);
284 AvFLAGS(av) = AVf_REIFY;
285 SvPVX(av) = (char*)ary;
286 AvFILL(av) = size - 1;
287 AvMAX(av) = size - 1;
304 if (SvREFCNT(av) <= 0) {
305 warn("Attempt to clear deleted array");
308 if (!av || AvMAX(av) < 0)
314 key = AvFILL(av) + 1;
316 SvREFCNT_dec(ary[--key]);
317 ary[key] = &sv_undef;
320 if (key = AvARRAY(av) - AvALLOC(av)) {
322 SvPVX(av) = (char*)AvALLOC(av);
337 key = AvFILL(av) + 1;
339 SvREFCNT_dec(AvARRAY(av)[--key]);
341 Safefree(AvALLOC(av));
344 AvMAX(av) = AvFILL(av) = -1;
346 SvREFCNT_dec(AvARYLEN(av));
358 av_store(av,AvFILL(av)+1,val);
367 if (!av || AvFILL(av) < 0)
371 retval = AvARRAY(av)[AvFILL(av)];
372 AvARRAY(av)[AvFILL(av)--] = &sv_undef;
384 register SV **sstr,**dstr;
390 if (!AvREAL(av) && AvREIFY(av))
392 i = AvARRAY(av) - AvALLOC(av);
400 SvPVX(av) = (char*)(AvARRAY(av) - i);
403 av_extend(av,AvFILL(av)+num);
405 dstr = AvARRAY(av) + AvFILL(av);
408 # pragma loop_opt(off) /* don't loop-optimize the following code */
409 #endif /* BUGGY_MSC5 */
410 for (i = AvFILL(av) - num; i >= 0; --i) {
413 # pragma loop_opt() /* loop-optimization back to command-line setting */
414 #endif /* BUGGY_MSC5 */
417 AvARRAY(av)[--num] = &sv_undef;
427 if (!av || AvFILL(av) < 0)
431 retval = *AvARRAY(av);
433 *AvARRAY(av) = &sv_undef;
434 SvPVX(av) = (char*)(AvARRAY(av) + 1);
455 croak("panic: null array");
458 if (fill <= AvMAX(av)) {
459 I32 key = AvFILL(av);
460 SV** ary = AvARRAY(av);
464 SvREFCNT_dec(ary[key]);
465 ary[key--] = &sv_undef;
470 ary[++key] = &sv_undef;
478 (void)av_store(av,fill,&sv_undef);