forked from Perl/perl5
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinline.h
4470 lines (3537 loc) · 130 KB
/
inline.h
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
/* inline.h
*
* Copyright (C) 2012 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.
*
* This file contains tables and code adapted from
* https://bjoern.hoehrmann.de/utf-8/decoder/dfa/, which requires this
* copyright notice:
Copyright (c) 2008-2009 Bjoern Hoehrmann <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*
* This file is a home for static inline functions that cannot go in other
* header files, because they depend on proto.h (included after most other
* headers) or struct definitions.
*
* Note also perlstatic.h for functions that can't or shouldn't be inlined, but
* whose details should be exposed to the compiler, for such things as tail
* call optimization.
*
* Each section names the header file that the functions "belong" to.
*/
/* ------------------------------- av.h ------------------------------- */
/*
=for apidoc_section $AV
=for apidoc av_count
Returns the number of elements in the array C<av>. This is the true length of
the array, including any undefined elements. It is always the same as
S<C<av_top_index(av) + 1>>.
=cut
*/
PERL_STATIC_INLINE Size_t
Perl_av_count(pTHX_ AV *av)
{
PERL_ARGS_ASSERT_AV_COUNT;
assert(SvTYPE(av) == SVt_PVAV);
return AvFILL(av) + 1;
}
/* ------------------------------- av.c ------------------------------- */
/*
=for apidoc av_store_simple
This is a cut-down version of av_store that assumes that the array is
very straightforward - no magic, not readonly, and AvREAL - and that
C<key> is not negative. This function MUST NOT be used in situations
where any of those assumptions may not hold.
Stores an SV in an array. The array index is specified as C<key>. It
can be dereferenced to get the C<SV*> that was stored there (= C<val>)).
Note that the caller is responsible for suitably incrementing the reference
count of C<val> before the call.
Approximate Perl equivalent: C<splice(@myarray, $key, 1, $val)>.
=cut
*/
PERL_STATIC_INLINE SV**
Perl_av_store_simple(pTHX_ AV *av, SSize_t key, SV *val)
{
SV** ary;
PERL_ARGS_ASSERT_AV_STORE_SIMPLE;
assert(SvTYPE(av) == SVt_PVAV);
assert(!SvMAGICAL(av));
assert(!SvREADONLY(av));
assert(AvREAL(av));
assert(key > -1);
ary = AvARRAY(av);
if (AvFILLp(av) < key) {
if (key > AvMAX(av)) {
av_extend(av,key);
ary = AvARRAY(av);
}
AvFILLp(av) = key;
} else
SvREFCNT_dec(ary[key]);
ary[key] = val;
return &ary[key];
}
/*
=for apidoc av_fetch_simple
This is a cut-down version of av_fetch that assumes that the array is
very straightforward - no magic, not readonly, and AvREAL - and that
C<key> is not negative. This function MUST NOT be used in situations
where any of those assumptions may not hold.
Returns the SV at the specified index in the array. The C<key> is the
index. If lval is true, you are guaranteed to get a real SV back (in case
it wasn't real before), which you can then modify. Check that the return
value is non-null before dereferencing it to a C<SV*>.
The rough perl equivalent is C<$myarray[$key]>.
=cut
*/
PERL_STATIC_INLINE SV**
Perl_av_fetch_simple(pTHX_ AV *av, SSize_t key, I32 lval)
{
PERL_ARGS_ASSERT_AV_FETCH_SIMPLE;
assert(SvTYPE(av) == SVt_PVAV);
assert(!SvMAGICAL(av));
assert(!SvREADONLY(av));
assert(AvREAL(av));
assert(key > -1);
if ( (key > AvFILLp(av)) || !AvARRAY(av)[key]) {
return lval ? av_store_simple(av,key,newSV_type(SVt_NULL)) : NULL;
} else {
return &AvARRAY(av)[key];
}
}
PERL_STATIC_INLINE void
Perl_av_push_simple(pTHX_ AV *av, SV *val)
{
PERL_ARGS_ASSERT_AV_PUSH_SIMPLE;
assert(SvTYPE(av) == SVt_PVAV);
assert(!SvMAGICAL(av));
assert(!SvREADONLY(av));
assert(AvREAL(av));
assert(AvFILLp(av) > -2);
(void)av_store_simple(av,AvFILLp(av)+1,val);
}
/*
=for apidoc av_new_alloc
This implements L<perlapi/C<newAV_alloc_x>>
and L<perlapi/C<newAV_alloc_xz>>, which are the public API for this
functionality.
Creates a new AV and allocates its SV* array.
This is similar to, but more efficient than doing:
AV *av = newAV();
av_extend(av, key);
The size parameter is used to pre-allocate a SV* array large enough to
hold at least elements C<0..(size-1)>. C<size> must be at least 1.
The C<zeroflag> parameter controls whether or not the array is NULL
initialized.
=cut
*/
PERL_STATIC_INLINE AV *
Perl_av_new_alloc(pTHX_ SSize_t size, bool zeroflag)
{
AV * const av = newAV();
SV** ary;
PERL_ARGS_ASSERT_AV_NEW_ALLOC;
assert(size > 0);
Newx(ary, size, SV*); /* Newx performs the memwrap check */
AvALLOC(av) = ary;
AvARRAY(av) = ary;
AvMAX(av) = size - 1;
if (zeroflag)
Zero(ary, size, SV*);
return av;
}
/* remove (AvARRAY(av) - AvALLOC(av)) offset from empty array */
PERL_STATIC_INLINE void
Perl_av_remove_offset(pTHX_ AV *av)
{
PERL_ARGS_ASSERT_AV_REMOVE_OFFSET;
assert(AvFILLp(av) == -1);
SSize_t i = AvARRAY(av) - AvALLOC(av);
if (i) {
AvARRAY(av) = AvALLOC(av);
AvMAX(av) += i;
#ifdef PERL_RC_STACK
Zero(AvALLOC(av), i, SV*);
#endif
}
}
/* ------------------------------- cv.h ------------------------------- */
/*
=for apidoc_section $CV
=for apidoc CvGV
Returns the GV associated with the CV C<sv>, reifying it if necessary.
=cut
*/
PERL_STATIC_INLINE GV *
Perl_CvGV(pTHX_ CV *sv)
{
PERL_ARGS_ASSERT_CVGV;
return CvNAMED(sv)
? Perl_cvgv_from_hek(aTHX_ sv)
: ((XPVCV*)MUTABLE_PTR(SvANY(sv)))->xcv_gv_u.xcv_gv;
}
/*
=for apidoc CvDEPTH
Returns the recursion level of the CV C<sv>. Hence >= 2 indicates we are in a
recursive call.
=cut
*/
PERL_STATIC_INLINE I32 *
Perl_CvDEPTH(const CV * const sv)
{
PERL_ARGS_ASSERT_CVDEPTH;
assert(SvTYPE(sv) == SVt_PVCV || SvTYPE(sv) == SVt_PVFM);
return &((XPVCV*)SvANY(sv))->xcv_depth;
}
/*
CvPROTO returns the prototype as stored, which is not necessarily what
the interpreter should be using. Specifically, the interpreter assumes
that spaces have been stripped, which has been the case if the prototype
was added by toke.c, but is generally not the case if it was added elsewhere.
Since we can't enforce the spacelessness at assignment time, this routine
provides a temporary copy at parse time with spaces removed.
I<orig> is the start of the original buffer, I<len> is the length of the
prototype and will be updated when this returns.
*/
#ifdef PERL_CORE
PERL_STATIC_INLINE char *
S_strip_spaces(pTHX_ const char * orig, STRLEN * const len)
{
SV * tmpsv;
char * tmps;
tmpsv = newSVpvn_flags(orig, *len, SVs_TEMP);
tmps = SvPVX(tmpsv);
while ((*len)--) {
if (!isSPACE(*orig))
*tmps++ = *orig;
orig++;
}
*tmps = '\0';
*len = tmps - SvPVX(tmpsv);
return SvPVX(tmpsv);
}
#endif
/* ------------------------------- iperlsys.h ------------------------------- */
#if ! defined(PERL_IMPLICIT_SYS) && defined(USE_ITHREADS)
/* Otherwise this function is implemented as macros in iperlsys.h */
PERL_STATIC_INLINE bool
S_PerlEnv_putenv(pTHX_ char * str)
{
PERL_ARGS_ASSERT_PERLENV_PUTENV;
ENV_LOCK;
bool retval = putenv(str);
ENV_UNLOCK;
return retval;
}
#endif
/* ------------------------------- mg.h ------------------------------- */
#if defined(PERL_CORE) || defined(PERL_EXT)
/* assumes get-magic and stringification have already occurred */
PERL_STATIC_INLINE STRLEN
S_MgBYTEPOS(pTHX_ MAGIC *mg, SV *sv, const char *s, STRLEN len)
{
assert(mg->mg_type == PERL_MAGIC_regex_global);
assert(mg->mg_len != -1);
if (mg->mg_flags & MGf_BYTES || !DO_UTF8(sv))
return (STRLEN)mg->mg_len;
else {
const STRLEN pos = (STRLEN)mg->mg_len;
/* Without this check, we may read past the end of the buffer: */
if (pos > sv_or_pv_len_utf8(sv, s, len)) return len+1;
return sv_or_pv_pos_u2b(sv, s, pos, NULL);
}
}
#endif
/* ------------------------------- pad.h ------------------------------ */
#if defined(PERL_IN_PAD_C) || defined(PERL_IN_OP_C)
PERL_STATIC_INLINE bool
S_PadnameIN_SCOPE(const PADNAME * const pn, const U32 seq)
{
PERL_ARGS_ASSERT_PADNAMEIN_SCOPE;
/* is seq within the range _LOW to _HIGH ?
* This is complicated by the fact that PL_cop_seqmax
* may have wrapped around at some point */
if (COP_SEQ_RANGE_LOW(pn) == PERL_PADSEQ_INTRO)
return FALSE; /* not yet introduced */
if (COP_SEQ_RANGE_HIGH(pn) == PERL_PADSEQ_INTRO) {
/* in compiling scope */
if (
(seq > COP_SEQ_RANGE_LOW(pn))
? (seq - COP_SEQ_RANGE_LOW(pn) < (U32_MAX >> 1))
: (COP_SEQ_RANGE_LOW(pn) - seq > (U32_MAX >> 1))
)
return TRUE;
}
else if (
(COP_SEQ_RANGE_LOW(pn) > COP_SEQ_RANGE_HIGH(pn))
?
( seq > COP_SEQ_RANGE_LOW(pn)
|| seq <= COP_SEQ_RANGE_HIGH(pn))
: ( seq > COP_SEQ_RANGE_LOW(pn)
&& seq <= COP_SEQ_RANGE_HIGH(pn))
)
return TRUE;
return FALSE;
}
#endif
/* ------------------------------- pp.h ------------------------------- */
PERL_STATIC_INLINE Stack_off_t
Perl_TOPMARK(pTHX)
{
DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
"MARK top %p %" IVdf "\n",
PL_markstack_ptr,
(IV)*PL_markstack_ptr)));
return *PL_markstack_ptr;
}
PERL_STATIC_INLINE Stack_off_t
Perl_POPMARK(pTHX)
{
DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
"MARK pop %p %" IVdf "\n",
(PL_markstack_ptr-1),
(IV)*(PL_markstack_ptr-1))));
assert((PL_markstack_ptr > PL_markstack) || !"MARK underflow");
return *PL_markstack_ptr--;
}
/*
=for apidoc_section $rpp
=for apidoc rpp_extend
Ensures that there is space on the stack to push C<n> items, extending it
if necessary.
=cut
*/
PERL_STATIC_INLINE void
Perl_rpp_extend(pTHX_ SSize_t n)
{
PERL_ARGS_ASSERT_RPP_EXTEND;
EXTEND_HWM_SET(PL_stack_sp, n);
#ifndef STRESS_REALLOC
if (UNLIKELY(_EXTEND_NEEDS_GROW(PL_stack_sp, n)))
#endif
{
(void)stack_grow(PL_stack_sp, PL_stack_sp, n);
}
}
/*
=for apidoc rpp_popfree_to
Pop and free all items on the argument stack above C<sp>. On return,
C<PL_stack_sp> will be equal to C<sp>.
=cut
*/
PERL_STATIC_INLINE void
Perl_rpp_popfree_to(pTHX_ SV **sp)
{
PERL_ARGS_ASSERT_RPP_POPFREE_TO;
assert(sp <= PL_stack_sp);
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
while (PL_stack_sp > sp) {
SV *sv = *PL_stack_sp--;
SvREFCNT_dec(sv);
}
#else
PL_stack_sp = sp;
#endif
}
/*
=for apidoc rpp_popfree_to_NN
A variant of rpp_popfree_to() which assumes that all the pointers being
popped off the stack are non-NULL.
=cut
*/
PERL_STATIC_INLINE void
Perl_rpp_popfree_to_NN(pTHX_ SV **sp)
{
PERL_ARGS_ASSERT_RPP_POPFREE_TO_NN;
assert(sp <= PL_stack_sp);
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
while (PL_stack_sp > sp) {
SV *sv = *PL_stack_sp--;
assert(sv);
SvREFCNT_dec_NN(sv);
}
#else
PL_stack_sp = sp;
#endif
}
/*
=for apidoc rpp_popfree_1
Pop and free the top item on the argument stack and update C<PL_stack_sp>.
=cut
*/
PERL_STATIC_INLINE void
Perl_rpp_popfree_1(pTHX)
{
PERL_ARGS_ASSERT_RPP_POPFREE_1;
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
SV *sv = *PL_stack_sp--;
SvREFCNT_dec(sv);
#else
PL_stack_sp--;
#endif
}
/*
=for apidoc rpp_popfree_1_NN
A variant of rpp_popfree_1() which assumes that the pointer being popped
off the stack is non-NULL.
=cut
*/
PERL_STATIC_INLINE void
Perl_rpp_popfree_1_NN(pTHX)
{
PERL_ARGS_ASSERT_RPP_POPFREE_1_NN;
assert(*PL_stack_sp);
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
SV *sv = *PL_stack_sp--;
SvREFCNT_dec_NN(sv);
#else
PL_stack_sp--;
#endif
}
/*
=for apidoc rpp_popfree_2
Pop and free the top two items on the argument stack and update
C<PL_stack_sp>.
=cut
*/
PERL_STATIC_INLINE void
Perl_rpp_popfree_2(pTHX)
{
PERL_ARGS_ASSERT_RPP_POPFREE_2;
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
for (int i = 0; i < 2; i++) {
SV *sv = *PL_stack_sp--;
SvREFCNT_dec(sv);
}
#else
PL_stack_sp -= 2;
#endif
}
/*
=for apidoc rpp_popfree_2_NN
A variant of rpp_popfree_2() which assumes that the two pointers being
popped off the stack are non-NULL.
=cut
*/
PERL_STATIC_INLINE void
Perl_rpp_popfree_2_NN(pTHX)
{
PERL_ARGS_ASSERT_RPP_POPFREE_2_NN;
#ifdef PERL_RC_STACK
SV *sv2 = *PL_stack_sp--;
assert(sv2);
SV *sv1 = *PL_stack_sp;
assert(sv1);
assert(rpp_stack_is_rc());
U32 rc1 = SvREFCNT(sv1);
U32 rc2 = SvREFCNT(sv2);
/* This expression is intended to be true if either of rc1 or rc2 has
* the value 0 or 1, but using only a single branch test, rather
* than the two branches that a compiler would plant for a boolean
* expression. We are working on the assumption that, most of the
* time, neither of the args to a binary function will need to be
* freed - they're likely to lex vars, or PADTMPs or whatever.
* So give the CPU a single branch that is rarely taken. */
if (UNLIKELY( !(rc1>>1) + !(rc2>>1) ))
/* at least one of the old SVs needs freeing. Do it the long way */
Perl_rpp_free_2_(aTHX_ sv1, sv2, rc1, rc2);
else {
SvREFCNT(sv1) = rc1 - 1;
SvREFCNT(sv2) = rc2 - 1;
}
PL_stack_sp--;
#else
PL_stack_sp -= 2;
#endif
}
/*
=for apidoc rpp_pop_1_norc
Pop and return the top item off the argument stack and update
C<PL_stack_sp>. It's similar to rpp_popfree_1(), except that it actually
returns a value, and it I<doesn't> decrement the SV's reference count.
On non-C<PERL_RC_STACK> builds it actually increments the SV's reference
count.
This is useful in cases where the popped value is immediately embedded
somewhere e.g. via av_store(), allowing you skip decrementing and then
immediately incrementing the reference count again (and risk prematurely
freeing the SV if it had a RC of 1). On non-RC builds, the reference count
bookkeeping still works too, which is why it should be used rather than
a simple C<*PL_stack_sp-->.
=cut
*/
PERL_STATIC_INLINE SV*
Perl_rpp_pop_1_norc(pTHX)
{
PERL_ARGS_ASSERT_RPP_POP_1_NORC
SV *sv = *PL_stack_sp--;
#ifndef PERL_RC_STACK
SvREFCNT_inc(sv);
#else
assert(rpp_stack_is_rc());
#endif
return sv;
}
/*
=for apidoc rpp_push_1
=for apidoc_item rpp_push_IMM
=for apidoc_item rpp_push_2
=for apidoc_item rpp_xpush_1
=for apidoc_item rpp_xpush_IMM
=for apidoc_item rpp_xpush_2
Push one or two SVs onto the stack, incrementing their reference counts
and updating C<PL_stack_sp>. With the C<x> variants, it extends the stack
first. The C<IMM> variants assume that the single argument is an immortal
such as <&PL_sv_undef> and, for efficiency, will skip incrementing its
reference count.
=cut
*/
PERL_STATIC_INLINE void
Perl_rpp_push_1(pTHX_ SV *sv)
{
PERL_ARGS_ASSERT_RPP_PUSH_1;
*++PL_stack_sp = sv;
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
SvREFCNT_inc_simple_void_NN(sv);
#endif
}
PERL_STATIC_INLINE void
Perl_rpp_push_IMM(pTHX_ SV *sv)
{
PERL_ARGS_ASSERT_RPP_PUSH_IMM;
assert(SvIMMORTAL(sv));
*++PL_stack_sp = sv;
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
#endif
}
PERL_STATIC_INLINE void
Perl_rpp_push_2(pTHX_ SV *sv1, SV *sv2)
{
PERL_ARGS_ASSERT_RPP_PUSH_2;
*++PL_stack_sp = sv1;
*++PL_stack_sp = sv2;
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
SvREFCNT_inc_simple_void_NN(sv1);
SvREFCNT_inc_simple_void_NN(sv2);
#endif
}
PERL_STATIC_INLINE void
Perl_rpp_xpush_1(pTHX_ SV *sv)
{
PERL_ARGS_ASSERT_RPP_XPUSH_1;
rpp_extend(1);
rpp_push_1(sv);
}
PERL_STATIC_INLINE void
Perl_rpp_xpush_IMM(pTHX_ SV *sv)
{
PERL_ARGS_ASSERT_RPP_XPUSH_IMM;
rpp_extend(1);
rpp_push_IMM(sv);
}
PERL_STATIC_INLINE void
Perl_rpp_xpush_2(pTHX_ SV *sv1, SV *sv2)
{
PERL_ARGS_ASSERT_RPP_XPUSH_2;
rpp_extend(2);
rpp_push_2(sv1, sv2);
}
/*
=for apidoc rpp_push_1_norc
Push C<sv> onto the stack without incrementing its reference count, and
update C<PL_stack_sp>. On non-PERL_RC_STACK builds, mortalise too.
This is most useful where an SV has just been created and already has a
reference count of 1, but has not yet been anchored anywhere.
=cut
*/
PERL_STATIC_INLINE void
Perl_rpp_push_1_norc(pTHX_ SV *sv)
{
PERL_ARGS_ASSERT_RPP_PUSH_1;
*++PL_stack_sp = sv;
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
#else
sv_2mortal(sv);
#endif
}
/*
=for apidoc rpp_replace_1_1
=for apidoc_item rpp_replace_1_1_NN
=for apidoc_item rpp_replace_1_IMM_NN
Replace the current top stack item with C<sv>, while suitably adjusting
reference counts. Equivalent to rpp_popfree_1(); rpp_push_1(sv), but
is more efficient and handles both SVs being the same.
The C<_NN> variant assumes that the pointer on the stack to the SV being
freed is non-NULL.
The C<IMM_NN> variant is like the C<_NN> variant, but in addition, assumes
that the single argument is an immortal such as <&PL_sv_undef> and, for
efficiency, will skip incrementing its reference count.
=cut
*/
PERL_STATIC_INLINE void
Perl_rpp_replace_1_1(pTHX_ SV *sv)
{
PERL_ARGS_ASSERT_RPP_REPLACE_1_1;
assert(sv);
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
SV *oldsv = *PL_stack_sp;
*PL_stack_sp = sv;
SvREFCNT_inc_simple_void_NN(sv);
SvREFCNT_dec(oldsv);
#else
*PL_stack_sp = sv;
#endif
}
PERL_STATIC_INLINE void
Perl_rpp_replace_1_1_NN(pTHX_ SV *sv)
{
PERL_ARGS_ASSERT_RPP_REPLACE_1_1_NN;
assert(sv);
assert(*PL_stack_sp);
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
SV *oldsv = *PL_stack_sp;
*PL_stack_sp = sv;
SvREFCNT_inc_simple_void_NN(sv);
SvREFCNT_dec_NN(oldsv);
#else
*PL_stack_sp = sv;
#endif
}
PERL_STATIC_INLINE void
Perl_rpp_replace_1_IMM_NN(pTHX_ SV *sv)
{
PERL_ARGS_ASSERT_RPP_REPLACE_1_IMM_NN;
assert(sv);
assert(SvIMMORTAL(sv));
assert(*PL_stack_sp);
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
SV *oldsv = *PL_stack_sp;
*PL_stack_sp = sv;
SvREFCNT_dec_NN(oldsv);
#else
*PL_stack_sp = sv;
#endif
}
/*
=for apidoc rpp_replace_2_1
=for apidoc_item rpp_replace_2_1_NN
=for apidoc_item rpp_replace_2_IMM_NN
Replace the current top to stacks item with C<sv>, while suitably
adjusting reference counts. Equivalent to rpp_popfree_2(); rpp_push_1(sv),
but is more efficient and handles SVs being the same.
The C<_NN> variant assumes that the pointers on the stack to the SVs being
freed are non-NULL.
The C<IMM_NN> variant is like the C<_NN> variant, but in addition, assumes
that the single argument is an immortal such as <&PL_sv_undef> and, for
efficiency, will skip incrementing its reference count.
=cut
*/
PERL_STATIC_INLINE void
Perl_rpp_replace_2_1(pTHX_ SV *sv)
{
PERL_ARGS_ASSERT_RPP_REPLACE_2_1;
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
/* replace PL_stack_sp[-1] first; leave PL_stack_sp[0] in place while
* we free [-1], so if an exception occurs, [0] will still be freed.
*/
SV *oldsv = PL_stack_sp[-1];
PL_stack_sp[-1] = sv;
SvREFCNT_inc_simple_void_NN(sv);
SvREFCNT_dec(oldsv);
oldsv = *PL_stack_sp--;
SvREFCNT_dec(oldsv);
#else
*--PL_stack_sp = sv;
#endif
}
/* Private helper function for _NN and _IMM_NN variants.
* Assumes sv has already had its ref count incremented,
* ready for being put on the stack.
* Intended to be small and fast, since it's inlined into many hot parts of
* code.
*/
PERL_STATIC_INLINE void
Perl_rpp_replace_2_1_COMMON(pTHX_ SV *sv)
{
assert(sv);
#ifdef PERL_RC_STACK
SV *sv2 = *PL_stack_sp--;
assert(sv2);
SV *sv1 = *PL_stack_sp;
assert(sv1);
*PL_stack_sp = sv;
assert(rpp_stack_is_rc());
U32 rc1 = SvREFCNT(sv1);
U32 rc2 = SvREFCNT(sv2);
/* This expression is intended to be true if either of rc1 or rc2 has
* the value 0 or 1, but using only a single branch test, rather
* than the two branches that a compiler would plant for a boolean
* expression. We are working on the assumption that, most of the
* time, neither of the args to a binary function will need to be
* freed - they're likely to lex vars, or PADTMPs or whatever.
* So give the CPU a single branch that is rarely taken. */
if (UNLIKELY( !(rc1>>1) + !(rc2>>1) ))
/* at least one of the old SVs needs freeing. Do it the long way */
Perl_rpp_free_2_(aTHX_ sv1, sv2, rc1, rc2);
else {
SvREFCNT(sv1) = rc1 - 1;
SvREFCNT(sv2) = rc2 - 1;
}
#else
*--PL_stack_sp = sv;
#endif
}
PERL_STATIC_INLINE void
Perl_rpp_replace_2_1_NN(pTHX_ SV *sv)
{
PERL_ARGS_ASSERT_RPP_REPLACE_2_1_NN;
assert(sv);
#ifdef PERL_RC_STACK
SvREFCNT_inc_simple_void_NN(sv);
#endif
rpp_replace_2_1_COMMON(sv);
}
PERL_STATIC_INLINE void
Perl_rpp_replace_2_IMM_NN(pTHX_ SV *sv)
{
PERL_ARGS_ASSERT_RPP_REPLACE_2_IMM_NN;
assert(sv);
assert(SvIMMORTAL(sv));
rpp_replace_2_1_COMMON(sv);
}
/*
=for apidoc rpp_replace_at
Replace the SV at address sp within the stack with C<sv>, while suitably
adjusting reference counts. Equivalent to C<*sp = sv>, except with proper
reference count handling.
=cut
*/
PERL_STATIC_INLINE void
Perl_rpp_replace_at(pTHX_ SV **sp, SV *sv)
{
PERL_ARGS_ASSERT_RPP_REPLACE_AT;
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
SV *oldsv = *sp;
*sp = sv;
SvREFCNT_inc_simple_void_NN(sv);
SvREFCNT_dec(oldsv);
#else
*sp = sv;
#endif
}
/*
=for apidoc rpp_replace_at_NN
A variant of rpp_replace_at() which assumes that the SV pointer on the
stack is non-NULL.
=cut
*/
PERL_STATIC_INLINE void
Perl_rpp_replace_at_NN(pTHX_ SV **sp, SV *sv)
{
PERL_ARGS_ASSERT_RPP_REPLACE_AT_NN;
assert(sv);
assert(*sp);
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
SV *oldsv = *sp;
*sp = sv;
SvREFCNT_inc_simple_void_NN(sv);
SvREFCNT_dec_NN(oldsv);
#else
*sp = sv;
#endif
}
/*
=for apidoc rpp_replace_at_norc
Replace the SV at address sp within the stack with C<sv>, while suitably
adjusting the reference count of the old SV. Equivalent to C<*sp = sv>,
except with proper reference count handling.
C<sv>'s reference count doesn't get incremented. On non-C<PERL_RC_STACK>
builds, it gets mortalised too.
This is most useful where an SV has just been created and already has a
reference count of 1, but has not yet been anchored anywhere.
=cut
*/
PERL_STATIC_INLINE void
Perl_rpp_replace_at_norc(pTHX_ SV **sp, SV *sv)
{
PERL_ARGS_ASSERT_RPP_REPLACE_AT_NORC;
#ifdef PERL_RC_STACK
assert(rpp_stack_is_rc());
SV *oldsv = *sp;
*sp = sv;
SvREFCNT_dec(oldsv);
#else
*sp = sv;
sv_2mortal(sv);
#endif
}
/*
=for apidoc rpp_replace_at_norc_NN