This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
more thorough cleaning of arenas--keep going until no more
[perl5.git] / scope.h
CommitLineData
b9d12d37
GS
1#define SAVEt_ITEM 0
2#define SAVEt_SV 1
3#define SAVEt_AV 2
4#define SAVEt_HV 3
5#define SAVEt_INT 4
6#define SAVEt_LONG 5
7#define SAVEt_I32 6
8#define SAVEt_IV 7
9#define SAVEt_SPTR 8
10#define SAVEt_APTR 9
11#define SAVEt_HPTR 10
12#define SAVEt_PPTR 11
13#define SAVEt_NSTAB 12
14#define SAVEt_SVREF 13
15#define SAVEt_GP 14
16#define SAVEt_FREESV 15
17#define SAVEt_FREEOP 16
18#define SAVEt_FREEPV 17
19#define SAVEt_CLEARSV 18
20#define SAVEt_DELETE 19
21#define SAVEt_DESTRUCTOR 20
22#define SAVEt_REGCONTEXT 21
23#define SAVEt_STACK_POS 22
24#define SAVEt_I16 23
25#define SAVEt_AELEM 24
26#define SAVEt_HELEM 25
27#define SAVEt_OP 26
28#define SAVEt_HINTS 27
29#define SAVEt_ALLOC 28
30#define SAVEt_GENERIC_SVREF 29
c76ac1ee 31#define SAVEt_DESTRUCTOR_X 30
7766f137 32#define SAVEt_VPTR 31
e8347627 33#define SAVEt_I8 32
354992b1 34#define SAVEt_COMPPAD 33
f4dd75d9 35#define SAVEt_GENERIC_PVREF 34
ccaecade 36#define SAVEt_PADSV 35
79072805 37
3280af22
NIS
38#define SSCHECK(need) if (PL_savestack_ix + need > PL_savestack_max) savestack_grow()
39#define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i))
40#define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i))
41#define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
42#define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p))
43#define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p))
c76ac1ee 44#define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
3280af22
NIS
45#define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
46#define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
47#define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
48#define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
49#define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
c76ac1ee 50#define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
8990e307 51
954c1994
GS
52/*
53=for apidoc Ams||SAVETMPS
54Opening bracket for temporaries on a callback. See C<FREETMPS> and
55L<perlcall>.
56
57=for apidoc Ams||FREETMPS
58Closing bracket for temporaries on a callback. See C<SAVETMPS> and
59L<perlcall>.
60
61=for apidoc Ams||ENTER
62Opening bracket on a callback. See C<LEAVE> and L<perlcall>.
63
64=for apidoc Ams||LEAVE
65Closing bracket on a callback. See C<ENTER> and L<perlcall>.
66
67=cut
68*/
69
3280af22
NIS
70#define SAVETMPS save_int((int*)&PL_tmps_floor), PL_tmps_floor = PL_tmps_ix
71#define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
a0d0e21e 72
f46d017c
GS
73#ifdef DEBUGGING
74#define ENTER \
75 STMT_START { \
76 push_scope(); \
cea2e8a9 77 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n", \
3280af22 78 PL_scopestack_ix, __FILE__, __LINE__))); \
f46d017c
GS
79 } STMT_END
80#define LEAVE \
81 STMT_START { \
cea2e8a9 82 DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n", \
3280af22 83 PL_scopestack_ix, __FILE__, __LINE__))); \
f46d017c
GS
84 pop_scope(); \
85 } STMT_END
86#else
a0d0e21e
LW
87#define ENTER push_scope()
88#define LEAVE pop_scope()
f46d017c 89#endif
3280af22 90#define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
a0d0e21e 91
55497cff 92/*
b9d12d37 93 * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV
55497cff 94 * because these are used for several kinds of pointer values
95 */
e8347627 96#define SAVEI8(i) save_I8(SOFT_CAST(I8*)&(i))
4fdae800 97#define SAVEI16(i) save_I16(SOFT_CAST(I16*)&(i))
98#define SAVEI32(i) save_I32(SOFT_CAST(I32*)&(i))
99#define SAVEINT(i) save_int(SOFT_CAST(int*)&(i))
100#define SAVEIV(i) save_iv(SOFT_CAST(IV*)&(i))
101#define SAVELONG(l) save_long(SOFT_CAST(long*)&(l))
55497cff 102#define SAVESPTR(s) save_sptr((SV**)&(s))
103#define SAVEPPTR(s) save_pptr(SOFT_CAST(char**)&(s))
57b2e452 104#define SAVEVPTR(s) save_vptr((void*)&(s))
ccaecade 105#define SAVEPADSV(s) save_padsv(s)
55497cff 106#define SAVEFREESV(s) save_freesv((SV*)(s))
107#define SAVEFREEOP(o) save_freeop(SOFT_CAST(OP*)(o))
108#define SAVEFREEPV(p) save_freepv(SOFT_CAST(char*)(p))
109#define SAVECLEARSV(sv) save_clearsv(SOFT_CAST(SV**)&(sv))
b9d12d37 110#define SAVEGENERICSV(s) save_generic_svref((SV**)&(s))
f4dd75d9 111#define SAVEGENERICPV(s) save_generic_pvref((char**)&(s))
55497cff 112#define SAVEDELETE(h,k,l) \
113 save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
114#define SAVEDESTRUCTOR(f,p) \
c76ac1ee
GS
115 save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), SOFT_CAST(void*)(p))
116
117#define SAVEDESTRUCTOR_X(f,p) \
118 save_destructor_x((DESTRUCTORFUNC_t)(f), SOFT_CAST(void*)(p))
25eaa213
GS
119
120#define SAVESTACK_POS() \
121 STMT_START { \
122 SSCHECK(2); \
3280af22 123 SSPUSHINT(PL_stack_sp - PL_stack_base); \
25eaa213
GS
124 SSPUSHINT(SAVEt_STACK_POS); \
125 } STMT_END
126
462e5cf6 127#define SAVEOP() save_op()
25eaa213
GS
128
129#define SAVEHINTS() \
130 STMT_START { \
3280af22 131 if (PL_hints & HINT_LOCALIZE_HH) \
25eaa213
GS
132 save_hints(); \
133 else { \
134 SSCHECK(2); \
3280af22 135 SSPUSHINT(PL_hints); \
25eaa213
GS
136 SSPUSHINT(SAVEt_HINTS); \
137 } \
138 } STMT_END
354992b1
GS
139
140#define SAVECOMPPAD() \
141 STMT_START { \
142 if (PL_comppad && PL_curpad == AvARRAY(PL_comppad)) { \
143 SSCHECK(2); \
144 SSPUSHPTR((SV*)PL_comppad); \
145 SSPUSHINT(SAVEt_COMPPAD); \
146 } \
147 else { \
148 SAVEVPTR(PL_curpad); \
149 SAVESPTR(PL_comppad); \
150 } \
151 } STMT_END
a9332b4a 152
57843af0 153#ifdef USE_ITHREADS
f4dd75d9
GS
154# define SAVECOPSTASH(c) SAVEPPTR(CopSTASHPV(c))
155# define SAVECOPSTASH_FREE(c) SAVEGENERICPV(CopSTASHPV(c))
156# define SAVECOPFILE(c) SAVEPPTR(CopFILE(c))
157# define SAVECOPFILE_FREE(c) SAVEGENERICPV(CopFILE(c))
57843af0 158#else
f4dd75d9
GS
159# define SAVECOPSTASH(c) SAVESPTR(CopSTASH(c))
160# define SAVECOPSTASH_FREE(c) SAVECOPSTASH(c) /* XXX not refcounted */
161# define SAVECOPFILE(c) SAVESPTR(CopFILEGV(c))
162# define SAVECOPFILE_FREE(c) SAVEGENERICSV(CopFILEGV(c))
57843af0
GS
163#endif
164
f4dd75d9 165#define SAVECOPLINE(c) SAVEI16(CopLINE(c))
57843af0 166
455ece5e
AD
167/* SSNEW() temporarily allocates a specified number of bytes of data on the
168 * savestack. It returns an integer index into the savestack, because a
169 * pointer would get broken if the savestack is moved on reallocation.
170 * SSNEWa() works like SSNEW(), but also aligns the data to the specified
171 * number of bytes. MEM_ALIGNBYTES is perhaps the most useful. The
172 * alignment will be preserved therough savestack reallocation *only* if
173 * realloc returns data aligned to a size divisible by `align'!
174 *
175 * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
176 */
177
0fe40b38 178#define SSNEW(size) Perl_save_alloc(aTHX_ (size), 0)
3c42a273 179#define SSNEWt(n,t) SSNEW((n)*sizeof(t))
0fe40b38 180#define SSNEWa(size,align) Perl_save_alloc(aTHX_ (size), \
455ece5e 181 (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
3c42a273 182#define SSNEWat(n,t,align) SSNEWa((n)*sizeof(t), align)
455ece5e 183
3c42a273
JH
184#define SSPTR(off,type) ((type) ((char*)PL_savestack + off))
185#define SSPTRt(off,type) ((type*) ((char*)PL_savestack + off))
455ece5e 186
54310121 187/* A jmpenv packages the state required to perform a proper non-local jump.
188 * Note that there is a start_env initialized when perl starts, and top_env
189 * points to this initially, so top_env should always be non-null.
190 *
191 * Existence of a non-null top_env->je_prev implies it is valid to call
6224f72b
GS
192 * longjmp() at that runlevel (we make sure start_env.je_prev is always
193 * null to ensure this).
54310121 194 *
195 * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
196 * establish a local jmpenv to handle exception traps. Care must be taken
197 * to restore the previous value of je_mustcatch before exiting the
198 * stack frame iff JMPENV_PUSH was not called in that stack frame.
6224f72b 199 * GSAR 97-03-27
54310121 200 */
201
202struct jmpenv {
203 struct jmpenv * je_prev;
312caa8e
CS
204 Sigjmp_buf je_buf; /* only for use if !je_throw */
205 int je_ret; /* last exception thrown */
206 bool je_mustcatch; /* need to call longjmp()? */
14dd3ad8 207#ifdef PERL_FLEXIBLE_EXCEPTIONS
312caa8e 208 void (*je_throw)(int v); /* last for bincompat */
db36c5a1 209 bool je_noset; /* no need for setjmp() */
14dd3ad8 210#endif
1163b5c4 211};
1163b5c4 212
6224f72b 213typedef struct jmpenv JMPENV;
1163b5c4 214
14dd3ad8
GS
215#ifdef OP_IN_REGISTER
216#define OP_REG_TO_MEM PL_opsave = op
217#define OP_MEM_TO_REG op = PL_opsave
218#else
219#define OP_REG_TO_MEM NOOP
220#define OP_MEM_TO_REG NOOP
221#endif
312caa8e
CS
222
223/*
224 * How to build the first jmpenv.
225 *
226 * top_env needs to be non-zero. It points to an area
227 * in which longjmp() stuff is stored, as C callstack
228 * info there at least is thread specific this has to
229 * be per-thread. Otherwise a 'die' in a thread gives
230 * that thread the C stack of last thread to do an eval {}!
231 */
232
233#define JMPENV_BOOTSTRAP \
234 STMT_START { \
14dd3ad8 235 Zero(&PL_start_env, 1, JMPENV); \
312caa8e
CS
236 PL_start_env.je_ret = -1; \
237 PL_start_env.je_mustcatch = TRUE; \
238 PL_top_env = &PL_start_env; \
239 } STMT_END
240
14dd3ad8 241#ifdef PERL_FLEXIBLE_EXCEPTIONS
462e5cf6 242
312caa8e
CS
243/*
244 * These exception-handling macros are split up to
245 * ease integration with C++ exceptions.
246 *
247 * To use C++ try+catch to catch Perl exceptions, an extension author
248 * needs to first write an extern "C" function to throw an appropriate
249 * exception object; typically it will be or contain an integer,
250 * because Perl's internals use integers to track exception types:
251 * extern "C" { static void thrower(int i) { throw i; } }
252 *
253 * Then (as shown below) the author needs to use, not the simple
254 * JMPENV_PUSH, but several of its constitutent macros, to arrange for
255 * the Perl internals to call thrower() rather than longjmp() to
256 * report exceptions:
257 *
258 * dJMPENV;
259 * JMPENV_PUSH_INIT(thrower);
260 * try {
261 * ... stuff that may throw exceptions ...
262 * }
263 * catch (int why) { // or whatever matches thrower()
264 * JMPENV_POST_CATCH;
265 * EXCEPT_SET(why);
266 * switch (why) {
267 * ... // handle various Perl exception codes
268 * }
269 * }
270 * JMPENV_POP; // don't forget this!
271 */
272
14dd3ad8
GS
273/*
274 * Function that catches/throws, and its callback for the
275 * body of protected processing.
276 */
277typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
278typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
279 int *, protect_body_t, ...);
280
db36c5a1
GS
281#define dJMPENV JMPENV cur_env; \
282 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
312caa8e 283
db36c5a1 284#define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
6224f72b 285 STMT_START { \
db36c5a1
GS
286 (ce).je_throw = (THROWFUNC); \
287 (ce).je_ret = -1; \
288 (ce).je_mustcatch = FALSE; \
289 (ce).je_prev = PL_top_env; \
290 PL_top_env = &(ce); \
6224f72b 291 OP_REG_TO_MEM; \
312caa8e 292 } STMT_END
1d651c14 293
0fe40b38 294#define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
1d651c14 295
db36c5a1 296#define JMPENV_POST_CATCH_ENV(ce) \
312caa8e 297 STMT_START { \
6224f72b 298 OP_MEM_TO_REG; \
db36c5a1 299 PL_top_env = &(ce); \
6224f72b 300 } STMT_END
312caa8e 301
db36c5a1 302#define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
1d651c14 303
db36c5a1
GS
304#define JMPENV_PUSH_ENV(ce,v) \
305 STMT_START { \
306 if (!(ce).je_noset) { \
14dd3ad8
GS
307 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
308 ce, PL_top_env)); \
db36c5a1
GS
309 JMPENV_PUSH_INIT_ENV(ce,NULL); \
310 EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, 1));\
311 (ce).je_noset = 1; \
312 } \
313 else \
314 EXCEPT_SET_ENV(ce,0); \
315 JMPENV_POST_CATCH_ENV(ce); \
316 (v) = EXCEPT_GET_ENV(ce); \
312caa8e
CS
317 } STMT_END
318
0fe40b38 319#define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
1d651c14 320
db36c5a1 321#define JMPENV_POP_ENV(ce) \
14dd3ad8
GS
322 STMT_START { \
323 if (PL_top_env == &(ce)) \
324 PL_top_env = (ce).je_prev; \
325 } STMT_END
312caa8e 326
0fe40b38 327#define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
1d651c14 328
22921e25
CS
329#define JMPENV_JUMP(v) \
330 STMT_START { \
462e5cf6 331 OP_REG_TO_MEM; \
312caa8e
CS
332 if (PL_top_env->je_prev) { \
333 if (PL_top_env->je_throw) \
334 PL_top_env->je_throw(v); \
335 else \
336 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
337 } \
6224f72b 338 if ((v) == 2) \
312caa8e 339 PerlProc_exit(STATUS_NATIVE_EXPORT); \
bf49b057 340 PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
312caa8e 341 PerlProc_exit(1); \
22921e25 342 } STMT_END
312caa8e 343
db36c5a1
GS
344#define EXCEPT_GET_ENV(ce) ((ce).je_ret)
345#define EXCEPT_GET EXCEPT_GET_ENV(*(JMPENV*)pcur_env)
346#define EXCEPT_SET_ENV(ce,v) ((ce).je_ret = (v))
347#define EXCEPT_SET(v) EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v)
312caa8e 348
14dd3ad8
GS
349#else /* !PERL_FLEXIBLE_EXCEPTIONS */
350
351#define dJMPENV JMPENV cur_env
352
353#define JMPENV_PUSH(v) \
354 STMT_START { \
355 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
356 &cur_env, PL_top_env)); \
357 cur_env.je_prev = PL_top_env; \
358 OP_REG_TO_MEM; \
359 cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, 1); \
360 OP_MEM_TO_REG; \
361 PL_top_env = &cur_env; \
362 cur_env.je_mustcatch = FALSE; \
363 (v) = cur_env.je_ret; \
364 } STMT_END
365
366#define JMPENV_POP \
367 STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
368
369#define JMPENV_JUMP(v) \
370 STMT_START { \
371 OP_REG_TO_MEM; \
372 if (PL_top_env->je_prev) \
373 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
374 if ((v) == 2) \
375 PerlProc_exit(STATUS_NATIVE_EXPORT); \
376 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n"); \
377 PerlProc_exit(1); \
378 } STMT_END
379
380#endif /* PERL_FLEXIBLE_EXCEPTIONS */
381
db36c5a1
GS
382#define CATCH_GET (PL_top_env->je_mustcatch)
383#define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))