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);
67 #ifndef STRANGE_MALLOC
71 newmax = key + AvMAX(av) / 5;
74 Renew(AvALLOC(av),newmax+1, SV*);
76 bytes = (newmax + 1) * sizeof(SV*);
77 #define MALLOC_OVERHEAD 16
78 tmp = MALLOC_OVERHEAD;
79 while (tmp - MALLOC_OVERHEAD < bytes)
81 tmp -= MALLOC_OVERHEAD;
85 New(2,ary, newmax+1, SV*);
86 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
87 if (AvMAX(av) > 64 && !nice_chunk) {
88 nice_chunk = (char*)AvALLOC(av);
89 nice_chunk_size = (AvMAX(av) + 1) * sizeof(SV*);
92 Safefree(AvALLOC(av));
95 ary = AvALLOC(av) + AvMAX(av) + 1;
96 tmp = newmax - AvMAX(av);
97 if (av == curstack) { /* Oops, grew stack (via av_store()?) */
98 stack_sp = AvALLOC(av) + (stack_sp - stack_base);
99 stack_base = AvALLOC(av);
100 stack_max = stack_base + newmax;
104 newmax = key < 4 ? 4 : key;
105 New(2,AvALLOC(av), newmax+1, SV*);
106 ary = AvALLOC(av) + 1;
108 AvALLOC(av)[0] = &sv_undef; /* For the stacks */
112 ary[--tmp] = &sv_undef;
115 SvPVX(av) = (char*)AvALLOC(av);
122 av_fetch(av,key,lval)
132 if (SvRMAGICAL(av)) {
133 if (mg_find((SV*)av,'P')) {
135 mg_copy((SV*)av, sv, 0, key);
142 key += AvFILL(av) + 1;
146 else if (key > AvFILL(av)) {
153 return av_store(av,key,sv);
155 if (AvARRAY(av)[key] == &sv_undef) {
158 return av_store(av,key,sv);
162 return &AvARRAY(av)[key];
176 if (SvRMAGICAL(av)) {
177 if (mg_find((SV*)av,'P')) {
178 mg_copy((SV*)av, val, 0, key);
184 key += AvFILL(av) + 1;
197 if (AvFILL(av) < key) {
199 if (av == curstack && key > stack_sp - stack_base)
200 stack_sp = stack_base + key; /* XPUSH in disguise */
202 ary[++AvFILL(av)] = &sv_undef;
203 while (AvFILL(av) < key);
208 SvREFCNT_dec(ary[key]);
210 if (SvSMAGICAL(av)) {
211 if (val != &sv_undef) {
212 MAGIC* mg = SvMAGIC(av);
213 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
225 av = (AV*)NEWSV(3,0);
226 sv_upgrade((SV *)av, SVt_PVAV);
230 AvMAX(av) = AvFILL(av) = -1;
243 av = (AV*)NEWSV(8,0);
244 sv_upgrade((SV *) av,SVt_PVAV);
245 New(4,ary,size+1,SV*);
247 AvFLAGS(av) = AVf_REAL;
248 SvPVX(av) = (char*)ary;
249 AvFILL(av) = size - 1;
250 AvMAX(av) = size - 1;
251 for (i = 0; i < size; i++) {
254 sv_setsv(ary[i], *strp);
268 av = (AV*)NEWSV(9,0);
269 sv_upgrade((SV *)av, SVt_PVAV);
270 New(4,ary,size+1,SV*);
272 Copy(strp,ary,size,SV*);
273 AvFLAGS(av) = AVf_REIFY;
274 SvPVX(av) = (char*)ary;
275 AvFILL(av) = size - 1;
276 AvMAX(av) = size - 1;
293 if (SvREFCNT(av) <= 0) {
294 warn("Attempt to clear deleted array");
297 if (!av || AvMAX(av) < 0)
303 key = AvFILL(av) + 1;
305 SvREFCNT_dec(ary[--key]);
306 ary[key] = &sv_undef;
309 if (key = AvARRAY(av) - AvALLOC(av)) {
311 SvPVX(av) = (char*)AvALLOC(av);
326 key = AvFILL(av) + 1;
328 SvREFCNT_dec(AvARRAY(av)[--key]);
330 if (key = AvARRAY(av) - AvALLOC(av)) {
332 SvPVX(av) = (char*)AvALLOC(av);
334 Safefree(AvALLOC(av));
337 AvMAX(av) = AvFILL(av) = -1;
339 SvREFCNT_dec(AvARYLEN(av));
351 av_store(av,AvFILL(av)+1,val);
360 if (!av || AvFILL(av) < 0)
362 retval = AvARRAY(av)[AvFILL(av)];
363 AvARRAY(av)[AvFILL(av)--] = &sv_undef;
375 register SV **sstr,**dstr;
383 croak("Can't unshift");
385 i = AvARRAY(av) - AvALLOC(av);
393 SvPVX(av) = (char*)(AvARRAY(av) - i);
396 av_extend(av,AvFILL(av)+num);
398 dstr = AvARRAY(av) + AvFILL(av);
401 # pragma loop_opt(off) /* don't loop-optimize the following code */
402 #endif /* BUGGY_MSC5 */
403 for (i = AvFILL(av) - num; i >= 0; --i) {
406 # pragma loop_opt() /* loop-optimization back to command-line setting */
407 #endif /* BUGGY_MSC5 */
410 AvARRAY(av)[--num] = &sv_undef;
420 if (!av || AvFILL(av) < 0)
422 retval = *AvARRAY(av);
424 *AvARRAY(av) = &sv_undef;
425 SvPVX(av) = (char*)(AvARRAY(av) + 1);
446 croak("panic: null array");
449 if (fill <= AvMAX(av)) {
450 I32 key = AvFILL(av);
451 SV** ary = AvARRAY(av);
455 SvREFCNT_dec(ary[key]);
456 ary[key--] = &sv_undef;
461 ary[++key] = &sv_undef;
469 (void)av_store(av,fill,&sv_undef);