Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 1 | #ifndef Py_CPYTHON_CEVAL_H |
| 2 | # error "this header file must not be included directly" |
| 3 | #endif |
| 4 | |
| 5 | #ifdef __cplusplus |
| 6 | extern "C" { |
| 7 | #endif |
| 8 | |
| 9 | PyAPI_DATA(int) _Py_CheckRecursionLimit; |
| 10 | |
| 11 | #ifdef USE_STACKCHECK |
| 12 | /* With USE_STACKCHECK macro defined, trigger stack checks in |
| 13 | _Py_CheckRecursiveCall() on every 64th call to Py_EnterRecursiveCall. */ |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 14 | static inline int _Py_MakeRecCheck(PyThreadState *tstate) { |
| 15 | return (++tstate->recursion_depth > _Py_CheckRecursionLimit |
| 16 | || ++tstate->stackcheck_counter > 64); |
| 17 | } |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 18 | #else |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 19 | static inline int _Py_MakeRecCheck(PyThreadState *tstate) { |
| 20 | return (++tstate->recursion_depth > _Py_CheckRecursionLimit); |
| 21 | } |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 22 | #endif |
| 23 | |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 24 | PyAPI_FUNC(int) _Py_CheckRecursiveCall( |
| 25 | PyThreadState *tstate, |
| 26 | const char *where); |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 27 | |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 28 | static inline int _Py_EnterRecursiveCall(PyThreadState *tstate, |
| 29 | const char *where) { |
| 30 | return (_Py_MakeRecCheck(tstate) && _Py_CheckRecursiveCall(tstate, where)); |
| 31 | } |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 32 | |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 33 | static inline int _Py_EnterRecursiveCall_inline(const char *where) { |
| 34 | PyThreadState *tstate = PyThreadState_GET(); |
| 35 | return _Py_EnterRecursiveCall(tstate, where); |
| 36 | } |
| 37 | |
| 38 | #define Py_EnterRecursiveCall(where) _Py_EnterRecursiveCall_inline(where) |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 39 | |
| 40 | |
| 41 | /* Compute the "lower-water mark" for a recursion limit. When |
| 42 | * Py_LeaveRecursiveCall() is called with a recursion depth below this mark, |
| 43 | * the overflowed flag is reset to 0. */ |
| 44 | #define _Py_RecursionLimitLowerWaterMark(limit) \ |
| 45 | (((limit) > 200) \ |
| 46 | ? ((limit) - 50) \ |
| 47 | : (3 * ((limit) >> 2))) |
| 48 | |
| 49 | #define _Py_MakeEndRecCheck(x) \ |
| 50 | (--(x) < _Py_RecursionLimitLowerWaterMark(_Py_CheckRecursionLimit)) |
| 51 | |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 52 | static inline void _Py_LeaveRecursiveCall(PyThreadState *tstate) { |
| 53 | if (_Py_MakeEndRecCheck(tstate->recursion_depth)) { |
| 54 | tstate->overflowed = 0; |
| 55 | } |
| 56 | } |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 57 | |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 58 | static inline void _Py_LeaveRecursiveCall_inline(void) { |
| 59 | PyThreadState *tstate = PyThreadState_GET(); |
| 60 | _Py_LeaveRecursiveCall(tstate); |
| 61 | } |
| 62 | |
| 63 | #define Py_LeaveRecursiveCall() _Py_LeaveRecursiveCall_inline() |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 64 | |
| 65 | #ifdef __cplusplus |
| 66 | } |
| 67 | #endif |