8000 gh-130396: Move PYOS_LOG2_STACK_MARGIN to internal headers (#135928) · python/cpython@28940e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 28940e8

Browse files
authored
gh-130396: Move PYOS_LOG2_STACK_MARGIN to internal headers (#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.
1 parent 0e19db6 commit 28940e8

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
@@ -346,13 +346,13 @@ _Py_ReachedRecursionLimitWithMargin(PyThreadState *tstate, int margin_count)
346346
{
347347
uintptr_t here_addr = _Py_get_machine_stack_pointer();
348348
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
349-
if (here_addr > _tstate->c_stack_soft_limit + margin_count * PYOS_STACK_MARGIN_BYTES) {
349+
if (here_addr > _tstate->c_stack_soft_limit + margin_count * _PyOS_STACK_MARGIN_BYTES) {
350350
return 0;
351351
}
352352
if (_tstate->c_stack_hard_limit == 0) {
353353
_Py_InitializeRecursionLimits(tstate);
354354
}
355-
return here_addr <= _tstate->c_stack_soft_limit + margin_count * PYOS_STACK_MARGIN_BYTES;
355+
return here_addr <= _tstate->c_stack_soft_limit + margin_count * _PyOS_STACK_MARGIN_BYTES;
356356
}
357357

358358
void
@@ -448,8 +448,8 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate)
448448
_tstate->c_stack_top = (uintptr_t)high;
449449
ULONG guarantee = 0;
450450
SetThreadStackGuarantee(&guarantee);
451-
_tstate->c_stack_hard_limit = ((uintptr_t)low) + guarantee + PYOS_STACK_MARGIN_BYTES;
452-
_tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + PYOS_STACK_MARGIN_BYTES;
451+
_tstate->c_stack_hard_limit = ((uintptr_t)low) + guarantee + _PyOS_STACK_MARGIN_BYTES;
452+
_tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + _PyOS_STACK_MARGIN_BYTES;
453453
#else
454454
uintptr_t here_addr = _Py_get_machine_stack_pointer();
455455
# if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && !defined(__NetBSD__)
@@ -469,17 +469,17 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate)
469469
// Thread sanitizer crashes if we use a bit more than half the stack.
470470
_tstate->c_stack_soft_limit = base + (stack_size / 2);
471471
#else
472-
_tstate->c_stack_soft_limit = base + PYOS_STACK_MARGIN_BYTES * 2;
472+
_tstate->c_stack_soft_limit = base + _PyOS_STACK_MARGIN_BYTES * 2;
473473
#endif
474-
_tstate->c_stack_hard_limit = base + PYOS_STACK_MARGIN_BYTES;
474+
_tstate->c_stack_hard_limit = base + _PyOS_STACK_MARGIN_BYTES;
475475
assert(_tstate->c_stack_soft_limit < here_addr);
476476
assert(here_addr < _tstate->c_stack_top);
477477
return;
478478
}
479479
# endif
480480
_tstate->c_stack_top = _Py_SIZE_ROUND_UP(here_addr, 4096);
481481
_tstate->c_stack_soft_limit = _tstate->c_stack_top - Py_C_STACK_SIZE;
482-
_tstate->c_stack_hard_limit = _tstate->c_stack_top - (Py_C_STACK_SIZE + PYOS_STACK_MARGIN_BYTES);
482+
_tstate->c_stack_hard_limit = _tstate->c_stack_top - (Py_C_STACK_SIZE + _PyOS_STACK_MARGIN_BYTES);
483483
#endif
484484
}
485485

0 commit comments

Comments
 (0)
0