This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Promote v5.36 usage and feature bundles doc
[perl5.git] / mg.h
CommitLineData
a0d0e21e 1/* mg.h
79072805 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
1129b882 4 * 2000, 2002, 2005, 2006, 2007, 2008 by Larry Wall and others
79072805
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
79072805
LW
9 */
10
11struct mgvtbl {
16c91539
BM
12 int (*svt_get) (pTHX_ SV *sv, MAGIC* mg);
13 int (*svt_set) (pTHX_ SV *sv, MAGIC* mg);
14 U32 (*svt_len) (pTHX_ SV *sv, MAGIC* mg);
6a84ef7c 15 int (*svt_clear) (pTHX_ SV *sv, MAGIC* mg);
16c91539
BM
16 int (*svt_free) (pTHX_ SV *sv, MAGIC* mg);
17 int (*svt_copy) (pTHX_ SV *sv, MAGIC* mg,
1604cfb0 18 SV *nsv, const char *name, I32 namlen);
16c91539
BM
19 int (*svt_dup) (pTHX_ MAGIC *mg, CLONE_PARAMS *param);
20 int (*svt_local)(pTHX_ SV *nsv, MAGIC *mg);
79072805
LW
21};
22
23struct magic {
24 MAGIC* mg_moremagic;
92e67595 25 MGVTBL* mg_virtual; /* pointer to magic functions */
79072805
LW
26 U16 mg_private;
27 char mg_type;
28 U8 mg_flags;
6174b39a 29 SSize_t mg_len;
79072805
LW
30 SV* mg_obj;
31 char* mg_ptr;
79072805 32};
8990e307 33
cb50f42d
YST
34#define MGf_TAINTEDDIR 1 /* PERL_MAGIC_envelem only */
35#define MGf_MINMATCH 1 /* PERL_MAGIC_regex_global only */
aa38f4b1 36#define MGf_REQUIRE_GV 1 /* PERL_MAGIC_checkcall only */
85e6fe83 37#define MGf_REFCOUNTED 2
fd69380d 38#define MGf_GSKIP 4 /* skip further GETs until after next SET */
a5063e7c
DM
39#define MGf_COPY 8 /* has an svt_copy MGVTBL entry */
40#define MGf_DUP 0x10 /* has an svt_dup MGVTBL entry */
41#define MGf_LOCAL 0x20 /* has an svt_local MGVTBL entry */
25fdce4a 42#define MGf_BYTES 0x40 /* PERL_MAGIC_regex_global only */
d39c26a6 43#define MGf_PERSIST 0x80 /* PERL_MAGIC_lvref only */
748a9306 44
1e422769 45#define MgTAINTEDDIR(mg) (mg->mg_flags & MGf_TAINTEDDIR)
46#define MgTAINTEDDIR_on(mg) (mg->mg_flags |= MGf_TAINTEDDIR)
47#define MgTAINTEDDIR_off(mg) (mg->mg_flags &= ~MGf_TAINTEDDIR)
99069129 48
50352f16
FG
49/* Extracts the SV stored in mg, or NULL. */
50#define MgSV(mg) (((int)((mg)->mg_len) == HEf_SVKEY) ? \
51 MUTABLE_SV((mg)->mg_ptr) : \
52 NULL)
53
54/* If mg contains an SV, these extract the PV stored in that SV;
55 otherwise, these extract the mg's mg_ptr/mg_len.
56 These do NOT account for the SV's UTF8 flag, so handle with care.
57*/
32da55ab 58#define MgPV(mg,lp) ((((int)(lp = (mg)->mg_len)) == HEf_SVKEY) ? \
1604cfb0
MS
59 SvPV(MUTABLE_SV((mg)->mg_ptr),lp) : \
60 (mg)->mg_ptr)
01b8bcb7 61#define MgPV_const(mg,lp) ((((int)(lp = (mg)->mg_len)) == HEf_SVKEY) ? \
1604cfb0
MS
62 SvPV_const(MUTABLE_SV((mg)->mg_ptr),lp) : \
63 (const char*)(mg)->mg_ptr)
b1bc3f34 64#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
1604cfb0
MS
65 SvPV_nolen_const(MUTABLE_SV((mg)->mg_ptr)) : \
66 (const char*)(mg)->mg_ptr)
33c27489 67
4608196e 68#define SvTIED_mg(sv,how) (SvRMAGICAL(sv) ? mg_find((sv),(how)) : NULL)
33c27489
GS
69#define SvTIED_obj(sv,mg) \
70 ((mg)->mg_obj ? (mg)->mg_obj : sv_2mortal(newRV(sv)))
e9a8c099 71
25fdce4a
FC
72#if defined(PERL_CORE) || defined(PERL_EXT)
73# define MgBYTEPOS(mg,sv,pv,len) S_MgBYTEPOS(aTHX_ mg,sv,pv,len)
74/* assumes get-magic and stringification have already occurred */
75# define MgBYTEPOS_set(mg,sv,pv,off) ( \
76 assert_((mg)->mg_type == PERL_MAGIC_regex_global) \
ed382232 77 SvPOK(sv) && (!SvGMAGICAL(sv) || sv_only_taint_gmagic(sv)) \
1604cfb0
MS
78 ? (mg)->mg_len = (off), (mg)->mg_flags |= MGf_BYTES \
79 : ((mg)->mg_len = DO_UTF8(sv) \
80 ? (SSize_t)utf8_length((U8 *)(pv), (U8 *)(pv)+(off)) \
81 : (SSize_t)(off), \
82 (mg)->mg_flags &= ~MGf_BYTES))
25fdce4a
FC
83#endif
84
84c7b88c
BF
85#define whichsig(pv) whichsig_pv(pv)
86
e9a8c099 87/*
14d04a33 88 * ex: set ts=8 sts=4 sw=4 et:
e9a8c099 89 */