Skip to content

Commit f152d60

Browse files
[3.14] gh-130396: Move PYOS_LOG2_STACK_MARGIN to internal headers (GH-135928) (#136173)
gh-130396: Move PYOS_LOG2_STACK_MARGIN to internal headers (GH-135928) Move PYOS_LOG2_STACK_MARGIN, PYOS_STACK_MARGIN, PYOS_STACK_MARGIN_BYTES and PYOS_STACK_MARGIN_SHIFT macros to pycore_pythonrun.h internal header. Add underscore (_) prefix to the names to make them private. Rename _PYOS to _PyOS. (cherry picked from commit 28940e8) Co-authored-by: Victor Stinner <[email protected]>
1 parent df4e87b commit f152d60

File tree

5 files changed

+34
-32
lines changed

5 files changed

+34
-32
lines changed

Include/internal/pycore_pystate.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_pythonrun.h" // _PyOS_STACK_MARGIN_SHIFT
1112
#include "pycore_typedefs.h" // _PyRuntimeState
1213
#include "pycore_tstate.h"
1314

@@ -325,7 +326,7 @@ _Py_RecursionLimit_GetMargin(PyThreadState *tstate)
325326
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
326327
assert(_tstate->c_stack_hard_limit != 0);
327328
intptr_t here_addr = _Py_get_machine_stack_pointer();
328-
return Py_ARITHMETIC_RIGHT_SHIFT(intptr_t, here_addr - (intptr_t)_tstate->c_stack_soft_limit, PYOS_STACK_MARGIN_SHIFT);
329+
return Py_ARITHMETIC_RIGHT_SHIFT(intptr_t, here_addr - (intptr_t)_tstate->c_stack_soft_limit, _PyOS_STACK_MARGIN_SHIFT);
329330
}
330331

331332
#ifdef __cplusplus

Include/internal/pycore_pythonrun.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@ extern const char* _Py_SourceAsString(
3333
PyCompilerFlags *cf,
3434
PyObject **cmd_copy);
3535

36+
37+
/* Stack size, in "pointers". This must be large enough, so
38+
* no two calls to check recursion depth are more than this far
39+
* apart. In practice, that means it must be larger than the C
40+
* stack consumption of PyEval_EvalDefault */
41+
#if defined(_Py_ADDRESS_SANITIZER) || defined(_Py_THREAD_SANITIZER)
42+
# define _PyOS_LOG2_STACK_MARGIN 12
43+
#elif defined(Py_DEBUG) && defined(WIN32)
44+
# define _PyOS_LOG2_STACK_MARGIN 12
45+
#else
46+
# define _PyOS_LOG2_STACK_MARGIN 11
47+
#endif
48+
#define _PyOS_STACK_MARGIN (1 << _PyOS_LOG2_STACK_MARGIN)
49+
#define _PyOS_STACK_MARGIN_BYTES (_PyOS_STACK_MARGIN * sizeof(void *))
50+
51+
#if SIZEOF_VOID_P == 8
52+
# define _PyOS_STACK_MARGIN_SHIFT (_PyOS_LOG2_STACK_MARGIN + 3)
53+
#else
54+
# define _PyOS_STACK_MARGIN_SHIFT (_PyOS_LOG2_STACK_MARGIN + 2)
55+
#endif
56+
57+
3658
#ifdef __cplusplus
3759
}
3860
#endif

Include/pythonrun.h

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,15 @@ PyAPI_FUNC(void) PyErr_DisplayException(PyObject *);
2121
/* Stuff with no proper home (yet) */
2222
PyAPI_DATA(int) (*PyOS_InputHook)(void);
2323

24-
/* Stack size, in "pointers". This must be large enough, so
25-
* no two calls to check recursion depth are more than this far
26-
* apart. In practice, that means it must be larger than the C
27-
* stack consumption of PyEval_EvalDefault */
28-
#if defined(_Py_ADDRESS_SANITIZER) || defined(_Py_THREAD_SANITIZER)
29-
# define PYOS_LOG2_STACK_MARGIN 12
30-
#elif defined(Py_DEBUG) && defined(WIN32)
31-
# define PYOS_LOG2_STACK_MARGIN 12
32-
#else
33-
# define PYOS_LOG2_STACK_MARGIN 11
34-
#endif
35-
#define PYOS_STACK_MARGIN (1 << PYOS_LOG2_STACK_MARGIN)
36-
#define PYOS_STACK_MARGIN_BYTES (PYOS_STACK_MARGIN * sizeof(void *))
37-
38-
#if SIZEOF_VOID_P == 8
39-
#define PYOS_STACK_MARGIN_SHIFT (PYOS_LOG2_STACK_MARGIN + 3)
40-
#else
41-
#define PYOS_STACK_MARGIN_SHIFT (PYOS_LOG2_STACK_MARGIN + 2)
42-
#endif
43-
44-
4524
#if defined(WIN32)
46-
#define USE_STACKCHECK
25+
# define USE_STACKCHECK
4726
#endif
48-
4927
#ifdef USE_STACKCHECK
5028
/* Check that we aren't overflowing our stack */
5129
PyAPI_FUNC(int) PyOS_CheckStack(void);
5230
#endif
5331

