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