-
Notifications
You must be signed in to change notification settings - Fork 561
/
Copy pathscope.c
2012 lines (1701 loc) · 57.2 KB
/
scope.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* scope.c
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
* 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* For the fashion of Minas Tirith was such that it was built on seven
* levels...
*
* [p.751 of _The Lord of the Rings_, V/i: "Minas Tirith"]
*/
/* This file contains functions to manipulate several of Perl's stacks;
* in particular it contains code to push various types of things onto
* the savestack, then to pop them off and perform the correct restorative
* action for each one. This corresponds to the cleanup Perl does at
* each scope exit.
*/
#include "EXTERN.h"
#define PERL_IN_SCOPE_C
#include "perl.h"
#include "feature.h"
SV**
Perl_stack_grow(pTHX_ SV **sp, SV **p, SSize_t n)
{
SSize_t extra;
SSize_t current = (p - PL_stack_base);
PERL_ARGS_ASSERT_STACK_GROW;
if (UNLIKELY(n < 0))
Perl_croak(aTHX_
"panic: stack_grow() negative count (%" IVdf ")", (IV)n);
PL_stack_sp = sp;
extra =
#ifdef STRESS_REALLOC
1;
#else
128;
#endif
/* If the total might wrap, panic instead. This is really testing
* that (current + n + extra < Stack_off_t_MAX), but done in a way that
* can't wrap */
if (UNLIKELY( current > Stack_off_t_MAX - extra
|| current + extra > Stack_off_t_MAX - n
))
/* diag_listed_as: Out of memory during %s extend */
Perl_croak(aTHX_ "Out of memory during stack extend");
av_extend(PL_curstack, current + n + extra);
#ifdef PERL_USE_HWM
PL_curstackinfo->si_stack_hwm = current + n + extra;
#endif
return PL_stack_sp;
}
#ifdef STRESS_REALLOC
#define GROW(old) ((old) + 1)
#else
#define GROW(old) ((old) * 3 / 2)
#endif
/* for backcomp */
PERL_SI *
Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems)
{
return new_stackinfo_flags(stitems, cxitems, 0);
}
/* current flag meanings:
* 1 make the new arg stack AvREAL
*/
PERL_SI *
Perl_new_stackinfo_flags(pTHX_ I32 stitems, I32 cxitems, UV flags)
{
PERL_SI *si;
Newx(si, 1, PERL_SI);
si->si_stack = newAV();
if (!(flags & 1))
AvREAL_off(si->si_stack);
av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0);
AvALLOC(si->si_stack)[0] = &PL_sv_undef;
AvFILLp(si->si_stack) = 0;
#ifdef PERL_RC_STACK
si->si_stack_nonrc_base = 0;
#endif
si->si_prev = 0;
si->si_next = 0;
si->si_cxmax = cxitems - 1;
si->si_cxix = -1;
si->si_cxsubix = -1;
si->si_type = PERLSI_UNDEF;
Newx(si->si_cxstack, cxitems, PERL_CONTEXT);
/* Without any kind of initialising CX_PUSHSUBST()
* in pp_subst() will read uninitialised heap. */
PoisonNew(si->si_cxstack, cxitems, PERL_CONTEXT);
return si;
}
I32
Perl_cxinc(pTHX)
{
const IV old_max = cxstack_max;
const IV new_max = GROW(cxstack_max);
Renew(cxstack, new_max + 1, PERL_CONTEXT);
cxstack_max = new_max;
/* Without any kind of initialising deep enough recursion
* will end up reading uninitialised PERL_CONTEXTs. */
PoisonNew(cxstack + old_max + 1, new_max - old_max, PERL_CONTEXT);
return cxstack_ix + 1;
}
/*
=for apidoc_section $callback
=for apidoc push_scope
Implements L<perlapi/C<ENTER>>
=cut
*/
void
Perl_push_scope(pTHX)
{
if (UNLIKELY(PL_scopestack_ix == PL_scopestack_max)) {
const IV new_max = GROW(PL_scopestack_max);
Renew(PL_scopestack, new_max, I32);
#ifdef DEBUGGING
Renew(PL_scopestack_name, new_max, const char*);
#endif
PL_scopestack_max = new_max;
}
#ifdef DEBUGGING
PL_scopestack_name[PL_scopestack_ix] = "unknown";
#endif
PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix;
}
/*
=for apidoc_section $callback
=for apidoc pop_scope
Implements L<perlapi/C<LEAVE>>
=cut
*/
void
Perl_pop_scope(pTHX)
{
const I32 oldsave = PL_scopestack[--PL_scopestack_ix];
LEAVE_SCOPE(oldsave);
}
Stack_off_t *
Perl_markstack_grow(pTHX)
{
const I32 oldmax = PL_markstack_max - PL_markstack;
const I32 newmax = GROW(oldmax);
Renew(PL_markstack, newmax, Stack_off_t);
PL_markstack_max = PL_markstack + newmax;
PL_markstack_ptr = PL_markstack + oldmax;
DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
"MARK grow %p %" IVdf " by %" IVdf "\n",
PL_markstack_ptr, (IV)*PL_markstack_ptr, (IV)oldmax)));
return PL_markstack_ptr;
}
void
Perl_savestack_grow(pTHX)
{
const I32 by = PL_savestack_max - PL_savestack_ix;
Perl_savestack_grow_cnt(aTHX_ by);
}
void
Perl_savestack_grow_cnt(pTHX_ I32 need)
{
/* NOTE: PL_savestack_max and PL_savestack_ix are I32.
*
* This makes sense when you consider that having I32_MAX items on
* the stack would be quite large.
*
* However, we use IV here so that we can detect if the new requested
* amount is larger than I32_MAX.
*/
const IV new_floor = PL_savestack_max + need; /* what we need */
/* the GROW() macro normally does scales by 1.5 but under
* STRESS_REALLOC it simply adds 1 */
IV new_max = GROW(new_floor); /* and some extra */
/* the new_max < PL_savestack_max is for cases where IV is I32
* and we have rolled over from I32_MAX to a small value */
if (new_max > I32_MAX || new_max < PL_savestack_max) {
if (new_floor > I32_MAX || new_floor < PL_savestack_max) {
Perl_croak(aTHX_ "panic: savestack overflows I32_MAX");
}
new_max = new_floor;
}
/* Note that we add an additional SS_MAXPUSH slots on top of
* PL_savestack_max so that SS_ADD_END(), SSGROW() etc can do
* a simper check and if necessary realloc *after* apparently
* overwriting the current PL_savestack_max. See scope.h.
*
* The +1 is because new_max/PL_savestack_max is the highest
* index, by Renew needs the number of items, which is one
* larger than the highest index. */
Renew(PL_savestack, new_max + SS_MAXPUSH + 1, ANY);
PL_savestack_max = new_max;
}
#undef GROW
/* The original function was called Perl_tmps_grow and was removed from public
API, Perl_tmps_grow_p is the replacement and it used in public macros but
isn't public itself.
Perl_tmps_grow_p takes a proposed ix. A proposed ix is PL_tmps_ix + extend_by,
where the result of (PL_tmps_ix + extend_by) is >= PL_tmps_max
Upon return, PL_tmps_stack[ix] will be a valid address. For machine code
optimization and register usage reasons, the proposed ix passed into
tmps_grow is returned to the caller which the caller can then use to write
an SV * to PL_tmps_stack[ix]. If the caller was using tmps_grow in
pre-extend mode (EXTEND_MORTAL macro), then it ignores the return value of
tmps_grow. Note, tmps_grow DOES NOT write ix to PL_tmps_ix, the caller
must assign ix or ret val of tmps_grow to PL_temps_ix themselves if that is
appropriate. The assignment to PL_temps_ix can happen before or after
tmps_grow call since tmps_grow doesn't look at PL_tmps_ix.
*/
SSize_t
Perl_tmps_grow_p(pTHX_ SSize_t ix)
{
SSize_t extend_to = ix;
#ifndef STRESS_REALLOC
SSize_t grow_size = PL_tmps_max < 512 ? 128 : PL_tmps_max / 2;
if (extend_to > SSize_t_MAX - grow_size - 1)
/* trigger memwrap message or fail allocation */
extend_to = SSize_t_MAX-1;
else
extend_to += grow_size;
#endif
Renew(PL_tmps_stack, extend_to + 1, SV*);
PL_tmps_max = extend_to + 1;
return ix;
}
void
Perl_free_tmps(pTHX)
{
/* XXX should tmps_floor live in cxstack? */
const SSize_t myfloor = PL_tmps_floor;
while (PL_tmps_ix > myfloor) { /* clean up after last statement */
SV* const sv = PL_tmps_stack[PL_tmps_ix--];
#ifdef PERL_POISON
PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB);
#endif
if (LIKELY(sv)) {
SvTEMP_off(sv);
SvREFCNT_dec_NN(sv); /* note, can modify tmps_ix!!! */
}
}
}
/*
=for apidoc save_scalar_at
A helper function for localizing the SV referenced by C<*sptr>.
If C<SAVEf_KEEPOLDELEM> is set in in C<flags>, the function returns the input
scalar untouched.
Otherwise it replaces C<*sptr> with a new C<undef> scalar, and returns that.
The new scalar will have the old one's magic (if any) copied to it.
If there is such magic, and C<SAVEf_SETMAGIC> is set in in C<flags>, 'set'
magic will be processed on the new scalar. If unset, 'set' magic will be
skipped. The latter typically means that assignment will soon follow (I<e.g.>,
S<C<'local $x = $y'>>), and that will handle the magic.
=for apidoc Amnh ||SAVEf_KEEPOLDELEM
=for apidoc Amnh ||SAVEf_SETMAGIC
=cut
*/
STATIC SV *
S_save_scalar_at(pTHX_ SV **sptr, const U32 flags)
{
SV * osv;
SV *sv;
PERL_ARGS_ASSERT_SAVE_SCALAR_AT;
osv = *sptr;
if (flags & SAVEf_KEEPOLDELEM)
sv = osv;
else {
sv = (*sptr = newSV_type(SVt_NULL));
if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv))
mg_localize(osv, sv, cBOOL(flags & SAVEf_SETMAGIC));
}
return sv;
}
void
Perl_save_pushptrptr(pTHX_ void *const ptr1, void *const ptr2, const int type)
{
dSS_ADD;
SS_ADD_PTR(ptr1);
SS_ADD_PTR(ptr2);
SS_ADD_UV(type);
SS_ADD_END(3);
}
SV *
Perl_save_scalar(pTHX_ GV *gv)
{
SV ** const sptr = &GvSVn(gv);
PERL_ARGS_ASSERT_SAVE_SCALAR;
if (UNLIKELY(SvGMAGICAL(*sptr))) {
PL_localizing = 1;
(void)mg_get(*sptr);
PL_localizing = 0;
}
save_pushptrptr(SvREFCNT_inc_simple(gv), SvREFCNT_inc(*sptr), SAVEt_SV);
return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
}
/*
=for apidoc save_generic_svref
Implements C<SAVEGENERICSV>.
Like save_sptr(), but also SvREFCNT_dec()s the new value. Can be used to
restore a global SV to its prior contents, freeing new value.
=cut
*/
void
Perl_save_generic_svref(pTHX_ SV **sptr)
{
PERL_ARGS_ASSERT_SAVE_GENERIC_SVREF;
save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_GENERIC_SVREF);
}
/*
=for apidoc save_rcpv
Implements C<SAVERCPV>.
Saves and restores a refcounted string, similar to what
save_generic_svref would do for a SV*. Can be used to restore
a refcounted string to its previous state. Performs the
appropriate refcount counting so that nothing should leak
or be prematurely freed.
=cut
*/
void
Perl_save_rcpv(pTHX_ char **prcpv) {
PERL_ARGS_ASSERT_SAVE_RCPV;
save_pushptrptr(prcpv, rcpv_copy(*prcpv), SAVEt_RCPV);
}
/*
=for apidoc save_freercpv
Implements C<SAVEFREERCPV>.
Saves and frees a refcounted string. Calls rcpv_free()
on the argument when the current pseudo block is finished.
=cut
*/
void
Perl_save_freercpv(pTHX_ char *rcpv) {
PERL_ARGS_ASSERT_SAVE_FREERCPV;
save_pushptr(rcpv, SAVEt_FREERCPV);
}
/*
=for apidoc_section $callback
=for apidoc save_generic_pvref
Implements C<SAVEGENERICPV>.
Like save_pptr(), but also Safefree()s the new value if it is different
from the old one. Can be used to restore a global char* to its prior
contents, freeing new value.
=cut
*/
void
Perl_save_generic_pvref(pTHX_ char **str)
{
PERL_ARGS_ASSERT_SAVE_GENERIC_PVREF;
save_pushptrptr(*str, str, SAVEt_GENERIC_PVREF);
}
/*
=for apidoc_section $callback
=for apidoc save_shared_pvref
Implements C<SAVESHAREDPV>.
Like save_generic_pvref(), but uses PerlMemShared_free() rather than Safefree().
Can be used to restore a shared global char* to its prior
contents, freeing new value.
=cut
*/
void
Perl_save_shared_pvref(pTHX_ char **str)
{
PERL_ARGS_ASSERT_SAVE_SHARED_PVREF;
save_pushptrptr(str, *str, SAVEt_SHARED_PVREF);
}
/*
=for apidoc_section $callback
=for apidoc save_set_svflags
Implements C<SAVESETSVFLAGS>.
Set the SvFLAGS specified by mask to the values in val
=cut
*/
void
Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val)
{
dSS_ADD;
PERL_ARGS_ASSERT_SAVE_SET_SVFLAGS;
SS_ADD_PTR(sv);
SS_ADD_INT(mask);
SS_ADD_INT(val);
SS_ADD_UV(SAVEt_SET_SVFLAGS);
SS_ADD_END(4);
}
/*
=for apidoc_section $GV
=for apidoc save_gp
Saves the current GP of gv on the save stack to be restored on scope exit.
If C<empty> is true, replace the GP with a new GP.
If C<empty> is false, mark C<gv> with C<GVf_INTRO> so the next reference
assigned is localized, which is how S<C< local *foo = $someref; >> works.
=cut
*/
void
Perl_save_gp(pTHX_ GV *gv, I32 empty)
{
PERL_ARGS_ASSERT_SAVE_GP;
/* XXX For now, we just upgrade any coderef in the stash to a full GV
during localisation. Maybe at some point we could make localis-
ation work without needing the upgrade. (In which case our
callers should probably call a different function, not save_gp.)
*/
if (!isGV(gv)) {
assert(isGV_or_RVCV(gv));
(void)CvGV(SvRV((SV *)gv)); /* CvGV does the upgrade */
assert(isGV(gv));
}
save_pushptrptr(SvREFCNT_inc(gv), GvGP(gv), SAVEt_GP);
if (empty) {
GP *gp = Perl_newGP(aTHX_ gv);
HV * const stash = GvSTASH(gv);
bool isa_changed = 0;
if (stash && HvHasENAME(stash)) {
if (memEQs(GvNAME(gv), GvNAMELEN(gv), "ISA"))
isa_changed = TRUE;
else if (GvCVu(gv))
/* taking a method out of circulation ("local")*/
mro_method_changed_in(stash);
}
if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) {
gp->gp_io = newIO();
IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START;
}
GvGP_set(gv,gp);
if (isa_changed) mro_isa_changed_in(stash);
}
else {
gp_ref(GvGP(gv));
GvINTRO_on(gv);
}
}
AV *
Perl_save_ary(pTHX_ GV *gv)
{
AV * const oav = GvAVn(gv);
AV *av;
PERL_ARGS_ASSERT_SAVE_ARY;
if (UNLIKELY(!AvREAL(oav) && AvREIFY(oav)))
av_reify(oav);
save_pushptrptr(SvREFCNT_inc_simple_NN(gv), oav, SAVEt_AV);
GvAV(gv) = NULL;
av = GvAVn(gv);
if (UNLIKELY(SvMAGIC(oav)))
mg_localize(MUTABLE_SV(oav), MUTABLE_SV(av), TRUE);
return av;
}
HV *
Perl_save_hash(pTHX_ GV *gv)
{
HV *ohv, *hv;
PERL_ARGS_ASSERT_SAVE_HASH;
save_pushptrptr(
SvREFCNT_inc_simple_NN(gv), (ohv = GvHVn(gv)), SAVEt_HV
);
GvHV(gv) = NULL;
hv = GvHVn(gv);
if (UNLIKELY(SvMAGIC(ohv)))
mg_localize(MUTABLE_SV(ohv), MUTABLE_SV(hv), TRUE);
return hv;
}
void
Perl_save_item(pTHX_ SV *item)
{
SV * const sv = newSVsv(item);
PERL_ARGS_ASSERT_SAVE_ITEM;
save_pushptrptr(item, /* remember the pointer */
sv, /* remember the value */
SAVEt_ITEM);
}
void
Perl_save_bool(pTHX_ bool *boolp)
{
dSS_ADD;
PERL_ARGS_ASSERT_SAVE_BOOL;
SS_ADD_PTR(boolp);
SS_ADD_UV(SAVEt_BOOL | (*boolp << 8));
SS_ADD_END(2);
}
void
Perl_save_pushi32ptr(pTHX_ const I32 i, void *const ptr, const int type)
{
dSS_ADD;
SS_ADD_INT(i);
SS_ADD_PTR(ptr);
SS_ADD_UV(type);
SS_ADD_END(3);
}
void
Perl_save_int(pTHX_ int *intp)
{
const int i = *intp;
UV type = ((UV)((UV)i << SAVE_TIGHT_SHIFT) | SAVEt_INT_SMALL);
int size = 2;
dSS_ADD;
PERL_ARGS_ASSERT_SAVE_INT;
if (UNLIKELY((int)(type >> SAVE_TIGHT_SHIFT) != i)) {
SS_ADD_INT(i);
type = SAVEt_INT;
size++;
}
SS_ADD_PTR(intp);
SS_ADD_UV(type);
SS_ADD_END(size);
}
void
Perl_save_I8(pTHX_ I8 *bytep)
{
dSS_ADD;
PERL_ARGS_ASSERT_SAVE_I8;
SS_ADD_PTR(bytep);
SS_ADD_UV(SAVEt_I8 | ((UV)*bytep << 8));
SS_ADD_END(2);
}
void
Perl_save_I16(pTHX_ I16 *intp)
{
dSS_ADD;
PERL_ARGS_ASSERT_SAVE_I16;
SS_ADD_PTR(intp);
SS_ADD_UV(SAVEt_I16 | ((UV)*intp << 8));
SS_ADD_END(2);
}
void
Perl_save_I32(pTHX_ I32 *intp)
{
const I32 i = *intp;
UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_I32_SMALL);
int size = 2;
dSS_ADD;
PERL_ARGS_ASSERT_SAVE_I32;
if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) {
SS_ADD_INT(i);
type = SAVEt_I32;
size++;
}
SS_ADD_PTR(intp);
SS_ADD_UV(type);
SS_ADD_END(size);
}
void
Perl_save_strlen(pTHX_ STRLEN *ptr)
{
const IV i = *ptr;
UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_STRLEN_SMALL);
int size = 2;
dSS_ADD;
PERL_ARGS_ASSERT_SAVE_STRLEN;
if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) {
SS_ADD_IV(*ptr);
type = SAVEt_STRLEN;
size++;
}
SS_ADD_PTR(ptr);
SS_ADD_UV(type);
SS_ADD_END(size);
}
void
Perl_save_iv(pTHX_ IV *ivp)
{
PERL_ARGS_ASSERT_SAVE_IV;
SSGROW(3);
SSPUSHIV(*ivp);
SSPUSHPTR(ivp);
SSPUSHUV(SAVEt_IV);
}
/* Cannot use save_sptr() to store a char* since the SV** cast will
* force word-alignment and we'll miss the pointer.
*/
void
Perl_save_pptr(pTHX_ char **pptr)
{
PERL_ARGS_ASSERT_SAVE_PPTR;
save_pushptrptr(*pptr, pptr, SAVEt_PPTR);
}
/*
=for apidoc_section $callback
=for apidoc save_vptr
Implements C<SAVEVPTR>.
=cut
*/
void
Perl_save_vptr(pTHX_ void *ptr)
{
PERL_ARGS_ASSERT_SAVE_VPTR;
save_pushptrptr(*(char**)ptr, ptr, SAVEt_VPTR);
}
void
Perl_save_sptr(pTHX_ SV **sptr)
{
PERL_ARGS_ASSERT_SAVE_SPTR;
save_pushptrptr(*sptr, sptr, SAVEt_SPTR);
}
/*
=for apidoc_section $callback
=for apidoc save_padsv_and_mortalize
Implements C<SAVEPADSVANDMORTALIZE>.
=cut
*/
void
Perl_save_padsv_and_mortalize(pTHX_ PADOFFSET off)
{
dSS_ADD;
ASSERT_CURPAD_ACTIVE("save_padsv");
SS_ADD_PTR(SvREFCNT_inc_simple_NN(PL_curpad[off]));
SS_ADD_PTR(PL_comppad);
SS_ADD_UV((UV)off);
SS_ADD_UV(SAVEt_PADSV_AND_MORTALIZE);
SS_ADD_END(4);
}
void
Perl_save_hptr(pTHX_ HV **hptr)
{
PERL_ARGS_ASSERT_SAVE_HPTR;
save_pushptrptr(*hptr, hptr, SAVEt_HPTR);
}
void
Perl_save_aptr(pTHX_ AV **aptr)
{
PERL_ARGS_ASSERT_SAVE_APTR;
save_pushptrptr(*aptr, aptr, SAVEt_APTR);
}
/*
=for apidoc_section $callback
=for apidoc save_pushptr
The refcnt of object C<ptr> will be decremented at the end of the current
I<pseudo-block>. C<type> gives the type of C<ptr>, expressed as one of the
constants in F<scope.h> whose name begins with C<SAVEt_>.
This is the underlying implementation of several macros, like
C<SAVEFREESV>.
=cut
*/
void
Perl_save_pushptr(pTHX_ void *const ptr, const int type)
{
dSS_ADD;
SS_ADD_PTR(ptr);
SS_ADD_UV(type);
SS_ADD_END(2);
}
void
Perl_save_clearsv(pTHX_ SV **svp)
{
const UV offset = svp - PL_curpad;
const UV offset_shifted = offset << SAVE_TIGHT_SHIFT;
PERL_ARGS_ASSERT_SAVE_CLEARSV;
ASSERT_CURPAD_ACTIVE("save_clearsv");
assert(*svp);
SvPADSTALE_off(*svp); /* mark lexical as active */
if (UNLIKELY((offset_shifted >> SAVE_TIGHT_SHIFT) != offset)) {
Perl_croak(aTHX_ "panic: pad offset %" UVuf " out of range (%p-%p)",
offset, svp, PL_curpad);
}
{
dSS_ADD;
SS_ADD_UV(offset_shifted | SAVEt_CLEARSV);
SS_ADD_END(1);
}
}
void
Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
{
PERL_ARGS_ASSERT_SAVE_DELETE;
save_pushptri32ptr(key, klen, SvREFCNT_inc_simple(hv), SAVEt_DELETE);
}
/*
=for apidoc_section $callback
=for apidoc save_hdelete
Implements C<SAVEHDELETE>.
=cut
*/
void
Perl_save_hdelete(pTHX_ HV *hv, SV *keysv)
{
STRLEN len;
I32 klen;
const char *key;
PERL_ARGS_ASSERT_SAVE_HDELETE;
key = SvPV_const(keysv, len);
klen = SvUTF8(keysv) ? -(I32)len : (I32)len;
SvREFCNT_inc_simple_void_NN(hv);
save_pushptri32ptr(savepvn(key, len), klen, hv, SAVEt_DELETE);
}
/*
=for apidoc_section $callback
=for apidoc save_adelete
Implements C<SAVEADELETE>.
=cut
*/
void
Perl_save_adelete(pTHX_ AV *av, SSize_t key)
{
dSS_ADD;
PERL_ARGS_ASSERT_SAVE_ADELETE;
SvREFCNT_inc_void(av);
SS_ADD_UV(key);
SS_ADD_PTR(av);
SS_ADD_IV(SAVEt_ADELETE);
SS_ADD_END(3);
}
void
Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p)
{
dSS_ADD;
PERL_ARGS_ASSERT_SAVE_DESTRUCTOR;
SS_ADD_DPTR(f);
SS_ADD_PTR(p);
SS_ADD_UV(SAVEt_DESTRUCTOR);
SS_ADD_END(3);
}
void
Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p)
{
dSS_ADD;
SS_ADD_DXPTR(f);
SS_ADD_PTR(p);
SS_ADD_UV(SAVEt_DESTRUCTOR_X);
SS_ADD_END(3);
}
/*
=for apidoc_section $callback
=for apidoc save_hints
Implements C<SAVEHINTS>.
=cut
*/
void
Perl_save_hints(pTHX)
{
COPHH *save_cophh = cophh_copy(CopHINTHASH_get(&PL_compiling));
if (PL_hints & HINT_LOCALIZE_HH) {
HV *oldhh = GvHV(PL_hintgv);
{
dSS_ADD;
SS_ADD_INT(PL_hints);
SS_ADD_PTR(save_cophh);
SS_ADD_PTR(oldhh);
SS_ADD_UV(SAVEt_HINTS_HH | (PL_prevailing_version << 8));
SS_ADD_END(4);
}
GvHV(PL_hintgv) = NULL; /* in case copying dies */
GvHV(PL_hintgv) = hv_copy_hints_hv(oldhh);
SAVEFEATUREBITS();
} else {
save_pushi32ptr(PL_hints, save_cophh, SAVEt_HINTS | (PL_prevailing_version << 8));
}
}
static void
S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2,
const int type)
{
dSS_ADD;
SS_ADD_PTR(ptr1);
SS_ADD_INT(i);
SS_ADD_PTR(ptr2);
SS_ADD_UV(type);
SS_ADD_END(4);
}
/*
=for apidoc_section $callback
=for apidoc save_aelem
=for apidoc_item save_aelem_flags
These each arrange for the value of the array element C<av[idx]> to be restored
at the end of the enclosing I<pseudo-block>.
In C<save_aelem>, the SV at C**sptr> will be replaced by a new C<undef>
scalar. That scalar will inherit any magic from the original C<**sptr>,
and any 'set' magic will be processed.
In C<save_aelem_flags>, C<SAVEf_KEEPOLDELEM> being set in C<flags> causes
the function to forgo all that: the scalar at C<**sptr> is untouched.
If C<SAVEf_KEEPOLDELEM> is not set, the SV at C**sptr> will be replaced by a
new C<undef> scalar. That scalar will inherit any magic from the original
C<**sptr>. Any 'set' magic will be processed if and only if C<SAVEf_SETMAGIC>
is set in in C<flags>.
=cut
*/
void
Perl_save_aelem_flags(pTHX_ AV *av, SSize_t idx, SV **sptr,
const U32 flags)
{
dSS_ADD;
SV *sv;
PERL_ARGS_ASSERT_SAVE_AELEM_FLAGS;
SvGETMAGIC(*sptr);
SS_ADD_PTR(SvREFCNT_inc_simple(av));
SS_ADD_IV(idx);
SS_ADD_PTR(SvREFCNT_inc(*sptr));
SS_ADD_UV(SAVEt_AELEM);
SS_ADD_END(4);
/* The array needs to hold a reference count on its new element, so it
must be AvREAL. */
if (UNLIKELY(!AvREAL(av) && AvREIFY(av)))
av_reify(av);
save_scalar_at(sptr, flags); /* XXX - FIXME - see #60360 */
if (flags & SAVEf_KEEPOLDELEM)
return;
sv = *sptr;
/* If we're localizing a tied array element, this new sv
* won't actually be stored in the array - so it won't get
* reaped when the localize ends. Ensure it gets reaped by
* mortifying it instead. DAPM */
if (UNLIKELY(SvTIED_mg((const SV *)av, PERL_MAGIC_tied)))
sv_2mortal(sv);
}
/*
=for apidoc_section $callback
=for apidoc save_helem
=for apidoc_item save_helem_flags
These each arrange for the value of the hash element (in Perlish terms)
C<$hv{key}]> to be restored at the end of the enclosing I<pseudo-block>.
In C<save_helem>, the SV at C**sptr> will be replaced by a new C<undef>
scalar. That scalar will inherit any magic from the original C<**sptr>,