32+
5433
#ifndef Py_LIMITED_API
5534
# define Py_CPYTHON_PYTHONRUN_H
5635
# include "cpython/pythonrun.h"

Modules/_testinternalcapi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ get_c_recursion_remaining(PyObject *self, PyObject *Py_UNUSED(args))
121121
PyThreadState *tstate = _PyThreadState_GET();
122122
uintptr_t here_addr = _Py_get_machine_stack_pointer();
123123
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
124-
int remaining = (int)((here_addr - _tstate->c_stack_soft_limit)/PYOS_STACK_MARGIN_BYTES * 50);
124+
int remaining = (int)((here_addr - _tstate->c_stack_soft_limit) / _PyOS_STACK_MARGIN_BYTES * 50);
125125
return PyLong_FromLong(remaining);
126126
}
127127

Python/ceval.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,13 @@ _Py_ReachedRecursionLimitWithMargin(PyThreadState *tstate, int margin_count)
333333
{
334334
uintptr_t here_addr = _Py_get_machine_stack_pointer();
335335
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
336-
if (here_addr > _tstate->c_stack_soft_limit + margin_count * PYOS_STACK_MARGIN_BYTES) {
336+
if (here_addr > _tstate->c_stack_soft_limit + margin_count * _PyOS_STACK_MARGIN_BYTES) {
337337
return 0;
338338
}
339339
if (_tstate->c_stack_hard_limit == 0) {
340340
_Py_InitializeRecursionLimits(tstate);
341341
}
342-
return here_addr <= _tstate->c_stack_soft_limit + margin_count * PYOS_STACK_MARGIN_BYTES;
342+
return here_addr <= _tstate->c_stack_soft_limit + margin_count * _PyOS_STACK_MARGIN_BYTES;
343343
}
344344

345345
void
@@ -435,8 +435,8 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate)
435435
_tstate->c_stack_top = (uintptr_t)high;
436436
ULONG guarantee = 0;
437437
SetThreadStackGuarantee(&guarantee);
438-
_tstate->c_stack_hard_limit = ((uintptr_t)low) + guarantee + PYOS_STACK_MARGIN_BYTES;
439-
_tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + PYOS_STACK_MARGIN_BYTES;
438+
_tstate->c_stack_hard_limit = ((uintptr_t)low) + guarantee + _PyOS_STACK_MARGIN_BYTES;
439+
_tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + _PyOS_STACK_MARGIN_BYTES;
440440
#else
441441
uintptr_t here_addr = _Py_get_machine_stack_pointer();
442442
# if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && !defined(__NetBSD__)
@@ -456,17 +456,17 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate)
456456
// Thread sanitizer crashes if we use a bit more than half the stack.
457457
_tstate->c_stack_soft_limit = base + (stack_size / 2);
458458
#else
459-
_tstate->c_stack_soft_limit = base + PYOS_STACK_MARGIN_BYTES * 2;
459+
_tstate->c_stack_soft_limit = base + _PyOS_STACK_MARGIN_BYTES * 2;
460460
#endif
461-
_tstate->c_stack_hard_limit = base + PYOS_STACK_MARGIN_BYTES;
461+
_tstate->c_stack_hard_limit = base + _PyOS_STACK_MARGIN_BYTES;
462462
assert(_tstate->c_stack_soft_limit < here_addr);
463463
assert(here_addr < _tstate->c_stack_top);
464464
return;
465465
}
466466
# endif
467467
_tstate->c_stack_top = _Py_SIZE_ROUND_UP(here_addr, 4096);
468468
_tstate->c_stack_soft_limit = _tstate->c_stack_top - Py_C_STACK_SIZE;
469-
_tstate->c_stack_hard_limit = _tstate->c_stack_top - (Py_C_STACK_SIZE + PYOS_STACK_MARGIN_BYTES);
469+
_tstate->c_stack_hard_limit = _tstate->c_stack_top - (Py_C_STACK_SIZE + _PyOS_STACK_MARGIN_BYTES);
470470
#endif
471471
}
472472

0 commit comments

Comments
 (0)