-
Notifications
You must be signed in to change notification settings - Fork 561
/
Copy pathsv.h
2845 lines (2330 loc) · 109 KB
/
sv.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
/* sv.h
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
* 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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.
*
*/
#ifdef sv_flags
#undef sv_flags /* Convex has this in <signal.h> for sigvec() */
#endif
/*
=for apidoc_section $SV_flags
=for apidoc Ay||svtype
An enum of flags for Perl types. These are found in the file F<sv.h>
in the C<svtype> enum. Test these flags with the C<SvTYPE> macro.
The types are:
SVt_NULL
SVt_IV
SVt_NV
SVt_RV
SVt_PV
SVt_PVIV
SVt_PVNV
SVt_PVMG
SVt_INVLIST
SVt_REGEXP
SVt_PVGV
SVt_PVLV
SVt_PVAV
SVt_PVHV
SVt_PVCV
SVt_PVFM
SVt_PVIO
SVt_PVOBJ
These are most easily explained from the bottom up.
C<SVt_PVOBJ> is for object instances of the new `use feature 'class'` kind.
C<SVt_PVIO> is for I/O objects, C<SVt_PVFM> for formats, C<SVt_PVCV> for
subroutines, C<SVt_PVHV> for hashes and C<SVt_PVAV> for arrays.
All the others are scalar types, that is, things that can be bound to a
C<$> variable. For these, the internal types are mostly orthogonal to
types in the Perl language.
Hence, checking C<< SvTYPE(sv) < SVt_PVAV >> is the best way to see whether
something is a scalar.
C<SVt_PVGV> represents a typeglob. If C<!SvFAKE(sv)>, then it is a real,
incoercible typeglob. If C<SvFAKE(sv)>, then it is a scalar to which a
typeglob has been assigned. Assigning to it again will stop it from being
a typeglob. C<SVt_PVLV> represents a scalar that delegates to another scalar
behind the scenes. It is used, e.g., for the return value of C<substr> and
for tied hash and array elements. It can hold any scalar value, including
a typeglob. C<SVt_REGEXP> is for regular
expressions. C<SVt_INVLIST> is for Perl
core internal use only.
C<SVt_PVMG> represents a "normal" scalar (not a typeglob, regular expression,
or delegate). Since most scalars do not need all the internal fields of a
PVMG, we save memory by allocating smaller structs when possible. All the
other types are just simpler forms of C<SVt_PVMG>, with fewer internal fields.
C<SVt_NULL> can only hold undef. C<SVt_IV> can hold undef, an integer, or a
reference. (C<SVt_RV> is an alias for C<SVt_IV>, which exists for backward
compatibility.) C<SVt_NV> can hold undef or a double. (In builds that support
headless NVs, these could also hold a reference via a suitable offset, in the
same way that SVt_IV does, but this is not currently supported and seems to
be a rare use case.) C<SVt_PV> can hold C<undef>, a string, or a reference.
C<SVt_PVIV> is a superset of C<SVt_PV> and C<SVt_IV>. C<SVt_PVNV> is a
superset of C<SVt_PV> and C<SVt_NV>. C<SVt_PVMG> can hold anything C<SVt_PVNV>
can hold, but it may also be blessed or magical.
=for apidoc AmnU||SVt_NULL
Type flag for scalars. See L</svtype>.
=for apidoc AmnU||SVt_IV
Type flag for scalars. See L</svtype>.
=for apidoc AmnU||SVt_NV
Type flag for scalars. See L</svtype>.
=for apidoc AmnU||SVt_PV
Type flag for scalars. See L</svtype>.
=for apidoc AmnU||SVt_PVIV
Type flag for scalars. See L</svtype>.
=for apidoc AmnU||SVt_PVNV
Type flag for scalars. See L</svtype>.
=for apidoc AmnU||SVt_PVMG
Type flag for scalars. See L</svtype>.
=for apidoc CmnU||SVt_INVLIST
Type flag for scalars. See L<perlapi/svtype>.
=for apidoc AmnU||SVt_REGEXP
Type flag for regular expressions. See L</svtype>.
=for apidoc AmnU||SVt_PVGV
Type flag for typeglobs. See L</svtype>.
=for apidoc AmnU||SVt_PVLV
Type flag for scalars. See L</svtype>.
=for apidoc AmnU||SVt_PVAV
Type flag for arrays. See L</svtype>.
=for apidoc AmnU||SVt_PVHV
Type flag for hashes. See L</svtype>.
=for apidoc AmnU||SVt_PVCV
Type flag for subroutines. See L</svtype>.
=for apidoc AmnU||SVt_PVFM
Type flag for formats. See L</svtype>.
=for apidoc AmnU||SVt_PVIO
Type flag for I/O objects. See L</svtype>.
=for apidoc AmnUx||SVt_PVOBJ
Type flag for object instances. See L</svtype>.
=cut
These are ordered so that the simpler types have a lower value; SvUPGRADE
doesn't allow you to upgrade from a higher numbered type to a lower numbered
one; also there is code that assumes that anything that has as a PV component
has a type numbered >= SVt_PV.
*/
typedef enum {
SVt_NULL, /* 0 */
/* BIND was here, before INVLIST replaced it. */
SVt_IV, /* 1 */
SVt_NV, /* 2 */
/* RV was here, before it was merged with IV. */
SVt_PV, /* 3 */
SVt_INVLIST, /* 4, implemented as a PV */
SVt_PVIV, /* 5 */
SVt_PVNV, /* 6 */
SVt_PVMG, /* 7 */
SVt_REGEXP, /* 8 */
/* PVBM was here, before BIND replaced it. */
SVt_PVGV, /* 9 */
SVt_PVLV, /* 10 */
SVt_PVAV, /* 11 */
SVt_PVHV, /* 12 */
SVt_PVCV, /* 13 */
SVt_PVFM, /* 14 */
SVt_PVIO, /* 15 */
SVt_PVOBJ, /* 16 */
/* 17-31: Unused, though one should be reserved for a
* freed sv, if the other 3 bits below the flags ones
* get allocated */
SVt_LAST /* keep last in enum. used to size arrays */
} svtype;
/* *** any alterations to the SV types above need to be reflected in
* SVt_MASK and the various PL_valid_types_* tables. As of this writing those
* tables are in perl.h. There are also two affected names tables in dump.c,
* one in B.xs, and 'bodies_by_type[]' in sv_inline.h.
*
* The bits that match 0xe0 are CURRENTLY UNUSED
* The bits above that are for flags, like SVf_IOK */
#define SVt_MASK 0x1f /* smallest bitmask that covers all types */
#ifndef PERL_CORE
/* Fast Boyer Moore tables are now stored in magic attached to PVMGs */
# define SVt_PVBM SVt_PVMG
/* Anything wanting to create a reference from clean should ensure that it has
a scalar of type SVt_IV now: */
# define SVt_RV SVt_IV
#endif
/* The array of arena roots for SV bodies is indexed by SvTYPE. SVt_NULL doesn't
* use a body, so that arena root is re-used for HEs. SVt_IV also doesn't, so
* that arena root is used for HVs with struct xpvhv_aux. */
#if defined(PERL_IN_HV_C) || defined(PERL_IN_XS_APITEST)
# define HE_ARENA_ROOT_IX SVt_NULL
#endif
#if defined(PERL_IN_HV_C) || defined(PERL_IN_SV_C)
# define HVAUX_ARENA_ROOT_IX SVt_IV
#endif
#ifdef PERL_IN_SV_C
# define SVt_FIRST SVt_NULL /* the type of SV that new_SV() in sv_inline.h returns */
#endif
#define PERL_ARENA_ROOTS_SIZE (SVt_LAST)
/* typedefs to eliminate some typing */
typedef struct he HE;
typedef struct hek HEK;
/* Using C's structural equivalence to help emulate C++ inheritance here... */
/* start with 2 sv-head building blocks */
#define _SV_HEAD(ptrtype) \
ptrtype sv_any; /* pointer to body */ \
U32 sv_refcnt; /* how many references to us */ \
U32 sv_flags /* what we are */
#if NVSIZE <= IVSIZE
# define _NV_BODYLESS_UNION NV svu_nv;
#else
# define _NV_BODYLESS_UNION
#endif
#define _SV_HEAD_UNION \
union { \
char* svu_pv; /* pointer to malloced string */ \
IV svu_iv; \
UV svu_uv; \
_NV_BODYLESS_UNION \
SV* svu_rv; /* pointer to another SV */ \
SV** svu_array; \
HE** svu_hash; \
GP* svu_gp; \
PerlIO *svu_fp; \
} sv_u \
_SV_HEAD_DEBUG
#ifdef DEBUG_LEAKING_SCALARS
#define _SV_HEAD_DEBUG ;\
PERL_BITFIELD32 sv_debug_optype:9; /* the type of OP that allocated us */ \
PERL_BITFIELD32 sv_debug_inpad:1; /* was allocated in a pad for an OP */ \
PERL_BITFIELD32 sv_debug_line:16; /* the line where we were allocated */ \
UV sv_debug_serial; /* serial number of sv allocation */ \
char * sv_debug_file; /* the file where we were allocated */ \
SV * sv_debug_parent /* what we were cloned from (ithreads)*/
#else
#define _SV_HEAD_DEBUG
#endif
struct STRUCT_SV { /* struct sv { */
_SV_HEAD(void*);
_SV_HEAD_UNION;
};
struct gv {
_SV_HEAD(XPVGV*); /* pointer to xpvgv body */
_SV_HEAD_UNION;
};
struct cv {
_SV_HEAD(XPVCV*); /* pointer to xpvcv body */
_SV_HEAD_UNION;
};
struct av {
_SV_HEAD(XPVAV*); /* pointer to xpvav body */
_SV_HEAD_UNION;
};
struct hv {
_SV_HEAD(XPVHV*); /* pointer to xpvhv body */
_SV_HEAD_UNION;
};
struct io {
_SV_HEAD(XPVIO*); /* pointer to xpvio body */
_SV_HEAD_UNION;
};
struct p5rx {
_SV_HEAD(struct regexp*); /* pointer to regexp body */
_SV_HEAD_UNION;
};
struct invlist {
_SV_HEAD(XINVLIST*); /* pointer to xpvinvlist body */
_SV_HEAD_UNION;
};
struct object {
_SV_HEAD(XPVOBJ*); /* pointer to xobject body */
_SV_HEAD_UNION;
};
#undef _SV_HEAD
#undef _SV_HEAD_UNION /* ensure no pollution */
/*
=for apidoc_section $SV
=for apidoc Am|U32|SvREFCNT|SV* sv
Returns the value of the object's reference count. Exposed
to perl code via Internals::SvREFCNT().
=for apidoc SvREFCNT_inc
=for apidoc_item SvREFCNT_inc_NN
=for apidoc_item SvREFCNT_inc_simple
=for apidoc_item SvREFCNT_inc_simple_NN
=for apidoc_item SvREFCNT_inc_simple_void
=for apidoc_item SvREFCNT_inc_simple_void_NN
=for apidoc_item SvREFCNT_inc_void
=for apidoc_item SvREFCNT_inc_void_NN
These all increment the reference count of the given SV.
The ones without C<void> in their names return the SV.
C<SvREFCNT_inc> is the base operation; the rest are optimizations if various
input constraints are known to be true; hence, all can be replaced with
C<SvREFCNT_inc>.
C<SvREFCNT_inc_NN> can only be used if you know C<sv> is not C<NULL>. Since we
don't have to check the NULLness, it's faster and smaller.
C<SvREFCNT_inc_void> can only be used if you don't need the
return value. The macro doesn't need to return a meaningful value.
C<SvREFCNT_inc_void_NN> can only be used if you both don't need the return
value, and you know that C<sv> is not C<NULL>. The macro doesn't need to
return a meaningful value, or check for NULLness, so it's smaller and faster.
C<SvREFCNT_inc_simple> can only be used with expressions without side
effects. Since we don't have to store a temporary value, it's faster.
C<SvREFCNT_inc_simple_NN> can only be used with expressions without side
effects and you know C<sv> is not C<NULL>. Since we don't have to store a
temporary value, nor check for NULLness, it's faster and smaller.
C<SvREFCNT_inc_simple_void> can only be used with expressions without side
effects and you don't need the return value.
C<SvREFCNT_inc_simple_void_NN> can only be used with expressions without side
effects, you don't need the return value, and you know C<sv> is not C<NULL>.
=for apidoc SvREFCNT_dec
=for apidoc_item SvREFCNT_dec_set_NULL
=for apidoc_item SvREFCNT_dec_ret_NULL
=for apidoc_item SvREFCNT_dec_NN
These decrement the reference count of the given SV.
C<SvREFCNT_dec_NN> may only be used when C<sv> is known to not be C<NULL>.
The function C<SvREFCNT_dec_ret_NULL()> is identical to the
C<SvREFCNT_dec()> except it returns a NULL C<SV *>. It is used by
C<SvREFCNT_dec_set_NULL()> which is a macro which will, when passed a
non-NULL argument, decrement the reference count of its argument and
then set it to NULL. You can replace code of the following form:
if (sv) {
SvREFCNT_dec_NN(sv);
sv = NULL;
}
with
SvREFCNT_dec_set_NULL(sv);
=for apidoc Am|svtype|SvTYPE|SV* sv
Returns the type of the SV. See C<L</svtype>>.
=for apidoc Am|void|SvUPGRADE|SV* sv|svtype type
Used to upgrade an SV to a more complex form. Uses C<sv_upgrade> to
perform the upgrade if necessary. See C<L</svtype>>.
=cut
*/
#define SvANY(sv) (sv)->sv_any
#define SvFLAGS(sv) (sv)->sv_flags
#define SvREFCNT(sv) (sv)->sv_refcnt
/*
=for apidoc_defn Am|SV *|SvREFCNT_inc_simple|SV *sv
=cut
*/
#define SvREFCNT_inc(sv) Perl_SvREFCNT_inc(MUTABLE_SV(sv))
#define SvREFCNT_inc_simple(sv) SvREFCNT_inc(sv)
#define SvREFCNT_inc_NN(sv) Perl_SvREFCNT_inc_NN(MUTABLE_SV(sv))
#define SvREFCNT_inc_void(sv) Perl_SvREFCNT_inc_void(MUTABLE_SV(sv))
/*
=for apidoc_defn Am|void|SvREFCNT_inc_simple_void|SV *sv
These guys don't need the curly blocks
=cut
*/
#define SvREFCNT_inc_simple_void(sv) \
STMT_START { \
SV * sv_ = MUTABLE_SV(sv); \
if (sv_) \
SvREFCNT(sv_)++; \
} STMT_END
/*
=for apidoc_defn Am|SV *|SvREFCNT_inc_simple_NN|SV *sv
=for apidoc_defn Am|void|SvREFCNT_inc_void_NN|SV *sv
=for apidoc_defn Am|void|SvREFCNT_inc_simple_void_NN|SV *sv
=cut
*/
#define SvREFCNT_inc_simple_NN(sv) (++(SvREFCNT(sv)),MUTABLE_SV(sv))
#define SvREFCNT_inc_void_NN(sv) (void)(++SvREFCNT(MUTABLE_SV(sv)))
#define SvREFCNT_inc_simple_void_NN(sv) (void)(++SvREFCNT(MUTABLE_SV(sv)))
#define SvREFCNT_dec(sv) Perl_SvREFCNT_dec(aTHX_ MUTABLE_SV(sv))
#define SvREFCNT_dec_set_NULL(sv) \
STMT_START { \
sv = Perl_SvREFCNT_dec_ret_NULL(aTHX_ MUTABLE_SV(sv)); \
} STMT_END
#define SvREFCNT_dec_NN(sv) Perl_SvREFCNT_dec_NN(aTHX_ MUTABLE_SV(sv))
#define SVTYPEMASK 0xff
#define SvTYPE(sv) ((svtype)((sv)->sv_flags & SVTYPEMASK))
/* Sadly there are some parts of the core that have pointers to already-freed
SV heads, and rely on being able to tell that they are now free. So mark
them all by using a consistent macro. */
#define SvIS_FREED(sv) UNLIKELY(((sv)->sv_flags == SVTYPEMASK))
/* this is defined in this peculiar way to avoid compiler warnings.
* See the <[email protected]> thread in p5p */
#define SvUPGRADE(sv, mt) \
((void)(SvTYPE(sv) >= (mt) || (sv_upgrade(sv, mt),1)))
#define SVf_IOK 0x00000100 /* has valid public integer value */
#define SVf_NOK 0x00000200 /* has valid public numeric value */
#define SVf_POK 0x00000400 /* has valid public pointer value */
#define SVf_ROK 0x00000800 /* has a valid reference pointer */
#define SVp_IOK 0x00001000 /* has valid non-public integer value */
#define SVp_NOK 0x00002000 /* has valid non-public numeric value */
#define SVp_POK 0x00004000 /* has valid non-public pointer value */
#define SVp_SCREAM 0x00008000 /* currently unused on plain scalars */
#define SVphv_CLONEABLE SVp_SCREAM /* PVHV (stashes) clone its objects */
#define SVpgv_GP SVp_SCREAM /* GV has a valid GP */
#define SVprv_PCS_IMPORTED SVp_SCREAM /* RV is a proxy for a constant
subroutine in another package. Set the
GvIMPORTED_CV_on() if it needs to be
expanded to a real GV */
/* SVf_PROTECT is what SVf_READONLY should have been: i.e. modifying
* this SV is completely illegal. However, SVf_READONLY (via
* Internals::SvREADONLY()) has come to be seen as a flag that can be
* temporarily set and unset by the user to indicate e.g. whether a hash
* is "locked". Now, Hash::Util et al only set SVf_READONLY, while core
* sets both (SVf_READONLY|SVf_PROTECT) to indicate both to core and user
* code that this SV should not be messed with.
*/
#define SVf_PROTECT 0x00010000 /* very read-only */
#define SVs_PADTMP 0x00020000 /* in use as tmp */
#define SVs_PADSTALE 0x00040000 /* lexical has gone out of scope;
only used when !PADTMP */
#define SVs_TEMP 0x00080000 /* mortal (implies string is stealable) */
#define SVs_OBJECT 0x00100000 /* is "blessed" */
#define SVs_GMG 0x00200000 /* has magical get method */
#define SVs_SMG 0x00400000 /* has magical set method */
#define SVs_RMG 0x00800000 /* has random magical methods */
#define SVf_FAKE 0x01000000 /* 0: glob is just a copy
1: SV head arena wasn't malloc()ed
2: For PVCV, whether CvUNIQUE(cv)
refers to an eval or once only
[CvEVAL(cv), CvSPECIAL(cv)]
3: HV: informally reserved by DAPM
for vtables
4: Together with other flags (or
lack thereof) indicates a regex,
including PVLV-as-regex. See
isREGEXP().
*/
#define SVf_OOK 0x02000000 /* has valid offset value */
#define SVphv_HasAUX SVf_OOK /* PVHV has an additional hv_aux struct */
#define SVf_BREAK 0x04000000 /* refcnt is artificially low - used by
SVs in final arena cleanup.
Set in S_regtry on PL_reg_curpm, so that
perl_destruct will skip it.
Used for mark and sweep by OP_AASSIGN
*/
#define SVf_READONLY 0x08000000 /* may not be modified */
#define SVf_THINKFIRST (SVf_READONLY|SVf_PROTECT|SVf_ROK|SVf_FAKE \
|SVs_RMG|SVf_IsCOW)
#define SVf_OK (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
SVp_IOK|SVp_NOK|SVp_POK|SVpgv_GP)
#define PRIVSHIFT 4 /* (SVp_?OK >> PRIVSHIFT) == SVf_?OK */
/* SVf_AMAGIC means that the stash *may* have overload methods. It's
* set each time a function is compiled into a stash, and is reset by the
* overload code when called for the first time and finds that there are
* no overload methods. Note that this used to be set on the object; but
* is now only set on stashes.
*/
#define SVf_AMAGIC 0x10000000 /* has magical overloaded methods */
#define SVf_IsCOW 0x10000000 /* copy on write (shared hash key if
SvLEN == 0) */
/* Ensure this value does not clash with the GV_ADD* flags in gv.h, or the
CV_CKPROTO_* flags in op.c, or the padadd_* flags in pad.h: */
#define SVf_UTF8 0x20000000 /* SvPV is UTF-8 encoded
This is also set on RVs whose overloaded
stringification is UTF-8. This might
only happen as a side effect of SvPV() */
/* PVHV */
#define SVphv_SHAREKEYS 0x20000000 /* PVHV keys live on shared string table */
/* PVAV could probably use 0x2000000 without conflict. I assume that PVFM can
be UTF-8 encoded, and PVCVs could well have UTF-8 prototypes. PVIOs haven't
been restructured, so sometimes get used as string buffers. */
/* Some private flags. */
/* scalar SVs with SVp_POK */
#define SVppv_STATIC 0x40000000 /* PV is pointer to static const; must be set with SVf_IsCOW */
/* PVAV */
#define SVpav_REAL 0x40000000 /* free old entries */
/* PVHV */
#define SVphv_LAZYDEL 0x40000000 /* entry in xhv_eiter must be deleted */
/* IV, PVIV, PVNV, PVMG, PVGV and (I assume) PVLV */
#define SVf_IVisUV 0x80000000 /* use XPVUV instead of XPVIV */
/* PVAV */
#define SVpav_REIFY 0x80000000 /* can become real */
/* PVHV */
#define SVphv_HASKFLAGS 0x80000000 /* keys have flag byte after hash */
/* RV upwards. However, SVf_ROK and SVp_IOK are exclusive */
#define SVprv_WEAKREF 0x80000000 /* Weak reference */
/* pad name vars only */
#define _XPV_HEAD \
HV* xmg_stash; /* class package */ \
union _xmgu xmg_u; \
STRLEN xpv_cur; /* length of svu_pv as a C string */ \
union { \
STRLEN xpvlenu_len; /* allocated size */ \
struct regexp* xpvlenu_rx; /* regex when SV body is XPVLV */ \
} xpv_len_u
#define xpv_len xpv_len_u.xpvlenu_len
union _xnvu {
NV xnv_nv; /* numeric value, if any */
HV * xgv_stash;
line_t xnv_lines; /* used internally by S_scan_subst() */
bool xnv_bm_tail; /* an SvVALID (BM) SV has an implicit "\n" */
};
union _xivu {
IV xivu_iv; /* integer value */
UV xivu_uv;
HEK * xivu_namehek; /* xpvlv, xpvgv: GvNAME */
bool xivu_eval_seen; /* used internally by S_scan_subst() */
};
union _xmgu {
MAGIC* xmg_magic; /* linked list of magicalness */
STRLEN xmg_hash_index; /* used while freeing hash entries */
};
struct xpv {
_XPV_HEAD;
};
struct xpviv {
_XPV_HEAD;
union _xivu xiv_u;
};
#define xiv_iv xiv_u.xivu_iv
struct xpvuv {
_XPV_HEAD;
union _xivu xuv_u;
};
#define xuv_uv xuv_u.xivu_uv
struct xpvnv {
_XPV_HEAD;
union _xivu xiv_u;
union _xnvu xnv_u;
};
/* This structure must match the beginning of struct xpvhv in hv.h. */
struct xpvmg {
_XPV_HEAD;
union _xivu xiv_u;
union _xnvu xnv_u;
};
struct xpvlv {
_XPV_HEAD;
union _xivu xiv_u;
union _xnvu xnv_u;
union {
STRLEN xlvu_targoff;
SSize_t xlvu_stargoff;
} xlv_targoff_u;
STRLEN xlv_targlen;
SV* xlv_targ;
char xlv_type; /* k=keys .=pos x=substr v=vec /=join/re
* y=alem/helem/iter t=tie T=tied HE */
char xlv_flags; /* 1 = negative offset 2 = negative len
4 = out of range (vec) */
};
#define xlv_targoff xlv_targoff_u.xlvu_targoff
struct xpvinvlist {
_XPV_HEAD;
IV prev_index; /* caches result of previous invlist_search() */
STRLEN iterator; /* Stores where we are in iterating */
bool is_offset; /* The data structure for all inversion lists
begins with an element for code point U+0000.
If this bool is set, the actual list contains
that 0; otherwise, the list actually begins
with the following element. Thus to invert
the list, merely toggle this flag */
};
/* This structure works in 2 ways - regular scalar, or GV with GP */
struct xpvgv {
_XPV_HEAD;
union _xivu xiv_u;
union _xnvu xnv_u;
};
typedef U32 cv_flags_t;
#define _XPVCV_COMMON \
HV * xcv_stash; \
union { \
OP * xcv_start; \
ANY xcv_xsubany; \
} xcv_start_u; \
union { \
OP * xcv_root; \
void (*xcv_xsub) (pTHX_ CV*); \
} xcv_root_u; \
union { \
GV * xcv_gv; \
HEK * xcv_hek; \
} xcv_gv_u; \
char * xcv_file; \
union { \
PADLIST * xcv_padlist; \
void * xcv_hscxt; \
} xcv_padlist_u; \
CV * xcv_outside; \
U32 xcv_outside_seq; /* the COP sequence (at the point of our \
* compilation) in the lexically enclosing \
* sub */ \
cv_flags_t xcv_flags; \
I32 xcv_depth /* >= 2 indicates recursive call */
/* This structure must match XPVCV in cv.h */
struct xpvfm {
_XPV_HEAD;
_XPVCV_COMMON;
};
struct xpvio {
_XPV_HEAD;
union _xivu xiv_u;
/* ifp and ofp are normally the same, but sockets need separate streams */
PerlIO * xio_ofp;
/* Cray addresses everything by word boundaries (64 bits) and
* code and data pointers cannot be mixed (which is exactly what
* Perl_filter_add() tries to do with the dirp), hence the
* following union trick (as suggested by Gurusamy Sarathy).
* For further information see Geir Johansen's problem report
* titled [ID 20000612.002 (#3366)] Perl problem on Cray system
* The any pointer (known as IoANY()) will also be a good place
* to hang any IO disciplines to.
*/
union {
DIR * xiou_dirp; /* for opendir, readdir, etc */
void * xiou_any; /* for alignment */
} xio_dirpu;
/* IV xio_lines is now in IVX $. */
IV xio_page; /* $% */
IV xio_page_len; /* $= */
IV xio_lines_left; /* $- */
char * xio_top_name; /* $^ */
GV * xio_top_gv; /* $^ */
char * xio_fmt_name; /* $~ */
GV * xio_fmt_gv; /* $~ */
char * xio_bottom_name;/* $^B */
GV * xio_bottom_gv; /* $^B */
char xio_type;
U8 xio_flags;
};
#define xio_dirp xio_dirpu.xiou_dirp
#define xio_any xio_dirpu.xiou_any
#define IOf_ARGV 1 /* this fp iterates over ARGV */
#define IOf_START 2 /* check for null ARGV and substitute '-' */
#define IOf_FLUSH 4 /* this fp wants a flush after write op */
#define IOf_DIDTOP 8 /* just did top of form */
#define IOf_UNTAINT 16 /* consider this fp (and its data) "safe" */
#define IOf_NOLINE 32 /* slurped a pseudo-line from empty file */
#define IOf_FAKE_DIRP 64 /* xio_dirp is fake (source filters kludge)
Also, when this is set, SvPVX() is valid */
struct xobject {
HV* xmg_stash;
union _xmgu xmg_u;
SSize_t xobject_maxfield;
SSize_t xobject_iter_sv_at; /* this is only used by Perl_sv_clear() */
SV** xobject_fields;
};
#define ObjectMAXFIELD(inst) ((XPVOBJ *)SvANY(inst))->xobject_maxfield
#define ObjectITERSVAT(inst) ((XPVOBJ *)SvANY(inst))->xobject_iter_sv_at
#define ObjectFIELDS(inst) ((XPVOBJ *)SvANY(inst))->xobject_fields
/* The following macros define implementation-independent predicates on SVs. */
/*
=for apidoc Am|U32|SvNIOK|SV* sv
Returns a U32 value indicating whether the SV contains a number, integer or
double.
=for apidoc Am|U32|SvNIOKp|SV* sv
Returns a U32 value indicating whether the SV contains a number, integer or
double. Checks the B<private> setting. Use C<SvNIOK> instead.
=for apidoc Am|void|SvNIOK_off|SV* sv
Unsets the NV/IV status of an SV.
=for apidoc Am|U32|SvOK|SV* sv
Returns a U32 value indicating whether the value is defined. This is
only meaningful for scalars.
=for apidoc Am|U32|SvIOKp|SV* sv
Returns a U32 value indicating whether the SV contains an integer. Checks
the B<private> setting. Use C<SvIOK> instead.
=for apidoc Am|U32|SvNOKp|SV* sv
Returns a U32 value indicating whether the SV contains a double. Checks the
B<private> setting. Use C<SvNOK> instead.
=for apidoc Am|U32|SvPOKp|SV* sv
Returns a U32 value indicating whether the SV contains a character string.
Checks the B<private> setting. Use C<SvPOK> instead.
=for apidoc Am|U32|SvIOK|SV* sv
Returns a U32 value indicating whether the SV contains an integer.
=for apidoc Am|void|SvIOK_on|SV* sv
Tells an SV that it is an integer.
=for apidoc Am|void|SvIOK_off|SV* sv
Unsets the IV status of an SV.
=for apidoc Am|void|SvIOK_only|SV* sv
Tells an SV that it is an integer and disables all other C<OK> bits.
=for apidoc Am|void|SvIOK_only_UV|SV* sv
Tells an SV that it is an unsigned integer and disables all other C<OK> bits.
=for apidoc Am|bool|SvIOK_UV|SV* sv
Returns a boolean indicating whether the SV contains an integer that must be
interpreted as unsigned. A non-negative integer whose value is within the
range of both an IV and a UV may be flagged as either C<SvUOK> or C<SvIOK>.
=for apidoc Am|bool|SvUOK|SV* sv
Returns a boolean indicating whether the SV contains an integer that must be
interpreted as unsigned. A non-negative integer whose value is within the
range of both an IV and a UV may be flagged as either C<SvUOK> or C<SvIOK>.
=for apidoc Am|bool|SvIOK_notUV|SV* sv
Returns a boolean indicating whether the SV contains a signed integer.
=for apidoc Am|U32|SvNOK|SV* sv
Returns a U32 value indicating whether the SV contains a double.
=for apidoc Am|void|SvNOK_on|SV* sv
Tells an SV that it is a double.
=for apidoc Am|void|SvNOK_off|SV* sv
Unsets the NV status of an SV.
=for apidoc Am|void|SvNOK_only|SV* sv
Tells an SV that it is a double and disables all other OK bits.
=for apidoc Am|U32|SvPOK|SV* sv
Returns a U32 value indicating whether the SV contains a character
string.
=for apidoc Am|void|SvPOK_on|SV* sv
Tells an SV that it is a string.
=for apidoc Am|void|SvPOK_off|SV* sv
Unsets the PV status of an SV.
=for apidoc Am|void|SvPOK_only|SV* sv
Tells an SV that it is a string and disables all other C<OK> bits.
Will also turn off the UTF-8 status.
=for apidoc Am|U32|SvBoolFlagsOK|SV* sv
Returns a bool indicating whether the SV has the right flags set such
that it is safe to call C<BOOL_INTERNALS_sv_isbool()> or
C<BOOL_INTERNALS_sv_isbool_true()> or
C<BOOL_INTERNALS_sv_isbool_false()>. Currently equivalent to
C<SvIandPOK(sv)> or C<SvIOK(sv) && SvPOK(sv)>. Serialization may want to
unroll this check. If so you are strongly recommended to add code like
C<assert(SvBoolFlagsOK(sv));> B<before> calling using any of the
BOOL_INTERNALS macros.
=for apidoc Am|U32|SvIandPOK|SV* sv
Returns a bool indicating whether the SV is both C<SvPOK()> and
C<SvIOK()> at the same time. Equivalent to C<SvIOK(sv) && SvPOK(sv)> but
more efficient.
=for apidoc Am|void|SvIandPOK_on|SV* sv
Tells an SV that is a string and a number in one operation. Equivalent
to C<SvIOK_on(sv); SvPOK_on(sv);> but more efficient.
=for apidoc Am|void|SvIandPOK_off|SV* sv
Unsets the PV and IV status of an SV in one operation. Equivalent to
C<SvIOK_off(sv); SvPK_off(v);> but more efficient.
=for apidoc Am|bool|BOOL_INTERNALS_sv_isbool|SV* sv
Checks if a C<SvBoolFlagsOK()> sv is a bool. B<Note> that it is the
caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()> before
calling this. This is only useful in specialized logic like
serialization code where performance is critical and the flags have
already been checked to be correct. Almost always you should be using
C<sv_isbool(sv)> instead.
=for apidoc Am|bool|BOOL_INTERNALS_sv_isbool_true|SV* sv
Checks if a C<SvBoolFlagsOK()> sv is a true bool. B<Note> that it is
the caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()>
before calling this. This is only useful in specialized logic like
serialization code where performance is critical and the flags have
already been checked to be correct. This is B<NOT> what you should use
to check if an SV is "true", for that you should be using
C<SvTRUE(sv)> instead.
=for apidoc Am|bool|BOOL_INTERNALS_sv_isbool_false|SV* sv
Checks if a C<SvBoolFlagsOK()> sv is a false bool. B<Note> that it is
the caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()>
before calling this. This is only useful in specialized logic like
serialization code where performance is critical and the flags have
already been checked to be correct. This is B<NOT> what you should use
to check if an SV is "false", for that you should be using
C<!SvTRUE(sv)> instead.
=for apidoc Am|bool|SvVOK|SV* sv
Returns a boolean indicating whether the SV contains a v-string.
=for apidoc Am|U32|SvOOK|SV* sv
Returns a U32 indicating whether the pointer to the string buffer is offset.
This hack is used internally to speed up removal of characters from the
beginning of a C<L</SvPV>>. When C<SvOOK> is true, then the start of the
allocated string buffer is actually C<SvOOK_offset()> bytes before C<SvPVX>.
This offset used to be stored in C<SvIVX>, but is now stored within the spare
part of the buffer.
=for apidoc Am|U32|SvROK|SV* sv
Tests if the SV is an RV.
=for apidoc Am|void|SvROK_on|SV* sv
Tells an SV that it is an RV.
=for apidoc Am|void|SvROK_off|SV* sv
Unsets the RV status of an SV.
=for apidoc Am|SV*|SvRV|SV* sv
Dereferences an RV to return the SV.
=for apidoc Am|IV|SvIVX|SV* sv
Returns the raw value in the SV's IV slot, without checks or conversions.
Only use when you are sure C<SvIOK> is true. See also C<L</SvIV>>.
=for apidoc Am|UV|SvUVX|SV* sv
Returns the raw value in the SV's UV slot, without checks or conversions.
Only use when you are sure C<SvIOK> is true. See also C<L</SvUV>>.
=for apidoc AmD|UV|SvUVXx|SV* sv
This is an unnecessary synonym for L</SvUVX>
=for apidoc Am|NV|SvNVX|SV* sv
Returns the raw value in the SV's NV slot, without checks or conversions.
Only use when you are sure C<SvNOK> is true. See also C<L</SvNV>>.
=for apidoc Am |char* |SvPVX|SV* sv
=for apidoc_item |const char*|SvPVX_const|SV* sv
=for apidoc_item |char* |SvPVX_mutable|SV* sv
=for apidoc_item |char* |SvPVXx|SV* sv
These return a pointer to the physical string in the SV. The SV must contain a
string. Prior to 5.9.3 it is not safe to execute these unless the SV's
type >= C<SVt_PV>.
These are also used to store the name of an autoloaded subroutine in an XS
AUTOLOAD routine. See L<perlguts/Autoloading with XSUBs>.
C<SvPVXx> is identical to C<SvPVX>.
C<SvPVX_mutable> is merely a synonym for C<SvPVX>, but its name emphasizes that
the string is modifiable by the caller.
C<SvPVX_const> differs in that the return value has been cast so that the
compiler will complain if you were to try to modify the contents of the string,
(unless you cast away const yourself).
=for apidoc Am|STRLEN|SvCUR|SV* sv
Returns the length, in bytes, of the PV inside the SV.
Note that this may not match Perl's C<length>; for that, use
C<sv_len_utf8(sv)>. See C<L</SvLEN>> also.
=for apidoc Am|STRLEN|SvLEN|SV* sv
Returns the size of the string buffer in the SV, not including any part
attributable to C<SvOOK>. See C<L</SvCUR>>.
=for apidoc Am|char*|SvEND|SV* sv
Returns a pointer to the spot just after the last character in
the string which is in the SV, where there is usually a trailing
C<NUL> character (even though Perl scalars do not strictly require it).
See C<L</SvCUR>>. Access the character as C<*(SvEND(sv))>.
Warning: If C<SvCUR> is equal to C<SvLEN>, then C<SvEND> points to
unallocated memory.
=for apidoc Am|HV*|SvSTASH|SV* sv
Returns the stash of the SV.
=for apidoc Am|void|SvIV_set|SV* sv|IV val
Set the value of the IV pointer in sv to val. It is possible to perform
the same function of this macro with an lvalue assignment to C<SvIVX>.
With future Perls, however, it will be more efficient to use
C<SvIV_set> instead of the lvalue assignment to C<SvIVX>.
=for apidoc Am|void|SvNV_set|SV* sv|NV val
Set the value of the NV pointer in C<sv> to val. See C<L</SvIV_set>>.
=for apidoc Am|void|SvPV_set|SV* sv|char* val
This is probably not what you want to use, you probably wanted
L</sv_usepvn_flags> or L</sv_setpvn> or L</sv_setpvs>.
Set the value of the PV pointer in C<sv> to the Perl allocated
C<NUL>-terminated string C<val>. See also C<L</SvIV_set>>.
Remember to free the previous PV buffer. There are many things to check.
Beware that the existing pointer may be involved in copy-on-write or other
mischief, so do C<SvOOK_off(sv)> and use C<sv_force_normal> or
C<SvPV_force> (or check the C<SvIsCOW> flag) first to make sure this
modification is safe. Then finally, if it is not a COW, call
C<L</SvPV_free>> to free the previous PV buffer.
=for apidoc Am|void|SvUV_set|SV* sv|UV val
Set the value of the UV pointer in C<sv> to val. See C<L</SvIV_set>>.
=for apidoc Am|void|SvRV_set|SV* sv|SV* val
Set the value of the RV pointer in C<sv> to val. See C<L</SvIV_set>>.
=for apidoc Am|void|SvMAGIC_set|SV* sv|MAGIC* val
Set the value of the MAGIC pointer in C<sv> to val. See C<L</SvIV_set>>.
=for apidoc Am|void|SvSTASH_set|SV* sv|HV* val
Set the value of the STASH pointer in C<sv> to val. See C<L</SvIV_set>>.
=for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
Sets the current length, in bytes, of the C string which is in the SV.
See C<L</SvCUR>> and C<SvIV_set>>.
=for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
Set the size of the string buffer for the SV. See C<L</SvLEN>>.
=cut
*/
#define SvNIOK(sv) (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
#define SvNIOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
#define SvNIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
SVp_IOK|SVp_NOK|SVf_IVisUV))
#define assert_not_ROK(sv) assert_(!SvROK(sv) || !SvRV(sv))
#define assert_not_glob(sv) assert_(!isGV_with_GP(sv))
#define SvOK(sv) (SvFLAGS(sv) & SVf_OK)
#define SvOK_off(sv) (assert_not_ROK(sv) assert_not_glob(sv) \