3 * Copyright (c) 1991-1994, 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);
44 if (key > AvMAX(av)) {
49 if (AvALLOC(av) != AvARRAY(av)) {
50 ary = AvALLOC(av) + AvFILL(av) + 1;
51 tmp = AvARRAY(av) - AvALLOC(av);
52 Move(AvARRAY(av), AvALLOC(av), AvFILL(av)+1, SV*);
54 SvPVX(av) = (char*)AvALLOC(av);
57 ary[--tmp] = &sv_undef;
60 if (key > AvMAX(av) - 10) {
61 newmax = key + AvMAX(av);
69 newmax = key + AvMAX(av) / 5;
72 Renew(AvALLOC(av),newmax+1, SV*);
74 bytes = (newmax + 1) * sizeof(SV*);
75 #define MALLOC_OVERHEAD 16
76 tmp = MALLOC_OVERHEAD;
77 while (tmp - MALLOC_OVERHEAD < bytes)
79 tmp -= MALLOC_OVERHEAD;
83 New(2,ary, newmax+1, SV*);
84 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
85 if (AvMAX(av) > 64 && !AvREUSED(av))
86 sv_add_arena((char*)AvALLOC(av), AvMAX(av) * sizeof(SV*),0);
88 Safefree(AvALLOC(av));
91 ary = AvALLOC(av) + AvMAX(av) + 1;
92 tmp = newmax - AvMAX(av);
93 if (av == stack) { /* Oops, grew stack (via av_store()?) */
94 stack_sp = AvALLOC(av) + (stack_sp - stack_base);
95 stack_base = AvALLOC(av);
96 stack_max = stack_base + newmax;
100 newmax = key < 4 ? 4 : key;
101 New(2,AvALLOC(av), newmax+1, SV*);
102 ary = AvALLOC(av) + 1;
104 AvALLOC(av)[0] = &sv_undef; /* For the stacks */
108 ary[--tmp] = &sv_undef;
111 SvPVX(av) = (char*)AvALLOC(av);
118 av_fetch(av,key,lval)
128 if (SvRMAGICAL(av)) {
129 if (mg_find((SV*)av,'P')) {
131 mg_copy((SV*)av, sv, 0, key);
138 key += AvFILL(av) + 1;
142 else if (key > AvFILL(av)) {
149 return av_store(av,key,sv);
151 if (AvARRAY(av)[key] == &sv_undef) {
154 return av_store(av,key,sv);
158 return &AvARRAY(av)[key];
172 if (SvRMAGICAL(av)) {
173 if (mg_find((SV*)av,'P')) {
174 mg_copy((SV*)av, val, 0, key);
180 key += AvFILL(av) + 1;
193 if (AvFILL(av) < key) {
195 if (av == stack && key > stack_sp - stack_base)
196 stack_sp = stack_base + key; /* XPUSH in disguise */
198 ary[++AvFILL(av)] = &sv_undef;
199 while (AvFILL(av) < key);
204 SvREFCNT_dec(ary[key]);
206 if (SvSMAGICAL(av)) {
207 if (val != &sv_undef) {
208 MAGIC* mg = SvMAGIC(av);
209 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
221 av = (AV*)NEWSV(3,0);
222 sv_upgrade((SV *)av, SVt_PVAV);
226 AvMAX(av) = AvFILL(av) = -1;
239 av = (AV*)NEWSV(8,0);
240 sv_upgrade((SV *) av,SVt_PVAV);
241 New(4,ary,size+1,SV*);
243 AvFLAGS(av) = AVf_REAL;
244 SvPVX(av) = (char*)ary;
245 AvFILL(av) = size - 1;
246 AvMAX(av) = size - 1;
247 for (i = 0; i < size; i++) {
250 sv_setsv(ary[i], *strp);
264 av = (AV*)NEWSV(9,0);
265 sv_upgrade((SV *)av, SVt_PVAV);
266 New(4,ary,size+1,SV*);
268 Copy(strp,ary,size,SV*);
269 AvFLAGS(av) = AVf_REIFY;
270 SvPVX(av) = (char*)ary;
271 AvFILL(av) = size - 1;
272 AvMAX(av) = size - 1;
288 if (!av || AvMAX(av) < 0)
294 key = AvFILL(av) + 1;
296 SvREFCNT_dec(ary[--key]);
297 ary[key] = &sv_undef;
300 if (key = AvARRAY(av) - AvALLOC(av)) {
302 SvPVX(av) = (char*)AvALLOC(av);
317 key = AvFILL(av) + 1;
319 SvREFCNT_dec(AvARRAY(av)[--key]);
321 if (key = AvARRAY(av) - AvALLOC(av)) {
323 SvPVX(av) = (char*)AvALLOC(av);
325 Safefree(AvALLOC(av));
328 AvMAX(av) = AvFILL(av) = -1;
329 AvREUSED_on(av); /* Avoid leak of making SVs out of old memory again. */
331 SvREFCNT_dec(AvARYLEN(av));
343 av_store(av,AvFILL(av)+1,val);
352 if (!av || AvFILL(av) < 0)
354 retval = AvARRAY(av)[AvFILL(av)];
355 AvARRAY(av)[AvFILL(av)--] = &sv_undef;
367 register SV **sstr,**dstr;
375 croak("Can't unshift");
377 i = AvARRAY(av) - AvALLOC(av);
385 SvPVX(av) = (char*)(AvARRAY(av) - i);
388 av_extend(av,AvFILL(av)+num);
390 dstr = AvARRAY(av) + AvFILL(av);
393 # pragma loop_opt(off) /* don't loop-optimize the following code */
394 #endif /* BUGGY_MSC5 */
395 for (i = AvFILL(av) - num; i >= 0; --i) {
398 # pragma loop_opt() /* loop-optimization back to command-line setting */
399 #endif /* BUGGY_MSC5 */
402 AvARRAY(av)[--num] = &sv_undef;
412 if (!av || AvFILL(av) < 0)
414 retval = *AvARRAY(av);
416 *AvARRAY(av) = &sv_undef;
417 SvPVX(av) = (char*)(AvARRAY(av) + 1);
438 croak("panic: null array");
441 if (fill <= AvMAX(av)) {
442 I32 key = AvFILL(av);
443 SV** ary = AvARRAY(av);
447 SvREFCNT_dec(ary[key]);
448 ary[key--] = &sv_undef;
453 ary[++key] = &sv_undef;
461 (void)av_store(av,fill,&sv_undef);