This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Various updates and fixes to some of the SysV IPC ops and their tests
[perl5.git] / scope.h
CommitLineData
d6376244
JH
1/* scope.h
2 *
1129b882
NC
3 * Copyright (C) 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2001,
4 * 2002, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
d6376244
JH
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 *
9 */
10
9a2fefd6 11/* *** Update arg_counts[] in scope.c if you modify these */
03dba561
DM
12
13/* zero args */
14
15#define SAVEt_ALLOC 0
16#define SAVEt_CLEARPADRANGE 1
17#define SAVEt_CLEARSV 2
18#define SAVEt_REGCONTEXT 3
03dba561 19
03dba561
DM
20/* one arg */
21
2ef9a108 22#define SAVEt_TMPSFLOOR 4
03dba561
DM
23#define SAVEt_BOOL 5
24#define SAVEt_COMPILE_WARNINGS 6
25#define SAVEt_COMPPAD 7
26#define SAVEt_FREECOPHH 8
27#define SAVEt_FREEOP 9
28#define SAVEt_FREEPV 10
29#define SAVEt_FREESV 11
30#define SAVEt_I16 12
31#define SAVEt_I32_SMALL 13
32#define SAVEt_I8 14
33#define SAVEt_INT_SMALL 15
34#define SAVEt_MORTALIZESV 16
35#define SAVEt_NSTAB 17
36#define SAVEt_OP 18
37#define SAVEt_PARSER 19
38#define SAVEt_STACK_POS 20
20d5dc23 39#define SAVEt_READONLY_OFF 21
0f94cb1f 40#define SAVEt_FREEPADNAME 22
03dba561 41
03dba561
DM
42/* two args */
43
03dba561
DM
44#define SAVEt_AV 23
45#define SAVEt_DESTRUCTOR 24
46#define SAVEt_DESTRUCTOR_X 25
47#define SAVEt_GENERIC_PVREF 26
48#define SAVEt_GENERIC_SVREF 27
49#define SAVEt_GP 28
50#define SAVEt_GVSV 29
51#define SAVEt_HINTS 30
52#define SAVEt_HPTR 31
53#define SAVEt_HV 32
54#define SAVEt_I32 33
55#define SAVEt_INT 34
56#define SAVEt_ITEM 35
57#define SAVEt_IV 36
58#define SAVEt_LONG 37
59#define SAVEt_PPTR 38
60#define SAVEt_SAVESWITCHSTACK 39
61#define SAVEt_SHARED_PVREF 40
62#define SAVEt_SPTR 41
e8eb279c 63#define SAVEt_STRLEN 42
03dba561
DM
64#define SAVEt_SV 43
65#define SAVEt_SVREF 44
66#define SAVEt_VPTR 45
20d5dc23 67#define SAVEt_ADELETE 46
0f94cb1f 68#define SAVEt_APTR 47
03dba561 69
03dba561
DM
70/* three args */
71
03dba561
DM
72#define SAVEt_HELEM 48
73#define SAVEt_PADSV_AND_MORTALIZE 49
74#define SAVEt_SET_SVFLAGS 50
75#define SAVEt_GVSLOT 51
20d5dc23 76#define SAVEt_AELEM 52
0f94cb1f 77#define SAVEt_DELETE 53
6c148c86 78#define SAVEt_HINTS_HH 54
0f94cb1f 79
79072805 80
af7df257 81#define SAVEf_SETMAGIC 1
75d34a09 82#define SAVEf_KEEPOLDELEM 2
af7df257 83
c6bf6a65
NC
84#define SAVE_TIGHT_SHIFT 6
85#define SAVE_MASK 0x3F
86
91d1c79f 87#define save_aelem(av,idx,sptr) save_aelem_flags(av,idx,sptr,SAVEf_SETMAGIC)
af7df257
CS
88#define save_helem(hv,key,sptr) save_helem_flags(hv,key,sptr,SAVEf_SETMAGIC)
89
b03c0a3a 90#ifndef SCOPE_SAVES_SIGNAL_MASK
1b266415 91#define SCOPE_SAVES_SIGNAL_MASK 0
b03c0a3a
NIS
92#endif
93
a3444cc5
DM
94/* the maximum number of entries that might be pushed using the SS_ADD*
95 * macros */
96#define SS_MAXPUSH 4
97
3caf0269
DM
98#define SSCHECK(need) if (UNLIKELY(PL_savestack_ix + (I32)(need) > PL_savestack_max)) savestack_grow()
99#define SSGROW(need) if (UNLIKELY(PL_savestack_ix + (I32)(need) > PL_savestack_max)) savestack_grow_cnt(need)
3280af22
NIS
100#define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i))
101#define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i))
9febdf04 102#define SSPUSHBOOL(p) (PL_savestack[PL_savestack_ix++].any_bool = (p))
3280af22 103#define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
c6bf6a65 104#define SSPUSHUV(u) (PL_savestack[PL_savestack_ix++].any_uv = (UV)(u))
3280af22
NIS
105#define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p))
106#define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p))
c76ac1ee 107#define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
a3444cc5
DM
108
109/* SS_ADD*: newer, faster versions of the above. Don't mix the two sets of
110 * macros. These are fast because they save reduce accesses to the PL_
111 * vars and move the size check to the end. Doing the check last means
112 * that values in registers will have been pushed and no longer needed, so
113 * don't need saving around the call to grow. Also, tail-call elimination
114 * of the grow() can be done. These changes reduce the code of something
115 * like save_pushptrptr() to half its former size.
116 * Of course, doing the size check *after* pushing means we must always
3caf0269
DM
117 * ensure there are SS_MAXPUSH free slots on the savestack. This ensured
118 * bt savestack_grow() and savestack_grow_cnt always allocating SS_MAXPUSH
119 * slots more than asked for, or that it sets PL_savestack_max to
a3444cc5
DM
120 *
121 * These are for internal core use only and are subject to change */
122
123#define dSS_ADD \
124 I32 ix = PL_savestack_ix; \
c70927a6 125 ANY *ssp = &PL_savestack[ix]
a3444cc5
DM
126
127#define SS_ADD_END(need) \
128 assert((need) <= SS_MAXPUSH); \
129 ix += (need); \
130 PL_savestack_ix = ix; \
3caf0269
DM
131 assert(ix <= PL_savestack_max + SS_MAXPUSH); \
132 if (UNLIKELY(ix > PL_savestack_max)) savestack_grow(); \
133 assert(PL_savestack_ix <= PL_savestack_max);
a3444cc5
DM
134
135#define SS_ADD_INT(i) ((ssp++)->any_i32 = (I32)(i))
136#define SS_ADD_LONG(i) ((ssp++)->any_long = (long)(i))
137#define SS_ADD_BOOL(p) ((ssp++)->any_bool = (p))
138#define SS_ADD_IV(i) ((ssp++)->any_iv = (IV)(i))
139#define SS_ADD_UV(u) ((ssp++)->any_uv = (UV)(u))
140#define SS_ADD_PTR(p) ((ssp++)->any_ptr = (void*)(p))
141#define SS_ADD_DPTR(p) ((ssp++)->any_dptr = (p))
142#define SS_ADD_DXPTR(p) ((ssp++)->any_dxptr = (p))
143
3280af22
NIS
144#define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
145#define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
9febdf04 146#define SSPOPBOOL (PL_savestack[--PL_savestack_ix].any_bool)
3280af22 147#define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
c6bf6a65 148#define SSPOPUV (PL_savestack[--PL_savestack_ix].any_uv)
3280af22
NIS
149#define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
150#define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
c76ac1ee 151#define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
8990e307 152
a3444cc5 153
954c1994 154/*
3f620621 155=for apidoc_section $callback
ccfc67b7 156
4e5171e9 157=for apidoc Amns||SAVETMPS
fbe13c60 158Opening bracket for temporaries on a callback. See C<L</FREETMPS>> and
954c1994
GS
159L<perlcall>.
160
4e5171e9 161=for apidoc Amns||FREETMPS
fbe13c60 162Closing bracket for temporaries on a callback. See C<L</SAVETMPS>> and
954c1994
GS
163L<perlcall>.
164
4e5171e9 165=for apidoc Amns||ENTER
fbe13c60 166Opening bracket on a callback. See C<L</LEAVE>> and L<perlcall>.
954c1994 167
4e5171e9 168=for apidoc Amns||LEAVE
fbe13c60 169Closing bracket on a callback. See C<L</ENTER>> and L<perlcall>.
954c1994 170
3bb9fd01 171=for apidoc Ams||ENTER_with_name|"name"
d343c3ef 172
ef7ea7ad 173Same as C<L</ENTER>>, but when debugging is enabled it also associates the
d343c3ef
GG
174given literal string with the new scope.
175
3bb9fd01 176=for apidoc Ams||LEAVE_with_name|"name"
d343c3ef 177
ef7ea7ad 178Same as C<L</LEAVE>>, but when debugging is enabled it first checks that the
1568d13a 179scope has the given name. C<name> must be a literal string.
d343c3ef 180
954c1994
GS
181=cut
182*/
183
2ef9a108
DM
184#define SAVETMPS Perl_savetmps(aTHX)
185
3280af22 186#define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
a0d0e21e 187
f46d017c
GS
188#ifdef DEBUGGING
189#define ENTER \
190 STMT_START { \
191 push_scope(); \
b4ab917c 192 DEBUG_SCOPE("ENTER") \
f46d017c
GS
193 } STMT_END
194#define LEAVE \
195 STMT_START { \
b4ab917c 196 DEBUG_SCOPE("LEAVE") \
f46d017c
GS
197 pop_scope(); \
198 } STMT_END
d343c3ef
GG
199#define ENTER_with_name(name) \
200 STMT_START { \
201 push_scope(); \
a09dc31d
NC
202 if (PL_scopestack_name) \
203 PL_scopestack_name[PL_scopestack_ix-1] = name; \
d343c3ef
GG
204 DEBUG_SCOPE("ENTER \"" name "\"") \
205 } STMT_END
206#define LEAVE_with_name(name) \
207 STMT_START { \
208 DEBUG_SCOPE("LEAVE \"" name "\"") \
a09dc31d
NC
209 if (PL_scopestack_name) { \
210 assert(((char*)PL_scopestack_name[PL_scopestack_ix-1] \
211 == (char*)name) \
212 || strEQ(PL_scopestack_name[PL_scopestack_ix-1], name)); \
213 } \
d343c3ef
GG
214 pop_scope(); \
215 } STMT_END
f46d017c 216#else
a0d0e21e
LW
217#define ENTER push_scope()
218#define LEAVE pop_scope()
d343c3ef
GG
219#define ENTER_with_name(name) ENTER
220#define LEAVE_with_name(name) LEAVE
f46d017c 221#endif
d699ecb7
FC
222#define LEAVE_SCOPE(old) STMT_START { \
223 if (PL_savestack_ix > old) leave_scope(old); \
224 } STMT_END
a0d0e21e 225
3f26ef39
NC
226#define SAVEI8(i) save_I8((I8*)&(i))
227#define SAVEI16(i) save_I16((I16*)&(i))
228#define SAVEI32(i) save_I32((I32*)&(i))
229#define SAVEINT(i) save_int((int*)&(i))
230#define SAVEIV(i) save_iv((IV*)&(i))
231#define SAVELONG(l) save_long((long*)&(l))
02644217 232#define SAVEBOOL(b) save_bool(&(b))
55497cff 233#define SAVESPTR(s) save_sptr((SV**)&(s))
3f26ef39 234#define SAVEPPTR(s) save_pptr((char**)&(s))
57b2e452 235#define SAVEVPTR(s) save_vptr((void*)&(s))
09edbca0 236#define SAVEPADSVANDMORTALIZE(s) save_padsv_and_mortalize(s)
b1bc3f34 237#define SAVEFREESV(s) save_freesv(MUTABLE_SV(s))
0f94cb1f 238#define SAVEFREEPADNAME(s) save_pushptr((void *)(s), SAVEt_FREEPADNAME)
b1bc3f34 239#define SAVEMORTALIZESV(s) save_mortalizesv(MUTABLE_SV(s))
3f26ef39
NC
240#define SAVEFREEOP(o) save_freeop((OP*)(o))
241#define SAVEFREEPV(p) save_freepv((char*)(p))
242#define SAVECLEARSV(sv) save_clearsv((SV**)&(sv))
b9d12d37 243#define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
f4dd75d9 244#define SAVEGENERICPV(s) save_generic_pvref((char**)&(s))
05ec9bb3 245#define SAVESHAREDPV(s) save_shared_pvref((char**)&(s))
14f338dc 246#define SAVESETSVFLAGS(sv,mask,val) save_set_svflags(sv,mask,val)
3987a177 247#define SAVEFREECOPHH(h) save_pushptr((void *)(h), SAVEt_FREECOPHH)
55497cff 248#define SAVEDELETE(h,k,l) \
dbebbdb4 249 save_delete(MUTABLE_HV(h), (char*)(k), (I32)(l))
af097752
VP
250#define SAVEHDELETE(h,s) \
251 save_hdelete(MUTABLE_HV(h), (s))
c68ec7a9 252#define SAVEADELETE(a,k) \
c70927a6 253 save_adelete(MUTABLE_AV(a), (SSize_t)(k))
55497cff 254#define SAVEDESTRUCTOR(f,p) \
3f26ef39 255 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), (void*)(p))
c76ac1ee
GS
256
257#define SAVEDESTRUCTOR_X(f,p) \
3f26ef39 258 save_destructor_x((DESTRUCTORFUNC_t)(f), (void*)(p))
25eaa213
GS
259
260#define SAVESTACK_POS() \
a3444cc5
DM
261 STMT_START { \
262 dSS_ADD; \
263 SS_ADD_INT(PL_stack_sp - PL_stack_base); \
264 SS_ADD_UV(SAVEt_STACK_POS); \
265 SS_ADD_END(2); \
25eaa213
GS
266 } STMT_END
267
462e5cf6 268#define SAVEOP() save_op()
25eaa213 269
da8315f8 270#define SAVEHINTS() save_hints()
354992b1 271
9c47725a 272#define SAVECOMPPAD() save_pushptr(MUTABLE_SV(PL_comppad), SAVEt_COMPPAD)
a9332b4a 273
8b7059b1
DM
274#define SAVESWITCHSTACK(f,t) \
275 STMT_START { \
dfcd3de5 276 save_pushptrptr(MUTABLE_SV(f), MUTABLE_SV(t), SAVEt_SAVESWITCHSTACK); \
8b7059b1
DM
277 SWITCHSTACK((f),(t)); \
278 PL_curstackinfo->si_stack = (t); \
279 } STMT_END
280
c88c823b 281/* Need to do the cop warnings like this, rather than a "SAVEFREESHAREDPV",
72dc9ed5
NC
282 because realloc() means that the value can actually change. Possibly
283 could have done savefreesharedpvREF, but this way actually seems cleaner,
284 as it simplifies the code that does the saves, and reduces the load on the
285 save stack. */
747e2fae 286#define SAVECOMPILEWARNINGS() save_pushptr(PL_compiling.cop_warnings, SAVEt_COMPILE_WARNINGS)
fc15ae8f 287
747e2fae 288#define SAVEPARSER(p) save_pushptr((p), SAVEt_PARSER)
7c197c94 289
57843af0 290#ifdef USE_ITHREADS
d4d03940 291# define SAVECOPSTASH_FREE(c) SAVEIV((c)->cop_stashoff)
1dc74fdb
FC
292# define SAVECOPFILE(c) SAVEPPTR(CopFILE(c))
293# define SAVECOPFILE_FREE(c) SAVESHAREDPV(CopFILE(c))
57843af0 294#else
61b3e50d
FC
295# /* XXX not refcounted */
296# define SAVECOPSTASH_FREE(c) SAVESPTR(CopSTASH(c))
f4dd75d9
GS
297# define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c))
298# define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c))
57843af0
GS
299#endif
300
dea28490 301#define SAVECOPLINE(c) SAVEI32(CopLINE(c))
57843af0 302
455ece5e 303/* SSNEW() temporarily allocates a specified number of bytes of data on the
e42a763c 304 * savestack. It returns an I32 index into the savestack, because a
455ece5e
AD
305 * pointer would get broken if the savestack is moved on reallocation.
306 * SSNEWa() works like SSNEW(), but also aligns the data to the specified
307 * number of bytes. MEM_ALIGNBYTES is perhaps the most useful. The
486ec47a 308 * alignment will be preserved through savestack reallocation *only* if
a0288114 309 * realloc returns data aligned to a size divisible by "align"!
455ece5e
AD
310 *
311 * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
312 */
313
f2b2c1a7 314#define SSNEW(size) Perl_save_alloc(aTHX_ (size), 0)
02db2b7b 315#define SSNEWt(n,t) SSNEW((n)*sizeof(t))
f2b2c1a7 316#define SSNEWa(size,align) Perl_save_alloc(aTHX_ (size), \
6b99f28a 317 (I32)(align - ((size_t)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
02db2b7b 318#define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align)
455ece5e 319
02db2b7b
IZ
320#define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
321#define SSPTRt(off,type) ((type*) ((char*)PL_savestack + off))
455ece5e 322
2fd8beea
NC
323#define save_freesv(op) save_pushptr((void *)(op), SAVEt_FREESV)
324#define save_mortalizesv(op) save_pushptr((void *)(op), SAVEt_MORTALIZESV)
6702ba93 325
bb38a9e0 326# define save_freeop(op) \
6702ba93 327STMT_START { \
bb38a9e0 328 OP * const _o = (OP *)(op); \
6c7ae946 329 assert(!_o->op_savefree); \
bb38a9e0
FC
330 _o->op_savefree = 1; \
331 save_pushptr((void *)(_o), SAVEt_FREEOP); \
6702ba93 332 } STMT_END
2fd8beea
NC
333#define save_freepv(pv) save_pushptr((void *)(pv), SAVEt_FREEPV)
334#define save_op() save_pushptr((void *)(PL_op), SAVEt_OP)
335
e9a8c099 336/*
14d04a33 337 * ex: set ts=8 sts=4 sw=4 et:
e9a8c099 338 */